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. --- a/ext/standard/exec.c 2021-01-05 14:54:54.000000000 +0100
  11. +++ b/ext/standard/exec.c 2021-01-08 11:05:40.001529198 +0100
  12. @@ -116,21 +116,30 @@
  13. FILE *fp;
  14. char *buf;
  15. int pclose_return;
  16. - char *b, *d=NULL;
  17. + char *cmd_p, *b, *d=NULL;
  18. php_stream *stream;
  19. size_t buflen, bufl = 0;
  20. #if PHP_SIGCHILD
  21. void (*sig_handler)() = NULL;
  22. #endif
  23.  
  24. + if (PG(exec_dir) && strlen(PG(exec_dir))) {
  25. + zend_string *cmd_esc = php_escape_shell_arg(cmd);
  26. + spprintf(&d, 0, "PATH=%s /bin/bash --php -rc %s", PG(exec_dir), ZSTR_VAL(cmd_esc));
  27. + zend_string_release(cmd_esc);
  28. + cmd_p = d;
  29. + } else {
  30. + cmd_p = cmd;
  31. + }
  32. +
  33. #if PHP_SIGCHILD
  34. sig_handler = signal (SIGCHLD, SIG_DFL);
  35. #endif
  36.  
  37. #ifdef PHP_WIN32
  38. - fp = VCWD_POPEN(cmd, "rb");
  39. + fp = VCWD_POPEN(cmd_p, "rb");
  40. #else
  41. - fp = VCWD_POPEN(cmd, "r");
  42. + fp = VCWD_POPEN(cmd_p, "r");
  43. #endif
  44. if (!fp) {
  45. php_error_docref(NULL, E_WARNING, "Unable to fork [%s]", cmd);
  46. @@ -513,7 +522,7 @@
  47. PHP_FUNCTION(shell_exec)
  48. {
  49. FILE *in;
  50. - char *command;
  51. + char *command, *command_p;
  52. size_t command_len;
  53. zend_string *ret;
  54. php_stream *stream;
  55. @@ -531,14 +540,24 @@
  56. RETURN_THROWS();
  57. }
  58.  
  59. + if (PG(exec_dir) && strlen(PG(exec_dir))) {
  60. + zend_string *cmd_esc = php_escape_shell_arg(command);
  61. + spprintf(&command_p, 0, "PATH=%s /bin/bash --php -rc %s", PG(exec_dir), ZSTR_VAL(cmd_esc));
  62. + zend_string_release(cmd_esc);
  63. + } else {
  64. + command_p = estrdup(command);
  65. + }
  66. +
  67. #ifdef PHP_WIN32
  68. - if ((in=VCWD_POPEN(command, "rt"))==NULL) {
  69. + if ((in=VCWD_POPEN(command_p, "rt"))==NULL) {
  70. #else
  71. - if ((in=VCWD_POPEN(command, "r"))==NULL) {
  72. + if ((in=VCWD_POPEN(command_p, "r"))==NULL) {
  73. #endif
  74. php_error_docref(NULL, E_WARNING, "Unable to execute '%s'", command);
  75. + efree(command_p);
  76. RETURN_FALSE;
  77. }
  78. + efree(command_p);
  79.  
  80. stream = php_stream_fopen_from_pipe(in, "rb");
  81. ret = php_stream_copy_to_mem(stream, PHP_STREAM_COPY_ALL, 0);
  82. --- a/ext/standard/file.c 2021-01-05 14:54:54.000000000 +0100
  83. +++ b/ext/standard/file.c 2021-01-08 11:05:40.002529191 +0100
  84. @@ -955,7 +955,16 @@
  85. RETURN_THROWS();
  86. }
  87.  
  88. - fp = VCWD_POPEN(command, posix_mode);
  89. + if (PG(exec_dir) && strlen(PG(exec_dir))) {
  90. + zend_string *cmd_esc = php_escape_shell_arg(command);
  91. + char *tmp;
  92. + spprintf(&tmp, 0, "PATH=%s /bin/bash --php -rc %s", PG(exec_dir), ZSTR_VAL(cmd_esc));
  93. + fp = VCWD_POPEN(tmp, posix_mode);
  94. + efree(tmp);
  95. + zend_string_release(cmd_esc);
  96. + } else {
  97. + fp = VCWD_POPEN(command, posix_mode);
  98. + }
  99. if (!fp) {
  100. php_error_docref2(NULL, command, posix_mode, E_WARNING, "%s", strerror(errno));
  101. efree(posix_mode);
  102. --- a/ext/standard/filestat.c 2021-01-05 14:54:54.000000000 +0100
  103. +++ b/ext/standard/filestat.c 2021-01-08 11:05:40.003529183 +0100
  104. @@ -734,6 +734,7 @@
  105. php_stream_statbuf ssb;
  106. int flags = 0, rmask=S_IROTH, wmask=S_IWOTH, xmask=S_IXOTH; /* access rights defaults to other */
  107. const char *local = NULL;
  108. + char local_safe[MAXPATHLEN];
  109. php_stream_wrapper *wrapper = NULL;
  110.  
  111. if (IS_ACCESS_CHECK(type)) {
  112. @@ -745,7 +746,7 @@
  113. }
  114.  
  115. if ((wrapper = php_stream_locate_url_wrapper(ZSTR_VAL(filename), &local, 0)) == &php_plain_files_wrapper
  116. - && php_check_open_basedir(local)) {
  117. + && type != FS_IS_X && php_check_open_basedir(local)) {
  118. RETURN_FALSE;
  119. }
  120.  
  121. @@ -769,7 +770,22 @@
  122. #endif
  123. #ifdef X_OK
  124. case FS_IS_X:
  125. - RETURN_BOOL(VCWD_ACCESS(local, X_OK) == 0);
  126. + bool xok = (VCWD_ACCESS(local, X_OK) == 0);
  127. + if (!xok && 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. + xok = (VCWD_ACCESS(local, X_OK) == 0);
  140. + }
  141. + RETURN_BOOL(xok);
  142. break;
  143. #endif
  144. }
  145. --- a/ext/standard/proc_open.c 2021-01-05 14:54:54.000000000 +0100
  146. +++ b/ext/standard/proc_open.c 2021-01-08 11:05:40.004529176 +0100
  147. @@ -618,7 +618,17 @@
  148. zend_string *command = NULL;
  149. int i = 0;
  150.  
  151. - *argv = safe_emalloc(sizeof(char *), num_elems + 1, 0);
  152. + *argv = safe_emalloc(sizeof(char *), num_elems + 1 + 5, 0);
  153. +
  154. + if (PG(exec_dir) && strlen(PG(exec_dir))) {
  155. + command = zend_string_init("/bin/bash", sizeof("/bin/bash") - 1, 0);
  156. + (*argv)[i++] = estrdup("/bin/bash");
  157. + (*argv)[i++] = estrdup("--php");
  158. + (*argv)[i++] = estrdup("-rc");
  159. + (*argv)[i++] = estrdup("exec \"$@\"");
  160. + (*argv)[i++] = estrdup("bash");
  161. + (*argv)[i] = NULL;
  162. + }
  163.  
  164. ZEND_HASH_FOREACH_VAL(array, arg_zv) {
  165. zend_string *arg_str = get_valid_arg_string(arg_zv, i + 1);
  166. @@ -1081,6 +1091,15 @@
  167. }
  168. #endif
  169.  
  170. + zval envpath;
  171. + if (PG(exec_dir) && strlen(PG(exec_dir))) {
  172. + if (!environment || zend_hash_num_elements(Z_ARRVAL_P(environment)) == 0) {
  173. + array_init_size(&envpath, 1);
  174. + environment = &envpath;
  175. + }
  176. + add_assoc_string(environment, "PATH", PG(exec_dir));
  177. + }
  178. +
  179. if (environment) {
  180. env = _php_array_to_envp(environment);
  181. }
  182. @@ -1207,10 +1226,14 @@
  183. }
  184. execvp(ZSTR_VAL(command_str), argv);
  185. } else {
  186. - if (env.envarray) {
  187. - execle("/bin/sh", "sh", "-c", ZSTR_VAL(command_str), NULL, env.envarray);
  188. + if (PG(exec_dir) && strlen(PG(exec_dir))) {
  189. + execle("/bin/bash", "bash", "--php", "-rc", ZSTR_VAL(command_str), NULL, env.envarray);
  190. } else {
  191. - execl("/bin/sh", "sh", "-c", ZSTR_VAL(command_str), NULL);
  192. + if (env.envarray) {
  193. + execle("/bin/sh", "sh", "-c", ZSTR_VAL(command_str), NULL, env.envarray);
  194. + } else {
  195. + execl("/bin/sh", "sh", "-c", ZSTR_VAL(command_str), NULL);
  196. + }
  197. }
  198. }
  199.  
  200. --- a/main/main.c 2021-01-08 10:57:59.719887573 +0100
  201. +++ b/main/main.c 2021-01-08 11:05:40.005529169 +0100
  202. @@ -749,6 +749,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. --- a/main/php_globals.h 2021-01-05 14:54:54.000000000 +0100
  211. +++ b/main/php_globals.h 2021-01-08 11:05:40.005529169 +0100
  212. @@ -165,6 +165,8 @@
  213. char *syslog_ident;
  214. bool have_called_openlog;
  215. zend_long syslog_filter;
  216. +
  217. + char *exec_dir;
  218. };
  219.  
  220.  
  221. --- a/php.ini-development 2021-01-05 14:54:54.000000000 +0100
  222. +++ b/php.ini-development 2021-01-08 11:05:40.006529161 +0100
  223. @@ -317,6 +317,10 @@
  224. ; https://php.net/open-basedir
  225. ;open_basedir =
  226.  
  227. +; Only executables located in the exec_dir will be allowed to be executed
  228. +; via the exec family of functions.
  229. +exec_dir =
  230. +
  231. ; This directive allows you to disable certain functions.
  232. ; It receives a comma-delimited list of function names.
  233. ; https://php.net/disable-functions
  234. --- a/php.ini-production 2021-01-05 14:54:54.000000000 +0100
  235. +++ b/php.ini-production 2021-01-08 11:05:40.007529154 +0100
  236. @@ -317,6 +317,10 @@
  237. ; https://php.net/open-basedir
  238. ;open_basedir =
  239.  
  240. +; Only executables located in the exec_dir will be allowed to be executed
  241. +; via the exec family of functions.
  242. +exec_dir =
  243. +
  244. ; This directive allows you to disable certain functions.
  245. ; It receives a comma-delimited list of function names.
  246. ; https://php.net/disable-functions
  247.