memfd_test.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969
  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 <linux/fcntl.h>
  9. #include <linux/memfd.h>
  10. #include <sched.h>
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <signal.h>
  14. #include <string.h>
  15. #include <sys/mman.h>
  16. #include <sys/stat.h>
  17. #include <sys/syscall.h>
  18. #include <sys/wait.h>
  19. #include <unistd.h>
  20. #include "common.h"
  21. #define MEMFD_STR "memfd:"
  22. #define MEMFD_HUGE_STR "memfd-hugetlb:"
  23. #define SHARED_FT_STR "(shared file-table)"
  24. #define MFD_DEF_SIZE 8192
  25. #define STACK_SIZE 65536
  26. /*
  27. * Default is not to test hugetlbfs
  28. */
  29. static size_t mfd_def_size = MFD_DEF_SIZE;
  30. static const char *memfd_str = MEMFD_STR;
  31. static int mfd_assert_new(const char *name, loff_t sz, unsigned int flags)
  32. {
  33. int r, fd;
  34. fd = sys_memfd_create(name, flags);
  35. if (fd < 0) {
  36. printf("memfd_create(\"%s\", %u) failed: %m\n",
  37. name, flags);
  38. abort();
  39. }
  40. r = ftruncate(fd, sz);
  41. if (r < 0) {
  42. printf("ftruncate(%llu) failed: %m\n", (unsigned long long)sz);
  43. abort();
  44. }
  45. return fd;
  46. }
  47. static void mfd_fail_new(const char *name, unsigned int flags)
  48. {
  49. int r;
  50. r = sys_memfd_create(name, flags);
  51. if (r >= 0) {
  52. printf("memfd_create(\"%s\", %u) succeeded, but failure expected\n",
  53. name, flags);
  54. close(r);
  55. abort();
  56. }
  57. }
  58. static unsigned int mfd_assert_get_seals(int fd)
  59. {
  60. int r;
  61. r = fcntl(fd, F_GET_SEALS);
  62. if (r < 0) {
  63. printf("GET_SEALS(%d) failed: %m\n", fd);
  64. abort();
  65. }
  66. return (unsigned int)r;
  67. }
  68. static void mfd_assert_has_seals(int fd, unsigned int seals)
  69. {
  70. unsigned int s;
  71. s = mfd_assert_get_seals(fd);
  72. if (s != seals) {
  73. printf("%u != %u = GET_SEALS(%d)\n", seals, s, fd);
  74. abort();
  75. }
  76. }
  77. static void mfd_assert_add_seals(int fd, unsigned int seals)
  78. {
  79. int r;
  80. unsigned int s;
  81. s = mfd_assert_get_seals(fd);
  82. r = fcntl(fd, F_ADD_SEALS, seals);
  83. if (r < 0) {
  84. printf("ADD_SEALS(%d, %u -> %u) failed: %m\n", fd, s, seals);
  85. abort();
  86. }
  87. }
  88. static void mfd_fail_add_seals(int fd, unsigned int seals)
  89. {
  90. int r;
  91. unsigned int s;
  92. r = fcntl(fd, F_GET_SEALS);
  93. if (r < 0)
  94. s = 0;
  95. else
  96. s = (unsigned int)r;
  97. r = fcntl(fd, F_ADD_SEALS, seals);
  98. if (r >= 0) {
  99. printf("ADD_SEALS(%d, %u -> %u) didn't fail as expected\n",
  100. fd, s, seals);
  101. abort();
  102. }
  103. }
  104. static void mfd_assert_size(int fd, size_t size)
  105. {
  106. struct stat st;
  107. int r;
  108. r = fstat(fd, &st);
  109. if (r < 0) {
  110. printf("fstat(%d) failed: %m\n", fd);
  111. abort();
  112. } else if (st.st_size != size) {
  113. printf("wrong file size %lld, but expected %lld\n",
  114. (long long)st.st_size, (long long)size);
  115. abort();
  116. }
  117. }
  118. static int mfd_assert_dup(int fd)
  119. {
  120. int r;
  121. r = dup(fd);
  122. if (r < 0) {
  123. printf("dup(%d) failed: %m\n", fd);
  124. abort();
  125. }
  126. return r;
  127. }
  128. static void *mfd_assert_mmap_shared(int fd)
  129. {
  130. void *p;
  131. p = mmap(NULL,
  132. mfd_def_size,
  133. PROT_READ | PROT_WRITE,
  134. MAP_SHARED,
  135. fd,
  136. 0);
  137. if (p == MAP_FAILED) {
  138. printf("mmap() failed: %m\n");
  139. abort();
  140. }
  141. return p;
  142. }
  143. static void *mfd_assert_mmap_private(int fd)
  144. {
  145. void *p;
  146. p = mmap(NULL,
  147. mfd_def_size,
  148. PROT_READ,
  149. MAP_PRIVATE,
  150. fd,
  151. 0);
  152. if (p == MAP_FAILED) {
  153. printf("mmap() failed: %m\n");
  154. abort();
  155. }
  156. return p;
  157. }
  158. static int mfd_assert_open(int fd, int flags, mode_t mode)
  159. {
  160. char buf[512];
  161. int r;
  162. sprintf(buf, "/proc/self/fd/%d", fd);
  163. r = open(buf, flags, mode);
  164. if (r < 0) {
  165. printf("open(%s) failed: %m\n", buf);
  166. abort();
  167. }
  168. return r;
  169. }
  170. static void mfd_fail_open(int fd, int flags, mode_t mode)
  171. {
  172. char buf[512];
  173. int r;
  174. sprintf(buf, "/proc/self/fd/%d", fd);
  175. r = open(buf, flags, mode);
  176. if (r >= 0) {
  177. printf("open(%s) didn't fail as expected\n", buf);
  178. abort();
  179. }
  180. }
  181. static void mfd_assert_read(int fd)
  182. {
  183. char buf[16];
  184. void *p;
  185. ssize_t l;
  186. l = read(fd, buf, sizeof(buf));
  187. if (l != sizeof(buf)) {
  188. printf("read() failed: %m\n");
  189. abort();
  190. }
  191. /* verify PROT_READ *is* allowed */
  192. p = mmap(NULL,
  193. mfd_def_size,
  194. PROT_READ,
  195. MAP_PRIVATE,
  196. fd,
  197. 0);
  198. if (p == MAP_FAILED) {
  199. printf("mmap() failed: %m\n");
  200. abort();
  201. }
  202. munmap(p, mfd_def_size);
  203. /* verify MAP_PRIVATE is *always* allowed (even writable) */
  204. p = mmap(NULL,
  205. mfd_def_size,
  206. PROT_READ | PROT_WRITE,
  207. MAP_PRIVATE,
  208. fd,
  209. 0);
  210. if (p == MAP_FAILED) {
  211. printf("mmap() failed: %m\n");
  212. abort();
  213. }
  214. munmap(p, mfd_def_size);
  215. }
  216. static void mfd_assert_write(int fd)
  217. {
  218. ssize_t l;
  219. void *p;
  220. int r;
  221. /*
  222. * huegtlbfs does not support write, but we want to
  223. * verify everything else here.
  224. */
  225. if (!hugetlbfs_test) {
  226. /* verify write() succeeds */
  227. l = write(fd, "\0\0\0\0", 4);
  228. if (l != 4) {
  229. printf("write() failed: %m\n");
  230. abort();
  231. }
  232. }
  233. /* verify PROT_READ | PROT_WRITE is allowed */
  234. p = mmap(NULL,
  235. mfd_def_size,
  236. PROT_READ | PROT_WRITE,
  237. MAP_SHARED,
  238. fd,
  239. 0);
  240. if (p == MAP_FAILED) {
  241. printf("mmap() failed: %m\n");
  242. abort();
  243. }
  244. *(char *)p = 0;
  245. munmap(p, mfd_def_size);
  246. /* verify PROT_WRITE is allowed */
  247. p = mmap(NULL,
  248. mfd_def_size,
  249. PROT_WRITE,
  250. MAP_SHARED,
  251. fd,
  252. 0);
  253. if (p == MAP_FAILED) {
  254. printf("mmap() failed: %m\n");
  255. abort();
  256. }
  257. *(char *)p = 0;
  258. munmap(p, mfd_def_size);
  259. /* verify PROT_READ with MAP_SHARED is allowed and a following
  260. * mprotect(PROT_WRITE) allows writing */
  261. p = mmap(NULL,
  262. mfd_def_size,
  263. PROT_READ,
  264. MAP_SHARED,
  265. fd,
  266. 0);
  267. if (p == MAP_FAILED) {
  268. printf("mmap() failed: %m\n");
  269. abort();
  270. }
  271. r = mprotect(p, mfd_def_size, PROT_READ | PROT_WRITE);
  272. if (r < 0) {
  273. printf("mprotect() failed: %m\n");
  274. abort();
  275. }
  276. *(char *)p = 0;
  277. munmap(p, mfd_def_size);
  278. /* verify PUNCH_HOLE works */
  279. r = fallocate(fd,
  280. FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
  281. 0,
  282. mfd_def_size);
  283. if (r < 0) {
  284. printf("fallocate(PUNCH_HOLE) failed: %m\n");
  285. abort();
  286. }
  287. }
  288. static void mfd_fail_write(int fd)
  289. {
  290. ssize_t l;
  291. void *p;
  292. int r;
  293. /* verify write() fails */
  294. l = write(fd, "data", 4);
  295. if (l != -EPERM) {
  296. printf("expected EPERM on write(), but got %d: %m\n", (int)l);
  297. abort();
  298. }
  299. /* verify PROT_READ | PROT_WRITE is not allowed */
  300. p = mmap(NULL,
  301. mfd_def_size,
  302. PROT_READ | PROT_WRITE,
  303. MAP_SHARED,
  304. fd,
  305. 0);
  306. if (p != MAP_FAILED) {
  307. printf("mmap() didn't fail as expected\n");
  308. abort();
  309. }
  310. /* verify PROT_WRITE is not allowed */
  311. p = mmap(NULL,
  312. mfd_def_size,
  313. PROT_WRITE,
  314. MAP_SHARED,
  315. fd,
  316. 0);
  317. if (p != MAP_FAILED) {
  318. printf("mmap() didn't fail as expected\n");
  319. abort();
  320. }
  321. /* Verify PROT_READ with MAP_SHARED with a following mprotect is not
  322. * allowed. Note that for r/w the kernel already prevents the mmap. */
  323. p = mmap(NULL,
  324. mfd_def_size,
  325. PROT_READ,
  326. MAP_SHARED,
  327. fd,
  328. 0);
  329. if (p != MAP_FAILED) {
  330. r = mprotect(p, mfd_def_size, PROT_READ | PROT_WRITE);
  331. if (r >= 0) {
  332. printf("mmap()+mprotect() didn't fail as expected\n");
  333. abort();
  334. }
  335. }
  336. /* verify PUNCH_HOLE fails */
  337. r = fallocate(fd,
  338. FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE,
  339. 0,
  340. mfd_def_size);
  341. if (r >= 0) {
  342. printf("fallocate(PUNCH_HOLE) didn't fail as expected\n");
  343. abort();
  344. }
  345. }
  346. static void mfd_assert_shrink(int fd)
  347. {
  348. int r, fd2;
  349. r = ftruncate(fd, mfd_def_size / 2);
  350. if (r < 0) {
  351. printf("ftruncate(SHRINK) failed: %m\n");
  352. abort();
  353. }
  354. mfd_assert_size(fd, mfd_def_size / 2);
  355. fd2 = mfd_assert_open(fd,
  356. O_RDWR | O_CREAT | O_TRUNC,
  357. S_IRUSR | S_IWUSR);
  358. close(fd2);
  359. mfd_assert_size(fd, 0);
  360. }
  361. static void mfd_fail_shrink(int fd)
  362. {
  363. int r;
  364. r = ftruncate(fd, mfd_def_size / 2);
  365. if (r >= 0) {
  366. printf("ftruncate(SHRINK) didn't fail as expected\n");
  367. abort();
  368. }
  369. mfd_fail_open(fd,
  370. O_RDWR | O_CREAT | O_TRUNC,
  371. S_IRUSR | S_IWUSR);
  372. }
  373. static void mfd_assert_grow(int fd)
  374. {
  375. int r;
  376. r = ftruncate(fd, mfd_def_size * 2);
  377. if (r < 0) {
  378. printf("ftruncate(GROW) failed: %m\n");
  379. abort();
  380. }
  381. mfd_assert_size(fd, mfd_def_size * 2);
  382. r = fallocate(fd,
  383. 0,
  384. 0,
  385. mfd_def_size * 4);
  386. if (r < 0) {
  387. printf("fallocate(ALLOC) failed: %m\n");
  388. abort();
  389. }
  390. mfd_assert_size(fd, mfd_def_size * 4);
  391. }
  392. static void mfd_fail_grow(int fd)
  393. {
  394. int r;
  395. r = ftruncate(fd, mfd_def_size * 2);
  396. if (r >= 0) {
  397. printf("ftruncate(GROW) didn't fail as expected\n");
  398. abort();
  399. }
  400. r = fallocate(fd,
  401. 0,
  402. 0,
  403. mfd_def_size * 4);
  404. if (r >= 0) {
  405. printf("fallocate(ALLOC) didn't fail as expected\n");
  406. abort();
  407. }
  408. }
  409. static void mfd_assert_grow_write(int fd)
  410. {
  411. static char *buf;
  412. ssize_t l;
  413. /* hugetlbfs does not support write */
  414. if (hugetlbfs_test)
  415. return;
  416. buf = malloc(mfd_def_size * 8);
  417. if (!buf) {
  418. printf("malloc(%zu) failed: %m\n", mfd_def_size * 8);
  419. abort();
  420. }
  421. l = pwrite(fd, buf, mfd_def_size * 8, 0);
  422. if (l != (mfd_def_size * 8)) {
  423. printf("pwrite() failed: %m\n");
  424. abort();
  425. }
  426. mfd_assert_size(fd, mfd_def_size * 8);
  427. }
  428. static void mfd_fail_grow_write(int fd)
  429. {
  430. static char *buf;
  431. ssize_t l;
  432. /* hugetlbfs does not support write */
  433. if (hugetlbfs_test)
  434. return;
  435. buf = malloc(mfd_def_size * 8);
  436. if (!buf) {
  437. printf("malloc(%zu) failed: %m\n", mfd_def_size * 8);
  438. abort();
  439. }
  440. l = pwrite(fd, buf, mfd_def_size * 8, 0);
  441. if (l == (mfd_def_size * 8)) {
  442. printf("pwrite() didn't fail as expected\n");
  443. abort();
  444. }
  445. }
  446. static int idle_thread_fn(void *arg)
  447. {
  448. sigset_t set;
  449. int sig;
  450. /* dummy waiter; SIGTERM terminates us anyway */
  451. sigemptyset(&set);
  452. sigaddset(&set, SIGTERM);
  453. sigwait(&set, &sig);
  454. return 0;
  455. }
  456. static pid_t spawn_idle_thread(unsigned int flags)
  457. {
  458. uint8_t *stack;
  459. pid_t pid;
  460. stack = malloc(STACK_SIZE);
  461. if (!stack) {
  462. printf("malloc(STACK_SIZE) failed: %m\n");
  463. abort();
  464. }
  465. pid = clone(idle_thread_fn,
  466. stack + STACK_SIZE,
  467. SIGCHLD | flags,
  468. NULL);
  469. if (pid < 0) {
  470. printf("clone() failed: %m\n");
  471. abort();
  472. }
  473. return pid;
  474. }
  475. static void join_idle_thread(pid_t pid)
  476. {
  477. kill(pid, SIGTERM);
  478. waitpid(pid, NULL, 0);
  479. }
  480. /*
  481. * Test memfd_create() syscall
  482. * Verify syscall-argument validation, including name checks, flag validation
  483. * and more.
  484. */
  485. static void test_create(void)
  486. {
  487. char buf[2048];
  488. int fd;
  489. printf("%s CREATE\n", memfd_str);
  490. /* test NULL name */
  491. mfd_fail_new(NULL, 0);
  492. /* test over-long name (not zero-terminated) */
  493. memset(buf, 0xff, sizeof(buf));
  494. mfd_fail_new(buf, 0);
  495. /* test over-long zero-terminated name */
  496. memset(buf, 0xff, sizeof(buf));
  497. buf[sizeof(buf) - 1] = 0;
  498. mfd_fail_new(buf, 0);
  499. /* verify "" is a valid name */
  500. fd = mfd_assert_new("", 0, 0);
  501. close(fd);
  502. /* verify invalid O_* open flags */
  503. mfd_fail_new("", 0x0100);
  504. mfd_fail_new("", ~MFD_CLOEXEC);
  505. mfd_fail_new("", ~MFD_ALLOW_SEALING);
  506. mfd_fail_new("", ~0);
  507. mfd_fail_new("", 0x80000000U);
  508. /* verify MFD_CLOEXEC is allowed */
  509. fd = mfd_assert_new("", 0, MFD_CLOEXEC);
  510. close(fd);
  511. /* verify MFD_ALLOW_SEALING is allowed */
  512. fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING);
  513. close(fd);
  514. /* verify MFD_ALLOW_SEALING | MFD_CLOEXEC is allowed */
  515. fd = mfd_assert_new("", 0, MFD_ALLOW_SEALING | MFD_CLOEXEC);
  516. close(fd);
  517. }
  518. /*
  519. * Test basic sealing
  520. * A very basic sealing test to see whether setting/retrieving seals works.
  521. */
  522. static void test_basic(void)
  523. {
  524. int fd;
  525. printf("%s BASIC\n", memfd_str);
  526. fd = mfd_assert_new("kern_memfd_basic",
  527. mfd_def_size,
  528. MFD_CLOEXEC | MFD_ALLOW_SEALING);
  529. /* add basic seals */
  530. mfd_assert_has_seals(fd, 0);
  531. mfd_assert_add_seals(fd, F_SEAL_SHRINK |
  532. F_SEAL_WRITE);
  533. mfd_assert_has_seals(fd, F_SEAL_SHRINK |
  534. F_SEAL_WRITE);
  535. /* add them again */
  536. mfd_assert_add_seals(fd, F_SEAL_SHRINK |
  537. F_SEAL_WRITE);
  538. mfd_assert_has_seals(fd, F_SEAL_SHRINK |
  539. F_SEAL_WRITE);
  540. /* add more seals and seal against sealing */
  541. mfd_assert_add_seals(fd, F_SEAL_GROW | F_SEAL_SEAL);
  542. mfd_assert_has_seals(fd, F_SEAL_SHRINK |
  543. F_SEAL_GROW |
  544. F_SEAL_WRITE |
  545. F_SEAL_SEAL);
  546. /* verify that sealing no longer works */
  547. mfd_fail_add_seals(fd, F_SEAL_GROW);
  548. mfd_fail_add_seals(fd, 0);
  549. close(fd);
  550. /* verify sealing does not work without MFD_ALLOW_SEALING */
  551. fd = mfd_assert_new("kern_memfd_basic",
  552. mfd_def_size,
  553. MFD_CLOEXEC);
  554. mfd_assert_has_seals(fd, F_SEAL_SEAL);
  555. mfd_fail_add_seals(fd, F_SEAL_SHRINK |
  556. F_SEAL_GROW |
  557. F_SEAL_WRITE);
  558. mfd_assert_has_seals(fd, F_SEAL_SEAL);
  559. close(fd);
  560. }
  561. /*
  562. * Test SEAL_WRITE
  563. * Test whether SEAL_WRITE actually prevents modifications.
  564. */
  565. static void test_seal_write(void)
  566. {
  567. int fd;
  568. printf("%s SEAL-WRITE\n", memfd_str);
  569. fd = mfd_assert_new("kern_memfd_seal_write",
  570. mfd_def_size,
  571. MFD_CLOEXEC | MFD_ALLOW_SEALING);
  572. mfd_assert_has_seals(fd, 0);
  573. mfd_assert_add_seals(fd, F_SEAL_WRITE);
  574. mfd_assert_has_seals(fd, F_SEAL_WRITE);
  575. mfd_assert_read(fd);
  576. mfd_fail_write(fd);
  577. mfd_assert_shrink(fd);
  578. mfd_assert_grow(fd);
  579. mfd_fail_grow_write(fd);
  580. close(fd);
  581. }
  582. /*
  583. * Test SEAL_SHRINK
  584. * Test whether SEAL_SHRINK actually prevents shrinking
  585. */
  586. static void test_seal_shrink(void)
  587. {
  588. int fd;
  589. printf("%s SEAL-SHRINK\n", memfd_str);
  590. fd = mfd_assert_new("kern_memfd_seal_shrink",
  591. mfd_def_size,
  592. MFD_CLOEXEC | MFD_ALLOW_SEALING);
  593. mfd_assert_has_seals(fd, 0);
  594. mfd_assert_add_seals(fd, F_SEAL_SHRINK);
  595. mfd_assert_has_seals(fd, F_SEAL_SHRINK);
  596. mfd_assert_read(fd);
  597. mfd_assert_write(fd);
  598. mfd_fail_shrink(fd);
  599. mfd_assert_grow(fd);
  600. mfd_assert_grow_write(fd);
  601. close(fd);
  602. }
  603. /*
  604. * Test SEAL_GROW
  605. * Test whether SEAL_GROW actually prevents growing
  606. */
  607. static void test_seal_grow(void)
  608. {
  609. int fd;
  610. printf("%s SEAL-GROW\n", memfd_str);
  611. fd = mfd_assert_new("kern_memfd_seal_grow",
  612. mfd_def_size,
  613. MFD_CLOEXEC | MFD_ALLOW_SEALING);
  614. mfd_assert_has_seals(fd, 0);
  615. mfd_assert_add_seals(fd, F_SEAL_GROW);
  616. mfd_assert_has_seals(fd, F_SEAL_GROW);
  617. mfd_assert_read(fd);
  618. mfd_assert_write(fd);
  619. mfd_assert_shrink(fd);
  620. mfd_fail_grow(fd);
  621. mfd_fail_grow_write(fd);
  622. close(fd);
  623. }
  624. /*
  625. * Test SEAL_SHRINK | SEAL_GROW
  626. * Test whether SEAL_SHRINK | SEAL_GROW actually prevents resizing
  627. */
  628. static void test_seal_resize(void)
  629. {
  630. int fd;
  631. printf("%s SEAL-RESIZE\n", memfd_str);
  632. fd = mfd_assert_new("kern_memfd_seal_resize",
  633. mfd_def_size,
  634. MFD_CLOEXEC | MFD_ALLOW_SEALING);
  635. mfd_assert_has_seals(fd, 0);
  636. mfd_assert_add_seals(fd, F_SEAL_SHRINK | F_SEAL_GROW);
  637. mfd_assert_has_seals(fd, F_SEAL_SHRINK | F_SEAL_GROW);
  638. mfd_assert_read(fd);
  639. mfd_assert_write(fd);
  640. mfd_fail_shrink(fd);
  641. mfd_fail_grow(fd);
  642. mfd_fail_grow_write(fd);
  643. close(fd);
  644. }
  645. /*
  646. * Test sharing via dup()
  647. * Test that seals are shared between dupped FDs and they're all equal.
  648. */
  649. static void test_share_dup(char *banner, char *b_suffix)
  650. {
  651. int fd, fd2;
  652. printf("%s %s %s\n", memfd_str, banner, b_suffix);
  653. fd = mfd_assert_new("kern_memfd_share_dup",
  654. mfd_def_size,
  655. MFD_CLOEXEC | MFD_ALLOW_SEALING);
  656. mfd_assert_has_seals(fd, 0);
  657. fd2 = mfd_assert_dup(fd);
  658. mfd_assert_has_seals(fd2, 0);
  659. mfd_assert_add_seals(fd, F_SEAL_WRITE);
  660. mfd_assert_has_seals(fd, F_SEAL_WRITE);
  661. mfd_assert_has_seals(fd2, F_SEAL_WRITE);
  662. mfd_assert_add_seals(fd2, F_SEAL_SHRINK);
  663. mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
  664. mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK);
  665. mfd_assert_add_seals(fd, F_SEAL_SEAL);
  666. mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
  667. mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
  668. mfd_fail_add_seals(fd, F_SEAL_GROW);
  669. mfd_fail_add_seals(fd2, F_SEAL_GROW);
  670. mfd_fail_add_seals(fd, F_SEAL_SEAL);
  671. mfd_fail_add_seals(fd2, F_SEAL_SEAL);
  672. close(fd2);
  673. mfd_fail_add_seals(fd, F_SEAL_GROW);
  674. close(fd);
  675. }
  676. /*
  677. * Test sealing with active mmap()s
  678. * Modifying seals is only allowed if no other mmap() refs exist.
  679. */
  680. static void test_share_mmap(char *banner, char *b_suffix)
  681. {
  682. int fd;
  683. void *p;
  684. printf("%s %s %s\n", memfd_str, banner, b_suffix);
  685. fd = mfd_assert_new("kern_memfd_share_mmap",
  686. mfd_def_size,
  687. MFD_CLOEXEC | MFD_ALLOW_SEALING);
  688. mfd_assert_has_seals(fd, 0);
  689. /* shared/writable ref prevents sealing WRITE, but allows others */
  690. p = mfd_assert_mmap_shared(fd);
  691. mfd_fail_add_seals(fd, F_SEAL_WRITE);
  692. mfd_assert_has_seals(fd, 0);
  693. mfd_assert_add_seals(fd, F_SEAL_SHRINK);
  694. mfd_assert_has_seals(fd, F_SEAL_SHRINK);
  695. munmap(p, mfd_def_size);
  696. /* readable ref allows sealing */
  697. p = mfd_assert_mmap_private(fd);
  698. mfd_assert_add_seals(fd, F_SEAL_WRITE);
  699. mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
  700. munmap(p, mfd_def_size);
  701. close(fd);
  702. }
  703. /*
  704. * Test sealing with open(/proc/self/fd/%d)
  705. * Via /proc we can get access to a separate file-context for the same memfd.
  706. * This is *not* like dup(), but like a real separate open(). Make sure the
  707. * semantics are as expected and we correctly check for RDONLY / WRONLY / RDWR.
  708. */
  709. static void test_share_open(char *banner, char *b_suffix)
  710. {
  711. int fd, fd2;
  712. printf("%s %s %s\n", memfd_str, banner, b_suffix);
  713. fd = mfd_assert_new("kern_memfd_share_open",
  714. mfd_def_size,
  715. MFD_CLOEXEC | MFD_ALLOW_SEALING);
  716. mfd_assert_has_seals(fd, 0);
  717. fd2 = mfd_assert_open(fd, O_RDWR, 0);
  718. mfd_assert_add_seals(fd, F_SEAL_WRITE);
  719. mfd_assert_has_seals(fd, F_SEAL_WRITE);
  720. mfd_assert_has_seals(fd2, F_SEAL_WRITE);
  721. mfd_assert_add_seals(fd2, F_SEAL_SHRINK);
  722. mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
  723. mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK);
  724. close(fd);
  725. fd = mfd_assert_open(fd2, O_RDONLY, 0);
  726. mfd_fail_add_seals(fd, F_SEAL_SEAL);
  727. mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK);
  728. mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK);
  729. close(fd2);
  730. fd2 = mfd_assert_open(fd, O_RDWR, 0);
  731. mfd_assert_add_seals(fd2, F_SEAL_SEAL);
  732. mfd_assert_has_seals(fd, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
  733. mfd_assert_has_seals(fd2, F_SEAL_WRITE | F_SEAL_SHRINK | F_SEAL_SEAL);
  734. close(fd2);
  735. close(fd);
  736. }
  737. /*
  738. * Test sharing via fork()
  739. * Test whether seal-modifications work as expected with forked childs.
  740. */
  741. static void test_share_fork(char *banner, char *b_suffix)
  742. {
  743. int fd;
  744. pid_t pid;
  745. printf("%s %s %s\n", memfd_str, banner, b_suffix);
  746. fd = mfd_assert_new("kern_memfd_share_fork",
  747. mfd_def_size,
  748. MFD_CLOEXEC | MFD_ALLOW_SEALING);
  749. mfd_assert_has_seals(fd, 0);
  750. pid = spawn_idle_thread(0);
  751. mfd_assert_add_seals(fd, F_SEAL_SEAL);
  752. mfd_assert_has_seals(fd, F_SEAL_SEAL);
  753. mfd_fail_add_seals(fd, F_SEAL_WRITE);
  754. mfd_assert_has_seals(fd, F_SEAL_SEAL);
  755. join_idle_thread(pid);
  756. mfd_fail_add_seals(fd, F_SEAL_WRITE);
  757. mfd_assert_has_seals(fd, F_SEAL_SEAL);
  758. close(fd);
  759. }
  760. int main(int argc, char **argv)
  761. {
  762. pid_t pid;
  763. if (argc == 2) {
  764. if (!strcmp(argv[1], "hugetlbfs")) {
  765. unsigned long hpage_size = default_huge_page_size();
  766. if (!hpage_size) {
  767. printf("Unable to determine huge page size\n");
  768. abort();
  769. }
  770. hugetlbfs_test = 1;
  771. memfd_str = MEMFD_HUGE_STR;
  772. mfd_def_size = hpage_size * 2;
  773. } else {
  774. printf("Unknown option: %s\n", argv[1]);
  775. abort();
  776. }
  777. }
  778. test_create();
  779. test_basic();
  780. test_seal_write();
  781. test_seal_shrink();
  782. test_seal_grow();
  783. test_seal_resize();
  784. test_share_dup("SHARE-DUP", "");
  785. test_share_mmap("SHARE-MMAP", "");
  786. test_share_open("SHARE-OPEN", "");
  787. test_share_fork("SHARE-FORK", "");
  788. /* Run test-suite in a multi-threaded environment with a shared
  789. * file-table. */
  790. pid = spawn_idle_thread(CLONE_FILES | CLONE_FS | CLONE_VM);
  791. test_share_dup("SHARE-DUP", SHARED_FT_STR);
  792. test_share_mmap("SHARE-MMAP", SHARED_FT_STR);
  793. test_share_open("SHARE-OPEN", SHARED_FT_STR);
  794. test_share_fork("SHARE-FORK", SHARED_FT_STR);
  795. join_idle_thread(pid);
  796. printf("memfd: DONE\n");
  797. return 0;
  798. }