1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright IBM Corporation, 2021
5 * Author: Mike Rapoport <rppt@linux.ibm.com>
12 #include <sys/types.h>
13 #include <sys/ptrace.h>
14 #include <sys/syscall.h>
15 #include <sys/resource.h>
16 #include <sys/capability.h>
24 #include "../kselftest.h"
26 #define fail(fmt, ...) ksft_test_result_fail(fmt, ##__VA_ARGS__)
27 #define pass(fmt, ...) ksft_test_result_pass(fmt, ##__VA_ARGS__)
28 #define skip(fmt, ...) ksft_test_result_skip(fmt, ##__VA_ARGS__)
30 #ifdef __NR_memfd_secret
34 static const int prot = PROT_READ | PROT_WRITE;
35 static const int mode = MAP_SHARED;
37 static unsigned long page_size;
38 static unsigned long mlock_limit_cur;
39 static unsigned long mlock_limit_max;
41 static int memfd_secret(unsigned int flags)
43 return syscall(__NR_memfd_secret, flags);
46 static void test_file_apis(int fd)
50 if ((read(fd, buf, sizeof(buf)) >= 0) ||
51 (write(fd, buf, sizeof(buf)) >= 0) ||
52 (pread(fd, buf, sizeof(buf), 0) >= 0) ||
53 (pwrite(fd, buf, sizeof(buf), 0) >= 0))
54 fail("unexpected file IO\n");
56 pass("file IO is blocked as expected\n");
59 static void test_mlock_limit(int fd)
64 len = mlock_limit_cur;
65 mem = mmap(NULL, len, prot, mode, fd, 0);
66 if (mem == MAP_FAILED) {
67 fail("unable to mmap secret memory\n");
72 len = mlock_limit_max * 2;
73 mem = mmap(NULL, len, prot, mode, fd, 0);
74 if (mem != MAP_FAILED) {
75 fail("unexpected mlock limit violation\n");
80 pass("mlock limit is respected\n");
83 static void try_process_vm_read(int fd, int pipefd[2])
85 struct iovec liov, riov;
89 if (read(pipefd[0], &mem, sizeof(mem)) < 0) {
90 fail("pipe write: %s\n", strerror(errno));
94 liov.iov_len = riov.iov_len = sizeof(buf);
98 if (process_vm_readv(getppid(), &liov, 1, &riov, 1, 0) < 0) {
107 static void try_ptrace(int fd, int pipefd[2])
109 pid_t ppid = getppid();
114 if (read(pipefd[0], &mem, sizeof(mem)) < 0) {
115 perror("pipe write");
119 ret = ptrace(PTRACE_ATTACH, ppid, 0, 0);
121 perror("ptrace_attach");
125 ret = waitpid(ppid, &status, WUNTRACED);
126 if ((ret != ppid) || !(WIFSTOPPED(status))) {
127 fprintf(stderr, "weird waitppid result %ld stat %x\n",
132 if (ptrace(PTRACE_PEEKDATA, ppid, mem, 0))
138 static void check_child_status(pid_t pid, const char *name)
142 waitpid(pid, &status, 0);
144 if (WIFEXITED(status) && WEXITSTATUS(status) == KSFT_SKIP) {
145 skip("%s is not supported\n", name);
149 if ((WIFEXITED(status) && WEXITSTATUS(status) == KSFT_PASS) ||
150 WIFSIGNALED(status)) {
151 pass("%s is blocked as expected\n", name);
155 fail("%s: unexpected memory access\n", name);
158 static void test_remote_access(int fd, const char *name,
159 void (*func)(int fd, int pipefd[2]))
166 fail("pipe failed: %s\n", strerror(errno));
172 fail("fork failed: %s\n", strerror(errno));
181 mem = mmap(NULL, page_size, prot, mode, fd, 0);
182 if (mem == MAP_FAILED) {
183 fail("Unable to mmap secret memory\n");
187 ftruncate(fd, page_size);
188 memset(mem, PATTERN, page_size);
190 if (write(pipefd[1], &mem, sizeof(mem)) < 0) {
191 fail("pipe write: %s\n", strerror(errno));
195 check_child_status(pid, name);
198 static void test_process_vm_read(int fd)
200 test_remote_access(fd, "process_vm_read", try_process_vm_read);
203 static void test_ptrace(int fd)
205 test_remote_access(fd, "ptrace", try_ptrace);
208 static int set_cap_limits(rlim_t max)
211 cap_t cap = cap_init();
215 if (setrlimit(RLIMIT_MEMLOCK, &new)) {
216 perror("setrlimit() returns error");
220 /* drop capabilities including CAP_IPC_LOCK */
221 if (cap_set_proc(cap)) {
222 perror("cap_set_proc() returns error");
229 static void prepare(void)
233 page_size = sysconf(_SC_PAGE_SIZE);
235 ksft_exit_fail_msg("Failed to get page size %s\n",
238 if (getrlimit(RLIMIT_MEMLOCK, &rlim))
239 ksft_exit_fail_msg("Unable to detect mlock limit: %s\n",
242 mlock_limit_cur = rlim.rlim_cur;
243 mlock_limit_max = rlim.rlim_max;
245 printf("page_size: %ld, mlock.soft: %ld, mlock.hard: %ld\n",
246 page_size, mlock_limit_cur, mlock_limit_max);
248 if (page_size > mlock_limit_cur)
249 mlock_limit_cur = page_size;
250 if (page_size > mlock_limit_max)
251 mlock_limit_max = page_size;
253 if (set_cap_limits(mlock_limit_max))
254 ksft_exit_fail_msg("Unable to set mlock limit: %s\n",
260 int main(int argc, char *argv[])
267 ksft_set_plan(NUM_TESTS);
269 fd = memfd_secret(0);
272 ksft_exit_skip("memfd_secret is not supported\n");
274 ksft_exit_fail_msg("memfd_secret failed: %s\n",
278 test_mlock_limit(fd);
280 test_process_vm_read(fd);
285 ksft_exit(!ksft_get_fail_cnt());
288 #else /* __NR_memfd_secret */
290 int main(int argc, char *argv[])
292 printf("skip: skipping memfd_secret test (missing __NR_memfd_secret)\n");
296 #endif /* __NR_memfd_secret */