1 // SPDX-License-Identifier: GPL-2.0-only 1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 2 /* 3 * System call table mapper 3 * System call table mapper 4 * 4 * 5 * (C) 2016 Arnaldo Carvalho de Melo <acme@red 5 * (C) 2016 Arnaldo Carvalho de Melo <acme@redhat.com> 6 */ 6 */ 7 7 8 #include "syscalltbl.h" 8 #include "syscalltbl.h" 9 #include <stdlib.h> 9 #include <stdlib.h> 10 #include <linux/compiler.h> 10 #include <linux/compiler.h> 11 #include <linux/zalloc.h> 11 #include <linux/zalloc.h> 12 12 13 #ifdef HAVE_SYSCALL_TABLE_SUPPORT 13 #ifdef HAVE_SYSCALL_TABLE_SUPPORT 14 #include <string.h> 14 #include <string.h> 15 #include "string2.h" 15 #include "string2.h" 16 16 17 #if defined(__x86_64__) 17 #if defined(__x86_64__) 18 #include <asm/syscalls_64.c> 18 #include <asm/syscalls_64.c> 19 const int syscalltbl_native_max_id = SYSCALLTB 19 const int syscalltbl_native_max_id = SYSCALLTBL_x86_64_MAX_ID; 20 static const char *const *syscalltbl_native = !! 20 static const char **syscalltbl_native = syscalltbl_x86_64; 21 #elif defined(__i386__) << 22 #include <asm/syscalls_32.c> << 23 const int syscalltbl_native_max_id = SYSCALLTB << 24 static const char *const *syscalltbl_native = << 25 #elif defined(__s390x__) 21 #elif defined(__s390x__) 26 #include <asm/syscalls_64.c> 22 #include <asm/syscalls_64.c> 27 const int syscalltbl_native_max_id = SYSCALLTB 23 const int syscalltbl_native_max_id = SYSCALLTBL_S390_64_MAX_ID; 28 static const char *const *syscalltbl_native = !! 24 static const char **syscalltbl_native = syscalltbl_s390_64; 29 #elif defined(__powerpc64__) 25 #elif defined(__powerpc64__) 30 #include <asm/syscalls_64.c> 26 #include <asm/syscalls_64.c> 31 const int syscalltbl_native_max_id = SYSCALLTB 27 const int syscalltbl_native_max_id = SYSCALLTBL_POWERPC_64_MAX_ID; 32 static const char *const *syscalltbl_native = !! 28 static const char **syscalltbl_native = syscalltbl_powerpc_64; 33 #elif defined(__powerpc__) 29 #elif defined(__powerpc__) 34 #include <asm/syscalls_32.c> 30 #include <asm/syscalls_32.c> 35 const int syscalltbl_native_max_id = SYSCALLTB 31 const int syscalltbl_native_max_id = SYSCALLTBL_POWERPC_32_MAX_ID; 36 static const char *const *syscalltbl_native = !! 32 static const char **syscalltbl_native = syscalltbl_powerpc_32; 37 #elif defined(__aarch64__) 33 #elif defined(__aarch64__) 38 #include <asm/syscalls.c> 34 #include <asm/syscalls.c> 39 const int syscalltbl_native_max_id = SYSCALLTB 35 const int syscalltbl_native_max_id = SYSCALLTBL_ARM64_MAX_ID; 40 static const char *const *syscalltbl_native = !! 36 static const char **syscalltbl_native = syscalltbl_arm64; 41 #elif defined(__mips__) 37 #elif defined(__mips__) 42 #include <asm/syscalls_n64.c> 38 #include <asm/syscalls_n64.c> 43 const int syscalltbl_native_max_id = SYSCALLTB 39 const int syscalltbl_native_max_id = SYSCALLTBL_MIPS_N64_MAX_ID; 44 static const char *const *syscalltbl_native = !! 40 static const char **syscalltbl_native = syscalltbl_mips_n64; 45 #elif defined(__loongarch__) << 46 #include <asm/syscalls.c> << 47 const int syscalltbl_native_max_id = SYSCALLTB << 48 static const char *const *syscalltbl_native = << 49 #else << 50 const int syscalltbl_native_max_id = 0; << 51 static const char *const syscalltbl_native[] = << 52 [0] = "unknown", << 53 }; << 54 #endif 41 #endif 55 42 56 struct syscall { 43 struct syscall { 57 int id; 44 int id; 58 const char *name; 45 const char *name; 59 }; 46 }; 60 47 61 static int syscallcmpname(const void *vkey, co 48 static int syscallcmpname(const void *vkey, const void *ventry) 62 { 49 { 63 const char *key = vkey; 50 const char *key = vkey; 64 const struct syscall *entry = ventry; 51 const struct syscall *entry = ventry; 65 52 66 return strcmp(key, entry->name); 53 return strcmp(key, entry->name); 67 } 54 } 68 55 69 static int syscallcmp(const void *va, const vo 56 static int syscallcmp(const void *va, const void *vb) 70 { 57 { 71 const struct syscall *a = va, *b = vb; 58 const struct syscall *a = va, *b = vb; 72 59 73 return strcmp(a->name, b->name); 60 return strcmp(a->name, b->name); 74 } 61 } 75 62 76 static int syscalltbl__init_native(struct sysc 63 static int syscalltbl__init_native(struct syscalltbl *tbl) 77 { 64 { 78 int nr_entries = 0, i, j; 65 int nr_entries = 0, i, j; 79 struct syscall *entries; 66 struct syscall *entries; 80 67 81 for (i = 0; i <= syscalltbl_native_max 68 for (i = 0; i <= syscalltbl_native_max_id; ++i) 82 if (syscalltbl_native[i]) 69 if (syscalltbl_native[i]) 83 ++nr_entries; 70 ++nr_entries; 84 71 85 entries = tbl->syscalls.entries = mall 72 entries = tbl->syscalls.entries = malloc(sizeof(struct syscall) * nr_entries); 86 if (tbl->syscalls.entries == NULL) 73 if (tbl->syscalls.entries == NULL) 87 return -1; 74 return -1; 88 75 89 for (i = 0, j = 0; i <= syscalltbl_nat 76 for (i = 0, j = 0; i <= syscalltbl_native_max_id; ++i) { 90 if (syscalltbl_native[i]) { 77 if (syscalltbl_native[i]) { 91 entries[j].name = sysc 78 entries[j].name = syscalltbl_native[i]; 92 entries[j].id = i; 79 entries[j].id = i; 93 ++j; 80 ++j; 94 } 81 } 95 } 82 } 96 83 97 qsort(tbl->syscalls.entries, nr_entrie 84 qsort(tbl->syscalls.entries, nr_entries, sizeof(struct syscall), syscallcmp); 98 tbl->syscalls.nr_entries = nr_entries; 85 tbl->syscalls.nr_entries = nr_entries; 99 tbl->syscalls.max_id = syscalltbl_ 86 tbl->syscalls.max_id = syscalltbl_native_max_id; 100 return 0; 87 return 0; 101 } 88 } 102 89 103 struct syscalltbl *syscalltbl__new(void) 90 struct syscalltbl *syscalltbl__new(void) 104 { 91 { 105 struct syscalltbl *tbl = malloc(sizeof 92 struct syscalltbl *tbl = malloc(sizeof(*tbl)); 106 if (tbl) { 93 if (tbl) { 107 if (syscalltbl__init_native(tb 94 if (syscalltbl__init_native(tbl)) { 108 free(tbl); 95 free(tbl); 109 return NULL; 96 return NULL; 110 } 97 } 111 } 98 } 112 return tbl; 99 return tbl; 113 } 100 } 114 101 115 void syscalltbl__delete(struct syscalltbl *tbl 102 void syscalltbl__delete(struct syscalltbl *tbl) 116 { 103 { 117 zfree(&tbl->syscalls.entries); 104 zfree(&tbl->syscalls.entries); 118 free(tbl); 105 free(tbl); 119 } 106 } 120 107 121 const char *syscalltbl__name(const struct sysc 108 const char *syscalltbl__name(const struct syscalltbl *tbl __maybe_unused, int id) 122 { 109 { 123 return id <= syscalltbl_native_max_id 110 return id <= syscalltbl_native_max_id ? syscalltbl_native[id]: NULL; 124 } 111 } 125 112 126 int syscalltbl__id(struct syscalltbl *tbl, con 113 int syscalltbl__id(struct syscalltbl *tbl, const char *name) 127 { 114 { 128 struct syscall *sc = bsearch(name, tbl 115 struct syscall *sc = bsearch(name, tbl->syscalls.entries, 129 tbl->sysc 116 tbl->syscalls.nr_entries, sizeof(*sc), 130 syscallcm 117 syscallcmpname); 131 118 132 return sc ? sc->id : -1; 119 return sc ? sc->id : -1; 133 } 120 } 134 121 135 int syscalltbl__id_at_idx(struct syscalltbl *t << 136 { << 137 struct syscall *syscalls = tbl->syscal << 138 << 139 return idx < tbl->syscalls.nr_entries << 140 } << 141 << 142 int syscalltbl__strglobmatch_next(struct sysca 122 int syscalltbl__strglobmatch_next(struct syscalltbl *tbl, const char *syscall_glob, int *idx) 143 { 123 { 144 int i; 124 int i; 145 struct syscall *syscalls = tbl->syscal 125 struct syscall *syscalls = tbl->syscalls.entries; 146 126 147 for (i = *idx + 1; i < tbl->syscalls.n 127 for (i = *idx + 1; i < tbl->syscalls.nr_entries; ++i) { 148 if (strglobmatch(syscalls[i].n 128 if (strglobmatch(syscalls[i].name, syscall_glob)) { 149 *idx = i; 129 *idx = i; 150 return syscalls[i].id; 130 return syscalls[i].id; 151 } 131 } 152 } 132 } 153 133 154 return -1; 134 return -1; 155 } 135 } 156 136 157 int syscalltbl__strglobmatch_first(struct sysc 137 int syscalltbl__strglobmatch_first(struct syscalltbl *tbl, const char *syscall_glob, int *idx) 158 { 138 { 159 *idx = -1; 139 *idx = -1; 160 return syscalltbl__strglobmatch_next(t 140 return syscalltbl__strglobmatch_next(tbl, syscall_glob, idx); 161 } 141 } 162 142 163 #else /* HAVE_SYSCALL_TABLE_SUPPORT */ 143 #else /* HAVE_SYSCALL_TABLE_SUPPORT */ 164 144 165 #include <libaudit.h> 145 #include <libaudit.h> 166 146 167 struct syscalltbl *syscalltbl__new(void) 147 struct syscalltbl *syscalltbl__new(void) 168 { 148 { 169 struct syscalltbl *tbl = zalloc(sizeof 149 struct syscalltbl *tbl = zalloc(sizeof(*tbl)); 170 if (tbl) 150 if (tbl) 171 tbl->audit_machine = audit_det 151 tbl->audit_machine = audit_detect_machine(); 172 return tbl; 152 return tbl; 173 } 153 } 174 154 175 void syscalltbl__delete(struct syscalltbl *tbl 155 void syscalltbl__delete(struct syscalltbl *tbl) 176 { 156 { 177 free(tbl); 157 free(tbl); 178 } 158 } 179 159 180 const char *syscalltbl__name(const struct sysc 160 const char *syscalltbl__name(const struct syscalltbl *tbl, int id) 181 { 161 { 182 return audit_syscall_to_name(id, tbl-> 162 return audit_syscall_to_name(id, tbl->audit_machine); 183 } 163 } 184 164 185 int syscalltbl__id(struct syscalltbl *tbl, con 165 int syscalltbl__id(struct syscalltbl *tbl, const char *name) 186 { 166 { 187 return audit_name_to_syscall(name, tbl 167 return audit_name_to_syscall(name, tbl->audit_machine); 188 } << 189 << 190 int syscalltbl__id_at_idx(struct syscalltbl *t << 191 { << 192 return idx; << 193 } 168 } 194 169 195 int syscalltbl__strglobmatch_next(struct sysca 170 int syscalltbl__strglobmatch_next(struct syscalltbl *tbl __maybe_unused, 196 const char * 171 const char *syscall_glob __maybe_unused, int *idx __maybe_unused) 197 { 172 { 198 return -1; 173 return -1; 199 } 174 } 200 175 201 int syscalltbl__strglobmatch_first(struct sysc 176 int syscalltbl__strglobmatch_first(struct syscalltbl *tbl, const char *syscall_glob, int *idx) 202 { 177 { 203 return syscalltbl__strglobmatch_next(t 178 return syscalltbl__strglobmatch_next(tbl, syscall_glob, idx); 204 } 179 } 205 #endif /* HAVE_SYSCALL_TABLE_SUPPORT */ 180 #endif /* HAVE_SYSCALL_TABLE_SUPPORT */ 206 181
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.