Skip to content

ext/pcntl: pcntl_exec() using execveat whenever possible instead. #17834

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion ext/pcntl/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ if test "$PHP_PCNTL" != "no"; then
done

AC_CHECK_FUNCS(m4_normalize([
forkx
execveat
getcpuid
getpriority
pidfd_open
Expand Down
43 changes: 41 additions & 2 deletions ext/pcntl/pcntl.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
#include <sys/resource.h>
#endif

#if defined(HAVE_EXECVEAT)
#include <fcntl.h>
#include <unistd.h>
#endif

#ifdef HAVE_WAITID
#if defined (HAVE_DECL_P_ALL) && HAVE_DECL_P_ALL == 1
#define HAVE_POSIX_IDTYPES 1
Expand Down Expand Up @@ -617,6 +622,40 @@ PHP_FUNCTION(pcntl_wstopsig)
}
/* }}} */

#ifdef HAVE_EXECVEAT
static zend_always_inline zend_result php_execve(const char *path, char **argv, char **envp) {
int fd = open(path, O_RDONLY | O_CLOEXEC);
if (fd < 0) {
return FAILURE;
}
#ifdef AT_EXECVE_CHECK
// Linux >= 6.14 allows to check if `path` is allowed
// for execution per kernel security policy (w/o actually running it)
if (execveat(fd, "", argv, envp, AT_EMPTY_PATH | AT_EXECVE_CHECK) < 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we consider that the check is successful most of the time, does this really makes much sense?

close(fd);
return FAILURE;
}
#endif
if (execveat(fd, "", argv, envp, AT_EMPTY_PATH) < 0) {
close(fd);
return FAILURE;
}
return SUCCESS;
}

static zend_always_inline zend_result php_execv(const char *path, char **argv) {
return php_execve(path, argv, 0);
}
#else
static zend_always_inline zend_result php_execve(const char *path, char **argv, char **envp) {
return execve(path, argv, envp) == 0 ? SUCCESS : FAILURE;
}

static zend_always_inline zend_result php_execv(const char *path, char **argv) {
return execv(path, argv) == 0 ? SUCCESS : FAILURE;
}
#endif

/* {{{ Executes specified program in current process space as defined by exec(2) */
PHP_FUNCTION(pcntl_exec)
{
Expand Down Expand Up @@ -716,7 +755,7 @@ PHP_FUNCTION(pcntl_exec)
} ZEND_HASH_FOREACH_END();
*(pair) = NULL;

if (execve(path, argv, envp) == -1) {
if (php_execve(path, argv, envp) == FAILURE) {
PCNTL_G(last_error) = errno;
php_error_docref(NULL, E_WARNING, "Error has occurred: (errno %d) %s", errno, strerror(errno));
}
Expand All @@ -727,7 +766,7 @@ PHP_FUNCTION(pcntl_exec)
efree(envp);
} else {

if (execv(path, argv) == -1) {
if (php_execv(path, argv) == FAILURE) {
PCNTL_G(last_error) = errno;
php_error_docref(NULL, E_WARNING, "Error has occurred: (errno %d) %s", errno, strerror(errno));
}
Expand Down
Loading