Skip to content

Commit 7c8a3e4

Browse files
committed
Fix GH-11808: Live filesystem modified by tests (security)
There's a test that tries to make /etc world-writable, and asserts that it fails. Although this test is guarded by a root user check, there are situations where you don't need to be root to be able to do this. This may thus have unwanted effects on your live filesystem. The simple solution is to remove that part of the test. It doesn't really add value anyway: we're trying to test the chmod error path, but that exact same error path can be reached with any failure condition that the kernel gives. For example, trying to chmod a non-existent file will trigger the same code path. While at it, also prefix the test path for the non-existent file such that we don't accidentally modify the filesystem. The chroot now has a better root-user check, that will not modify the filesystem. Other root-modifying mkdir tests were removed because they added no value either. Closes GH-13566.
1 parent abfe5ff commit 7c8a3e4

File tree

6 files changed

+33
-90
lines changed

6 files changed

+33
-90
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ PHP NEWS
1313
. Fixed bug GH-13531 (Unable to resize SplfixedArray after being unserialized
1414
in PHP 8.2.15). (nielsdos)
1515

16+
- Standard:
17+
. Fixed bug GH-11808 (Live filesystem modified by tests). (nielsdos)
18+
1619
- XML:
1720
. Fixed bug GH-13517 (Multiple test failures when building with
1821
--with-expat). (nielsdos)

ext/standard/tests/file/006_error.phpt

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,9 @@ require __DIR__ . '/../skipif_root.inc';
1111
<?php
1212
echo "*** Testing error conditions for fileperms(), chmod() ***\n";
1313

14-
/* With standard files and dirs */
15-
var_dump( chmod("/etc/passwd", 0777) );
16-
printf("%o", fileperms("/etc/passwd") );
17-
echo "\n";
18-
clearstatcache();
19-
20-
var_dump( chmod("/etc", 0777) );
21-
printf("%o", fileperms("/etc") );
22-
echo "\n";
23-
clearstatcache();
24-
2514
/* With non-existing file or dir */
26-
var_dump( chmod("/no/such/file/dir", 0777) );
27-
var_dump( fileperms("/no/such/file/dir") );
15+
var_dump( chmod(__DIR__ . "/no/such/file/dir", 0777) );
16+
var_dump( fileperms(__DIR__ . "/no/such/file/dir") );
2817
echo "\n";
2918

3019
echo "\n*** Done ***\n";
@@ -34,16 +23,8 @@ echo "\n*** Done ***\n";
3423

3524
Warning: chmod(): %s in %s on line %d
3625
bool(false)
37-
100%d44
38-
39-
Warning: chmod(): %s in %s on line %d
40-
bool(false)
41-
40755
42-
43-
Warning: chmod(): No such file or directory in %s on line %d
44-
bool(false)
4526

46-
Warning: fileperms(): stat failed for /no/such/file/dir in %s on line %d
27+
Warning: fileperms(): stat failed for %s/no/such/file/dir in %s on line %d
4728
bool(false)
4829

4930

ext/standard/tests/file/chroot_001.phpt

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,22 @@
22
chroot()
33
--SKIPIF--
44
<?php
5-
chdir("/");
6-
if (!@mkdir("testtmpskipifdir")) {
7-
die("skip for root only");
8-
}
9-
rmdir("testtmpskipifdir");
105
if (!function_exists("chroot")) {
116
die("skip chroot() not available");
127
}
8+
// Skip if not being run by root (files are always readable, writeable and executable)
9+
$filename = @tempnam(__DIR__, 'root_check_');
10+
if (!file_exists($filename)) {
11+
die('WARN Unable to create the "root check" file');
12+
}
13+
14+
$isRoot = fileowner($filename) == 0;
15+
16+
unlink($filename);
17+
18+
if (!$isRoot) {
19+
die('SKIP Must be run as root');
20+
}
1321
?>
1422
--FILE--
1523
<?php
Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,20 @@
11
--TEST--
2-
recursive mkdir() tests
3-
--SKIPIF--
4-
<?php
5-
chdir("/");
6-
if (!@mkdir("testtmpskipifdir")) {
7-
die("skip for root only");
8-
}
9-
rmdir("testtmpskipifdir");
10-
?>
2+
recursive mkdir() with unclean paths
113
--FILE--
124
<?php
5+
chdir(__DIR__);
6+
$dirpath = "./tmp/foo//bar/logs";
7+
mkdir($dirpath, 0777, true);
138

14-
var_dump(mkdir("/testdir/subdir", 0777, true));
15-
var_dump(rmdir("/testdir/subdir"));
16-
var_dump(rmdir("/testdir"));
17-
18-
echo "Done\n";
9+
if (is_dir($dirpath)) {
10+
echo "Ok.\n";
11+
} else {
12+
echo "Failed.\n";
13+
}
14+
rmdir("./tmp/foo/bar/logs");
15+
rmdir("./tmp/foo/bar/");
16+
rmdir("./tmp/foo/");
17+
rmdir("./tmp/");
1918
?>
2019
--EXPECT--
21-
bool(true)
22-
bool(true)
23-
bool(true)
24-
Done
20+
Ok.

ext/standard/tests/file/mkdir-005.phpt

Lines changed: 0 additions & 25 deletions
This file was deleted.

ext/standard/tests/file/mkdir-006.phpt

Lines changed: 0 additions & 20 deletions
This file was deleted.

0 commit comments

Comments
 (0)