execveat.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404
  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. /* Change file position to confirm it doesn't affect anything */
  216. lseek(fd, 10, SEEK_SET);
  217. /* Normal executable file: */
  218. /* dfd + path */
  219. fail += check_execveat(subdir_dfd, "../execveat", 0);
  220. fail += check_execveat(dot_dfd, "execveat", 0);
  221. fail += check_execveat(dot_dfd_path, "execveat", 0);
  222. /* absolute path */
  223. fail += check_execveat(AT_FDCWD, fullname, 0);
  224. /* absolute path with nonsense dfd */
  225. fail += check_execveat(99, fullname, 0);
  226. /* fd + no path */
  227. fail += check_execveat(fd, "", AT_EMPTY_PATH);
  228. /* O_CLOEXEC fd + no path */
  229. fail += check_execveat(fd_cloexec, "", AT_EMPTY_PATH);
  230. /* O_PATH fd */
  231. fail += check_execveat(fd_path, "", AT_EMPTY_PATH);
  232. /* Mess with executable file that's already open: */
  233. /* fd + no path to a file that's been renamed */
  234. rename("execveat.ephemeral", "execveat.moved");
  235. fail += check_execveat(fd_ephemeral, "", AT_EMPTY_PATH);
  236. /* fd + no path to a file that's been deleted */
  237. unlink("execveat.moved"); /* remove the file now fd open */
  238. fail += check_execveat(fd_ephemeral, "", AT_EMPTY_PATH);
  239. /* Mess with executable file that's already open with O_PATH */
  240. /* fd + no path to a file that's been deleted */
  241. unlink("execveat.path.ephemeral");
  242. fail += check_execveat(fd_ephemeral_path, "", AT_EMPTY_PATH);
  243. /* Invalid argument failures */
  244. fail += check_execveat_fail(fd, "", 0, ENOENT);
  245. fail += check_execveat_fail(fd, NULL, AT_EMPTY_PATH, EFAULT);
  246. /* Symlink to executable file: */
  247. /* dfd + path */
  248. fail += check_execveat(dot_dfd, "execveat.symlink", 0);
  249. fail += check_execveat(dot_dfd_path, "execveat.symlink", 0);
  250. /* absolute path */
  251. fail += check_execveat(AT_FDCWD, fullname_symlink, 0);
  252. /* fd + no path, even with AT_SYMLINK_NOFOLLOW (already followed) */
  253. fail += check_execveat(fd_symlink, "", AT_EMPTY_PATH);
  254. fail += check_execveat(fd_symlink, "",
  255. AT_EMPTY_PATH|AT_SYMLINK_NOFOLLOW);
  256. /* Symlink fails when AT_SYMLINK_NOFOLLOW set: */
  257. /* dfd + path */
  258. fail += check_execveat_fail(dot_dfd, "execveat.symlink",
  259. AT_SYMLINK_NOFOLLOW, ELOOP);
  260. fail += check_execveat_fail(dot_dfd_path, "execveat.symlink",
  261. AT_SYMLINK_NOFOLLOW, ELOOP);
  262. /* absolute path */
  263. fail += check_execveat_fail(AT_FDCWD, fullname_symlink,
  264. AT_SYMLINK_NOFOLLOW, ELOOP);
  265. /* Shell script wrapping executable file: */
  266. /* dfd + path */
  267. fail += check_execveat(subdir_dfd, "../script", 0);
  268. fail += check_execveat(dot_dfd, "script", 0);
  269. fail += check_execveat(dot_dfd_path, "script", 0);
  270. /* absolute path */
  271. fail += check_execveat(AT_FDCWD, fullname_script, 0);
  272. /* fd + no path */
  273. fail += check_execveat(fd_script, "", AT_EMPTY_PATH);
  274. fail += check_execveat(fd_script, "",
  275. AT_EMPTY_PATH|AT_SYMLINK_NOFOLLOW);
  276. /* O_CLOEXEC fd fails for a script (as script file inaccessible) */
  277. fail += check_execveat_fail(fd_script_cloexec, "", AT_EMPTY_PATH,
  278. ENOENT);
  279. fail += check_execveat_fail(dot_dfd_cloexec, "script", 0, ENOENT);
  280. /* Mess with script file that's already open: */
  281. /* fd + no path to a file that's been renamed */
  282. rename("script.ephemeral", "script.moved");
  283. fail += check_execveat(fd_script_ephemeral, "", AT_EMPTY_PATH);
  284. /* fd + no path to a file that's been deleted */
  285. unlink("script.moved"); /* remove the file while fd open */
  286. fail += check_execveat(fd_script_ephemeral, "", AT_EMPTY_PATH);
  287. /* Rename a subdirectory in the path: */
  288. rename("subdir.ephemeral", "subdir.moved");
  289. fail += check_execveat(subdir_dfd_ephemeral, "../script", 0);
  290. fail += check_execveat(subdir_dfd_ephemeral, "script", 0);
  291. /* Remove the subdir and its contents */
  292. unlink("subdir.moved/script");
  293. unlink("subdir.moved");
  294. /* Shell loads via deleted subdir OK because name starts with .. */
  295. fail += check_execveat(subdir_dfd_ephemeral, "../script", 0);
  296. fail += check_execveat_fail(subdir_dfd_ephemeral, "script", 0, ENOENT);
  297. /* Flag values other than AT_SYMLINK_NOFOLLOW => EINVAL */
  298. fail += check_execveat_fail(dot_dfd, "execveat", 0xFFFF, EINVAL);
  299. /* Invalid path => ENOENT */
  300. fail += check_execveat_fail(dot_dfd, "no-such-file", 0, ENOENT);
  301. fail += check_execveat_fail(dot_dfd_path, "no-such-file", 0, ENOENT);
  302. fail += check_execveat_fail(AT_FDCWD, "no-such-file", 0, ENOENT);
  303. /* Attempt to execute directory => EACCES */
  304. fail += check_execveat_fail(dot_dfd, "", AT_EMPTY_PATH, EACCES);
  305. /* Attempt to execute non-executable => EACCES */
  306. fail += check_execveat_fail(dot_dfd, "Makefile", 0, EACCES);
  307. fail += check_execveat_fail(fd_denatured, "", AT_EMPTY_PATH, EACCES);
  308. fail += check_execveat_fail(fd_denatured_path, "", AT_EMPTY_PATH,
  309. EACCES);
  310. /* Attempt to execute nonsense FD => EBADF */
  311. fail += check_execveat_fail(99, "", AT_EMPTY_PATH, EBADF);
  312. fail += check_execveat_fail(99, "execveat", 0, EBADF);
  313. /* Attempt to execute relative to non-directory => ENOTDIR */
  314. fail += check_execveat_fail(fd, "execveat", 0, ENOTDIR);
  315. fail += check_execveat_pathmax(dot_dfd, "execveat", 0);
  316. fail += check_execveat_pathmax(dot_dfd, "script", 1);
  317. return fail;
  318. }
  319. static void prerequisites(void)
  320. {
  321. int fd;
  322. const char *script = "#!/bin/sh\nexit $*\n";
  323. /* Create ephemeral copies of files */
  324. exe_cp("execveat", "execveat.ephemeral");
  325. exe_cp("execveat", "execveat.path.ephemeral");
  326. exe_cp("script", "script.ephemeral");
  327. mkdir("subdir.ephemeral", 0755);
  328. fd = open("subdir.ephemeral/script", O_RDWR|O_CREAT|O_TRUNC, 0755);
  329. write(fd, script, strlen(script));
  330. close(fd);
  331. }
  332. int main(int argc, char **argv)
  333. {
  334. int ii;
  335. int rc;
  336. const char *verbose = getenv("VERBOSE");
  337. if (argc >= 2) {
  338. /* If we are invoked with an argument, don't run tests. */
  339. const char *in_test = getenv("IN_TEST");
  340. if (verbose) {
  341. printf(" invoked with:");
  342. for (ii = 0; ii < argc; ii++)
  343. printf(" [%d]='%s'", ii, argv[ii]);
  344. printf("\n");
  345. }
  346. /* Check expected environment transferred. */
  347. if (!in_test || strcmp(in_test, "yes") != 0) {
  348. printf("[FAIL] (no IN_TEST=yes in env)\n");
  349. return 1;
  350. }
  351. /* Use the final argument as an exit code. */
  352. rc = atoi(argv[argc - 1]);
  353. fflush(stdout);
  354. } else {
  355. prerequisites();
  356. if (verbose)
  357. envp[1] = "VERBOSE=1";
  358. rc = run_tests();
  359. if (rc > 0)
  360. printf("%d tests failed\n", rc);
  361. }
  362. return rc;
  363. }