1 // SPDX-License-Identifier: GPL-2.0-only << 2 /* 1 /* 3 * System call table mapper 2 * System call table mapper 4 * 3 * 5 * (C) 2016 Arnaldo Carvalho de Melo <acme@red 4 * (C) 2016 Arnaldo Carvalho de Melo <acme@redhat.com> >> 5 * >> 6 * This program is free software; you can redistribute it and/or modify it >> 7 * under the terms and conditions of the GNU General Public License, >> 8 * version 2, as published by the Free Software Foundation. >> 9 * >> 10 * This program is distributed in the hope it will be useful, but WITHOUT >> 11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or >> 12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for >> 13 * more details. 6 */ 14 */ 7 15 8 #include "syscalltbl.h" 16 #include "syscalltbl.h" 9 #include <stdlib.h> 17 #include <stdlib.h> 10 #include <linux/compiler.h> 18 #include <linux/compiler.h> 11 #include <linux/zalloc.h> << 12 19 13 #ifdef HAVE_SYSCALL_TABLE_SUPPORT 20 #ifdef HAVE_SYSCALL_TABLE_SUPPORT 14 #include <string.h> 21 #include <string.h> 15 #include "string2.h" 22 #include "string2.h" >> 23 #include "util.h" 16 24 17 #if defined(__x86_64__) 25 #if defined(__x86_64__) 18 #include <asm/syscalls_64.c> 26 #include <asm/syscalls_64.c> 19 const int syscalltbl_native_max_id = SYSCALLTB 27 const int syscalltbl_native_max_id = SYSCALLTBL_x86_64_MAX_ID; 20 static const char *const *syscalltbl_native = !! 28 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__) 29 #elif defined(__s390x__) 26 #include <asm/syscalls_64.c> 30 #include <asm/syscalls_64.c> 27 const int syscalltbl_native_max_id = SYSCALLTB 31 const int syscalltbl_native_max_id = SYSCALLTBL_S390_64_MAX_ID; 28 static const char *const *syscalltbl_native = !! 32 static const char **syscalltbl_native = syscalltbl_s390_64; 29 #elif defined(__powerpc64__) 33 #elif defined(__powerpc64__) 30 #include <asm/syscalls_64.c> 34 #include <asm/syscalls_64.c> 31 const int syscalltbl_native_max_id = SYSCALLTB 35 const int syscalltbl_native_max_id = SYSCALLTBL_POWERPC_64_MAX_ID; 32 static const char *const *syscalltbl_native = !! 36 static const char **syscalltbl_native = syscalltbl_powerpc_64; 33 #elif defined(__powerpc__) 37 #elif defined(__powerpc__) 34 #include <asm/syscalls_32.c> 38 #include <asm/syscalls_32.c> 35 const int syscalltbl_native_max_id = SYSCALLTB 39 const int syscalltbl_native_max_id = SYSCALLTBL_POWERPC_32_MAX_ID; 36 static const char *const *syscalltbl_native = !! 40 static const char **syscalltbl_native = syscalltbl_powerpc_32; 37 #elif defined(__aarch64__) << 38 #include <asm/syscalls.c> << 39 const int syscalltbl_native_max_id = SYSCALLTB << 40 static const char *const *syscalltbl_native = << 41 #elif defined(__mips__) << 42 #include <asm/syscalls_n64.c> << 43 const int syscalltbl_native_max_id = SYSCALLTB << 44 static const char *const *syscalltbl_native = << 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_ << 100 return 0; 86 return 0; 101 } 87 } 102 88 103 struct syscalltbl *syscalltbl__new(void) 89 struct syscalltbl *syscalltbl__new(void) 104 { 90 { 105 struct syscalltbl *tbl = malloc(sizeof 91 struct syscalltbl *tbl = malloc(sizeof(*tbl)); 106 if (tbl) { 92 if (tbl) { 107 if (syscalltbl__init_native(tb 93 if (syscalltbl__init_native(tbl)) { 108 free(tbl); 94 free(tbl); 109 return NULL; 95 return NULL; 110 } 96 } 111 } 97 } 112 return tbl; 98 return tbl; 113 } 99 } 114 100 115 void syscalltbl__delete(struct syscalltbl *tbl 101 void syscalltbl__delete(struct syscalltbl *tbl) 116 { 102 { 117 zfree(&tbl->syscalls.entries); 103 zfree(&tbl->syscalls.entries); 118 free(tbl); 104 free(tbl); 119 } 105 } 120 106 121 const char *syscalltbl__name(const struct sysc 107 const char *syscalltbl__name(const struct syscalltbl *tbl __maybe_unused, int id) 122 { 108 { 123 return id <= syscalltbl_native_max_id 109 return id <= syscalltbl_native_max_id ? syscalltbl_native[id]: NULL; 124 } 110 } 125 111 126 int syscalltbl__id(struct syscalltbl *tbl, con 112 int syscalltbl__id(struct syscalltbl *tbl, const char *name) 127 { 113 { 128 struct syscall *sc = bsearch(name, tbl 114 struct syscall *sc = bsearch(name, tbl->syscalls.entries, 129 tbl->sysc 115 tbl->syscalls.nr_entries, sizeof(*sc), 130 syscallcm 116 syscallcmpname); 131 117 132 return sc ? sc->id : -1; 118 return sc ? sc->id : -1; 133 } 119 } 134 120 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 121 int syscalltbl__strglobmatch_next(struct syscalltbl *tbl, const char *syscall_glob, int *idx) 143 { 122 { 144 int i; 123 int i; 145 struct syscall *syscalls = tbl->syscal 124 struct syscall *syscalls = tbl->syscalls.entries; 146 125 147 for (i = *idx + 1; i < tbl->syscalls.n 126 for (i = *idx + 1; i < tbl->syscalls.nr_entries; ++i) { 148 if (strglobmatch(syscalls[i].n 127 if (strglobmatch(syscalls[i].name, syscall_glob)) { 149 *idx = i; 128 *idx = i; 150 return syscalls[i].id; 129 return syscalls[i].id; 151 } 130 } 152 } 131 } 153 132 154 return -1; 133 return -1; 155 } 134 } 156 135 157 int syscalltbl__strglobmatch_first(struct sysc 136 int syscalltbl__strglobmatch_first(struct syscalltbl *tbl, const char *syscall_glob, int *idx) 158 { 137 { 159 *idx = -1; 138 *idx = -1; 160 return syscalltbl__strglobmatch_next(t 139 return syscalltbl__strglobmatch_next(tbl, syscall_glob, idx); 161 } 140 } 162 141 163 #else /* HAVE_SYSCALL_TABLE_SUPPORT */ 142 #else /* HAVE_SYSCALL_TABLE_SUPPORT */ 164 143 165 #include <libaudit.h> 144 #include <libaudit.h> 166 145 167 struct syscalltbl *syscalltbl__new(void) 146 struct syscalltbl *syscalltbl__new(void) 168 { 147 { 169 struct syscalltbl *tbl = zalloc(sizeof !! 148 struct syscalltbl *tbl = malloc(sizeof(*tbl)); 170 if (tbl) 149 if (tbl) 171 tbl->audit_machine = audit_det 150 tbl->audit_machine = audit_detect_machine(); 172 return tbl; 151 return tbl; 173 } 152 } 174 153 175 void syscalltbl__delete(struct syscalltbl *tbl 154 void syscalltbl__delete(struct syscalltbl *tbl) 176 { 155 { 177 free(tbl); 156 free(tbl); 178 } 157 } 179 158 180 const char *syscalltbl__name(const struct sysc 159 const char *syscalltbl__name(const struct syscalltbl *tbl, int id) 181 { 160 { 182 return audit_syscall_to_name(id, tbl-> 161 return audit_syscall_to_name(id, tbl->audit_machine); 183 } 162 } 184 163 185 int syscalltbl__id(struct syscalltbl *tbl, con 164 int syscalltbl__id(struct syscalltbl *tbl, const char *name) 186 { 165 { 187 return audit_name_to_syscall(name, tbl 166 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 } 167 } 194 168 195 int syscalltbl__strglobmatch_next(struct sysca 169 int syscalltbl__strglobmatch_next(struct syscalltbl *tbl __maybe_unused, 196 const char * 170 const char *syscall_glob __maybe_unused, int *idx __maybe_unused) 197 { 171 { 198 return -1; 172 return -1; 199 } 173 } 200 174 201 int syscalltbl__strglobmatch_first(struct sysc 175 int syscalltbl__strglobmatch_first(struct syscalltbl *tbl, const char *syscall_glob, int *idx) 202 { 176 { 203 return syscalltbl__strglobmatch_next(t 177 return syscalltbl__strglobmatch_next(tbl, syscall_glob, idx); 204 } 178 } 205 #endif /* HAVE_SYSCALL_TABLE_SUPPORT */ 179 #endif /* HAVE_SYSCALL_TABLE_SUPPORT */ 206 180
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.