blob: 3393d7ee940141c62158410ee90f4a3dee691b51 [file] [log] [blame]
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -03001/*
2 * System call table mapper
3 *
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.
14 */
15
16#include "syscalltbl.h"
Arnaldo Carvalho de Melo5af56fa2016-04-04 17:52:18 -030017#include <stdlib.h>
Akemi Yagi090657c2017-09-22 22:11:53 +000018#include <linux/compiler.h>
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -030019
Jin Yao22e9af42018-04-09 18:26:48 +080020#ifdef HAVE_SYSCALL_TABLE_SUPPORT
Arnaldo Carvalho de Melo5af56fa2016-04-04 17:52:18 -030021#include <string.h>
Arnaldo Carvalho de Melo89be3f82017-08-31 11:46:49 -030022#include "string2.h"
Arnaldo Carvalho de Melo5af56fa2016-04-04 17:52:18 -030023#include "util.h"
24
Arnaldo Carvalho de Melo1b700c92016-04-04 19:05:36 -030025#if defined(__x86_64__)
26#include <asm/syscalls_64.c>
27const int syscalltbl_native_max_id = SYSCALLTBL_x86_64_MAX_ID;
28static const char **syscalltbl_native = syscalltbl_x86_64;
Hendrik Brueckner901bb022017-12-07 09:27:59 +010029#elif defined(__s390x__)
30#include <asm/syscalls_64.c>
31const int syscalltbl_native_max_id = SYSCALLTBL_S390_64_MAX_ID;
32static const char **syscalltbl_native = syscalltbl_s390_64;
Ravi Bangoria4281da22018-01-29 14:04:17 +053033#elif defined(__powerpc64__)
34#include <asm/syscalls_64.c>
35const int syscalltbl_native_max_id = SYSCALLTBL_POWERPC_64_MAX_ID;
36static const char **syscalltbl_native = syscalltbl_powerpc_64;
37#elif defined(__powerpc__)
38#include <asm/syscalls_32.c>
39const int syscalltbl_native_max_id = SYSCALLTBL_POWERPC_32_MAX_ID;
40static const char **syscalltbl_native = syscalltbl_powerpc_32;
Kim Phillipsa7f660d2018-07-06 16:34:54 -050041#elif defined(__aarch64__)
42#include <asm/syscalls.c>
43const int syscalltbl_native_max_id = SYSCALLTBL_ARM64_MAX_ID;
44static const char **syscalltbl_native = syscalltbl_arm64;
Arnaldo Carvalho de Melo1b700c92016-04-04 19:05:36 -030045#endif
46
Arnaldo Carvalho de Melo5af56fa2016-04-04 17:52:18 -030047struct syscall {
48 int id;
49 const char *name;
50};
51
52static int syscallcmpname(const void *vkey, const void *ventry)
53{
54 const char *key = vkey;
55 const struct syscall *entry = ventry;
56
57 return strcmp(key, entry->name);
58}
59
60static int syscallcmp(const void *va, const void *vb)
61{
62 const struct syscall *a = va, *b = vb;
63
64 return strcmp(a->name, b->name);
65}
66
67static int syscalltbl__init_native(struct syscalltbl *tbl)
68{
69 int nr_entries = 0, i, j;
70 struct syscall *entries;
71
72 for (i = 0; i <= syscalltbl_native_max_id; ++i)
73 if (syscalltbl_native[i])
74 ++nr_entries;
75
76 entries = tbl->syscalls.entries = malloc(sizeof(struct syscall) * nr_entries);
77 if (tbl->syscalls.entries == NULL)
78 return -1;
79
80 for (i = 0, j = 0; i <= syscalltbl_native_max_id; ++i) {
81 if (syscalltbl_native[i]) {
82 entries[j].name = syscalltbl_native[i];
83 entries[j].id = i;
84 ++j;
85 }
86 }
87
88 qsort(tbl->syscalls.entries, nr_entries, sizeof(struct syscall), syscallcmp);
89 tbl->syscalls.nr_entries = nr_entries;
90 return 0;
91}
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -030092
93struct syscalltbl *syscalltbl__new(void)
94{
95 struct syscalltbl *tbl = malloc(sizeof(*tbl));
96 if (tbl) {
Arnaldo Carvalho de Melo5af56fa2016-04-04 17:52:18 -030097 if (syscalltbl__init_native(tbl)) {
98 free(tbl);
99 return NULL;
100 }
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -0300101 }
102 return tbl;
103}
104
105void syscalltbl__delete(struct syscalltbl *tbl)
106{
Arnaldo Carvalho de Melo5af56fa2016-04-04 17:52:18 -0300107 zfree(&tbl->syscalls.entries);
108 free(tbl);
109}
110
111const char *syscalltbl__name(const struct syscalltbl *tbl __maybe_unused, int id)
112{
113 return id <= syscalltbl_native_max_id ? syscalltbl_native[id]: NULL;
114}
115
116int syscalltbl__id(struct syscalltbl *tbl, const char *name)
117{
118 struct syscall *sc = bsearch(name, tbl->syscalls.entries,
119 tbl->syscalls.nr_entries, sizeof(*sc),
120 syscallcmpname);
121
122 return sc ? sc->id : -1;
123}
124
Arnaldo Carvalho de Melo89be3f82017-08-31 11:46:49 -0300125int syscalltbl__strglobmatch_next(struct syscalltbl *tbl, const char *syscall_glob, int *idx)
126{
127 int i;
128 struct syscall *syscalls = tbl->syscalls.entries;
129
130 for (i = *idx + 1; i < tbl->syscalls.nr_entries; ++i) {
131 if (strglobmatch(syscalls[i].name, syscall_glob)) {
132 *idx = i;
133 return syscalls[i].id;
134 }
135 }
136
137 return -1;
138}
139
140int syscalltbl__strglobmatch_first(struct syscalltbl *tbl, const char *syscall_glob, int *idx)
141{
142 *idx = -1;
143 return syscalltbl__strglobmatch_next(tbl, syscall_glob, idx);
144}
145
Jin Yao22e9af42018-04-09 18:26:48 +0800146#else /* HAVE_SYSCALL_TABLE_SUPPORT */
Arnaldo Carvalho de Melo5af56fa2016-04-04 17:52:18 -0300147
148#include <libaudit.h>
149
150struct syscalltbl *syscalltbl__new(void)
151{
152 struct syscalltbl *tbl = malloc(sizeof(*tbl));
153 if (tbl)
154 tbl->audit_machine = audit_detect_machine();
155 return tbl;
156}
157
158void syscalltbl__delete(struct syscalltbl *tbl)
159{
Arnaldo Carvalho de Melofd0db102016-04-04 13:32:20 -0300160 free(tbl);
161}
162
163const char *syscalltbl__name(const struct syscalltbl *tbl, int id)
164{
165 return audit_syscall_to_name(id, tbl->audit_machine);
166}
167
168int syscalltbl__id(struct syscalltbl *tbl, const char *name)
169{
170 return audit_name_to_syscall(name, tbl->audit_machine);
171}
Arnaldo Carvalho de Melo89be3f82017-08-31 11:46:49 -0300172
173int syscalltbl__strglobmatch_next(struct syscalltbl *tbl __maybe_unused,
174 const char *syscall_glob __maybe_unused, int *idx __maybe_unused)
175{
176 return -1;
177}
178
179int syscalltbl__strglobmatch_first(struct syscalltbl *tbl, const char *syscall_glob, int *idx)
180{
181 return syscalltbl__strglobmatch_next(tbl, syscall_glob, idx);
182}
Jin Yao22e9af42018-04-09 18:26:48 +0800183#endif /* HAVE_SYSCALL_TABLE_SUPPORT */