Download | Plain Text | No Line Numbers


  1. This patch is based on http://kyberdigi.cz/projects/execdir/ but instead trying
  2. to sanitize the exec command string inside PHP this job is handled by bash and
  3. a special restricted PHP-mode which strips off all path informations.
  4.  
  5. This patch depends on:
  6. - bash restricted php mode patch
  7. https://manuel.mausz.at/coding/patches/php/bash-4.2-restricted-php.patch
  8.  
  9. No warranty!
  10. diff -Naur php-7.4.4.orig/ext/standard/exec.c php-7.4.4/ext/standard/exec.c
  11. --- php-7.4.4.orig/ext/standard/exec.c 2020-03-17 11:40:33.000000000 +0100
  12. +++ php-7.4.4/ext/standard/exec.c 2020-03-19 14:36:43.456447593 +0100
  13. @@ -96,21 +96,30 @@
  14. char *buf;
  15. size_t l = 0;
  16. int pclose_return;
  17. - char *b, *d=NULL;
  18. + char *cmd_p, *b, *d=NULL;
  19. php_stream *stream;
  20. size_t buflen, bufl = 0;
  21. #if PHP_SIGCHILD
  22. void (*sig_handler)() = NULL;
  23. #endif
  24.  
  25. + if (PG(exec_dir) && strlen(PG(exec_dir))) {
  26. + zend_string *cmd_esc = php_escape_shell_arg(cmd);
  27. + spprintf(&d, 0, "PATH=%s /bin/bash --php -rc %s", PG(exec_dir), ZSTR_VAL(cmd_esc));
  28. + zend_string_release(cmd_esc);
  29. + cmd_p = d;
  30. + } else {
  31. + cmd_p = cmd;
  32. + }
  33. +
  34. #if PHP_SIGCHILD
  35. sig_handler = signal (SIGCHLD, SIG_DFL);
  36. #endif
  37.  
  38. #ifdef PHP_WIN32
  39. - fp = VCWD_POPEN(cmd, "rb");
  40. + fp = VCWD_POPEN(cmd_p, "rb");
  41. #else
  42. - fp = VCWD_POPEN(cmd, "r");
  43. + fp = VCWD_POPEN(cmd_p, "r");
  44. #endif
  45. if (!fp) {
  46. php_error_docref(NULL, E_WARNING, "Unable to fork [%s]", cmd);
  47. @@ -521,7 +530,7 @@
  48. PHP_FUNCTION(shell_exec)
  49. {
  50. FILE *in;
  51. - char *command;
  52. + char *command, *command_p;
  53. size_t command_len;
  54. zend_string *ret;
  55. php_stream *stream;
  56. @@ -530,14 +539,24 @@
  57. Z_PARAM_STRING(command, command_len)
  58. ZEND_PARSE_PARAMETERS_END();
  59.  
  60. + if (PG(exec_dir) && strlen(PG(exec_dir))) {
  61. + zend_string *cmd_esc = php_escape_shell_arg(command);
  62. + spprintf(&command_p, 0, "PATH=%s /bin/bash --php -rc %s", PG(exec_dir), ZSTR_VAL(cmd_esc));
  63. + zend_string_release(cmd_esc);
  64. + } else {
  65. + command_p = estrdup(command);
  66. + }
  67. +
  68. #ifdef PHP_WIN32
  69. - if ((in=VCWD_POPEN(command, "rt"))==NULL) {
  70. + if ((in=VCWD_POPEN(command_p, "rt"))==NULL) {
  71. #else
  72. - if ((in=VCWD_POPEN(command, "r"))==NULL) {
  73. + if ((in=VCWD_POPEN(command_p, "r"))==NULL) {
  74. #endif
  75. php_error_docref(NULL, E_WARNING, "Unable to execute '%s'", command);
  76. + efree(command_p);
  77. RETURN_FALSE;
  78. }
  79. + efree(command_p);
  80.  
  81. stream = php_stream_fopen_from_pipe(in, "rb");
  82. ret = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0);
  83. diff -Naur php-7.4.4.orig/ext/standard/file.c php-7.4.4/ext/standard/file.c
  84. --- php-7.4.4.orig/ext/standard/file.c 2020-03-17 11:40:30.000000000 +0100
  85. +++ php-7.4.4/ext/standard/file.c 2020-03-19 14:36:43.457447585 +0100
  86. @@ -952,7 +952,16 @@
  87. }
  88. #endif
  89.  
  90. - fp = VCWD_POPEN(command, posix_mode);
  91. + if (PG(exec_dir) && strlen(PG(exec_dir))) {
  92. + zend_string *cmd_esc = php_escape_shell_arg(command);
  93. + char *tmp;
  94. + spprintf(&tmp, 0, "PATH=%s /bin/bash --php -rc %s", PG(exec_dir), ZSTR_VAL(cmd_esc));
  95. + fp = VCWD_POPEN(tmp, posix_mode);
  96. + efree(tmp);
  97. + zend_string_release(cmd_esc);
  98. + } else {
  99. + fp = VCWD_POPEN(command, posix_mode);
  100. + }
  101. if (!fp) {
  102. php_error_docref2(NULL, command, posix_mode, E_WARNING, "%s", strerror(errno));
  103. efree(posix_mode);
  104. diff -Naur php-7.4.4.orig/ext/standard/filestat.c php-7.4.4/ext/standard/filestat.c
  105. --- php-7.4.4.orig/ext/standard/filestat.c 2020-03-17 11:40:30.000000000 +0100
  106. +++ php-7.4.4/ext/standard/filestat.c 2020-03-19 14:36:43.458447578 +0100
  107. @@ -769,13 +769,14 @@
  108. "size", "atime", "mtime", "ctime", "blksize", "blocks"
  109. };
  110. const char *local;
  111. + char local_safe[MAXPATHLEN];
  112. php_stream_wrapper *wrapper;
  113.  
  114. if (!filename_length) {
  115. RETURN_FALSE;
  116. }
  117.  
  118. - if ((wrapper = php_stream_locate_url_wrapper(filename, &local, 0)) == &php_plain_files_wrapper && php_check_open_basedir(local)) {
  119. + if ((wrapper = php_stream_locate_url_wrapper(filename, &local, 0)) == &php_plain_files_wrapper && type != FS_IS_X && php_check_open_basedir(local)) {
  120. RETURN_FALSE;
  121. }
  122.  
  123. @@ -800,6 +801,19 @@
  124. #endif
  125. #ifdef X_OK
  126. case FS_IS_X:
  127. + if (PG(exec_dir) && strlen(PG(exec_dir))) {
  128. + if (strstr(local, "..")) {
  129. + RETURN_FALSE;
  130. + } else {
  131. + char *b = strrchr(local, PHP_DIR_SEPARATOR);
  132. + if (b) {
  133. + snprintf(local_safe, MAXPATHLEN, "%s%s", PG(exec_dir), b);
  134. + } else {
  135. + snprintf(local_safe, MAXPATHLEN, "%s%c%s", PG(exec_dir), PHP_DIR_SEPARATOR, local);
  136. + }
  137. + local = (char *)&local_safe;
  138. + }
  139. + }
  140. RETURN_BOOL(VCWD_ACCESS(local, X_OK) == 0);
  141. break;
  142. #endif
  143. diff -Naur php-7.4.4.orig/ext/standard/proc_open.c php-7.4.4/ext/standard/proc_open.c
  144. --- php-7.4.4.orig/ext/standard/proc_open.c 2020-03-17 11:40:33.000000000 +0100
  145. +++ php-7.4.4/ext/standard/proc_open.c 2020-03-19 14:36:43.458447578 +0100
  146. @@ -543,8 +543,17 @@
  147. RETURN_FALSE;
  148. }
  149. #else
  150. - argv = safe_emalloc(sizeof(char *), num_elems + 1, 0);
  151. + argv = safe_emalloc(sizeof(char *), num_elems + 1 + 5, 0);
  152. i = 0;
  153. + if (PG(exec_dir) && strlen(PG(exec_dir))) {
  154. + command = pestrdup("/bin/bash", is_persistent);
  155. + argv[i++] = estrdup("/bin/bash");
  156. + argv[i++] = estrdup("--php");
  157. + argv[i++] = estrdup("-rc");
  158. + argv[i++] = estrdup("exec \"$@\"");
  159. + argv[i++] = estrdup("bash");
  160. + argv[i] = NULL;
  161. + }
  162. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(command_zv), arg_zv) {
  163. zend_string *arg_str = get_valid_arg_string(arg_zv, i + 1);
  164. if (!arg_str) {
  165. @@ -609,6 +618,15 @@
  166. }
  167. #endif
  168.  
  169. + zval envpath;
  170. + if (PG(exec_dir) && strlen(PG(exec_dir))) {
  171. + if (!environment || zend_hash_num_elements(Z_ARRVAL_P(environment)) == 0) {
  172. + array_init_size(&envpath, 1);
  173. + environment = &envpath;
  174. + }
  175. + add_assoc_string(environment, "PATH", PG(exec_dir));
  176. + }
  177. +
  178. if (environment) {
  179. env = _php_array_to_envp(environment, is_persistent);
  180. }
  181. @@ -1057,10 +1075,14 @@
  182. }
  183. execvp(command, argv);
  184. } else {
  185. - if (env.envarray) {
  186. - execle("/bin/sh", "sh", "-c", command, NULL, env.envarray);
  187. + if (PG(exec_dir) && strlen(PG(exec_dir))) {
  188. + execle("/bin/bash", "bash", "--php", "-rc", command, NULL, env.envarray);
  189. } else {
  190. - execl("/bin/sh", "sh", "-c", command, NULL);
  191. + if (env.envarray) {
  192. + execle("/bin/sh", "sh", "-c", command, NULL, env.envarray);
  193. + } else {
  194. + execl("/bin/sh", "sh", "-c", command, NULL);
  195. + }
  196. }
  197. }
  198. _exit(127);
  199. diff -Naur php-7.4.4.orig/main/main.c php-7.4.4/main/main.c
  200. --- php-7.4.4.orig/main/main.c 2020-03-19 14:34:35.398407536 +0100
  201. +++ php-7.4.4/main/main.c 2020-03-19 14:36:43.459447570 +0100
  202. @@ -820,6 +820,7 @@
  203. STD_PHP_INI_ENTRY("syslog.facility", "LOG_USER", PHP_INI_SYSTEM, OnSetFacility, syslog_facility, php_core_globals, core_globals)
  204. STD_PHP_INI_ENTRY("syslog.ident", "php", PHP_INI_SYSTEM, OnUpdateString, syslog_ident, php_core_globals, core_globals)
  205. STD_PHP_INI_ENTRY("syslog.filter", "no-ctrl", PHP_INI_ALL, OnSetLogFilter, syslog_filter, php_core_globals, core_globals)
  206. + STD_PHP_INI_ENTRY("exec_dir", NULL, PHP_INI_SYSTEM, OnUpdateString, exec_dir, php_core_globals, core_globals)
  207. PHP_INI_END()
  208. /* }}} */
  209.  
  210. diff -Naur php-7.4.4.orig/main/php_globals.h php-7.4.4/main/php_globals.h
  211. --- php-7.4.4.orig/main/php_globals.h 2020-03-17 11:40:21.000000000 +0100
  212. +++ php-7.4.4/main/php_globals.h 2020-03-19 14:36:43.460447563 +0100
  213. @@ -170,6 +170,8 @@
  214. char *syslog_ident;
  215. zend_bool have_called_openlog;
  216. zend_long syslog_filter;
  217. +
  218. + char *exec_dir;
  219. };
  220.  
  221.  
  222. diff -Naur php-7.4.4.orig/php.ini-development php-7.4.4/php.ini-development
  223. --- php-7.4.4.orig/php.ini-development 2020-03-17 11:40:21.000000000 +0100
  224. +++ php-7.4.4/php.ini-development 2020-03-19 14:36:43.460447563 +0100
  225. @@ -306,6 +306,10 @@
  226. ; http://php.net/open-basedir
  227. ;open_basedir =
  228.  
  229. +; Only executables located in the exec_dir will be allowed to be executed
  230. +; via the exec family of functions.
  231. +exec_dir =
  232. +
  233. ; This directive allows you to disable certain functions.
  234. ; It receives a comma-delimited list of function names.
  235. ; http://php.net/disable-functions
  236. diff -Naur php-7.4.4.orig/php.ini-production php-7.4.4/php.ini-production
  237. --- php-7.4.4.orig/php.ini-production 2020-03-17 11:40:21.000000000 +0100
  238. +++ php-7.4.4/php.ini-production 2020-03-19 14:36:43.461447555 +0100
  239. @@ -306,6 +306,10 @@
  240. ; http://php.net/open-basedir
  241. ;open_basedir =
  242.  
  243. +; Only executables located in the exec_dir will be allowed to be executed
  244. +; via the exec family of functions.
  245. +exec_dir =
  246. +
  247. ; This directive allows you to disable certain functions.
  248. ; It receives a comma-delimited list of function names.
  249. ; http://php.net/disable-functions
  250.