safesetid-test.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define _GNU_SOURCE
  3. #include <stdio.h>
  4. #include <errno.h>
  5. #include <pwd.h>
  6. #include <grp.h>
  7. #include <string.h>
  8. #include <syscall.h>
  9. #include <sys/capability.h>
  10. #include <sys/types.h>
  11. #include <sys/mount.h>
  12. #include <sys/prctl.h>
  13. #include <sys/wait.h>
  14. #include <stdlib.h>
  15. #include <unistd.h>
  16. #include <fcntl.h>
  17. #include <stdbool.h>
  18. #include <stdarg.h>
  19. /*
  20. * NOTES about this test:
  21. * - requries libcap-dev to be installed on test system
  22. * - requires securityfs to me mounted at /sys/kernel/security, e.g.:
  23. * mount -n -t securityfs -o nodev,noexec,nosuid securityfs /sys/kernel/security
  24. * - needs CONFIG_SECURITYFS and CONFIG_SAFESETID to be enabled
  25. */
  26. #ifndef CLONE_NEWUSER
  27. # define CLONE_NEWUSER 0x10000000
  28. #endif
  29. #define ROOT_UGID 0
  30. #define RESTRICTED_PARENT_UGID 1
  31. #define ALLOWED_CHILD1_UGID 2
  32. #define ALLOWED_CHILD2_UGID 3
  33. #define NO_POLICY_UGID 4
  34. #define UGID_POLICY_STRING "1:2\n1:3\n2:2\n3:3\n"
  35. char* add_uid_whitelist_policy_file = "/sys/kernel/security/safesetid/uid_allowlist_policy";
  36. char* add_gid_whitelist_policy_file = "/sys/kernel/security/safesetid/gid_allowlist_policy";
  37. static void die(char *fmt, ...)
  38. {
  39. va_list ap;
  40. va_start(ap, fmt);
  41. vfprintf(stderr, fmt, ap);
  42. va_end(ap);
  43. exit(EXIT_FAILURE);
  44. }
  45. static bool vmaybe_write_file(bool enoent_ok, char *filename, char *fmt, va_list ap)
  46. {
  47. char buf[4096];
  48. int fd;
  49. ssize_t written;
  50. int buf_len;
  51. buf_len = vsnprintf(buf, sizeof(buf), fmt, ap);
  52. if (buf_len < 0) {
  53. printf("vsnprintf failed: %s\n",
  54. strerror(errno));
  55. return false;
  56. }
  57. if (buf_len >= sizeof(buf)) {
  58. printf("vsnprintf output truncated\n");
  59. return false;
  60. }
  61. fd = open(filename, O_WRONLY);
  62. if (fd < 0) {
  63. if ((errno == ENOENT) && enoent_ok)
  64. return true;
  65. return false;
  66. }
  67. written = write(fd, buf, buf_len);
  68. if (written != buf_len) {
  69. if (written >= 0) {
  70. printf("short write to %s\n", filename);
  71. return false;
  72. } else {
  73. printf("write to %s failed: %s\n",
  74. filename, strerror(errno));
  75. return false;
  76. }
  77. }
  78. if (close(fd) != 0) {
  79. printf("close of %s failed: %s\n",
  80. filename, strerror(errno));
  81. return false;
  82. }
  83. return true;
  84. }
  85. static bool write_file(char *filename, char *fmt, ...)
  86. {
  87. va_list ap;
  88. bool ret;
  89. va_start(ap, fmt);
  90. ret = vmaybe_write_file(false, filename, fmt, ap);
  91. va_end(ap);
  92. return ret;
  93. }
  94. static void ensure_user_exists(uid_t uid)
  95. {
  96. struct passwd p;
  97. FILE *fd;
  98. char name_str[10];
  99. if (getpwuid(uid) == NULL) {
  100. memset(&p,0x00,sizeof(p));
  101. fd=fopen("/etc/passwd","a");
  102. if (fd == NULL)
  103. die("couldn't open file\n");
  104. if (fseek(fd, 0, SEEK_END))
  105. die("couldn't fseek\n");
  106. snprintf(name_str, 10, "user %d", uid);
  107. p.pw_name=name_str;
  108. p.pw_uid=uid;
  109. p.pw_gid=uid;
  110. p.pw_gecos="Test account";
  111. p.pw_dir="/dev/null";
  112. p.pw_shell="/bin/false";
  113. int value = putpwent(&p,fd);
  114. if (value != 0)
  115. die("putpwent failed\n");
  116. if (fclose(fd))
  117. die("fclose failed\n");
  118. }
  119. }
  120. static void ensure_group_exists(gid_t gid)
  121. {
  122. struct group g;
  123. FILE *fd;
  124. char name_str[10];
  125. if (getgrgid(gid) == NULL) {
  126. memset(&g,0x00,sizeof(g));
  127. fd=fopen("/etc/group","a");
  128. if (fd == NULL)
  129. die("couldn't open group file\n");
  130. if (fseek(fd, 0, SEEK_END))
  131. die("couldn't fseek group file\n");
  132. snprintf(name_str, 10, "group %d", gid);
  133. g.gr_name=name_str;
  134. g.gr_gid=gid;
  135. g.gr_passwd=NULL;
  136. g.gr_mem=NULL;
  137. int value = putgrent(&g,fd);
  138. if (value != 0)
  139. die("putgrent failed\n");
  140. if (fclose(fd))
  141. die("fclose failed\n");
  142. }
  143. }
  144. static void ensure_securityfs_mounted(void)
  145. {
  146. int fd = open(add_uid_whitelist_policy_file, O_WRONLY);
  147. if (fd < 0) {
  148. if (errno == ENOENT) {
  149. // Need to mount securityfs
  150. if (mount("securityfs", "/sys/kernel/security",
  151. "securityfs", 0, NULL) < 0)
  152. die("mounting securityfs failed\n");
  153. } else {
  154. die("couldn't find securityfs for unknown reason\n");
  155. }
  156. } else {
  157. if (close(fd) != 0) {
  158. die("close of %s failed: %s\n",
  159. add_uid_whitelist_policy_file, strerror(errno));
  160. }
  161. }
  162. }
  163. static void write_uid_policies()
  164. {
  165. static char *policy_str = UGID_POLICY_STRING;
  166. ssize_t written;
  167. int fd;
  168. fd = open(add_uid_whitelist_policy_file, O_WRONLY);
  169. if (fd < 0)
  170. die("can't open add_uid_whitelist_policy file\n");
  171. written = write(fd, policy_str, strlen(policy_str));
  172. if (written != strlen(policy_str)) {
  173. if (written >= 0) {
  174. die("short write to %s\n", add_uid_whitelist_policy_file);
  175. } else {
  176. die("write to %s failed: %s\n",
  177. add_uid_whitelist_policy_file, strerror(errno));
  178. }
  179. }
  180. if (close(fd) != 0) {
  181. die("close of %s failed: %s\n",
  182. add_uid_whitelist_policy_file, strerror(errno));
  183. }
  184. }
  185. static void write_gid_policies()
  186. {
  187. static char *policy_str = UGID_POLICY_STRING;
  188. ssize_t written;
  189. int fd;
  190. fd = open(add_gid_whitelist_policy_file, O_WRONLY);
  191. if (fd < 0)
  192. die("can't open add_gid_whitelist_policy file\n");
  193. written = write(fd, policy_str, strlen(policy_str));
  194. if (written != strlen(policy_str)) {
  195. if (written >= 0) {
  196. die("short write to %s\n", add_gid_whitelist_policy_file);
  197. } else {
  198. die("write to %s failed: %s\n",
  199. add_gid_whitelist_policy_file, strerror(errno));
  200. }
  201. }
  202. if (close(fd) != 0) {
  203. die("close of %s failed: %s\n",
  204. add_gid_whitelist_policy_file, strerror(errno));
  205. }
  206. }
  207. static bool test_userns(bool expect_success)
  208. {
  209. uid_t uid;
  210. char map_file_name[32];
  211. size_t sz = sizeof(map_file_name);
  212. pid_t cpid;
  213. bool success;
  214. uid = getuid();
  215. int clone_flags = CLONE_NEWUSER;
  216. cpid = syscall(SYS_clone, clone_flags, NULL);
  217. if (cpid == -1) {
  218. printf("clone failed");
  219. return false;
  220. }
  221. if (cpid == 0) { /* Code executed by child */
  222. // Give parent 1 second to write map file
  223. sleep(1);
  224. exit(EXIT_SUCCESS);
  225. } else { /* Code executed by parent */
  226. if(snprintf(map_file_name, sz, "/proc/%d/uid_map", cpid) < 0) {
  227. printf("preparing file name string failed");
  228. return false;
  229. }
  230. success = write_file(map_file_name, "0 %d 1", uid);
  231. return success == expect_success;
  232. }
  233. printf("should not reach here");
  234. return false;
  235. }
  236. static void test_setuid(uid_t child_uid, bool expect_success)
  237. {
  238. pid_t cpid, w;
  239. int wstatus;
  240. cpid = fork();
  241. if (cpid == -1) {
  242. die("fork\n");
  243. }
  244. if (cpid == 0) { /* Code executed by child */
  245. if (setuid(child_uid) < 0)
  246. exit(EXIT_FAILURE);
  247. if (getuid() == child_uid)
  248. exit(EXIT_SUCCESS);
  249. else
  250. exit(EXIT_FAILURE);
  251. } else { /* Code executed by parent */
  252. do {
  253. w = waitpid(cpid, &wstatus, WUNTRACED | WCONTINUED);
  254. if (w == -1) {
  255. die("waitpid\n");
  256. }
  257. if (WIFEXITED(wstatus)) {
  258. if (WEXITSTATUS(wstatus) == EXIT_SUCCESS) {
  259. if (expect_success) {
  260. return;
  261. } else {
  262. die("unexpected success\n");
  263. }
  264. } else {
  265. if (expect_success) {
  266. die("unexpected failure\n");
  267. } else {
  268. return;
  269. }
  270. }
  271. } else if (WIFSIGNALED(wstatus)) {
  272. if (WTERMSIG(wstatus) == 9) {
  273. if (expect_success)
  274. die("killed unexpectedly\n");
  275. else
  276. return;
  277. } else {
  278. die("unexpected signal: %d\n", wstatus);
  279. }
  280. } else {
  281. die("unexpected status: %d\n", wstatus);
  282. }
  283. } while (!WIFEXITED(wstatus) && !WIFSIGNALED(wstatus));
  284. }
  285. die("should not reach here\n");
  286. }
  287. static void test_setgid(gid_t child_gid, bool expect_success)
  288. {
  289. pid_t cpid, w;
  290. int wstatus;
  291. cpid = fork();
  292. if (cpid == -1) {
  293. die("fork\n");
  294. }
  295. if (cpid == 0) { /* Code executed by child */
  296. if (setgid(child_gid) < 0)
  297. exit(EXIT_FAILURE);
  298. if (getgid() == child_gid)
  299. exit(EXIT_SUCCESS);
  300. else
  301. exit(EXIT_FAILURE);
  302. } else { /* Code executed by parent */
  303. do {
  304. w = waitpid(cpid, &wstatus, WUNTRACED | WCONTINUED);
  305. if (w == -1) {
  306. die("waitpid\n");
  307. }
  308. if (WIFEXITED(wstatus)) {
  309. if (WEXITSTATUS(wstatus) == EXIT_SUCCESS) {
  310. if (expect_success) {
  311. return;
  312. } else {
  313. die("unexpected success\n");
  314. }
  315. } else {
  316. if (expect_success) {
  317. die("unexpected failure\n");
  318. } else {
  319. return;
  320. }
  321. }
  322. } else if (WIFSIGNALED(wstatus)) {
  323. if (WTERMSIG(wstatus) == 9) {
  324. if (expect_success)
  325. die("killed unexpectedly\n");
  326. else
  327. return;
  328. } else {
  329. die("unexpected signal: %d\n", wstatus);
  330. }
  331. } else {
  332. die("unexpected status: %d\n", wstatus);
  333. }
  334. } while (!WIFEXITED(wstatus) && !WIFSIGNALED(wstatus));
  335. }
  336. die("should not reach here\n");
  337. }
  338. static void test_setgroups(gid_t* child_groups, size_t len, bool expect_success)
  339. {
  340. pid_t cpid, w;
  341. int wstatus;
  342. gid_t groupset[len];
  343. int i, j;
  344. cpid = fork();
  345. if (cpid == -1) {
  346. die("fork\n");
  347. }
  348. if (cpid == 0) { /* Code executed by child */
  349. if (setgroups(len, child_groups) != 0)
  350. exit(EXIT_FAILURE);
  351. if (getgroups(len, groupset) != len)
  352. exit(EXIT_FAILURE);
  353. for (i = 0; i < len; i++) {
  354. for (j = 0; j < len; j++) {
  355. if (child_groups[i] == groupset[j])
  356. break;
  357. if (j == len - 1)
  358. exit(EXIT_FAILURE);
  359. }
  360. }
  361. exit(EXIT_SUCCESS);
  362. } else { /* Code executed by parent */
  363. do {
  364. w = waitpid(cpid, &wstatus, WUNTRACED | WCONTINUED);
  365. if (w == -1) {
  366. die("waitpid\n");
  367. }
  368. if (WIFEXITED(wstatus)) {
  369. if (WEXITSTATUS(wstatus) == EXIT_SUCCESS) {
  370. if (expect_success) {
  371. return;
  372. } else {
  373. die("unexpected success\n");
  374. }
  375. } else {
  376. if (expect_success) {
  377. die("unexpected failure\n");
  378. } else {
  379. return;
  380. }
  381. }
  382. } else if (WIFSIGNALED(wstatus)) {
  383. if (WTERMSIG(wstatus) == 9) {
  384. if (expect_success)
  385. die("killed unexpectedly\n");
  386. else
  387. return;
  388. } else {
  389. die("unexpected signal: %d\n", wstatus);
  390. }
  391. } else {
  392. die("unexpected status: %d\n", wstatus);
  393. }
  394. } while (!WIFEXITED(wstatus) && !WIFSIGNALED(wstatus));
  395. }
  396. die("should not reach here\n");
  397. }
  398. static void ensure_users_exist(void)
  399. {
  400. ensure_user_exists(ROOT_UGID);
  401. ensure_user_exists(RESTRICTED_PARENT_UGID);
  402. ensure_user_exists(ALLOWED_CHILD1_UGID);
  403. ensure_user_exists(ALLOWED_CHILD2_UGID);
  404. ensure_user_exists(NO_POLICY_UGID);
  405. }
  406. static void ensure_groups_exist(void)
  407. {
  408. ensure_group_exists(ROOT_UGID);
  409. ensure_group_exists(RESTRICTED_PARENT_UGID);
  410. ensure_group_exists(ALLOWED_CHILD1_UGID);
  411. ensure_group_exists(ALLOWED_CHILD2_UGID);
  412. ensure_group_exists(NO_POLICY_UGID);
  413. }
  414. static void drop_caps(bool setid_retained)
  415. {
  416. cap_value_t cap_values[] = {CAP_SETUID, CAP_SETGID};
  417. cap_t caps;
  418. caps = cap_get_proc();
  419. if (setid_retained)
  420. cap_set_flag(caps, CAP_EFFECTIVE, 2, cap_values, CAP_SET);
  421. else
  422. cap_clear(caps);
  423. cap_set_proc(caps);
  424. cap_free(caps);
  425. }
  426. int main(int argc, char **argv)
  427. {
  428. ensure_groups_exist();
  429. ensure_users_exist();
  430. ensure_securityfs_mounted();
  431. write_uid_policies();
  432. write_gid_policies();
  433. if (prctl(PR_SET_KEEPCAPS, 1L))
  434. die("Error with set keepcaps\n");
  435. // First test to make sure we can write userns mappings from a non-root
  436. // user that doesn't have any restrictions (as long as it has
  437. // CAP_SETUID);
  438. if (setgid(NO_POLICY_UGID) < 0)
  439. die("Error with set gid(%d)\n", NO_POLICY_UGID);
  440. if (setuid(NO_POLICY_UGID) < 0)
  441. die("Error with set uid(%d)\n", NO_POLICY_UGID);
  442. // Take away all but setid caps
  443. drop_caps(true);
  444. // Need PR_SET_DUMPABLE flag set so we can write /proc/[pid]/uid_map
  445. // from non-root parent process.
  446. if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0))
  447. die("Error with set dumpable\n");
  448. if (!test_userns(true)) {
  449. die("test_userns failed when it should work\n");
  450. }
  451. // Now switch to a user/group with restrictions
  452. if (setgid(RESTRICTED_PARENT_UGID) < 0)
  453. die("Error with set gid(%d)\n", RESTRICTED_PARENT_UGID);
  454. if (setuid(RESTRICTED_PARENT_UGID) < 0)
  455. die("Error with set uid(%d)\n", RESTRICTED_PARENT_UGID);
  456. test_setuid(ROOT_UGID, false);
  457. test_setuid(ALLOWED_CHILD1_UGID, true);
  458. test_setuid(ALLOWED_CHILD2_UGID, true);
  459. test_setuid(NO_POLICY_UGID, false);
  460. test_setgid(ROOT_UGID, false);
  461. test_setgid(ALLOWED_CHILD1_UGID, true);
  462. test_setgid(ALLOWED_CHILD2_UGID, true);
  463. test_setgid(NO_POLICY_UGID, false);
  464. gid_t allowed_supp_groups[2] = {ALLOWED_CHILD1_UGID, ALLOWED_CHILD2_UGID};
  465. gid_t disallowed_supp_groups[2] = {ROOT_UGID, NO_POLICY_UGID};
  466. test_setgroups(allowed_supp_groups, 2, true);
  467. test_setgroups(disallowed_supp_groups, 2, false);
  468. if (!test_userns(false)) {
  469. die("test_userns worked when it should fail\n");
  470. }
  471. // Now take away all caps
  472. drop_caps(false);
  473. test_setuid(2, false);
  474. test_setuid(3, false);
  475. test_setuid(4, false);
  476. test_setgid(2, false);
  477. test_setgid(3, false);
  478. test_setgid(4, false);
  479. // NOTE: this test doesn't clean up users that were created in
  480. // /etc/passwd or flush policies that were added to the LSM.
  481. printf("test successful!\n");
  482. return EXIT_SUCCESS;
  483. }