fuse_test.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * memfd GUP test-case
  3. * This tests memfd interactions with get_user_pages(). We require the
  4. * fuse_mnt.c program to provide a fake direct-IO FUSE mount-point for us. This
  5. * file-system delays _all_ reads by 1s and forces direct-IO. This means, any
  6. * read() on files in that file-system will pin the receive-buffer pages for at
  7. * least 1s via get_user_pages().
  8. *
  9. * We use this trick to race ADD_SEALS against a write on a memfd object. The
  10. * ADD_SEALS must fail if the memfd pages are still pinned. Note that we use
  11. * the read() syscall with our memory-mapped memfd object as receive buffer to
  12. * force the kernel to write into our memfd object.
  13. */
  14. #define _GNU_SOURCE
  15. #define __EXPORTED_HEADERS__
  16. #include <errno.h>
  17. #include <inttypes.h>
  18. #include <limits.h>
  19. #include <linux/falloc.h>
  20. #include <linux/fcntl.h>
  21. #include <linux/memfd.h>
  22. #include <sched.h>
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <signal.h>
  26. #include <string.h>
  27. #include <sys/mman.h>
  28. #include <sys/stat.h>
  29. #include <sys/syscall.h>
  30. #include <sys/wait.h>
  31. #include <unistd.h>
  32. #define MFD_DEF_SIZE 8192
  33. #define STACK_SIZE 65535
  34. static int sys_memfd_create(const char *name,
  35. unsigned int flags)
  36. {
  37. return syscall(__NR_memfd_create, name, flags);
  38. }
  39. static int mfd_assert_new(const char *name, loff_t sz, unsigned int flags)
  40. {
  41. int r, fd;
  42. fd = sys_memfd_create(name, flags);
  43. if (fd < 0) {
  44. printf("memfd_create(\"%s\", %u) failed: %m\n",
  45. name, flags);
  46. abort();
  47. }
  48. r = ftruncate(fd, sz);
  49. if (r < 0) {
  50. printf("ftruncate(%llu) failed: %m\n", (unsigned long long)sz);
  51. abort();
  52. }
  53. return fd;
  54. }
  55. static __u64 mfd_assert_get_seals(int fd)
  56. {
  57. long r;
  58. r = fcntl(fd, F_GET_SEALS);
  59. if (r < 0) {
  60. printf("GET_SEALS(%d) failed: %m\n", fd);
  61. abort();
  62. }
  63. return r;
  64. }
  65. static void mfd_assert_has_seals(int fd, __u64 seals)
  66. {
  67. __u64 s;
  68. s = mfd_assert_get_seals(fd);
  69. if (s != seals) {
  70. printf("%llu != %llu = GET_SEALS(%d)\n",
  71. (unsigned long long)seals, (unsigned long long)s, fd);
  72. abort();
  73. }
  74. }
  75. static void mfd_assert_add_seals(int fd, __u64 seals)
  76. {
  77. long r;
  78. __u64 s;
  79. s = mfd_assert_get_seals(fd);
  80. r = fcntl(fd, F_ADD_SEALS, seals);
  81. if (r < 0) {
  82. printf("ADD_SEALS(%d, %llu -> %llu) failed: %m\n",
  83. fd, (unsigned long long)s, (unsigned long long)seals);
  84. abort();
  85. }
  86. }
  87. static int mfd_busy_add_seals(int fd, __u64 seals)
  88. {
  89. long r;
  90. __u64 s;
  91. r = fcntl(fd, F_GET_SEALS);
  92. if (r < 0)
  93. s = 0;
  94. else
  95. s = r;
  96. r = fcntl(fd, F_ADD_SEALS, seals);
  97. if (r < 0 && errno != EBUSY) {
  98. printf("ADD_SEALS(%d, %llu -> %llu) didn't fail as expected with EBUSY: %m\n",
  99. fd, (unsigned long long)s, (unsigned long long)seals);
  100. abort();
  101. }
  102. return r;
  103. }
  104. static void *mfd_assert_mmap_shared(int fd)
  105. {
  106. void *p;
  107. p = mmap(NULL,
  108. MFD_DEF_SIZE,
  109. PROT_READ | PROT_WRITE,
  110. MAP_SHARED,
  111. fd,
  112. 0);
  113. if (p == MAP_FAILED) {
  114. printf("mmap() failed: %m\n");
  115. abort();
  116. }
  117. return p;
  118. }
  119. static void *mfd_assert_mmap_private(int fd)
  120. {
  121. void *p;
  122. p = mmap(NULL,
  123. MFD_DEF_SIZE,
  124. PROT_READ | PROT_WRITE,
  125. MAP_PRIVATE,
  126. fd,
  127. 0);
  128. if (p == MAP_FAILED) {
  129. printf("mmap() failed: %m\n");
  130. abort();
  131. }
  132. return p;
  133. }
  134. static int global_mfd = -1;
  135. static void *global_p = NULL;
  136. static int sealing_thread_fn(void *arg)
  137. {
  138. int sig, r;
  139. /*
  140. * This thread first waits 200ms so any pending operation in the parent
  141. * is correctly started. After that, it tries to seal @global_mfd as
  142. * SEAL_WRITE. This _must_ fail as the parent thread has a read() into
  143. * that memory mapped object still ongoing.
  144. * We then wait one more second and try sealing again. This time it
  145. * must succeed as there shouldn't be anyone else pinning the pages.
  146. */
  147. /* wait 200ms for FUSE-request to be active */
  148. usleep(200000);
  149. /* unmount mapping before sealing to avoid i_mmap_writable failures */
  150. munmap(global_p, MFD_DEF_SIZE);
  151. /* Try sealing the global file; expect EBUSY or success. Current
  152. * kernels will never succeed, but in the future, kernels might
  153. * implement page-replacements or other fancy ways to avoid racing
  154. * writes. */
  155. r = mfd_busy_add_seals(global_mfd, F_SEAL_WRITE);
  156. if (r >= 0) {
  157. printf("HURRAY! This kernel fixed GUP races!\n");
  158. } else {
  159. /* wait 1s more so the FUSE-request is done */
  160. sleep(1);
  161. /* try sealing the global file again */
  162. mfd_assert_add_seals(global_mfd, F_SEAL_WRITE);
  163. }
  164. return 0;
  165. }
  166. static pid_t spawn_sealing_thread(void)
  167. {
  168. uint8_t *stack;
  169. pid_t pid;
  170. stack = malloc(STACK_SIZE);
  171. if (!stack) {
  172. printf("malloc(STACK_SIZE) failed: %m\n");
  173. abort();
  174. }
  175. pid = clone(sealing_thread_fn,
  176. stack + STACK_SIZE,
  177. SIGCHLD | CLONE_FILES | CLONE_FS | CLONE_VM,
  178. NULL);
  179. if (pid < 0) {
  180. printf("clone() failed: %m\n");
  181. abort();
  182. }
  183. return pid;
  184. }
  185. static void join_sealing_thread(pid_t pid)
  186. {
  187. waitpid(pid, NULL, 0);
  188. }
  189. int main(int argc, char **argv)
  190. {
  191. static const char zero[MFD_DEF_SIZE];
  192. int fd, mfd, r;
  193. void *p;
  194. int was_sealed;
  195. pid_t pid;
  196. if (argc < 2) {
  197. printf("error: please pass path to file in fuse_mnt mount-point\n");
  198. abort();
  199. }
  200. /* open FUSE memfd file for GUP testing */
  201. printf("opening: %s\n", argv[1]);
  202. fd = open(argv[1], O_RDONLY | O_CLOEXEC);
  203. if (fd < 0) {
  204. printf("cannot open(\"%s\"): %m\n", argv[1]);
  205. abort();
  206. }
  207. /* create new memfd-object */
  208. mfd = mfd_assert_new("kern_memfd_fuse",
  209. MFD_DEF_SIZE,
  210. MFD_CLOEXEC | MFD_ALLOW_SEALING);
  211. /* mmap memfd-object for writing */
  212. p = mfd_assert_mmap_shared(mfd);
  213. /* pass mfd+mapping to a separate sealing-thread which tries to seal
  214. * the memfd objects with SEAL_WRITE while we write into it */
  215. global_mfd = mfd;
  216. global_p = p;
  217. pid = spawn_sealing_thread();
  218. /* Use read() on the FUSE file to read into our memory-mapped memfd
  219. * object. This races the other thread which tries to seal the
  220. * memfd-object.
  221. * If @fd is on the memfd-fake-FUSE-FS, the read() is delayed by 1s.
  222. * This guarantees that the receive-buffer is pinned for 1s until the
  223. * data is written into it. The racing ADD_SEALS should thus fail as
  224. * the pages are still pinned. */
  225. r = read(fd, p, MFD_DEF_SIZE);
  226. if (r < 0) {
  227. printf("read() failed: %m\n");
  228. abort();
  229. } else if (!r) {
  230. printf("unexpected EOF on read()\n");
  231. abort();
  232. }
  233. was_sealed = mfd_assert_get_seals(mfd) & F_SEAL_WRITE;
  234. /* Wait for sealing-thread to finish and verify that it
  235. * successfully sealed the file after the second try. */
  236. join_sealing_thread(pid);
  237. mfd_assert_has_seals(mfd, F_SEAL_WRITE);
  238. /* *IF* the memfd-object was sealed at the time our read() returned,
  239. * then the kernel did a page-replacement or canceled the read() (or
  240. * whatever magic it did..). In that case, the memfd object is still
  241. * all zero.
  242. * In case the memfd-object was *not* sealed, the read() was successfull
  243. * and the memfd object must *not* be all zero.
  244. * Note that in real scenarios, there might be a mixture of both, but
  245. * in this test-cases, we have explicit 200ms delays which should be
  246. * enough to avoid any in-flight writes. */
  247. p = mfd_assert_mmap_private(mfd);
  248. if (was_sealed && memcmp(p, zero, MFD_DEF_SIZE)) {
  249. printf("memfd sealed during read() but data not discarded\n");
  250. abort();
  251. } else if (!was_sealed && !memcmp(p, zero, MFD_DEF_SIZE)) {
  252. printf("memfd sealed after read() but data discarded\n");
  253. abort();
  254. }
  255. close(mfd);
  256. close(fd);
  257. printf("fuse: DONE\n");
  258. return 0;
  259. }