Skip to content

Commit ee7a8ac

Browse files
committed
Fix handling of open_basedir that contains cwd
While resolving the path, the last step will reduce it down to "" (an empty string) and realpath() will resolve this to getcwd(). If open_basedir contains the CWD, then that means open_basedir will be bypassed for paths that don't have any components that exist (if one of the components exists, then we abort the realpath loop at that point). Closes GH-7015.
1 parent 36b9bde commit ee7a8ac

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
CWD on open_basedir should not imply that everything is accessible
3+
--FILE--
4+
<?php
5+
6+
$cwd = getcwd();
7+
ini_set('open_basedir', $cwd);
8+
var_dump(file_get_contents('/some/path/outside/open/basedir'));
9+
10+
?>
11+
--EXPECTF--
12+
Warning: file_get_contents(): open_basedir restriction in effect. File(/some/path/outside/open/basedir) is not within the allowed path(s): (%s) in %s on line %d
13+
14+
Warning: file_get_contents(/some/path/outside/open/basedir): failed to open stream: Operation not permitted in %s on line %d
15+
bool(false)

main/fopen_wrappers.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,10 @@ PHPAPI int php_check_specific_open_basedir(const char *basedir, const char *path
207207
path_tmp[path_len - 1] = '\0';
208208
#endif
209209
}
210+
if (*path_tmp == '\0') {
211+
/* Do not pass an empty string to realpath(), as this will resolve to CWD. */
212+
break;
213+
}
210214
nesting_level++;
211215
}
212216

0 commit comments

Comments
 (0)