execveat.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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 dot_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. memset(longname, 'x', XX_DIR_LEN - 1);
  143. longname[XX_DIR_LEN - 1] = '/';
  144. longname[XX_DIR_LEN] = '\0';
  145. count = (PATH_MAX - 3) / XX_DIR_LEN;
  146. for (ii = 0; ii < count; ii++) {
  147. strcat(longpath, longname);
  148. mkdir(longpath, 0755);
  149. }
  150. len = (PATH_MAX - 3) - (count * XX_DIR_LEN);
  151. if (len <= 0)
  152. len = 1;
  153. memset(longname, 'y', len);
  154. longname[len] = '\0';
  155. strcat(longpath, longname);
  156. }
  157. exe_cp(src, longpath);
  158. /*
  159. * Execute as a pre-opened file descriptor, which works whether this is
  160. * a script or not (because the interpreter sees a filename like
  161. * "/dev/fd/20").
  162. */
  163. fd = open(longpath, O_RDONLY);
  164. if (fd > 0) {
  165. printf("Invoke copy of '%s' via filename of length %zu:\n",
  166. src, strlen(longpath));
  167. fail += check_execveat(fd, "", AT_EMPTY_PATH);
  168. } else {
  169. printf("Failed to open length %zu filename, errno=%d (%s)\n",
  170. strlen(longpath), errno, strerror(errno));
  171. fail++;
  172. }
  173. /*
  174. * Execute as a long pathname relative to ".". If this is a script,
  175. * the interpreter will launch but fail to open the script because its
  176. * name ("/dev/fd/5/xxx....") is bigger than PATH_MAX.
  177. *
  178. * The failure code is usually 127 (POSIX: "If a command is not found,
  179. * the exit status shall be 127."), but some systems give 126 (POSIX:
  180. * "If the command name is found, but it is not an executable utility,
  181. * the exit status shall be 126."), so allow either.
  182. */
  183. if (is_script)
  184. fail += check_execveat_invoked_rc(dot_dfd, longpath, 0,
  185. 127, 126);
  186. else
  187. fail += check_execveat(dot_dfd, longpath, 0);
  188. return fail;
  189. }
  190. static int run_tests(void)
  191. {
  192. int fail = 0;
  193. char *fullname = realpath("execveat", NULL);
  194. char *fullname_script = realpath("script", NULL);
  195. char *fullname_symlink = concat(fullname, ".symlink");
  196. int subdir_dfd = open_or_die("subdir", O_DIRECTORY|O_RDONLY);
  197. int subdir_dfd_ephemeral = open_or_die("subdir.ephemeral",
  198. O_DIRECTORY|O_RDONLY);
  199. int dot_dfd = open_or_die(".", O_DIRECTORY|O_RDONLY);
  200. int dot_dfd_path = open_or_die(".", O_DIRECTORY|O_RDONLY|O_PATH);
  201. int dot_dfd_cloexec = open_or_die(".", O_DIRECTORY|O_RDONLY|O_CLOEXEC);
  202. int fd = open_or_die("execveat", O_RDONLY);
  203. int fd_path = open_or_die("execveat", O_RDONLY|O_PATH);
  204. int fd_symlink = open_or_die("execveat.symlink", O_RDONLY);
  205. int fd_denatured = open_or_die("execveat.denatured", O_RDONLY);
  206. int fd_denatured_path = open_or_die("execveat.denatured",
  207. O_RDONLY|O_PATH);
  208. int fd_script = open_or_die("script", O_RDONLY);
  209. int fd_ephemeral = open_or_die("execveat.ephemeral", O_RDONLY);
  210. int fd_ephemeral_path = open_or_die("execveat.path.ephemeral",
  211. O_RDONLY|O_PATH);
  212. int fd_script_ephemeral = open_or_die("script.ephemeral", O_RDONLY);
  213. int fd_cloexec = open_or_die("execveat", O_RDONLY|O_CLOEXEC);
  214. int fd_script_cloexec = open_or_die("script", O_RDONLY|O_CLOEXEC);
  215. /* Check if we have execveat at all, and bail early if not */
  216. errno = 0;
  217. execveat_(-1, NULL, NULL, NULL, 0);
  218. if (errno == ENOSYS) {
  219. printf("[FAIL] ENOSYS calling execveat - no kernel support?\n");
  220. return 1;
  221. }
  222. /* Change file position to confirm it doesn't affect anything */
  223. lseek(fd, 10, SEEK_SET);
  224. /* Normal executable file: */
  225. /* dfd + path */
  226. fail += check_execveat(subdir_dfd, "../execveat", 0);
  227. fail += check_execveat(dot_dfd, "execveat", 0);
  228. fail += check_execveat(dot_dfd_path, "execveat", 0);
  229. /* absolute path */
  230. fail += check_execveat(AT_FDCWD, fullname, 0);
  231. /* absolute path with nonsense dfd */
  232. fail += check_execveat(99, fullname, 0);
  233. /* fd + no path */
  234. fail += check_execveat(fd, "", AT_EMPTY_PATH);
  235. /* O_CLOEXEC fd + no path */
  236. fail += check_execveat(fd_cloexec, "", AT_EMPTY_PATH);
  237. /* O_PATH fd */
  238. fail += check_execveat(fd_path, "", AT_EMPTY_PATH);
  239. /* Mess with executable file that's already open: */
  240. /* fd + no path to a file that's been renamed */
  241. rename("execveat.ephemeral", "execveat.moved");
  242. fail += check_execveat(fd_ephemeral, "", AT_EMPTY_PATH);
  243. /* fd + no path to a file that's been deleted */
  244. unlink("execveat.moved"); /* remove the file now fd open */
  245. fail += check_execveat(fd_ephemeral, "", AT_EMPTY_PATH);
  246. /* Mess with executable file that's already open with O_PATH */
  247. /* fd + no path to a file that's been deleted */
  248. unlink("execveat.path.ephemeral");
  249. fail += check_execveat(fd_ephemeral_path, "", AT_EMPTY_PATH);
  250. /* Invalid argument failures */
  251. fail += check_execveat_fail(fd, "", 0, ENOENT);
  252. fail += check_execveat_fail(fd, NULL, AT_EMPTY_PATH, EFAULT);
  253. /* Symlink to executable file: */
  254. /* dfd + path */
  255. fail += check_execveat(dot_dfd, "execveat.symlink", 0);
  256. fail += check_execveat(dot_dfd_path, "execveat.symlink", 0);
  257. /* absolute path */
  258. fail += check_execveat(AT_FDCWD, fullname_symlink, 0);
  259. /* fd + no path, even with AT_SYMLINK_NOFOLLOW (already followed) */
  260. fail += check_execveat(fd_symlink, "", AT_EMPTY_PATH);
  261. fail += check_execveat(fd_symlink, "",
  262. AT_EMPTY_PATH|AT_SYMLINK_NOFOLLOW);
  263. /* Symlink fails when AT_SYMLINK_NOFOLLOW set: */
  264. /* dfd + path */
  265. fail += check_execveat_fail(dot_dfd, "execveat.symlink",
  266. AT_SYMLINK_NOFOLLOW, ELOOP);
  267. fail += check_execveat_fail(dot_dfd_path, "execveat.symlink",
  268. AT_SYMLINK_NOFOLLOW, ELOOP);
  269. /* absolute path */
  270. fail += check_execveat_fail(AT_FDCWD, fullname_symlink,
  271. AT_SYMLINK_NOFOLLOW, ELOOP);
  272. /* Shell script wrapping executable file: */
  273. /* dfd + path */
  274. fail += check_execveat(subdir_dfd, "../script", 0);
  275. fail += check_execveat(dot_dfd, "script", 0);
  276. fail += check_execveat(dot_dfd_path, "script", 0);
  277. /* absolute path */
  278. fail += check_execveat(AT_FDCWD, fullname_script, 0);
  279. /* fd + no path */
  280. fail += check_execveat(fd_script, "", AT_EMPTY_PATH);
  281. fail += check_execveat(fd_script, "",
  282. AT_EMPTY_PATH|AT_SYMLINK_NOFOLLOW);
  283. /* O_CLOEXEC fd fails for a script (as script file inaccessible) */
  284. fail += check_execveat_fail(fd_script_cloexec, "", AT_EMPTY_PATH,
  285. ENOENT);
  286. fail += check_execveat_fail(dot_dfd_cloexec, "script", 0, ENOENT);
  287. /* Mess with script file that's already open: */
  288. /* fd + no path to a file that's been renamed */
  289. rename("script.ephemeral", "script.moved");
  290. fail += check_execveat(fd_script_ephemeral, "", AT_EMPTY_PATH);
  291. /* fd + no path to a file that's been deleted */
  292. unlink("script.moved"); /* remove the file while fd open */
  293. fail += check_execveat(fd_script_ephemeral, "", AT_EMPTY_PATH);
  294. /* Rename a subdirectory in the path: */
  295. rename("subdir.ephemeral", "subdir.moved");
  296. fail += check_execveat(subdir_dfd_ephemeral, "../script", 0);
  297. fail += check_execveat(subdir_dfd_ephemeral, "script", 0);
  298. /* Remove the subdir and its contents */
  299. unlink("subdir.moved/script");
  300. unlink("subdir.moved");
  301. /* Shell loads via deleted subdir OK because name starts with .. */
  302. fail += check_execveat(subdir_dfd_ephemeral, "../script", 0);
  303. fail += check_execveat_fail(subdir_dfd_ephemeral, "script", 0, ENOENT);
  304. /* Flag values other than AT_SYMLINK_NOFOLLOW => EINVAL */
  305. fail += check_execveat_fail(dot_dfd, "execveat", 0xFFFF, EINVAL);
  306. /* Invalid path => ENOENT */
  307. fail += check_execveat_fail(dot_dfd, "no-such-file", 0, ENOENT);
  308. fail += check_execveat_fail(dot_dfd_path, "no-such-file", 0, ENOENT);
  309. fail += check_execveat_fail(AT_FDCWD, "no-such-file", 0, ENOENT);
  310. /* Attempt to execute directory => EACCES */
  311. fail += check_execveat_fail(dot_dfd, "", AT_EMPTY_PATH, EACCES);
  312. /* Attempt to execute non-executable => EACCES */
  313. fail += check_execveat_fail(dot_dfd, "Makefile", 0, EACCES);
  314. fail += check_execveat_fail(fd_denatured, "", AT_EMPTY_PATH, EACCES);
  315. fail += check_execveat_fail(fd_denatured_path, "", AT_EMPTY_PATH,
  316. EACCES);
  317. /* Attempt to execute nonsense FD => EBADF */
  318. fail += check_execveat_fail(99, "", AT_EMPTY_PATH, EBADF);
  319. fail += check_execveat_fail(99, "execveat", 0, EBADF);
  320. /* Attempt to execute relative to non-directory => ENOTDIR */
  321. fail += check_execveat_fail(fd, "execveat", 0, ENOTDIR);
  322. fail += check_execveat_pathmax(dot_dfd, "execveat", 0);
  323. fail += check_execveat_pathmax(dot_dfd, "script", 1);
  324. return fail;
  325. }
  326. static void prerequisites(void)
  327. {
  328. int fd;
  329. const char *script = "#!/bin/sh\nexit $*\n";
  330. /* Create ephemeral copies of files */
  331. exe_cp("execveat", "execveat.ephemeral");
  332. exe_cp("execveat", "execveat.path.ephemeral");
  333. exe_cp("script", "script.ephemeral");
  334. mkdir("subdir.ephemeral", 0755);
  335. fd = open("subdir.ephemeral/script", O_RDWR|O_CREAT|O_TRUNC, 0755);
  336. write(fd, script, strlen(script));
  337. close(fd);
  338. }
  339. int main(int argc, char **argv)
  340. {
  341. int ii;
  342. int rc;
  343. const char *verbose = getenv("VERBOSE");
  344. if (argc >= 2) {
  345. /* If we are invoked with an argument, don't run tests. */
  346. const char *in_test = getenv("IN_TEST");
  347. if (verbose) {
  348. printf(" invoked with:");
  349. for (ii = 0; ii < argc; ii++)
  350. printf(" [%d]='%s'", ii, argv[ii]);
  351. printf("\n");
  352. }
  353. /* Check expected environment transferred. */
  354. if (!in_test || strcmp(in_test, "yes") != 0) {
  355. printf("[FAIL] (no IN_TEST=yes in env)\n");
  356. return 1;
  357. }
  358. /* Use the final argument as an exit code. */
  359. rc = atoi(argv[argc - 1]);
  360. fflush(stdout);
  361. } else {
  362. prerequisites();
  363. if (verbose)
  364. envp[1] = "VERBOSE=1";
  365. rc = run_tests();
  366. if (rc > 0)
  367. printf("%d tests failed\n", rc);
  368. }
  369. return rc;
  370. }