| 1 | /** |
| 2 | * Seccomp Library test program |
| 3 | * |
| 4 | * Copyright (c) 2013 Red Hat <pmoore@redhat.com> |
| 5 | * Author: Paul Moore <paul@paul-moore.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 <fcntl.h> |
| 24 | #include <string.h> |
| 25 | #include <unistd.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <sys/stat.h> |
| 28 | |
| 29 | #include <seccomp.h> |
| 30 | |
| 31 | #include "util.h" |
| 32 | |
| 33 | int main(int argc, char *argv[]) |
| 34 | { |
| 35 | int rc; |
| 36 | int fd; |
| 37 | scmp_filter_ctx ctx = NULL; |
| 38 | const char buf[] = "testing"; |
| 39 | ssize_t buf_len = strlen(buf); |
| 40 | |
| 41 | rc = util_action_parse(argv[1]); |
| 42 | if (rc != SCMP_ACT_ALLOW) { |
| 43 | rc = 1; |
| 44 | goto out; |
| 45 | } |
| 46 | |
| 47 | rc = util_trap_install(); |
| 48 | if (rc != 0) |
| 49 | goto out; |
| 50 | |
| 51 | fd = open("/dev/null", O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR); |
| 52 | if (fd < 0) { |
| 53 | rc = errno; |
| 54 | goto out; |
| 55 | } |
| 56 | |
| 57 | ctx = seccomp_init(SCMP_ACT_TRAP); |
| 58 | if (ctx == NULL) |
| 59 | return ENOMEM; |
| 60 | |
| 61 | rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(write), 1, |
| 62 | SCMP_A0(SCMP_CMP_EQ, fd)); |
| 63 | if (rc != 0) |
| 64 | goto out; |
| 65 | rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(close), 0); |
| 66 | if (rc != 0) |
| 67 | goto out; |
| 68 | rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(rt_sigreturn), 0); |
| 69 | if (rc != 0) |
| 70 | goto out; |
| 71 | rc = seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(exit_group), 0); |
| 72 | if (rc != 0) |
| 73 | goto out; |
| 74 | |
| 75 | rc = seccomp_load(ctx); |
| 76 | if (rc != 0) |
| 77 | goto out; |
| 78 | |
| 79 | if (write(fd, buf, buf_len) < buf_len) { |
| 80 | rc = errno; |
| 81 | goto out; |
| 82 | } |
| 83 | if (close(fd) < 0) { |
| 84 | rc = errno; |
| 85 | goto out; |
| 86 | } |
| 87 | |
| 88 | rc = 160; |
| 89 | |
| 90 | out: |
| 91 | seccomp_release(ctx); |
| 92 | return (rc < 0 ? -rc : rc); |
| 93 | } |