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) 
26*184a9358STycho Andersen #define TESTS_EXPECTED 54
2747903c1dSMark Brown #define TEST_NAME_LEN (PATH_MAX * 4)
2847903c1dSMark Brown 
29*184a9358STycho Andersen #define CHECK_COMM "CHECK_COMM"
30*184a9358STycho Andersen 
31c9b26b81SDavid Drysdale static char longpath[2 * PATH_MAX] = "";
32c9b26b81SDavid Drysdale static char *envp[] = { "IN_TEST=yes", NULL, NULL };
33c9b26b81SDavid Drysdale static char *argv[] = { "execveat", "99", NULL };
34c9b26b81SDavid Drysdale 
execveat_(int fd,const char * path,char ** argv,char ** envp,int flags)35c9b26b81SDavid Drysdale static int execveat_(int fd, const char *path, char **argv, char **envp,
36c9b26b81SDavid Drysdale 		     int flags)
37c9b26b81SDavid Drysdale {
38c9b26b81SDavid Drysdale #ifdef __NR_execveat
39c9b26b81SDavid Drysdale 	return syscall(__NR_execveat, fd, path, argv, envp, flags);
40c9b26b81SDavid Drysdale #else
419a0b5745SMichael Ellerman 	errno = ENOSYS;
42c9b26b81SDavid Drysdale 	return -1;
43c9b26b81SDavid Drysdale #endif
44c9b26b81SDavid Drysdale }
45c9b26b81SDavid Drysdale 
46c9b26b81SDavid Drysdale #define check_execveat_fail(fd, path, flags, errno)	\
47c9b26b81SDavid 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)48c9b26b81SDavid Drysdale static int _check_execveat_fail(int fd, const char *path, int flags,
49c9b26b81SDavid Drysdale 				int expected_errno, const char *errno_str)
50c9b26b81SDavid Drysdale {
5147903c1dSMark Brown 	char test_name[TEST_NAME_LEN];
52c9b26b81SDavid Drysdale 	int rc;
53c9b26b81SDavid Drysdale 
54c9b26b81SDavid Drysdale 	errno = 0;
5547903c1dSMark Brown 	snprintf(test_name, sizeof(test_name),
5647903c1dSMark Brown 		 "Check failure of execveat(%d, '%s', %d) with %s",
57c9b26b81SDavid Drysdale 		 fd, path?:"(null)", flags, errno_str);
58c9b26b81SDavid Drysdale 	rc = execveat_(fd, path, argv, envp, flags);
59c9b26b81SDavid Drysdale 
60c9b26b81SDavid Drysdale 	if (rc > 0) {
6147903c1dSMark Brown 		ksft_print_msg("unexpected success from execveat(2)\n");
6247903c1dSMark Brown 		ksft_test_result_fail("%s\n", test_name);
63c9b26b81SDavid Drysdale 		return 1;
64c9b26b81SDavid Drysdale 	}
65c9b26b81SDavid Drysdale 	if (errno != expected_errno) {
6647903c1dSMark Brown 		ksft_print_msg("expected errno %d (%s) not %d (%s)\n",
67c9b26b81SDavid Drysdale 			       expected_errno, strerror(expected_errno),
68c9b26b81SDavid Drysdale 			       errno, strerror(errno));
6947903c1dSMark Brown 		ksft_test_result_fail("%s\n", test_name);
70c9b26b81SDavid Drysdale 		return 1;
71c9b26b81SDavid Drysdale 	}
7247903c1dSMark Brown 	ksft_test_result_pass("%s\n", test_name);
73c9b26b81SDavid Drysdale 	return 0;
74c9b26b81SDavid Drysdale }
75c9b26b81SDavid Drysdale 
check_execveat_invoked_rc(int fd,const char * path,int flags,int expected_rc,int expected_rc2)76c9b26b81SDavid Drysdale static int check_execveat_invoked_rc(int fd, const char *path, int flags,
77cd805f36SDavid Drysdale 				     int expected_rc, int expected_rc2)
78c9b26b81SDavid Drysdale {
7947903c1dSMark Brown 	char test_name[TEST_NAME_LEN];
80c9b26b81SDavid Drysdale 	int status;
81c9b26b81SDavid Drysdale 	int rc;
82c9b26b81SDavid Drysdale 	pid_t child;
83c9b26b81SDavid Drysdale 	int pathlen = path ? strlen(path) : 0;
84c9b26b81SDavid Drysdale 
85c9b26b81SDavid Drysdale 	if (pathlen > 40)
8647903c1dSMark Brown 		snprintf(test_name, sizeof(test_name),
8747903c1dSMark Brown 			 "Check success of execveat(%d, '%.20s...%s', %d)... ",
88c9b26b81SDavid Drysdale 			 fd, path, (path + pathlen - 20), flags);
89c9b26b81SDavid Drysdale 	else
9047903c1dSMark Brown 		snprintf(test_name, sizeof(test_name),
9147903c1dSMark Brown 			 "Check success of execveat(%d, '%s', %d)... ",
92c9b26b81SDavid Drysdale 			 fd, path?:"(null)", flags);
9347903c1dSMark Brown 
94c9b26b81SDavid Drysdale 	child = fork();
95c9b26b81SDavid Drysdale 	if (child < 0) {
9647903c1dSMark Brown 		ksft_perror("fork() failed");
9747903c1dSMark Brown 		ksft_test_result_fail("%s\n", test_name);
98c9b26b81SDavid Drysdale 		return 1;
99c9b26b81SDavid Drysdale 	}
100c9b26b81SDavid Drysdale 	if (child == 0) {
101c9b26b81SDavid Drysdale 		/* Child: do execveat(). */
102c9b26b81SDavid Drysdale 		rc = execveat_(fd, path, argv, envp, flags);
1030ef58ccbSKees Cook 		ksft_print_msg("child execveat() failed, rc=%d errno=%d (%s)\n",
104c9b26b81SDavid Drysdale 			       rc, errno, strerror(errno));
1050ef58ccbSKees Cook 		exit(errno);
106c9b26b81SDavid Drysdale 	}
107c9b26b81SDavid Drysdale 	/* Parent: wait for & check child's exit status. */
108c9b26b81SDavid Drysdale 	rc = waitpid(child, &status, 0);
109c9b26b81SDavid Drysdale 	if (rc != child) {
11047903c1dSMark Brown 		ksft_print_msg("waitpid(%d,...) returned %d\n", child, rc);
11147903c1dSMark Brown 		ksft_test_result_fail("%s\n", test_name);
112c9b26b81SDavid Drysdale 		return 1;
113c9b26b81SDavid Drysdale 	}
114c9b26b81SDavid Drysdale 	if (!WIFEXITED(status)) {
11547903c1dSMark Brown 		ksft_print_msg("child %d did not exit cleanly, status=%08x\n",
116c9b26b81SDavid Drysdale 			       child, status);
11747903c1dSMark Brown 		ksft_test_result_fail("%s\n", test_name);
118c9b26b81SDavid Drysdale 		return 1;
119c9b26b81SDavid Drysdale 	}
120cd805f36SDavid Drysdale 	if ((WEXITSTATUS(status) != expected_rc) &&
121cd805f36SDavid Drysdale 	    (WEXITSTATUS(status) != expected_rc2)) {
12237ee7d19SChang Yu 		ksft_print_msg("child %d exited with %d neither %d nor %d\n",
12347903c1dSMark Brown 			       child, WEXITSTATUS(status), expected_rc,
12447903c1dSMark Brown 			       expected_rc2);
12547903c1dSMark Brown 		ksft_test_result_fail("%s\n", test_name);
126c9b26b81SDavid Drysdale 		return 1;
127c9b26b81SDavid Drysdale 	}
12847903c1dSMark Brown 	ksft_test_result_pass("%s\n", test_name);
129c9b26b81SDavid Drysdale 	return 0;
130c9b26b81SDavid Drysdale }
131c9b26b81SDavid Drysdale 
check_execveat(int fd,const char * path,int flags)132c9b26b81SDavid Drysdale static int check_execveat(int fd, const char *path, int flags)
133c9b26b81SDavid Drysdale {
134cd805f36SDavid Drysdale 	return check_execveat_invoked_rc(fd, path, flags, 99, 99);
135c9b26b81SDavid Drysdale }
136c9b26b81SDavid Drysdale 
concat(const char * left,const char * right)137c9b26b81SDavid Drysdale static char *concat(const char *left, const char *right)
138c9b26b81SDavid Drysdale {
139c9b26b81SDavid Drysdale 	char *result = malloc(strlen(left) + strlen(right) + 1);
140c9b26b81SDavid Drysdale 
141c9b26b81SDavid Drysdale 	strcpy(result, left);
142c9b26b81SDavid Drysdale 	strcat(result, right);
143c9b26b81SDavid Drysdale 	return result;
144c9b26b81SDavid Drysdale }
145c9b26b81SDavid Drysdale 
open_or_die(const char * filename,int flags)146c9b26b81SDavid Drysdale static int open_or_die(const char *filename, int flags)
147c9b26b81SDavid Drysdale {
148c9b26b81SDavid Drysdale 	int fd = open(filename, flags);
149c9b26b81SDavid Drysdale 
15047903c1dSMark Brown 	if (fd < 0)
15147903c1dSMark Brown 		ksft_exit_fail_msg("Failed to open '%s'; "
152c9b26b81SDavid Drysdale 			"check prerequisites are available\n", filename);
153c9b26b81SDavid Drysdale 	return fd;
154c9b26b81SDavid Drysdale }
155c9b26b81SDavid Drysdale 
exe_cp(const char * src,const char * dest)156c9b26b81SDavid Drysdale static void exe_cp(const char *src, const char *dest)
157c9b26b81SDavid Drysdale {
158c9b26b81SDavid Drysdale 	int in_fd = open_or_die(src, O_RDONLY);
159c9b26b81SDavid Drysdale 	int out_fd = open(dest, O_RDWR|O_CREAT|O_TRUNC, 0755);
160c9b26b81SDavid Drysdale 	struct stat info;
161c9b26b81SDavid Drysdale 
162c9b26b81SDavid Drysdale 	fstat(in_fd, &info);
163c9b26b81SDavid Drysdale 	sendfile(out_fd, in_fd, NULL, info.st_size);
164c9b26b81SDavid Drysdale 	close(in_fd);
165c9b26b81SDavid Drysdale 	close(out_fd);
166c9b26b81SDavid Drysdale }
167c9b26b81SDavid Drysdale 
168c9b26b81SDavid Drysdale #define XX_DIR_LEN 200
check_execveat_pathmax(int root_dfd,const char * src,int is_script)1692d80c92dSSteve Muckle static int check_execveat_pathmax(int root_dfd, const char *src, int is_script)
170c9b26b81SDavid Drysdale {
171c9b26b81SDavid Drysdale 	int fail = 0;
172c9b26b81SDavid Drysdale 	int ii, count, len;
173c9b26b81SDavid Drysdale 	char longname[XX_DIR_LEN + 1];
174c9b26b81SDavid Drysdale 	int fd;
175c9b26b81SDavid Drysdale 
176c9b26b81SDavid Drysdale 	if (*longpath == '\0') {
177c9b26b81SDavid Drysdale 		/* Create a filename close to PATH_MAX in length */
1782d80c92dSSteve Muckle 		char *cwd = getcwd(NULL, 0);
1792d80c92dSSteve Muckle 
1802d80c92dSSteve Muckle 		if (!cwd) {
18147903c1dSMark Brown 			ksft_perror("Failed to getcwd()");
1822d80c92dSSteve Muckle 			return 2;
1832d80c92dSSteve Muckle 		}
1842d80c92dSSteve Muckle 		strcpy(longpath, cwd);
1852d80c92dSSteve Muckle 		strcat(longpath, "/");
186c9b26b81SDavid Drysdale 		memset(longname, 'x', XX_DIR_LEN - 1);
187c9b26b81SDavid Drysdale 		longname[XX_DIR_LEN - 1] = '/';
188c9b26b81SDavid Drysdale 		longname[XX_DIR_LEN] = '\0';
1892d80c92dSSteve Muckle 		count = (PATH_MAX - 3 - strlen(cwd)) / XX_DIR_LEN;
190c9b26b81SDavid Drysdale 		for (ii = 0; ii < count; ii++) {
191c9b26b81SDavid Drysdale 			strcat(longpath, longname);
192c9b26b81SDavid Drysdale 			mkdir(longpath, 0755);
193c9b26b81SDavid Drysdale 		}
1942d80c92dSSteve Muckle 		len = (PATH_MAX - 3 - strlen(cwd)) - (count * XX_DIR_LEN);
195c9b26b81SDavid Drysdale 		if (len <= 0)
196c9b26b81SDavid Drysdale 			len = 1;
197c9b26b81SDavid Drysdale 		memset(longname, 'y', len);
198c9b26b81SDavid Drysdale 		longname[len] = '\0';
199c9b26b81SDavid Drysdale 		strcat(longpath, longname);
2002d80c92dSSteve Muckle 		free(cwd);
201c9b26b81SDavid Drysdale 	}
202c9b26b81SDavid Drysdale 	exe_cp(src, longpath);
203c9b26b81SDavid Drysdale 
204c9b26b81SDavid Drysdale 	/*
205c9b26b81SDavid Drysdale 	 * Execute as a pre-opened file descriptor, which works whether this is
206c9b26b81SDavid Drysdale 	 * a script or not (because the interpreter sees a filename like
207c9b26b81SDavid Drysdale 	 * "/dev/fd/20").
208c9b26b81SDavid Drysdale 	 */
209c9b26b81SDavid Drysdale 	fd = open(longpath, O_RDONLY);
210c9b26b81SDavid Drysdale 	if (fd > 0) {
21147903c1dSMark Brown 		ksft_print_msg("Invoke copy of '%s' via filename of length %zu:\n",
212c9b26b81SDavid Drysdale 			       src, strlen(longpath));
213c9b26b81SDavid Drysdale 		fail += check_execveat(fd, "", AT_EMPTY_PATH);
214c9b26b81SDavid Drysdale 	} else {
21547903c1dSMark Brown 		ksft_print_msg("Failed to open length %zu filename, errno=%d (%s)\n",
216c9b26b81SDavid Drysdale 			       strlen(longpath), errno, strerror(errno));
217c9b26b81SDavid Drysdale 		fail++;
218c9b26b81SDavid Drysdale 	}
219c9b26b81SDavid Drysdale 
220c9b26b81SDavid Drysdale 	/*
2212d80c92dSSteve Muckle 	 * Execute as a long pathname relative to "/".  If this is a script,
222c9b26b81SDavid Drysdale 	 * the interpreter will launch but fail to open the script because its
223c9b26b81SDavid Drysdale 	 * name ("/dev/fd/5/xxx....") is bigger than PATH_MAX.
224cd805f36SDavid Drysdale 	 *
225cd805f36SDavid Drysdale 	 * The failure code is usually 127 (POSIX: "If a command is not found,
226cd805f36SDavid Drysdale 	 * the exit status shall be 127."), but some systems give 126 (POSIX:
227cd805f36SDavid Drysdale 	 * "If the command name is found, but it is not an executable utility,
228cd805f36SDavid Drysdale 	 * the exit status shall be 126."), so allow either.
229c9b26b81SDavid Drysdale 	 */
2300ef58ccbSKees Cook 	if (is_script) {
2310ef58ccbSKees Cook 		ksft_print_msg("Invoke script via root_dfd and relative filename\n");
2322d80c92dSSteve Muckle 		fail += check_execveat_invoked_rc(root_dfd, longpath + 1, 0,
233cd805f36SDavid Drysdale 						  127, 126);
2340ef58ccbSKees Cook 	} else {
2350ef58ccbSKees Cook 		ksft_print_msg("Invoke exec via root_dfd and relative filename\n");
2362d80c92dSSteve Muckle 		fail += check_execveat(root_dfd, longpath + 1, 0);
2370ef58ccbSKees Cook 	}
238c9b26b81SDavid Drysdale 
239c9b26b81SDavid Drysdale 	return fail;
240c9b26b81SDavid Drysdale }
241c9b26b81SDavid Drysdale 
check_execveat_comm(int fd,char * argv0,char * expected)242*184a9358STycho Andersen static int check_execveat_comm(int fd, char *argv0, char *expected)
243*184a9358STycho Andersen {
244*184a9358STycho Andersen 	char buf[128], *old_env, *old_argv0;
245*184a9358STycho Andersen 	int ret;
246*184a9358STycho Andersen 
247*184a9358STycho Andersen 	snprintf(buf, sizeof(buf), CHECK_COMM "=%s", expected);
248*184a9358STycho Andersen 
249*184a9358STycho Andersen 	old_env = envp[1];
250*184a9358STycho Andersen 	envp[1] = buf;
251*184a9358STycho Andersen 
252*184a9358STycho Andersen 	old_argv0 = argv[0];
253*184a9358STycho Andersen 	argv[0] = argv0;
254*184a9358STycho Andersen 
255*184a9358STycho Andersen 	ksft_print_msg("Check execveat(AT_EMPTY_PATH)'s comm is %s\n",
256*184a9358STycho Andersen 		       expected);
257*184a9358STycho Andersen 	ret = check_execveat_invoked_rc(fd, "", AT_EMPTY_PATH, 0, 0);
258*184a9358STycho Andersen 
259*184a9358STycho Andersen 	envp[1] = old_env;
260*184a9358STycho Andersen 	argv[0] = old_argv0;
261*184a9358STycho Andersen 
262*184a9358STycho Andersen 	return ret;
263*184a9358STycho Andersen }
264*184a9358STycho Andersen 
run_tests(void)265c9b26b81SDavid Drysdale static int run_tests(void)
266c9b26b81SDavid Drysdale {
267c9b26b81SDavid Drysdale 	int fail = 0;
268c9b26b81SDavid Drysdale 	char *fullname = realpath("execveat", NULL);
269c9b26b81SDavid Drysdale 	char *fullname_script = realpath("script", NULL);
270c9b26b81SDavid Drysdale 	char *fullname_symlink = concat(fullname, ".symlink");
271c9b26b81SDavid Drysdale 	int subdir_dfd = open_or_die("subdir", O_DIRECTORY|O_RDONLY);
272c9b26b81SDavid Drysdale 	int subdir_dfd_ephemeral = open_or_die("subdir.ephemeral",
273c9b26b81SDavid Drysdale 					       O_DIRECTORY|O_RDONLY);
274c9b26b81SDavid Drysdale 	int dot_dfd = open_or_die(".", O_DIRECTORY|O_RDONLY);
2752d80c92dSSteve Muckle 	int root_dfd = open_or_die("/", O_DIRECTORY|O_RDONLY);
276c9b26b81SDavid Drysdale 	int dot_dfd_path = open_or_die(".", O_DIRECTORY|O_RDONLY|O_PATH);
277c9b26b81SDavid Drysdale 	int dot_dfd_cloexec = open_or_die(".", O_DIRECTORY|O_RDONLY|O_CLOEXEC);
278c9b26b81SDavid Drysdale 	int fd = open_or_die("execveat", O_RDONLY);
279c9b26b81SDavid Drysdale 	int fd_path = open_or_die("execveat", O_RDONLY|O_PATH);
280c9b26b81SDavid Drysdale 	int fd_symlink = open_or_die("execveat.symlink", O_RDONLY);
281c9b26b81SDavid Drysdale 	int fd_denatured = open_or_die("execveat.denatured", O_RDONLY);
282c9b26b81SDavid Drysdale 	int fd_denatured_path = open_or_die("execveat.denatured",
283c9b26b81SDavid Drysdale 					    O_RDONLY|O_PATH);
284c9b26b81SDavid Drysdale 	int fd_script = open_or_die("script", O_RDONLY);
285c9b26b81SDavid Drysdale 	int fd_ephemeral = open_or_die("execveat.ephemeral", O_RDONLY);
286c9b26b81SDavid Drysdale 	int fd_ephemeral_path = open_or_die("execveat.path.ephemeral",
287c9b26b81SDavid Drysdale 					    O_RDONLY|O_PATH);
288c9b26b81SDavid Drysdale 	int fd_script_ephemeral = open_or_die("script.ephemeral", O_RDONLY);
289c9b26b81SDavid Drysdale 	int fd_cloexec = open_or_die("execveat", O_RDONLY|O_CLOEXEC);
290c9b26b81SDavid Drysdale 	int fd_script_cloexec = open_or_die("script", O_RDONLY|O_CLOEXEC);
291c9b26b81SDavid Drysdale 
2929a0b5745SMichael Ellerman 	/* Check if we have execveat at all, and bail early if not */
2939a0b5745SMichael Ellerman 	errno = 0;
2949a0b5745SMichael Ellerman 	execveat_(-1, NULL, NULL, NULL, 0);
2959a0b5745SMichael Ellerman 	if (errno == ENOSYS) {
296964224d7SShuah Khan (Samsung OSG) 		ksft_exit_skip(
297964224d7SShuah Khan (Samsung OSG) 			"ENOSYS calling execveat - no kernel support?\n");
2989a0b5745SMichael Ellerman 	}
2999a0b5745SMichael Ellerman 
300c9b26b81SDavid Drysdale 	/* Change file position to confirm it doesn't affect anything */
301c9b26b81SDavid Drysdale 	lseek(fd, 10, SEEK_SET);
302c9b26b81SDavid Drysdale 
303c9b26b81SDavid Drysdale 	/* Normal executable file: */
304c9b26b81SDavid Drysdale 	/*   dfd + path */
305c9b26b81SDavid Drysdale 	fail += check_execveat(subdir_dfd, "../execveat", 0);
306c9b26b81SDavid Drysdale 	fail += check_execveat(dot_dfd, "execveat", 0);
307c9b26b81SDavid Drysdale 	fail += check_execveat(dot_dfd_path, "execveat", 0);
308c9b26b81SDavid Drysdale 	/*   absolute path */
309c9b26b81SDavid Drysdale 	fail += check_execveat(AT_FDCWD, fullname, 0);
310c9b26b81SDavid Drysdale 	/*   absolute path with nonsense dfd */
311c9b26b81SDavid Drysdale 	fail += check_execveat(99, fullname, 0);
312c9b26b81SDavid Drysdale 	/*   fd + no path */
313c9b26b81SDavid Drysdale 	fail += check_execveat(fd, "", AT_EMPTY_PATH);
314c9b26b81SDavid Drysdale 	/*   O_CLOEXEC fd + no path */
315c9b26b81SDavid Drysdale 	fail += check_execveat(fd_cloexec, "", AT_EMPTY_PATH);
316c9b26b81SDavid Drysdale 	/*   O_PATH fd */
317c9b26b81SDavid Drysdale 	fail += check_execveat(fd_path, "", AT_EMPTY_PATH);
318c9b26b81SDavid Drysdale 
319c9b26b81SDavid Drysdale 	/* Mess with executable file that's already open: */
320c9b26b81SDavid Drysdale 	/*   fd + no path to a file that's been renamed */
321c9b26b81SDavid Drysdale 	rename("execveat.ephemeral", "execveat.moved");
322c9b26b81SDavid Drysdale 	fail += check_execveat(fd_ephemeral, "", AT_EMPTY_PATH);
323c9b26b81SDavid Drysdale 	/*   fd + no path to a file that's been deleted */
324c9b26b81SDavid Drysdale 	unlink("execveat.moved"); /* remove the file now fd open */
325c9b26b81SDavid Drysdale 	fail += check_execveat(fd_ephemeral, "", AT_EMPTY_PATH);
326c9b26b81SDavid Drysdale 
327c9b26b81SDavid Drysdale 	/* Mess with executable file that's already open with O_PATH */
328c9b26b81SDavid Drysdale 	/*   fd + no path to a file that's been deleted */
329c9b26b81SDavid Drysdale 	unlink("execveat.path.ephemeral");
330c9b26b81SDavid Drysdale 	fail += check_execveat(fd_ephemeral_path, "", AT_EMPTY_PATH);
331c9b26b81SDavid Drysdale 
332c9b26b81SDavid Drysdale 	/* Invalid argument failures */
333c9b26b81SDavid Drysdale 	fail += check_execveat_fail(fd, "", 0, ENOENT);
334c9b26b81SDavid Drysdale 	fail += check_execveat_fail(fd, NULL, AT_EMPTY_PATH, EFAULT);
335c9b26b81SDavid Drysdale 
336c9b26b81SDavid Drysdale 	/* Symlink to executable file: */
337c9b26b81SDavid Drysdale 	/*   dfd + path */
338c9b26b81SDavid Drysdale 	fail += check_execveat(dot_dfd, "execveat.symlink", 0);
339c9b26b81SDavid Drysdale 	fail += check_execveat(dot_dfd_path, "execveat.symlink", 0);
340c9b26b81SDavid Drysdale 	/*   absolute path */
341c9b26b81SDavid Drysdale 	fail += check_execveat(AT_FDCWD, fullname_symlink, 0);
342c9b26b81SDavid Drysdale 	/*   fd + no path, even with AT_SYMLINK_NOFOLLOW (already followed) */
343c9b26b81SDavid Drysdale 	fail += check_execveat(fd_symlink, "", AT_EMPTY_PATH);
344c9b26b81SDavid Drysdale 	fail += check_execveat(fd_symlink, "",
345c9b26b81SDavid Drysdale 			       AT_EMPTY_PATH|AT_SYMLINK_NOFOLLOW);
346c9b26b81SDavid Drysdale 
347c9b26b81SDavid Drysdale 	/* Symlink fails when AT_SYMLINK_NOFOLLOW set: */
348c9b26b81SDavid Drysdale 	/*   dfd + path */
349c9b26b81SDavid Drysdale 	fail += check_execveat_fail(dot_dfd, "execveat.symlink",
350c9b26b81SDavid Drysdale 				    AT_SYMLINK_NOFOLLOW, ELOOP);
351c9b26b81SDavid Drysdale 	fail += check_execveat_fail(dot_dfd_path, "execveat.symlink",
352c9b26b81SDavid Drysdale 				    AT_SYMLINK_NOFOLLOW, ELOOP);
353c9b26b81SDavid Drysdale 	/*   absolute path */
354c9b26b81SDavid Drysdale 	fail += check_execveat_fail(AT_FDCWD, fullname_symlink,
355c9b26b81SDavid Drysdale 				    AT_SYMLINK_NOFOLLOW, ELOOP);
356c9b26b81SDavid Drysdale 
35761016db1SKees Cook 	/*  Non-regular file failure */
35861016db1SKees Cook 	fail += check_execveat_fail(dot_dfd, "pipe", 0, EACCES);
35961016db1SKees Cook 	unlink("pipe");
36061016db1SKees Cook 
361c9b26b81SDavid Drysdale 	/* Shell script wrapping executable file: */
362c9b26b81SDavid Drysdale 	/*   dfd + path */
363c9b26b81SDavid Drysdale 	fail += check_execveat(subdir_dfd, "../script", 0);
364c9b26b81SDavid Drysdale 	fail += check_execveat(dot_dfd, "script", 0);
365c9b26b81SDavid Drysdale 	fail += check_execveat(dot_dfd_path, "script", 0);
366c9b26b81SDavid Drysdale 	/*   absolute path */
367c9b26b81SDavid Drysdale 	fail += check_execveat(AT_FDCWD, fullname_script, 0);
368c9b26b81SDavid Drysdale 	/*   fd + no path */
369c9b26b81SDavid Drysdale 	fail += check_execveat(fd_script, "", AT_EMPTY_PATH);
370c9b26b81SDavid Drysdale 	fail += check_execveat(fd_script, "",
371c9b26b81SDavid Drysdale 			       AT_EMPTY_PATH|AT_SYMLINK_NOFOLLOW);
372c9b26b81SDavid Drysdale 	/*   O_CLOEXEC fd fails for a script (as script file inaccessible) */
373c9b26b81SDavid Drysdale 	fail += check_execveat_fail(fd_script_cloexec, "", AT_EMPTY_PATH,
374c9b26b81SDavid Drysdale 				    ENOENT);
375c9b26b81SDavid Drysdale 	fail += check_execveat_fail(dot_dfd_cloexec, "script", 0, ENOENT);
376c9b26b81SDavid Drysdale 
377c9b26b81SDavid Drysdale 	/* Mess with script file that's already open: */
378c9b26b81SDavid Drysdale 	/*   fd + no path to a file that's been renamed */
379c9b26b81SDavid Drysdale 	rename("script.ephemeral", "script.moved");
380c9b26b81SDavid Drysdale 	fail += check_execveat(fd_script_ephemeral, "", AT_EMPTY_PATH);
381c9b26b81SDavid Drysdale 	/*   fd + no path to a file that's been deleted */
382c9b26b81SDavid Drysdale 	unlink("script.moved"); /* remove the file while fd open */
383c9b26b81SDavid Drysdale 	fail += check_execveat(fd_script_ephemeral, "", AT_EMPTY_PATH);
384c9b26b81SDavid Drysdale 
385c9b26b81SDavid Drysdale 	/* Rename a subdirectory in the path: */
386c9b26b81SDavid Drysdale 	rename("subdir.ephemeral", "subdir.moved");
387c9b26b81SDavid Drysdale 	fail += check_execveat(subdir_dfd_ephemeral, "../script", 0);
388c9b26b81SDavid Drysdale 	fail += check_execveat(subdir_dfd_ephemeral, "script", 0);
389c9b26b81SDavid Drysdale 	/* Remove the subdir and its contents */
390c9b26b81SDavid Drysdale 	unlink("subdir.moved/script");
391c9b26b81SDavid Drysdale 	unlink("subdir.moved");
392c9b26b81SDavid Drysdale 	/* Shell loads via deleted subdir OK because name starts with .. */
393c9b26b81SDavid Drysdale 	fail += check_execveat(subdir_dfd_ephemeral, "../script", 0);
394c9b26b81SDavid Drysdale 	fail += check_execveat_fail(subdir_dfd_ephemeral, "script", 0, ENOENT);
395c9b26b81SDavid Drysdale 
396c9b26b81SDavid Drysdale 	/* Flag values other than AT_SYMLINK_NOFOLLOW => EINVAL */
397c9b26b81SDavid Drysdale 	fail += check_execveat_fail(dot_dfd, "execveat", 0xFFFF, EINVAL);
398c9b26b81SDavid Drysdale 	/* Invalid path => ENOENT */
399c9b26b81SDavid Drysdale 	fail += check_execveat_fail(dot_dfd, "no-such-file", 0, ENOENT);
400c9b26b81SDavid Drysdale 	fail += check_execveat_fail(dot_dfd_path, "no-such-file", 0, ENOENT);
401c9b26b81SDavid Drysdale 	fail += check_execveat_fail(AT_FDCWD, "no-such-file", 0, ENOENT);
402c9b26b81SDavid Drysdale 	/* Attempt to execute directory => EACCES */
403c9b26b81SDavid Drysdale 	fail += check_execveat_fail(dot_dfd, "", AT_EMPTY_PATH, EACCES);
404c9b26b81SDavid Drysdale 	/* Attempt to execute non-executable => EACCES */
405c9b26b81SDavid Drysdale 	fail += check_execveat_fail(dot_dfd, "Makefile", 0, EACCES);
406c9b26b81SDavid Drysdale 	fail += check_execveat_fail(fd_denatured, "", AT_EMPTY_PATH, EACCES);
407c9b26b81SDavid Drysdale 	fail += check_execveat_fail(fd_denatured_path, "", AT_EMPTY_PATH,
408c9b26b81SDavid Drysdale 				    EACCES);
409c9b26b81SDavid Drysdale 	/* Attempt to execute nonsense FD => EBADF */
410c9b26b81SDavid Drysdale 	fail += check_execveat_fail(99, "", AT_EMPTY_PATH, EBADF);
411c9b26b81SDavid Drysdale 	fail += check_execveat_fail(99, "execveat", 0, EBADF);
412c9b26b81SDavid Drysdale 	/* Attempt to execute relative to non-directory => ENOTDIR */
413c9b26b81SDavid Drysdale 	fail += check_execveat_fail(fd, "execveat", 0, ENOTDIR);
414c9b26b81SDavid Drysdale 
4152d80c92dSSteve Muckle 	fail += check_execveat_pathmax(root_dfd, "execveat", 0);
4162d80c92dSSteve Muckle 	fail += check_execveat_pathmax(root_dfd, "script", 1);
417*184a9358STycho Andersen 
418*184a9358STycho Andersen 	/* /proc/pid/comm gives filename by default */
419*184a9358STycho Andersen 	fail += check_execveat_comm(fd, "sentinel", "execveat");
420*184a9358STycho Andersen 	/* /proc/pid/comm gives argv[0] when invoked via link */
421*184a9358STycho Andersen 	fail += check_execveat_comm(fd_symlink, "sentinel", "execveat");
422*184a9358STycho Andersen 	/* /proc/pid/comm gives filename if NULL is passed */
423*184a9358STycho Andersen 	fail += check_execveat_comm(fd, NULL, "execveat");
424*184a9358STycho Andersen 
425c9b26b81SDavid Drysdale 	return fail;
426c9b26b81SDavid Drysdale }
427c9b26b81SDavid Drysdale 
prerequisites(void)428c9b26b81SDavid Drysdale static void prerequisites(void)
429c9b26b81SDavid Drysdale {
430c9b26b81SDavid Drysdale 	int fd;
43117107429SKees Cook 	const char *script = "#!/bin/bash\nexit $*\n";
432c9b26b81SDavid Drysdale 
433c9b26b81SDavid Drysdale 	/* Create ephemeral copies of files */
434c9b26b81SDavid Drysdale 	exe_cp("execveat", "execveat.ephemeral");
435c9b26b81SDavid Drysdale 	exe_cp("execveat", "execveat.path.ephemeral");
436c9b26b81SDavid Drysdale 	exe_cp("script", "script.ephemeral");
437c9b26b81SDavid Drysdale 	mkdir("subdir.ephemeral", 0755);
438c9b26b81SDavid Drysdale 
439c9b26b81SDavid Drysdale 	fd = open("subdir.ephemeral/script", O_RDWR|O_CREAT|O_TRUNC, 0755);
440c9b26b81SDavid Drysdale 	write(fd, script, strlen(script));
441c9b26b81SDavid Drysdale 	close(fd);
44261016db1SKees Cook 
44361016db1SKees Cook 	mkfifo("pipe", 0755);
444c9b26b81SDavid Drysdale }
445c9b26b81SDavid Drysdale 
main(int argc,char ** argv)446c9b26b81SDavid Drysdale int main(int argc, char **argv)
447c9b26b81SDavid Drysdale {
448c9b26b81SDavid Drysdale 	int ii;
449c9b26b81SDavid Drysdale 	int rc;
450c9b26b81SDavid Drysdale 	const char *verbose = getenv("VERBOSE");
451*184a9358STycho Andersen 	const char *check_comm = getenv(CHECK_COMM);
452c9b26b81SDavid Drysdale 
453*184a9358STycho Andersen 	if (argc >= 2 || check_comm) {
454*184a9358STycho Andersen 		/*
455*184a9358STycho Andersen 		 * If we are invoked with an argument, or no arguments but a
456*184a9358STycho Andersen 		 * command to check, don't run tests.
457*184a9358STycho Andersen 		 */
458c9b26b81SDavid Drysdale 		const char *in_test = getenv("IN_TEST");
459c9b26b81SDavid Drysdale 
460c9b26b81SDavid Drysdale 		if (verbose) {
46147903c1dSMark Brown 			ksft_print_msg("invoked with:\n");
462c9b26b81SDavid Drysdale 			for (ii = 0; ii < argc; ii++)
46347903c1dSMark Brown 				ksft_print_msg("\t[%d]='%s\n'", ii, argv[ii]);
464c9b26b81SDavid Drysdale 		}
465c9b26b81SDavid Drysdale 
466*184a9358STycho Andersen 		/* If the tests wanted us to check the command, do so. */
467*184a9358STycho Andersen 		if (check_comm) {
468*184a9358STycho Andersen 			/* TASK_COMM_LEN == 16 */
469*184a9358STycho Andersen 			char buf[32];
470*184a9358STycho Andersen 			int fd, ret;
471*184a9358STycho Andersen 
472*184a9358STycho Andersen 			fd = open("/proc/self/comm", O_RDONLY);
473*184a9358STycho Andersen 			if (fd < 0) {
474*184a9358STycho Andersen 				ksft_perror("open() comm failed");
475*184a9358STycho Andersen 				exit(1);
476*184a9358STycho Andersen 			}
477*184a9358STycho Andersen 
478*184a9358STycho Andersen 			ret = read(fd, buf, sizeof(buf));
479*184a9358STycho Andersen 			if (ret < 0) {
480*184a9358STycho Andersen 				ksft_perror("read() comm failed");
481*184a9358STycho Andersen 				close(fd);
482*184a9358STycho Andersen 				exit(1);
483*184a9358STycho Andersen 			}
484*184a9358STycho Andersen 			close(fd);
485*184a9358STycho Andersen 
486*184a9358STycho Andersen 			// trim off the \n
487*184a9358STycho Andersen 			buf[ret-1] = 0;
488*184a9358STycho Andersen 
489*184a9358STycho Andersen 			if (strcmp(buf, check_comm)) {
490*184a9358STycho Andersen 				ksft_print_msg("bad comm, got: %s expected: %s\n",
491*184a9358STycho Andersen 					       buf, check_comm);
492*184a9358STycho Andersen 				exit(1);
493*184a9358STycho Andersen 			}
494*184a9358STycho Andersen 
495*184a9358STycho Andersen 			exit(0);
496*184a9358STycho Andersen 		}
497*184a9358STycho Andersen 
498c9b26b81SDavid Drysdale 		/* Check expected environment transferred. */
499c9b26b81SDavid Drysdale 		if (!in_test || strcmp(in_test, "yes") != 0) {
50047903c1dSMark Brown 			ksft_print_msg("no IN_TEST=yes in env\n");
501c9b26b81SDavid Drysdale 			return 1;
502c9b26b81SDavid Drysdale 		}
503c9b26b81SDavid Drysdale 
504c9b26b81SDavid Drysdale 		/* Use the final argument as an exit code. */
505c9b26b81SDavid Drysdale 		rc = atoi(argv[argc - 1]);
50647903c1dSMark Brown 		exit(rc);
507c9b26b81SDavid Drysdale 	} else {
50847903c1dSMark Brown 		ksft_print_header();
50947903c1dSMark Brown 		ksft_set_plan(TESTS_EXPECTED);
510c9b26b81SDavid Drysdale 		prerequisites();
511c9b26b81SDavid Drysdale 		if (verbose)
512c9b26b81SDavid Drysdale 			envp[1] = "VERBOSE=1";
513c9b26b81SDavid Drysdale 		rc = run_tests();
514c9b26b81SDavid Drysdale 		if (rc > 0)
515c9b26b81SDavid Drysdale 			printf("%d tests failed\n", rc);
51647903c1dSMark Brown 		ksft_finished();
517c9b26b81SDavid Drysdale 	}
51847903c1dSMark Brown 
519c9b26b81SDavid Drysdale 	return rc;
520c9b26b81SDavid Drysdale }
521