execveat.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /*
  2. * Copyright (c) 2014 Google, Inc.
  3. *
  4. * Licensed under the terms of the GNU GPL License version 2
  5. *
  6. * Selftests for execveat(2).
  7. */
  8. #define _GNU_SOURCE /* to get O_PATH, AT_EMPTY_PATH */
  9. #include <sys/sendfile.h>
  10. #include <sys/stat.h>
  11. #include <sys/syscall.h>
  12. #include <sys/types.h>
  13. #include <sys/wait.h>
  14. #include <errno.h>
  15. #include <fcntl.h>
  16. #include <limits.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <unistd.h>
  21. #include "../kselftest.h"
  22. static char longpath[2 * PATH_MAX] = "";
  23. static char *envp[] = { "IN_TEST=yes", NULL, NULL };
  24. static char *argv[] = { "execveat", "99", NULL };
  25. static int execveat_(int fd, const char *path, char **argv, char **envp,
  26. int flags)
  27. {
  28. #ifdef __NR_execveat
  29. return syscall(__NR_execveat, fd, path, argv, envp, flags);
  30. #else
  31. errno = ENOSYS;
  32. return -1;
  33. #endif
  34. }
  35. #define check_execveat_fail(fd, path, flags, errno) \
  36. _check_execveat_fail(fd, path, flags, errno, #errno)
  37. static int _check_execveat_fail(int fd, const char *path, int flags,
  38. int expected_errno, const char *errno_str)
  39. {
  40. int rc;
  41. errno = 0;
  42. printf("Check failure of execveat(%d, '%s', %d) with %s... ",
  43. fd, path?:"(null)", flags, errno_str);
  44. rc = execveat_(fd, path, argv, envp, flags);
  45. if (rc > 0) {
  46. printf("[FAIL] (unexpected success from execveat(2))\n");
  47. return 1;
  48. }
  49. if (errno != expected_errno) {
  50. printf("[FAIL] (expected errno %d (%s) not %d (%s)\n",
  51. expected_errno, strerror(expected_errno),
  52. errno, strerror(errno));
  53. return 1;
  54. }
  55. printf("[OK]\n");
  56. return 0;
  57. }
  58. static int check_execveat_invoked_rc(int fd, const char *path, int flags,
  59. int expected_rc, int expected_rc2)
  60. {
  61. int status;
  62. int rc;
  63. pid_t child;
  64. int pathlen = path ? strlen(path) : 0;
  65. if (pathlen > 40)
  66. printf("Check success of execveat(%d, '%.20s...%s', %d)... ",
  67. fd, path, (path + pathlen - 20), flags);
  68. else
  69. printf("Check success of execveat(%d, '%s', %d)... ",
  70. fd, path?:"(null)", flags);
  71. child = fork();
  72. if (child < 0) {
  73. printf("[FAIL] (fork() failed)\n");
  74. return 1;
  75. }
  76. if (child == 0) {
  77. /* Child: do execveat(). */
  78. rc = execveat_(fd, path, argv, envp, flags);
  79. printf("[FAIL]: execveat() failed, rc=%d errno=%d (%s)\n",
  80. rc, errno, strerror(errno));
  81. exit(1); /* should not reach here */
  82. }
  83. /* Parent: wait for & check child's exit status. */
  84. rc = waitpid(child, &status, 0);
  85. if (rc != child) {
  86. printf("[FAIL] (waitpid(%d,...) returned %d)\n", child, rc);
  87. return 1;
  88. }
  89. if (!WIFEXITED(status)) {
  90. printf("[FAIL] (child %d did not exit cleanly, status=%08x)\n",
  91. child, status);
  92. return 1;
  93. }
  94. if ((WEXITSTATUS(status) != expected_rc) &&
  95. (WEXITSTATUS(status) != expected_rc2)) {
  96. printf("[FAIL] (child %d exited with %d not %d nor %d)\n",
  97. child, WEXITSTATUS(status), expected_rc, expected_rc2);
  98. return 1;
  99. }
  100. printf("[OK]\n");
  101. return 0;
  102. }
  103. static int check_execveat(int fd, const char *path, int flags)
  104. {
  105. return check_execveat_invoked_rc(fd, path, flags, 99, 99);
  106. }
  107. static char *concat(const char *left, const char *right)
  108. {
  109. char *result = malloc(strlen(left) + strlen(right) + 1);
  110. strcpy(result, left);
  111. strcat(result, right);
  112. return result;
  113. }
  114. static int open_or_die(const char *filename, int flags)
  115. {
  116. int fd = open(filename, flags);
  117. if (fd < 0) {
  118. printf("Failed to open '%s'; "
  119. "check prerequisites are available\n", filename);
  120. exit(1);
  121. }
  122. return fd;
  123. }
  124. static void exe_cp(const char *src, const char *dest)
  125. {
  126. int in_fd = open_or_die(src, O_RDONLY);
  127. int out_fd = open(dest, O_RDWR|O_CREAT|O_TRUNC, 0755);
  128. struct stat info;
  129. fstat(in_fd, &info);
  130. sendfile(out_fd, in_fd, NULL, info.st_size);
  131. close(in_fd);
  132. close(out_fd);
  133. }
  134. #define XX_DIR_LEN 200
  135. static int check_execveat_pathmax(int root_dfd, const char *src, int is_script)
  136. {
  137. int fail = 0;
  138. int ii, count, len;
  139. char longname[XX_DIR_LEN + 1];
  140. int fd;
  141. if (*longpath == '\0') {
  142. /* Create a filename close to PATH_MAX in length */
  143. char *cwd = getcwd(NULL, 0);
  144. if (!cwd) {
  145. printf("Failed to getcwd(), errno=%d (%s)\n",
  146. errno, strerror(errno));
  147. return 2;
  148. }
  149. strcpy(longpath, cwd);
  150. strcat(longpath, "/");
  151. memset(longname, 'x', XX_DIR_LEN - 1);
  152. longname[XX_DIR_LEN - 1] = '/';
  153. longname[XX_DIR_LEN] = '\0';
  154. count = (PATH_MAX - 3 - strlen(cwd)) / XX_DIR_LEN;
  155. for (ii = 0; ii < count; ii++) {
  156. strcat(longpath, longname);
  157. mkdir(longpath, 0755);
  158. }
  159. len = (PATH_MAX - 3 - strlen(cwd)) - (count * XX_DIR_LEN);
  160. if (len <= 0)
  161. len = 1;
  162. memset(longname, 'y', len);
  163. longname[len] = '\0';
  164. strcat(longpath, longname);
  165. free(cwd);
  166. }
  167. exe_cp(src, longpath);
  168. /*
  169. * Execute as a pre-opened file descriptor, which works whether this is
  170. * a script or not (because the interpreter sees a filename like
  171. * "/dev/fd/20").
  172. */
  173. fd = open(longpath, O_RDONLY);
  174. if (fd > 0) {
  175. printf("Invoke copy of '%s' via filename of length %zu:\n",
  176. src, strlen(longpath));
  177. fail += check_execveat(fd, "", AT_EMPTY_PATH);
  178. } else {
  179. printf("Failed to open length %zu filename, errno=%d (%s)\n",
  180. strlen(longpath), errno, strerror(errno));
  181. fail++;
  182. }
  183. /*
  184. * Execute as a long pathname relative to "/". If this is a script,
  185. * the interpreter will launch but fail to open the script because its
  186. * name ("/dev/fd/5/xxx....") is bigger than PATH_MAX.
  187. *
  188. * The failure code is usually 127 (POSIX: "If a command is not found,
  189. * the exit status shall be 127."), but some systems give 126 (POSIX:
  190. * "If the command name is found, but it is not an executable utility,
  191. * the exit status shall be 126."), so allow either.
  192. */
  193. if (is_script)
  194. fail += check_execveat_invoked_rc(root_dfd, longpath + 1, 0,
  195. 127, 126);
  196. else
  197. fail += check_execveat(root_dfd, longpath + 1, 0);
  198. return fail;
  199. }
  200. static int run_tests(void)
  201. {
  202. int fail = 0;
  203. char *fullname = realpath("execveat", NULL);
  204. char *fullname_script = realpath("script", NULL);
  205. char *fullname_symlink = concat(fullname, ".symlink");
  206. int subdir_dfd = open_or_die("subdir", O_DIRECTORY|O_RDONLY);
  207. int subdir_dfd_ephemeral = open_or_die("subdir.ephemeral",
  208. O_DIRECTORY|O_RDONLY);
  209. int dot_dfd = open_or_die(".", O_DIRECTORY|O_RDONLY);
  210. int root_dfd = open_or_die("/", O_DIRECTORY|O_RDONLY);
  211. int dot_dfd_path = open_or_die(".", O_DIRECTORY|O_RDONLY|O_PATH);
  212. int dot_dfd_cloexec = open_or_die(".", O_DIRECTORY|O_RDONLY|O_CLOEXEC);
  213. int fd = open_or_die("execveat", O_RDONLY);
  214. int fd_path = open_or_die("execveat", O_RDONLY|O_PATH);
  215. int fd_symlink = open_or_die("execveat.symlink", O_RDONLY);
  216. int fd_denatured = open_or_die("execveat.denatured", O_RDONLY);
  217. int fd_denatured_path = open_or_die("execveat.denatured",
  218. O_RDONLY|O_PATH);
  219. int fd_script = open_or_die("script", O_RDONLY);
  220. int fd_ephemeral = open_or_die("execveat.ephemeral", O_RDONLY);
  221. int fd_ephemeral_path = open_or_die("execveat.path.ephemeral",
  222. O_RDONLY|O_PATH);
  223. int fd_script_ephemeral = open_or_die("script.ephemeral", O_RDONLY);
  224. int fd_cloexec = open_or_die("execveat", O_RDONLY|O_CLOEXEC);
  225. int fd_script_cloexec = open_or_die("script", O_RDONLY|O_CLOEXEC);
  226. /* Check if we have execveat at all, and bail early if not */
  227. errno = 0;
  228. execveat_(-1, NULL, NULL, NULL, 0);
  229. if (errno == ENOSYS) {
  230. ksft_exit_skip(
  231. "ENOSYS calling execveat - no kernel support?\n");
  232. }
  233. /* Change file position to confirm it doesn't affect anything */
  234. lseek(fd, 10, SEEK_SET);
  235. /* Normal executable file: */
  236. /* dfd + path */
  237. fail += check_execveat(subdir_dfd, "../execveat", 0);
  238. fail += check_execveat(dot_dfd, "execveat", 0);
  239. fail += check_execveat(dot_dfd_path, "execveat", 0);
  240. /* absolute path */
  241. fail += check_execveat(AT_FDCWD, fullname, 0);
  242. /* absolute path with nonsense dfd */
  243. fail += check_execveat(99, fullname, 0);
  244. /* fd + no path */
  245. fail += check_execveat(fd, "", AT_EMPTY_PATH);
  246. /* O_CLOEXEC fd + no path */
  247. fail += check_execveat(fd_cloexec, "", AT_EMPTY_PATH);
  248. /* O_PATH fd */
  249. fail += check_execveat(fd_path, "", AT_EMPTY_PATH);
  250. /* Mess with executable file that's already open: */
  251. /* fd + no path to a file that's been renamed */
  252. rename("execveat.ephemeral", "execveat.moved");
  253. fail += check_execveat(fd_ephemeral, "", AT_EMPTY_PATH);
  254. /* fd + no path to a file that's been deleted */
  255. unlink("execveat.moved"); /* remove the file now fd open */
  256. fail += check_execveat(fd_ephemeral, "", AT_EMPTY_PATH);
  257. /* Mess with executable file that's already open with O_PATH */
  258. /* fd + no path to a file that's been deleted */
  259. unlink("execveat.path.ephemeral");
  260. fail += check_execveat(fd_ephemeral_path, "", AT_EMPTY_PATH);
  261. /* Invalid argument failures */
  262. fail += check_execveat_fail(fd, "", 0, ENOENT);
  263. fail += check_execveat_fail(fd, NULL, AT_EMPTY_PATH, EFAULT);
  264. /* Symlink to executable file: */
  265. /* dfd + path */
  266. fail += check_execveat(dot_dfd, "execveat.symlink", 0);
  267. fail += check_execveat(dot_dfd_path, "execveat.symlink", 0);
  268. /* absolute path */
  269. fail += check_execveat(AT_FDCWD, fullname_symlink, 0);
  270. /* fd + no path, even with AT_SYMLINK_NOFOLLOW (already followed) */
  271. fail += check_execveat(fd_symlink, "", AT_EMPTY_PATH);
  272. fail += check_execveat(fd_symlink, "",
  273. AT_EMPTY_PATH|AT_SYMLINK_NOFOLLOW);
  274. /* Symlink fails when AT_SYMLINK_NOFOLLOW set: */
  275. /* dfd + path */
  276. fail += check_execveat_fail(dot_dfd, "execveat.symlink",
  277. AT_SYMLINK_NOFOLLOW, ELOOP);
  278. fail += check_execveat_fail(dot_dfd_path, "execveat.symlink",
  279. AT_SYMLINK_NOFOLLOW, ELOOP);
  280. /* absolute path */
  281. fail += check_execveat_fail(AT_FDCWD, fullname_symlink,
  282. AT_SYMLINK_NOFOLLOW, ELOOP);
  283. /* Shell script wrapping executable file: */
  284. /* dfd + path */
  285. fail += check_execveat(subdir_dfd, "../script", 0);
  286. fail += check_execveat(dot_dfd, "script", 0);
  287. fail += check_execveat(dot_dfd_path, "script", 0);
  288. /* absolute path */
  289. fail += check_execveat(AT_FDCWD, fullname_script, 0);
  290. /* fd + no path */
  291. fail += check_execveat(fd_script, "", AT_EMPTY_PATH);
  292. fail += check_execveat(fd_script, "",
  293. AT_EMPTY_PATH|AT_SYMLINK_NOFOLLOW);
  294. /* O_CLOEXEC fd fails for a script (as script file inaccessible) */
  295. fail += check_execveat_fail(fd_script_cloexec, "", AT_EMPTY_PATH,
  296. ENOENT);
  297. fail += check_execveat_fail(dot_dfd_cloexec, "script", 0, ENOENT);
  298. /* Mess with script file that's already open: */
  299. /* fd + no path to a file that's been renamed */
  300. rename("script.ephemeral", "script.moved");
  301. fail += check_execveat(fd_script_ephemeral, "", AT_EMPTY_PATH);
  302. /* fd + no path to a file that's been deleted */
  303. unlink("script.moved"); /* remove the file while fd open */
  304. fail += check_execveat(fd_script_ephemeral, "", AT_EMPTY_PATH);
  305. /* Rename a subdirectory in the path: */
  306. rename("subdir.ephemeral", "subdir.moved");
  307. fail += check_execveat(subdir_dfd_ephemeral, "../script", 0);
  308. fail += check_execveat(subdir_dfd_ephemeral, "script", 0);
  309. /* Remove the subdir and its contents */
  310. unlink("subdir.moved/script");
  311. unlink("subdir.moved");
  312. /* Shell loads via deleted subdir OK because name starts with .. */
  313. fail += check_execveat(subdir_dfd_ephemeral, "../script", 0);
  314. fail += check_execveat_fail(subdir_dfd_ephemeral, "script", 0, ENOENT);
  315. /* Flag values other than AT_SYMLINK_NOFOLLOW => EINVAL */
  316. fail += check_execveat_fail(dot_dfd, "execveat", 0xFFFF, EINVAL);
  317. /* Invalid path => ENOENT */
  318. fail += check_execveat_fail(dot_dfd, "no-such-file", 0, ENOENT);
  319. fail += check_execveat_fail(dot_dfd_path, "no-such-file", 0, ENOENT);
  320. fail += check_execveat_fail(AT_FDCWD, "no-such-file", 0, ENOENT);
  321. /* Attempt to execute directory => EACCES */
  322. fail += check_execveat_fail(dot_dfd, "", AT_EMPTY_PATH, EACCES);
  323. /* Attempt to execute non-executable => EACCES */
  324. fail += check_execveat_fail(dot_dfd, "Makefile", 0, EACCES);
  325. fail += check_execveat_fail(fd_denatured, "", AT_EMPTY_PATH, EACCES);
  326. fail += check_execveat_fail(fd_denatured_path, "", AT_EMPTY_PATH,
  327. EACCES);
  328. /* Attempt to execute nonsense FD => EBADF */
  329. fail += check_execveat_fail(99, "", AT_EMPTY_PATH, EBADF);
  330. fail += check_execveat_fail(99, "execveat", 0, EBADF);
  331. /* Attempt to execute relative to non-directory => ENOTDIR */
  332. fail += check_execveat_fail(fd, "execveat", 0, ENOTDIR);
  333. fail += check_execveat_pathmax(root_dfd, "execveat", 0);
  334. fail += check_execveat_pathmax(root_dfd, "script", 1);
  335. return fail;
  336. }
  337. static void prerequisites(void)
  338. {
  339. int fd;
  340. const char *script = "#!/bin/sh\nexit $*\n";
  341. /* Create ephemeral copies of files */
  342. exe_cp("execveat", "execveat.ephemeral");
  343. exe_cp("execveat", "execveat.path.ephemeral");
  344. exe_cp("script", "script.ephemeral");
  345. mkdir("subdir.ephemeral", 0755);
  346. fd = open("subdir.ephemeral/script", O_RDWR|O_CREAT|O_TRUNC, 0755);
  347. write(fd, script, strlen(script));
  348. close(fd);
  349. }
  350. int main(int argc, char **argv)
  351. {
  352. int ii;
  353. int rc;
  354. const char *verbose = getenv("VERBOSE");
  355. if (argc >= 2) {
  356. /* If we are invoked with an argument, don't run tests. */
  357. const char *in_test = getenv("IN_TEST");
  358. if (verbose) {
  359. printf(" invoked with:");
  360. for (ii = 0; ii < argc; ii++)
  361. printf(" [%d]='%s'", ii, argv[ii]);
  362. printf("\n");
  363. }
  364. /* Check expected environment transferred. */
  365. if (!in_test || strcmp(in_test, "yes") != 0) {
  366. printf("[FAIL] (no IN_TEST=yes in env)\n");
  367. return 1;
  368. }
  369. /* Use the final argument as an exit code. */
  370. rc = atoi(argv[argc - 1]);
  371. fflush(stdout);
  372. } else {
  373. prerequisites();
  374. if (verbose)
  375. envp[1] = "VERBOSE=1";
  376. rc = run_tests();
  377. if (rc > 0)
  378. printf("%d tests failed\n", rc);
  379. }
  380. return rc;
  381. }