upgraded: improve error handling
[project/procd.git] / jail / seccomp.c
1 /*
2  * seccomp example with syscall reporting
3  *
4  * Copyright (c) 2012 The Chromium OS Authors <chromium-os-dev@chromium.org>
5  * Authors:
6  *  Kees Cook <keescook@chromium.org>
7  *  Will Drewry <wad@chromium.org>
8  *
9  * Use of this source code is governed by a BSD-style license that can be
10  * found in the LICENSE file.
11  */
12 #define _GNU_SOURCE 1
13 #include <stddef.h>
14 #include <stdlib.h>
15 #include <unistd.h>
16
17 #include <libubox/utils.h>
18 #include <libubox/blobmsg.h>
19 #include <libubox/blobmsg_json.h>
20
21 #include "seccomp-bpf.h"
22 #include "seccomp.h"
23 #include "../syscall-names.h"
24
25 static int max_syscall = ARRAY_SIZE(syscall_names);
26
27 static int find_syscall(const char *name)
28 {
29         int i;
30
31         for (i = 0; i < max_syscall; i++)
32                 if (syscall_names[i] && !strcmp(syscall_names[i], name))
33                         return i;
34
35         return -1;
36 }
37
38 static void set_filter(struct sock_filter *filter, __u16 code, __u8 jt, __u8 jf, __u32 k)
39 {
40         filter->code = code;
41         filter->jt = jt;
42         filter->jf = jf;
43         filter->k = k;
44 }
45
46 int install_syscall_filter(const char *argv, const char *file)
47 {
48         enum {
49                 SECCOMP_WHITELIST,
50                 SECCOMP_POLICY,
51                 __SECCOMP_MAX
52         };
53         static const struct blobmsg_policy policy[__SECCOMP_MAX] = {
54                 [SECCOMP_WHITELIST] = { .name = "whitelist", .type = BLOBMSG_TYPE_ARRAY },
55                 [SECCOMP_POLICY] = { .name = "policy", .type = BLOBMSG_TYPE_INT32 },
56         };
57         struct blob_buf b = { 0 };
58         struct blob_attr *tb[__SECCOMP_MAX];
59         struct blob_attr *cur;
60         int rem;
61
62         struct sock_filter *filter;
63         struct sock_fprog prog = { 0 };
64         int sz = 5, idx = 0, default_policy = 0;
65
66         INFO("%s: setting up syscall filter\n", argv);
67
68         blob_buf_init(&b, 0);
69         if (!blobmsg_add_json_from_file(&b, file)) {
70                 INFO("%s: failed to load %s\n", argv, file);
71                 return -1;
72         }
73
74         blobmsg_parse(policy, __SECCOMP_MAX, tb, blob_data(b.head), blob_len(b.head));
75         if (!tb[SECCOMP_WHITELIST]) {
76                 INFO("%s: %s is missing the syscall table\n", argv, file);
77                 return -1;
78         }
79
80         if (tb[SECCOMP_POLICY])
81                 default_policy = blobmsg_get_u32(tb[SECCOMP_POLICY]);
82
83         blobmsg_for_each_attr(cur, tb[SECCOMP_WHITELIST], rem)
84                 sz += 2;
85
86         filter = calloc(sz, sizeof(struct sock_filter));
87         if (!filter) {
88                 INFO("failed to allocate filter memory\n");
89                 return -1;
90         }
91
92         /* validate arch */
93         set_filter(&filter[idx++], BPF_LD + BPF_W + BPF_ABS, 0, 0, arch_nr);
94         set_filter(&filter[idx++], BPF_JMP + BPF_JEQ + BPF_K, 1, 0, ARCH_NR);
95         set_filter(&filter[idx++], BPF_RET + BPF_K, 0, 0, SECCOMP_RET_KILL);
96
97         /* get syscall */
98         set_filter(&filter[idx++], BPF_LD + BPF_W + BPF_ABS, 0, 0, syscall_nr);
99
100         blobmsg_for_each_attr(cur, tb[SECCOMP_WHITELIST], rem) {
101                 char *name = blobmsg_get_string(cur);
102                 int nr;
103
104                 if (!name) {
105                         INFO("%s: invalid syscall name\n", argv);
106                         continue;
107                 }
108
109                 nr  = find_syscall(name);
110                 if (nr == -1) {
111                         INFO("%s: unknown syscall %s\n", argv, name);
112                         continue;
113                 }
114
115                 /* add whitelist */
116                 set_filter(&filter[idx++], BPF_JMP + BPF_JEQ + BPF_K, 0, 1, nr);
117                 set_filter(&filter[idx++], BPF_RET + BPF_K, 0, 0, SECCOMP_RET_ALLOW);
118         }
119
120         if (default_policy)
121                 /* return -1 and set errno */
122                 set_filter(&filter[idx], BPF_RET + BPF_K, 0, 0, SECCOMP_RET_LOGGER(default_policy));
123         else
124                 /* kill the process */
125                 set_filter(&filter[idx], BPF_RET + BPF_K, 0, 0, SECCOMP_RET_KILL);
126
127         if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0)) {
128                 INFO("%s: prctl(PR_SET_NO_NEW_PRIVS) failed: %s\n", argv, strerror(errno));
129                 return errno;
130         }
131
132         prog.len = (unsigned short) idx + 1;
133         prog.filter = filter;
134
135         if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog)) {
136                 INFO("%s: prctl(PR_SET_SECCOMP) failed: %s\n", argv, strerror(errno));
137                 return errno;
138         }
139         return 0;
140 }