Skip to content

Commit

Permalink
Handle EFAULT
Browse files Browse the repository at this point in the history
  • Loading branch information
tvlooy committed Oct 7, 2022
1 parent ba733f0 commit d60b1d8
Show file tree
Hide file tree
Showing 8 changed files with 88 additions and 27 deletions.
23 changes: 19 additions & 4 deletions package.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,18 @@
<email>tom@ctors.net</email>
<active>yes</active>
</lead>
<date>2018-10-13</date>
<date>2022-10-07</date>
<version>
<release>2.0.2</release>
<api>2.0.2</api>
<release>2.0.3</release>
<api>2.0.3</api>
</version>
<stability>
<release>stable</release>
<api>stable</api>
</stability>
<license uri="https://github.com/tvlooy/php-pledge/blob/master/LICENSE">ISC License</license>
<notes>
- correct reflection information
- handle EFAULT errors
</notes>
<contents>
<dir name="/">
Expand Down Expand Up @@ -58,6 +58,21 @@
<providesextension>pledge</providesextension>
<extsrcrelease></extsrcrelease>
<changelog>
<release>
<date>2018-10-13</date>
<version>
<release>2.0.2</release>
<api>2.0.2</api>
</version>
<stability>
<release>stable</release>
<api>stable</api>
</stability>
<license uri="https://github.com/tvlooy/php-pledge/blob/master/LICENSE">ISC License</license>
<notes>
- correct reflection information
</notes>
</release>
<release>
<date>2016-10-12</date>
<version>
Expand Down
2 changes: 1 addition & 1 deletion php_pledge.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/* php_pledge.h */

#define PHP_PLEDGE_EXTNAME "pledge"
#define PHP_PLEDGE_VERSION "2.0.2"
#define PHP_PLEDGE_VERSION "2.0.3"

extern zend_module_entry pledge_module_entry;
#define phpext_pledge_ptr &check_pledge_entry
Expand Down
74 changes: 60 additions & 14 deletions pledge.c
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ zend_module_entry pledge_module_entry = {
/* Install module */
ZEND_GET_MODULE(pledge)

/* function pledge(string $promises = null, string $execpromises = null): bool */
/* function pledge(?string $promises = null, ?string $execpromises = null): bool */
PHP_FUNCTION(pledge) {
char *promises = NULL;
size_t promises_len = 0;
Expand All @@ -84,20 +84,39 @@ PHP_FUNCTION(pledge) {
}

switch (errno) {
case EFAULT:
zend_throw_exception(
pledge_exception_ce,
"promises or execpromises points outside the process's allocated address space",
errno
);
break;
case EINVAL:
zend_throw_exception(pledge_exception_ce, "Invalid promise in promises string", errno);
zend_throw_exception(
pledge_exception_ce,
"promises is malformed or contains invalid keywords",
errno
);
break;
case EPERM:
zend_throw_exception(pledge_exception_ce, "Attempt to increase permissions", errno);
zend_throw_exception(
pledge_exception_ce,
"This process is attempting to increase permissions",
errno
);
break;
default:
zend_throw_exception(pledge_exception_ce, "Pledge error", errno);
zend_throw_exception(
pledge_exception_ce,
"Pledge error",
errno
);
}

RETURN_FALSE;
}

/* function unveil(string $path = null, string $permissions = null): bool */
/* function unveil(?string $path = null, ?string $permissions = null): bool */
PHP_FUNCTION(unveil) {
char *path = NULL;
size_t path_len = 0;
Expand All @@ -115,20 +134,47 @@ PHP_FUNCTION(unveil) {
}

switch (errno) {
case EINVAL:
zend_throw_exception(unveil_exception_ce, "Invalid permission value", errno);
break;
case EPERM:
zend_throw_exception(unveil_exception_ce, "Attempt to increase permissions", errno);
break;
case E2BIG:
zend_throw_exception(unveil_exception_ce, "Too many unveiled paths", errno);
zend_throw_exception(
unveil_exception_ce,
"The addition of path would exceed the per-process limit for unveiled paths",
errno
);
break;
case EFAULT:
zend_throw_exception(
unveil_exception_ce,
"path or permissions points outside the process's allocated address space",
errno
);
break;
case ENOENT:
zend_throw_exception(unveil_exception_ce, "No such directory", errno);
zend_throw_exception(
unveil_exception_ce,
"A directory in path did not exist",
errno
);
break;
case EINVAL:
zend_throw_exception(
unveil_exception_ce,
"An invalid value of permissions was used",
errno
);
break;
case EPERM:
zend_throw_exception(
unveil_exception_ce,
"An attempt to increase permissions was made, or the path was not accessible, or unveil() was called after locking",
errno
);
break;
default:
zend_throw_exception(unveil_exception_ce, "Unveil error", errno);
zend_throw_exception(
unveil_exception_ce,
"Unveil error",
errno
);
}

RETURN_FALSE;
Expand Down
2 changes: 1 addition & 1 deletion tests/pledge_increase_permissions.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,5 @@ try {
}
?>
--EXPECT--
string(31) "Attempt to increase permissions"
string(50) "This process is attempting to increase permissions"
int(1)
2 changes: 1 addition & 1 deletion tests/pledge_invalid_promise.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ try {
}
?>
--EXPECT--
string(34) "Invalid promise in promises string"
string(50) "promises is malformed or contains invalid keywords"
int(22)
8 changes: 4 additions & 4 deletions tests/reflection.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ array(2) {
string(12) "execpromises"
}
}
string(6) "string"
string(6) "string"
string(7) "?string"
string(7) "?string"
string(4) "bool"
array(2) {
[0]=>
Expand All @@ -49,5 +49,5 @@ array(2) {
string(11) "permissions"
}
}
string(6) "string"
string(6) "string"
string(7) "?string"
string(7) "?string"
2 changes: 1 addition & 1 deletion tests/unveil_increase_attempt.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ unveil();
unveil('/etc/', 'r');
?>
--EXPECTF--
Fatal error: Uncaught Exception: Attempt to increase permissions in %s:%d
Fatal error: Uncaught Exception: An attempt to increase permissions was made, or the path was not accessible, or unveil() was called after locking in %s:%d
Stack trace:
#0 %s(%d): unveil('/etc/', 'r')
#1 {main}
Expand Down
2 changes: 1 addition & 1 deletion tests/unveil_readonly.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ file_put_contents('test', 'test');
--EXPECTF--
bool(true)

Warning: file_put_contents(test): failed to open stream: No such file or directory in %s on line %d
Warning: file_put_contents(test): Failed to open stream: No such file or directory in %s on line %d

0 comments on commit d60b1d8

Please sign in to comment.