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