|
| 1 | +/** |
| 2 | + * Seccomp Library test program |
| 3 | + * |
| 4 | + * Copyright (c) 2025 Oracle and/or its affiliates. |
| 5 | + * Author: Tom Hromatka <tom.hromatka@oracle.com> |
| 6 | + */ |
| 7 | + |
| 8 | +/* |
| 9 | + * This library is free software; you can redistribute it and/or modify it |
| 10 | + * under the terms of version 2.1 of the GNU Lesser General Public License as |
| 11 | + * published by the Free Software Foundation. |
| 12 | + * |
| 13 | + * This library is distributed in the hope that it will be useful, but WITHOUT |
| 14 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 15 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License |
| 16 | + * for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Lesser General Public License |
| 19 | + * along with this library; if not, see <http://www.gnu.org/licenses>. |
| 20 | + */ |
| 21 | + |
| 22 | +#include <errno.h> |
| 23 | +#include <unistd.h> |
| 24 | + |
| 25 | +#include <seccomp.h> |
| 26 | + |
| 27 | +#include "util.h" |
| 28 | + |
| 29 | +#include <stdio.h> |
| 30 | +int main(int argc, char *argv[]) |
| 31 | +{ |
| 32 | + int rc; |
| 33 | + struct util_options opts; |
| 34 | + scmp_filter_ctx ctx = NULL; |
| 35 | + |
| 36 | + rc = util_getopt(argc, argv, &opts); |
| 37 | + if (rc < 0) |
| 38 | + goto out; |
| 39 | + |
| 40 | + ctx = seccomp_init(SCMP_ACT_KILL); |
| 41 | + if (ctx == NULL) |
| 42 | + return ENOMEM; |
| 43 | + |
| 44 | + rc = seccomp_arch_remove(ctx, SCMP_ARCH_NATIVE); |
| 45 | + if (rc != 0) |
| 46 | + goto out; |
| 47 | + |
| 48 | + rc = seccomp_arch_add(ctx, SCMP_ARCH_X86_64); |
| 49 | + if (rc != 0) |
| 50 | + goto out; |
| 51 | + rc = seccomp_arch_add(ctx, SCMP_ARCH_X32); |
| 52 | + if (rc != 0) |
| 53 | + goto out; |
| 54 | + |
| 55 | + rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(read), 0); |
| 56 | + if (rc != 0) |
| 57 | + goto out; |
| 58 | + rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 0); |
| 59 | + if (rc != 0) |
| 60 | + goto out; |
| 61 | + rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(poll), 0); |
| 62 | + if (rc != 0) |
| 63 | + goto out; |
| 64 | + rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(nanosleep), 0); |
| 65 | + if (rc != 0) |
| 66 | + goto out; |
| 67 | + |
| 68 | + rc = seccomp_attr_set(ctx, SCMP_FLTATR_CTL_OPTIMIZE, 2); |
| 69 | + if (rc != 0) |
| 70 | + goto out; |
| 71 | + rc = seccomp_attr_set(ctx, SCMP_FLTATR_ACT_ENOSYS, SCMP_ACT_ERRNO(3)); |
| 72 | + if (rc != 0) |
| 73 | + goto out; |
| 74 | + rc = seccomp_attr_set(ctx, SCMP_FLTATR_CTL_KVERMAX, SCMP_KV_6_5); |
| 75 | + if (rc != 0) |
| 76 | + goto out; |
| 77 | + |
| 78 | + /* unknown action must be set before the kernel version is set */ |
| 79 | + rc = seccomp_attr_set(ctx, SCMP_FLTATR_ACT_ENOSYS, SCMP_ACT_ERRNO(9)); |
| 80 | + if (rc != -EINVAL) |
| 81 | + goto out; |
| 82 | + |
| 83 | + /* |
| 84 | + * Attempt to add a rule after the maximum kernel version has been |
| 85 | + * set. This should fail because libseccomp currently does not |
| 86 | + * support overwriting existing rules. |
| 87 | + */ |
| 88 | + rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(open), 0); |
| 89 | + if (rc != -EINVAL) |
| 90 | + goto out; |
| 91 | + |
| 92 | + rc = util_filter_output(&opts, ctx); |
| 93 | + if (rc) |
| 94 | + goto out; |
| 95 | + |
| 96 | +out: |
| 97 | + seccomp_release(ctx); |
| 98 | + return (rc < 0 ? -rc : rc); |
| 99 | +} |
0 commit comments