blob: 1ca4eec2ed73f8fea1c37c0cdb4f714e1594373a [file] [log] [blame]
Paul Lawrencedfe84342017-02-16 09:24:39 -08001/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "seccomp_policy.h"
18
Paul Lawrence89fa81f2017-02-17 10:22:03 -080019#include <assert.h>
Paul Lawrencedfe84342017-02-16 09:24:39 -080020#include <linux/audit.h>
Paul Lawrencedfe84342017-02-16 09:24:39 -080021#include <linux/seccomp.h>
22#include <sys/prctl.h>
Martijn Coenend269d9b2018-11-08 16:41:42 +010023#include <sys/syscall.h>
Paul Lawrencedfe84342017-02-16 09:24:39 -080024
25#include <vector>
26
27#include <android-base/logging.h>
Elliott Hughes704772b2022-10-10 17:06:43 +000028#include <android-base/macros.h>
Paul Lawrencedfe84342017-02-16 09:24:39 -080029
Martijn Coenend269d9b2018-11-08 16:41:42 +010030#include "func_to_syscall_nrs.h"
Paul Lawrence89fa81f2017-02-17 10:22:03 -080031#include "seccomp_bpfs.h"
32
Paul Lawrencedfe84342017-02-16 09:24:39 -080033#if defined __arm__ || defined __aarch64__
34
Paul Lawrence89fa81f2017-02-17 10:22:03 -080035#define PRIMARY_ARCH AUDIT_ARCH_AARCH64
Victor Hsieh4f02dd52017-12-20 09:19:22 -080036static const struct sock_filter* primary_app_filter = arm64_app_filter;
37static const size_t primary_app_filter_size = arm64_app_filter_size;
Martijn Coenenc3752be2019-01-09 16:19:57 +010038static const struct sock_filter* primary_app_zygote_filter = arm64_app_zygote_filter;
39static const size_t primary_app_zygote_filter_size = arm64_app_zygote_filter_size;
Victor Hsieh4f02dd52017-12-20 09:19:22 -080040static const struct sock_filter* primary_system_filter = arm64_system_filter;
41static const size_t primary_system_filter_size = arm64_system_filter_size;
Martijn Coenend269d9b2018-11-08 16:41:42 +010042
43static const long primary_setresgid = __arm64_setresgid;
44static const long primary_setresuid = __arm64_setresuid;
Paul Lawrence89fa81f2017-02-17 10:22:03 -080045#define SECONDARY_ARCH AUDIT_ARCH_ARM
Victor Hsieh4f02dd52017-12-20 09:19:22 -080046static const struct sock_filter* secondary_app_filter = arm_app_filter;
47static const size_t secondary_app_filter_size = arm_app_filter_size;
Martijn Coenenc3752be2019-01-09 16:19:57 +010048static const struct sock_filter* secondary_app_zygote_filter = arm_app_zygote_filter;
49static const size_t secondary_app_zygote_filter_size = arm_app_zygote_filter_size;
Victor Hsieh4f02dd52017-12-20 09:19:22 -080050static const struct sock_filter* secondary_system_filter = arm_system_filter;
51static const size_t secondary_system_filter_size = arm_system_filter_size;
Paul Lawrence89fa81f2017-02-17 10:22:03 -080052
Martijn Coenend269d9b2018-11-08 16:41:42 +010053static const long secondary_setresgid = __arm_setresgid;
54static const long secondary_setresuid = __arm_setresuid;
Elliott Hughes704772b2022-10-10 17:06:43 +000055
Paul Lawrence89fa81f2017-02-17 10:22:03 -080056#elif defined __i386__ || defined __x86_64__
57
Paul Lawrence89fa81f2017-02-17 10:22:03 -080058#define PRIMARY_ARCH AUDIT_ARCH_X86_64
Victor Hsieh4f02dd52017-12-20 09:19:22 -080059static const struct sock_filter* primary_app_filter = x86_64_app_filter;
60static const size_t primary_app_filter_size = x86_64_app_filter_size;
Martijn Coenenc3752be2019-01-09 16:19:57 +010061static const struct sock_filter* primary_app_zygote_filter = x86_64_app_zygote_filter;
62static const size_t primary_app_zygote_filter_size = x86_64_app_zygote_filter_size;
Victor Hsieh4f02dd52017-12-20 09:19:22 -080063static const struct sock_filter* primary_system_filter = x86_64_system_filter;
64static const size_t primary_system_filter_size = x86_64_system_filter_size;
Martijn Coenend269d9b2018-11-08 16:41:42 +010065
66static const long primary_setresgid = __x86_64_setresgid;
67static const long primary_setresuid = __x86_64_setresuid;
Paul Lawrence89fa81f2017-02-17 10:22:03 -080068#define SECONDARY_ARCH AUDIT_ARCH_I386
Victor Hsieh4f02dd52017-12-20 09:19:22 -080069static const struct sock_filter* secondary_app_filter = x86_app_filter;
70static const size_t secondary_app_filter_size = x86_app_filter_size;
Martijn Coenenc3752be2019-01-09 16:19:57 +010071static const struct sock_filter* secondary_app_zygote_filter = x86_app_zygote_filter;
72static const size_t secondary_app_zygote_filter_size = x86_app_zygote_filter_size;
Victor Hsieh4f02dd52017-12-20 09:19:22 -080073static const struct sock_filter* secondary_system_filter = x86_system_filter;
74static const size_t secondary_system_filter_size = x86_system_filter_size;
Paul Lawrence89fa81f2017-02-17 10:22:03 -080075
Martijn Coenend269d9b2018-11-08 16:41:42 +010076static const long secondary_setresgid = __x86_setresgid;
77static const long secondary_setresuid = __x86_setresuid;
Elliott Hughes704772b2022-10-10 17:06:43 +000078
79#elif defined(__riscv)
80
81#define PRIMARY_ARCH AUDIT_ARCH_RISCV64
82static const struct sock_filter* primary_app_filter = riscv64_app_filter;
83static const size_t primary_app_filter_size = riscv64_app_filter_size;
84static const struct sock_filter* primary_app_zygote_filter = riscv64_app_zygote_filter;
85static const size_t primary_app_zygote_filter_size = riscv64_app_zygote_filter_size;
86static const struct sock_filter* primary_system_filter = riscv64_system_filter;
87static const size_t primary_system_filter_size = riscv64_system_filter_size;
88
89static const long primary_setresgid = __riscv64_setresgid;
90static const long primary_setresuid = __riscv64_setresuid;
91
Paul Lawrence89fa81f2017-02-17 10:22:03 -080092#else
93#error No architecture was defined!
94#endif
95
Paul Lawrencedfe84342017-02-16 09:24:39 -080096
97#define syscall_nr (offsetof(struct seccomp_data, nr))
Martijn Coenend269d9b2018-11-08 16:41:42 +010098#define syscall_arg(_n) (offsetof(struct seccomp_data, args[_n]))
Paul Lawrencedfe84342017-02-16 09:24:39 -080099#define arch_nr (offsetof(struct seccomp_data, arch))
100
101typedef std::vector<sock_filter> filter;
102
Martijn Coenend269d9b2018-11-08 16:41:42 +0100103inline void Allow(filter& f) {
104 f.push_back(BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW));
105}
106
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800107inline void Disallow(filter& f) {
Paul Lawrencedfe84342017-02-16 09:24:39 -0800108 f.push_back(BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRAP));
109}
110
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800111static void ExamineSyscall(filter& f) {
Paul Lawrencedfe84342017-02-16 09:24:39 -0800112 f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, syscall_nr));
113}
114
Elliott Hughes704772b2022-10-10 17:06:43 +0000115#if defined(SECONDARY_ARCH)
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800116static bool SetValidateArchitectureJumpTarget(size_t offset, filter& f) {
Paul Lawrencedfe84342017-02-16 09:24:39 -0800117 size_t jump_length = f.size() - offset - 1;
118 auto u8_jump_length = (__u8) jump_length;
119 if (u8_jump_length != jump_length) {
120 LOG(FATAL)
121 << "Can't set jump greater than 255 - actual jump is " << jump_length;
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800122 return false;
Paul Lawrencedfe84342017-02-16 09:24:39 -0800123 }
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800124 f[offset] = BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SECONDARY_ARCH, u8_jump_length, 0);
125 return true;
Paul Lawrencedfe84342017-02-16 09:24:39 -0800126}
127
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800128static size_t ValidateArchitectureAndJumpIfNeeded(filter& f) {
Paul Lawrencedfe84342017-02-16 09:24:39 -0800129 f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, arch_nr));
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800130 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, PRIMARY_ARCH, 2, 0));
131 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SECONDARY_ARCH, 1, 0));
132 Disallow(f);
Paul Lawrencedfe84342017-02-16 09:24:39 -0800133 return f.size() - 2;
134}
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800135#else
136static void ValidateArchitecture(filter& f) {
137 f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, arch_nr));
138 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, PRIMARY_ARCH, 1, 0));
139 Disallow(f);
140}
141#endif
Paul Lawrencedfe84342017-02-16 09:24:39 -0800142
Martijn Coenend269d9b2018-11-08 16:41:42 +0100143static void ValidateSyscallArgInRange(filter& f, __u32 arg_num, __u32 range_min, __u32 range_max) {
144 const __u32 syscall_arg = syscall_arg(arg_num);
145
146 if (range_max == UINT32_MAX) {
147 LOG(FATAL) << "range_max exceeds maximum argument range.";
148 return;
149 }
150
151 f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, syscall_arg));
152 f.push_back(BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, range_min, 0, 1));
153 f.push_back(BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, range_max + 1, 0, 1));
154 Disallow(f);
155}
156
Victor Hsiehdbb86702020-06-15 09:29:07 -0700157// This filter is meant to be installed in addition to a regular allowlist filter.
Martijn Coenend269d9b2018-11-08 16:41:42 +0100158// Therefore, it's default action has to be Allow, except when the evaluated
159// system call matches setresuid/setresgid and the arguments don't fall within the
160// passed in range.
161//
Victor Hsiehdbb86702020-06-15 09:29:07 -0700162// The regular allowlist only allows setresuid/setresgid for UID/GID changes, so
Martijn Coenend269d9b2018-11-08 16:41:42 +0100163// that's the only system call we need to check here. A CTS test ensures the other
164// calls will remain blocked.
165static void ValidateSetUidGid(filter& f, uint32_t uid_gid_min, uint32_t uid_gid_max, bool primary) {
Elliott Hughes704772b2022-10-10 17:06:43 +0000166#if defined(SECONDARY_ARCH)
167 __u32 setresuid_nr = primary ? primary_setresuid : secondary_setresuid;
168 __u32 setresgid_nr = primary ? primary_setresgid : secondary_setresgid;
169#else
170 __u32 setresuid_nr = primary_setresuid;
171 __u32 setresgid_nr = primary_setresgid;
172 UNUSED(primary);
173#endif
174
Martijn Coenend269d9b2018-11-08 16:41:42 +0100175 // Check setresuid(ruid, euid, sguid) fall within range
176 ExamineSyscall(f);
Martijn Coenend269d9b2018-11-08 16:41:42 +0100177 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, setresuid_nr, 0, 12));
178 for (int arg = 0; arg < 3; arg++) {
179 ValidateSyscallArgInRange(f, arg, uid_gid_min, uid_gid_max);
180 }
181
182 // Check setresgid(rgid, egid, sgid) fall within range
183 ExamineSyscall(f);
Martijn Coenend269d9b2018-11-08 16:41:42 +0100184 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, setresgid_nr, 0, 12));
185 for (int arg = 0; arg < 3; arg++) {
186 ValidateSyscallArgInRange(f, arg, uid_gid_min, uid_gid_max);
187 }
188
189 // Default is to allow; other filters may still reject this call.
190 Allow(f);
191}
192
Paul Lawrencedfe84342017-02-16 09:24:39 -0800193static bool install_filter(filter const& f) {
194 struct sock_fprog prog = {
195 static_cast<unsigned short>(f.size()),
196 const_cast<struct sock_filter*>(&f[0]),
197 };
Victor Hsiehdab45ad2018-01-15 11:04:26 -0800198 // This assumes either the current process has CAP_SYS_ADMIN, or PR_SET_NO_NEW_PRIVS bit is set.
Paul Lawrencedfe84342017-02-16 09:24:39 -0800199 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) < 0) {
200 PLOG(FATAL) << "Could not set seccomp filter of size " << f.size();
201 return false;
202 }
Paul Lawrencedfe84342017-02-16 09:24:39 -0800203 return true;
204}
205
Martijn Coenend269d9b2018-11-08 16:41:42 +0100206bool _install_setuidgid_filter(uint32_t uid_gid_min, uint32_t uid_gid_max) {
207 filter f;
Elliott Hughes704772b2022-10-10 17:06:43 +0000208#if defined(SECONDARY_ARCH)
Martijn Coenend269d9b2018-11-08 16:41:42 +0100209 // Note that for mixed 64/32 bit architectures, ValidateArchitecture inserts a
210 // jump that must be changed to point to the start of the 32-bit policy
211 // 32 bit syscalls will not hit the policy between here and the call to SetJump
212 auto offset_to_secondary_filter = ValidateArchitectureAndJumpIfNeeded(f);
213#else
214 ValidateArchitecture(f);
215#endif
216
217 ValidateSetUidGid(f, uid_gid_min, uid_gid_max, true /* primary */);
218
Elliott Hughes704772b2022-10-10 17:06:43 +0000219#if defined(SECONDARY_ARCH)
Martijn Coenend269d9b2018-11-08 16:41:42 +0100220 if (!SetValidateArchitectureJumpTarget(offset_to_secondary_filter, f)) {
221 return false;
222 }
223
224 ValidateSetUidGid(f, uid_gid_min, uid_gid_max, false /* primary */);
225#endif
226
227 return install_filter(f);
228}
229
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800230enum FilterType {
231 APP,
Martijn Coenenc3752be2019-01-09 16:19:57 +0100232 APP_ZYGOTE,
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800233 SYSTEM,
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800234};
235
236bool _set_seccomp_filter(FilterType type) {
Paul Lawrencedfe84342017-02-16 09:24:39 -0800237 filter f;
238
Elliott Hughes704772b2022-10-10 17:06:43 +0000239 const sock_filter* p;
240 size_t p_size;
241#if defined(SECONDARY_ARCH)
242 const sock_filter* s;
243 size_t s_size;
244#endif
245
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800246 switch (type) {
247 case APP:
248 p = primary_app_filter;
249 p_size = primary_app_filter_size;
Elliott Hughes704772b2022-10-10 17:06:43 +0000250#if defined(SECONDARY_ARCH)
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800251 s = secondary_app_filter;
252 s_size = secondary_app_filter_size;
Elliott Hughes704772b2022-10-10 17:06:43 +0000253#endif
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800254 break;
Martijn Coenenc3752be2019-01-09 16:19:57 +0100255 case APP_ZYGOTE:
256 p = primary_app_zygote_filter;
257 p_size = primary_app_zygote_filter_size;
Elliott Hughes704772b2022-10-10 17:06:43 +0000258#if defined(SECONDARY_ARCH)
Martijn Coenenc3752be2019-01-09 16:19:57 +0100259 s = secondary_app_zygote_filter;
260 s_size = secondary_app_zygote_filter_size;
Elliott Hughes704772b2022-10-10 17:06:43 +0000261#endif
Martijn Coenenc3752be2019-01-09 16:19:57 +0100262 break;
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800263 case SYSTEM:
264 p = primary_system_filter;
265 p_size = primary_system_filter_size;
Elliott Hughes704772b2022-10-10 17:06:43 +0000266#if defined(SECONDARY_ARCH)
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800267 s = secondary_system_filter;
268 s_size = secondary_system_filter_size;
Elliott Hughes704772b2022-10-10 17:06:43 +0000269#endif
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800270 break;
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700271 }
272
Elliott Hughes704772b2022-10-10 17:06:43 +0000273#if defined(SECONDARY_ARCH)
Paul Lawrencedfe84342017-02-16 09:24:39 -0800274 // Note that for mixed 64/32 bit architectures, ValidateArchitecture inserts a
275 // jump that must be changed to point to the start of the 32-bit policy
276 // 32 bit syscalls will not hit the policy between here and the call to SetJump
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800277 auto offset_to_secondary_filter = ValidateArchitectureAndJumpIfNeeded(f);
278#else
279 ValidateArchitecture(f);
280#endif
Paul Lawrencedfe84342017-02-16 09:24:39 -0800281
Paul Lawrencedfe84342017-02-16 09:24:39 -0800282 ExamineSyscall(f);
283
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700284 for (size_t i = 0; i < p_size; ++i) {
285 f.push_back(p[i]);
Paul Lawrencedfe84342017-02-16 09:24:39 -0800286 }
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800287 Disallow(f);
Paul Lawrencedfe84342017-02-16 09:24:39 -0800288
Elliott Hughes704772b2022-10-10 17:06:43 +0000289#if defined(SECONDARY_ARCH)
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800290 if (!SetValidateArchitectureJumpTarget(offset_to_secondary_filter, f)) {
291 return false;
Paul Lawrencedfe84342017-02-16 09:24:39 -0800292 }
293
Paul Lawrencedfe84342017-02-16 09:24:39 -0800294 ExamineSyscall(f);
295
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700296 for (size_t i = 0; i < s_size; ++i) {
297 f.push_back(s[i]);
Paul Lawrencedfe84342017-02-16 09:24:39 -0800298 }
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800299 Disallow(f);
300#endif
Paul Lawrencedfe84342017-02-16 09:24:39 -0800301
302 return install_filter(f);
303}
Paul Lawrence26f57b62017-03-27 15:38:37 -0700304
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800305bool set_app_seccomp_filter() {
306 return _set_seccomp_filter(FilterType::APP);
307}
308
Martijn Coenenc3752be2019-01-09 16:19:57 +0100309bool set_app_zygote_seccomp_filter() {
310 return _set_seccomp_filter(FilterType::APP_ZYGOTE);
311}
312
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800313bool set_system_seccomp_filter() {
314 return _set_seccomp_filter(FilterType::SYSTEM);
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700315}
316
Martijn Coenend269d9b2018-11-08 16:41:42 +0100317bool install_setuidgid_seccomp_filter(uint32_t uid_gid_min, uint32_t uid_gid_max) {
318 return _install_setuidgid_filter(uid_gid_min, uid_gid_max);
319}