test_execve.c 13 KB

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