1 // SPDX-License-Identifier: GPL-2.0
4 * This tests memfd interactions with get_user_pages(). We require the
5 * fuse_mnt.c program to provide a fake direct-IO FUSE mount-point for us. This
6 * file-system delays _all_ reads by 1s and forces direct-IO. This means, any
7 * read() on files in that file-system will pin the receive-buffer pages for at
8 * least 1s via get_user_pages().
10 * We use this trick to race ADD_SEALS against a write on a memfd object. The
11 * ADD_SEALS must fail if the memfd pages are still pinned. Note that we use
12 * the read() syscall with our memory-mapped memfd object as receive buffer to
13 * force the kernel to write into our memfd object.
17 #define __EXPORTED_HEADERS__
22 #include <linux/falloc.h>
23 #include <linux/fcntl.h>
24 #include <linux/memfd.h>
32 #include <sys/syscall.h>
36 #define MFD_DEF_SIZE 8192
37 #define STACK_SIZE 65536
39 static int sys_memfd_create(const char *name,
42 return syscall(__NR_memfd_create, name, flags);
45 static int mfd_assert_new(const char *name, loff_t sz, unsigned int flags)
49 fd = sys_memfd_create(name, flags);
51 printf("memfd_create(\"%s\", %u) failed: %m\n",
56 r = ftruncate(fd, sz);
58 printf("ftruncate(%llu) failed: %m\n", (unsigned long long)sz);
65 static __u64 mfd_assert_get_seals(int fd)
69 r = fcntl(fd, F_GET_SEALS);
71 printf("GET_SEALS(%d) failed: %m\n", fd);
78 static void mfd_assert_has_seals(int fd, __u64 seals)
82 s = mfd_assert_get_seals(fd);
84 printf("%llu != %llu = GET_SEALS(%d)\n",
85 (unsigned long long)seals, (unsigned long long)s, fd);
90 static void mfd_assert_add_seals(int fd, __u64 seals)
95 s = mfd_assert_get_seals(fd);
96 r = fcntl(fd, F_ADD_SEALS, seals);
98 printf("ADD_SEALS(%d, %llu -> %llu) failed: %m\n",
99 fd, (unsigned long long)s, (unsigned long long)seals);
104 static int mfd_busy_add_seals(int fd, __u64 seals)
109 r = fcntl(fd, F_GET_SEALS);
115 r = fcntl(fd, F_ADD_SEALS, seals);
116 if (r < 0 && errno != EBUSY) {
117 printf("ADD_SEALS(%d, %llu -> %llu) didn't fail as expected with EBUSY: %m\n",
118 fd, (unsigned long long)s, (unsigned long long)seals);
125 static void *mfd_assert_mmap_shared(int fd)
131 PROT_READ | PROT_WRITE,
135 if (p == MAP_FAILED) {
136 printf("mmap() failed: %m\n");
143 static void *mfd_assert_mmap_private(int fd)
149 PROT_READ | PROT_WRITE,
153 if (p == MAP_FAILED) {
154 printf("mmap() failed: %m\n");
161 static int global_mfd = -1;
162 static void *global_p = NULL;
164 static int sealing_thread_fn(void *arg)
169 * This thread first waits 200ms so any pending operation in the parent
170 * is correctly started. After that, it tries to seal @global_mfd as
171 * SEAL_WRITE. This _must_ fail as the parent thread has a read() into
172 * that memory mapped object still ongoing.
173 * We then wait one more second and try sealing again. This time it
174 * must succeed as there shouldn't be anyone else pinning the pages.
177 /* wait 200ms for FUSE-request to be active */
180 /* unmount mapping before sealing to avoid i_mmap_writable failures */
181 munmap(global_p, MFD_DEF_SIZE);
183 /* Try sealing the global file; expect EBUSY or success. Current
184 * kernels will never succeed, but in the future, kernels might
185 * implement page-replacements or other fancy ways to avoid racing
187 r = mfd_busy_add_seals(global_mfd, F_SEAL_WRITE);
189 printf("HURRAY! This kernel fixed GUP races!\n");
191 /* wait 1s more so the FUSE-request is done */
194 /* try sealing the global file again */
195 mfd_assert_add_seals(global_mfd, F_SEAL_WRITE);
201 static pid_t spawn_sealing_thread(void)
206 stack = malloc(STACK_SIZE);
208 printf("malloc(STACK_SIZE) failed: %m\n");
212 pid = clone(sealing_thread_fn,
214 SIGCHLD | CLONE_FILES | CLONE_FS | CLONE_VM,
217 printf("clone() failed: %m\n");
224 static void join_sealing_thread(pid_t pid)
226 waitpid(pid, NULL, 0);
229 int main(int argc, char **argv)
231 static const char zero[MFD_DEF_SIZE];
238 printf("error: please pass path to file in fuse_mnt mount-point\n");
242 /* open FUSE memfd file for GUP testing */
243 printf("opening: %s\n", argv[1]);
244 fd = open(argv[1], O_RDONLY | O_CLOEXEC);
246 printf("cannot open(\"%s\"): %m\n", argv[1]);
250 /* create new memfd-object */
251 mfd = mfd_assert_new("kern_memfd_fuse",
253 MFD_CLOEXEC | MFD_ALLOW_SEALING);
255 /* mmap memfd-object for writing */
256 p = mfd_assert_mmap_shared(mfd);
258 /* pass mfd+mapping to a separate sealing-thread which tries to seal
259 * the memfd objects with SEAL_WRITE while we write into it */
262 pid = spawn_sealing_thread();
264 /* Use read() on the FUSE file to read into our memory-mapped memfd
265 * object. This races the other thread which tries to seal the
267 * If @fd is on the memfd-fake-FUSE-FS, the read() is delayed by 1s.
268 * This guarantees that the receive-buffer is pinned for 1s until the
269 * data is written into it. The racing ADD_SEALS should thus fail as
270 * the pages are still pinned. */
271 r = read(fd, p, MFD_DEF_SIZE);
273 printf("read() failed: %m\n");
276 printf("unexpected EOF on read()\n");
280 was_sealed = mfd_assert_get_seals(mfd) & F_SEAL_WRITE;
282 /* Wait for sealing-thread to finish and verify that it
283 * successfully sealed the file after the second try. */
284 join_sealing_thread(pid);
285 mfd_assert_has_seals(mfd, F_SEAL_WRITE);
287 /* *IF* the memfd-object was sealed at the time our read() returned,
288 * then the kernel did a page-replacement or canceled the read() (or
289 * whatever magic it did..). In that case, the memfd object is still
291 * In case the memfd-object was *not* sealed, the read() was successfull
292 * and the memfd object must *not* be all zero.
293 * Note that in real scenarios, there might be a mixture of both, but
294 * in this test-cases, we have explicit 200ms delays which should be
295 * enough to avoid any in-flight writes. */
297 p = mfd_assert_mmap_private(mfd);
298 if (was_sealed && memcmp(p, zero, MFD_DEF_SIZE)) {
299 printf("memfd sealed during read() but data not discarded\n");
301 } else if (!was_sealed && !memcmp(p, zero, MFD_DEF_SIZE)) {
302 printf("memfd sealed after read() but data discarded\n");
309 printf("fuse: DONE\n");