test_execve.c 13 KB

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