| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
|
| 3 |
#include "alloc-util.h" |
| 4 |
#include "analyze.h" |
| 5 |
#include "analyze-syscall-filter.h" |
| 6 |
#include "ansi-color.h" |
| 7 |
#include "fd-util.h" |
| 8 |
#include "fileio.h" |
| 9 |
#include "log.h" |
| 10 |
#include "nulstr-util.h" |
| 11 |
#include "pager.h" |
| 12 |
#include "seccomp-util.h" |
| 13 |
#include "set.h" |
| 14 |
#include "string-util.h" |
| 15 |
#include "strv.h" |
| 16 |
|
| 17 |
#if HAVE_SECCOMP |
| 18 |
|
| 19 |
static int load_kernel_syscalls(Set **ret) { |
| 20 |
_cleanup_set_free_ Set *syscalls = NULL; |
| 21 |
_cleanup_fclose_ FILE *f = NULL; |
| 22 |
int r; |
| 23 |
|
| 24 |
assert(ret); |
| 25 |
|
| 26 |
/* Let's read the available system calls from the list of available tracing events. Slightly dirty, |
| 27 |
* but good enough for analysis purposes. */ |
| 28 |
|
| 29 |
f = fopen("/sys/kernel/tracing/available_events", "re"); |
| 30 |
if (!f) { |
| 31 |
/* We tried the non-debugfs mount point and that didn't work. If it wasn't mounted, maybe the |
| 32 |
* old debugfs mount point works? */ |
| 33 |
f = fopen("/sys/kernel/debug/tracing/available_events", "re"); |
| 34 |
if (!f) |
| 35 |
return log_full_errno(IN_SET(errno, EPERM, EACCES, ENOENT) ? LOG_DEBUG : LOG_WARNING, errno, |
| 36 |
"Can't read open tracefs' available_events file: %m"); |
| 37 |
} |
| 38 |
|
| 39 |
for (;;) { |
| 40 |
_cleanup_free_ char *line = NULL; |
| 41 |
const char *e; |
| 42 |
|
| 43 |
r = read_line(f, LONG_LINE_MAX, &line); |
| 44 |
if (r < 0) |
| 45 |
return log_error_errno(r, "Failed to read system call list: %m"); |
| 46 |
if (r == 0) |
| 47 |
break; |
| 48 |
|
| 49 |
e = startswith(line, "syscalls:sys_enter_"); |
| 50 |
if (!e) |
| 51 |
continue; |
| 52 |
|
| 53 |
/* These are named differently inside the kernel than their external name for historical |
| 54 |
* reasons. Let's hide them here. */ |
| 55 |
if (STR_IN_SET(e, "newuname", "newfstat", "newstat", "newlstat", "sysctl")) |
| 56 |
continue; |
| 57 |
|
| 58 |
r = set_put_strdup(&syscalls, e); |
| 59 |
if (r < 0) |
| 60 |
return log_error_errno(r, "Failed to add system call to list: %m"); |
| 61 |
} |
| 62 |
|
| 63 |
*ret = TAKE_PTR(syscalls); |
| 64 |
return 0; |
| 65 |
} |
| 66 |
|
| 67 |
static int syscall_set_add(Set **s, const SyscallFilterSet *set) { |
| 68 |
int r; |
| 69 |
|
| 70 |
assert(s); |
| 71 |
|
| 72 |
if (!set) |
| 73 |
return 0; |
| 74 |
|
| 75 |
NULSTR_FOREACH(sc, set->value) { |
| 76 |
if (sc[0] == '@') |
| 77 |
continue; |
| 78 |
|
| 79 |
r = set_put_strdup(s, sc); |
| 80 |
if (r < 0) |
| 81 |
return r; |
| 82 |
} |
| 83 |
|
| 84 |
return 0; |
| 85 |
} |
| 86 |
|
| 87 |
static void syscall_set_remove(Set *s, const SyscallFilterSet *set) { |
| 88 |
if (!set) |
| 89 |
return; |
| 90 |
|
| 91 |
NULSTR_FOREACH(sc, set->value) { |
| 92 |
if (sc[0] == '@') |
| 93 |
continue; |
| 94 |
|
| 95 |
free(set_remove(s, sc)); |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
static void dump_syscall_filter(const SyscallFilterSet *set) { |
| 100 |
printf("%s%s%s\n" |
| 101 |
" # %s\n", |
| 102 |
ansi_highlight(), |
| 103 |
set->name, |
| 104 |
ansi_normal(), |
| 105 |
set->help); |
| 106 |
|
| 107 |
NULSTR_FOREACH(syscall, set->value) |
| 108 |
printf(" %s%s%s\n", syscall[0] == '@' ? ansi_underline() : "", syscall, ansi_normal()); |
| 109 |
} |
| 110 |
|
| 111 |
int verb_syscall_filters(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 112 |
int r; |
| 113 |
|
| 114 |
pager_open(arg_pager_flags); |
| 115 |
|
| 116 |
char **args = strv_skip(argv, 1); |
| 117 |
if (args) |
| 118 |
STRV_FOREACH(name, args) { |
| 119 |
const SyscallFilterSet *set; |
| 120 |
|
| 121 |
if (name != args) |
| 122 |
puts(""); |
| 123 |
|
| 124 |
set = syscall_filter_set_find(*name); |
| 125 |
if (!set) { |
| 126 |
/* make sure the error appears below normal output */ |
| 127 |
fflush(stdout); |
| 128 |
|
| 129 |
return log_error_errno(SYNTHETIC_ERRNO(ENOENT), |
| 130 |
"Filter set \"%s\" not found.", *name); |
| 131 |
} |
| 132 |
|
| 133 |
dump_syscall_filter(set); |
| 134 |
} |
| 135 |
else { |
| 136 |
_cleanup_set_free_ Set *kernel = NULL, *known = NULL; |
| 137 |
int k = 0; /* explicit initialization to appease gcc */ |
| 138 |
|
| 139 |
r = syscall_set_add(&known, syscall_filter_sets + SYSCALL_FILTER_SET_KNOWN); |
| 140 |
if (r < 0) |
| 141 |
return log_error_errno(r, "Failed to prepare set of known system calls: %m"); |
| 142 |
|
| 143 |
if (!arg_quiet) |
| 144 |
k = load_kernel_syscalls(&kernel); |
| 145 |
|
| 146 |
for (int i = 0; i < _SYSCALL_FILTER_SET_MAX; i++) { |
| 147 |
const SyscallFilterSet *set = syscall_filter_sets + i; |
| 148 |
if (i > 0) |
| 149 |
puts(""); |
| 150 |
|
| 151 |
dump_syscall_filter(set); |
| 152 |
syscall_set_remove(kernel, set); |
| 153 |
if (i != SYSCALL_FILTER_SET_KNOWN) |
| 154 |
syscall_set_remove(known, set); |
| 155 |
} |
| 156 |
|
| 157 |
if (arg_quiet) /* Let's not show the extra stuff in quiet mode */ |
| 158 |
return 0; |
| 159 |
|
| 160 |
if (!set_isempty(known)) { |
| 161 |
printf("\n" |
| 162 |
"# %sUngrouped System Calls%s (known but not included in any of the groups except @known):\n", |
| 163 |
ansi_highlight(), ansi_normal()); |
| 164 |
|
| 165 |
_cleanup_free_ char **l = set_get_strv(known); |
| 166 |
if (!l) |
| 167 |
return log_oom(); |
| 168 |
|
| 169 |
strv_sort(l); |
| 170 |
|
| 171 |
STRV_FOREACH(syscall, l) |
| 172 |
printf("# %s\n", *syscall); |
| 173 |
} |
| 174 |
|
| 175 |
if (k < 0) { |
| 176 |
fputc('\n', stdout); |
| 177 |
fflush(stdout); |
| 178 |
if (!arg_quiet) |
| 179 |
log_notice_errno(k, "# Not showing unlisted system calls, couldn't retrieve kernel system call list: %m"); |
| 180 |
} else if (!set_isempty(kernel)) { |
| 181 |
printf("\n" |
| 182 |
"# %sUnlisted System Calls%s (supported by the local kernel, but not included in any of the groups listed above):\n", |
| 183 |
ansi_highlight(), ansi_normal()); |
| 184 |
|
| 185 |
_cleanup_free_ char **l = set_get_strv(kernel); |
| 186 |
if (!l) |
| 187 |
return log_oom(); |
| 188 |
|
| 189 |
strv_sort(l); |
| 190 |
|
| 191 |
STRV_FOREACH(syscall, l) |
| 192 |
printf("# %s\n", *syscall); |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
return EXIT_SUCCESS; |
| 197 |
} |
| 198 |
|
| 199 |
#else |
| 200 |
int verb_syscall_filters(int argc, char *argv[], uintptr_t _data, void *userdata) { |
| 201 |
return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "Not compiled with syscall filters, sorry."); |
| 202 |
} |
| 203 |
#endif |