Skip to content

Commit 38567b9

Browse files
l0kodkees
authored andcommitted
selftests: Handle old glibc without execveat(2)
Add an execveat(2) wrapper because glibc < 2.34 does not have one. This fixes the check-exec tests and samples. Cc: Günther Noack <gnoack@google.com> Cc: Jeff Xu <jeffxu@chromium.org> Cc: Kees Cook <kees@kernel.org> Cc: Mimi Zohar <zohar@linux.ibm.com> Cc: Paul Moore <paul@paul-moore.com> Cc: Roberto Sassu <roberto.sassu@huawei.com> Cc: Serge Hallyn <serge@hallyn.com> Cc: Stefan Berger <stefanb@linux.ibm.com> Cc: Stephen Rothwell <sfr@canb.auug.org.au> Reported-by: Nathan Chancellor <nathan@kernel.org> Closes: https://lore.kernel.org/r/20250114205645.GA2825031@ax162 Signed-off-by: Mickaël Salaün <mic@digikod.net> Reviewed-by: Günther Noack <gnoack3000@gmail.com> Link: https://lore.kernel.org/r/20250115144753.311152-1-mic@digikod.net Signed-off-by: Kees Cook <kees@kernel.org>
1 parent 95b3cda commit 38567b9

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

samples/check-exec/inc.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,15 @@
2121
#include <stdlib.h>
2222
#include <string.h>
2323
#include <sys/prctl.h>
24+
#include <sys/syscall.h>
2425
#include <unistd.h>
2526

27+
static int sys_execveat(int dirfd, const char *pathname, char *const argv[],
28+
char *const envp[], int flags)
29+
{
30+
return syscall(__NR_execveat, dirfd, pathname, argv, envp, flags);
31+
}
32+
2633
/* Returns 1 on error, 0 otherwise. */
2734
static int interpret_buffer(char *buffer, size_t buffer_size)
2835
{
@@ -78,8 +85,8 @@ static int interpret_stream(FILE *script, char *const script_name,
7885
* script execution. We must use the script file descriptor instead of
7986
* the script path name to avoid race conditions.
8087
*/
81-
err = execveat(fileno(script), "", script_argv, envp,
82-
AT_EMPTY_PATH | AT_EXECVE_CHECK);
88+
err = sys_execveat(fileno(script), "", script_argv, envp,
89+
AT_EMPTY_PATH | AT_EXECVE_CHECK);
8390
if (err && restrict_stream) {
8491
perror("ERROR: Script execution check");
8592
return 1;

tools/testing/selftests/exec/check-exec.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include <sys/prctl.h>
2323
#include <sys/socket.h>
2424
#include <sys/stat.h>
25+
#include <sys/syscall.h>
2526
#include <sys/sysmacros.h>
2627
#include <unistd.h>
2728

@@ -31,6 +32,12 @@
3132

3233
#include "../kselftest_harness.h"
3334

35+
static int sys_execveat(int dirfd, const char *pathname, char *const argv[],
36+
char *const envp[], int flags)
37+
{
38+
return syscall(__NR_execveat, dirfd, pathname, argv, envp, flags);
39+
}
40+
3441
static void drop_privileges(struct __test_metadata *const _metadata)
3542
{
3643
const unsigned int noroot = SECBIT_NOROOT | SECBIT_NOROOT_LOCKED;
@@ -219,8 +226,8 @@ static void test_exec_fd(struct __test_metadata *_metadata, const int fd,
219226
* test framework as an error. With AT_EXECVE_CHECK, we only check a
220227
* potential successful execution.
221228
*/
222-
access_ret =
223-
execveat(fd, "", argv, NULL, AT_EMPTY_PATH | AT_EXECVE_CHECK);
229+
access_ret = sys_execveat(fd, "", argv, NULL,
230+
AT_EMPTY_PATH | AT_EXECVE_CHECK);
224231
access_errno = errno;
225232
if (err_code) {
226233
EXPECT_EQ(-1, access_ret);

tools/testing/selftests/landlock/fs_test.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ int open_tree(int dfd, const char *filename, unsigned int flags)
5959
}
6060
#endif
6161

62+
static int sys_execveat(int dirfd, const char *pathname, char *const argv[],
63+
char *const envp[], int flags)
64+
{
65+
return syscall(__NR_execveat, dirfd, pathname, argv, envp, flags);
66+
}
67+
6268
#ifndef RENAME_EXCHANGE
6369
#define RENAME_EXCHANGE (1 << 1)
6470
#endif
@@ -2018,8 +2024,8 @@ static void test_check_exec(struct __test_metadata *const _metadata,
20182024
int ret;
20192025
char *const argv[] = { (char *)path, NULL };
20202026

2021-
ret = execveat(AT_FDCWD, path, argv, NULL,
2022-
AT_EMPTY_PATH | AT_EXECVE_CHECK);
2027+
ret = sys_execveat(AT_FDCWD, path, argv, NULL,
2028+
AT_EMPTY_PATH | AT_EXECVE_CHECK);
20232029
if (err) {
20242030
EXPECT_EQ(-1, ret);
20252031
EXPECT_EQ(errno, err);

0 commit comments

Comments
 (0)