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