0001
0002
0003
0004
0005
0006
0007
0008 #include "syscalltbl.h"
0009 #include <stdlib.h>
0010 #include <linux/compiler.h>
0011 #include <linux/zalloc.h>
0012
0013 #ifdef HAVE_SYSCALL_TABLE_SUPPORT
0014 #include <string.h>
0015 #include "string2.h"
0016
0017 #if defined(__x86_64__)
0018 #include <asm/syscalls_64.c>
0019 const int syscalltbl_native_max_id = SYSCALLTBL_x86_64_MAX_ID;
0020 static const char **syscalltbl_native = syscalltbl_x86_64;
0021 #elif defined(__s390x__)
0022 #include <asm/syscalls_64.c>
0023 const int syscalltbl_native_max_id = SYSCALLTBL_S390_64_MAX_ID;
0024 static const char **syscalltbl_native = syscalltbl_s390_64;
0025 #elif defined(__powerpc64__)
0026 #include <asm/syscalls_64.c>
0027 const int syscalltbl_native_max_id = SYSCALLTBL_POWERPC_64_MAX_ID;
0028 static const char **syscalltbl_native = syscalltbl_powerpc_64;
0029 #elif defined(__powerpc__)
0030 #include <asm/syscalls_32.c>
0031 const int syscalltbl_native_max_id = SYSCALLTBL_POWERPC_32_MAX_ID;
0032 static const char **syscalltbl_native = syscalltbl_powerpc_32;
0033 #elif defined(__aarch64__)
0034 #include <asm/syscalls.c>
0035 const int syscalltbl_native_max_id = SYSCALLTBL_ARM64_MAX_ID;
0036 static const char **syscalltbl_native = syscalltbl_arm64;
0037 #elif defined(__mips__)
0038 #include <asm/syscalls_n64.c>
0039 const int syscalltbl_native_max_id = SYSCALLTBL_MIPS_N64_MAX_ID;
0040 static const char **syscalltbl_native = syscalltbl_mips_n64;
0041 #endif
0042
0043 struct syscall {
0044 int id;
0045 const char *name;
0046 };
0047
0048 static int syscallcmpname(const void *vkey, const void *ventry)
0049 {
0050 const char *key = vkey;
0051 const struct syscall *entry = ventry;
0052
0053 return strcmp(key, entry->name);
0054 }
0055
0056 static int syscallcmp(const void *va, const void *vb)
0057 {
0058 const struct syscall *a = va, *b = vb;
0059
0060 return strcmp(a->name, b->name);
0061 }
0062
0063 static int syscalltbl__init_native(struct syscalltbl *tbl)
0064 {
0065 int nr_entries = 0, i, j;
0066 struct syscall *entries;
0067
0068 for (i = 0; i <= syscalltbl_native_max_id; ++i)
0069 if (syscalltbl_native[i])
0070 ++nr_entries;
0071
0072 entries = tbl->syscalls.entries = malloc(sizeof(struct syscall) * nr_entries);
0073 if (tbl->syscalls.entries == NULL)
0074 return -1;
0075
0076 for (i = 0, j = 0; i <= syscalltbl_native_max_id; ++i) {
0077 if (syscalltbl_native[i]) {
0078 entries[j].name = syscalltbl_native[i];
0079 entries[j].id = i;
0080 ++j;
0081 }
0082 }
0083
0084 qsort(tbl->syscalls.entries, nr_entries, sizeof(struct syscall), syscallcmp);
0085 tbl->syscalls.nr_entries = nr_entries;
0086 tbl->syscalls.max_id = syscalltbl_native_max_id;
0087 return 0;
0088 }
0089
0090 struct syscalltbl *syscalltbl__new(void)
0091 {
0092 struct syscalltbl *tbl = malloc(sizeof(*tbl));
0093 if (tbl) {
0094 if (syscalltbl__init_native(tbl)) {
0095 free(tbl);
0096 return NULL;
0097 }
0098 }
0099 return tbl;
0100 }
0101
0102 void syscalltbl__delete(struct syscalltbl *tbl)
0103 {
0104 zfree(&tbl->syscalls.entries);
0105 free(tbl);
0106 }
0107
0108 const char *syscalltbl__name(const struct syscalltbl *tbl __maybe_unused, int id)
0109 {
0110 return id <= syscalltbl_native_max_id ? syscalltbl_native[id]: NULL;
0111 }
0112
0113 int syscalltbl__id(struct syscalltbl *tbl, const char *name)
0114 {
0115 struct syscall *sc = bsearch(name, tbl->syscalls.entries,
0116 tbl->syscalls.nr_entries, sizeof(*sc),
0117 syscallcmpname);
0118
0119 return sc ? sc->id : -1;
0120 }
0121
0122 int syscalltbl__strglobmatch_next(struct syscalltbl *tbl, const char *syscall_glob, int *idx)
0123 {
0124 int i;
0125 struct syscall *syscalls = tbl->syscalls.entries;
0126
0127 for (i = *idx + 1; i < tbl->syscalls.nr_entries; ++i) {
0128 if (strglobmatch(syscalls[i].name, syscall_glob)) {
0129 *idx = i;
0130 return syscalls[i].id;
0131 }
0132 }
0133
0134 return -1;
0135 }
0136
0137 int syscalltbl__strglobmatch_first(struct syscalltbl *tbl, const char *syscall_glob, int *idx)
0138 {
0139 *idx = -1;
0140 return syscalltbl__strglobmatch_next(tbl, syscall_glob, idx);
0141 }
0142
0143 #else
0144
0145 #include <libaudit.h>
0146
0147 struct syscalltbl *syscalltbl__new(void)
0148 {
0149 struct syscalltbl *tbl = zalloc(sizeof(*tbl));
0150 if (tbl)
0151 tbl->audit_machine = audit_detect_machine();
0152 return tbl;
0153 }
0154
0155 void syscalltbl__delete(struct syscalltbl *tbl)
0156 {
0157 free(tbl);
0158 }
0159
0160 const char *syscalltbl__name(const struct syscalltbl *tbl, int id)
0161 {
0162 return audit_syscall_to_name(id, tbl->audit_machine);
0163 }
0164
0165 int syscalltbl__id(struct syscalltbl *tbl, const char *name)
0166 {
0167 return audit_name_to_syscall(name, tbl->audit_machine);
0168 }
0169
0170 int syscalltbl__strglobmatch_next(struct syscalltbl *tbl __maybe_unused,
0171 const char *syscall_glob __maybe_unused, int *idx __maybe_unused)
0172 {
0173 return -1;
0174 }
0175
0176 int syscalltbl__strglobmatch_first(struct syscalltbl *tbl, const char *syscall_glob, int *idx)
0177 {
0178 return syscalltbl__strglobmatch_next(tbl, syscall_glob, idx);
0179 }
0180 #endif