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