Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * Copyright IBM Corporation, 2021
0004  *
0005  * Author: Mike Rapoport <rppt@linux.ibm.com>
0006  */
0007 
0008 #define _GNU_SOURCE
0009 #include <sys/uio.h>
0010 #include <sys/mman.h>
0011 #include <sys/wait.h>
0012 #include <sys/types.h>
0013 #include <sys/ptrace.h>
0014 #include <sys/syscall.h>
0015 #include <sys/resource.h>
0016 #include <sys/capability.h>
0017 
0018 #include <stdlib.h>
0019 #include <string.h>
0020 #include <unistd.h>
0021 #include <errno.h>
0022 #include <stdio.h>
0023 
0024 #include "../kselftest.h"
0025 
0026 #define fail(fmt, ...) ksft_test_result_fail(fmt, ##__VA_ARGS__)
0027 #define pass(fmt, ...) ksft_test_result_pass(fmt, ##__VA_ARGS__)
0028 #define skip(fmt, ...) ksft_test_result_skip(fmt, ##__VA_ARGS__)
0029 
0030 #ifdef __NR_memfd_secret
0031 
0032 #define PATTERN 0x55
0033 
0034 static const int prot = PROT_READ | PROT_WRITE;
0035 static const int mode = MAP_SHARED;
0036 
0037 static unsigned long page_size;
0038 static unsigned long mlock_limit_cur;
0039 static unsigned long mlock_limit_max;
0040 
0041 static int memfd_secret(unsigned int flags)
0042 {
0043     return syscall(__NR_memfd_secret, flags);
0044 }
0045 
0046 static void test_file_apis(int fd)
0047 {
0048     char buf[64];
0049 
0050     if ((read(fd, buf, sizeof(buf)) >= 0) ||
0051         (write(fd, buf, sizeof(buf)) >= 0) ||
0052         (pread(fd, buf, sizeof(buf), 0) >= 0) ||
0053         (pwrite(fd, buf, sizeof(buf), 0) >= 0))
0054         fail("unexpected file IO\n");
0055     else
0056         pass("file IO is blocked as expected\n");
0057 }
0058 
0059 static void test_mlock_limit(int fd)
0060 {
0061     size_t len;
0062     char *mem;
0063 
0064     len = mlock_limit_cur;
0065     mem = mmap(NULL, len, prot, mode, fd, 0);
0066     if (mem == MAP_FAILED) {
0067         fail("unable to mmap secret memory\n");
0068         return;
0069     }
0070     munmap(mem, len);
0071 
0072     len = mlock_limit_max * 2;
0073     mem = mmap(NULL, len, prot, mode, fd, 0);
0074     if (mem != MAP_FAILED) {
0075         fail("unexpected mlock limit violation\n");
0076         munmap(mem, len);
0077         return;
0078     }
0079 
0080     pass("mlock limit is respected\n");
0081 }
0082 
0083 static void try_process_vm_read(int fd, int pipefd[2])
0084 {
0085     struct iovec liov, riov;
0086     char buf[64];
0087     char *mem;
0088 
0089     if (read(pipefd[0], &mem, sizeof(mem)) < 0) {
0090         fail("pipe write: %s\n", strerror(errno));
0091         exit(KSFT_FAIL);
0092     }
0093 
0094     liov.iov_len = riov.iov_len = sizeof(buf);
0095     liov.iov_base = buf;
0096     riov.iov_base = mem;
0097 
0098     if (process_vm_readv(getppid(), &liov, 1, &riov, 1, 0) < 0) {
0099         if (errno == ENOSYS)
0100             exit(KSFT_SKIP);
0101         exit(KSFT_PASS);
0102     }
0103 
0104     exit(KSFT_FAIL);
0105 }
0106 
0107 static void try_ptrace(int fd, int pipefd[2])
0108 {
0109     pid_t ppid = getppid();
0110     int status;
0111     char *mem;
0112     long ret;
0113 
0114     if (read(pipefd[0], &mem, sizeof(mem)) < 0) {
0115         perror("pipe write");
0116         exit(KSFT_FAIL);
0117     }
0118 
0119     ret = ptrace(PTRACE_ATTACH, ppid, 0, 0);
0120     if (ret) {
0121         perror("ptrace_attach");
0122         exit(KSFT_FAIL);
0123     }
0124 
0125     ret = waitpid(ppid, &status, WUNTRACED);
0126     if ((ret != ppid) || !(WIFSTOPPED(status))) {
0127         fprintf(stderr, "weird waitppid result %ld stat %x\n",
0128             ret, status);
0129         exit(KSFT_FAIL);
0130     }
0131 
0132     if (ptrace(PTRACE_PEEKDATA, ppid, mem, 0))
0133         exit(KSFT_PASS);
0134 
0135     exit(KSFT_FAIL);
0136 }
0137 
0138 static void check_child_status(pid_t pid, const char *name)
0139 {
0140     int status;
0141 
0142     waitpid(pid, &status, 0);
0143 
0144     if (WIFEXITED(status) && WEXITSTATUS(status) == KSFT_SKIP) {
0145         skip("%s is not supported\n", name);
0146         return;
0147     }
0148 
0149     if ((WIFEXITED(status) && WEXITSTATUS(status) == KSFT_PASS) ||
0150         WIFSIGNALED(status)) {
0151         pass("%s is blocked as expected\n", name);
0152         return;
0153     }
0154 
0155     fail("%s: unexpected memory access\n", name);
0156 }
0157 
0158 static void test_remote_access(int fd, const char *name,
0159                    void (*func)(int fd, int pipefd[2]))
0160 {
0161     int pipefd[2];
0162     pid_t pid;
0163     char *mem;
0164 
0165     if (pipe(pipefd)) {
0166         fail("pipe failed: %s\n", strerror(errno));
0167         return;
0168     }
0169 
0170     pid = fork();
0171     if (pid < 0) {
0172         fail("fork failed: %s\n", strerror(errno));
0173         return;
0174     }
0175 
0176     if (pid == 0) {
0177         func(fd, pipefd);
0178         return;
0179     }
0180 
0181     mem = mmap(NULL, page_size, prot, mode, fd, 0);
0182     if (mem == MAP_FAILED) {
0183         fail("Unable to mmap secret memory\n");
0184         return;
0185     }
0186 
0187     ftruncate(fd, page_size);
0188     memset(mem, PATTERN, page_size);
0189 
0190     if (write(pipefd[1], &mem, sizeof(mem)) < 0) {
0191         fail("pipe write: %s\n", strerror(errno));
0192         return;
0193     }
0194 
0195     check_child_status(pid, name);
0196 }
0197 
0198 static void test_process_vm_read(int fd)
0199 {
0200     test_remote_access(fd, "process_vm_read", try_process_vm_read);
0201 }
0202 
0203 static void test_ptrace(int fd)
0204 {
0205     test_remote_access(fd, "ptrace", try_ptrace);
0206 }
0207 
0208 static int set_cap_limits(rlim_t max)
0209 {
0210     struct rlimit new;
0211     cap_t cap = cap_init();
0212 
0213     new.rlim_cur = max;
0214     new.rlim_max = max;
0215     if (setrlimit(RLIMIT_MEMLOCK, &new)) {
0216         perror("setrlimit() returns error");
0217         return -1;
0218     }
0219 
0220     /* drop capabilities including CAP_IPC_LOCK */
0221     if (cap_set_proc(cap)) {
0222         perror("cap_set_proc() returns error");
0223         return -2;
0224     }
0225 
0226     return 0;
0227 }
0228 
0229 static void prepare(void)
0230 {
0231     struct rlimit rlim;
0232 
0233     page_size = sysconf(_SC_PAGE_SIZE);
0234     if (!page_size)
0235         ksft_exit_fail_msg("Failed to get page size %s\n",
0236                    strerror(errno));
0237 
0238     if (getrlimit(RLIMIT_MEMLOCK, &rlim))
0239         ksft_exit_fail_msg("Unable to detect mlock limit: %s\n",
0240                    strerror(errno));
0241 
0242     mlock_limit_cur = rlim.rlim_cur;
0243     mlock_limit_max = rlim.rlim_max;
0244 
0245     printf("page_size: %ld, mlock.soft: %ld, mlock.hard: %ld\n",
0246            page_size, mlock_limit_cur, mlock_limit_max);
0247 
0248     if (page_size > mlock_limit_cur)
0249         mlock_limit_cur = page_size;
0250     if (page_size > mlock_limit_max)
0251         mlock_limit_max = page_size;
0252 
0253     if (set_cap_limits(mlock_limit_max))
0254         ksft_exit_fail_msg("Unable to set mlock limit: %s\n",
0255                    strerror(errno));
0256 }
0257 
0258 #define NUM_TESTS 4
0259 
0260 int main(int argc, char *argv[])
0261 {
0262     int fd;
0263 
0264     prepare();
0265 
0266     ksft_print_header();
0267     ksft_set_plan(NUM_TESTS);
0268 
0269     fd = memfd_secret(0);
0270     if (fd < 0) {
0271         if (errno == ENOSYS)
0272             ksft_exit_skip("memfd_secret is not supported\n");
0273         else
0274             ksft_exit_fail_msg("memfd_secret failed: %s\n",
0275                        strerror(errno));
0276     }
0277 
0278     test_mlock_limit(fd);
0279     test_file_apis(fd);
0280     test_process_vm_read(fd);
0281     test_ptrace(fd);
0282 
0283     close(fd);
0284 
0285     ksft_finished();
0286 }
0287 
0288 #else /* __NR_memfd_secret */
0289 
0290 int main(int argc, char *argv[])
0291 {
0292     printf("skip: skipping memfd_secret test (missing __NR_memfd_secret)\n");
0293     return KSFT_SKIP;
0294 }
0295 
0296 #endif /* __NR_memfd_secret */