exec.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602
  1. /*
  2. +----------------------------------------------------------------------+
  3. | PHP Version 7 |
  4. +----------------------------------------------------------------------+
  5. | Copyright (c) 1997-2018 The PHP Group |
  6. +----------------------------------------------------------------------+
  7. | This source file is subject to version 3.01 of the PHP license, |
  8. | that is bundled with this package in the file LICENSE, and is |
  9. | available through the world-wide-web at the following url: |
  10. | http://www.php.net/license/3_01.txt |
  11. | If you did not receive a copy of the PHP license and are unable to |
  12. | obtain it through the world-wide-web, please send a note to |
  13. | license@php.net so we can mail you a copy immediately. |
  14. +----------------------------------------------------------------------+
  15. | Author: Rasmus Lerdorf <rasmus@php.net> |
  16. | Ilia Alshanetsky <iliaa@php.net> |
  17. +----------------------------------------------------------------------+
  18. */
  19. #include <stdio.h>
  20. #include "php.h"
  21. #include <ctype.h>
  22. #include "php_string.h"
  23. #include "ext/standard/head.h"
  24. #include "ext/standard/file.h"
  25. #include "basic_functions.h"
  26. #include "exec.h"
  27. #include "php_globals.h"
  28. #include "SAPI.h"
  29. #if HAVE_SYS_WAIT_H
  30. #include <sys/wait.h>
  31. #endif
  32. #if HAVE_SIGNAL_H
  33. #include <signal.h>
  34. #endif
  35. #if HAVE_SYS_TYPES_H
  36. #include <sys/types.h>
  37. #endif
  38. #if HAVE_SYS_STAT_H
  39. #include <sys/stat.h>
  40. #endif
  41. #if HAVE_FCNTL_H
  42. #include <fcntl.h>
  43. #endif
  44. #if HAVE_UNISTD_H
  45. #include <unistd.h>
  46. #endif
  47. #if HAVE_LIMITS_H
  48. #include <limits.h>
  49. #endif
  50. #ifdef PHP_WIN32
  51. # include "win32/nice.h"
  52. #endif
  53. static size_t cmd_max_len;
  54. /* {{{ PHP_MINIT_FUNCTION(exec) */
  55. PHP_MINIT_FUNCTION(exec)
  56. {
  57. #ifdef _SC_ARG_MAX
  58. cmd_max_len = sysconf(_SC_ARG_MAX);
  59. if ((size_t)-1 == cmd_max_len) {
  60. #ifdef _POSIX_ARG_MAX
  61. cmd_max_len = _POSIX_ARG_MAX;
  62. #else
  63. cmd_max_len = 4096;
  64. #endif
  65. }
  66. #elif defined(ARG_MAX)
  67. cmd_max_len = ARG_MAX;
  68. #elif defined(PHP_WIN32)
  69. /* Executed commands will run through cmd.exe. As long as it's the case,
  70. it's just the constant limit.*/
  71. cmd_max_len = 8192;
  72. #else
  73. /* This is just an arbitrary value for the fallback case. */
  74. cmd_max_len = 4096;
  75. #endif
  76. return SUCCESS;
  77. }
  78. /* }}} */
  79. /* {{{ php_exec
  80. * If type==0, only last line of output is returned (exec)
  81. * If type==1, all lines will be printed and last lined returned (system)
  82. * If type==2, all lines will be saved to given array (exec with &$array)
  83. * If type==3, output will be printed binary, no lines will be saved or returned (passthru)
  84. *
  85. */
  86. PHPAPI int php_exec(int type, char *cmd, zval *array, zval *return_value)
  87. {
  88. FILE *fp;
  89. char *buf;
  90. size_t l = 0;
  91. int pclose_return;
  92. char *b, *d=NULL;
  93. php_stream *stream;
  94. size_t buflen, bufl = 0;
  95. #if PHP_SIGCHILD
  96. void (*sig_handler)() = NULL;
  97. #endif
  98. #if PHP_SIGCHILD
  99. sig_handler = signal (SIGCHLD, SIG_DFL);
  100. #endif
  101. #ifdef PHP_WIN32
  102. fp = VCWD_POPEN(cmd, "rb");
  103. #else
  104. fp = VCWD_POPEN(cmd, "r");
  105. #endif
  106. if (!fp) {
  107. php_error_docref(NULL, E_WARNING, "Unable to fork [%s]", cmd);
  108. goto err;
  109. }
  110. stream = php_stream_fopen_from_pipe(fp, "rb");
  111. buf = (char *) emalloc(EXEC_INPUT_BUF);
  112. buflen = EXEC_INPUT_BUF;
  113. if (type != 3) {
  114. b = buf;
  115. while (php_stream_get_line(stream, b, EXEC_INPUT_BUF, &bufl)) {
  116. /* no new line found, let's read some more */
  117. if (b[bufl - 1] != '\n' && !php_stream_eof(stream)) {
  118. if (buflen < (bufl + (b - buf) + EXEC_INPUT_BUF)) {
  119. bufl += b - buf;
  120. buflen = bufl + EXEC_INPUT_BUF;
  121. buf = erealloc(buf, buflen);
  122. b = buf + bufl;
  123. } else {
  124. b += bufl;
  125. }
  126. continue;
  127. } else if (b != buf) {
  128. bufl += b - buf;
  129. }
  130. if (type == 1) {
  131. PHPWRITE(buf, bufl);
  132. if (php_output_get_level() < 1) {
  133. sapi_flush();
  134. }
  135. } else if (type == 2) {
  136. /* strip trailing whitespaces */
  137. l = bufl;
  138. while (l-- > 0 && isspace(((unsigned char *)buf)[l]));
  139. if (l != (bufl - 1)) {
  140. bufl = l + 1;
  141. buf[bufl] = '\0';
  142. }
  143. add_next_index_stringl(array, buf, bufl);
  144. }
  145. b = buf;
  146. }
  147. if (bufl) {
  148. /* output remaining data in buffer */
  149. if (type == 1 && buf != b) {
  150. PHPWRITE(buf, bufl);
  151. if (php_output_get_level() < 1) {
  152. sapi_flush();
  153. }
  154. }
  155. /* strip trailing whitespaces if we have not done so already */
  156. if ((type == 2 && buf != b) || type != 2) {
  157. l = bufl;
  158. while (l-- > 0 && isspace(((unsigned char *)buf)[l]));
  159. if (l != (bufl - 1)) {
  160. bufl = l + 1;
  161. buf[bufl] = '\0';
  162. }
  163. if (type == 2) {
  164. add_next_index_stringl(array, buf, bufl);
  165. }
  166. }
  167. /* Return last line from the shell command */
  168. RETVAL_STRINGL(buf, bufl);
  169. } else { /* should return NULL, but for BC we return "" */
  170. RETVAL_EMPTY_STRING();
  171. }
  172. } else {
  173. while((bufl = php_stream_read(stream, buf, EXEC_INPUT_BUF)) > 0) {
  174. PHPWRITE(buf, bufl);
  175. }
  176. }
  177. pclose_return = php_stream_close(stream);
  178. efree(buf);
  179. done:
  180. #if PHP_SIGCHILD
  181. if (sig_handler) {
  182. signal(SIGCHLD, sig_handler);
  183. }
  184. #endif
  185. if (d) {
  186. efree(d);
  187. }
  188. return pclose_return;
  189. err:
  190. pclose_return = -1;
  191. goto done;
  192. }
  193. /* }}} */
  194. static void php_exec_ex(INTERNAL_FUNCTION_PARAMETERS, int mode) /* {{{ */
  195. {
  196. char *cmd;
  197. size_t cmd_len;
  198. zval *ret_code=NULL, *ret_array=NULL;
  199. int ret;
  200. ZEND_PARSE_PARAMETERS_START(1, (mode ? 2 : 3))
  201. Z_PARAM_STRING(cmd, cmd_len)
  202. Z_PARAM_OPTIONAL
  203. if (!mode) {
  204. Z_PARAM_ZVAL_DEREF(ret_array)
  205. }
  206. Z_PARAM_ZVAL_DEREF(ret_code)
  207. ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
  208. if (!cmd_len) {
  209. php_error_docref(NULL, E_WARNING, "Cannot execute a blank command");
  210. RETURN_FALSE;
  211. }
  212. if (strlen(cmd) != cmd_len) {
  213. php_error_docref(NULL, E_WARNING, "NULL byte detected. Possible attack");
  214. RETURN_FALSE;
  215. }
  216. if (!ret_array) {
  217. ret = php_exec(mode, cmd, NULL, return_value);
  218. } else {
  219. if (Z_TYPE_P(ret_array) != IS_ARRAY) {
  220. zval_ptr_dtor(ret_array);
  221. array_init(ret_array);
  222. } else if (Z_REFCOUNT_P(ret_array) > 1) {
  223. zval_ptr_dtor(ret_array);
  224. ZVAL_ARR(ret_array, zend_array_dup(Z_ARR_P(ret_array)));
  225. }
  226. ret = php_exec(2, cmd, ret_array, return_value);
  227. }
  228. if (ret_code) {
  229. zval_ptr_dtor(ret_code);
  230. ZVAL_LONG(ret_code, ret);
  231. }
  232. }
  233. /* }}} */
  234. /* {{{ proto string exec(string command [, array &output [, int &return_value]])
  235. Execute an external program */
  236. PHP_FUNCTION(exec)
  237. {
  238. php_exec_ex(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
  239. }
  240. /* }}} */
  241. /* {{{ proto int system(string command [, int &return_value])
  242. Execute an external program and display output */
  243. PHP_FUNCTION(system)
  244. {
  245. php_exec_ex(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
  246. }
  247. /* }}} */
  248. /* {{{ proto void passthru(string command [, int &return_value])
  249. Execute an external program and display raw output */
  250. PHP_FUNCTION(passthru)
  251. {
  252. php_exec_ex(INTERNAL_FUNCTION_PARAM_PASSTHRU, 3);
  253. }
  254. /* }}} */
  255. /* {{{ php_escape_shell_cmd
  256. Escape all chars that could possibly be used to
  257. break out of a shell command
  258. This function emalloc's a string and returns the pointer.
  259. Remember to efree it when done with it.
  260. *NOT* safe for binary strings
  261. */
  262. PHPAPI zend_string *php_escape_shell_cmd(char *str)
  263. {
  264. register size_t x, y;
  265. size_t l = strlen(str);
  266. uint64_t estimate = (2 * (uint64_t)l) + 1;
  267. zend_string *cmd;
  268. #ifndef PHP_WIN32
  269. char *p = NULL;
  270. #endif
  271. /* max command line length - two single quotes - \0 byte length */
  272. if (l > cmd_max_len - 2 - 1) {
  273. php_error_docref(NULL, E_ERROR, "Command exceeds the allowed length of %zu bytes", cmd_max_len);
  274. return ZSTR_EMPTY_ALLOC();
  275. }
  276. cmd = zend_string_safe_alloc(2, l, 0, 0);
  277. for (x = 0, y = 0; x < l; x++) {
  278. int mb_len = php_mblen(str + x, (l - x));
  279. /* skip non-valid multibyte characters */
  280. if (mb_len < 0) {
  281. continue;
  282. } else if (mb_len > 1) {
  283. memcpy(ZSTR_VAL(cmd) + y, str + x, mb_len);
  284. y += mb_len;
  285. x += mb_len - 1;
  286. continue;
  287. }
  288. switch (str[x]) {
  289. #ifndef PHP_WIN32
  290. case '"':
  291. case '\'':
  292. if (!p && (p = memchr(str + x + 1, str[x], l - x - 1))) {
  293. /* noop */
  294. } else if (p && *p == str[x]) {
  295. p = NULL;
  296. } else {
  297. ZSTR_VAL(cmd)[y++] = '\\';
  298. }
  299. ZSTR_VAL(cmd)[y++] = str[x];
  300. break;
  301. #else
  302. /* % is Windows specific for environmental variables, ^%PATH% will
  303. output PATH while ^%PATH^% will not. escapeshellcmd->val will escape all % and !.
  304. */
  305. case '%':
  306. case '!':
  307. case '"':
  308. case '\'':
  309. #endif
  310. case '#': /* This is character-set independent */
  311. case '&':
  312. case ';':
  313. case '`':
  314. case '|':
  315. case '*':
  316. case '?':
  317. case '~':
  318. case '<':
  319. case '>':
  320. case '^':
  321. case '(':
  322. case ')':
  323. case '[':
  324. case ']':
  325. case '{':
  326. case '}':
  327. case '$':
  328. case '\\':
  329. case '\x0A': /* excluding these two */
  330. case '\xFF':
  331. #ifdef PHP_WIN32
  332. ZSTR_VAL(cmd)[y++] = '^';
  333. #else
  334. ZSTR_VAL(cmd)[y++] = '\\';
  335. #endif
  336. /* fall-through */
  337. default:
  338. ZSTR_VAL(cmd)[y++] = str[x];
  339. }
  340. }
  341. ZSTR_VAL(cmd)[y] = '\0';
  342. if (y > cmd_max_len + 1) {
  343. php_error_docref(NULL, E_ERROR, "Escaped command exceeds the allowed length of %zu bytes", cmd_max_len);
  344. zend_string_release_ex(cmd, 0);
  345. return ZSTR_EMPTY_ALLOC();
  346. }
  347. if ((estimate - y) > 4096) {
  348. /* realloc if the estimate was way overill
  349. * Arbitrary cutoff point of 4096 */
  350. cmd = zend_string_truncate(cmd, y, 0);
  351. }
  352. ZSTR_LEN(cmd) = y;
  353. return cmd;
  354. }
  355. /* }}} */
  356. /* {{{ php_escape_shell_arg
  357. */
  358. PHPAPI zend_string *php_escape_shell_arg(char *str)
  359. {
  360. size_t x, y = 0;
  361. size_t l = strlen(str);
  362. zend_string *cmd;
  363. uint64_t estimate = (4 * (uint64_t)l) + 3;
  364. /* max command line length - two single quotes - \0 byte length */
  365. if (l > cmd_max_len - 2 - 1) {
  366. php_error_docref(NULL, E_ERROR, "Argument exceeds the allowed length of %zu bytes", cmd_max_len);
  367. return ZSTR_EMPTY_ALLOC();
  368. }
  369. cmd = zend_string_safe_alloc(4, l, 2, 0); /* worst case */
  370. #ifdef PHP_WIN32
  371. ZSTR_VAL(cmd)[y++] = '"';
  372. #else
  373. ZSTR_VAL(cmd)[y++] = '\'';
  374. #endif
  375. for (x = 0; x < l; x++) {
  376. int mb_len = php_mblen(str + x, (l - x));
  377. /* skip non-valid multibyte characters */
  378. if (mb_len < 0) {
  379. continue;
  380. } else if (mb_len > 1) {
  381. memcpy(ZSTR_VAL(cmd) + y, str + x, mb_len);
  382. y += mb_len;
  383. x += mb_len - 1;
  384. continue;
  385. }
  386. switch (str[x]) {
  387. #ifdef PHP_WIN32
  388. case '"':
  389. case '%':
  390. case '!':
  391. ZSTR_VAL(cmd)[y++] = ' ';
  392. break;
  393. #else
  394. case '\'':
  395. ZSTR_VAL(cmd)[y++] = '\'';
  396. ZSTR_VAL(cmd)[y++] = '\\';
  397. ZSTR_VAL(cmd)[y++] = '\'';
  398. #endif
  399. /* fall-through */
  400. default:
  401. ZSTR_VAL(cmd)[y++] = str[x];
  402. }
  403. }
  404. #ifdef PHP_WIN32
  405. if (y > 0 && '\\' == ZSTR_VAL(cmd)[y - 1]) {
  406. int k = 0, n = y - 1;
  407. for (; n >= 0 && '\\' == ZSTR_VAL(cmd)[n]; n--, k++);
  408. if (k % 2) {
  409. ZSTR_VAL(cmd)[y++] = '\\';
  410. }
  411. }
  412. ZSTR_VAL(cmd)[y++] = '"';
  413. #else
  414. ZSTR_VAL(cmd)[y++] = '\'';
  415. #endif
  416. ZSTR_VAL(cmd)[y] = '\0';
  417. if (y > cmd_max_len + 1) {
  418. php_error_docref(NULL, E_ERROR, "Escaped argument exceeds the allowed length of %zu bytes", cmd_max_len);
  419. zend_string_release_ex(cmd, 0);
  420. return ZSTR_EMPTY_ALLOC();
  421. }
  422. if ((estimate - y) > 4096) {
  423. /* realloc if the estimate was way overill
  424. * Arbitrary cutoff point of 4096 */
  425. cmd = zend_string_truncate(cmd, y, 0);
  426. }
  427. ZSTR_LEN(cmd) = y;
  428. return cmd;
  429. }
  430. /* }}} */
  431. /* {{{ proto string escapeshellcmd(string command)
  432. Escape shell metacharacters */
  433. PHP_FUNCTION(escapeshellcmd)
  434. {
  435. char *command;
  436. size_t command_len;
  437. ZEND_PARSE_PARAMETERS_START(1, 1)
  438. Z_PARAM_STRING(command, command_len)
  439. ZEND_PARSE_PARAMETERS_END();
  440. if (command_len) {
  441. if (command_len != strlen(command)) {
  442. php_error_docref(NULL, E_ERROR, "Input string contains NULL bytes");
  443. return;
  444. }
  445. RETVAL_STR(php_escape_shell_cmd(command));
  446. } else {
  447. RETVAL_EMPTY_STRING();
  448. }
  449. }
  450. /* }}} */
  451. /* {{{ proto string escapeshellarg(string arg)
  452. Quote and escape an argument for use in a shell command */
  453. PHP_FUNCTION(escapeshellarg)
  454. {
  455. char *argument;
  456. size_t argument_len;
  457. ZEND_PARSE_PARAMETERS_START(1, 1)
  458. Z_PARAM_STRING(argument, argument_len)
  459. ZEND_PARSE_PARAMETERS_END();
  460. if (argument) {
  461. if (argument_len != strlen(argument)) {
  462. php_error_docref(NULL, E_ERROR, "Input string contains NULL bytes");
  463. return;
  464. }
  465. RETVAL_STR(php_escape_shell_arg(argument));
  466. }
  467. }
  468. /* }}} */
  469. /* {{{ proto string shell_exec(string cmd)
  470. Execute command via shell and return complete output as string */
  471. PHP_FUNCTION(shell_exec)
  472. {
  473. FILE *in;
  474. char *command;
  475. size_t command_len;
  476. zend_string *ret;
  477. php_stream *stream;
  478. ZEND_PARSE_PARAMETERS_START(1, 1)
  479. Z_PARAM_STRING(command, command_len)
  480. ZEND_PARSE_PARAMETERS_END();
  481. if (!command_len) {
  482. php_error_docref(NULL, E_WARNING, "Cannot execute a blank command");
  483. RETURN_FALSE;
  484. }
  485. if (strlen(command) != command_len) {
  486. php_error_docref(NULL, E_WARNING, "NULL byte detected. Possible attack");
  487. RETURN_FALSE;
  488. }
  489. #ifdef PHP_WIN32
  490. if ((in=VCWD_POPEN(command, "rt"))==NULL) {
  491. #else
  492. if ((in=VCWD_POPEN(command, "r"))==NULL) {
  493. #endif
  494. php_error_docref(NULL, E_WARNING, "Unable to execute '%s'", command);
  495. RETURN_FALSE;
  496. }
  497. stream = php_stream_fopen_from_pipe(in, "rb");
  498. ret = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0);
  499. php_stream_close(stream);
  500. if (ret && ZSTR_LEN(ret) > 0) {
  501. RETVAL_STR(ret);
  502. }
  503. }
  504. /* }}} */
  505. #ifdef HAVE_NICE
  506. /* {{{ proto bool proc_nice(int priority)
  507. Change the priority of the current process */
  508. PHP_FUNCTION(proc_nice)
  509. {
  510. zend_long pri;
  511. ZEND_PARSE_PARAMETERS_START(1, 1)
  512. Z_PARAM_LONG(pri)
  513. ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
  514. errno = 0;
  515. php_ignore_value(nice(pri));
  516. if (errno) {
  517. #ifdef PHP_WIN32
  518. php_error_docref(NULL, E_WARNING, "%s", php_win_err());
  519. #else
  520. php_error_docref(NULL, E_WARNING, "Only a super user may attempt to increase the priority of a process");
  521. #endif
  522. RETURN_FALSE;
  523. }
  524. RETURN_TRUE;
  525. }
  526. /* }}} */
  527. #endif
  528. /*
  529. * Local variables:
  530. * tab-width: 4
  531. * c-basic-offset: 4
  532. * End:
  533. * vim600: sw=4 ts=4 fdm=marker
  534. * vim<600: sw=4 ts=4
  535. */