|
38 | 38 | #include <linux/seccomp.h>
|
39 | 39 | #include <linux/filter.h>
|
40 | 40 | #include <sys/prctl.h>
|
| 41 | +#include <sys/syscall.h> |
| 42 | + |
| 43 | +static int |
| 44 | +syscall_seccomp (unsigned int operation, unsigned int flags, void *args) |
| 45 | +{ |
| 46 | + return (int) syscall (__NR_seccomp, operation, flags, args); |
| 47 | +} |
41 | 48 |
|
42 | 49 | unsigned long
|
43 | 50 | get_seccomp_operator (const char *name, libcrun_error_t *err)
|
@@ -99,32 +106,59 @@ cleanup_seccompp (void *p)
|
99 | 106 | #define cleanup_seccomp __attribute__((cleanup (cleanup_seccompp)))
|
100 | 107 |
|
101 | 108 | int
|
102 |
| -libcrun_apply_seccomp (int infd, libcrun_error_t *err) |
| 109 | +libcrun_apply_seccomp (int infd, char **seccomp_flags, size_t seccomp_flags_len, libcrun_error_t *err) |
103 | 110 | {
|
104 | 111 | int ret;
|
105 | 112 | struct sock_fprog seccomp_filter;
|
106 | 113 | cleanup_free char *bpf = NULL;
|
| 114 | + unsigned int flags = 0; |
107 | 115 | size_t len;
|
108 | 116 |
|
109 | 117 | if (infd < 0)
|
110 | 118 | return 0;
|
111 | 119 |
|
| 120 | + |
| 121 | + /* if no seccomp flag was specified use a sane default. */ |
| 122 | + if (seccomp_flags == NULL) |
| 123 | + flags = SECCOMP_FILTER_FLAG_LOG|SECCOMP_FILTER_FLAG_SPEC_ALLOW; |
| 124 | + else |
| 125 | + { |
| 126 | + size_t i = 0; |
| 127 | + for (i = 0; i < seccomp_flags_len; i++) |
| 128 | + { |
| 129 | + if (strcmp (seccomp_flags[i], "SECCOMP_FILTER_FLAG_TSYNC") == 0) |
| 130 | + flags |= SECCOMP_FILTER_FLAG_TSYNC; |
| 131 | + else if (strcmp (seccomp_flags[i], "SECCOMP_FILTER_FLAG_SPEC_ALLOW") == 0) |
| 132 | + flags |= SECCOMP_FILTER_FLAG_SPEC_ALLOW; |
| 133 | + else if (strcmp (seccomp_flags[i], "SECCOMP_FILTER_FLAG_LOG") == 0) |
| 134 | + flags |= SECCOMP_FILTER_FLAG_LOG; |
| 135 | + else |
| 136 | + return crun_make_error (err, 0, "unknown seccomp option %s", seccomp_flags[i]); |
| 137 | + } |
| 138 | + } |
| 139 | + |
112 | 140 | ret = read_all_fd (infd, "seccomp.bpf", &bpf, &len, err);
|
113 | 141 | if (UNLIKELY (ret < 0))
|
114 | 142 | return ret;
|
115 | 143 |
|
116 | 144 | seccomp_filter.len = len / 8;
|
117 | 145 | seccomp_filter.filter = (struct sock_filter *) bpf;
|
118 | 146 |
|
119 |
| - ret = prctl (PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &seccomp_filter); |
| 147 | + ret = syscall_seccomp (SECCOMP_SET_MODE_FILTER, flags, &seccomp_filter); |
120 | 148 | if (UNLIKELY (ret < 0))
|
121 |
| - return crun_make_error (err, errno, "prctl (PR_SET_SECCOMP)"); |
| 149 | + { |
| 150 | + /* If any of the flags is not supported, try again without specifying them: */ |
| 151 | + if (errno == EINVAL) |
| 152 | + ret = syscall_seccomp (SECCOMP_SET_MODE_FILTER, 0, &seccomp_filter); |
| 153 | + if (UNLIKELY (ret < 0)) |
| 154 | + return crun_make_error (err, errno, "seccomp (SECCOMP_SET_MODE_FILTER)"); |
| 155 | + } |
122 | 156 |
|
123 | 157 | return 0;
|
124 | 158 | }
|
125 | 159 |
|
126 | 160 | int
|
127 |
| -libcrun_generate_and_load_seccomp (libcrun_container_t *container, int outfd, libcrun_error_t *err) |
| 161 | +libcrun_generate_and_load_seccomp (libcrun_container_t *container, int outfd, char **flags, size_t flags_len, libcrun_error_t *err) |
128 | 162 | {
|
129 | 163 | oci_container_linux_seccomp *seccomp = container->container_def->linux->seccomp;
|
130 | 164 | int ret;
|
@@ -233,8 +267,8 @@ libcrun_generate_and_load_seccomp (libcrun_container_t *container, int outfd, li
|
233 | 267 | return crun_make_error (err, 0, "seccomp_export_bpf");
|
234 | 268 | }
|
235 | 269 |
|
236 |
| - if (lseek (outfd, 0, SEEK_SET) == (off_t) -1) |
| 270 | + if (UNLIKELY (lseek (outfd, 0, SEEK_SET) == (off_t) -1)) |
237 | 271 | return crun_make_error (err, 0, "lseek");
|
238 | 272 |
|
239 |
| - return libcrun_apply_seccomp (outfd, err); |
| 273 | + return libcrun_apply_seccomp (outfd, flags, flags_len, err); |
240 | 274 | }
|
0 commit comments