execveat.c 13 KB

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