blob: f8d1370e16790f5193d85b730ff1dbf93d2abbfa [file] [log] [blame]
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00001/*
Dmitry V. Levinc497c962016-01-05 23:17:29 +00002 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00003 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
Dmitry V. Levin0c8853c2016-01-02 13:28:43 +000028#include "tests.h"
Dmitry V. Levin485f8fb2015-03-19 00:40:49 +000029#include <stddef.h>
30#include <unistd.h>
31#include <stdio.h>
32#include <errno.h>
33#include <sys/syscall.h>
34
35#ifdef HAVE_PRCTL
36# include <sys/prctl.h>
37#endif
38#ifdef HAVE_LINUX_SECCOMP_H
39# include <linux/seccomp.h>
40#endif
41#ifdef HAVE_LINUX_FILTER_H
42# include <linux/filter.h>
43#endif
44
45#if defined HAVE_PRCTL \
46 && defined PR_SET_NO_NEW_PRIVS \
47 && defined PR_SET_SECCOMP \
48 && defined SECCOMP_MODE_FILTER \
49 && defined SECCOMP_RET_ERRNO \
50 && defined BPF_JUMP \
51 && defined BPF_STMT
52
53#define SOCK_FILTER_ALLOW_SYSCALL(nr) \
54 BPF_JUMP(BPF_JMP | BPF_K | BPF_JEQ, __NR_ ## nr, 0, 1), \
55 BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW)
56
57#define SOCK_FILTER_DENY_SYSCALL(nr, err) \
58 BPF_JUMP(BPF_JMP | BPF_K | BPF_JEQ, __NR_ ## nr, 0, 1), \
59 BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ERRNO | (SECCOMP_RET_DATA & (err)))
60
61#define SOCK_FILTER_KILL_PROCESS \
62 BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_KILL)
63
64#define PRINT_ALLOW_SYSCALL(nr) \
65 printf("BPF_JUMP(BPF_JMP | BPF_K | BPF_JEQ, %#x, 0, 0x1), " \
66 "BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ALLOW), ", \
67 __NR_ ## nr)
68
69#define PRINT_DENY_SYSCALL(nr, err) \
70 printf("BPF_JUMP(BPF_JMP | BPF_K | BPF_JEQ, %#x, 0, 0x1), " \
71 "BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_ERRNO | %#x), ", \
72 __NR_ ## nr, err)
73
74static const struct sock_filter filter[] = {
75 /* load syscall number */
76 BPF_STMT(BPF_LD | BPF_W | BPF_ABS, offsetof(struct seccomp_data, nr)),
77
78 /* allow syscalls */
79 SOCK_FILTER_ALLOW_SYSCALL(close),
80 SOCK_FILTER_ALLOW_SYSCALL(exit),
81 SOCK_FILTER_ALLOW_SYSCALL(exit_group),
82
83 /* deny syscalls */
84 SOCK_FILTER_DENY_SYSCALL(sync, EBUSY),
85 SOCK_FILTER_DENY_SYSCALL(setsid, EPERM),
86
87 /* kill process */
88 SOCK_FILTER_KILL_PROCESS
89};
90
91static const struct sock_fprog prog = {
92 .len = sizeof(filter) / sizeof(filter[0]),
93 .filter = (struct sock_filter *) filter,
94};
95
96int
97main(void)
98{
99 int fds[2];
100
101 puts("prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) = 0");
102
103 printf("prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, [");
104
105 printf("BPF_STMT(BPF_LD | BPF_W | BPF_ABS, %#x), ",
106 (unsigned) offsetof(struct seccomp_data, nr));
107
108 PRINT_ALLOW_SYSCALL(close);
109 PRINT_ALLOW_SYSCALL(exit);
110 PRINT_ALLOW_SYSCALL(exit_group);
111
112 PRINT_DENY_SYSCALL(sync, EBUSY),
113 PRINT_DENY_SYSCALL(setsid, EPERM),
114
115 printf("BPF_STMT(BPF_RET | BPF_K, SECCOMP_RET_KILL)");
116
117 puts("]) = 0");
118 puts("+++ exited with 0 +++");
119
120 fflush(stdout);
121 close(0);
122 close(1);
123
Dmitry V. Levinc497c962016-01-05 23:17:29 +0000124 if (pipe(fds))
125 perror_msg_and_fail("pipe");
126 if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0))
127 perror_msg_and_skip("PR_SET_NO_NEW_PRIVS");
128 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog))
129 perror_msg_and_skip("PR_SET_SECCOMP");
130 if (close(0) || close(1))
Dmitry V. Levin485f8fb2015-03-19 00:40:49 +0000131 _exit(77);
132
133 _exit(0);
134}
135
136#else
137
Dmitry V. Levinc497c962016-01-05 23:17:29 +0000138SKIP_MAIN_UNDEFINED("HAVE_PRCTL && PR_SET_NO_NEW_PRIVS && PR_SET_SECCOMP"
139 " && SECCOMP_MODE_FILTER && SECCOMP_RET_ERRNO"
140 " && BPF_JUMP && BPF_STMT")
Dmitry V. Levin485f8fb2015-03-19 00:40:49 +0000141
142#endif