test_execve.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. }
  120. static void copy_fromat_to(int fromfd, const char *fromname, const char *toname)
  121. {
  122. int from = openat(fromfd, fromname, O_RDONLY);
  123. if (from == -1)
  124. err(1, "open copy source");
  125. int to = open(toname, O_CREAT | O_WRONLY | O_EXCL, 0700);
  126. while (true) {
  127. char buf[4096];
  128. ssize_t sz = read(from, buf, sizeof(buf));
  129. if (sz == 0)
  130. break;
  131. if (sz < 0)
  132. err(1, "read");
  133. if (write(to, buf, sz) != sz)
  134. err(1, "write"); /* no short writes on tmpfs */
  135. }
  136. close(from);
  137. close(to);
  138. }
  139. static bool fork_wait(void)
  140. {
  141. pid_t child = fork();
  142. if (child == 0) {
  143. nerrs = 0;
  144. return true;
  145. } else if (child > 0) {
  146. int status;
  147. if (waitpid(child, &status, 0) != child ||
  148. !WIFEXITED(status)) {
  149. printf("[FAIL]\tChild died\n");
  150. nerrs++;
  151. } else if (WEXITSTATUS(status) != 0) {
  152. printf("[FAIL]\tChild failed\n");
  153. nerrs++;
  154. } else {
  155. printf("[OK]\tChild succeeded\n");
  156. }
  157. return false;
  158. } else {
  159. err(1, "fork");
  160. }
  161. }
  162. static void exec_other_validate_cap(const char *name,
  163. bool eff, bool perm, bool inh, bool ambient)
  164. {
  165. execl(name, name, (eff ? "1" : "0"),
  166. (perm ? "1" : "0"), (inh ? "1" : "0"), (ambient ? "1" : "0"),
  167. NULL);
  168. err(1, "execl");
  169. }
  170. static void exec_validate_cap(bool eff, bool perm, bool inh, bool ambient)
  171. {
  172. exec_other_validate_cap("./validate_cap", eff, perm, inh, ambient);
  173. }
  174. static int do_tests(int uid, const char *our_path)
  175. {
  176. bool have_outer_privilege = create_and_enter_ns(uid);
  177. int ourpath_fd = open(our_path, O_RDONLY | O_DIRECTORY);
  178. if (ourpath_fd == -1)
  179. err(1, "open '%s'", our_path);
  180. chdir_to_tmpfs();
  181. copy_fromat_to(ourpath_fd, "validate_cap", "validate_cap");
  182. if (have_outer_privilege) {
  183. uid_t gid = getegid();
  184. copy_fromat_to(ourpath_fd, "validate_cap",
  185. "validate_cap_suidroot");
  186. if (chown("validate_cap_suidroot", 0, -1) != 0)
  187. err(1, "chown");
  188. if (chmod("validate_cap_suidroot", S_ISUID | 0700) != 0)
  189. err(1, "chmod");
  190. copy_fromat_to(ourpath_fd, "validate_cap",
  191. "validate_cap_suidnonroot");
  192. if (chown("validate_cap_suidnonroot", uid + 1, -1) != 0)
  193. err(1, "chown");
  194. if (chmod("validate_cap_suidnonroot", S_ISUID | 0700) != 0)
  195. err(1, "chmod");
  196. copy_fromat_to(ourpath_fd, "validate_cap",
  197. "validate_cap_sgidroot");
  198. if (chown("validate_cap_sgidroot", -1, 0) != 0)
  199. err(1, "chown");
  200. if (chmod("validate_cap_sgidroot", S_ISGID | 0710) != 0)
  201. err(1, "chmod");
  202. copy_fromat_to(ourpath_fd, "validate_cap",
  203. "validate_cap_sgidnonroot");
  204. if (chown("validate_cap_sgidnonroot", -1, gid + 1) != 0)
  205. err(1, "chown");
  206. if (chmod("validate_cap_sgidnonroot", S_ISGID | 0710) != 0)
  207. err(1, "chmod");
  208. }
  209. capng_get_caps_process();
  210. /* Make sure that i starts out clear */
  211. capng_update(CAPNG_DROP, CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE);
  212. if (capng_apply(CAPNG_SELECT_CAPS) != 0)
  213. err(1, "capng_apply");
  214. if (uid == 0) {
  215. printf("[RUN]\tRoot => ep\n");
  216. if (fork_wait())
  217. exec_validate_cap(true, true, false, false);
  218. } else {
  219. printf("[RUN]\tNon-root => no caps\n");
  220. if (fork_wait())
  221. exec_validate_cap(false, false, false, false);
  222. }
  223. printf("[OK]\tCheck cap_ambient manipulation rules\n");
  224. /* We should not be able to add ambient caps yet. */
  225. if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_BIND_SERVICE, 0, 0, 0) != -1 || errno != EPERM) {
  226. if (errno == EINVAL)
  227. printf("[FAIL]\tPR_CAP_AMBIENT_RAISE isn't supported\n");
  228. else
  229. printf("[FAIL]\tPR_CAP_AMBIENT_RAISE should have failed eith EPERM on a non-inheritable cap\n");
  230. return 1;
  231. }
  232. printf("[OK]\tPR_CAP_AMBIENT_RAISE failed on non-inheritable cap\n");
  233. capng_update(CAPNG_ADD, CAPNG_INHERITABLE, CAP_NET_RAW);
  234. capng_update(CAPNG_DROP, CAPNG_PERMITTED, CAP_NET_RAW);
  235. capng_update(CAPNG_DROP, CAPNG_EFFECTIVE, CAP_NET_RAW);
  236. if (capng_apply(CAPNG_SELECT_CAPS) != 0)
  237. err(1, "capng_apply");
  238. if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_RAW, 0, 0, 0) != -1 || errno != EPERM) {
  239. printf("[FAIL]\tPR_CAP_AMBIENT_RAISE should have failed on a non-permitted cap\n");
  240. return 1;
  241. }
  242. printf("[OK]\tPR_CAP_AMBIENT_RAISE failed on non-permitted cap\n");
  243. capng_update(CAPNG_ADD, CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE);
  244. if (capng_apply(CAPNG_SELECT_CAPS) != 0)
  245. err(1, "capng_apply");
  246. if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0) {
  247. printf("[FAIL]\tPR_CAP_AMBIENT_RAISE should have succeeded\n");
  248. return 1;
  249. }
  250. printf("[OK]\tPR_CAP_AMBIENT_RAISE worked\n");
  251. if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_NET_BIND_SERVICE, 0, 0, 0) != 1) {
  252. printf("[FAIL]\tPR_CAP_AMBIENT_IS_SET is broken\n");
  253. return 1;
  254. }
  255. if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0, 0) != 0)
  256. err(1, "PR_CAP_AMBIENT_CLEAR_ALL");
  257. if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0) {
  258. printf("[FAIL]\tPR_CAP_AMBIENT_CLEAR_ALL didn't work\n");
  259. return 1;
  260. }
  261. if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0)
  262. err(1, "PR_CAP_AMBIENT_RAISE");
  263. capng_update(CAPNG_DROP, CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE);
  264. if (capng_apply(CAPNG_SELECT_CAPS) != 0)
  265. err(1, "capng_apply");
  266. if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0) {
  267. printf("[FAIL]\tDropping I should have dropped A\n");
  268. return 1;
  269. }
  270. printf("[OK]\tBasic manipulation appears to work\n");
  271. capng_update(CAPNG_ADD, CAPNG_INHERITABLE, CAP_NET_BIND_SERVICE);
  272. if (capng_apply(CAPNG_SELECT_CAPS) != 0)
  273. err(1, "capng_apply");
  274. if (uid == 0) {
  275. printf("[RUN]\tRoot +i => eip\n");
  276. if (fork_wait())
  277. exec_validate_cap(true, true, true, false);
  278. } else {
  279. printf("[RUN]\tNon-root +i => i\n");
  280. if (fork_wait())
  281. exec_validate_cap(false, false, true, false);
  282. }
  283. if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, CAP_NET_BIND_SERVICE, 0, 0, 0) != 0)
  284. err(1, "PR_CAP_AMBIENT_RAISE");
  285. printf("[RUN]\tUID %d +ia => eipa\n", uid);
  286. if (fork_wait())
  287. exec_validate_cap(true, true, true, true);
  288. /* The remaining tests need real privilege */
  289. if (!have_outer_privilege) {
  290. printf("[SKIP]\tSUID/SGID tests (needs privilege)\n");
  291. goto done;
  292. }
  293. if (uid == 0) {
  294. printf("[RUN]\tRoot +ia, suidroot => eipa\n");
  295. if (fork_wait())
  296. exec_other_validate_cap("./validate_cap_suidroot",
  297. true, true, true, true);
  298. printf("[RUN]\tRoot +ia, suidnonroot => ip\n");
  299. if (fork_wait())
  300. exec_other_validate_cap("./validate_cap_suidnonroot",
  301. false, true, true, false);
  302. printf("[RUN]\tRoot +ia, sgidroot => eipa\n");
  303. if (fork_wait())
  304. exec_other_validate_cap("./validate_cap_sgidroot",
  305. true, true, true, true);
  306. if (fork_wait()) {
  307. printf("[RUN]\tRoot, gid != 0, +ia, sgidroot => eip\n");
  308. if (setresgid(1, 1, 1) != 0)
  309. err(1, "setresgid");
  310. exec_other_validate_cap("./validate_cap_sgidroot",
  311. true, true, true, false);
  312. }
  313. printf("[RUN]\tRoot +ia, sgidnonroot => eip\n");
  314. if (fork_wait())
  315. exec_other_validate_cap("./validate_cap_sgidnonroot",
  316. true, true, true, false);
  317. } else {
  318. printf("[RUN]\tNon-root +ia, sgidnonroot => i\n");
  319. exec_other_validate_cap("./validate_cap_sgidnonroot",
  320. false, false, true, false);
  321. if (fork_wait()) {
  322. printf("[RUN]\tNon-root +ia, sgidroot => i\n");
  323. if (setresgid(1, 1, 1) != 0)
  324. err(1, "setresgid");
  325. exec_other_validate_cap("./validate_cap_sgidroot",
  326. false, false, true, false);
  327. }
  328. }
  329. done:
  330. return nerrs ? 1 : 0;
  331. }
  332. int main(int argc, char **argv)
  333. {
  334. char *tmp1, *tmp2, *our_path;
  335. /* Find our path */
  336. tmp1 = strdup(argv[0]);
  337. if (!tmp1)
  338. err(1, "strdup");
  339. tmp2 = dirname(tmp1);
  340. our_path = strdup(tmp2);
  341. if (!our_path)
  342. err(1, "strdup");
  343. free(tmp1);
  344. if (fork_wait()) {
  345. printf("[RUN]\t+++ Tests with uid == 0 +++\n");
  346. return do_tests(0, our_path);
  347. }
  348. if (fork_wait()) {
  349. printf("[RUN]\t+++ Tests with uid != 0 +++\n");
  350. return do_tests(1, our_path);
  351. }
  352. return nerrs ? 1 : 0;
  353. }