xref: /linux/tools/testing/selftests/exec/execveat.c (revision 0ef58ccb)
14f19048fSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2c9b26b81SDavid Drysdale /*
3c9b26b81SDavid Drysdale  * Copyright (c) 2014 Google, Inc.
4c9b26b81SDavid Drysdale  *
5c9b26b81SDavid Drysdale  * Selftests for execveat(2).
6c9b26b81SDavid Drysdale  */
7c9b26b81SDavid Drysdale 
861016db1SKees Cook #ifndef _GNU_SOURCE
9c9b26b81SDavid Drysdale #define _GNU_SOURCE  /* to get O_PATH, AT_EMPTY_PATH */
1061016db1SKees Cook #endif
11c9b26b81SDavid Drysdale #include <sys/sendfile.h>
12c9b26b81SDavid Drysdale #include <sys/stat.h>
13c9b26b81SDavid Drysdale #include <sys/syscall.h>
14c9b26b81SDavid Drysdale #include <sys/types.h>
15c9b26b81SDavid Drysdale #include <sys/wait.h>
16c9b26b81SDavid Drysdale #include <errno.h>
17c9b26b81SDavid Drysdale #include <fcntl.h>
18c9b26b81SDavid Drysdale #include <limits.h>
19c9b26b81SDavid Drysdale #include <stdio.h>
20c9b26b81SDavid Drysdale #include <stdlib.h>
21c9b26b81SDavid Drysdale #include <string.h>
22c9b26b81SDavid Drysdale #include <unistd.h>
23c9b26b81SDavid Drysdale 
24964224d7SShuah Khan (Samsung OSG) #include "../kselftest.h"
25964224d7SShuah Khan (Samsung OSG) 
2647903c1dSMark Brown #define TESTS_EXPECTED 51
2747903c1dSMark Brown #define TEST_NAME_LEN (PATH_MAX * 4)
2847903c1dSMark Brown 
29c9b26b81SDavid Drysdale static char longpath[2 * PATH_MAX] = "";
30c9b26b81SDavid Drysdale static char *envp[] = { "IN_TEST=yes", NULL, NULL };
31c9b26b81SDavid Drysdale static char *argv[] = { "execveat", "99", NULL };
32c9b26b81SDavid Drysdale 
execveat_(int fd,const char * path,char ** argv,char ** envp,int flags)33c9b26b81SDavid Drysdale static int execveat_(int fd, const char *path, char **argv, char **envp,
34c9b26b81SDavid Drysdale 		     int flags)
35c9b26b81SDavid Drysdale {
36c9b26b81SDavid Drysdale #ifdef __NR_execveat
37c9b26b81SDavid Drysdale 	return syscall(__NR_execveat, fd, path, argv, envp, flags);
38c9b26b81SDavid Drysdale #else
399a0b5745SMichael Ellerman 	errno = ENOSYS;
40c9b26b81SDavid Drysdale 	return -1;
41c9b26b81SDavid Drysdale #endif
42c9b26b81SDavid Drysdale }
43c9b26b81SDavid Drysdale 
44c9b26b81SDavid Drysdale #define check_execveat_fail(fd, path, flags, errno)	\
45c9b26b81SDavid Drysdale 	_check_execveat_fail(fd, path, flags, errno, #errno)
_check_execveat_fail(int fd,const char * path,int flags,int expected_errno,const char * errno_str)46c9b26b81SDavid Drysdale static int _check_execveat_fail(int fd, const char *path, int flags,
47c9b26b81SDavid Drysdale 				int expected_errno, const char *errno_str)
48c9b26b81SDavid Drysdale {
4947903c1dSMark Brown 	char test_name[TEST_NAME_LEN];
50c9b26b81SDavid Drysdale 	int rc;
51c9b26b81SDavid Drysdale 
52c9b26b81SDavid Drysdale 	errno = 0;
5347903c1dSMark Brown 	snprintf(test_name, sizeof(test_name),
5447903c1dSMark Brown 		 "Check failure of execveat(%d, '%s', %d) with %s",
55c9b26b81SDavid Drysdale 		 fd, path?:"(null)", flags, errno_str);
56c9b26b81SDavid Drysdale 	rc = execveat_(fd, path, argv, envp, flags);
57c9b26b81SDavid Drysdale 
58c9b26b81SDavid Drysdale 	if (rc > 0) {
5947903c1dSMark Brown 		ksft_print_msg("unexpected success from execveat(2)\n");
6047903c1dSMark Brown 		ksft_test_result_fail("%s\n", test_name);
61c9b26b81SDavid Drysdale 		return 1;
62c9b26b81SDavid Drysdale 	}
63c9b26b81SDavid Drysdale 	if (errno != expected_errno) {
6447903c1dSMark Brown 		ksft_print_msg("expected errno %d (%s) not %d (%s)\n",
65c9b26b81SDavid Drysdale 			       expected_errno, strerror(expected_errno),
66c9b26b81SDavid Drysdale 			       errno, strerror(errno));
6747903c1dSMark Brown 		ksft_test_result_fail("%s\n", test_name);
68c9b26b81SDavid Drysdale 		return 1;
69c9b26b81SDavid Drysdale 	}
7047903c1dSMark Brown 	ksft_test_result_pass("%s\n", test_name);
71c9b26b81SDavid Drysdale 	return 0;
72c9b26b81SDavid Drysdale }
73c9b26b81SDavid Drysdale 
check_execveat_invoked_rc(int fd,const char * path,int flags,int expected_rc,int expected_rc2)74c9b26b81SDavid Drysdale static int check_execveat_invoked_rc(int fd, const char *path, int flags,
75cd805f36SDavid Drysdale 				     int expected_rc, int expected_rc2)
76c9b26b81SDavid Drysdale {
7747903c1dSMark Brown 	char test_name[TEST_NAME_LEN];
78c9b26b81SDavid Drysdale 	int status;
79c9b26b81SDavid Drysdale 	int rc;
80c9b26b81SDavid Drysdale 	pid_t child;
81c9b26b81SDavid Drysdale 	int pathlen = path ? strlen(path) : 0;
82c9b26b81SDavid Drysdale 
83c9b26b81SDavid Drysdale 	if (pathlen > 40)
8447903c1dSMark Brown 		snprintf(test_name, sizeof(test_name),
8547903c1dSMark Brown 			 "Check success of execveat(%d, '%.20s...%s', %d)... ",
86c9b26b81SDavid Drysdale 			 fd, path, (path + pathlen - 20), flags);
87c9b26b81SDavid Drysdale 	else
8847903c1dSMark Brown 		snprintf(test_name, sizeof(test_name),
8947903c1dSMark Brown 			 "Check success of execveat(%d, '%s', %d)... ",
90c9b26b81SDavid Drysdale 			 fd, path?:"(null)", flags);
9147903c1dSMark Brown 
92c9b26b81SDavid Drysdale 	child = fork();
93c9b26b81SDavid Drysdale 	if (child < 0) {
9447903c1dSMark Brown 		ksft_perror("fork() failed");
9547903c1dSMark Brown 		ksft_test_result_fail("%s\n", test_name);
96c9b26b81SDavid Drysdale 		return 1;
97c9b26b81SDavid Drysdale 	}
98c9b26b81SDavid Drysdale 	if (child == 0) {
99c9b26b81SDavid Drysdale 		/* Child: do execveat(). */
100c9b26b81SDavid Drysdale 		rc = execveat_(fd, path, argv, envp, flags);
101*0ef58ccbSKees Cook 		ksft_print_msg("child execveat() failed, rc=%d errno=%d (%s)\n",
102c9b26b81SDavid Drysdale 			       rc, errno, strerror(errno));
103*0ef58ccbSKees Cook 		exit(errno);
104c9b26b81SDavid Drysdale 	}
105c9b26b81SDavid Drysdale 	/* Parent: wait for & check child's exit status. */
106c9b26b81SDavid Drysdale 	rc = waitpid(child, &status, 0);
107c9b26b81SDavid Drysdale 	if (rc != child) {
10847903c1dSMark Brown 		ksft_print_msg("waitpid(%d,...) returned %d\n", child, rc);
10947903c1dSMark Brown 		ksft_test_result_fail("%s\n", test_name);
110c9b26b81SDavid Drysdale 		return 1;
111c9b26b81SDavid Drysdale 	}
112c9b26b81SDavid Drysdale 	if (!WIFEXITED(status)) {
11347903c1dSMark Brown 		ksft_print_msg("child %d did not exit cleanly, status=%08x\n",
114c9b26b81SDavid Drysdale 			       child, status);
11547903c1dSMark Brown 		ksft_test_result_fail("%s\n", test_name);
116c9b26b81SDavid Drysdale 		return 1;
117c9b26b81SDavid Drysdale 	}
118cd805f36SDavid Drysdale 	if ((WEXITSTATUS(status) != expected_rc) &&
119cd805f36SDavid Drysdale 	    (WEXITSTATUS(status) != expected_rc2)) {
12047903c1dSMark Brown 		ksft_print_msg("child %d exited with %d not %d nor %d\n",
12147903c1dSMark Brown 			       child, WEXITSTATUS(status), expected_rc,
12247903c1dSMark Brown 			       expected_rc2);
12347903c1dSMark Brown 		ksft_test_result_fail("%s\n", test_name);
124c9b26b81SDavid Drysdale 		return 1;
125c9b26b81SDavid Drysdale 	}
12647903c1dSMark Brown 	ksft_test_result_pass("%s\n", test_name);
127c9b26b81SDavid Drysdale 	return 0;
128c9b26b81SDavid Drysdale }
129c9b26b81SDavid Drysdale 
check_execveat(int fd,const char * path,int flags)130c9b26b81SDavid Drysdale static int check_execveat(int fd, const char *path, int flags)
131c9b26b81SDavid Drysdale {
132cd805f36SDavid Drysdale 	return check_execveat_invoked_rc(fd, path, flags, 99, 99);
133c9b26b81SDavid Drysdale }
134c9b26b81SDavid Drysdale 
concat(const char * left,const char * right)135c9b26b81SDavid Drysdale static char *concat(const char *left, const char *right)
136c9b26b81SDavid Drysdale {
137c9b26b81SDavid Drysdale 	char *result = malloc(strlen(left) + strlen(right) + 1);
138c9b26b81SDavid Drysdale 
139c9b26b81SDavid Drysdale 	strcpy(result, left);
140c9b26b81SDavid Drysdale 	strcat(result, right);
141c9b26b81SDavid Drysdale 	return result;
142c9b26b81SDavid Drysdale }
143c9b26b81SDavid Drysdale 
open_or_die(const char * filename,int flags)144c9b26b81SDavid Drysdale static int open_or_die(const char *filename, int flags)
145c9b26b81SDavid Drysdale {
146c9b26b81SDavid Drysdale 	int fd = open(filename, flags);
147c9b26b81SDavid Drysdale 
14847903c1dSMark Brown 	if (fd < 0)
14947903c1dSMark Brown 		ksft_exit_fail_msg("Failed to open '%s'; "
150c9b26b81SDavid Drysdale 			"check prerequisites are available\n", filename);
151c9b26b81SDavid Drysdale 	return fd;
152c9b26b81SDavid Drysdale }
153c9b26b81SDavid Drysdale 
exe_cp(const char * src,const char * dest)154c9b26b81SDavid Drysdale static void exe_cp(const char *src, const char *dest)
155c9b26b81SDavid Drysdale {
156c9b26b81SDavid Drysdale 	int in_fd = open_or_die(src, O_RDONLY);
157c9b26b81SDavid Drysdale 	int out_fd = open(dest, O_RDWR|O_CREAT|O_TRUNC, 0755);
158c9b26b81SDavid Drysdale 	struct stat info;
159c9b26b81SDavid Drysdale 
160c9b26b81SDavid Drysdale 	fstat(in_fd, &info);
161c9b26b81SDavid Drysdale 	sendfile(out_fd, in_fd, NULL, info.st_size);
162c9b26b81SDavid Drysdale 	close(in_fd);
163c9b26b81SDavid Drysdale 	close(out_fd);
164c9b26b81SDavid Drysdale }
165c9b26b81SDavid Drysdale 
166c9b26b81SDavid Drysdale #define XX_DIR_LEN 200
check_execveat_pathmax(int root_dfd,const char * src,int is_script)1672d80c92dSSteve Muckle static int check_execveat_pathmax(int root_dfd, const char *src, int is_script)
168c9b26b81SDavid Drysdale {
169c9b26b81SDavid Drysdale 	int fail = 0;
170c9b26b81SDavid Drysdale 	int ii, count, len;
171c9b26b81SDavid Drysdale 	char longname[XX_DIR_LEN + 1];
172c9b26b81SDavid Drysdale 	int fd;
173c9b26b81SDavid Drysdale 
174c9b26b81SDavid Drysdale 	if (*longpath == '\0') {
175c9b26b81SDavid Drysdale 		/* Create a filename close to PATH_MAX in length */
1762d80c92dSSteve Muckle 		char *cwd = getcwd(NULL, 0);
1772d80c92dSSteve Muckle 
1782d80c92dSSteve Muckle 		if (!cwd) {
17947903c1dSMark Brown 			ksft_perror("Failed to getcwd()");
1802d80c92dSSteve Muckle 			return 2;
1812d80c92dSSteve Muckle 		}
1822d80c92dSSteve Muckle 		strcpy(longpath, cwd);
1832d80c92dSSteve Muckle 		strcat(longpath, "/");
184c9b26b81SDavid Drysdale 		memset(longname, 'x', XX_DIR_LEN - 1);
185c9b26b81SDavid Drysdale 		longname[XX_DIR_LEN - 1] = '/';
186c9b26b81SDavid Drysdale 		longname[XX_DIR_LEN] = '\0';
1872d80c92dSSteve Muckle 		count = (PATH_MAX - 3 - strlen(cwd)) / XX_DIR_LEN;
188c9b26b81SDavid Drysdale 		for (ii = 0; ii < count; ii++) {
189c9b26b81SDavid Drysdale 			strcat(longpath, longname);
190c9b26b81SDavid Drysdale 			mkdir(longpath, 0755);
191c9b26b81SDavid Drysdale 		}
1922d80c92dSSteve Muckle 		len = (PATH_MAX - 3 - strlen(cwd)) - (count * XX_DIR_LEN);
193c9b26b81SDavid Drysdale 		if (len <= 0)
194c9b26b81SDavid Drysdale 			len = 1;
195c9b26b81SDavid Drysdale 		memset(longname, 'y', len);
196c9b26b81SDavid Drysdale 		longname[len] = '\0';
197c9b26b81SDavid Drysdale 		strcat(longpath, longname);
1982d80c92dSSteve Muckle 		free(cwd);
199c9b26b81SDavid Drysdale 	}
200c9b26b81SDavid Drysdale 	exe_cp(src, longpath);
201c9b26b81SDavid Drysdale 
202c9b26b81SDavid Drysdale 	/*
203c9b26b81SDavid Drysdale 	 * Execute as a pre-opened file descriptor, which works whether this is
204c9b26b81SDavid Drysdale 	 * a script or not (because the interpreter sees a filename like
205c9b26b81SDavid Drysdale 	 * "/dev/fd/20").
206c9b26b81SDavid Drysdale 	 */
207c9b26b81SDavid Drysdale 	fd = open(longpath, O_RDONLY);
208c9b26b81SDavid Drysdale 	if (fd > 0) {
20947903c1dSMark Brown 		ksft_print_msg("Invoke copy of '%s' via filename of length %zu:\n",
210c9b26b81SDavid Drysdale 			       src, strlen(longpath));
211c9b26b81SDavid Drysdale 		fail += check_execveat(fd, "", AT_EMPTY_PATH);
212c9b26b81SDavid Drysdale 	} else {
21347903c1dSMark Brown 		ksft_print_msg("Failed to open length %zu filename, errno=%d (%s)\n",
214c9b26b81SDavid Drysdale 			       strlen(longpath), errno, strerror(errno));
215c9b26b81SDavid Drysdale 		fail++;
216c9b26b81SDavid Drysdale 	}
217c9b26b81SDavid Drysdale 
218c9b26b81SDavid Drysdale 	/*
2192d80c92dSSteve Muckle 	 * Execute as a long pathname relative to "/".  If this is a script,
220c9b26b81SDavid Drysdale 	 * the interpreter will launch but fail to open the script because its
221c9b26b81SDavid Drysdale 	 * name ("/dev/fd/5/xxx....") is bigger than PATH_MAX.
222cd805f36SDavid Drysdale 	 *
223cd805f36SDavid Drysdale 	 * The failure code is usually 127 (POSIX: "If a command is not found,
224cd805f36SDavid Drysdale 	 * the exit status shall be 127."), but some systems give 126 (POSIX:
225cd805f36SDavid Drysdale 	 * "If the command name is found, but it is not an executable utility,
226cd805f36SDavid Drysdale 	 * the exit status shall be 126."), so allow either.
227c9b26b81SDavid Drysdale 	 */
228*0ef58ccbSKees Cook 	if (is_script) {
229*0ef58ccbSKees Cook 		ksft_print_msg("Invoke script via root_dfd and relative filename\n");
2302d80c92dSSteve Muckle 		fail += check_execveat_invoked_rc(root_dfd, longpath + 1, 0,
231cd805f36SDavid Drysdale 						  127, 126);
232*0ef58ccbSKees Cook 	} else {
233*0ef58ccbSKees Cook 		ksft_print_msg("Invoke exec via root_dfd and relative filename\n");
2342d80c92dSSteve Muckle 		fail += check_execveat(root_dfd, longpath + 1, 0);
235*0ef58ccbSKees Cook 	}
236c9b26b81SDavid Drysdale 
237c9b26b81SDavid Drysdale 	return fail;
238c9b26b81SDavid Drysdale }
239c9b26b81SDavid Drysdale 
run_tests(void)240c9b26b81SDavid Drysdale static int run_tests(void)
241c9b26b81SDavid Drysdale {
242c9b26b81SDavid Drysdale 	int fail = 0;
243c9b26b81SDavid Drysdale 	char *fullname = realpath("execveat", NULL);
244c9b26b81SDavid Drysdale 	char *fullname_script = realpath("script", NULL);
245c9b26b81SDavid Drysdale 	char *fullname_symlink = concat(fullname, ".symlink");
246c9b26b81SDavid Drysdale 	int subdir_dfd = open_or_die("subdir", O_DIRECTORY|O_RDONLY);
247c9b26b81SDavid Drysdale 	int subdir_dfd_ephemeral = open_or_die("subdir.ephemeral",
248c9b26b81SDavid Drysdale 					       O_DIRECTORY|O_RDONLY);
249c9b26b81SDavid Drysdale 	int dot_dfd = open_or_die(".", O_DIRECTORY|O_RDONLY);
2502d80c92dSSteve Muckle 	int root_dfd = open_or_die("/", O_DIRECTORY|O_RDONLY);
251c9b26b81SDavid Drysdale 	int dot_dfd_path = open_or_die(".", O_DIRECTORY|O_RDONLY|O_PATH);
252c9b26b81SDavid Drysdale 	int dot_dfd_cloexec = open_or_die(".", O_DIRECTORY|O_RDONLY|O_CLOEXEC);
253c9b26b81SDavid Drysdale 	int fd = open_or_die("execveat", O_RDONLY);
254c9b26b81SDavid Drysdale 	int fd_path = open_or_die("execveat", O_RDONLY|O_PATH);
255c9b26b81SDavid Drysdale 	int fd_symlink = open_or_die("execveat.symlink", O_RDONLY);
256c9b26b81SDavid Drysdale 	int fd_denatured = open_or_die("execveat.denatured", O_RDONLY);
257c9b26b81SDavid Drysdale 	int fd_denatured_path = open_or_die("execveat.denatured",
258c9b26b81SDavid Drysdale 					    O_RDONLY|O_PATH);
259c9b26b81SDavid Drysdale 	int fd_script = open_or_die("script", O_RDONLY);
260c9b26b81SDavid Drysdale 	int fd_ephemeral = open_or_die("execveat.ephemeral", O_RDONLY);
261c9b26b81SDavid Drysdale 	int fd_ephemeral_path = open_or_die("execveat.path.ephemeral",
262c9b26b81SDavid Drysdale 					    O_RDONLY|O_PATH);
263c9b26b81SDavid Drysdale 	int fd_script_ephemeral = open_or_die("script.ephemeral", O_RDONLY);
264c9b26b81SDavid Drysdale 	int fd_cloexec = open_or_die("execveat", O_RDONLY|O_CLOEXEC);
265c9b26b81SDavid Drysdale 	int fd_script_cloexec = open_or_die("script", O_RDONLY|O_CLOEXEC);
266c9b26b81SDavid Drysdale 
2679a0b5745SMichael Ellerman 	/* Check if we have execveat at all, and bail early if not */
2689a0b5745SMichael Ellerman 	errno = 0;
2699a0b5745SMichael Ellerman 	execveat_(-1, NULL, NULL, NULL, 0);
2709a0b5745SMichael Ellerman 	if (errno == ENOSYS) {
271964224d7SShuah Khan (Samsung OSG) 		ksft_exit_skip(
272964224d7SShuah Khan (Samsung OSG) 			"ENOSYS calling execveat - no kernel support?\n");
2739a0b5745SMichael Ellerman 	}
2749a0b5745SMichael Ellerman 
275c9b26b81SDavid Drysdale 	/* Change file position to confirm it doesn't affect anything */
276c9b26b81SDavid Drysdale 	lseek(fd, 10, SEEK_SET);
277c9b26b81SDavid Drysdale 
278c9b26b81SDavid Drysdale 	/* Normal executable file: */
279c9b26b81SDavid Drysdale 	/*   dfd + path */
280c9b26b81SDavid Drysdale 	fail += check_execveat(subdir_dfd, "../execveat", 0);
281c9b26b81SDavid Drysdale 	fail += check_execveat(dot_dfd, "execveat", 0);
282c9b26b81SDavid Drysdale 	fail += check_execveat(dot_dfd_path, "execveat", 0);
283c9b26b81SDavid Drysdale 	/*   absolute path */
284c9b26b81SDavid Drysdale 	fail += check_execveat(AT_FDCWD, fullname, 0);
285c9b26b81SDavid Drysdale 	/*   absolute path with nonsense dfd */
286c9b26b81SDavid Drysdale 	fail += check_execveat(99, fullname, 0);
287c9b26b81SDavid Drysdale 	/*   fd + no path */
288c9b26b81SDavid Drysdale 	fail += check_execveat(fd, "", AT_EMPTY_PATH);
289c9b26b81SDavid Drysdale 	/*   O_CLOEXEC fd + no path */
290c9b26b81SDavid Drysdale 	fail += check_execveat(fd_cloexec, "", AT_EMPTY_PATH);
291c9b26b81SDavid Drysdale 	/*   O_PATH fd */
292c9b26b81SDavid Drysdale 	fail += check_execveat(fd_path, "", AT_EMPTY_PATH);
293c9b26b81SDavid Drysdale 
294c9b26b81SDavid Drysdale 	/* Mess with executable file that's already open: */
295c9b26b81SDavid Drysdale 	/*   fd + no path to a file that's been renamed */
296c9b26b81SDavid Drysdale 	rename("execveat.ephemeral", "execveat.moved");
297c9b26b81SDavid Drysdale 	fail += check_execveat(fd_ephemeral, "", AT_EMPTY_PATH);
298c9b26b81SDavid Drysdale 	/*   fd + no path to a file that's been deleted */
299c9b26b81SDavid Drysdale 	unlink("execveat.moved"); /* remove the file now fd open */
300c9b26b81SDavid Drysdale 	fail += check_execveat(fd_ephemeral, "", AT_EMPTY_PATH);
301c9b26b81SDavid Drysdale 
302c9b26b81SDavid Drysdale 	/* Mess with executable file that's already open with O_PATH */
303c9b26b81SDavid Drysdale 	/*   fd + no path to a file that's been deleted */
304c9b26b81SDavid Drysdale 	unlink("execveat.path.ephemeral");
305c9b26b81SDavid Drysdale 	fail += check_execveat(fd_ephemeral_path, "", AT_EMPTY_PATH);
306c9b26b81SDavid Drysdale 
307c9b26b81SDavid Drysdale 	/* Invalid argument failures */
308c9b26b81SDavid Drysdale 	fail += check_execveat_fail(fd, "", 0, ENOENT);
309c9b26b81SDavid Drysdale 	fail += check_execveat_fail(fd, NULL, AT_EMPTY_PATH, EFAULT);
310c9b26b81SDavid Drysdale 
311c9b26b81SDavid Drysdale 	/* Symlink to executable file: */
312c9b26b81SDavid Drysdale 	/*   dfd + path */
313c9b26b81SDavid Drysdale 	fail += check_execveat(dot_dfd, "execveat.symlink", 0);
314c9b26b81SDavid Drysdale 	fail += check_execveat(dot_dfd_path, "execveat.symlink", 0);
315c9b26b81SDavid Drysdale 	/*   absolute path */
316c9b26b81SDavid Drysdale 	fail += check_execveat(AT_FDCWD, fullname_symlink, 0);
317c9b26b81SDavid Drysdale 	/*   fd + no path, even with AT_SYMLINK_NOFOLLOW (already followed) */
318c9b26b81SDavid Drysdale 	fail += check_execveat(fd_symlink, "", AT_EMPTY_PATH);
319c9b26b81SDavid Drysdale 	fail += check_execveat(fd_symlink, "",
320c9b26b81SDavid Drysdale 			       AT_EMPTY_PATH|AT_SYMLINK_NOFOLLOW);
321c9b26b81SDavid Drysdale 
322c9b26b81SDavid Drysdale 	/* Symlink fails when AT_SYMLINK_NOFOLLOW set: */
323c9b26b81SDavid Drysdale 	/*   dfd + path */
324c9b26b81SDavid Drysdale 	fail += check_execveat_fail(dot_dfd, "execveat.symlink",
325c9b26b81SDavid Drysdale 				    AT_SYMLINK_NOFOLLOW, ELOOP);
326c9b26b81SDavid Drysdale 	fail += check_execveat_fail(dot_dfd_path, "execveat.symlink",
327c9b26b81SDavid Drysdale 				    AT_SYMLINK_NOFOLLOW, ELOOP);
328c9b26b81SDavid Drysdale 	/*   absolute path */
329c9b26b81SDavid Drysdale 	fail += check_execveat_fail(AT_FDCWD, fullname_symlink,
330c9b26b81SDavid Drysdale 				    AT_SYMLINK_NOFOLLOW, ELOOP);
331c9b26b81SDavid Drysdale 
33261016db1SKees Cook 	/*  Non-regular file failure */
33361016db1SKees Cook 	fail += check_execveat_fail(dot_dfd, "pipe", 0, EACCES);
33461016db1SKees Cook 	unlink("pipe");
33561016db1SKees Cook 
336c9b26b81SDavid Drysdale 	/* Shell script wrapping executable file: */
337c9b26b81SDavid Drysdale 	/*   dfd + path */
338c9b26b81SDavid Drysdale 	fail += check_execveat(subdir_dfd, "../script", 0);
339c9b26b81SDavid Drysdale 	fail += check_execveat(dot_dfd, "script", 0);
340c9b26b81SDavid Drysdale 	fail += check_execveat(dot_dfd_path, "script", 0);
341c9b26b81SDavid Drysdale 	/*   absolute path */
342c9b26b81SDavid Drysdale 	fail += check_execveat(AT_FDCWD, fullname_script, 0);
343c9b26b81SDavid Drysdale 	/*   fd + no path */
344c9b26b81SDavid Drysdale 	fail += check_execveat(fd_script, "", AT_EMPTY_PATH);
345c9b26b81SDavid Drysdale 	fail += check_execveat(fd_script, "",
346c9b26b81SDavid Drysdale 			       AT_EMPTY_PATH|AT_SYMLINK_NOFOLLOW);
347c9b26b81SDavid Drysdale 	/*   O_CLOEXEC fd fails for a script (as script file inaccessible) */
348c9b26b81SDavid Drysdale 	fail += check_execveat_fail(fd_script_cloexec, "", AT_EMPTY_PATH,
349c9b26b81SDavid Drysdale 				    ENOENT);
350c9b26b81SDavid Drysdale 	fail += check_execveat_fail(dot_dfd_cloexec, "script", 0, ENOENT);
351c9b26b81SDavid Drysdale 
352c9b26b81SDavid Drysdale 	/* Mess with script file that's already open: */
353c9b26b81SDavid Drysdale 	/*   fd + no path to a file that's been renamed */
354c9b26b81SDavid Drysdale 	rename("script.ephemeral", "script.moved");
355c9b26b81SDavid Drysdale 	fail += check_execveat(fd_script_ephemeral, "", AT_EMPTY_PATH);
356c9b26b81SDavid Drysdale 	/*   fd + no path to a file that's been deleted */
357c9b26b81SDavid Drysdale 	unlink("script.moved"); /* remove the file while fd open */
358c9b26b81SDavid Drysdale 	fail += check_execveat(fd_script_ephemeral, "", AT_EMPTY_PATH);
359c9b26b81SDavid Drysdale 
360c9b26b81SDavid Drysdale 	/* Rename a subdirectory in the path: */
361c9b26b81SDavid Drysdale 	rename("subdir.ephemeral", "subdir.moved");
362c9b26b81SDavid Drysdale 	fail += check_execveat(subdir_dfd_ephemeral, "../script", 0);
363c9b26b81SDavid Drysdale 	fail += check_execveat(subdir_dfd_ephemeral, "script", 0);
364c9b26b81SDavid Drysdale 	/* Remove the subdir and its contents */
365c9b26b81SDavid Drysdale 	unlink("subdir.moved/script");
366c9b26b81SDavid Drysdale 	unlink("subdir.moved");
367c9b26b81SDavid Drysdale 	/* Shell loads via deleted subdir OK because name starts with .. */
368c9b26b81SDavid Drysdale 	fail += check_execveat(subdir_dfd_ephemeral, "../script", 0);
369c9b26b81SDavid Drysdale 	fail += check_execveat_fail(subdir_dfd_ephemeral, "script", 0, ENOENT);
370c9b26b81SDavid Drysdale 
371c9b26b81SDavid Drysdale 	/* Flag values other than AT_SYMLINK_NOFOLLOW => EINVAL */
372c9b26b81SDavid Drysdale 	fail += check_execveat_fail(dot_dfd, "execveat", 0xFFFF, EINVAL);
373c9b26b81SDavid Drysdale 	/* Invalid path => ENOENT */
374c9b26b81SDavid Drysdale 	fail += check_execveat_fail(dot_dfd, "no-such-file", 0, ENOENT);
375c9b26b81SDavid Drysdale 	fail += check_execveat_fail(dot_dfd_path, "no-such-file", 0, ENOENT);
376c9b26b81SDavid Drysdale 	fail += check_execveat_fail(AT_FDCWD, "no-such-file", 0, ENOENT);
377c9b26b81SDavid Drysdale 	/* Attempt to execute directory => EACCES */
378c9b26b81SDavid Drysdale 	fail += check_execveat_fail(dot_dfd, "", AT_EMPTY_PATH, EACCES);
379c9b26b81SDavid Drysdale 	/* Attempt to execute non-executable => EACCES */
380c9b26b81SDavid Drysdale 	fail += check_execveat_fail(dot_dfd, "Makefile", 0, EACCES);
381c9b26b81SDavid Drysdale 	fail += check_execveat_fail(fd_denatured, "", AT_EMPTY_PATH, EACCES);
382c9b26b81SDavid Drysdale 	fail += check_execveat_fail(fd_denatured_path, "", AT_EMPTY_PATH,
383c9b26b81SDavid Drysdale 				    EACCES);
384c9b26b81SDavid Drysdale 	/* Attempt to execute nonsense FD => EBADF */
385c9b26b81SDavid Drysdale 	fail += check_execveat_fail(99, "", AT_EMPTY_PATH, EBADF);
386c9b26b81SDavid Drysdale 	fail += check_execveat_fail(99, "execveat", 0, EBADF);
387c9b26b81SDavid Drysdale 	/* Attempt to execute relative to non-directory => ENOTDIR */
388c9b26b81SDavid Drysdale 	fail += check_execveat_fail(fd, "execveat", 0, ENOTDIR);
389c9b26b81SDavid Drysdale 
3902d80c92dSSteve Muckle 	fail += check_execveat_pathmax(root_dfd, "execveat", 0);
3912d80c92dSSteve Muckle 	fail += check_execveat_pathmax(root_dfd, "script", 1);
392c9b26b81SDavid Drysdale 	return fail;
393c9b26b81SDavid Drysdale }
394c9b26b81SDavid Drysdale 
prerequisites(void)395c9b26b81SDavid Drysdale static void prerequisites(void)
396c9b26b81SDavid Drysdale {
397c9b26b81SDavid Drysdale 	int fd;
39817107429SKees Cook 	const char *script = "#!/bin/bash\nexit $*\n";
399c9b26b81SDavid Drysdale 
400c9b26b81SDavid Drysdale 	/* Create ephemeral copies of files */
401c9b26b81SDavid Drysdale 	exe_cp("execveat", "execveat.ephemeral");
402c9b26b81SDavid Drysdale 	exe_cp("execveat", "execveat.path.ephemeral");
403c9b26b81SDavid Drysdale 	exe_cp("script", "script.ephemeral");
404c9b26b81SDavid Drysdale 	mkdir("subdir.ephemeral", 0755);
405c9b26b81SDavid Drysdale 
406c9b26b81SDavid Drysdale 	fd = open("subdir.ephemeral/script", O_RDWR|O_CREAT|O_TRUNC, 0755);
407c9b26b81SDavid Drysdale 	write(fd, script, strlen(script));
408c9b26b81SDavid Drysdale 	close(fd);
40961016db1SKees Cook 
41061016db1SKees Cook 	mkfifo("pipe", 0755);
411c9b26b81SDavid Drysdale }
412c9b26b81SDavid Drysdale 
main(int argc,char ** argv)413c9b26b81SDavid Drysdale int main(int argc, char **argv)
414c9b26b81SDavid Drysdale {
415c9b26b81SDavid Drysdale 	int ii;
416c9b26b81SDavid Drysdale 	int rc;
417c9b26b81SDavid Drysdale 	const char *verbose = getenv("VERBOSE");
418c9b26b81SDavid Drysdale 
419c9b26b81SDavid Drysdale 	if (argc >= 2) {
420c9b26b81SDavid Drysdale 		/* If we are invoked with an argument, don't run tests. */
421c9b26b81SDavid Drysdale 		const char *in_test = getenv("IN_TEST");
422c9b26b81SDavid Drysdale 
423c9b26b81SDavid Drysdale 		if (verbose) {
42447903c1dSMark Brown 			ksft_print_msg("invoked with:\n");
425c9b26b81SDavid Drysdale 			for (ii = 0; ii < argc; ii++)
42647903c1dSMark Brown 				ksft_print_msg("\t[%d]='%s\n'", ii, argv[ii]);
427c9b26b81SDavid Drysdale 		}
428c9b26b81SDavid Drysdale 
429c9b26b81SDavid Drysdale 		/* Check expected environment transferred. */
430c9b26b81SDavid Drysdale 		if (!in_test || strcmp(in_test, "yes") != 0) {
43147903c1dSMark Brown 			ksft_print_msg("no IN_TEST=yes in env\n");
432c9b26b81SDavid Drysdale 			return 1;
433c9b26b81SDavid Drysdale 		}
434c9b26b81SDavid Drysdale 
435c9b26b81SDavid Drysdale 		/* Use the final argument as an exit code. */
436c9b26b81SDavid Drysdale 		rc = atoi(argv[argc - 1]);
43747903c1dSMark Brown 		exit(rc);
438c9b26b81SDavid Drysdale 	} else {
43947903c1dSMark Brown 		ksft_print_header();
44047903c1dSMark Brown 		ksft_set_plan(TESTS_EXPECTED);
441c9b26b81SDavid Drysdale 		prerequisites();
442c9b26b81SDavid Drysdale 		if (verbose)
443c9b26b81SDavid Drysdale 			envp[1] = "VERBOSE=1";
444c9b26b81SDavid Drysdale 		rc = run_tests();
445c9b26b81SDavid Drysdale 		if (rc > 0)
446c9b26b81SDavid Drysdale 			printf("%d tests failed\n", rc);
44747903c1dSMark Brown 		ksft_finished();
448c9b26b81SDavid Drysdale 	}
44947903c1dSMark Brown 
450c9b26b81SDavid Drysdale 	return rc;
451c9b26b81SDavid Drysdale }
452