test_execve.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. #define _GNU_SOURCE
  2. #include <cap-ng.h>
  3. #include <err.h>
  4. #include <linux/capability.h>
  5. #include <stdbool.h>
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <fcntl.h>
  9. #include <errno.h>
  10. #include <stdarg.h>
  11. #include <sched.h>
  12. #include <sys/mount.h>
  13. #include <limits.h>
  14. #include <libgen.h>
  15. #include <malloc.h>
  16. #include <sys/wait.h>
  17. #include <sys/prctl.h>
  18. #include <sys/stat.h>
  19. #ifndef PR_CAP_AMBIENT
  20. #define PR_CAP_AMBIENT 47
  21. # define PR_CAP_AMBIENT_IS_SET 1
  22. # define PR_CAP_AMBIENT_RAISE 2
  23. # define PR_CAP_AMBIENT_LOWER 3
  24. # define PR_CAP_AMBIENT_CLEAR_ALL 4
  25. #endif
  26. static int nerrs;
  27. static void vmaybe_write_file(bool enoent_ok, char *filename, char *fmt, va_list ap)
  28. {
  29. char buf[4096];
  30. int fd;
  31. ssize_t written;
  32. int buf_len;
  33. buf_len = vsnprintf(buf, sizeof(buf), fmt, ap);
  34. if (buf_len < 0) {
  35. err(1, "vsnprintf failed");
  36. }
  37. if (buf_len >= sizeof(buf)) {
  38. errx(1, "vsnprintf output truncated");
  39. }
  40. fd = open(filename, O_WRONLY);
  41. if (fd < 0) {
  42. if ((errno == ENOENT) && enoent_ok)
  43. return;
  44. err(1, "open of %s failed", filename);
  45. }
  46. written = write(fd, buf, buf_len);
  47. if (written != buf_len) {
  48. if (written >= 0) {
  49. errx(1, "short write to %s", filename);
  50. } else {
  51. err(1, "write to %s failed", filename);
  52. }
  53. }
  54. if (close(fd) != 0) {
  55. err(1, "close of %s failed", filename);
  56. }
  57. }
  58. static void maybe_write_file(char *filename, char *fmt, ...)
  59. {
  60. va_list ap;
  61. va_start(ap, fmt);
  62. vmaybe_write_file(true, filename, fmt, ap);
  63. va_end(ap);
  64. }
  65. static void write_file(char *filename, char *fmt, ...)
  66. {
  67. va_list ap;
  68. va_start(ap, fmt);
  69. vmaybe_write_file(false, filename, fmt, ap);
  70. va_end(ap);
  71. }
  72. static bool create_and_enter_ns(uid_t inner_uid)
  73. {
  74. uid_t outer_uid;
  75. gid_t outer_gid;
  76. int i;
  77. bool have_outer_privilege;
  78. outer_uid = getuid();
  79. outer_gid = getgid();
  80. /*
  81. * TODO: If we're already root, we could skip creating the userns.
  82. */
  83. if (unshare(CLONE_NEWNS) == 0) {
  84. printf("[NOTE]\tUsing global UIDs for tests\n");
  85. if (prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0) != 0)
  86. err(1, "PR_SET_KEEPCAPS");
  87. if (setresuid(inner_uid, inner_uid, -1) != 0)
  88. err(1, "setresuid");
  89. // Re-enable effective caps
  90. capng_get_caps_process();
  91. for (i = 0; i < CAP_LAST_CAP; i++)
  92. if (capng_have_capability(CAPNG_PERMITTED, i))
  93. capng_update(CAPNG_ADD, CAPNG_EFFECTIVE, i);
  94. if (capng_apply(CAPNG_SELECT_CAPS) != 0)
  95. err(1, "capng_apply");
  96. have_outer_privilege = true;
  97. } else if (unshare(CLONE_NEWUSER | CLONE_NEWNS) == 0) {
  98. printf("[NOTE]\tUsing a user namespace for tests\n");
  99. maybe_write_file("/proc/self/setgroups", "deny");
  100. write_file("/proc/self/uid_map", "%d %d 1", inner_uid, outer_uid);
  101. write_file("/proc/self/gid_map", "0 %d 1", outer_gid);
  102. have_outer_privilege = false;
  103. } else {
  104. errx(1, "must be root or be able to create a userns");
  105. }
  106. if (mount("none", "/", NULL, MS_REC | MS_PRIVATE, NULL) != 0)
  107. err(1, "remount everything private");
  108. return have_outer_privilege;
  109. }
  110. static void chdir_to_tmpfs(void)
  111. {
  112. char cwd[PATH_MAX];
  113. if (getcwd(cwd, sizeof(cwd)) != cwd)
  114. err(1, "getcwd");
  115. if (mount("private_tmp", ".", "tmpfs", 0, "mode=0777") != 0)
  116. err(1, "mount private tmpfs");
  117. if (chdir(cwd) != 0)
  118. err(1, "chdir to private tmpfs");
  119. if (umount2(".", MNT_DETACH) != 0)
  120. err(1, "detach private tmpfs");
  121. }
  122. static void copy_fromat_to(int fromfd, const char *fromname, const char *toname)
  123. {
  124. int from = openat(fromfd, fromname, O_RDONLY);
  125. if (from == -1)
  126. err(1, "open copy source");
  127. int to = open(toname, O_CREAT | O_WRONLY | O_EXCL, 0700);
  128. while (true) {
  129. char buf[4096];
  130. ssize_t sz = read(from, buf, sizeof(buf));
  131. if (sz == 0)
  132. break;
  133. if (sz < 0)
  134. err(1, "read");
  135. if (write(to, buf, sz) != sz)
  136. err(1, "write"); /* no short writes on tmpfs */
  137. }
  138. close(from);
  139. close(to);
  140. }
  141. static bool fork_wait(void)
  142. {
  143. pid_t child = fork();
  144. if (child == 0) {
  145. nerrs = 0;
  146. return true;
  147. } else if (child > 0) {
  148. int status;
  149. if (waitpid(child, &status, 0) != child ||
  150. !WIFEXITED(status)) {
  151. printf("[FAIL]\tChild died\n");
  152. nerrs++;
  153. } else if (WEXITSTATUS(status) != 0) {
  154. printf("[FAIL]\tChild failed\n");
  155. nerrs++;
  156. } else {
  157. printf("[OK]\tChild succeeded\n");
  158. }
  159. return false;
  160. } else {
  161. err(1, "fork");
  162. }
  163. }
  164. static void exec_other_validate_cap(const char *name,
  165. bool eff, bool perm, bool inh, bool ambient)
  166. {
  167. execl(name, name, (eff ? "1" : "0"),
  168. (perm ? "1" : "0"), (inh ? "1" : "0"), (ambient ? "1" : "0"),
  169. NULL);
  170. err(1, "execl");
  171. }
  172. static void exec_validate_cap(bool eff, bool perm, bool inh, bool ambient)
  173. {
  174. exec_other_validate_cap("./validate_cap", eff, perm, inh, ambient);
  175. }
  176. static int do_tests(int uid, const char *our_path)
  177. {
  178. bool have_outer_privilege = create_and_enter_ns(uid);
  179. int ourpath_fd = open(our_path, O_RDONLY | O_DIRECTORY);
  180. if (ourpath_fd == -1)
  181. err(1, "open '%s'", our_path);
  182. chdir_to_tmpfs();
  183. copy_fromat_to(ourpath_fd, "validate_cap", "validate_cap");
  184. if (have_outer_privilege) {
  185. uid_t gid = getegid();
  186. copy_fromat_to(ourpath_fd, "validate_cap",
  187. "validate_cap_suidroot");
  188. if (chown("validate_cap_suidroot", 0, -1) != 0)
  189. err(1, "chown");
  190. if (chmod("validate_cap_suidroot", S_ISUID | 0700) != 0)
  191. err(1, "chmod");
  192. copy_fromat_to(ourpath_fd, "validate_cap",
  193. "validate_cap_suidnonroot");
  194. if (chown("validate_cap_suidnonroot", uid + 1, -1) != 0)
  195. err(1, "chown");
  196. if (chmod("validate_cap_suidnonroot", S_ISUID | 0700) != 0)
  197. err(1, "chmod");
  198. copy_fromat_to(ourpath_fd, "validate_cap",
  199. "validate_cap_sgidroot");
  200. if (chown("validate_cap_sgidroot", -1, 0) != 0)
  201. err(1, "chown");
  202. if (chmod("validate_cap_sgidroot", S_ISGID | 0710) != 0)
  203. err(1, "chmod");
  204. copy_fromat_to(ourpath_fd, "validate_cap",
  205. "validate_cap_sgidnonroot");
  206. if (chown("validate_cap_sgidnonroot", -1, gid + 1) != 0)
  207. err(1, "chown");
  208. if (chmod("validate_cap_sgidnonroot", S_ISGID | 0710) != 0)
  209. err(1, "chmod");
  210. }
  211. capng_get_caps_process();
  212. /* Make sure that i starts out clear */
  213. capng_update(CAPNG_DROP, CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE);
  214. if (capng_apply(CAPNG_SELECT_CAPS) != 0)
  215. err(1, "capng_apply");
  216. if (uid == 0) {
  217. printf("[RUN]\tRoot => ep\n");
  218. if (fork_wait())
  219. exec_validate_cap(true, true, false, false);
  220. } else {
  221. printf("[RUN]\tNon-root => no caps\n");
  222. if (fork_wait())
  223. exec_validate_cap(false, false, false, false);
  224. }
  225. printf("[OK]\tCheck cap_ambient manipulation rules\n");
  226. /* We should not be able to add ambient caps yet. */
  227. if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_BIND_SERVICE, 0, 0, 0) != -1 || errno != EPERM) {
  228. if (errno == EINVAL)
  229. printf("[FAIL]\tPR_CAP_AMBIENT_RAISE isn't supported\n");
  230. else
  231. printf("[FAIL]\tPR_CAP_AMBIENT_RAISE should have failed eith EPERM on a non-inheritable cap\n");
  232. return 1;
  233. }
  234. printf("[OK]\tPR_CAP_AMBIENT_RAISE failed on non-inheritable cap\n");
  235. capng_update(CAPNG_ADD, CAPNG_INHERITABLE, CAP_NET_RAW);
  236. capng_update(CAPNG_DROP, CAPNG_PERMITTED, CAP_NET_RAW);
  237. capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_NET_RAW);
  238. if (capng_apply(CAPNG_SELECT_CAPS) != 0)
  239. err(1, "capng_apply");
  240. if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_RAW, 0, 0, 0) != -1 || errno != EPERM) {
  241. printf("[FAIL]\tPR_CAP_AMBIENT_RAISE should have failed on a non-permitted cap\n");
  242. return 1;
  243. }
  244. printf("[OK]\tPR_CAP_AMBIENT_RAISE failed on non-permitted cap\n");
  245. capng_update(CAPNG_ADD, CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE);
  246. if (capng_apply(CAPNG_SELECT_CAPS) != 0)
  247. err(1, "capng_apply");
  248. if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0) {
  249. printf("[FAIL]\tPR_CAP_AMBIENT_RAISE should have succeeded\n");
  250. return 1;
  251. }
  252. printf("[OK]\tPR_CAP_AMBIENT_RAISE worked\n");
  253. if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_NET_BIND_SERVICE, 0, 0, 0) != 1) {
  254. printf("[FAIL]\tPR_CAP_AMBIENT_IS_SET is broken\n");
  255. return 1;
  256. }
  257. if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0, 0) != 0)
  258. err(1, "PR_CAP_AMBIENT_CLEAR_ALL");
  259. if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0) {
  260. printf("[FAIL]\tPR_CAP_AMBIENT_CLEAR_ALL didn't work\n");
  261. return 1;
  262. }
  263. if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0)
  264. err(1, "PR_CAP_AMBIENT_RAISE");
  265. capng_update(CAPNG_DROP, CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE);
  266. if (capng_apply(CAPNG_SELECT_CAPS) != 0)
  267. err(1, "capng_apply");
  268. if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0) {
  269. printf("[FAIL]\tDropping I should have dropped A\n");
  270. return 1;
  271. }
  272. printf("[OK]\tBasic manipulation appears to work\n");
  273. capng_update(CAPNG_ADD, CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE);
  274. if (capng_apply(CAPNG_SELECT_CAPS) != 0)
  275. err(1, "capng_apply");
  276. if (uid == 0) {
  277. printf("[RUN]\tRoot +i => eip\n");
  278. if (fork_wait())
  279. exec_validate_cap(true, true, true, false);
  280. } else {
  281. printf("[RUN]\tNon-root +i => i\n");
  282. if (fork_wait())
  283. exec_validate_cap(false, false, true, false);
  284. }
  285. if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0)
  286. err(1, "PR_CAP_AMBIENT_RAISE");
  287. printf("[RUN]\tUID %d +ia => eipa\n", uid);
  288. if (fork_wait())
  289. exec_validate_cap(true, true, true, true);
  290. /* The remaining tests need real privilege */
  291. if (!have_outer_privilege) {
  292. printf("[SKIP]\tSUID/SGID tests (needs privilege)\n");
  293. goto done;
  294. }
  295. if (uid == 0) {
  296. printf("[RUN]\tRoot +ia, suidroot => eipa\n");
  297. if (fork_wait())
  298. exec_other_validate_cap("./validate_cap_suidroot",
  299. true, true, true, true);
  300. printf("[RUN]\tRoot +ia, suidnonroot => ip\n");
  301. if (fork_wait())
  302. exec_other_validate_cap("./validate_cap_suidnonroot",
  303. false, true, true, false);
  304. printf("[RUN]\tRoot +ia, sgidroot => eipa\n");
  305. if (fork_wait())
  306. exec_other_validate_cap("./validate_cap_sgidroot",
  307. true, true, true, true);
  308. if (fork_wait()) {
  309. printf("[RUN]\tRoot, gid != 0, +ia, sgidroot => eip\n");
  310. if (setresgid(1, 1, 1) != 0)
  311. err(1, "setresgid");
  312. exec_other_validate_cap("./validate_cap_sgidroot",
  313. true, true, true, false);
  314. }
  315. printf("[RUN]\tRoot +ia, sgidnonroot => eip\n");
  316. if (fork_wait())
  317. exec_other_validate_cap("./validate_cap_sgidnonroot",
  318. true, true, true, false);
  319. } else {
  320. printf("[RUN]\tNon-root +ia, sgidnonroot => i\n");
  321. exec_other_validate_cap("./validate_cap_sgidnonroot",
  322. false, false, true, false);
  323. if (fork_wait()) {
  324. printf("[RUN]\tNon-root +ia, sgidroot => i\n");
  325. if (setresgid(1, 1, 1) != 0)
  326. err(1, "setresgid");
  327. exec_other_validate_cap("./validate_cap_sgidroot",
  328. false, false, true, false);
  329. }
  330. }
  331. done:
  332. return nerrs ? 1 : 0;
  333. }
  334. int main(int argc, char **argv)
  335. {
  336. char *tmp1, *tmp2, *our_path;
  337. /* Find our path */
  338. tmp1 = strdup(argv[0]);
  339. if (!tmp1)
  340. err(1, "strdup");
  341. tmp2 = dirname(tmp1);
  342. our_path = strdup(tmp2);
  343. if (!our_path)
  344. err(1, "strdup");
  345. free(tmp1);
  346. if (fork_wait()) {
  347. printf("[RUN]\t+++ Tests with uid == 0 +++\n");
  348. return do_tests(0, our_path);
  349. }
  350. if (fork_wait()) {
  351. printf("[RUN]\t+++ Tests with uid != 0 +++\n");
  352. return do_tests(1, our_path);
  353. }
  354. return nerrs ? 1 : 0;
  355. }