execveat.c 12 KB

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