Skip to content

Commit bffe686

Browse files
userfaultfd: Add test using UFFDIO_WRITEPROTECT
Signed-off-by: Ricardo Branco <[email protected]>
1 parent 132c38a commit bffe686

File tree

4 files changed

+149
-0
lines changed

4 files changed

+149
-0
lines changed

runtest/syscalls

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1774,6 +1774,7 @@ userfaultfd02 userfaultfd02
17741774
userfaultfd03 userfaultfd03
17751775
userfaultfd04 userfaultfd04
17761776
userfaultfd05 userfaultfd05
1777+
userfaultfd06 userfaultfd06
17771778

17781779
ustat01 ustat01
17791780
ustat02 ustat02

testcases/kernel/syscalls/userfaultfd/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
/userfaultfd03
44
/userfaultfd04
55
/userfaultfd05
6+
/userfaultfd06

testcases/kernel/syscalls/userfaultfd/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ userfaultfd02: CFLAGS += -pthread
1616
userfaultfd03: CFLAGS += -pthread
1717
userfaultfd04: CFLAGS += -pthread
1818
userfaultfd05: CFLAGS += -pthread
19+
userfaultfd06: CFLAGS += -pthread
Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
// SPDX-License-Identifier: GPL-2.0-or-later
2+
/*
3+
* Copyright (c) 2025 SUSE LLC
4+
* Author: Christian Amann <[email protected]>
5+
* Author: Ricardo Branco <[email protected]>
6+
*
7+
* Test userfaultfd write protection
8+
*
9+
* Force a pagefault event and handle it using userfaultfd
10+
* from a different thread
11+
*/
12+
13+
#include "config.h"
14+
#include "tst_test.h"
15+
16+
#include <poll.h>
17+
18+
#include "tst_safe_macros.h"
19+
#include "tst_safe_pthread.h"
20+
#include "lapi/userfaultfd.h"
21+
22+
static int page_size;
23+
static char *page;
24+
static int uffd;
25+
static volatile int wp_fault_seen;
26+
27+
static int sys_userfaultfd(int flags)
28+
{
29+
return tst_syscall(__NR_userfaultfd, flags);
30+
}
31+
32+
static void set_pages(void)
33+
{
34+
page_size = sysconf(_SC_PAGE_SIZE);
35+
page = SAFE_MMAP(NULL, page_size, PROT_READ | PROT_WRITE,
36+
MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
37+
38+
memset(page, 0, page_size);
39+
}
40+
41+
static void handle_thread(void)
42+
{
43+
static struct uffd_msg msg;
44+
struct uffdio_writeprotect uffdio_writeprotect;
45+
46+
struct pollfd pollfd;
47+
int nready;
48+
49+
pollfd.fd = uffd;
50+
pollfd.events = POLLIN;
51+
nready = poll(&pollfd, 1, -1);
52+
if (nready == -1)
53+
tst_brk(TBROK | TERRNO,
54+
"Error on poll");
55+
56+
SAFE_READ(1, uffd, &msg, sizeof(msg));
57+
58+
if (msg.event != UFFD_EVENT_PAGEFAULT)
59+
tst_brk(TBROK | TERRNO,
60+
"Received unexpected UFFD_EVENT");
61+
62+
if (!(msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WP) ||
63+
!(msg.arg.pagefault.flags & UFFD_PAGEFAULT_FLAG_WRITE)) {
64+
tst_brk(TBROK,
65+
"Expected WP+WRITE fault but flags=%lx",
66+
(unsigned long)msg.arg.pagefault.flags);
67+
}
68+
69+
/* While the WP fault is pending, the write must NOT be visible. */
70+
if (page[0xf] != '\0')
71+
tst_brk(TFAIL,
72+
"Write became visible while page was write-protected!");
73+
74+
wp_fault_seen = 1;
75+
76+
/* Resolve the fault by clearing WP so the writer can resume. */
77+
uffdio_writeprotect.range.start = msg.arg.pagefault.address & ~(page_size - 1);
78+
uffdio_writeprotect.range.len = page_size;
79+
uffdio_writeprotect.mode = 0; /* clear write-protect */
80+
81+
SAFE_IOCTL(uffd, UFFDIO_WRITEPROTECT, &uffdio_writeprotect);
82+
83+
close(uffd);
84+
}
85+
86+
static void run(void)
87+
{
88+
pthread_t thr;
89+
struct uffdio_api uffdio_api;
90+
struct uffdio_register uffdio_register;
91+
struct uffdio_writeprotect uffdio_writeprotect;
92+
93+
set_pages();
94+
95+
TEST(sys_userfaultfd(O_CLOEXEC | O_NONBLOCK));
96+
97+
if (TST_RET == -1) {
98+
if (TST_ERR == EPERM) {
99+
tst_res(TCONF, "Hint: check /proc/sys/vm/unprivileged_userfaultfd");
100+
tst_brk(TCONF | TTERRNO,
101+
"userfaultfd() requires CAP_SYS_PTRACE on this system");
102+
} else
103+
tst_brk(TBROK | TTERRNO,
104+
"Could not create userfault file descriptor");
105+
}
106+
107+
uffd = TST_RET;
108+
uffdio_api.api = UFFD_API;
109+
uffdio_api.features = UFFD_FEATURE_PAGEFAULT_FLAG_WP;
110+
SAFE_IOCTL(uffd, UFFDIO_API, &uffdio_api);
111+
112+
if (!(uffdio_api.features & UFFD_FEATURE_PAGEFAULT_FLAG_WP)) {
113+
tst_brk(TCONF, "UFFD write-protect unsupported");
114+
return;
115+
}
116+
117+
uffdio_register.range.start = (unsigned long) page;
118+
uffdio_register.range.len = page_size;
119+
uffdio_register.mode = UFFDIO_REGISTER_MODE_WP;
120+
121+
SAFE_IOCTL(uffd, UFFDIO_REGISTER, &uffdio_register);
122+
123+
uffdio_writeprotect.range.start = (unsigned long)page;
124+
uffdio_writeprotect.range.len = page_size;
125+
uffdio_writeprotect.mode = UFFDIO_WRITEPROTECT_MODE_WP;
126+
127+
SAFE_IOCTL(uffd, UFFDIO_WRITEPROTECT, &uffdio_writeprotect);
128+
129+
SAFE_PTHREAD_CREATE(&thr, NULL,
130+
(void * (*)(void *)) handle_thread, NULL);
131+
132+
/* Try to write */
133+
page[0xf] = 'W';
134+
135+
SAFE_PTHREAD_JOIN(thr, NULL);
136+
137+
if (wp_fault_seen)
138+
tst_res(TPASS, "WRITEPROTECT pagefault handled!");
139+
else
140+
tst_res(TFAIL, "No WRITEPROTECT pagefault observed");
141+
}
142+
143+
static struct tst_test test = {
144+
.test_all = run,
145+
.min_kver = "5.7",
146+
};

0 commit comments

Comments
 (0)