memfd_test.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define _GNU_SOURCE
  3. #define __EXPORTED_HEADERS__
  4. #include <errno.h>
  5. #include <inttypes.h>
  6. #include <limits.h>
  7. #include <linux/falloc.h>
  8. #include <fcntl.h>
  9. #include <linux/memfd.h>
  10. #include <sched.h>
  11. #include <stdbool.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <signal.h>
  15. #include <string.h>
  16. #include <sys/mman.h>
  17. #include <sys/stat.h>
  18. #include <sys/syscall.h>
  19. #include <sys/wait.h>
  20. #include <unistd.h>
  21. #include <ctype.h>
  22. #include "common.h"
  23. #define MEMFD_STR "memfd:"
  24. #define MEMFD_HUGE_STR "memfd-hugetlb:"
  25. #define SHARED_FT_STR "(shared file-table)"
  26. #define MFD_DEF_SIZE 8192
  27. #define STACK_SIZE 65536
  28. #define F_SEAL_EXEC 0x0020
  29. #define F_WX_SEALS (F_SEAL_SHRINK | \
  30. F_SEAL_GROW | \
  31. F_SEAL_WRITE | \
  32. F_SEAL_FUTURE_WRITE | \
  33. F_SEAL_EXEC)
  34. #define MFD_NOEXEC_SEAL 0x0008U
  35. /*
  36. * Default is not to test hugetlbfs
  37. */
  38. static size_t mfd_def_size = MFD_DEF_SIZE;
  39. static const char *memfd_str = MEMFD_STR;
  40. static ssize_t fd2name(int fd, char *buf, size_t bufsize)
  41. {
  42. char buf1[PATH_MAX];
  43. int size;
  44. ssize_t nbytes;
  45. size = snprintf(buf1, PATH_MAX, "/proc/self/fd/%d", fd);
  46. if (size < 0) {
  47. printf("snprintf(%d) failed on %m\n", fd);
  48. abort();
  49. }
  50. /*
  51. * reserver one byte for string termination.
  52. */
  53. nbytes = readlink(buf1, buf, bufsize-1);
  54. if (nbytes == -1) {
  55. printf("readlink(%s) failed %m\n", buf1);
  56. abort();
  57. }
  58. buf[nbytes] = '\0';
  59. return nbytes;
  60. }
  61. static int mfd_assert_new(const char *name, loff_t sz, unsigned int flags)
  62. {
  63. int r, fd;
  64. fd = sys_memfd_create(name, flags);
  65. if (fd < 0) {
  66. printf("memfd_create(\"%s\", %u) failed: %m\n",
  67. name, flags);
  68. abort();
  69. }
  70. r = ftruncate(fd, sz);
  71. if (r < 0) {
  72. printf("ftruncate(%llu) failed: %m\n", (unsigned long long)sz);
  73. abort();
  74. }
  75. return fd;
  76. }
  77. static void sysctl_assert_write(const char *val)
  78. {
  79. int fd = open("/proc/sys/vm/memfd_noexec", O_WRONLY | O_CLOEXEC);
  80. if (fd < 0) {
  81. printf("open sysctl failed: %m\n");
  82. abort();
  83. }
  84. if (write(fd, val, strlen(val)) < 0) {
  85. printf("write sysctl %s failed: %m\n", val);
  86. abort();
  87. }
  88. }
  89. static void sysctl_fail_write(const char *val)
  90. {
  91. int fd = open("/proc/sys/vm/memfd_noexec", O_WRONLY | O_CLOEXEC);
  92. if (fd < 0) {
  93. printf("open sysctl failed: %m\n");
  94. abort();
  95. }
  96. if (write(fd, val, strlen(val)) >= 0) {
  97. printf("write sysctl %s succeeded, but failure expected\n",
  98. val);
  99. abort();
  100. }
  101. }
  102. static void sysctl_assert_equal(const char *val)
  103. {
  104. char *p, buf[128] = {};
  105. int fd = open("/proc/sys/vm/memfd_noexec", O_RDONLY | O_CLOEXEC);
  106. if (fd < 0) {
  107. printf("open sysctl failed: %m\n");
  108. abort();
  109. }
  110. if (read(fd, buf, sizeof(buf)) < 0) {
  111. printf("read sysctl failed: %m\n");
  112. abort();
  113. }
  114. /* Strip trailing whitespace. */
  115. p = buf;
  116. while (!isspace(*p))
  117. p++;
  118. *p = '\0';
  119. if (strcmp(buf, val) != 0) {
  120. printf("unexpected sysctl value: expected %s, got %s\n", val, buf);
  121. abort();
  122. }
  123. }
  124. static int mfd_assert_reopen_fd(int fd_in)
  125. {
  126. int fd;
  127. char path[100];
  128. sprintf(path, "/proc/self/fd/%d", fd_in);
  129. fd = open(path, O_RDWR);
  130. if (fd < 0) {
  131. printf("re-open of existing fd %d failed\n", fd_in);
  132. abort();
  133. }
  134. return fd;
  135. }
  136. static void mfd_fail_new(const char *name, unsigned int flags)
  137. {
  138. int r;
  139. r = sys_memfd_create(name, flags);
  140. if (r >= 0) {
  141. printf("memfd_create(\"%s\", %u) succeeded, but failure expected\n",
  142. name, flags);
  143. close(r);
  144. abort();
  145. }
  146. }
  147. static unsigned int mfd_assert_get_seals(int fd)
  148. {
  149. int r;
  150. r = fcntl(fd, F_GET_SEALS);
  151. if (r < 0) {
  152. printf("GET_SEALS(%d) failed: %m\n", fd);
  153. abort();
  154. }
  155. return (unsigned int)r;
  156. }
  157. static void mfd_assert_has_seals(int fd, unsigned int seals)
  158. {
  159. char buf[PATH_MAX];
  160. unsigned int s;
  161. fd2name(fd, buf, PATH_MAX);
  162. s = mfd_assert_get_seals(fd);
  163. if (s != seals) {
  164. printf("%u != %u = GET_SEALS(%s)\n", seals, s, buf);
  165. abort();
  166. }
  167. }
  168. static void mfd_assert_add_seals(int fd, unsigned int seals)
  169. {
  170. int r;
  171. unsigned int s;
  172. s = mfd_assert_get_seals(fd);
  173. r = fcntl(fd, F_ADD_SEALS, seals);
  174. if (r < 0) {
  175. printf("ADD_SEALS(%d, %u -> %u) failed: %m\n", fd, s, seals);
  176. abort();
  177. }
  178. }
  179. static void mfd_fail_add_seals(int fd, unsigned int seals)
  180. {
  181. int r;
  182. unsigned int s;
  183. r = fcntl(fd, F_GET_SEALS);
  184. if (r < 0)
  185. s = 0;
  186. else
  187. s = (unsigned int)r;
  188. r = fcntl(fd, F_ADD_SEALS, seals);
  189. if (r >= 0) {
  190. printf("ADD_SEALS(%d, %u -> %u) didn't fail as expected\n",
  191. fd, s, seals);
  192. abort();
  193. }
  194. }
  195. static void mfd_assert_size(int fd, size_t size)
  196. {
  197. struct stat st;
  198. int r;
  199. r = fstat(fd, &st);
  200. if (r < 0) {
  201. printf("fstat(%d) failed: %m\n", fd);
  202. abort();
  203. } else if (st.st_size != size) {
  204. printf("wrong file size %lld, but expected %lld\n",
  205. (long long)st.st_size, (long long)size);
  206. abort();
  207. }
  208. }
  209. static int mfd_assert_dup(int fd)
  210. {
  211. int r;
  212. r = dup(fd);
  213. if (r < 0) {
  214. printf("dup(%d) failed: %m\n", fd);
  215. abort();
  216. }
  217. return r;
  218. }
  219. static void *mfd_assert_mmap_shared(int fd)
  220. {
  221. void *p;
  222. p = mmap(NULL,
  223. mfd_def_size,
  224. PROT_READ | PROT_WRITE,
  225. MAP_SHARED,
  226. fd,
  227. 0);
  228. if (p == MAP_FAILED) {
  229. printf("mmap() failed: %m\n");
  230. abort();
  231. }
  232. return p;
  233. }
  234. static void *mfd_assert_mmap_private(int fd)
  235. {
  236. void *p;
  237. p = mmap(NULL,
  238. mfd_def_size,
  239. PROT_READ,
  240. MAP_PRIVATE,
  241. fd,
  242. 0);
  243. if (p == MAP_FAILED) {
  244. printf("mmap() failed: %m\n");
  245. abort();
  246. }
  247. return p;
  248. }
  249. static int mfd_assert_open(int fd, int flags, mode_t mode)
  250. {
  251. char buf[512];
  252. int r;
  253. sprintf(buf, "/proc/self/fd/%d", fd);
  254. r = open(buf, flags, mode);
  255. if (r < 0) {
  256. printf("open(%s) failed: %m\n", buf);
  257. abort();
  258. }
  259. return r;
  260. }
  261. static void mfd_fail_open(int fd, int flags, mode_t mode)
  262. {
  263. char buf[512];
  264. int r;
  265. sprintf(buf, "/proc/self/fd/%d", fd);
  266. r = open(buf, flags, mode);
  267. if (r >= 0) {
  268. printf("open(%s) didn't fail as expected\n", buf);
  269. abort();
  270. }
  271. }
  272. static void mfd_assert_read(int fd)
  273. {
  274. char buf[16];
  275. void *p;
  276. ssize_t l;
  277. l = read(fd, buf, sizeof(buf));
  278. if (l != sizeof(buf)) {
  279. printf("read() failed: %m\n");
  280. abort();
  281. }
  282. /* verify PROT_READ *is* allowed */
  283. p = mmap(NULL,
  284. mfd_def_size,
  285. PROT_READ,
  286. MAP_PRIVATE,
  287. fd,
  288. 0);
  289. if (p == MAP_FAILED) {
  290. printf("mmap() failed: %m\n");
  291. abort();
  292. }
  293. munmap(p, mfd_def_size);
  294. /* verify MAP_PRIVATE is *always* allowed (even writable) */
  295. p = mmap(NULL,
  296. mfd_def_size,
  297. PROT_READ | PROT_WRITE,
  298. MAP_PRIVATE,
  299. fd,
  300. 0);
  301. if (p == MAP_FAILED) {
  302. printf("mmap() failed: %m\n");
  303. abort();
  304. }
  305. munmap(p, mfd_def_size);
  306. }
  307. /* Test that PROT_READ + MAP_SHARED mappings work. */
  308. static void mfd_assert_read_shared(int fd)
  309. {
  310. void *p;
  311. /* verify PROT_READ and MAP_SHARED *is* allowed */
  312. p = mmap(NULL,
  313. mfd_def_size,
  314. PROT_READ,
  315. MAP_SHARED,
  316. fd,
  317. 0);
  318. if (p == MAP_FAILED) {
  319. printf("mmap() failed: %m\n");
  320. abort();
  321. }
  322. munmap(p, mfd_def_size);
  323. }
  324. static void mfd_assert_fork_private_write(int fd)
  325. {
  326. int *p;
  327. pid_t pid;
  328. p = mmap(NULL,
  329. mfd_def_size,
  330. PROT_READ | PROT_WRITE,
  331. MAP_PRIVATE,
  332. fd,
  333. 0);
  334. if (p == MAP_FAILED) {
  335. printf("mmap() failed: %m\n");
  336. abort();
  337. }
  338. p[0] = 22;
  339. pid = fork();
  340. if (pid == 0) {
  341. p[0] = 33;
  342. exit(0);
  343. } else {
  344. waitpid(pid, NULL, 0);
  345. if (p[0] != 22) {
  346. printf("MAP_PRIVATE copy-on-write failed: %m\n");
  347. abort();
  348. }
  349. }
  350. munmap(p, mfd_def_size);
  351. }
  352. static void mfd_assert_write(int fd)
  353. {
  354. ssize_t l;
  355. void *p;
  356. int r;
  357. /*
  358. * huegtlbfs does not support write, but we want to
  359. * verify everything else here.
  360. */
  361. if (!hugetlbfs_test) {
  362. /* verify write() succeeds */
  363. l = write(fd, "\0\0\0\0", 4);
  364. if (l != 4) {
  365. printf("write() failed: %m\n");
  366. abort();
  367. }
  368. }
  369. /* verify PROT_READ | PROT_WRITE is allowed */
  370. p = mmap(NULL,
  371. mfd_def_size,
  372. PROT_READ | PROT_WRITE,
  373. MAP_SHARED,
  374. fd,
  375. 0);
  376. if (p == MAP_FAILED) {
  377. printf("mmap() failed: %m\n");
  378. abort();
  379. }
  380. *(char *)p = 0;
  381. munmap(p, mfd_def_size);
  382. /* verify PROT_WRITE is allowed */
  383. p = mmap(NULL,
  384. mfd_def_size,
  385. PROT_WRITE,
  386. MAP_SHARED,
  387. fd,
  388. 0);
  389. if (p == MAP_FAILED) {
  390. printf("mmap() failed: %m\n");
  391. abort();
  392. }
  393. *(char *)p = 0;
  394. munmap(p, mfd_def_size);
  395. /* verify PROT_READ with MAP_SHARED is allowed and a following
  396. * mprotect(PROT_WRITE) allows writing */
  397. p = mmap(NULL,
  398. mfd_def_size,
  399. PROT_READ,
  400. MAP_SHARED,
  401. fd,
  402. 0);
  403. if (p == MAP_FAILED) {
  404. printf("mmap() failed: %m\n");
  405. abort();
  406. }
  407. r = mprotect(p, mfd_def_size, PROT_READ | PROT_WRITE);
  408. if (r < 0) {
  409. printf("mprotect() failed: %m\n");
  410. abort();
  411. }
  412. *(char *)p = 0;
  413. munmap(p, mfd_def_size);
  414. /* verify PUNCH_HOLE works */
  415. r = fallocate(fd,
  416. FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
  417. 0,
  418. mfd_def_size);
  419. if (r < 0) {
  420. printf("fallocate(PUNCH_HOLE) failed: %m\n");
  421. abort();
  422. }
  423. }
  424. static void mfd_fail_write(int fd)
  425. {
  426. ssize_t l;
  427. void *p;
  428. int r;
  429. /* verify write() fails */
  430. l = write(fd, "data", 4);
  431. if (l != -EPERM) {
  432. printf("expected EPERM on write(), but got %d: %m\n", (int)l);
  433. abort();
  434. }
  435. /* verify PROT_READ | PROT_WRITE is not allowed */
  436. p = mmap(NULL,
  437. mfd_def_size,
  438. PROT_READ | PROT_WRITE,
  439. MAP_SHARED,
  440. fd,
  441. 0);
  442. if (p != MAP_FAILED) {
  443. printf("mmap() didn't fail as expected\n");
  444. abort();
  445. }
  446. /* verify PROT_WRITE is not allowed */
  447. p = mmap(NULL,
  448. mfd_def_size,
  449. PROT_WRITE,
  450. MAP_SHARED,
  451. fd,
  452. 0);
  453. if (p != MAP_FAILED) {
  454. printf("mmap() didn't fail as expected\n");
  455. abort();
  456. }
  457. /* Verify PROT_READ with MAP_SHARED with a following mprotect is not
  458. * allowed. Note that for r/w the kernel already prevents the mmap. */
  459. p = mmap(NULL,
  460. mfd_def_size,
  461. PROT_READ,
  462. MAP_SHARED,
  463. fd,
  464. 0);
  465. if (p != MAP_FAILED) {
  466. r = mprotect(p, mfd_def_size, PROT_READ | PROT_WRITE);
  467. if (r >= 0) {
  468. printf("mmap()+mprotect() didn't fail as expected\n");
  469. abort();
  470. }
  471. munmap(p, mfd_def_size);
  472. }
  473. /* verify PUNCH_HOLE fails */
  474. r = fallocate(fd,
  475. FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
  476. 0,
  477. mfd_def_size);
  478. if (r >= 0) {
  479. printf("fallocate(PUNCH_HOLE) didn't fail as expected\n");
  480. abort();
  481. }
  482. }
  483. static void mfd_assert_shrink(int fd)
  484. {
  485. int r, fd2;
  486. r = ftruncate(fd, mfd_def_size / 2);
  487. if (r < 0) {
  488. printf("ftruncate(SHRINK) failed: %m\n");
  489. abort();
  490. }
  491. mfd_assert_size(fd, mfd_def_size / 2);
  492. fd2 = mfd_assert_open(fd,
  493. O_RDWR | O_CREAT | O_TRUNC,
  494. S_IRUSR | S_IWUSR);
  495. close(fd2);
  496. mfd_assert_size(fd, 0);
  497. }
  498. static void mfd_fail_shrink(int fd)
  499. {
  500. int r;
  501. r = ftruncate(fd, mfd_def_size / 2);
  502. if (r >= 0) {
  503. printf("ftruncate(SHRINK) didn't fail as expected\n");
  504. abort();
  505. }
  506. mfd_fail_open(fd,
  507. O_RDWR | O_CREAT | O_TRUNC,
  508. S_IRUSR | S_IWUSR);
  509. }
  510. static void mfd_assert_grow(int fd)
  511. {
  512. int r;
  513. r = ftruncate(fd, mfd_def_size * 2);
  514. if (r < 0) {
  515. printf("ftruncate(GROW) failed: %m\n");
  516. abort();
  517. }
  518. mfd_assert_size(fd, mfd_def_size * 2);
  519. r = fallocate(fd,
  520. 0,
  521. 0,
  522. mfd_def_size * 4);
  523. if (r < 0) {
  524. printf("fallocate(ALLOC) failed: %m\n");
  525. abort();
  526. }
  527. mfd_assert_size(fd, mfd_def_size * 4);
  528. }
  529. static void mfd_fail_grow(int fd)
  530. {
  531. int r;
  532. r = ftruncate(fd, mfd_def_size * 2);
  533. if (r >= 0) {
  534. printf("ftruncate(GROW) didn't fail as expected\n");
  535. abort();
  536. }
  537. r = fallocate(fd,
  538. 0,
  539. 0,
  540. mfd_def_size * 4);
  541. if (r >= 0) {
  542. printf("fallocate(ALLOC) didn't fail as expected\n");
  543. abort();
  544. }
  545. }
  546. static void mfd_assert_grow_write(int fd)
  547. {
  548. static char *buf;
  549. ssize_t l;
  550. /* hugetlbfs does not support write */
  551. if (hugetlbfs_test)
  552. return;
  553. buf = malloc(mfd_def_size * 8);
  554. if (!buf) {
  555. printf("malloc(%zu) failed: %m\n", mfd_def_size * 8);
  556. abort();
  557. }
  558. l = pwrite(fd, buf, mfd_def_size * 8, 0);
  559. if (l != (mfd_def_size * 8)) {
  560. printf("pwrite() failed: %m\n");
  561. abort();
  562. }
  563. mfd_assert_size(fd, mfd_def_size * 8);
  564. }
  565. static void mfd_fail_grow_write(int fd)
  566. {
  567. static char *buf;
  568. ssize_t l;
  569. /* hugetlbfs does not support write */
  570. if (hugetlbfs_test)
  571. return;
  572. buf = malloc(mfd_def_size * 8);
  573. if (!buf) {
  574. printf("malloc(%zu) failed: %m\n", mfd_def_size * 8);
  575. abort();
  576. }
  577. l = pwrite(fd, buf, mfd_def_size * 8, 0);
  578. if (l == (mfd_def_size * 8)) {
  579. printf("pwrite() didn't fail as expected\n");
  580. abort();
  581. }
  582. }
  583. static void mfd_assert_mode(int fd, int mode)
  584. {
  585. struct stat st;
  586. char buf[PATH_MAX];
  587. fd2name(fd, buf, PATH_MAX);
  588. if (fstat(fd, &st) < 0) {
  589. printf("fstat(%s) failed: %m\n", buf);
  590. abort();
  591. }
  592. if ((st.st_mode & 07777) != mode) {
  593. printf("fstat(%s) wrong file mode 0%04o, but expected 0%04o\n",
  594. buf, (int)st.st_mode & 07777, mode);
  595. abort();
  596. }
  597. }
  598. static void mfd_assert_chmod(int fd, int mode)
  599. {
  600. char buf[PATH_MAX];
  601. fd2name(fd, buf, PATH_MAX);
  602. if (fchmod(fd, mode) < 0) {
  603. printf("fchmod(%s, 0%04o) failed: %m\n", buf, mode);
  604. abort();
  605. }
  606. mfd_assert_mode(fd, mode);
  607. }
  608. static void mfd_fail_chmod(int fd, int mode)
  609. {
  610. struct stat st;
  611. char buf[PATH_MAX];
  612. fd2name(fd, buf, PATH_MAX);
  613. if (fstat(fd, &st) < 0) {
  614. printf("fstat(%s) failed: %m\n", buf);
  615. abort();
  616. }
  617. if (fchmod(fd, mode) == 0) {
  618. printf("fchmod(%s, 0%04o) didn't fail as expected\n",
  619. buf, mode);
  620. abort();
  621. }
  622. /* verify that file mode bits did not change */
  623. mfd_assert_mode(fd, st.st_mode & 07777);
  624. }
  625. static int idle_thread_fn(void *arg)
  626. {
  627. sigset_t set;
  628. int sig;
  629. /* dummy waiter; SIGTERM terminates us anyway */
  630. sigemptyset(&set);
  631. sigaddset(&set, SIGTERM);
  632. sigwait(&set, &sig);
  633. return 0;
  634. }
  635. static pid_t spawn_thread(unsigned int flags, int (*fn)(void *), void *arg)
  636. {
  637. uint8_t *stack;
  638. pid_t pid;
  639. stack = malloc(STACK_SIZE);
  640. if (!stack) {
  641. printf("malloc(STACK_SIZE) failed: %m\n");
  642. abort();
  643. }
  644. pid = clone(fn, stack + STACK_SIZE, SIGCHLD | flags, arg);
  645. if (pid < 0) {
  646. printf("clone() failed: %m\n");
  647. abort();
  648. }
  649. return pid;
  650. }
  651. static void join_thread(pid_t pid)
  652. {
  653. int wstatus;
  654. if (waitpid(pid, &wstatus, 0) < 0) {
  655. printf("newpid thread: waitpid() failed: %m\n");
  656. abort();
  657. }
  658. if (WIFEXITED(wstatus) && WEXITSTATUS(wstatus) != 0) {
  659. printf("newpid thread: exited with non-zero error code %d\n",
  660. WEXITSTATUS(wstatus));
  661. abort();
  662. }
  663. if (WIFSIGNALED(wstatus)) {
  664. printf("newpid thread: killed by signal %d\n",
  665. WTERMSIG(wstatus));
  666. abort();
  667. }
  668. }
  669. static pid_t spawn_idle_thread(unsigned int flags)
  670. {
  671. return spawn_thread(flags, idle_thread_fn, NULL);
  672. }
  673. static void join_idle_thread(pid_t pid)
  674. {
  675. kill(pid, SIGTERM);
  676. waitpid(pid, NULL, 0);
  677. }
  678. /*
  679. * Test memfd_create() syscall
  680. * Verify syscall-argument validation, including name checks, flag validation
  681. * and more.
  682. */
  683. static void test_create(void)
  684. {
  685. char buf[2048];
  686. int fd;
  687. printf("%s CREATE\n", memfd_str);
  688. /* test NULL name */
  689. mfd_fail_new(NULL, 0);
  690. /* test over-long name (not zero-terminated) */
  691. memset(buf, 0xff, sizeof(buf));
  692. mfd_fail_new(buf, 0);
  693. /* test over-long zero-terminated name */
  694. memset(buf, 0xff, sizeof(buf));
  695. buf[sizeof(buf) - 1] = 0;
  696. mfd_fail_new(buf, 0);
  697. /* verify "" is a valid name */
  698. fd = mfd_assert_new("", 0, 0);
  699. close(fd);
  700. /* verify invalid O_* open flags */
  701. mfd_fail_new("", 0x0100);
  702. mfd_fail_new("", ~MFD_CLOEXEC);
  703. mfd_fail_new("", ~MFD_ALLOW_SEALING);
  704. mfd_fail_new("", ~0);
  705. mfd_fail_new("", 0x80000000U);
  706. /* verify EXEC and NOEXEC_SEAL can't both be set */
  707. mfd_fail_new("", MFD_EXEC | MFD_NOEXEC_SEAL);
  708. /* verify MFD_CLOEXEC is allowed */
  709. fd = mfd_assert_new("", 0, MFD_CLOEXEC);
  710. close(fd);
  711. /* verify MFD_ALLOW_SEALING is allowed */
  712. fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING);
  713. close(fd);
  714. /* verify MFD_ALLOW_SEALING | MFD_CLOEXEC is allowed */
  715. fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING | MFD_CLOEXEC);
  716. close(fd);
  717. }
  718. /*
  719. * Test basic sealing
  720. * A very basic sealing test to see whether setting/retrieving seals works.
  721. */
  722. static void test_basic(void)
  723. {
  724. int fd;
  725. printf("%s BASIC\n", memfd_str);
  726. fd = mfd_assert_new("kern_memfd_basic",
  727. mfd_def_size,
  728. MFD_CLOEXEC | MFD_ALLOW_SEALING);
  729. /* add basic seals */
  730. mfd_assert_has_seals(fd, 0);
  731. mfd_assert_add_seals(fd, F_SEAL_SHRINK |
  732. F_SEAL_WRITE);
  733. mfd_assert_has_seals(fd, F_SEAL_SHRINK |
  734. F_SEAL_WRITE);
  735. /* add them again */
  736. mfd_assert_add_seals(fd, F_SEAL_SHRINK |
  737. F_SEAL_WRITE);
  738. mfd_assert_has_seals(fd, F_SEAL_SHRINK |
  739. F_SEAL_WRITE);
  740. /* add more seals and seal against sealing */
  741. mfd_assert_add_seals(fd, F_SEAL_GROW | F_SEAL_SEAL);
  742. mfd_assert_has_seals(fd, F_SEAL_SHRINK |
  743. F_SEAL_GROW |
  744. F_SEAL_WRITE |
  745. F_SEAL_SEAL);
  746. /* verify that sealing no longer works */
  747. mfd_fail_add_seals(fd, F_SEAL_GROW);
  748. mfd_fail_add_seals(fd, 0);
  749. close(fd);
  750. /* verify sealing does not work without MFD_ALLOW_SEALING */
  751. fd = mfd_assert_new("kern_memfd_basic",
  752. mfd_def_size,
  753. MFD_CLOEXEC);
  754. mfd_assert_has_seals(fd, F_SEAL_SEAL);
  755. mfd_fail_add_seals(fd, F_SEAL_SHRINK |
  756. F_SEAL_GROW |
  757. F_SEAL_WRITE);
  758. mfd_assert_has_seals(fd, F_SEAL_SEAL);
  759. close(fd);
  760. }
  761. /*
  762. * Test SEAL_WRITE
  763. * Test whether SEAL_WRITE actually prevents modifications.
  764. */
  765. static void test_seal_write(void)
  766. {
  767. int fd;
  768. printf("%s SEAL-WRITE\n", memfd_str);
  769. fd = mfd_assert_new("kern_memfd_seal_write",
  770. mfd_def_size,
  771. MFD_CLOEXEC | MFD_ALLOW_SEALING);
  772. mfd_assert_has_seals(fd, 0);
  773. mfd_assert_add_seals(fd, F_SEAL_WRITE);
  774. mfd_assert_has_seals(fd, F_SEAL_WRITE);
  775. mfd_assert_read(fd);
  776. mfd_fail_write(fd);
  777. mfd_assert_shrink(fd);
  778. mfd_assert_grow(fd);
  779. mfd_fail_grow_write(fd);
  780. close(fd);
  781. }
  782. /*
  783. * Test SEAL_FUTURE_WRITE
  784. * Test whether SEAL_FUTURE_WRITE actually prevents modifications.
  785. */
  786. static void test_seal_future_write(void)
  787. {
  788. int fd, fd2;
  789. void *p;
  790. printf("%s SEAL-FUTURE-WRITE\n", memfd_str);
  791. fd = mfd_assert_new("kern_memfd_seal_future_write",
  792. mfd_def_size,
  793. MFD_CLOEXEC | MFD_ALLOW_SEALING);
  794. p = mfd_assert_mmap_shared(fd);
  795. mfd_assert_has_seals(fd, 0);
  796. mfd_assert_add_seals(fd, F_SEAL_FUTURE_WRITE);
  797. mfd_assert_has_seals(fd, F_SEAL_FUTURE_WRITE);
  798. /* read should pass, writes should fail */
  799. mfd_assert_read(fd);
  800. mfd_assert_read_shared(fd);
  801. mfd_fail_write(fd);
  802. fd2 = mfd_assert_reopen_fd(fd);
  803. /* read should pass, writes should still fail */
  804. mfd_assert_read(fd2);
  805. mfd_assert_read_shared(fd2);
  806. mfd_fail_write(fd2);
  807. mfd_assert_fork_private_write(fd);
  808. munmap(p, mfd_def_size);
  809. close(fd2);
  810. close(fd);
  811. }
  812. /*
  813. * Test SEAL_SHRINK
  814. * Test whether SEAL_SHRINK actually prevents shrinking
  815. */
  816. static void test_seal_shrink(void)
  817. {
  818. int fd;
  819. printf("%s SEAL-SHRINK\n", memfd_str);
  820. fd = mfd_assert_new("kern_memfd_seal_shrink",
  821. mfd_def_size,
  822. MFD_CLOEXEC | MFD_ALLOW_SEALING);
  823. mfd_assert_has_seals(fd, 0);
  824. mfd_assert_add_seals(fd, F_SEAL_SHRINK);
  825. mfd_assert_has_seals(fd, F_SEAL_SHRINK);
  826. mfd_assert_read(fd);
  827. mfd_assert_write(fd);
  828. mfd_fail_shrink(fd);
  829. mfd_assert_grow(fd);
  830. mfd_assert_grow_write(fd);
  831. close(fd);
  832. }
  833. /*
  834. * Test SEAL_GROW
  835. * Test whether SEAL_GROW actually prevents growing
  836. */
  837. static void test_seal_grow(void)
  838. {
  839. int fd;
  840. printf("%s SEAL-GROW\n", memfd_str);
  841. fd = mfd_assert_new("kern_memfd_seal_grow",
  842. mfd_def_size,
  843. MFD_CLOEXEC | MFD_ALLOW_SEALING);
  844. mfd_assert_has_seals(fd, 0);
  845. mfd_assert_add_seals(fd, F_SEAL_GROW);
  846. mfd_assert_has_seals(fd, F_SEAL_GROW);
  847. mfd_assert_read(fd);
  848. mfd_assert_write(fd);
  849. mfd_assert_shrink(fd);
  850. mfd_fail_grow(fd);
  851. mfd_fail_grow_write(fd);
  852. close(fd);
  853. }
  854. /*
  855. * Test SEAL_SHRINK | SEAL_GROW
  856. * Test whether SEAL_SHRINK | SEAL_GROW actually prevents resizing
  857. */
  858. static void test_seal_resize(void)
  859. {
  860. int fd;
  861. printf("%s SEAL-RESIZE\n", memfd_str);
  862. fd = mfd_assert_new("kern_memfd_seal_resize",
  863. mfd_def_size,
  864. MFD_CLOEXEC | MFD_ALLOW_SEALING);
  865. mfd_assert_has_seals(fd, 0);
  866. mfd_assert_add_seals(fd, F_SEAL_SHRINK | F_SEAL_GROW);
  867. mfd_assert_has_seals(fd, F_SEAL_SHRINK | F_SEAL_GROW);
  868. mfd_assert_read(fd);
  869. mfd_assert_write(fd);
  870. mfd_fail_shrink(fd);
  871. mfd_fail_grow(fd);
  872. mfd_fail_grow_write(fd);
  873. close(fd);
  874. }
  875. /*
  876. * Test SEAL_EXEC
  877. * Test fd is created with exec and allow sealing.
  878. * chmod() cannot change x bits after sealing.
  879. */
  880. static void test_exec_seal(void)
  881. {
  882. int fd;
  883. printf("%s SEAL-EXEC\n", memfd_str);
  884. printf("%s Apply SEAL_EXEC\n", memfd_str);
  885. fd = mfd_assert_new("kern_memfd_seal_exec",
  886. mfd_def_size,
  887. MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_EXEC);
  888. mfd_assert_mode(fd, 0777);
  889. mfd_assert_chmod(fd, 0644);
  890. mfd_assert_has_seals(fd, 0);
  891. mfd_assert_add_seals(fd, F_SEAL_EXEC);
  892. mfd_assert_has_seals(fd, F_SEAL_EXEC);
  893. mfd_assert_chmod(fd, 0600);
  894. mfd_fail_chmod(fd, 0777);
  895. mfd_fail_chmod(fd, 0670);
  896. mfd_fail_chmod(fd, 0605);
  897. mfd_fail_chmod(fd, 0700);
  898. mfd_fail_chmod(fd, 0100);
  899. mfd_assert_chmod(fd, 0666);
  900. mfd_assert_write(fd);
  901. close(fd);
  902. printf("%s Apply ALL_SEALS\n", memfd_str);
  903. fd = mfd_assert_new("kern_memfd_seal_exec",
  904. mfd_def_size,
  905. MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_EXEC);
  906. mfd_assert_mode(fd, 0777);
  907. mfd_assert_chmod(fd, 0700);
  908. mfd_assert_has_seals(fd, 0);
  909. mfd_assert_add_seals(fd, F_SEAL_EXEC);
  910. mfd_assert_has_seals(fd, F_WX_SEALS);
  911. mfd_fail_chmod(fd, 0711);
  912. mfd_fail_chmod(fd, 0600);
  913. mfd_fail_write(fd);
  914. close(fd);
  915. }
  916. /*
  917. * Test EXEC_NO_SEAL
  918. * Test fd is created with exec and not allow sealing.
  919. */
  920. static void test_exec_no_seal(void)
  921. {
  922. int fd;
  923. printf("%s EXEC_NO_SEAL\n", memfd_str);
  924. /* Create with EXEC but without ALLOW_SEALING */
  925. fd = mfd_assert_new("kern_memfd_exec_no_sealing",
  926. mfd_def_size,
  927. MFD_CLOEXEC | MFD_EXEC);
  928. mfd_assert_mode(fd, 0777);
  929. mfd_assert_has_seals(fd, F_SEAL_SEAL);
  930. mfd_assert_chmod(fd, 0666);
  931. close(fd);
  932. }
  933. /*
  934. * Test memfd_create with MFD_NOEXEC flag
  935. */
  936. static void test_noexec_seal(void)
  937. {
  938. int fd;
  939. printf("%s NOEXEC_SEAL\n", memfd_str);
  940. /* Create with NOEXEC and ALLOW_SEALING */
  941. fd = mfd_assert_new("kern_memfd_noexec",
  942. mfd_def_size,
  943. MFD_CLOEXEC | MFD_ALLOW_SEALING | MFD_NOEXEC_SEAL);
  944. mfd_assert_mode(fd, 0666);
  945. mfd_assert_has_seals(fd, F_SEAL_EXEC);
  946. mfd_fail_chmod(fd, 0777);
  947. close(fd);
  948. /* Create with NOEXEC but without ALLOW_SEALING */
  949. fd = mfd_assert_new("kern_memfd_noexec",
  950. mfd_def_size,
  951. MFD_CLOEXEC | MFD_NOEXEC_SEAL);
  952. mfd_assert_mode(fd, 0666);
  953. mfd_assert_has_seals(fd, F_SEAL_EXEC);
  954. mfd_fail_chmod(fd, 0777);
  955. close(fd);
  956. }
  957. static void test_sysctl_sysctl0(void)
  958. {
  959. int fd;
  960. sysctl_assert_equal("0");
  961. fd = mfd_assert_new("kern_memfd_sysctl_0_dfl",
  962. mfd_def_size,
  963. MFD_CLOEXEC | MFD_ALLOW_SEALING);
  964. mfd_assert_mode(fd, 0777);
  965. mfd_assert_has_seals(fd, 0);
  966. mfd_assert_chmod(fd, 0644);
  967. close(fd);
  968. }
  969. static void test_sysctl_set_sysctl0(void)
  970. {
  971. sysctl_assert_write("0");
  972. test_sysctl_sysctl0();
  973. }
  974. static void test_sysctl_sysctl1(void)
  975. {
  976. int fd;
  977. sysctl_assert_equal("1");
  978. fd = mfd_assert_new("kern_memfd_sysctl_1_dfl",
  979. mfd_def_size,
  980. MFD_CLOEXEC | MFD_ALLOW_SEALING);
  981. mfd_assert_mode(fd, 0666);
  982. mfd_assert_has_seals(fd, F_SEAL_EXEC);
  983. mfd_fail_chmod(fd, 0777);
  984. close(fd);
  985. fd = mfd_assert_new("kern_memfd_sysctl_1_exec",
  986. mfd_def_size,
  987. MFD_CLOEXEC | MFD_EXEC | MFD_ALLOW_SEALING);
  988. mfd_assert_mode(fd, 0777);
  989. mfd_assert_has_seals(fd, 0);
  990. mfd_assert_chmod(fd, 0644);
  991. close(fd);
  992. fd = mfd_assert_new("kern_memfd_sysctl_1_noexec",
  993. mfd_def_size,
  994. MFD_CLOEXEC | MFD_NOEXEC_SEAL | MFD_ALLOW_SEALING);
  995. mfd_assert_mode(fd, 0666);
  996. mfd_assert_has_seals(fd, F_SEAL_EXEC);
  997. mfd_fail_chmod(fd, 0777);
  998. close(fd);
  999. }
  1000. static void test_sysctl_set_sysctl1(void)
  1001. {
  1002. sysctl_assert_write("1");
  1003. test_sysctl_sysctl1();
  1004. }
  1005. static void test_sysctl_sysctl2(void)
  1006. {
  1007. int fd;
  1008. sysctl_assert_equal("2");
  1009. fd = mfd_assert_new("kern_memfd_sysctl_2_dfl",
  1010. mfd_def_size,
  1011. MFD_CLOEXEC | MFD_ALLOW_SEALING);
  1012. mfd_assert_mode(fd, 0666);
  1013. mfd_assert_has_seals(fd, F_SEAL_EXEC);
  1014. mfd_fail_chmod(fd, 0777);
  1015. close(fd);
  1016. mfd_fail_new("kern_memfd_sysctl_2_exec",
  1017. MFD_CLOEXEC | MFD_EXEC | MFD_ALLOW_SEALING);
  1018. fd = mfd_assert_new("kern_memfd_sysctl_2_noexec",
  1019. mfd_def_size,
  1020. MFD_CLOEXEC | MFD_NOEXEC_SEAL | MFD_ALLOW_SEALING);
  1021. mfd_assert_mode(fd, 0666);
  1022. mfd_assert_has_seals(fd, F_SEAL_EXEC);
  1023. mfd_fail_chmod(fd, 0777);
  1024. close(fd);
  1025. }
  1026. static void test_sysctl_set_sysctl2(void)
  1027. {
  1028. sysctl_assert_write("2");
  1029. test_sysctl_sysctl2();
  1030. }
  1031. static int sysctl_simple_child(void *arg)
  1032. {
  1033. printf("%s sysctl 0\n", memfd_str);
  1034. test_sysctl_set_sysctl0();
  1035. printf("%s sysctl 1\n", memfd_str);
  1036. test_sysctl_set_sysctl1();
  1037. printf("%s sysctl 0\n", memfd_str);
  1038. test_sysctl_set_sysctl0();
  1039. printf("%s sysctl 2\n", memfd_str);
  1040. test_sysctl_set_sysctl2();
  1041. printf("%s sysctl 1\n", memfd_str);
  1042. test_sysctl_set_sysctl1();
  1043. printf("%s sysctl 0\n", memfd_str);
  1044. test_sysctl_set_sysctl0();
  1045. return 0;
  1046. }
  1047. /*
  1048. * Test sysctl
  1049. * A very basic test to make sure the core sysctl semantics work.
  1050. */
  1051. static void test_sysctl_simple(void)
  1052. {
  1053. int pid = spawn_thread(CLONE_NEWPID, sysctl_simple_child, NULL);
  1054. join_thread(pid);
  1055. }
  1056. static int sysctl_nested(void *arg)
  1057. {
  1058. void (*fn)(void) = arg;
  1059. fn();
  1060. return 0;
  1061. }
  1062. static int sysctl_nested_wait(void *arg)
  1063. {
  1064. /* Wait for a SIGCONT. */
  1065. kill(getpid(), SIGSTOP);
  1066. return sysctl_nested(arg);
  1067. }
  1068. static void test_sysctl_sysctl1_failset(void)
  1069. {
  1070. sysctl_fail_write("0");
  1071. test_sysctl_sysctl1();
  1072. }
  1073. static void test_sysctl_sysctl2_failset(void)
  1074. {
  1075. sysctl_fail_write("1");
  1076. test_sysctl_sysctl2();
  1077. sysctl_fail_write("0");
  1078. test_sysctl_sysctl2();
  1079. }
  1080. static int sysctl_nested_child(void *arg)
  1081. {
  1082. int pid;
  1083. printf("%s nested sysctl 0\n", memfd_str);
  1084. sysctl_assert_write("0");
  1085. /* A further nested pidns works the same. */
  1086. pid = spawn_thread(CLONE_NEWPID, sysctl_simple_child, NULL);
  1087. join_thread(pid);
  1088. printf("%s nested sysctl 1\n", memfd_str);
  1089. sysctl_assert_write("1");
  1090. /* Child inherits our setting. */
  1091. pid = spawn_thread(CLONE_NEWPID, sysctl_nested, test_sysctl_sysctl1);
  1092. join_thread(pid);
  1093. /* Child cannot raise the setting. */
  1094. pid = spawn_thread(CLONE_NEWPID, sysctl_nested,
  1095. test_sysctl_sysctl1_failset);
  1096. join_thread(pid);
  1097. /* Child can lower the setting. */
  1098. pid = spawn_thread(CLONE_NEWPID, sysctl_nested,
  1099. test_sysctl_set_sysctl2);
  1100. join_thread(pid);
  1101. /* Child lowering the setting has no effect on our setting. */
  1102. test_sysctl_sysctl1();
  1103. printf("%s nested sysctl 2\n", memfd_str);
  1104. sysctl_assert_write("2");
  1105. /* Child inherits our setting. */
  1106. pid = spawn_thread(CLONE_NEWPID, sysctl_nested, test_sysctl_sysctl2);
  1107. join_thread(pid);
  1108. /* Child cannot raise the setting. */
  1109. pid = spawn_thread(CLONE_NEWPID, sysctl_nested,
  1110. test_sysctl_sysctl2_failset);
  1111. join_thread(pid);
  1112. /* Verify that the rules are actually inherited after fork. */
  1113. printf("%s nested sysctl 0 -> 1 after fork\n", memfd_str);
  1114. sysctl_assert_write("0");
  1115. pid = spawn_thread(CLONE_NEWPID, sysctl_nested_wait,
  1116. test_sysctl_sysctl1_failset);
  1117. sysctl_assert_write("1");
  1118. kill(pid, SIGCONT);
  1119. join_thread(pid);
  1120. printf("%s nested sysctl 0 -> 2 after fork\n", memfd_str);
  1121. sysctl_assert_write("0");
  1122. pid = spawn_thread(CLONE_NEWPID, sysctl_nested_wait,
  1123. test_sysctl_sysctl2_failset);
  1124. sysctl_assert_write("2");
  1125. kill(pid, SIGCONT);
  1126. join_thread(pid);
  1127. /*
  1128. * Verify that the current effective setting is saved on fork, meaning
  1129. * that the parent lowering the sysctl doesn't affect already-forked
  1130. * children.
  1131. */
  1132. printf("%s nested sysctl 2 -> 1 after fork\n", memfd_str);
  1133. sysctl_assert_write("2");
  1134. pid = spawn_thread(CLONE_NEWPID, sysctl_nested_wait,
  1135. test_sysctl_sysctl2);
  1136. sysctl_assert_write("1");
  1137. kill(pid, SIGCONT);
  1138. join_thread(pid);
  1139. printf("%s nested sysctl 2 -> 0 after fork\n", memfd_str);
  1140. sysctl_assert_write("2");
  1141. pid = spawn_thread(CLONE_NEWPID, sysctl_nested_wait,
  1142. test_sysctl_sysctl2);
  1143. sysctl_assert_write("0");
  1144. kill(pid, SIGCONT);
  1145. join_thread(pid);
  1146. printf("%s nested sysctl 1 -> 0 after fork\n", memfd_str);
  1147. sysctl_assert_write("1");
  1148. pid = spawn_thread(CLONE_NEWPID, sysctl_nested_wait,
  1149. test_sysctl_sysctl1);
  1150. sysctl_assert_write("0");
  1151. kill(pid, SIGCONT);
  1152. join_thread(pid);
  1153. return 0;
  1154. }
  1155. /*
  1156. * Test sysctl with nested pid namespaces
  1157. * Make sure that the sysctl nesting semantics work correctly.
  1158. */
  1159. static void test_sysctl_nested(void)
  1160. {
  1161. int pid = spawn_thread(CLONE_NEWPID, sysctl_nested_child, NULL);
  1162. join_thread(pid);
  1163. }
  1164. /*
  1165. * Test sharing via dup()
  1166. * Test that seals are shared between dupped FDs and they're all equal.
  1167. */
  1168. static void test_share_dup(char *banner, char *b_suffix)
  1169. {
  1170. int fd, fd2;
  1171. printf("%s %s %s\n", memfd_str, banner, b_suffix);
  1172. fd = mfd_assert_new("kern_memfd_share_dup",
  1173. mfd_def_size,
  1174. MFD_CLOEXEC | MFD_ALLOW_SEALING);
  1175. mfd_assert_has_seals(fd, 0);
  1176. fd2 = mfd_assert_dup(fd);
  1177. mfd_assert_has_seals(fd2, 0);
  1178. mfd_assert_add_seals(fd, F_SEAL_WRITE);
  1179. mfd_assert_has_seals(fd, F_SEAL_WRITE);
  1180. mfd_assert_has_seals(fd2, F_SEAL_WRITE);
  1181. mfd_assert_add_seals(fd2, F_SEAL_SHRINK);
  1182. mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
  1183. mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK);
  1184. mfd_assert_add_seals(fd, F_SEAL_SEAL);
  1185. mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
  1186. mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
  1187. mfd_fail_add_seals(fd, F_SEAL_GROW);
  1188. mfd_fail_add_seals(fd2, F_SEAL_GROW);
  1189. mfd_fail_add_seals(fd, F_SEAL_SEAL);
  1190. mfd_fail_add_seals(fd2, F_SEAL_SEAL);
  1191. close(fd2);
  1192. mfd_fail_add_seals(fd, F_SEAL_GROW);
  1193. close(fd);
  1194. }
  1195. /*
  1196. * Test sealing with active mmap()s
  1197. * Modifying seals is only allowed if no other mmap() refs exist.
  1198. */
  1199. static void test_share_mmap(char *banner, char *b_suffix)
  1200. {
  1201. int fd;
  1202. void *p;
  1203. printf("%s %s %s\n", memfd_str, banner, b_suffix);
  1204. fd = mfd_assert_new("kern_memfd_share_mmap",
  1205. mfd_def_size,
  1206. MFD_CLOEXEC | MFD_ALLOW_SEALING);
  1207. mfd_assert_has_seals(fd, 0);
  1208. /* shared/writable ref prevents sealing WRITE, but allows others */
  1209. p = mfd_assert_mmap_shared(fd);
  1210. mfd_fail_add_seals(fd, F_SEAL_WRITE);
  1211. mfd_assert_has_seals(fd, 0);
  1212. mfd_assert_add_seals(fd, F_SEAL_SHRINK);
  1213. mfd_assert_has_seals(fd, F_SEAL_SHRINK);
  1214. munmap(p, mfd_def_size);
  1215. /* readable ref allows sealing */
  1216. p = mfd_assert_mmap_private(fd);
  1217. mfd_assert_add_seals(fd, F_SEAL_WRITE);
  1218. mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
  1219. munmap(p, mfd_def_size);
  1220. close(fd);
  1221. }
  1222. /*
  1223. * Test sealing with open(/proc/self/fd/%d)
  1224. * Via /proc we can get access to a separate file-context for the same memfd.
  1225. * This is *not* like dup(), but like a real separate open(). Make sure the
  1226. * semantics are as expected and we correctly check for RDONLY / WRONLY / RDWR.
  1227. */
  1228. static void test_share_open(char *banner, char *b_suffix)
  1229. {
  1230. int fd, fd2;
  1231. printf("%s %s %s\n", memfd_str, banner, b_suffix);
  1232. fd = mfd_assert_new("kern_memfd_share_open",
  1233. mfd_def_size,
  1234. MFD_CLOEXEC | MFD_ALLOW_SEALING);
  1235. mfd_assert_has_seals(fd, 0);
  1236. fd2 = mfd_assert_open(fd, O_RDWR, 0);
  1237. mfd_assert_add_seals(fd, F_SEAL_WRITE);
  1238. mfd_assert_has_seals(fd, F_SEAL_WRITE);
  1239. mfd_assert_has_seals(fd2, F_SEAL_WRITE);
  1240. mfd_assert_add_seals(fd2, F_SEAL_SHRINK);
  1241. mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
  1242. mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK);
  1243. close(fd);
  1244. fd = mfd_assert_open(fd2, O_RDONLY, 0);
  1245. mfd_fail_add_seals(fd, F_SEAL_SEAL);
  1246. mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
  1247. mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK);
  1248. close(fd2);
  1249. fd2 = mfd_assert_open(fd, O_RDWR, 0);
  1250. mfd_assert_add_seals(fd2, F_SEAL_SEAL);
  1251. mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
  1252. mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
  1253. close(fd2);
  1254. close(fd);
  1255. }
  1256. /*
  1257. * Test sharing via fork()
  1258. * Test whether seal-modifications work as expected with forked children.
  1259. */
  1260. static void test_share_fork(char *banner, char *b_suffix)
  1261. {
  1262. int fd;
  1263. pid_t pid;
  1264. printf("%s %s %s\n", memfd_str, banner, b_suffix);
  1265. fd = mfd_assert_new("kern_memfd_share_fork",
  1266. mfd_def_size,
  1267. MFD_CLOEXEC | MFD_ALLOW_SEALING);
  1268. mfd_assert_has_seals(fd, 0);
  1269. pid = spawn_idle_thread(0);
  1270. mfd_assert_add_seals(fd, F_SEAL_SEAL);
  1271. mfd_assert_has_seals(fd, F_SEAL_SEAL);
  1272. mfd_fail_add_seals(fd, F_SEAL_WRITE);
  1273. mfd_assert_has_seals(fd, F_SEAL_SEAL);
  1274. join_idle_thread(pid);
  1275. mfd_fail_add_seals(fd, F_SEAL_WRITE);
  1276. mfd_assert_has_seals(fd, F_SEAL_SEAL);
  1277. close(fd);
  1278. }
  1279. static bool pid_ns_supported(void)
  1280. {
  1281. return access("/proc/self/ns/pid", F_OK) == 0;
  1282. }
  1283. int main(int argc, char **argv)
  1284. {
  1285. pid_t pid;
  1286. if (argc == 2) {
  1287. if (!strcmp(argv[1], "hugetlbfs")) {
  1288. unsigned long hpage_size = default_huge_page_size();
  1289. if (!hpage_size) {
  1290. printf("Unable to determine huge page size\n");
  1291. abort();
  1292. }
  1293. hugetlbfs_test = 1;
  1294. memfd_str = MEMFD_HUGE_STR;
  1295. mfd_def_size = hpage_size * 2;
  1296. } else {
  1297. printf("Unknown option: %s\n", argv[1]);
  1298. abort();
  1299. }
  1300. }
  1301. test_create();
  1302. test_basic();
  1303. test_exec_seal();
  1304. test_exec_no_seal();
  1305. test_noexec_seal();
  1306. test_seal_write();
  1307. test_seal_future_write();
  1308. test_seal_shrink();
  1309. test_seal_grow();
  1310. test_seal_resize();
  1311. if (pid_ns_supported()) {
  1312. test_sysctl_simple();
  1313. test_sysctl_nested();
  1314. } else {
  1315. printf("PID namespaces are not supported; skipping sysctl tests\n");
  1316. }
  1317. test_share_dup("SHARE-DUP", "");
  1318. test_share_mmap("SHARE-MMAP", "");
  1319. test_share_open("SHARE-OPEN", "");
  1320. test_share_fork("SHARE-FORK", "");
  1321. /* Run test-suite in a multi-threaded environment with a shared
  1322. * file-table. */
  1323. pid = spawn_idle_thread(CLONE_FILES | CLONE_FS | CLONE_VM);
  1324. test_share_dup("SHARE-DUP", SHARED_FT_STR);
  1325. test_share_mmap("SHARE-MMAP", SHARED_FT_STR);
  1326. test_share_open("SHARE-OPEN", SHARED_FT_STR);
  1327. test_share_fork("SHARE-FORK", SHARED_FT_STR);
  1328. join_idle_thread(pid);
  1329. printf("memfd: DONE\n");
  1330. return 0;
  1331. }