1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * System call table mapper 4 * 5 * (C) 2016 Arnaldo Carvalho de Melo <acme@redhat.com> 6 */ 7 8 #include "syscalltbl.h" 9 #include <stdlib.h> 10 #include <linux/compiler.h> 11 #include <linux/zalloc.h> 12 13 #ifdef HAVE_SYSCALL_TABLE_SUPPORT 14 #include <string.h> 15 #include "string2.h" 16 17 #if defined(__s390x__) 18 #include <asm/syscalls_64.c> 19 const int syscalltbl_native_max_id = SYSCALLTBL_S390_64_MAX_ID; 20 static const char *const *syscalltbl_native = syscalltbl_s390_64; 21 #elif defined(GENERIC_SYSCALL_TABLE) 22 #include <syscall_table.h> 23 const int syscalltbl_native_max_id = SYSCALLTBL_MAX_ID; 24 static const char *const *syscalltbl_native = syscalltbl; 25 #else 26 const int syscalltbl_native_max_id = 0; 27 static const char *const syscalltbl_native[] = { 28 [0] = "unknown", 29 }; 30 #endif 31 32 struct syscall { 33 int id; 34 const char *name; 35 }; 36 37 static int syscallcmpname(const void *vkey, const void *ventry) 38 { 39 const char *key = vkey; 40 const struct syscall *entry = ventry; 41 42 return strcmp(key, entry->name); 43 } 44 45 static int syscallcmp(const void *va, const void *vb) 46 { 47 const struct syscall *a = va, *b = vb; 48 49 return strcmp(a->name, b->name); 50 } 51 52 static int syscalltbl__init_native(struct syscalltbl *tbl) 53 { 54 int nr_entries = 0, i, j; 55 struct syscall *entries; 56 57 for (i = 0; i <= syscalltbl_native_max_id; ++i) 58 if (syscalltbl_native[i]) 59 ++nr_entries; 60 61 entries = tbl->syscalls.entries = malloc(sizeof(struct syscall) * nr_entries); 62 if (tbl->syscalls.entries == NULL) 63 return -1; 64 65 for (i = 0, j = 0; i <= syscalltbl_native_max_id; ++i) { 66 if (syscalltbl_native[i]) { 67 entries[j].name = syscalltbl_native[i]; 68 entries[j].id = i; 69 ++j; 70 } 71 } 72 73 qsort(tbl->syscalls.entries, nr_entries, sizeof(struct syscall), syscallcmp); 74 tbl->syscalls.nr_entries = nr_entries; 75 tbl->syscalls.max_id = syscalltbl_native_max_id; 76 return 0; 77 } 78 79 struct syscalltbl *syscalltbl__new(void) 80 { 81 struct syscalltbl *tbl = malloc(sizeof(*tbl)); 82 if (tbl) { 83 if (syscalltbl__init_native(tbl)) { 84 free(tbl); 85 return NULL; 86 } 87 } 88 return tbl; 89 } 90 91 void syscalltbl__delete(struct syscalltbl *tbl) 92 { 93 zfree(&tbl->syscalls.entries); 94 free(tbl); 95 } 96 97 const char *syscalltbl__name(const struct syscalltbl *tbl __maybe_unused, int id) 98 { 99 return id <= syscalltbl_native_max_id ? syscalltbl_native[id]: NULL; 100 } 101 102 int syscalltbl__id(struct syscalltbl *tbl, const char *name) 103 { 104 struct syscall *sc = bsearch(name, tbl->syscalls.entries, 105 tbl->syscalls.nr_entries, sizeof(*sc), 106 syscallcmpname); 107 108 return sc ? sc->id : -1; 109 } 110 111 int syscalltbl__id_at_idx(struct syscalltbl *tbl, int idx) 112 { 113 struct syscall *syscalls = tbl->syscalls.entries; 114 115 return idx < tbl->syscalls.nr_entries ? syscalls[idx].id : -1; 116 } 117 118 int syscalltbl__strglobmatch_next(struct syscalltbl *tbl, const char *syscall_glob, int *idx) 119 { 120 int i; 121 struct syscall *syscalls = tbl->syscalls.entries; 122 123 for (i = *idx + 1; i < tbl->syscalls.nr_entries; ++i) { 124 if (strglobmatch(syscalls[i].name, syscall_glob)) { 125 *idx = i; 126 return syscalls[i].id; 127 } 128 } 129 130 return -1; 131 } 132 133 int syscalltbl__strglobmatch_first(struct syscalltbl *tbl, const char *syscall_glob, int *idx) 134 { 135 *idx = -1; 136 return syscalltbl__strglobmatch_next(tbl, syscall_glob, idx); 137 } 138 139 #else /* HAVE_SYSCALL_TABLE_SUPPORT */ 140 141 #include <libaudit.h> 142 143 struct syscalltbl *syscalltbl__new(void) 144 { 145 struct syscalltbl *tbl = zalloc(sizeof(*tbl)); 146 if (tbl) 147 tbl->audit_machine = audit_detect_machine(); 148 return tbl; 149 } 150 151 void syscalltbl__delete(struct syscalltbl *tbl) 152 { 153 free(tbl); 154 } 155 156 const char *syscalltbl__name(const struct syscalltbl *tbl, int id) 157 { 158 return audit_syscall_to_name(id, tbl->audit_machine); 159 } 160 161 int syscalltbl__id(struct syscalltbl *tbl, const char *name) 162 { 163 return audit_name_to_syscall(name, tbl->audit_machine); 164 } 165 166 int syscalltbl__id_at_idx(struct syscalltbl *tbl __maybe_unused, int idx) 167 { 168 return idx; 169 } 170 171 int syscalltbl__strglobmatch_next(struct syscalltbl *tbl __maybe_unused, 172 const char *syscall_glob __maybe_unused, int *idx __maybe_unused) 173 { 174 return -1; 175 } 176 177 int syscalltbl__strglobmatch_first(struct syscalltbl *tbl, const char *syscall_glob, int *idx) 178 { 179 return syscalltbl__strglobmatch_next(tbl, syscall_glob, idx); 180 } 181 #endif /* HAVE_SYSCALL_TABLE_SUPPORT */ 182