| 1 | /** |
| 2 | * Seccomp Library test program |
| 3 | * |
| 4 | * Copyright IBM Corp. 2012 |
| 5 | * Author: Corey Bryant <coreyb@linux.vnet.ibm.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 | int main(int argc, char *argv[]) |
| 28 | { |
| 29 | int rc; |
| 30 | scmp_filter_ctx ctx; |
| 31 | |
| 32 | /* seccomp_init errors */ |
| 33 | ctx = seccomp_init(SCMP_ACT_ALLOW + 1); |
| 34 | if (ctx != NULL) |
| 35 | return -1; |
| 36 | |
| 37 | ctx = seccomp_init(SCMP_ACT_ALLOW); |
| 38 | if (ctx == NULL) |
| 39 | return -1; |
| 40 | seccomp_release(ctx); |
| 41 | ctx = NULL; |
| 42 | |
| 43 | /* seccomp_reset error */ |
| 44 | rc = seccomp_reset(ctx, SCMP_ACT_KILL + 1); |
| 45 | if (rc != -EINVAL) |
| 46 | return -1; |
| 47 | rc = seccomp_reset(ctx, SCMP_ACT_KILL); |
| 48 | if (rc != -EINVAL) |
| 49 | return -1; |
| 50 | |
| 51 | /* seccomp_load error */ |
| 52 | rc = seccomp_load(ctx); |
| 53 | if (rc != -EINVAL) |
| 54 | return -1; |
| 55 | |
| 56 | /* seccomp_syscall_priority errors */ |
| 57 | rc = seccomp_syscall_priority(ctx, SCMP_SYS(read), 1); |
| 58 | if (rc != -EINVAL) |
| 59 | return -1; |
| 60 | |
| 61 | ctx = seccomp_init(SCMP_ACT_ALLOW); |
| 62 | if (ctx == NULL) |
| 63 | return -1; |
| 64 | else { |
| 65 | rc = seccomp_syscall_priority(ctx, -10, 1); |
| 66 | if (rc != -EINVAL) |
| 67 | return -1; |
| 68 | } |
| 69 | seccomp_release(ctx); |
| 70 | ctx = NULL; |
| 71 | |
| 72 | /* seccomp_rule_add errors */ |
| 73 | rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(read), 1, |
| 74 | SCMP_A0(SCMP_CMP_EQ, 0)); |
| 75 | if (rc != -EINVAL) |
| 76 | return -1; |
| 77 | |
| 78 | ctx = seccomp_init(SCMP_ACT_ALLOW); |
| 79 | if (ctx == NULL) |
| 80 | return -1; |
| 81 | else { |
| 82 | rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(read), 0); |
| 83 | if (rc != -EPERM) |
| 84 | return -1; |
| 85 | rc = seccomp_rule_add(ctx, SCMP_ACT_KILL - 1, SCMP_SYS(read), 0); |
| 86 | if (rc != -EINVAL) |
| 87 | return -1; |
| 88 | rc = seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(read), 7); |
| 89 | if (rc != -EINVAL) |
| 90 | return -1; |
| 91 | rc = seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(read), 7, |
| 92 | SCMP_A0(SCMP_CMP_EQ, 0), |
| 93 | SCMP_A1(SCMP_CMP_EQ, 0), |
| 94 | SCMP_A2(SCMP_CMP_EQ, 0), |
| 95 | SCMP_A3(SCMP_CMP_EQ, 0), |
| 96 | SCMP_A4(SCMP_CMP_EQ, 0), |
| 97 | SCMP_A5(SCMP_CMP_EQ, 0), |
| 98 | SCMP_CMP(6, SCMP_CMP_EQ, 0)); |
| 99 | if (rc != -EINVAL) |
| 100 | return -1; |
| 101 | rc = seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(read), 1, |
| 102 | SCMP_A0(_SCMP_CMP_MIN, 0)); |
| 103 | if (rc != -EINVAL) |
| 104 | return -1; |
| 105 | rc = seccomp_rule_add(ctx, SCMP_ACT_KILL, SCMP_SYS(read), 1, |
| 106 | SCMP_A0(_SCMP_CMP_MAX, 0)); |
| 107 | if (rc != -EINVAL) |
| 108 | return -1; |
| 109 | rc = seccomp_rule_add_exact(ctx, SCMP_ACT_KILL, -10001, 0); |
| 110 | if (rc != -EDOM) |
| 111 | return -1; |
| 112 | } |
| 113 | seccomp_release(ctx); |
| 114 | ctx = NULL; |
| 115 | |
| 116 | /* seccomp_rule_add_exact error */ |
| 117 | ctx = seccomp_init(SCMP_ACT_ALLOW); |
| 118 | if (ctx == NULL) |
| 119 | return -1; |
| 120 | rc = seccomp_arch_remove(ctx, SCMP_ARCH_NATIVE); |
| 121 | if (rc != 0) |
| 122 | return -1; |
| 123 | rc = seccomp_arch_add(ctx, SCMP_ARCH_X86); |
| 124 | if (rc != 0) |
| 125 | return -1; |
| 126 | rc = seccomp_rule_add_exact(ctx, SCMP_ACT_KILL, SCMP_SYS(socket), 1, |
| 127 | SCMP_A0(SCMP_CMP_EQ, 2)); |
| 128 | if (rc != -EINVAL) |
| 129 | return -1; |
| 130 | seccomp_release(ctx); |
| 131 | ctx = NULL; |
| 132 | |
| 133 | /* errno values beyond MAX_ERRNO */ |
| 134 | ctx = seccomp_init(SCMP_ACT_ALLOW); |
| 135 | if (ctx == NULL) |
| 136 | return -1; |
| 137 | rc = seccomp_rule_add(ctx, SCMP_ACT_ERRNO(0xffff), 0, 0); |
| 138 | if (rc != -EINVAL) |
| 139 | return -1; |
| 140 | seccomp_release(ctx); |
| 141 | ctx = NULL; |
| 142 | |
| 143 | /* seccomp_export_pfc errors */ |
| 144 | rc = seccomp_export_pfc(ctx, STDOUT_FILENO); |
| 145 | if (rc != -EINVAL) |
| 146 | return -1; |
| 147 | |
| 148 | ctx = seccomp_init(SCMP_ACT_ALLOW); |
| 149 | if (ctx == NULL) |
| 150 | return -1; |
| 151 | else { |
| 152 | rc = seccomp_export_pfc(ctx, sysconf(_SC_OPEN_MAX) - 1); |
| 153 | if (rc != EBADF) |
| 154 | return -1; |
| 155 | } |
| 156 | seccomp_release(ctx); |
| 157 | ctx = NULL; |
| 158 | |
| 159 | /* seccomp_export_bpf errors */ |
| 160 | rc = seccomp_export_bpf(ctx, STDOUT_FILENO); |
| 161 | if (rc != -EINVAL) |
| 162 | return -1; |
| 163 | |
| 164 | ctx = seccomp_init(SCMP_ACT_ALLOW); |
| 165 | if (ctx == NULL) |
| 166 | return -1; |
| 167 | else { |
| 168 | rc = seccomp_export_bpf(ctx, sysconf(_SC_OPEN_MAX) - 1); |
| 169 | if (rc != -EBADF) |
| 170 | return -1; |
| 171 | } |
| 172 | seccomp_release(ctx); |
| 173 | ctx = NULL; |
| 174 | |
| 175 | return 0; |
| 176 | } |