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 *const *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 *const *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 *const *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 *const *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 *const *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 *const *syscalltbl_native = syscalltbl_mips_n64; 45 #elif defined(__loongarch__) 41 #elif defined(__loongarch__) 46 #include <asm/syscalls.c> 42 #include <asm/syscalls.c> 47 const int syscalltbl_native_max_id = SYSCALLTB 43 const int syscalltbl_native_max_id = SYSCALLTBL_LOONGARCH_MAX_ID; 48 static const char *const *syscalltbl_native = 44 static const char *const *syscalltbl_native = syscalltbl_loongarch; 49 #else 45 #else 50 const int syscalltbl_native_max_id = 0; 46 const int syscalltbl_native_max_id = 0; 51 static const char *const syscalltbl_native[] = 47 static const char *const syscalltbl_native[] = { 52 [0] = "unknown", 48 [0] = "unknown", 53 }; 49 }; 54 #endif 50 #endif 55 51 56 struct syscall { 52 struct syscall { 57 int id; 53 int id; 58 const char *name; 54 const char *name; 59 }; 55 }; 60 56 61 static int syscallcmpname(const void *vkey, co 57 static int syscallcmpname(const void *vkey, const void *ventry) 62 { 58 { 63 const char *key = vkey; 59 const char *key = vkey; 64 const struct syscall *entry = ventry; 60 const struct syscall *entry = ventry; 65 61 66 return strcmp(key, entry->name); 62 return strcmp(key, entry->name); 67 } 63 } 68 64 69 static int syscallcmp(const void *va, const vo 65 static int syscallcmp(const void *va, const void *vb) 70 { 66 { 71 const struct syscall *a = va, *b = vb; 67 const struct syscall *a = va, *b = vb; 72 68 73 return strcmp(a->name, b->name); 69 return strcmp(a->name, b->name); 74 } 70 } 75 71 76 static int syscalltbl__init_native(struct sysc 72 static int syscalltbl__init_native(struct syscalltbl *tbl) 77 { 73 { 78 int nr_entries = 0, i, j; 74 int nr_entries = 0, i, j; 79 struct syscall *entries; 75 struct syscall *entries; 80 76 81 for (i = 0; i <= syscalltbl_native_max 77 for (i = 0; i <= syscalltbl_native_max_id; ++i) 82 if (syscalltbl_native[i]) 78 if (syscalltbl_native[i]) 83 ++nr_entries; 79 ++nr_entries; 84 80 85 entries = tbl->syscalls.entries = mall 81 entries = tbl->syscalls.entries = malloc(sizeof(struct syscall) * nr_entries); 86 if (tbl->syscalls.entries == NULL) 82 if (tbl->syscalls.entries == NULL) 87 return -1; 83 return -1; 88 84 89 for (i = 0, j = 0; i <= syscalltbl_nat 85 for (i = 0, j = 0; i <= syscalltbl_native_max_id; ++i) { 90 if (syscalltbl_native[i]) { 86 if (syscalltbl_native[i]) { 91 entries[j].name = sysc 87 entries[j].name = syscalltbl_native[i]; 92 entries[j].id = i; 88 entries[j].id = i; 93 ++j; 89 ++j; 94 } 90 } 95 } 91 } 96 92 97 qsort(tbl->syscalls.entries, nr_entrie 93 qsort(tbl->syscalls.entries, nr_entries, sizeof(struct syscall), syscallcmp); 98 tbl->syscalls.nr_entries = nr_entries; 94 tbl->syscalls.nr_entries = nr_entries; 99 tbl->syscalls.max_id = syscalltbl_ 95 tbl->syscalls.max_id = syscalltbl_native_max_id; 100 return 0; 96 return 0; 101 } 97 } 102 98 103 struct syscalltbl *syscalltbl__new(void) 99 struct syscalltbl *syscalltbl__new(void) 104 { 100 { 105 struct syscalltbl *tbl = malloc(sizeof 101 struct syscalltbl *tbl = malloc(sizeof(*tbl)); 106 if (tbl) { 102 if (tbl) { 107 if (syscalltbl__init_native(tb 103 if (syscalltbl__init_native(tbl)) { 108 free(tbl); 104 free(tbl); 109 return NULL; 105 return NULL; 110 } 106 } 111 } 107 } 112 return tbl; 108 return tbl; 113 } 109 } 114 110 115 void syscalltbl__delete(struct syscalltbl *tbl 111 void syscalltbl__delete(struct syscalltbl *tbl) 116 { 112 { 117 zfree(&tbl->syscalls.entries); 113 zfree(&tbl->syscalls.entries); 118 free(tbl); 114 free(tbl); 119 } 115 } 120 116 121 const char *syscalltbl__name(const struct sysc 117 const char *syscalltbl__name(const struct syscalltbl *tbl __maybe_unused, int id) 122 { 118 { 123 return id <= syscalltbl_native_max_id 119 return id <= syscalltbl_native_max_id ? syscalltbl_native[id]: NULL; 124 } 120 } 125 121 126 int syscalltbl__id(struct syscalltbl *tbl, con 122 int syscalltbl__id(struct syscalltbl *tbl, const char *name) 127 { 123 { 128 struct syscall *sc = bsearch(name, tbl 124 struct syscall *sc = bsearch(name, tbl->syscalls.entries, 129 tbl->sysc 125 tbl->syscalls.nr_entries, sizeof(*sc), 130 syscallcm 126 syscallcmpname); 131 127 132 return sc ? sc->id : -1; 128 return sc ? sc->id : -1; 133 } 129 } 134 130 135 int syscalltbl__id_at_idx(struct syscalltbl *t 131 int syscalltbl__id_at_idx(struct syscalltbl *tbl, int idx) 136 { 132 { 137 struct syscall *syscalls = tbl->syscal 133 struct syscall *syscalls = tbl->syscalls.entries; 138 134 139 return idx < tbl->syscalls.nr_entries 135 return idx < tbl->syscalls.nr_entries ? syscalls[idx].id : -1; 140 } 136 } 141 137 142 int syscalltbl__strglobmatch_next(struct sysca 138 int syscalltbl__strglobmatch_next(struct syscalltbl *tbl, const char *syscall_glob, int *idx) 143 { 139 { 144 int i; 140 int i; 145 struct syscall *syscalls = tbl->syscal 141 struct syscall *syscalls = tbl->syscalls.entries; 146 142 147 for (i = *idx + 1; i < tbl->syscalls.n 143 for (i = *idx + 1; i < tbl->syscalls.nr_entries; ++i) { 148 if (strglobmatch(syscalls[i].n 144 if (strglobmatch(syscalls[i].name, syscall_glob)) { 149 *idx = i; 145 *idx = i; 150 return syscalls[i].id; 146 return syscalls[i].id; 151 } 147 } 152 } 148 } 153 149 154 return -1; 150 return -1; 155 } 151 } 156 152 157 int syscalltbl__strglobmatch_first(struct sysc 153 int syscalltbl__strglobmatch_first(struct syscalltbl *tbl, const char *syscall_glob, int *idx) 158 { 154 { 159 *idx = -1; 155 *idx = -1; 160 return syscalltbl__strglobmatch_next(t 156 return syscalltbl__strglobmatch_next(tbl, syscall_glob, idx); 161 } 157 } 162 158 163 #else /* HAVE_SYSCALL_TABLE_SUPPORT */ 159 #else /* HAVE_SYSCALL_TABLE_SUPPORT */ 164 160 165 #include <libaudit.h> 161 #include <libaudit.h> 166 162 167 struct syscalltbl *syscalltbl__new(void) 163 struct syscalltbl *syscalltbl__new(void) 168 { 164 { 169 struct syscalltbl *tbl = zalloc(sizeof 165 struct syscalltbl *tbl = zalloc(sizeof(*tbl)); 170 if (tbl) 166 if (tbl) 171 tbl->audit_machine = audit_det 167 tbl->audit_machine = audit_detect_machine(); 172 return tbl; 168 return tbl; 173 } 169 } 174 170 175 void syscalltbl__delete(struct syscalltbl *tbl 171 void syscalltbl__delete(struct syscalltbl *tbl) 176 { 172 { 177 free(tbl); 173 free(tbl); 178 } 174 } 179 175 180 const char *syscalltbl__name(const struct sysc 176 const char *syscalltbl__name(const struct syscalltbl *tbl, int id) 181 { 177 { 182 return audit_syscall_to_name(id, tbl-> 178 return audit_syscall_to_name(id, tbl->audit_machine); 183 } 179 } 184 180 185 int syscalltbl__id(struct syscalltbl *tbl, con 181 int syscalltbl__id(struct syscalltbl *tbl, const char *name) 186 { 182 { 187 return audit_name_to_syscall(name, tbl 183 return audit_name_to_syscall(name, tbl->audit_machine); 188 } 184 } 189 185 190 int syscalltbl__id_at_idx(struct syscalltbl *t 186 int syscalltbl__id_at_idx(struct syscalltbl *tbl __maybe_unused, int idx) 191 { 187 { 192 return idx; 188 return idx; 193 } 189 } 194 190 195 int syscalltbl__strglobmatch_next(struct sysca 191 int syscalltbl__strglobmatch_next(struct syscalltbl *tbl __maybe_unused, 196 const char * 192 const char *syscall_glob __maybe_unused, int *idx __maybe_unused) 197 { 193 { 198 return -1; 194 return -1; 199 } 195 } 200 196 201 int syscalltbl__strglobmatch_first(struct sysc 197 int syscalltbl__strglobmatch_first(struct syscalltbl *tbl, const char *syscall_glob, int *idx) 202 { 198 { 203 return syscalltbl__strglobmatch_next(t 199 return syscalltbl__strglobmatch_next(tbl, syscall_glob, idx); 204 } 200 } 205 #endif /* HAVE_SYSCALL_TABLE_SUPPORT */ 201 #endif /* HAVE_SYSCALL_TABLE_SUPPORT */ 206 202
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.