projects/guix·blob javascript-team · refs

223 lines · 10.2 KiB

1#pragma once
2
3#include "util.hh"
4#include <cstdint>
5#include <linux/audit.h> /* For AUDIT_ARCH_* */
6#include <linux/seccomp.h>
7#include <linux/filter.h>
8
9
10/* This file provides two preprocessor macros (among other things):
11 1. AUDIT_ARCH_NATIVE, which evaluates to whichever of the AUDIT_ARCH_*
12 values best represents the target system. Linux's internal headers have
13 a SECCOMP_ARCH_NATIVE since 2020, but it's not user-visible. Detection
14 of this is based on src/arch.c in libseccomp.
15 2. NATIVE_SYSCALL_RANGES, an array initializer for an array of two-element
16 objects, the first of which is an integral number representing the
17 start (inclusive) of a range of valid syscall numbers, and the second
18 of which is an integral number representing the end (inclusive) of that
19 range of valid syscall numbers. The ranges provided are all
20 non-overlapping and strictly ascending (that is, the start of a range is
21 strictly higher than any of the numbers in any of the ranges that
22 precede it). All numbers involved fit into a long.
23
24 These ranges were generated from the various syscall.tbl,
25 syscall_32.tbl, and syscall_64.tbl files lying around in the linux
26 kernel source. Some were derived from
27 include/uapi/asm-generic/unistd.h. The kernel source used was commit
28 b3ee1e460951 of https://github.com/torvalds/linux.git, read on
29 2025-04-23. Not all of the gaps in the files have any comments pointing
30 them out, so I recommend using build-aux/extract-syscall-ranges.sh for
31 the *.tbl files.
32
33 The intent behind saving these ranges is to be able to use a
34 default-allow seccomp policy that nevertheless disallows future
35 syscalls. This ensures that our security analysis can work with a
36 static, well-defined set of system calls that won't grow in the future
37 unless someone explicitly revisits the system call tables to consider
38 the implications of the new additions. */
39
40/* Both ends are inclusive. Some of the .tbl files use strange entries for
41 * the "abi" field, check arch/$ARCH/kernel/Makefile.syscalls to see what it
42 * specifies for syscall_abis_32 and syscall_abis_64 in addition to 32 or 64
43 * and "common" (added in Makefile.asm-headers). Also check what, if
44 * anything, the makefile uses as the --offset flag to syscallhdr.sh. And
45 * look at arch/$ARCH/include/uapi/asm/unistd.h to see what value the offset
46 * takes in what configurations. */
47
48#ifndef AUDIT_ARCH_NATIVE
49
50#if __i386__
51#define AUDIT_ARCH_NATIVE AUDIT_ARCH_I386
52#define NATIVE_SYSCALL_RANGES { {0, 221}, {224, 250}, {252, 284}, {286, 386}, \
53 {393, 414}, {416, 466} }
54#elif __x86_64__
55#define AUDIT_ARCH_NATIVE AUDIT_ARCH_X86_64
56#ifdef __ILP32__
57#include <asm/unistd.h>
58#define X32RANGE(low, high) { (low | __X32_SYSCALL_BIT), (high | __X32_SYSCALL_BIT) }
59#define NATIVE_SYSCALL_RANGES \
60 { X32RANGE(0, 12), X32RANGE(14, 14), X32RANGE(17, 18), X32RANGE(21, 44), X32RANGE(48, 53), \
61 X32RANGE(56, 58), X32RANGE(60, 100), X32RANGE(102, 126), X32RANGE(130, 130), \
62 X32RANGE(132, 133), X32RANGE(135, 155), X32RANGE(157, 173), X32RANGE(175, 176), \
63 X32RANGE(179, 179), X32RANGE(181, 204), X32RANGE(207, 208), X32RANGE(210, 210), \
64 X32RANGE(212, 213), X32RANGE(216, 221), X32RANGE(223, 235), X32RANGE(237, 243), \
65 X32RANGE(245, 245), X32RANGE(248, 272), X32RANGE(275, 277), X32RANGE(280, 294), \
66 X32RANGE(298, 298), X32RANGE(300, 306), X32RANGE(308, 309), X32RANGE(312, 321), \
67 X32RANGE(323, 326), X32RANGE(329, 335), X32RANGE(424, 466), X32RANGE(512, 547) }
68#else
69#define NATIVE_SYSCALL_RANGES { {0, 335}, {424, 466} }
70#endif
71#elif __arm__
72#define AUDIT_ARCH_NATIVE AUDIT_ARCH_ARM
73/* Note: there are at present 6 extra ARM syscall numbers not listed in
74 arch/arm/tools/syscall.tbl, namely __ARM_NR_breakpoint through
75 __ARM_NR_get_tls. */
76#ifdef __ARM_EABI__
77#include <asm/unistd.h>
78#define NATIVE_SYSCALL_RANGES \
79 { {0, 6}, {8, 12}, {14, 16}, {19, 21}, {23, 24}, {26, 26}, {29, 29}, \
80 {33, 34}, {36, 43}, {45, 47}, {49, 52}, {54, 55}, {57, 57}, {60, 67}, \
81 {70, 75}, {77, 81}, {83, 83}, {85, 88}, {91, 97}, {99, 100}, {103, 108}, \
82 {111, 111}, {114, 116}, {118, 122}, {124, 126}, {128, 129}, {131, 136}, \
83 {138, 165}, {168, 187}, {190, 221}, {224, 253}, {256, 401}, {403, 414}, \
84 {416, 446}, {448, 466}, {(__ARM_NR_BASE + 1), (__ARM_NR_BASE + 6)} }
85#else
86#include <asm/unistd.h>
87#define OABIRANGE(low, high) { (low | __NR_OABI_SYSCALL_BASE), (high | __NR_OABI_SYSCALL_BASE) }
88#define NATIVE_SYSCALL_RANGES \
89 { OABIRANGE(0, 6), OABIRANGE(8, 16), OABIRANGE(19, 27), OABIRANGE(29, 30), \
90 OABIRANGE(33, 34), OABIRANGE(36, 43), OABIRANGE(45, 47), OABIRANGE(49, 52), \
91 OABIRANGE(54, 55), OABIRANGE(57, 57), OABIRANGE(60, 67), OABIRANGE(70, 83), \
92 OABIRANGE(85, 97), OABIRANGE(99, 100), OABIRANGE(102, 108), \
93 OABIRANGE(111, 111), OABIRANGE(113, 122), OABIRANGE(124, 126), \
94 OABIRANGE(128, 129), OABIRANGE(131, 136), OABIRANGE(138, 165), \
95 OABIRANGE(168, 187), OABIRANGE(190, 221), OABIRANGE(224, 253), \
96 OABIRANGE(256, 401), OABIRANGE(403, 414), OABIRANGE(416, 446), \
97 OABIRANGE(448, 466), {(__ARM_NR_BASE + 1), (__ARM_NR_BASE + 6)} }
98#endif
99#elif __aarch64__
100#define AUDIT_ARCH_NATIVE AUDIT_ARCH_AARCH64
101/* extract-syscall-ranges.sh $LINUXSOURCE/arch/arm64/tools/syscall_64.tbl \
102 '64|renameat|rlimit|memfd_secret'
103
104 the extra ABIs are taken from arch/arm64/kernel/Makefile.syscalls and
105 scripts/Makefile.asm-headers */
106#define NATIVE_SYSCALL_RANGES { {0, 243}, {260, 294}, {424, 466} }
107/* To my knowledge there is no x32 equivalent for aarch64 in mainline linux */
108#elif __mips__ && _MIPS_SIM == _MIPS_SIM_ABI32
109/* o32 abi in both endianness cases */
110#include <asm/unistd.h>
111#define SYSRANGE(low, high) {(low) + __NR_Linux, (high) + __NR_Linux}
112#define NATIVE_SYSCALL_RANGES \
113 { SYSRANGE(0, 278), SYSRANGE(280, 368), SYSRANGE(393, 414), \
114 SYSRANGE(416, 446), SYSRANGE(448, 466) }
115#if __MIPSEB__
116#define AUDIT_ARCH_NATIVE AUDIT_ARCH_MIPS;
117#elif __MIPSEL__
118#define AUDIT_ARCH_NATIVE AUDIT_ARCH_MIPSEL
119#endif
120#elif __mips__ && _MIPS_SIM == _MIPS_SIM_ABI64
121/* n64 abi in both endianness cases */
122#include <asm/unistd.h>
123#define SYSRANGE(low, high) {(low) + __NR_Linux, (high) + __NR_Linux}
124#define NATIVE_SYSCALL_RANGES \
125 { SYSRANGE(0, 237), SYSRANGE(239, 328), SYSRANGE(424, 446), SYSRANGE(448, 466) }
126#if __MIPSEB__
127#define AUDIT_ARCH_NATIVE AUDIT_ARCH_MIPS64
128#elif __MIPSEL__
129#define AUDIT_ARCH_NATIVE AUDIT_ARCH_MIPSEL64
130#endif /* _MIPS_SIM_ABI64 */
131#elif __mips__ && _MIPS_SIM == _MIPS_SIM_NABI32
132/* n32 abi in both endianness cases */
133#include <asm/unistd.h>
134#define SYSRANGE(low, high) {(low) + __NR_Linux, (high) + __NR_Linux}
135#define NATIVE_SYSCALL_RANGES \
136 { SYSRANGE(0, 241), SYSRANGE(243, 332), SYSRANGE(403, 414), \
137 SYSRANGE(416, 446), SYSRANGE(448, 466) }
138#if __MIPSEB__
139#define AUDIT_ARCH_NATIVE AUDIT_ARCH_MIPS64N32
140#elif __MIPSEL__
141#define AUDIT_ARCH_NATIVE AUDIT_ARCH_MIPSEL64N32
142#endif /* _MIPS_SIM_NABI32 */
143#elif __hppa64__ /* hppa64 must be checked before hppa */
144#define NATIVE_SYSCALL_RANGES \
145 { {0, 101}, {103, 126}, {128, 129}, {131, 136}, {138, 166}, \
146 {168, 168}, {170, 195}, {198, 202}, {206, 212}, {215, 219}, \
147 {222, 262}, {264, 302}, {304, 356}, {424, 446}, {448, 466} }
148#define AUDIT_ARCH_NATIVE AUDIT_ARCH_PARISC64
149#elif __hppa__
150#define NATIVE_SYSCALL_RANGES \
151 { {0, 101}, {103, 126}, {128, 129}, {131, 136}, {138, 166}, \
152 {168, 168}, {170, 195}, {198, 202}, {206, 212}, {215, 219}, \
153 {222, 262}, {264, 302}, {304, 356}, {403, 414}, {416, 446}, {448, 466} }
154#define AUDIT_ARCH_NATIVE AUDIT_ARCH_PARISC
155#elif __PPC64__
156#define NATIVE_SYSCALL_RANGES \
157 { {0, 191}, {198, 203}, {205, 223}, {225, 225}, {227, 253}, \
158 {255, 256}, {258, 365}, {378, 388}, {392, 402}, {424, 446}, {448, 466} }
159#ifdef __BIG_ENDIAN__
160#define AUDIT_ARCH_NATIVE AUDIT_ARCH_PPC64
161#else
162#define AUDIT_ARCH_NATIVE AUDIT_ARCH_PPC64LE
163#endif
164#elif __PPC__
165#define NATIVE_SYSCALL_RANGES \
166 { {0, 223}, {225, 256}, {258, 365}, {378, 388}, {393, 414}, \
167 {416, 446}, {448, 466} }
168#define AUDIT_ARCH_NATIVE AUDIT_ARCH_PPC
169#elif __s390x__ /* s390x must be checked before s390 */
170#define NATIVE_SYSCALL_RANGES \
171 { {1, 12}, {14, 15}, {19, 22}, {26, 27}, {29, 30}, {33, 34}, \
172 {36, 43}, {45, 45}, {48, 48}, {51, 52}, {54, 55}, {57, 57}, \
173 {60, 67}, {72, 75}, {77, 79}, {83, 83}, {85, 94}, {96, 97}, \
174 {99, 100}, {102, 108}, {110, 112}, {114, 122}, {124, 137}, \
175 {141, 163}, {167, 169}, {172, 181}, {183, 191}, {198, 220}, \
176 {222, 222}, {224, 241}, {243, 262}, {265, 386}, {392, 402}, {424, 466} }
177#define AUDIT_ARCH_NATIVE AUDIT_ARCH_S390X
178#elif __s390__
179#define NATIVE_SYSCALL_RANGES \
180 { {1, 16}, {19, 27}, {29, 30}, {33, 34}, {36, 43}, {45, 52}, \
181 {54, 55}, {57, 57}, {60, 67}, {70, 81}, {83, 83}, {85, 97}, \
182 {99, 108}, {110, 112}, {114, 122}, {124, 165}, {167, 241}, \
183 {243, 262}, {264, 386}, {393, 414}, {416, 466} }
184#define AUDIT_ARCH_NATIVE AUDIT_ARCH_S390
185#elif __riscv && __riscv_xlen == 64
186#define NATIVE_SYSCALL_RANGES { {0, 37}, {39, 243}, {258, 294}, {424, 466} }
187#define AUDIT_ARCH_NATIVE AUDIT_ARCH_RISCV64
188#elif __riscv && __riscv_xlen == 32
189#define NATIVE_SYSCALL_RANGES \
190 { {0, 3}, {5, 37}, {39, 71}, {74, 78}, {81, 85}, {89, 97}, {99, 100}, \
191 {102, 107}, {109, 109}, {111, 111}, {116, 126}, {128, 136}, {138, 162}, \
192 {165, 168}, {172, 181}, {184, 191}, {193, 242}, {258, 259}, {261, 265}, \
193 {267, 291}, {293, 294}, {403, 414}, {416, 466} }
194#define AUDIT_ARCH_NATIVE AUDIT_ARCH_RISCV32
195#else
196#error cannot determine which AUDIT_ARCH_* value to use for AUDIT_ARCH_NATIVE
197#endif
198
199#else
200#ifndef NATIVE_SYSCALL_RANGES
201/* Fall back to default-allow if the user specified (with
202 -DAUDIT_ARCH_NATIVE=...) an arch but not NATIVE_SYSCALL_RANGES */
203#define NATIVE_SYSCALL_RANGES {}
204#endif
205#endif /* #ifndef AUDIT_ARCH_NATIVE */
206
207namespace nix {
208
209struct Uint32RangeAction {
210 uint32_t low; /* inclusive */
211 uint32_t high; /* inclusive */
212 std::vector<struct sock_filter> instructions;
213};
214
215std::vector<struct sock_filter> rangeActionsToFilter(std::vector<Uint32RangeAction> & ranges);
216
217
218std::vector<struct sock_filter>
219seccompMatchu64(std::vector<struct sock_filter> & out,
220 uint64_t value,
221 std::vector<struct sock_filter> instructions,
222 uint32_t offset);
223}