Skip to content

Commit f924e97

Browse files
committed
Fix #71542: disk_total_space does not work with relative paths
For ZTS builds, we need to expand the path given to `disk_free_space()` and `disk_total_space()` to properly support the VCWD. Closes GH-7377.
1 parent bcc2f07 commit f924e97

File tree

4 files changed

+39
-14
lines changed

4 files changed

+39
-14
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@ PHP NEWS
1111
. Fixed bug #81353 (segfault with preloading and statically bound closure).
1212
(Nikita)
1313

14+
- Standard:
15+
. Fixed bug #71542 (disk_total_space does not work with relative paths). (cmb)
16+
1417
- XML:
1518
. Fixed bug #81351 (xml_parse may fail, but has no error code). (cmb, Nikita)
1619

ext/standard/filestat.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -183,18 +183,22 @@ static int php_disk_total_space(char *path, double *space) /* {{{ */
183183
PHP_FUNCTION(disk_total_space)
184184
{
185185
double bytestotal;
186-
char *path;
186+
char *path, fullpath[MAXPATHLEN];
187187
size_t path_len;
188188

189189
ZEND_PARSE_PARAMETERS_START(1, 1)
190190
Z_PARAM_PATH(path, path_len)
191191
ZEND_PARSE_PARAMETERS_END();
192192

193-
if (php_check_open_basedir(path)) {
193+
if (!expand_filepath(path, fullpath)) {
194194
RETURN_FALSE;
195195
}
196196

197-
if (php_disk_total_space(path, &bytestotal) == SUCCESS) {
197+
if (php_check_open_basedir(fullpath)) {
198+
RETURN_FALSE;
199+
}
200+
201+
if (php_disk_total_space(fullpath, &bytestotal) == SUCCESS) {
198202
RETURN_DOUBLE(bytestotal);
199203
}
200204
RETURN_FALSE;
@@ -278,18 +282,22 @@ static int php_disk_free_space(char *path, double *space) /* {{{ */
278282
PHP_FUNCTION(disk_free_space)
279283
{
280284
double bytesfree;
281-
char *path;
285+
char *path, fullpath[MAXPATHLEN];
282286
size_t path_len;
283287

284288
ZEND_PARSE_PARAMETERS_START(1, 1)
285289
Z_PARAM_PATH(path, path_len)
286290
ZEND_PARSE_PARAMETERS_END();
287291

288-
if (php_check_open_basedir(path)) {
292+
if (!expand_filepath(path, fullpath)) {
293+
RETURN_FALSE;
294+
}
295+
296+
if (php_check_open_basedir(fullpath)) {
289297
RETURN_FALSE;
290298
}
291299

292-
if (php_disk_free_space(path, &bytesfree) == SUCCESS) {
300+
if (php_disk_free_space(fullpath, &bytesfree) == SUCCESS) {
293301
RETURN_DOUBLE(bytesfree);
294302
}
295303
RETURN_FALSE;

ext/standard/tests/dir/bug71542.phpt

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
Bug #71542 (disk_total_space does not work with relative paths)
3+
--FILE--
4+
<?php
5+
$dir = basename(getcwd());
6+
chdir("..");
7+
var_dump(
8+
disk_total_space($dir) !== false,
9+
disk_free_space($dir) !== false
10+
);
11+
?>
12+
--EXPECT--
13+
bool(true)
14+
bool(true)

tests/security/open_basedir_disk_free_space.phpt

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,28 +25,28 @@ bool(true)
2525
bool(true)
2626
bool(true)
2727

28-
Warning: disk_free_space(): open_basedir restriction in effect. File(../bad) is not within the allowed path(s): (.) in %s on line %d
28+
Warning: disk_free_space(): open_basedir restriction in effect. File(%s) is not within the allowed path(s): (.) in %s on line %d
2929
bool(false)
3030

31-
Warning: disk_free_space(): open_basedir restriction in effect. File(../bad/bad.txt) is not within the allowed path(s): (.) in %s on line %d
31+
Warning: disk_free_space(): open_basedir restriction in effect. File(%s) is not within the allowed path(s): (.) in %s on line %d
3232
bool(false)
3333

34-
Warning: disk_free_space(): open_basedir restriction in effect. File(..) is not within the allowed path(s): (.) in %s on line %d
34+
Warning: disk_free_space(): open_basedir restriction in effect. File(%s) is not within the allowed path(s): (.) in %s on line %d
3535
bool(false)
3636

37-
Warning: disk_free_space(): open_basedir restriction in effect. File(../) is not within the allowed path(s): (.) in %s on line %d
37+
Warning: disk_free_space(): open_basedir restriction in effect. File(%s) is not within the allowed path(s): (.) in %s on line %d
3838
bool(false)
3939

40-
Warning: disk_free_space(): open_basedir restriction in effect. File(/) is not within the allowed path(s): (.) in %s on line %d
40+
Warning: disk_free_space(): open_basedir restriction in effect. File(%s) is not within the allowed path(s): (.) in %s on line %d
4141
bool(false)
4242

43-
Warning: disk_free_space(): open_basedir restriction in effect. File(../bad/.) is not within the allowed path(s): (.) in %s on line %d
43+
Warning: disk_free_space(): open_basedir restriction in effect. File(%s) is not within the allowed path(s): (.) in %s on line %d
4444
bool(false)
4545

46-
Warning: disk_free_space(): open_basedir restriction in effect. File(%s/test/bad/bad.txt) is not within the allowed path(s): (.) in %s on line %d
46+
Warning: disk_free_space(): open_basedir restriction in effect. File(%s) is not within the allowed path(s): (.) in %s on line %d
4747
bool(false)
4848

49-
Warning: disk_free_space(): open_basedir restriction in effect. File(%s/test/bad/../bad/bad.txt) is not within the allowed path(s): (.) in %s on line %d
49+
Warning: disk_free_space(): open_basedir restriction in effect. File(%s) is not within the allowed path(s): (.) in %s on line %d
5050
bool(false)
5151
float(%s)
5252
*** Finished testing open_basedir configuration [disk_free_space] ***

0 commit comments

Comments
 (0)