blob: 10e14997524082c29c9c01a21b955951682b1ce4 [file] [log] [blame]
Mike Frysinger50e31fa2018-01-19 18:59:49 -05001/* Copyright 2017 The Chromium OS Authors. All rights reserved.
2 * Use of this source code is governed by a BSD-style license that can be
3 * found in the LICENSE file.
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -04004 */
5
6#include "system.h"
7
8#include <errno.h>
9#include <fcntl.h>
Luis Hector Chavez71323552017-09-05 09:17:22 -070010#include <grp.h>
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040011#include <net/if.h>
Luis Hector Chavez71323552017-09-05 09:17:22 -070012#include <pwd.h>
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040013#include <stdbool.h>
14#include <stdio.h>
15#include <string.h>
16#include <sys/ioctl.h>
17#include <sys/prctl.h>
18#include <sys/socket.h>
19#include <sys/stat.h>
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -070020#include <sys/statvfs.h>
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040021#include <unistd.h>
22
Mattias Nisslere5200192018-10-18 12:29:40 +020023#include <linux/securebits.h>
24
Luis Héctor Chávez01b628c2021-01-03 05:46:57 -080025#include "syscall_wrapper.h"
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040026#include "util.h"
27
Mattias Nisslere5200192018-10-18 12:29:40 +020028/*
29 * SECBIT_NO_CAP_AMBIENT_RAISE was added in kernel 4.3, so fill in the
30 * definition if the securebits header doesn't provide it.
31 */
32#ifndef SECBIT_NO_CAP_AMBIENT_RAISE
33#define SECBIT_NO_CAP_AMBIENT_RAISE (issecure_mask(6))
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040034#endif
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -040035
Mattias Nisslere5200192018-10-18 12:29:40 +020036#ifndef SECBIT_NO_CAP_AMBIENT_RAISE_LOCKED
37#define SECBIT_NO_CAP_AMBIENT_RAISE_LOCKED (issecure_mask(7))
38#endif
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040039
40/*
41 * Assert the value of SECURE_ALL_BITS at compile-time.
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -040042 * Android devices are currently compiled against 4.4 kernel headers. Kernel 4.3
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040043 * added a new securebit.
44 * When a new securebit is added, the new SECURE_ALL_BITS mask will return EPERM
45 * when used on older kernels. The compile-time assert will catch this situation
46 * at compile time.
47 */
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -040048#if defined(__ANDROID__)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040049_Static_assert(SECURE_ALL_BITS == 0x55, "SECURE_ALL_BITS == 0x55.");
50#endif
51
Stéphane Lesimple84ffabc2020-02-14 20:33:34 +010052/* Used by lookup_(user|group) functions. */
53#define MAX_PWENT_SZ (1 << 20)
54#define MAX_GRENT_SZ (1 << 20)
55
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -040056int secure_noroot_set_and_locked(uint64_t mask)
57{
58 return (mask & (SECBIT_NOROOT | SECBIT_NOROOT_LOCKED)) ==
59 (SECBIT_NOROOT | SECBIT_NOROOT_LOCKED);
60}
61
Mattias Nissler48b5ff12018-10-11 15:31:41 +020062int lock_securebits(uint64_t skip_mask, bool require_keep_caps)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040063{
Mattias Nissler48b5ff12018-10-11 15:31:41 +020064 /* The general idea is to set all bits, subject to exceptions below. */
65 unsigned long securebits = SECURE_ALL_BITS | SECURE_ALL_LOCKS;
66
67 /*
68 * SECBIT_KEEP_CAPS is special in that it is automatically cleared on
69 * execve(2). This implies that attempts to set SECBIT_KEEP_CAPS (as is
70 * the default) in processes that have it locked already (such as nested
71 * minijail usage) would fail. Thus, unless the caller requires it,
72 * allow it to remain off if it is already locked.
73 */
74 if (!require_keep_caps) {
75 int current_securebits = prctl(PR_GET_SECUREBITS);
76 if (current_securebits < 0) {
77 pwarn("prctl(PR_GET_SECUREBITS) failed");
78 return -1;
79 }
80
81 if ((current_securebits & SECBIT_KEEP_CAPS_LOCKED) != 0 &&
82 (current_securebits & SECBIT_KEEP_CAPS) == 0) {
83 securebits &= ~SECBIT_KEEP_CAPS;
84 }
85 }
86
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040087 /*
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -040088 * Ambient capabilities can only be raised if they're already present
89 * in the permitted *and* inheritable set. Therefore, we don't really
90 * need to lock the NO_CAP_AMBIENT_RAISE securebit, since we are already
91 * configuring the permitted and inheritable set.
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -040092 */
Mattias Nissler48b5ff12018-10-11 15:31:41 +020093 securebits &=
94 ~(SECBIT_NO_CAP_AMBIENT_RAISE | SECBIT_NO_CAP_AMBIENT_RAISE_LOCKED);
95
96 /* Don't set any bits that the user requested not to be touched. */
97 securebits &= ~skip_mask;
98
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -070099 if (!securebits) {
Jorge Lucangeli Obes54234212018-04-26 11:52:15 -0400100 warn("not locking any securebits");
Luis Hector Chavezec0a2c12017-06-29 20:29:57 -0700101 return 0;
102 }
103 int securebits_ret = prctl(PR_SET_SECUREBITS, securebits);
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400104 if (securebits_ret < 0) {
105 pwarn("prctl(PR_SET_SECUREBITS) failed");
106 return -1;
107 }
108
109 return 0;
110}
111
112int write_proc_file(pid_t pid, const char *content, const char *basename)
113{
114 int fd, ret;
115 size_t sz, len;
116 ssize_t written;
117 char filename[32];
118
119 sz = sizeof(filename);
120 ret = snprintf(filename, sz, "/proc/%d/%s", pid, basename);
121 if (ret < 0 || (size_t)ret >= sz) {
122 warn("failed to generate %s filename", basename);
123 return -1;
124 }
125
126 fd = open(filename, O_WRONLY | O_CLOEXEC);
127 if (fd < 0) {
128 pwarn("failed to open '%s'", filename);
129 return -errno;
130 }
131
132 len = strlen(content);
133 written = write(fd, content, len);
134 if (written < 0) {
135 pwarn("failed to write '%s'", filename);
Jorge Lucangeli Obes673c89d2018-10-04 16:08:10 -0400136 return -errno;
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400137 }
138
139 if ((size_t)written < len) {
140 warn("failed to write %zu bytes to '%s'", len, filename);
141 return -1;
142 }
143 close(fd);
144 return 0;
145}
146
147/*
148 * We specifically do not use cap_valid() as that only tells us the last
149 * valid cap we were *compiled* against (i.e. what the version of kernel
150 * headers says). If we run on a different kernel version, then it's not
151 * uncommon for that to be less (if an older kernel) or more (if a newer
152 * kernel).
153 * Normally, we suck up the answer via /proc. On Android, not all processes are
154 * guaranteed to be able to access '/proc/sys/kernel/cap_last_cap' so we
155 * programmatically find the value by calling prctl(PR_CAPBSET_READ).
156 */
157unsigned int get_last_valid_cap(void)
158{
159 unsigned int last_valid_cap = 0;
160 if (is_android()) {
161 for (; prctl(PR_CAPBSET_READ, last_valid_cap, 0, 0, 0) >= 0;
162 ++last_valid_cap)
163 ;
164
165 /* |last_valid_cap| will be the first failing value. */
166 if (last_valid_cap > 0) {
167 last_valid_cap--;
168 }
169 } else {
170 const char cap_file[] = "/proc/sys/kernel/cap_last_cap";
171 FILE *fp = fopen(cap_file, "re");
172 if (fscanf(fp, "%u", &last_valid_cap) != 1)
173 pdie("fscanf(%s)", cap_file);
174 fclose(fp);
175 }
176 return last_valid_cap;
177}
178
Jorge Lucangeli Obesa6eb21a2017-04-20 10:44:00 -0400179int cap_ambient_supported(void)
180{
181 return prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, CAP_CHOWN, 0, 0) >=
182 0;
183}
184
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400185int config_net_loopback(void)
186{
187 const char ifname[] = "lo";
188 int sock;
189 struct ifreq ifr;
190
191 /* Make sure people don't try to add really long names. */
192 _Static_assert(sizeof(ifname) <= IFNAMSIZ, "interface name too long");
193
194 sock = socket(AF_LOCAL, SOCK_DGRAM | SOCK_CLOEXEC, 0);
195 if (sock < 0) {
196 pwarn("socket(AF_LOCAL) failed");
197 return -1;
198 }
199
200 /*
201 * Do the equiv of `ip link set up lo`. The kernel will assign
202 * IPv4 (127.0.0.1) & IPv6 (::1) addresses automatically!
203 */
204 strcpy(ifr.ifr_name, ifname);
205 if (ioctl(sock, SIOCGIFFLAGS, &ifr) < 0) {
206 pwarn("ioctl(SIOCGIFFLAGS) failed");
207 return -1;
208 }
209
210 /* The kernel preserves ifr.ifr_name for use. */
211 ifr.ifr_flags |= IFF_UP | IFF_RUNNING;
212 if (ioctl(sock, SIOCSIFFLAGS, &ifr) < 0) {
213 pwarn("ioctl(SIOCSIFFLAGS) failed");
214 return -1;
215 }
216
217 close(sock);
218 return 0;
219}
220
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400221int write_pid_to_path(pid_t pid, const char *path)
222{
Jorge Lucangeli Obes1f5d0952019-06-04 09:18:26 -0400223 FILE *fp = fopen(path, "we");
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400224
225 if (!fp) {
Jorge Lucangeli Obes1f5d0952019-06-04 09:18:26 -0400226 pwarn("failed to open '%s'", path);
227 return -errno;
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400228 }
229 if (fprintf(fp, "%d\n", (int)pid) < 0) {
230 /* fprintf(3) does not set errno on failure. */
231 warn("fprintf(%s) failed", path);
Jorge Lucangeli Obes1f5d0952019-06-04 09:18:26 -0400232 return -1;
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400233 }
234 if (fclose(fp)) {
235 pwarn("fclose(%s) failed", path);
Jorge Lucangeli Obes1f5d0952019-06-04 09:18:26 -0400236 return -errno;
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400237 }
238
Jorge Lucangeli Obes1f5d0952019-06-04 09:18:26 -0400239 return 0;
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400240}
241
242/*
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500243 * Create the |path| directory and its parents (if need be) with |mode|.
244 * If not |isdir|, then |path| is actually a file, so the last component
245 * will not be created.
246 */
247int mkdir_p(const char *path, mode_t mode, bool isdir)
248{
yusukes059e0bd2018-03-05 10:22:16 -0800249 int rc;
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500250 char *dir = strdup(path);
yusukes059e0bd2018-03-05 10:22:16 -0800251 if (!dir) {
252 rc = errno;
253 pwarn("strdup(%s) failed", path);
254 return -rc;
255 }
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500256
257 /* Starting from the root, work our way out to the end. */
258 char *p = strchr(dir + 1, '/');
259 while (p) {
260 *p = '\0';
261 if (mkdir(dir, mode) && errno != EEXIST) {
yusukes059e0bd2018-03-05 10:22:16 -0800262 rc = errno;
263 pwarn("mkdir(%s, 0%o) failed", dir, mode);
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500264 free(dir);
yusukes059e0bd2018-03-05 10:22:16 -0800265 return -rc;
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500266 }
267 *p = '/';
268 p = strchr(p + 1, '/');
269 }
270
271 /*
272 * Create the last directory. We still check EEXIST here in case
273 * of trailing slashes.
274 */
275 free(dir);
yusukes059e0bd2018-03-05 10:22:16 -0800276 if (isdir && mkdir(path, mode) && errno != EEXIST) {
277 rc = errno;
278 pwarn("mkdir(%s, 0%o) failed", path, mode);
279 return -rc;
280 }
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500281 return 0;
282}
283
284/*
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400285 * setup_mount_destination: Ensures the mount target exists.
286 * Creates it if needed and possible.
287 */
288int setup_mount_destination(const char *source, const char *dest, uid_t uid,
Luis Hector Chavez0bacbf82018-07-10 20:06:55 -0700289 uid_t gid, bool bind, unsigned long *mnt_flags)
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400290{
291 int rc;
292 struct stat st_buf;
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -0400293 bool domkdir;
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400294
295 rc = stat(dest, &st_buf);
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -0400296 if (rc == 0) /* destination exists */
Jorge Lucangeli Obesb4b7c5a2019-09-09 10:47:36 -0400297 return 0;
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400298
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -0400299 /*
300 * Try to create the destination.
301 * Either make a directory or touch a file depending on the source type.
302 *
303 * If the source isn't an absolute path, assume it is a filesystem type
304 * such as "tmpfs" and create a directory to mount it on. The dest will
305 * be something like "none" or "proc" which we shouldn't be checking.
306 */
307 if (source[0] == '/') {
308 /* The source is an absolute path -- it better exist! */
309 rc = stat(source, &st_buf);
310 if (rc) {
Jorge Lucangeli Obes9299cae2019-08-23 11:28:39 -0400311 rc = errno;
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -0400312 pwarn("stat(%s) failed", source);
Jorge Lucangeli Obes9299cae2019-08-23 11:28:39 -0400313 return -rc;
314 }
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -0400315
316 /*
317 * If bind mounting, we only create a directory if the source
318 * is a directory, else we always bind mount it as a file to
319 * support device nodes, sockets, etc...
320 *
321 * For all other mounts, we assume a block/char source is
322 * going to want a directory to mount to. If the source is
323 * something else (e.g. a fifo or socket), this probably will
324 * not do the right thing, but we'll fail later on when we try
325 * to mount(), so shouldn't be a big deal.
326 */
327 domkdir = S_ISDIR(st_buf.st_mode) ||
328 (!bind && (S_ISBLK(st_buf.st_mode) ||
329 S_ISCHR(st_buf.st_mode)));
330
Jorge Lucangeli Obesb4b7c5a2019-09-09 10:47:36 -0400331 /* If bind mounting, also grab the mount flags of the source. */
332 if (bind && mnt_flags) {
333 struct statvfs stvfs_buf;
334 rc = statvfs(source, &stvfs_buf);
335 if (rc) {
336 rc = errno;
337 pwarn(
338 "failed to look up mount flags: source=%s",
339 source);
340 return -rc;
341 }
342 *mnt_flags = stvfs_buf.f_flag;
343 }
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -0400344 } else {
345 /* The source is a relative path -- assume it's a pseudo fs. */
346
347 /* Disallow relative bind mounts. */
348 if (bind) {
349 warn("relative bind-mounts are not allowed: source=%s",
350 source);
351 return -EINVAL;
352 }
353
354 domkdir = true;
Mike Frysingereaab4202017-08-14 14:57:21 -0400355 }
356
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500357 /*
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -0400358 * Now that we know what we want to do, do it!
359 * We always create the intermediate dirs and the final path with 0755
360 * perms and root/root ownership. This shouldn't be a problem because
361 * the actual mount will set those perms/ownership on the mount point
362 * which is all people should need to access it.
Mike Frysinger5fdba4e2018-01-17 15:39:48 -0500363 */
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -0400364 rc = mkdir_p(dest, 0755, domkdir);
365 if (rc)
366 return rc;
367 if (!domkdir) {
368 int fd = open(dest, O_RDWR | O_CREAT | O_CLOEXEC, 0700);
369 if (fd < 0) {
370 rc = errno;
371 pwarn("open(%s) failed", dest);
372 return -rc;
373 }
374 close(fd);
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400375 }
Jorge Lucangeli Obes7654c6e2019-09-09 10:45:38 -0400376 if (chown(dest, uid, gid)) {
377 rc = errno;
378 pwarn("chown(%s, %u, %u) failed", dest, uid, gid);
379 return -rc;
380 }
Jorge Lucangeli Obesb4b7c5a2019-09-09 10:47:36 -0400381 return 0;
Jorge Lucangeli Obes0b208772017-04-19 14:15:46 -0400382}
Luis Hector Chavez71323552017-09-05 09:17:22 -0700383
384/*
385 * lookup_user: Gets the uid/gid for the given username.
386 */
387int lookup_user(const char *user, uid_t *uid, gid_t *gid)
388{
389 char *buf = NULL;
390 struct passwd pw;
391 struct passwd *ppw = NULL;
Stéphane Lesimple84ffabc2020-02-14 20:33:34 +0100392 /*
393 * sysconf(_SC_GETPW_R_SIZE_MAX), under glibc, is documented to return
394 * a suggested starting size for the buffer, so let's try getting this
395 * size first, and fallback to a default othersise.
396 */
Luis Hector Chavez71323552017-09-05 09:17:22 -0700397 ssize_t sz = sysconf(_SC_GETPW_R_SIZE_MAX);
398 if (sz == -1)
399 sz = 65536; /* your guess is as good as mine... */
400
Stéphane Lesimple84ffabc2020-02-14 20:33:34 +0100401 do {
402 buf = malloc(sz);
403 if (!buf)
404 return -ENOMEM;
405 int err = getpwnam_r(user, &pw, buf, sz, &ppw);
406 /*
407 * We're safe to free the buffer here. The strings inside |pw|
408 * point inside |buf|, but we don't use any of them; this leaves
409 * the pointers dangling but it's safe.
410 * |ppw| points at |pw| if getpwnam_r(3) succeeded.
411 */
412 free(buf);
413 if (err == ERANGE) {
414 /* |buf| was too small, retry with a bigger one. */
415 sz <<= 1;
416 } else if (err != 0) {
417 /* We got an error not related to the size of |buf|. */
418 return -err;
419 } else if (!ppw) {
420 /* Not found. */
421 return -ENOENT;
422 } else {
423 *uid = ppw->pw_uid;
424 *gid = ppw->pw_gid;
425 return 0;
426 }
427 } while (sz <= MAX_PWENT_SZ);
Mattias Nissler160d58f2020-02-25 11:01:30 +0100428
Stéphane Lesimple84ffabc2020-02-14 20:33:34 +0100429 /* A buffer of size MAX_PWENT_SZ is still too small, return an error. */
430 return -ERANGE;
Luis Hector Chavez71323552017-09-05 09:17:22 -0700431}
432
433/*
434 * lookup_group: Gets the gid for the given group name.
435 */
436int lookup_group(const char *group, gid_t *gid)
437{
438 char *buf = NULL;
439 struct group gr;
440 struct group *pgr = NULL;
Stéphane Lesimple84ffabc2020-02-14 20:33:34 +0100441 /*
442 * sysconf(_SC_GETGR_R_SIZE_MAX), under glibc, is documented to return
443 * a suggested starting size for the buffer, so let's try getting this
444 * size first, and fallback to a default otherwise.
445 */
Luis Hector Chavez71323552017-09-05 09:17:22 -0700446 ssize_t sz = sysconf(_SC_GETGR_R_SIZE_MAX);
447 if (sz == -1)
448 sz = 65536; /* and mine is as good as yours, really */
449
Stéphane Lesimple84ffabc2020-02-14 20:33:34 +0100450 do {
451 buf = malloc(sz);
452 if (!buf)
453 return -ENOMEM;
454 int err = getgrnam_r(group, &gr, buf, sz, &pgr);
455 /*
456 * We're safe to free the buffer here. The strings inside |gr|
457 * point inside |buf|, but we don't use any of them; this leaves
458 * the pointers dangling but it's safe.
459 * |pgr| points at |gr| if getgrnam_r(3) succeeded.
460 */
461 free(buf);
462 if (err == ERANGE) {
463 /* |buf| was too small, retry with a bigger one. */
464 sz <<= 1;
465 } else if (err != 0) {
466 /* We got an error not related to the size of |buf|. */
467 return -err;
468 } else if (!pgr) {
469 /* Not found. */
470 return -ENOENT;
471 } else {
472 *gid = pgr->gr_gid;
473 return 0;
474 }
475 } while (sz <= MAX_GRENT_SZ);
Mattias Nissler160d58f2020-02-25 11:01:30 +0100476
Stéphane Lesimple84ffabc2020-02-14 20:33:34 +0100477 /* A buffer of size MAX_GRENT_SZ is still too small, return an error. */
478 return -ERANGE;
Luis Hector Chavez71323552017-09-05 09:17:22 -0700479}
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400480
Luis Héctor Chávez4baeafa2021-01-03 05:47:13 -0800481static bool seccomp_action_is_available(const char *wanted)
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400482{
483 if (is_android()) {
484 /*
485 * Accessing |actions_avail| is generating SELinux denials, so
486 * skip for now.
487 * TODO(crbug.com/978022, jorgelo): Remove once the denial is
488 * fixed.
489 */
Luis Héctor Chávez4baeafa2021-01-03 05:47:13 -0800490 return false;
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400491 }
492 const char actions_avail_path[] =
493 "/proc/sys/kernel/seccomp/actions_avail";
494 FILE *f = fopen(actions_avail_path, "re");
495
496 if (!f) {
497 pwarn("fopen(%s) failed", actions_avail_path);
Luis Héctor Chávez4baeafa2021-01-03 05:47:13 -0800498 return false;
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400499 }
500
501 char *actions_avail = NULL;
502 size_t buf_size = 0;
503 if (getline(&actions_avail, &buf_size, f) < 0) {
504 pwarn("getline() failed");
505 free(actions_avail);
Luis Héctor Chávez4baeafa2021-01-03 05:47:13 -0800506 return false;
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400507 }
508
509 /*
510 * This is just substring search, which means that partial matches will
511 * match too (e.g. "action" would match "longaction"). There are no
512 * seccomp actions which include other actions though, so we're good for
513 * now. Eventually we might want to split the string by spaces.
514 */
Luis Héctor Chávez4baeafa2021-01-03 05:47:13 -0800515 bool available = strstr(actions_avail, wanted) != NULL;
516 free(actions_avail);
517 return available;
Jorge Lucangeli Obes32201f82019-06-12 14:45:06 -0400518}
519
520int seccomp_ret_log_available(void)
521{
522 static int ret_log_available = -1;
523
524 if (ret_log_available == -1)
525 ret_log_available = seccomp_action_is_available("log");
526
527 return ret_log_available;
528}
529
530int seccomp_ret_kill_process_available(void)
531{
532 static int ret_kill_process_available = -1;
533
534 if (ret_kill_process_available == -1)
535 ret_kill_process_available =
536 seccomp_action_is_available("kill_process");
537
538 return ret_kill_process_available;
539}
Luis Héctor Chávez01b628c2021-01-03 05:46:57 -0800540
541bool seccomp_filter_flags_available(unsigned int flags)
542{
543 return sys_seccomp(SECCOMP_SET_MODE_FILTER, flags, NULL) != -1 ||
544 errno != EINVAL;
545}