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 2025-11-04 17:38:06.000000000 +0100
  11. +++ b/ext/standard/exec.c 2025-11-14 15:36:33.038427564 +0100
  12. @@ -114,17 +114,28 @@
  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)(int) = signal(SIGCHLD, SIG_DFL);
  22. #endif
  23.  
  24. + if (PG(exec_dir) && strlen(PG(exec_dir))) {
  25. + zend_string *cmd_str = zend_string_init(cmd, strlen(cmd), 0);
  26. + zend_string *cmd_esc = php_escape_shell_arg(cmd_str);
  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. + zend_string_release(cmd_str);
  30. + cmd_p = d;
  31. + } else {
  32. + cmd_p = cmd;
  33. + }
  34. +
  35. #ifdef PHP_WIN32
  36. - fp = VCWD_POPEN(cmd, "rb");
  37. + fp = VCWD_POPEN(cmd_p, "rb");
  38. #else
  39. - fp = VCWD_POPEN(cmd, "r");
  40. + fp = VCWD_POPEN(cmd_p, "r");
  41. #endif
  42. if (!fp) {
  43. php_error_docref(NULL, E_WARNING, "Unable to fork [%s]", cmd);
  44. @@ -498,7 +509,7 @@
  45. PHP_FUNCTION(shell_exec)
  46. {
  47. FILE *in;
  48. - char *command;
  49. + char *command, *command_p;
  50. size_t command_len;
  51. zend_string *ret;
  52. php_stream *stream;
  53. @@ -512,14 +523,26 @@
  54. RETURN_THROWS();
  55. }
  56.  
  57. + if (PG(exec_dir) && strlen(PG(exec_dir))) {
  58. + zend_string *cmd_str = zend_string_init(command, command_len, 0);
  59. + zend_string *cmd_esc = php_escape_shell_arg(cmd_str);
  60. + spprintf(&command_p, 0, "PATH=%s /bin/bash --php -rc %s", PG(exec_dir), ZSTR_VAL(cmd_esc));
  61. + zend_string_release(cmd_esc);
  62. + zend_string_release(cmd_str);
  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 2025-11-04 17:38:06.000000000 +0100
  83. +++ b/ext/standard/file.c 2025-11-14 15:36:33.039427557 +0100
  84. @@ -28,6 +28,7 @@
  85. #include "ext/standard/basic_functions.h"
  86. #include "php_ini.h"
  87. #include "zend_smart_str.h"
  88. +#include "ext/standard/exec.h"
  89.  
  90. #include <stdio.h>
  91. #include <stdlib.h>
  92. @@ -805,7 +806,18 @@
  93. RETURN_THROWS();
  94. }
  95.  
  96. - fp = VCWD_POPEN(command, posix_mode);
  97. + if (PG(exec_dir) && strlen(PG(exec_dir))) {
  98. + zend_string *cmd_str = zend_string_init(command, command_len, 0);
  99. + zend_string *cmd_esc = php_escape_shell_arg(cmd_str);
  100. + char *tmp;
  101. + spprintf(&tmp, 0, "PATH=%s /bin/bash --php -rc %s", PG(exec_dir), ZSTR_VAL(cmd_esc));
  102. + fp = VCWD_POPEN(tmp, posix_mode);
  103. + efree(tmp);
  104. + zend_string_release(cmd_esc);
  105. + zend_string_release(cmd_str);
  106. + } else {
  107. + fp = VCWD_POPEN(command, posix_mode);
  108. + }
  109. if (!fp) {
  110. php_error_docref2(NULL, command, posix_mode, E_WARNING, "%s", strerror(errno));
  111. efree(posix_mode);
  112. --- a/ext/standard/filestat.c 2025-11-04 17:38:06.000000000 +0100
  113. +++ b/ext/standard/filestat.c 2025-11-14 15:36:33.039427557 +0100
  114. @@ -746,6 +746,7 @@
  115. zend_stat_t *stat_sb = &ssb.sb;
  116. int flags = 0, rmask=S_IROTH, wmask=S_IWOTH, xmask=S_IXOTH; /* access rights defaults to other */
  117. const char *local = NULL;
  118. + char local_safe[MAXPATHLEN];
  119. php_stream_wrapper *wrapper = NULL;
  120.  
  121. if (IS_ACCESS_CHECK(type)) {
  122. @@ -757,7 +758,7 @@
  123. }
  124.  
  125. if ((wrapper = php_stream_locate_url_wrapper(ZSTR_VAL(filename), &local, 0)) == &php_plain_files_wrapper
  126. - && php_check_open_basedir(local)) {
  127. + && type != FS_IS_X && php_check_open_basedir(local)) {
  128. RETURN_FALSE;
  129. }
  130.  
  131. @@ -787,9 +788,25 @@
  132. break;
  133. #endif
  134. #ifdef X_OK
  135. - case FS_IS_X:
  136. - RETURN_BOOL(VCWD_ACCESS(file_path_to_check, X_OK) == 0);
  137. + case FS_IS_X: {
  138. + bool xok = (VCWD_ACCESS(file_path_to_check, X_OK) == 0);
  139. + if (!xok && PG(exec_dir) && strlen(PG(exec_dir))) {
  140. + if (strstr(file_path_to_check, "..")) {
  141. + RETURN_FALSE;
  142. + } else {
  143. + char *b = strrchr(file_path_to_check, PHP_DIR_SEPARATOR);
  144. + if (b) {
  145. + snprintf(local_safe, MAXPATHLEN, "%s%s", PG(exec_dir), b);
  146. + } else {
  147. + snprintf(local_safe, MAXPATHLEN, "%s%c%s", PG(exec_dir), PHP_DIR_SEPARATOR, file_path_to_check);
  148. + }
  149. + file_path_to_check = (char *)&local_safe;
  150. + }
  151. + xok = (VCWD_ACCESS(file_path_to_check, X_OK) == 0);
  152. + }
  153. + RETURN_BOOL(xok);
  154. break;
  155. + }
  156. #endif
  157. }
  158. }
  159. --- a/ext/standard/proc_open.c 2025-11-04 17:38:06.000000000 +0100
  160. +++ b/ext/standard/proc_open.c 2025-11-14 15:37:38.558984315 +0100
  161. @@ -780,7 +780,17 @@
  162. zend_string *command = NULL;
  163. uint32_t i = 0;
  164.  
  165. - *argv = safe_emalloc(sizeof(char *), num_elems + 1, 0);
  166. + *argv = safe_emalloc(sizeof(char *), num_elems + 1 + 5, 0);
  167. +
  168. + if (PG(exec_dir) && strlen(PG(exec_dir))) {
  169. + command = zend_string_init("/bin/bash", sizeof("/bin/bash") - 1, 0);
  170. + (*argv)[i++] = estrdup("/bin/bash");
  171. + (*argv)[i++] = estrdup("--php");
  172. + (*argv)[i++] = estrdup("-rc");
  173. + (*argv)[i++] = estrdup("exec \"$@\"");
  174. + (*argv)[i++] = estrdup("bash");
  175. + (*argv)[i] = NULL;
  176. + }
  177.  
  178. ZEND_HASH_FOREACH_VAL(array, arg_zv) {
  179. zend_string *arg_str = get_valid_arg_string(arg_zv, i + 1);
  180. @@ -1278,8 +1288,37 @@
  181. }
  182. #endif
  183.  
  184. + HashTable *envpath = NULL;
  185. + if (PG(exec_dir) && strlen(PG(exec_dir))) {
  186. + zend_string *key;
  187. + zend_ulong idx;
  188. + zval *val;
  189. + uint32_t count = environment ? zend_hash_num_elements(environment) : 0;
  190. + ALLOC_HASHTABLE(envpath);
  191. + zend_hash_init(envpath, count + 1, NULL, ZVAL_PTR_DTOR, 0);
  192. + if (count) {
  193. + ZEND_HASH_FOREACH_KEY_VAL(environment, idx, key, val) {
  194. + if (key) {
  195. + zend_hash_add_new(envpath, key, val);
  196. + } else {
  197. + zend_hash_index_add_new(envpath, idx, val);
  198. + }
  199. + zval_add_ref(val);
  200. + } ZEND_HASH_FOREACH_END();
  201. + }
  202. + zval tmp;
  203. + ZVAL_STRING(&tmp, PG(exec_dir));
  204. + zend_hash_str_update(envpath, "PATH", strlen("PATH"), &tmp);
  205. + environment = envpath;
  206. + }
  207. +
  208. if (environment) {
  209. env = php_array_to_envp(environment);
  210. + if (envpath) {
  211. + zend_hash_destroy(envpath);
  212. + FREE_HASHTABLE(envpath);
  213. + environment = NULL;
  214. + }
  215. }
  216.  
  217. descriptors = alloc_descriptor_array(descriptorspec);
  218. @@ -1403,9 +1442,15 @@
  219. if (argv) {
  220. r = posix_spawnp(&child, ZSTR_VAL(command_str), &factions, NULL, argv, (env.envarray ? env.envarray : environ));
  221. } else {
  222. - r = posix_spawn(&child, "/bin/sh" , &factions, NULL,
  223. - (char * const[]) {"sh", "-c", ZSTR_VAL(command_str), NULL},
  224. - env.envarray ? env.envarray : environ);
  225. + if (PG(exec_dir) && strlen(PG(exec_dir))) {
  226. + r = posix_spawn(&child, "/bin/bash" , &factions, NULL,
  227. + (char * const[]) {"bash", "--php", "-rc", ZSTR_VAL(command_str), NULL},
  228. + env.envarray ? env.envarray : environ);
  229. + } else {
  230. + r = posix_spawn(&child, "/bin/sh" , &factions, NULL,
  231. + (char * const[]) {"sh", "-c", ZSTR_VAL(command_str), NULL},
  232. + env.envarray ? env.envarray : environ);
  233. + }
  234. }
  235. posix_spawn_file_actions_destroy(&factions);
  236. if (r != 0) {
  237. @@ -1438,10 +1483,14 @@
  238. }
  239. execvp(ZSTR_VAL(command_str), argv);
  240. } else {
  241. - if (env.envarray) {
  242. - execle("/bin/sh", "sh", "-c", ZSTR_VAL(command_str), NULL, env.envarray);
  243. + if (PG(exec_dir) && strlen(PG(exec_dir))) {
  244. + execle("/bin/bash", "bash", "--php", "-rc", ZSTR_VAL(command_str), NULL, env.envarray);
  245. } else {
  246. - execl("/bin/sh", "sh", "-c", ZSTR_VAL(command_str), NULL);
  247. + if (env.envarray) {
  248. + execle("/bin/sh", "sh", "-c", ZSTR_VAL(command_str), NULL, env.envarray);
  249. + } else {
  250. + execl("/bin/sh", "sh", "-c", ZSTR_VAL(command_str), NULL);
  251. + }
  252. }
  253. }
  254.  
  255. --- a/main/main.c 2025-11-14 15:36:07.023603557 +0100
  256. +++ b/main/main.c 2025-11-14 15:36:33.040427551 +0100
  257. @@ -889,6 +889,7 @@
  258. STD_PHP_INI_ENTRY("syslog.facility", "LOG_USER", PHP_INI_SYSTEM, OnSetFacility, syslog_facility, php_core_globals, core_globals)
  259. STD_PHP_INI_ENTRY("syslog.ident", "php", PHP_INI_SYSTEM, OnUpdateString, syslog_ident, php_core_globals, core_globals)
  260. STD_PHP_INI_ENTRY("syslog.filter", "no-ctrl", PHP_INI_ALL, OnSetLogFilter, syslog_filter, php_core_globals, core_globals)
  261. + STD_PHP_INI_ENTRY("exec_dir", NULL, PHP_INI_SYSTEM, OnUpdateString, exec_dir, php_core_globals, core_globals)
  262. PHP_INI_END()
  263. /* }}} */
  264.  
  265. --- a/main/php_globals.h 2025-11-04 17:38:06.000000000 +0100
  266. +++ b/main/php_globals.h 2025-11-14 15:36:33.040427551 +0100
  267. @@ -173,6 +173,8 @@
  268. char *syslog_ident;
  269. zend_long syslog_filter;
  270. zend_long error_log_mode;
  271. +
  272. + char *exec_dir;
  273. };
  274.  
  275.  
  276. --- a/php.ini-development 2025-11-04 17:38:06.000000000 +0100
  277. +++ b/php.ini-development 2025-11-14 15:36:33.041427544 +0100
  278. @@ -317,6 +317,10 @@
  279. ; https://php.net/open-basedir
  280. ;open_basedir =
  281.  
  282. +; Only executables located in the exec_dir will be allowed to be executed
  283. +; via the exec family of functions.
  284. +exec_dir =
  285. +
  286. ; This directive allows you to disable certain functions.
  287. ; It receives a comma-delimited list of function names.
  288. ; https://php.net/disable-functions
  289. --- a/php.ini-production 2025-11-04 17:38:06.000000000 +0100
  290. +++ b/php.ini-production 2025-11-14 15:36:33.041427544 +0100
  291. @@ -317,6 +317,10 @@
  292. ; https://php.net/open-basedir
  293. ;open_basedir =
  294.  
  295. +; Only executables located in the exec_dir will be allowed to be executed
  296. +; via the exec family of functions.
  297. +exec_dir =
  298. +
  299. ; This directive allows you to disable certain functions.
  300. ; It receives a comma-delimited list of function names.
  301. ; https://php.net/disable-functions
  302.