| 1 | #define _GNU_SOURCE |
| 2 | #if HAVE_CONFIG_H |
| 3 | #include "config.h" |
| 4 | #endif |
| 5 | |
| 6 | #include "arping.h" |
| 7 | #include "cast.h" |
| 8 | |
| 9 | #include <errno.h> |
| 10 | #include <stdlib.h> |
| 11 | #include <stdio.h> |
| 12 | #include <string.h> |
| 13 | #include <unistd.h> |
| 14 | |
| 15 | #define UNUSED(x) (void)(x) |
| 16 | |
| 17 | #if !USE_SECCOMP |
| 18 | void |
| 19 | drop_seccomp(int libnet_fd) |
| 20 | { |
| 21 | UNUSED(libnet_fd); |
| 22 | if (verbose > 2) { |
| 23 | printf("arping: seccomp support not built in, skipping\n"); |
| 24 | } |
| 25 | } |
| 26 | #else |
| 27 | #include <seccomp.h> |
| 28 | |
| 29 | /** |
| 30 | * Allow syscall by name. |
| 31 | * |
| 32 | * If there's no such syscall, that's fine. |
| 33 | * |
| 34 | * If allowing this syscall fails, then log verbose error. |
| 35 | */ |
| 36 | static void |
| 37 | seccomp_allow(scmp_filter_ctx ctx, const char* name) |
| 38 | { |
| 39 | const int resolved = seccomp_syscall_resolve_name(name); |
| 40 | if (resolved == __NR_SCMP_ERROR) { |
| 41 | if (verbose) { |
| 42 | fprintf(stderr, |
| 43 | "arping: seccomp can't resolve syscall %s:" |
| 44 | " skipping allowing that\n" |
| 45 | "arping: If arping fails, retry with -Z\n", |
| 46 | name); |
| 47 | } |
| 48 | return; |
| 49 | } |
| 50 | if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, resolved, 0)) { |
| 51 | if (verbose) { |
| 52 | fprintf(stderr, |
| 53 | "arping: seccomp_rule_add_exact(%s): %s\n", |
| 54 | name, strerror(errno)); |
| 55 | } |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Allow syscall by name if first arg is the given file descriptor. |
| 61 | * |
| 62 | * If there's no such syscall, that's fine. |
| 63 | * |
| 64 | * If allowing this syscall fails, then print warning to stderr. It's write(), |
| 65 | * so this has to work. |
| 66 | */ |
| 67 | static void |
| 68 | seccomp_allow_fd(scmp_filter_ctx ctx, const char* name, int fd) |
| 69 | { |
| 70 | const int resolved = seccomp_syscall_resolve_name(name); |
| 71 | if (resolved == __NR_SCMP_ERROR) { |
| 72 | if (verbose) { |
| 73 | fprintf(stderr, |
| 74 | "arping: seccomp can't resolve syscall %s:" |
| 75 | " skipping allowing that\n" |
| 76 | "arping: If arping fails, retry with -Z\n", |
| 77 | name); |
| 78 | } |
| 79 | return; |
| 80 | } |
| 81 | if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, resolved, 1, |
| 82 | SCMP_A0(SCMP_CMP_EQ, cast_int_ulong(fd, NULL)))) { |
| 83 | fprintf(stderr, "arping: seccomp_rule_add(%s for fd %d)", |
| 84 | name, fd); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | drop_seccomp(int libnet_fd) |
| 90 | { |
| 91 | //scmp_filter_ctx ctx = seccomp_init(SCMP_ACT_ERRNO(13)); |
| 92 | scmp_filter_ctx ctx = seccomp_init(SCMP_ACT_KILL); |
| 93 | if (!ctx) { |
| 94 | perror("seccomp_init()"); |
| 95 | exit(1); |
| 96 | } |
| 97 | |
| 98 | // |
| 99 | // Whitelist. |
| 100 | // |
| 101 | // Stat and write to stdout and stderr. |
| 102 | seccomp_allow_fd(ctx, "statx", STDOUT_FILENO); |
| 103 | seccomp_allow_fd(ctx, "fstat", STDOUT_FILENO); |
| 104 | seccomp_allow_fd(ctx, "write", STDOUT_FILENO); |
| 105 | seccomp_allow_fd(ctx, "write", STDERR_FILENO); |
| 106 | |
| 107 | // Libnet. |
| 108 | if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(ioctl), 1, SCMP_A0(SCMP_CMP_EQ, cast_int_uint(libnet_fd, NULL)))) { |
| 109 | perror("seccomp_rule_add(ioctl libnet)"); |
| 110 | exit(1); |
| 111 | } |
| 112 | if (seccomp_rule_add(ctx, SCMP_ACT_ALLOW, SCMP_SYS(sendto), 1, SCMP_A0(SCMP_CMP_EQ, cast_int_uint(libnet_fd, NULL)))) { |
| 113 | perror("seccomp_rule_add(sendto libnet)"); |
| 114 | exit(1); |
| 115 | } |
| 116 | |
| 117 | // Other. |
| 118 | seccomp_allow(ctx, "read"); |
| 119 | seccomp_allow(ctx, "recvfrom"); |
| 120 | seccomp_allow(ctx, "select"); |
| 121 | seccomp_allow(ctx, "pselect6"); |
| 122 | seccomp_allow(ctx, "newfstatat"); |
| 123 | seccomp_allow(ctx, "exit_group"); |
| 124 | seccomp_allow(ctx, "rt_sigreturn"); |
| 125 | seccomp_allow(ctx, "clock_gettime64"); |
| 126 | seccomp_allow(ctx, "clock_nanosleep"); |
| 127 | seccomp_allow(ctx, "nanosleep"); |
| 128 | seccomp_allow(ctx, "clock_nanosleep_time64"); |
| 129 | seccomp_allow(ctx, "clock_gettime"); |
| 130 | |
| 131 | // Load. |
| 132 | if (seccomp_load(ctx)) { |
| 133 | perror("seccomp_load()"); |
| 134 | exit(1); |
| 135 | } |
| 136 | seccomp_release(ctx); |
| 137 | if (verbose > 1) { |
| 138 | printf("arping: Successfully applied seccomp policy\n"); |
| 139 | } |
| 140 | } |
| 141 | #endif |
| 142 | /* ---- Emacs Variables ---- |
| 143 | * Local Variables: |
| 144 | * c-basic-offset: 8 |
| 145 | * indent-tabs-mode: nil |
| 146 | * End: |
| 147 | * |
| 148 | * vim: ts=8 sw=8 |
| 149 | */ |