Skip to content

Commit df7af12

Browse files
committed
posix: validate mode argument in posix_access
1 parent 927b9ee commit df7af12

2 files changed

Lines changed: 59 additions & 0 deletions

File tree

ext/posix/posix.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -744,6 +744,15 @@ PHP_FUNCTION(posix_access)
744744
RETURN_FALSE;
745745
}
746746

747+
if (mode < 0 || (mode & ~(F_OK | R_OK | W_OK | X_OK))) {
748+
zend_argument_value_error(
749+
2,
750+
"must be a combination of POSIX_F_OK, POSIX_R_OK, POSIX_W_OK, and POSIX_X_OK"
751+
);
752+
efree(path);
753+
RETURN_THROWS();
754+
}
755+
747756
ret = access(path, mode);
748757
efree(path);
749758

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
--TEST--
2+
posix_access() flag (mode) validation
3+
--FILE--
4+
<?php
5+
6+
$dir = __DIR__;
7+
$testfile = "$dir/testfile.txt";
8+
9+
// Create a temporary file for valid access tests
10+
file_put_contents($testfile, "hello");
11+
12+
// Invalid: negative mode
13+
try {
14+
posix_access($testfile, -1);
15+
} catch (ValueError $e) {
16+
echo $e->getMessage(), "\n";
17+
}
18+
19+
// Invalid: mode with garbage bits
20+
try {
21+
posix_access($testfile, 01000); // S_ISVTX bit (sticky)
22+
} catch (ValueError $e) {
23+
echo $e->getMessage(), "\n";
24+
}
25+
26+
// Invalid: mode with unrelated high bits
27+
try {
28+
posix_access($testfile, 02000); // S_ISGID bit
29+
} catch (ValueError $e) {
30+
echo $e->getMessage(), "\n";
31+
}
32+
33+
// Valid: check read and write access
34+
if (posix_access($testfile, POSIX_R_OK | POSIX_W_OK)) {
35+
echo "Read/write access OK\n";
36+
}
37+
38+
// Valid: check file existence
39+
if (posix_access($testfile, POSIX_F_OK)) {
40+
echo "File exists OK\n";
41+
}
42+
43+
unlink($testfile);
44+
?>
45+
--EXPECTF--
46+
posix_access(): Argument #2 ($flags) must be a combination of POSIX_F_OK, POSIX_R_OK, POSIX_W_OK, and POSIX_X_OK
47+
posix_access(): Argument #2 ($flags) must be a combination of POSIX_F_OK, POSIX_R_OK, POSIX_W_OK, and POSIX_X_OK
48+
posix_access(): Argument #2 ($flags) must be a combination of POSIX_F_OK, POSIX_R_OK, POSIX_W_OK, and POSIX_X_OK
49+
Read/write access OK
50+
File exists OK

0 commit comments

Comments
 (0)