1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright (c) 2019 FUJITSU LIMITED. All rights reserved.
4*49cdfc7eSAndroid Build Coastguard Worker * Author: Yang Xu <[email protected]>
5*49cdfc7eSAndroid Build Coastguard Worker */
6*49cdfc7eSAndroid Build Coastguard Worker
7*49cdfc7eSAndroid Build Coastguard Worker /*\
8*49cdfc7eSAndroid Build Coastguard Worker * [Description]
9*49cdfc7eSAndroid Build Coastguard Worker *
10*49cdfc7eSAndroid Build Coastguard Worker * Test PR_GET_SECCOMP and PR_SET_SECCOMP of prctl(2).
11*49cdfc7eSAndroid Build Coastguard Worker *
12*49cdfc7eSAndroid Build Coastguard Worker * - If PR_SET_SECCOMP sets the SECCOMP_MODE_STRICT for the calling thread,
13*49cdfc7eSAndroid Build Coastguard Worker * the only system call that the thread is permitted to make are read(2),
14*49cdfc7eSAndroid Build Coastguard Worker * write(2),_exit(2)(but not exit_group(2)), and sigreturn(2). Other
15*49cdfc7eSAndroid Build Coastguard Worker * system calls result in the delivery of a SIGKILL signal. This operation
16*49cdfc7eSAndroid Build Coastguard Worker * is available only if the kernel is configured with CONFIG_SECCOMP enabled.
17*49cdfc7eSAndroid Build Coastguard Worker *
18*49cdfc7eSAndroid Build Coastguard Worker * - If PR_SET_SECCOMP sets the SECCOMP_MODE_FILTER for the calling thread,
19*49cdfc7eSAndroid Build Coastguard Worker * the system calls allowed are defined by a pointer to a Berkeley Packet
20*49cdfc7eSAndroid Build Coastguard Worker * Filter. Other system calls result int the delivery of a SIGSYS signal
21*49cdfc7eSAndroid Build Coastguard Worker * with SECCOMP_RET_KILL. The SECCOMP_SET_MODE_FILTER operation is available
22*49cdfc7eSAndroid Build Coastguard Worker * only if the kernel is configured with CONFIG_SECCOMP_FILTER enabled.
23*49cdfc7eSAndroid Build Coastguard Worker *
24*49cdfc7eSAndroid Build Coastguard Worker * - If SECCOMP_MODE_FILTER filters permit fork(2), then the seccomp mode
25*49cdfc7eSAndroid Build Coastguard Worker * is inherited by children created by fork(2).
26*49cdfc7eSAndroid Build Coastguard Worker */
27*49cdfc7eSAndroid Build Coastguard Worker
28*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
29*49cdfc7eSAndroid Build Coastguard Worker #include <signal.h>
30*49cdfc7eSAndroid Build Coastguard Worker #include <sys/prctl.h>
31*49cdfc7eSAndroid Build Coastguard Worker #include <sys/wait.h>
32*49cdfc7eSAndroid Build Coastguard Worker #include <sys/types.h>
33*49cdfc7eSAndroid Build Coastguard Worker #include <linux/filter.h>
34*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
35*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
36*49cdfc7eSAndroid Build Coastguard Worker #include <stddef.h>
37*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
38*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/syscalls.h"
39*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/prctl.h"
40*49cdfc7eSAndroid Build Coastguard Worker #include "config.h"
41*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/seccomp.h"
42*49cdfc7eSAndroid Build Coastguard Worker
43*49cdfc7eSAndroid Build Coastguard Worker #define FNAME "filename"
44*49cdfc7eSAndroid Build Coastguard Worker
45*49cdfc7eSAndroid Build Coastguard Worker static const struct sock_filter strict_filter[] = {
46*49cdfc7eSAndroid Build Coastguard Worker BPF_STMT(BPF_LD | BPF_W | BPF_ABS, (offsetof(struct seccomp_data, nr))),
47*49cdfc7eSAndroid Build Coastguard Worker
48*49cdfc7eSAndroid Build Coastguard Worker BPF_JUMP(BPF_JMP | BPF_JEQ, __NR_waitid, 7, 0),
49*49cdfc7eSAndroid Build Coastguard Worker BPF_JUMP(BPF_JMP | BPF_JEQ, __NR_rt_sigprocmask, 6, 0),
50*49cdfc7eSAndroid Build Coastguard Worker BPF_JUMP(BPF_JMP | BPF_JEQ, __NR_close, 5, 0),
51*49cdfc7eSAndroid Build Coastguard Worker BPF_JUMP(BPF_JMP | BPF_JEQ, __NR_exit, 4, 0),
52*49cdfc7eSAndroid Build Coastguard Worker BPF_JUMP(BPF_JMP | BPF_JEQ, __NR_wait4, 3, 0),
53*49cdfc7eSAndroid Build Coastguard Worker BPF_JUMP(BPF_JMP | BPF_JEQ, __NR_write, 2, 0),
54*49cdfc7eSAndroid Build Coastguard Worker BPF_JUMP(BPF_JMP | BPF_JEQ, __NR_clone, 1, 0),
55*49cdfc7eSAndroid Build Coastguard Worker
56*49cdfc7eSAndroid Build Coastguard Worker BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_KILL),
57*49cdfc7eSAndroid Build Coastguard Worker BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW)
58*49cdfc7eSAndroid Build Coastguard Worker };
59*49cdfc7eSAndroid Build Coastguard Worker
60*49cdfc7eSAndroid Build Coastguard Worker static const struct sock_fprog strict = {
61*49cdfc7eSAndroid Build Coastguard Worker .len = (unsigned short)ARRAY_SIZE(strict_filter),
62*49cdfc7eSAndroid Build Coastguard Worker .filter = (struct sock_filter *)strict_filter
63*49cdfc7eSAndroid Build Coastguard Worker };
64*49cdfc7eSAndroid Build Coastguard Worker
65*49cdfc7eSAndroid Build Coastguard Worker static void check_strict_mode(int);
66*49cdfc7eSAndroid Build Coastguard Worker static void check_filter_mode(int);
67*49cdfc7eSAndroid Build Coastguard Worker
68*49cdfc7eSAndroid Build Coastguard Worker static struct tcase {
69*49cdfc7eSAndroid Build Coastguard Worker void (*func_check)();
70*49cdfc7eSAndroid Build Coastguard Worker int pass_flag;
71*49cdfc7eSAndroid Build Coastguard Worker int val;
72*49cdfc7eSAndroid Build Coastguard Worker int exp_signal;
73*49cdfc7eSAndroid Build Coastguard Worker char *message;
74*49cdfc7eSAndroid Build Coastguard Worker } tcases[] = {
75*49cdfc7eSAndroid Build Coastguard Worker {check_strict_mode, 1, 1, SIGKILL,
76*49cdfc7eSAndroid Build Coastguard Worker "SECCOMP_MODE_STRICT doesn't permit GET_SECCOMP call"},
77*49cdfc7eSAndroid Build Coastguard Worker
78*49cdfc7eSAndroid Build Coastguard Worker {check_strict_mode, 0, 2, SIGKILL,
79*49cdfc7eSAndroid Build Coastguard Worker "SECCOMP_MODE_STRICT doesn't permit read(2) write(2) and _exit(2)"},
80*49cdfc7eSAndroid Build Coastguard Worker
81*49cdfc7eSAndroid Build Coastguard Worker {check_strict_mode, 1, 3, SIGKILL,
82*49cdfc7eSAndroid Build Coastguard Worker "SECCOMP_MODE_STRICT doesn't permit close(2)"},
83*49cdfc7eSAndroid Build Coastguard Worker
84*49cdfc7eSAndroid Build Coastguard Worker {check_filter_mode, 1, 1, SIGSYS,
85*49cdfc7eSAndroid Build Coastguard Worker "SECCOMP_MODE_FILTER doestn't permit GET_SECCOMP call"},
86*49cdfc7eSAndroid Build Coastguard Worker
87*49cdfc7eSAndroid Build Coastguard Worker {check_filter_mode, 0, 2, SIGSYS,
88*49cdfc7eSAndroid Build Coastguard Worker "SECCOMP_MODE_FILTER doesn't permit close(2)"},
89*49cdfc7eSAndroid Build Coastguard Worker
90*49cdfc7eSAndroid Build Coastguard Worker {check_filter_mode, 2, 3, SIGSYS,
91*49cdfc7eSAndroid Build Coastguard Worker "SECCOMP_MODE_FILTER doesn't permit exit()"},
92*49cdfc7eSAndroid Build Coastguard Worker
93*49cdfc7eSAndroid Build Coastguard Worker {check_filter_mode, 0, 4, SIGSYS,
94*49cdfc7eSAndroid Build Coastguard Worker "SECCOMP_MODE_FILTER doesn't permit exit()"}
95*49cdfc7eSAndroid Build Coastguard Worker };
96*49cdfc7eSAndroid Build Coastguard Worker
97*49cdfc7eSAndroid Build Coastguard Worker
98*49cdfc7eSAndroid Build Coastguard Worker static int mode_filter_not_supported;
99*49cdfc7eSAndroid Build Coastguard Worker
check_filter_mode_inherit(void)100*49cdfc7eSAndroid Build Coastguard Worker static void check_filter_mode_inherit(void)
101*49cdfc7eSAndroid Build Coastguard Worker {
102*49cdfc7eSAndroid Build Coastguard Worker int childpid;
103*49cdfc7eSAndroid Build Coastguard Worker int childstatus;
104*49cdfc7eSAndroid Build Coastguard Worker
105*49cdfc7eSAndroid Build Coastguard Worker childpid = SAFE_FORK();
106*49cdfc7eSAndroid Build Coastguard Worker if (childpid == 0) {
107*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "SECCOMP_MODE_FILTER permits fork(2)");
108*49cdfc7eSAndroid Build Coastguard Worker exit(0);
109*49cdfc7eSAndroid Build Coastguard Worker }
110*49cdfc7eSAndroid Build Coastguard Worker
111*49cdfc7eSAndroid Build Coastguard Worker wait4(childpid, &childstatus, 0, NULL);
112*49cdfc7eSAndroid Build Coastguard Worker if (WIFSIGNALED(childstatus) && WTERMSIG(childstatus) == SIGSYS)
113*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS,
114*49cdfc7eSAndroid Build Coastguard Worker "SECCOMP_MODE_FILTER has been inherited by child");
115*49cdfc7eSAndroid Build Coastguard Worker else
116*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL,
117*49cdfc7eSAndroid Build Coastguard Worker "SECCOMP_MODE_FILTER isn't been inherited by child");
118*49cdfc7eSAndroid Build Coastguard Worker }
119*49cdfc7eSAndroid Build Coastguard Worker
check_strict_mode(int val)120*49cdfc7eSAndroid Build Coastguard Worker static void check_strict_mode(int val)
121*49cdfc7eSAndroid Build Coastguard Worker {
122*49cdfc7eSAndroid Build Coastguard Worker int fd;
123*49cdfc7eSAndroid Build Coastguard Worker char buf[2];
124*49cdfc7eSAndroid Build Coastguard Worker
125*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(FNAME, O_RDWR | O_CREAT, 0666);
126*49cdfc7eSAndroid Build Coastguard Worker
127*49cdfc7eSAndroid Build Coastguard Worker TEST(prctl(PR_SET_SECCOMP, SECCOMP_MODE_STRICT));
128*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET == -1) {
129*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO,
130*49cdfc7eSAndroid Build Coastguard Worker "prctl(PR_SET_SECCOMP) sets SECCOMP_MODE_STRICT failed");
131*49cdfc7eSAndroid Build Coastguard Worker return;
132*49cdfc7eSAndroid Build Coastguard Worker }
133*49cdfc7eSAndroid Build Coastguard Worker
134*49cdfc7eSAndroid Build Coastguard Worker switch (val) {
135*49cdfc7eSAndroid Build Coastguard Worker case 1:
136*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS,
137*49cdfc7eSAndroid Build Coastguard Worker "prctl(PR_SET_SECCOMP) sets SECCOMP_MODE_STRICT succeed");
138*49cdfc7eSAndroid Build Coastguard Worker prctl(PR_GET_SECCOMP);
139*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "prctl(PR_GET_SECCOMP) succeed unexpectedly");
140*49cdfc7eSAndroid Build Coastguard Worker break;
141*49cdfc7eSAndroid Build Coastguard Worker case 2:
142*49cdfc7eSAndroid Build Coastguard Worker SAFE_WRITE(SAFE_WRITE_ALL, fd, "a", 1);
143*49cdfc7eSAndroid Build Coastguard Worker SAFE_READ(0, fd, buf, 1);
144*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS,
145*49cdfc7eSAndroid Build Coastguard Worker "SECCOMP_MODE_STRICT permits read(2) write(2) and _exit(2)");
146*49cdfc7eSAndroid Build Coastguard Worker break;
147*49cdfc7eSAndroid Build Coastguard Worker case 3:
148*49cdfc7eSAndroid Build Coastguard Worker close(fd);
149*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL,
150*49cdfc7eSAndroid Build Coastguard Worker "SECCOMP_MODE_STRICT permits close(2) unexpectdly");
151*49cdfc7eSAndroid Build Coastguard Worker break;
152*49cdfc7eSAndroid Build Coastguard Worker }
153*49cdfc7eSAndroid Build Coastguard Worker
154*49cdfc7eSAndroid Build Coastguard Worker tst_syscall(__NR_exit, 0);
155*49cdfc7eSAndroid Build Coastguard Worker }
156*49cdfc7eSAndroid Build Coastguard Worker
check_filter_mode(int val)157*49cdfc7eSAndroid Build Coastguard Worker static void check_filter_mode(int val)
158*49cdfc7eSAndroid Build Coastguard Worker {
159*49cdfc7eSAndroid Build Coastguard Worker int fd;
160*49cdfc7eSAndroid Build Coastguard Worker
161*49cdfc7eSAndroid Build Coastguard Worker if (mode_filter_not_supported == 1) {
162*49cdfc7eSAndroid Build Coastguard Worker tst_res(TCONF, "kernel doesn't support SECCOMP_MODE_FILTER");
163*49cdfc7eSAndroid Build Coastguard Worker return;
164*49cdfc7eSAndroid Build Coastguard Worker }
165*49cdfc7eSAndroid Build Coastguard Worker
166*49cdfc7eSAndroid Build Coastguard Worker fd = SAFE_OPEN(FNAME, O_RDWR | O_CREAT, 0666);
167*49cdfc7eSAndroid Build Coastguard Worker
168*49cdfc7eSAndroid Build Coastguard Worker TEST(prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &strict));
169*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET == -1) {
170*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TERRNO,
171*49cdfc7eSAndroid Build Coastguard Worker "prctl(PR_SET_SECCOMP) sets SECCOMP_MODE_FILTER failed");
172*49cdfc7eSAndroid Build Coastguard Worker return;
173*49cdfc7eSAndroid Build Coastguard Worker }
174*49cdfc7eSAndroid Build Coastguard Worker
175*49cdfc7eSAndroid Build Coastguard Worker switch (val) {
176*49cdfc7eSAndroid Build Coastguard Worker case 1:
177*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS,
178*49cdfc7eSAndroid Build Coastguard Worker "prctl(PR_SET_SECCOMP) sets SECCOMP_MODE_FILTER succeed");
179*49cdfc7eSAndroid Build Coastguard Worker prctl(PR_GET_SECCOMP);
180*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "prctl(PR_GET_SECCOMP) succeed unexpectedly");
181*49cdfc7eSAndroid Build Coastguard Worker break;
182*49cdfc7eSAndroid Build Coastguard Worker case 2:
183*49cdfc7eSAndroid Build Coastguard Worker close(fd);
184*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "SECCOMP_MODE_FILTER permits close(2)");
185*49cdfc7eSAndroid Build Coastguard Worker break;
186*49cdfc7eSAndroid Build Coastguard Worker case 3:
187*49cdfc7eSAndroid Build Coastguard Worker exit(0);
188*49cdfc7eSAndroid Build Coastguard Worker break;
189*49cdfc7eSAndroid Build Coastguard Worker case 4:
190*49cdfc7eSAndroid Build Coastguard Worker check_filter_mode_inherit();
191*49cdfc7eSAndroid Build Coastguard Worker break;
192*49cdfc7eSAndroid Build Coastguard Worker }
193*49cdfc7eSAndroid Build Coastguard Worker
194*49cdfc7eSAndroid Build Coastguard Worker tst_syscall(__NR_exit, 0);
195*49cdfc7eSAndroid Build Coastguard Worker }
196*49cdfc7eSAndroid Build Coastguard Worker
verify_prctl(unsigned int n)197*49cdfc7eSAndroid Build Coastguard Worker static void verify_prctl(unsigned int n)
198*49cdfc7eSAndroid Build Coastguard Worker {
199*49cdfc7eSAndroid Build Coastguard Worker int pid;
200*49cdfc7eSAndroid Build Coastguard Worker int status;
201*49cdfc7eSAndroid Build Coastguard Worker struct tcase *tc = &tcases[n];
202*49cdfc7eSAndroid Build Coastguard Worker
203*49cdfc7eSAndroid Build Coastguard Worker pid = SAFE_FORK();
204*49cdfc7eSAndroid Build Coastguard Worker if (pid == 0) {
205*49cdfc7eSAndroid Build Coastguard Worker tc->func_check(tc->val);
206*49cdfc7eSAndroid Build Coastguard Worker } else {
207*49cdfc7eSAndroid Build Coastguard Worker SAFE_WAITPID(pid, &status, 0);
208*49cdfc7eSAndroid Build Coastguard Worker if (WIFSIGNALED(status) && WTERMSIG(status) == tc->exp_signal) {
209*49cdfc7eSAndroid Build Coastguard Worker if (tc->pass_flag)
210*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "%s", tc->message);
211*49cdfc7eSAndroid Build Coastguard Worker else
212*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "%s", tc->message);
213*49cdfc7eSAndroid Build Coastguard Worker return;
214*49cdfc7eSAndroid Build Coastguard Worker }
215*49cdfc7eSAndroid Build Coastguard Worker
216*49cdfc7eSAndroid Build Coastguard Worker if (tc->pass_flag == 2 && mode_filter_not_supported == 0)
217*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL,
218*49cdfc7eSAndroid Build Coastguard Worker "SECCOMP_MODE_FILTER permits exit() unexpectedly");
219*49cdfc7eSAndroid Build Coastguard Worker }
220*49cdfc7eSAndroid Build Coastguard Worker }
221*49cdfc7eSAndroid Build Coastguard Worker
setup(void)222*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
223*49cdfc7eSAndroid Build Coastguard Worker {
224*49cdfc7eSAndroid Build Coastguard Worker TEST(prctl(PR_GET_SECCOMP));
225*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET == 0) {
226*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "kernel supports PR_GET/SET_SECCOMP");
227*49cdfc7eSAndroid Build Coastguard Worker
228*49cdfc7eSAndroid Build Coastguard Worker TEST(prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, NULL));
229*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET == -1 && TST_ERR == EINVAL) {
230*49cdfc7eSAndroid Build Coastguard Worker mode_filter_not_supported = 1;
231*49cdfc7eSAndroid Build Coastguard Worker return;
232*49cdfc7eSAndroid Build Coastguard Worker }
233*49cdfc7eSAndroid Build Coastguard Worker
234*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO, "kernel supports SECCOMP_MODE_FILTER");
235*49cdfc7eSAndroid Build Coastguard Worker return;
236*49cdfc7eSAndroid Build Coastguard Worker }
237*49cdfc7eSAndroid Build Coastguard Worker
238*49cdfc7eSAndroid Build Coastguard Worker if (TST_ERR == EINVAL)
239*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TCONF, "kernel doesn't support PR_GET/SET_SECCOMP");
240*49cdfc7eSAndroid Build Coastguard Worker
241*49cdfc7eSAndroid Build Coastguard Worker tst_brk(TBROK | TTERRNO,
242*49cdfc7eSAndroid Build Coastguard Worker "current environment doesn't permit PR_GET/SET_SECCOMP");
243*49cdfc7eSAndroid Build Coastguard Worker }
244*49cdfc7eSAndroid Build Coastguard Worker
245*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
246*49cdfc7eSAndroid Build Coastguard Worker .setup = setup,
247*49cdfc7eSAndroid Build Coastguard Worker .test = verify_prctl,
248*49cdfc7eSAndroid Build Coastguard Worker .tcnt = ARRAY_SIZE(tcases),
249*49cdfc7eSAndroid Build Coastguard Worker .forks_child = 1,
250*49cdfc7eSAndroid Build Coastguard Worker .needs_tmpdir = 1,
251*49cdfc7eSAndroid Build Coastguard Worker .needs_root = 1,
252*49cdfc7eSAndroid Build Coastguard Worker };
253