1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2014 Google, Inc.
5 * Selftests for execveat(2).
9 #define _GNU_SOURCE /* to get O_PATH, AT_EMPTY_PATH */
11 #include <sys/sendfile.h>
13 #include <sys/syscall.h>
14 #include <sys/types.h>
24 #include "../kselftest.h"
26 #define TESTS_EXPECTED 51
27 #define TEST_NAME_LEN (PATH_MAX * 4)
29 static char longpath[2 * PATH_MAX] = "";
30 static char *envp[] = { "IN_TEST=yes", NULL, NULL };
31 static char *argv[] = { "execveat", "99", NULL };
33 static int execveat_(int fd, const char *path, char **argv, char **envp,
37 return syscall(__NR_execveat, fd, path, argv, envp, flags);
44 #define check_execveat_fail(fd, path, flags, errno) \
45 _check_execveat_fail(fd, path, flags, errno, #errno)
46 static int _check_execveat_fail(int fd, const char *path, int flags,
47 int expected_errno, const char *errno_str)
49 char test_name[TEST_NAME_LEN];
53 snprintf(test_name, sizeof(test_name),
54 "Check failure of execveat(%d, '%s', %d) with %s",
55 fd, path?:"(null)", flags, errno_str);
56 rc = execveat_(fd, path, argv, envp, flags);
59 ksft_print_msg("unexpected success from execveat(2)\n");
60 ksft_test_result_fail("%s\n", test_name);
63 if (errno != expected_errno) {
64 ksft_print_msg("expected errno %d (%s) not %d (%s)\n",
65 expected_errno, strerror(expected_errno),
66 errno, strerror(errno));
67 ksft_test_result_fail("%s\n", test_name);
70 ksft_test_result_pass("%s\n", test_name);
74 static int check_execveat_invoked_rc(int fd, const char *path, int flags,
75 int expected_rc, int expected_rc2)
77 char test_name[TEST_NAME_LEN];
81 int pathlen = path ? strlen(path) : 0;
84 snprintf(test_name, sizeof(test_name),
85 "Check success of execveat(%d, '%.20s...%s', %d)... ",
86 fd, path, (path + pathlen - 20), flags);
88 snprintf(test_name, sizeof(test_name),
89 "Check success of execveat(%d, '%s', %d)... ",
90 fd, path?:"(null)", flags);
94 ksft_perror("fork() failed");
95 ksft_test_result_fail("%s\n", test_name);
99 /* Child: do execveat(). */
100 rc = execveat_(fd, path, argv, envp, flags);
101 ksft_print_msg("execveat() failed, rc=%d errno=%d (%s)\n",
102 rc, errno, strerror(errno));
103 ksft_test_result_fail("%s\n", test_name);
104 exit(1); /* should not reach here */
106 /* Parent: wait for & check child's exit status. */
107 rc = waitpid(child, &status, 0);
109 ksft_print_msg("waitpid(%d,...) returned %d\n", child, rc);
110 ksft_test_result_fail("%s\n", test_name);
113 if (!WIFEXITED(status)) {
114 ksft_print_msg("child %d did not exit cleanly, status=%08x\n",
116 ksft_test_result_fail("%s\n", test_name);
119 if ((WEXITSTATUS(status) != expected_rc) &&
120 (WEXITSTATUS(status) != expected_rc2)) {
121 ksft_print_msg("child %d exited with %d not %d nor %d\n",
122 child, WEXITSTATUS(status), expected_rc,
124 ksft_test_result_fail("%s\n", test_name);
127 ksft_test_result_pass("%s\n", test_name);
131 static int check_execveat(int fd, const char *path, int flags)
133 return check_execveat_invoked_rc(fd, path, flags, 99, 99);
136 static char *concat(const char *left, const char *right)
138 char *result = malloc(strlen(left) + strlen(right) + 1);
140 strcpy(result, left);
141 strcat(result, right);
145 static int open_or_die(const char *filename, int flags)
147 int fd = open(filename, flags);
150 ksft_exit_fail_msg("Failed to open '%s'; "
151 "check prerequisites are available\n", filename);
155 static void exe_cp(const char *src, const char *dest)
157 int in_fd = open_or_die(src, O_RDONLY);
158 int out_fd = open(dest, O_RDWR|O_CREAT|O_TRUNC, 0755);
162 sendfile(out_fd, in_fd, NULL, info.st_size);
167 #define XX_DIR_LEN 200
168 static int check_execveat_pathmax(int root_dfd, const char *src, int is_script)
172 char longname[XX_DIR_LEN + 1];
175 if (*longpath == '\0') {
176 /* Create a filename close to PATH_MAX in length */
177 char *cwd = getcwd(NULL, 0);
180 ksft_perror("Failed to getcwd()");
183 strcpy(longpath, cwd);
184 strcat(longpath, "/");
185 memset(longname, 'x', XX_DIR_LEN - 1);
186 longname[XX_DIR_LEN - 1] = '/';
187 longname[XX_DIR_LEN] = '\0';
188 count = (PATH_MAX - 3 - strlen(cwd)) / XX_DIR_LEN;
189 for (ii = 0; ii < count; ii++) {
190 strcat(longpath, longname);
191 mkdir(longpath, 0755);
193 len = (PATH_MAX - 3 - strlen(cwd)) - (count * XX_DIR_LEN);
196 memset(longname, 'y', len);
197 longname[len] = '\0';
198 strcat(longpath, longname);
201 exe_cp(src, longpath);
204 * Execute as a pre-opened file descriptor, which works whether this is
205 * a script or not (because the interpreter sees a filename like
208 fd = open(longpath, O_RDONLY);
210 ksft_print_msg("Invoke copy of '%s' via filename of length %zu:\n",
211 src, strlen(longpath));
212 fail += check_execveat(fd, "", AT_EMPTY_PATH);
214 ksft_print_msg("Failed to open length %zu filename, errno=%d (%s)\n",
215 strlen(longpath), errno, strerror(errno));
220 * Execute as a long pathname relative to "/". If this is a script,
221 * the interpreter will launch but fail to open the script because its
222 * name ("/dev/fd/5/xxx....") is bigger than PATH_MAX.
224 * The failure code is usually 127 (POSIX: "If a command is not found,
225 * the exit status shall be 127."), but some systems give 126 (POSIX:
226 * "If the command name is found, but it is not an executable utility,
227 * the exit status shall be 126."), so allow either.
230 fail += check_execveat_invoked_rc(root_dfd, longpath + 1, 0,
233 fail += check_execveat(root_dfd, longpath + 1, 0);
238 static int run_tests(void)
241 char *fullname = realpath("execveat", NULL);
242 char *fullname_script = realpath("script", NULL);
243 char *fullname_symlink = concat(fullname, ".symlink");
244 int subdir_dfd = open_or_die("subdir", O_DIRECTORY|O_RDONLY);
245 int subdir_dfd_ephemeral = open_or_die("subdir.ephemeral",
246 O_DIRECTORY|O_RDONLY);
247 int dot_dfd = open_or_die(".", O_DIRECTORY|O_RDONLY);
248 int root_dfd = open_or_die("/", O_DIRECTORY|O_RDONLY);
249 int dot_dfd_path = open_or_die(".", O_DIRECTORY|O_RDONLY|O_PATH);
250 int dot_dfd_cloexec = open_or_die(".", O_DIRECTORY|O_RDONLY|O_CLOEXEC);
251 int fd = open_or_die("execveat", O_RDONLY);
252 int fd_path = open_or_die("execveat", O_RDONLY|O_PATH);
253 int fd_symlink = open_or_die("execveat.symlink", O_RDONLY);
254 int fd_denatured = open_or_die("execveat.denatured", O_RDONLY);
255 int fd_denatured_path = open_or_die("execveat.denatured",
257 int fd_script = open_or_die("script", O_RDONLY);
258 int fd_ephemeral = open_or_die("execveat.ephemeral", O_RDONLY);
259 int fd_ephemeral_path = open_or_die("execveat.path.ephemeral",
261 int fd_script_ephemeral = open_or_die("script.ephemeral", O_RDONLY);
262 int fd_cloexec = open_or_die("execveat", O_RDONLY|O_CLOEXEC);
263 int fd_script_cloexec = open_or_die("script", O_RDONLY|O_CLOEXEC);
265 /* Check if we have execveat at all, and bail early if not */
267 execveat_(-1, NULL, NULL, NULL, 0);
268 if (errno == ENOSYS) {
270 "ENOSYS calling execveat - no kernel support?\n");
273 /* Change file position to confirm it doesn't affect anything */
274 lseek(fd, 10, SEEK_SET);
276 /* Normal executable file: */
278 fail += check_execveat(subdir_dfd, "../execveat", 0);
279 fail += check_execveat(dot_dfd, "execveat", 0);
280 fail += check_execveat(dot_dfd_path, "execveat", 0);
282 fail += check_execveat(AT_FDCWD, fullname, 0);
283 /* absolute path with nonsense dfd */
284 fail += check_execveat(99, fullname, 0);
286 fail += check_execveat(fd, "", AT_EMPTY_PATH);
287 /* O_CLOEXEC fd + no path */
288 fail += check_execveat(fd_cloexec, "", AT_EMPTY_PATH);
290 fail += check_execveat(fd_path, "", AT_EMPTY_PATH);
292 /* Mess with executable file that's already open: */
293 /* fd + no path to a file that's been renamed */
294 rename("execveat.ephemeral", "execveat.moved");
295 fail += check_execveat(fd_ephemeral, "", AT_EMPTY_PATH);
296 /* fd + no path to a file that's been deleted */
297 unlink("execveat.moved"); /* remove the file now fd open */
298 fail += check_execveat(fd_ephemeral, "", AT_EMPTY_PATH);
300 /* Mess with executable file that's already open with O_PATH */
301 /* fd + no path to a file that's been deleted */
302 unlink("execveat.path.ephemeral");
303 fail += check_execveat(fd_ephemeral_path, "", AT_EMPTY_PATH);
305 /* Invalid argument failures */
306 fail += check_execveat_fail(fd, "", 0, ENOENT);
307 fail += check_execveat_fail(fd, NULL, AT_EMPTY_PATH, EFAULT);
309 /* Symlink to executable file: */
311 fail += check_execveat(dot_dfd, "execveat.symlink", 0);
312 fail += check_execveat(dot_dfd_path, "execveat.symlink", 0);
314 fail += check_execveat(AT_FDCWD, fullname_symlink, 0);
315 /* fd + no path, even with AT_SYMLINK_NOFOLLOW (already followed) */
316 fail += check_execveat(fd_symlink, "", AT_EMPTY_PATH);
317 fail += check_execveat(fd_symlink, "",
318 AT_EMPTY_PATH|AT_SYMLINK_NOFOLLOW);
320 /* Symlink fails when AT_SYMLINK_NOFOLLOW set: */
322 fail += check_execveat_fail(dot_dfd, "execveat.symlink",
323 AT_SYMLINK_NOFOLLOW, ELOOP);
324 fail += check_execveat_fail(dot_dfd_path, "execveat.symlink",
325 AT_SYMLINK_NOFOLLOW, ELOOP);
327 fail += check_execveat_fail(AT_FDCWD, fullname_symlink,
328 AT_SYMLINK_NOFOLLOW, ELOOP);
330 /* Non-regular file failure */
331 fail += check_execveat_fail(dot_dfd, "pipe", 0, EACCES);
334 /* Shell script wrapping executable file: */
336 fail += check_execveat(subdir_dfd, "../script", 0);
337 fail += check_execveat(dot_dfd, "script", 0);
338 fail += check_execveat(dot_dfd_path, "script", 0);
340 fail += check_execveat(AT_FDCWD, fullname_script, 0);
342 fail += check_execveat(fd_script, "", AT_EMPTY_PATH);
343 fail += check_execveat(fd_script, "",
344 AT_EMPTY_PATH|AT_SYMLINK_NOFOLLOW);
345 /* O_CLOEXEC fd fails for a script (as script file inaccessible) */
346 fail += check_execveat_fail(fd_script_cloexec, "", AT_EMPTY_PATH,
348 fail += check_execveat_fail(dot_dfd_cloexec, "script", 0, ENOENT);
350 /* Mess with script file that's already open: */
351 /* fd + no path to a file that's been renamed */
352 rename("script.ephemeral", "script.moved");
353 fail += check_execveat(fd_script_ephemeral, "", AT_EMPTY_PATH);
354 /* fd + no path to a file that's been deleted */
355 unlink("script.moved"); /* remove the file while fd open */
356 fail += check_execveat(fd_script_ephemeral, "", AT_EMPTY_PATH);
358 /* Rename a subdirectory in the path: */
359 rename("subdir.ephemeral", "subdir.moved");
360 fail += check_execveat(subdir_dfd_ephemeral, "../script", 0);
361 fail += check_execveat(subdir_dfd_ephemeral, "script", 0);
362 /* Remove the subdir and its contents */
363 unlink("subdir.moved/script");
364 unlink("subdir.moved");
365 /* Shell loads via deleted subdir OK because name starts with .. */
366 fail += check_execveat(subdir_dfd_ephemeral, "../script", 0);
367 fail += check_execveat_fail(subdir_dfd_ephemeral, "script", 0, ENOENT);
369 /* Flag values other than AT_SYMLINK_NOFOLLOW => EINVAL */
370 fail += check_execveat_fail(dot_dfd, "execveat", 0xFFFF, EINVAL);
371 /* Invalid path => ENOENT */
372 fail += check_execveat_fail(dot_dfd, "no-such-file", 0, ENOENT);
373 fail += check_execveat_fail(dot_dfd_path, "no-such-file", 0, ENOENT);
374 fail += check_execveat_fail(AT_FDCWD, "no-such-file", 0, ENOENT);
375 /* Attempt to execute directory => EACCES */
376 fail += check_execveat_fail(dot_dfd, "", AT_EMPTY_PATH, EACCES);
377 /* Attempt to execute non-executable => EACCES */
378 fail += check_execveat_fail(dot_dfd, "Makefile", 0, EACCES);
379 fail += check_execveat_fail(fd_denatured, "", AT_EMPTY_PATH, EACCES);
380 fail += check_execveat_fail(fd_denatured_path, "", AT_EMPTY_PATH,
382 /* Attempt to execute nonsense FD => EBADF */
383 fail += check_execveat_fail(99, "", AT_EMPTY_PATH, EBADF);
384 fail += check_execveat_fail(99, "execveat", 0, EBADF);
385 /* Attempt to execute relative to non-directory => ENOTDIR */
386 fail += check_execveat_fail(fd, "execveat", 0, ENOTDIR);
388 fail += check_execveat_pathmax(root_dfd, "execveat", 0);
389 fail += check_execveat_pathmax(root_dfd, "script", 1);
393 static void prerequisites(void)
396 const char *script = "#!/bin/bash\nexit $*\n";
398 /* Create ephemeral copies of files */
399 exe_cp("execveat", "execveat.ephemeral");
400 exe_cp("execveat", "execveat.path.ephemeral");
401 exe_cp("script", "script.ephemeral");
402 mkdir("subdir.ephemeral", 0755);
404 fd = open("subdir.ephemeral/script", O_RDWR|O_CREAT|O_TRUNC, 0755);
405 write(fd, script, strlen(script));
408 mkfifo("pipe", 0755);
411 int main(int argc, char **argv)
415 const char *verbose = getenv("VERBOSE");
418 /* If we are invoked with an argument, don't run tests. */
419 const char *in_test = getenv("IN_TEST");
422 ksft_print_msg("invoked with:\n");
423 for (ii = 0; ii < argc; ii++)
424 ksft_print_msg("\t[%d]='%s\n'", ii, argv[ii]);
427 /* Check expected environment transferred. */
428 if (!in_test || strcmp(in_test, "yes") != 0) {
429 ksft_print_msg("no IN_TEST=yes in env\n");
433 /* Use the final argument as an exit code. */
434 rc = atoi(argv[argc - 1]);
438 ksft_set_plan(TESTS_EXPECTED);
441 envp[1] = "VERBOSE=1";
444 printf("%d tests failed\n", rc);