| 1 | #cxx . . $ `<file>/src/seccomp.c`/define _GNU_SOURCEcxx . . $ `src/seccomp.c:1:9`! |
| 2 | #if HAVE_CONFIG_Hcxx . . $ `<built-in>:411:9`! |
| 3 | #include "config.h"cxx . . $ `<file>/config.h`/ |
| 4 | #endif |
| 5 | |
| 6 | #include "arping.h"cxx . . $ `<file>/src/arping.h`/ |
| 7 | #include "cast.h"cxx . . $ `<file>/src/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 UNUSEDcxx . . $ `src/seccomp.c:15:9`!(x) (void)(x) |
| 16 | |
| 17 | #if !USE_SECCOMPcxx . . $ `src/arping.h:33:9`! |
| 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_allowcxx . . $ seccomp_allow(e7c9eaecdb7c5ce7).(scmp_filter_ctxcxx . . $ scmp_filter_ctx# ctx, const char* name) |
| 38 | { |
| 39 | const int resolved = seccomp_syscall_resolve_namecxx . . $ seccomp_syscall_resolve_name(5c4306d8553b3696).(name); |
| 40 | if (resolved == __NR_SCMP_ERRORcxx . . $ `<external>/9605212039769689496/seccomp.h:897:9`!) { |
| 41 | if (verbosecxx . . $ verbose.) { |
| 42 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, |
| 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_addcxx . . $ seccomp_rule_add(66fd6da22d7623a6).(ctx, SCMP_ACT_ALLOWcxx . . $ `<external>/9605212039769689496/seccomp.h:389:9`!, resolved, 0)) { |
| 51 | if (verbosecxx . . $ verbose.) { |
| 52 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, |
| 53 | "arping: seccomp_rule_add_exact(%s): %s\n", |
| 54 | name, strerrorcxx . . $ strerror(ff224f080444ee45).(errnocxx . . $ `<external>/17633486526465735210/errno.h:38:10`!)); |
| 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_fdcxx . . $ seccomp_allow_fd(d86d8cda5c46b2ce).(scmp_filter_ctxcxx . . $ scmp_filter_ctx# ctx, const char* name, int fd) |
| 69 | { |
| 70 | const int resolved = seccomp_syscall_resolve_namecxx . . $ seccomp_syscall_resolve_name(5c4306d8553b3696).(name); |
| 71 | if (resolved == __NR_SCMP_ERRORcxx . . $ `<external>/9605212039769689496/seccomp.h:897:9`!) { |
| 72 | if (verbosecxx . . $ verbose.) { |
| 73 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, |
| 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_addcxx . . $ seccomp_rule_add(66fd6da22d7623a6).(ctx, SCMP_ACT_ALLOWcxx . . $ `<external>/9605212039769689496/seccomp.h:389:9`!, resolved, 1, |
| 82 | SCMP_A0cxx . . $ `<external>/9605212039769689496/seccomp.h:288:9`!(SCMP_CMP_EQ, cast_int_ulong(fd, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!)))) { |
| 83 | fprintfcxx . . $ fprintf(bc216f8e5b1ebeac).(stderrcxx . . $ `<external>/232824719582829767/stdio.h:159:9`!, "arping: seccomp_rule_add(%s for fd %d)", |
| 84 | name, fd); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | drop_seccompcxx . . $ drop_seccomp(d4f767463ce0a6b3).(int libnet_fd) |
| 90 | { |
| 91 | //scmp_filter_ctx ctx = seccomp_init(SCMP_ACT_ERRNO(13)); |
| 92 | scmp_filter_ctxcxx . . $ scmp_filter_ctx# ctx = seccomp_initcxx . . $ seccomp_init(52408c21a1e21ff7).(SCMP_ACT_KILLcxx . . $ `<external>/9605212039769689496/seccomp.h:365:9`!); |
| 93 | if (!ctx) { |
| 94 | perrorcxx . . $ perror(85c52e162fed56f9).("seccomp_init()"); |
| 95 | exitcxx . . $ exit(3ca292c338df9bb3).(1); |
| 96 | } |
| 97 | |
| 98 | // |
| 99 | // Whitelist. |
| 100 | // |
| 101 | // Stat and write to stdout and stderr. |
| 102 | seccomp_allow_fdcxx . . $ seccomp_allow_fd(d86d8cda5c46b2ce).(ctx, "statx", STDOUT_FILENOcxx . . $ `<external>/12628407106812493757/unistd.h:211:9`!); |
| 103 | seccomp_allow_fdcxx . . $ seccomp_allow_fd(d86d8cda5c46b2ce).(ctx, "fstat", STDOUT_FILENOcxx . . $ `<external>/12628407106812493757/unistd.h:211:9`!); |
| 104 | seccomp_allow_fdcxx . . $ seccomp_allow_fd(d86d8cda5c46b2ce).(ctx, "write", STDOUT_FILENOcxx . . $ `<external>/12628407106812493757/unistd.h:211:9`!); |
| 105 | seccomp_allow_fdcxx . . $ seccomp_allow_fd(d86d8cda5c46b2ce).(ctx, "write", STDERR_FILENOcxx . . $ `<external>/12628407106812493757/unistd.h:212:9`!); |
| 106 | |
| 107 | // Libnet. |
| 108 | if (seccomp_rule_addcxx . . $ seccomp_rule_add(66fd6da22d7623a6).(ctx, SCMP_ACT_ALLOWcxx . . $ `<external>/9605212039769689496/seccomp.h:389:9`!, SCMP_SYScxx . . $ `<external>/9605212039769689496/seccomp.h:248:9`!(ioctl), 1, SCMP_A0cxx . . $ `<external>/9605212039769689496/seccomp.h:288:9`!(SCMP_CMP_EQ, cast_int_uint(libnet_fd, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!)))) { |
| 109 | perrorcxx . . $ perror(85c52e162fed56f9).("seccomp_rule_add(ioctl libnet)"); |
| 110 | exitcxx . . $ exit(3ca292c338df9bb3).(1); |
| 111 | } |
| 112 | if (seccomp_rule_addcxx . . $ seccomp_rule_add(66fd6da22d7623a6).(ctx, SCMP_ACT_ALLOWcxx . . $ `<external>/9605212039769689496/seccomp.h:389:9`!, SCMP_SYScxx . . $ `<external>/9605212039769689496/seccomp.h:248:9`!(sendto), 1, SCMP_A0cxx . . $ `<external>/9605212039769689496/seccomp.h:288:9`!(SCMP_CMP_EQ, cast_int_uint(libnet_fd, NULLcxx . . $ `/usr/lib/gcc/x86_64-linux-gnu/16/include/stddef.h:419:9`!)))) { |
| 113 | perrorcxx . . $ perror(85c52e162fed56f9).("seccomp_rule_add(sendto libnet)"); |
| 114 | exitcxx . . $ exit(3ca292c338df9bb3).(1); |
| 115 | } |
| 116 | |
| 117 | // Other. |
| 118 | seccomp_allowcxx . . $ seccomp_allow(e7c9eaecdb7c5ce7).(ctx, "read"); |
| 119 | seccomp_allowcxx . . $ seccomp_allow(e7c9eaecdb7c5ce7).(ctx, "recvfrom"); |
| 120 | seccomp_allowcxx . . $ seccomp_allow(e7c9eaecdb7c5ce7).(ctx, "select"); |
| 121 | seccomp_allowcxx . . $ seccomp_allow(e7c9eaecdb7c5ce7).(ctx, "pselect6"); |
| 122 | seccomp_allowcxx . . $ seccomp_allow(e7c9eaecdb7c5ce7).(ctx, "newfstatat"); |
| 123 | seccomp_allowcxx . . $ seccomp_allow(e7c9eaecdb7c5ce7).(ctx, "exit_group"); |
| 124 | seccomp_allowcxx . . $ seccomp_allow(e7c9eaecdb7c5ce7).(ctx, "rt_sigreturn"); |
| 125 | seccomp_allowcxx . . $ seccomp_allow(e7c9eaecdb7c5ce7).(ctx, "clock_gettime64"); |
| 126 | seccomp_allowcxx . . $ seccomp_allow(e7c9eaecdb7c5ce7).(ctx, "clock_nanosleep"); |
| 127 | seccomp_allowcxx . . $ seccomp_allow(e7c9eaecdb7c5ce7).(ctx, "nanosleep"); |
| 128 | seccomp_allowcxx . . $ seccomp_allow(e7c9eaecdb7c5ce7).(ctx, "clock_nanosleep_time64"); |
| 129 | seccomp_allowcxx . . $ seccomp_allow(e7c9eaecdb7c5ce7).(ctx, "clock_gettime"); |
| 130 | |
| 131 | // Load. |
| 132 | if (seccomp_loadcxx . . $ seccomp_load(dedf1e9b1c614a07).(ctx)) { |
| 133 | perrorcxx . . $ perror(85c52e162fed56f9).("seccomp_load()"); |
| 134 | exitcxx . . $ exit(3ca292c338df9bb3).(1); |
| 135 | } |
| 136 | seccomp_releasecxx . . $ seccomp_release(5d22bdacc48458e8).(ctx); |
| 137 | if (verbosecxx . . $ verbose. > 1) { |
| 138 | printfcxx . . $ printf(581fddd72a8db8fc).("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 | */ |