blob: 222a2c87ddcc76d83f8eec1fc42c47bcc36435d9 [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>
28
Martijn Coenend269d9b2018-11-08 16:41:42 +010029#include "func_to_syscall_nrs.h"
Paul Lawrence89fa81f2017-02-17 10:22:03 -080030#include "seccomp_bpfs.h"
31
Paul Lawrencedfe84342017-02-16 09:24:39 -080032#if defined __arm__ || defined __aarch64__
33
Paul Lawrence89fa81f2017-02-17 10:22:03 -080034#define DUAL_ARCH
35#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;
Steve Muckleaa3f96c2017-07-20 13:11:54 -070042static const struct sock_filter* primary_global_filter = arm64_global_filter;
43static const size_t primary_global_filter_size = arm64_global_filter_size;
Martijn Coenend269d9b2018-11-08 16:41:42 +010044
45static const long primary_setresgid = __arm64_setresgid;
46static const long primary_setresuid = __arm64_setresuid;
Paul Lawrence89fa81f2017-02-17 10:22:03 -080047#define SECONDARY_ARCH AUDIT_ARCH_ARM
Victor Hsieh4f02dd52017-12-20 09:19:22 -080048static const struct sock_filter* secondary_app_filter = arm_app_filter;
49static const size_t secondary_app_filter_size = arm_app_filter_size;
Martijn Coenenc3752be2019-01-09 16:19:57 +010050static const struct sock_filter* secondary_app_zygote_filter = arm_app_zygote_filter;
51static const size_t secondary_app_zygote_filter_size = arm_app_zygote_filter_size;
Victor Hsieh4f02dd52017-12-20 09:19:22 -080052static const struct sock_filter* secondary_system_filter = arm_system_filter;
53static const size_t secondary_system_filter_size = arm_system_filter_size;
Steve Muckleaa3f96c2017-07-20 13:11:54 -070054static const struct sock_filter* secondary_global_filter = arm_global_filter;
55static const size_t secondary_global_filter_size = arm_global_filter_size;
Paul Lawrence89fa81f2017-02-17 10:22:03 -080056
Martijn Coenend269d9b2018-11-08 16:41:42 +010057static const long secondary_setresgid = __arm_setresgid;
58static const long secondary_setresuid = __arm_setresuid;
Paul Lawrence89fa81f2017-02-17 10:22:03 -080059#elif defined __i386__ || defined __x86_64__
60
61#define DUAL_ARCH
62#define PRIMARY_ARCH AUDIT_ARCH_X86_64
Victor Hsieh4f02dd52017-12-20 09:19:22 -080063static const struct sock_filter* primary_app_filter = x86_64_app_filter;
64static const size_t primary_app_filter_size = x86_64_app_filter_size;
Martijn Coenenc3752be2019-01-09 16:19:57 +010065static const struct sock_filter* primary_app_zygote_filter = x86_64_app_zygote_filter;
66static const size_t primary_app_zygote_filter_size = x86_64_app_zygote_filter_size;
Victor Hsieh4f02dd52017-12-20 09:19:22 -080067static const struct sock_filter* primary_system_filter = x86_64_system_filter;
68static const size_t primary_system_filter_size = x86_64_system_filter_size;
Steve Muckleaa3f96c2017-07-20 13:11:54 -070069static const struct sock_filter* primary_global_filter = x86_64_global_filter;
70static const size_t primary_global_filter_size = x86_64_global_filter_size;
Martijn Coenend269d9b2018-11-08 16:41:42 +010071
72static const long primary_setresgid = __x86_64_setresgid;
73static const long primary_setresuid = __x86_64_setresuid;
Paul Lawrence89fa81f2017-02-17 10:22:03 -080074#define SECONDARY_ARCH AUDIT_ARCH_I386
Victor Hsieh4f02dd52017-12-20 09:19:22 -080075static const struct sock_filter* secondary_app_filter = x86_app_filter;
76static const size_t secondary_app_filter_size = x86_app_filter_size;
Martijn Coenenc3752be2019-01-09 16:19:57 +010077static const struct sock_filter* secondary_app_zygote_filter = x86_app_zygote_filter;
78static const size_t secondary_app_zygote_filter_size = x86_app_zygote_filter_size;
Victor Hsieh4f02dd52017-12-20 09:19:22 -080079static const struct sock_filter* secondary_system_filter = x86_system_filter;
80static const size_t secondary_system_filter_size = x86_system_filter_size;
Steve Muckleaa3f96c2017-07-20 13:11:54 -070081static const struct sock_filter* secondary_global_filter = x86_global_filter;
82static const size_t secondary_global_filter_size = x86_global_filter_size;
Paul Lawrence89fa81f2017-02-17 10:22:03 -080083
Martijn Coenend269d9b2018-11-08 16:41:42 +010084static const long secondary_setresgid = __x86_setresgid;
85static const long secondary_setresuid = __x86_setresuid;
Paul Lawrence89fa81f2017-02-17 10:22:03 -080086#elif defined __mips__ || defined __mips64__
87
88#define DUAL_ARCH
Lazar Trsic22b43512017-05-05 14:29:34 +020089#define PRIMARY_ARCH AUDIT_ARCH_MIPSEL64
Victor Hsieh4f02dd52017-12-20 09:19:22 -080090static const struct sock_filter* primary_app_filter = mips64_app_filter;
91static const size_t primary_app_filter_size = mips64_app_filter_size;
Martijn Coenenc3752be2019-01-09 16:19:57 +010092static const struct sock_filter* primary_app_zygote_filter = mips64_app_zygote_filter;
93static const size_t primary_app_zygote_filter_size = mips64_app_zygote_filter_size;
Victor Hsieh4f02dd52017-12-20 09:19:22 -080094static const struct sock_filter* primary_system_filter = mips64_system_filter;
95static const size_t primary_system_filter_size = mips64_system_filter_size;
Steve Muckleaa3f96c2017-07-20 13:11:54 -070096static const struct sock_filter* primary_global_filter = mips64_global_filter;
97static const size_t primary_global_filter_size = mips64_global_filter_size;
Martijn Coenend269d9b2018-11-08 16:41:42 +010098
99static const long primary_setresgid = __mips64_setresgid;
100static const long primary_setresuid = __mips64_setresuid;
Lazar Trsic22b43512017-05-05 14:29:34 +0200101#define SECONDARY_ARCH AUDIT_ARCH_MIPSEL
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800102static const struct sock_filter* secondary_app_filter = mips_app_filter;
103static const size_t secondary_app_filter_size = mips_app_filter_size;
Martijn Coenenc3752be2019-01-09 16:19:57 +0100104static const struct sock_filter* secondary_app_zygote_filter = mips_app_zygote_filter;
105static const size_t secondary_app_zygote_filter_size = mips_app_zygote_filter_size;
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800106static const struct sock_filter* secondary_system_filter = mips_system_filter;
107static const size_t secondary_system_filter_size = mips_system_filter_size;
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700108static const struct sock_filter* secondary_global_filter = mips_global_filter;
109static const size_t secondary_global_filter_size = mips_global_filter_size;
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800110
Martijn Coenend269d9b2018-11-08 16:41:42 +0100111static const long secondary_setresgid = __mips_setresgid;
112static const long secondary_setresuid = __mips_setresuid;
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800113#else
114#error No architecture was defined!
115#endif
116
Paul Lawrencedfe84342017-02-16 09:24:39 -0800117
118#define syscall_nr (offsetof(struct seccomp_data, nr))
Martijn Coenend269d9b2018-11-08 16:41:42 +0100119#define syscall_arg(_n) (offsetof(struct seccomp_data, args[_n]))
Paul Lawrencedfe84342017-02-16 09:24:39 -0800120#define arch_nr (offsetof(struct seccomp_data, arch))
121
122typedef std::vector<sock_filter> filter;
123
Martijn Coenend269d9b2018-11-08 16:41:42 +0100124inline void Allow(filter& f) {
125 f.push_back(BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW));
126}
127
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800128inline void Disallow(filter& f) {
Paul Lawrencedfe84342017-02-16 09:24:39 -0800129 f.push_back(BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRAP));
130}
131
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800132static void ExamineSyscall(filter& f) {
Paul Lawrencedfe84342017-02-16 09:24:39 -0800133 f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, syscall_nr));
134}
135
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800136#ifdef DUAL_ARCH
137static bool SetValidateArchitectureJumpTarget(size_t offset, filter& f) {
Paul Lawrencedfe84342017-02-16 09:24:39 -0800138 size_t jump_length = f.size() - offset - 1;
139 auto u8_jump_length = (__u8) jump_length;
140 if (u8_jump_length != jump_length) {
141 LOG(FATAL)
142 << "Can't set jump greater than 255 - actual jump is " << jump_length;
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800143 return false;
Paul Lawrencedfe84342017-02-16 09:24:39 -0800144 }
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800145 f[offset] = BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SECONDARY_ARCH, u8_jump_length, 0);
146 return true;
Paul Lawrencedfe84342017-02-16 09:24:39 -0800147}
148
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800149static size_t ValidateArchitectureAndJumpIfNeeded(filter& f) {
Paul Lawrencedfe84342017-02-16 09:24:39 -0800150 f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, arch_nr));
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800151 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, PRIMARY_ARCH, 2, 0));
152 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SECONDARY_ARCH, 1, 0));
153 Disallow(f);
Paul Lawrencedfe84342017-02-16 09:24:39 -0800154 return f.size() - 2;
155}
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800156#else
157static void ValidateArchitecture(filter& f) {
158 f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, arch_nr));
159 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, PRIMARY_ARCH, 1, 0));
160 Disallow(f);
161}
162#endif
Paul Lawrencedfe84342017-02-16 09:24:39 -0800163
Martijn Coenend269d9b2018-11-08 16:41:42 +0100164static void ValidateSyscallArgInRange(filter& f, __u32 arg_num, __u32 range_min, __u32 range_max) {
165 const __u32 syscall_arg = syscall_arg(arg_num);
166
167 if (range_max == UINT32_MAX) {
168 LOG(FATAL) << "range_max exceeds maximum argument range.";
169 return;
170 }
171
172 f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, syscall_arg));
173 f.push_back(BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, range_min, 0, 1));
174 f.push_back(BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, range_max + 1, 0, 1));
175 Disallow(f);
176}
177
178// This filter is meant to be installed in addition to a regular whitelist filter.
179// Therefore, it's default action has to be Allow, except when the evaluated
180// system call matches setresuid/setresgid and the arguments don't fall within the
181// passed in range.
182//
183// The regular whitelist only allows setresuid/setresgid for UID/GID changes, so
184// that's the only system call we need to check here. A CTS test ensures the other
185// calls will remain blocked.
186static void ValidateSetUidGid(filter& f, uint32_t uid_gid_min, uint32_t uid_gid_max, bool primary) {
187 // Check setresuid(ruid, euid, sguid) fall within range
188 ExamineSyscall(f);
189 __u32 setresuid_nr = primary ? primary_setresuid : secondary_setresuid;
190 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, setresuid_nr, 0, 12));
191 for (int arg = 0; arg < 3; arg++) {
192 ValidateSyscallArgInRange(f, arg, uid_gid_min, uid_gid_max);
193 }
194
195 // Check setresgid(rgid, egid, sgid) fall within range
196 ExamineSyscall(f);
197 __u32 setresgid_nr = primary ? primary_setresgid : secondary_setresgid;
198 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, setresgid_nr, 0, 12));
199 for (int arg = 0; arg < 3; arg++) {
200 ValidateSyscallArgInRange(f, arg, uid_gid_min, uid_gid_max);
201 }
202
203 // Default is to allow; other filters may still reject this call.
204 Allow(f);
205}
206
Paul Lawrencedfe84342017-02-16 09:24:39 -0800207static bool install_filter(filter const& f) {
208 struct sock_fprog prog = {
209 static_cast<unsigned short>(f.size()),
210 const_cast<struct sock_filter*>(&f[0]),
211 };
Victor Hsiehdab45ad2018-01-15 11:04:26 -0800212 // 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 -0800213 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) < 0) {
214 PLOG(FATAL) << "Could not set seccomp filter of size " << f.size();
215 return false;
216 }
Paul Lawrencedfe84342017-02-16 09:24:39 -0800217 return true;
218}
219
Martijn Coenend269d9b2018-11-08 16:41:42 +0100220bool _install_setuidgid_filter(uint32_t uid_gid_min, uint32_t uid_gid_max) {
221 filter f;
222#ifdef DUAL_ARCH
223 // Note that for mixed 64/32 bit architectures, ValidateArchitecture inserts a
224 // jump that must be changed to point to the start of the 32-bit policy
225 // 32 bit syscalls will not hit the policy between here and the call to SetJump
226 auto offset_to_secondary_filter = ValidateArchitectureAndJumpIfNeeded(f);
227#else
228 ValidateArchitecture(f);
229#endif
230
231 ValidateSetUidGid(f, uid_gid_min, uid_gid_max, true /* primary */);
232
233#ifdef DUAL_ARCH
234 if (!SetValidateArchitectureJumpTarget(offset_to_secondary_filter, f)) {
235 return false;
236 }
237
238 ValidateSetUidGid(f, uid_gid_min, uid_gid_max, false /* primary */);
239#endif
240
241 return install_filter(f);
242}
243
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800244enum FilterType {
245 APP,
Martijn Coenenc3752be2019-01-09 16:19:57 +0100246 APP_ZYGOTE,
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800247 SYSTEM,
248 GLOBAL
249};
250
251bool _set_seccomp_filter(FilterType type) {
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700252 const sock_filter *p, *s;
253 size_t p_size, s_size;
Paul Lawrencedfe84342017-02-16 09:24:39 -0800254 filter f;
255
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800256 switch (type) {
257 case APP:
258 p = primary_app_filter;
259 p_size = primary_app_filter_size;
260 s = secondary_app_filter;
261 s_size = secondary_app_filter_size;
262 break;
Martijn Coenenc3752be2019-01-09 16:19:57 +0100263 case APP_ZYGOTE:
264 p = primary_app_zygote_filter;
265 p_size = primary_app_zygote_filter_size;
266 s = secondary_app_zygote_filter;
267 s_size = secondary_app_zygote_filter_size;
268 break;
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800269 case SYSTEM:
270 p = primary_system_filter;
271 p_size = primary_system_filter_size;
272 s = secondary_system_filter;
273 s_size = secondary_system_filter_size;
274 break;
275 case GLOBAL:
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700276 p = primary_global_filter;
277 p_size = primary_global_filter_size;
278 s = secondary_global_filter;
279 s_size = secondary_global_filter_size;
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800280 break;
281
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700282 }
283
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800284#ifdef DUAL_ARCH
Paul Lawrencedfe84342017-02-16 09:24:39 -0800285 // Note that for mixed 64/32 bit architectures, ValidateArchitecture inserts a
286 // jump that must be changed to point to the start of the 32-bit policy
287 // 32 bit syscalls will not hit the policy between here and the call to SetJump
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800288 auto offset_to_secondary_filter = ValidateArchitectureAndJumpIfNeeded(f);
289#else
290 ValidateArchitecture(f);
291#endif
Paul Lawrencedfe84342017-02-16 09:24:39 -0800292
Paul Lawrencedfe84342017-02-16 09:24:39 -0800293 ExamineSyscall(f);
294
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700295 for (size_t i = 0; i < p_size; ++i) {
296 f.push_back(p[i]);
Paul Lawrencedfe84342017-02-16 09:24:39 -0800297 }
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800298 Disallow(f);
Paul Lawrencedfe84342017-02-16 09:24:39 -0800299
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800300#ifdef DUAL_ARCH
301 if (!SetValidateArchitectureJumpTarget(offset_to_secondary_filter, f)) {
302 return false;
Paul Lawrencedfe84342017-02-16 09:24:39 -0800303 }
304
Paul Lawrencedfe84342017-02-16 09:24:39 -0800305 ExamineSyscall(f);
306
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700307 for (size_t i = 0; i < s_size; ++i) {
308 f.push_back(s[i]);
Paul Lawrencedfe84342017-02-16 09:24:39 -0800309 }
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800310 Disallow(f);
311#endif
Paul Lawrencedfe84342017-02-16 09:24:39 -0800312
313 return install_filter(f);
314}
Paul Lawrence26f57b62017-03-27 15:38:37 -0700315
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800316bool set_app_seccomp_filter() {
317 return _set_seccomp_filter(FilterType::APP);
318}
319
Martijn Coenenc3752be2019-01-09 16:19:57 +0100320bool set_app_zygote_seccomp_filter() {
321 return _set_seccomp_filter(FilterType::APP_ZYGOTE);
322}
323
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800324bool set_system_seccomp_filter() {
325 return _set_seccomp_filter(FilterType::SYSTEM);
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700326}
327
328bool set_global_seccomp_filter() {
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800329 return _set_seccomp_filter(FilterType::GLOBAL);
Paul Lawrence26f57b62017-03-27 15:38:37 -0700330}
Martijn Coenend269d9b2018-11-08 16:41:42 +0100331
332bool install_setuidgid_seccomp_filter(uint32_t uid_gid_min, uint32_t uid_gid_max) {
333 return _install_setuidgid_filter(uid_gid_min, uid_gid_max);
334}