blob: bce7b14e86646c523c19f26ed9d780c1a41487f5 [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;
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;
Paul Lawrence89fa81f2017-02-17 10:22:03 -080055#elif defined __i386__ || defined __x86_64__
56
57#define DUAL_ARCH
58#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;
Paul Lawrence89fa81f2017-02-17 10:22:03 -080078#elif defined __mips__ || defined __mips64__
79
80#define DUAL_ARCH
Lazar Trsic22b43512017-05-05 14:29:34 +020081#define PRIMARY_ARCH AUDIT_ARCH_MIPSEL64
Victor Hsieh4f02dd52017-12-20 09:19:22 -080082static const struct sock_filter* primary_app_filter = mips64_app_filter;
83static const size_t primary_app_filter_size = mips64_app_filter_size;
Martijn Coenenc3752be2019-01-09 16:19:57 +010084static const struct sock_filter* primary_app_zygote_filter = mips64_app_zygote_filter;
85static const size_t primary_app_zygote_filter_size = mips64_app_zygote_filter_size;
Victor Hsieh4f02dd52017-12-20 09:19:22 -080086static const struct sock_filter* primary_system_filter = mips64_system_filter;
87static const size_t primary_system_filter_size = mips64_system_filter_size;
Martijn Coenend269d9b2018-11-08 16:41:42 +010088
89static const long primary_setresgid = __mips64_setresgid;
90static const long primary_setresuid = __mips64_setresuid;
Lazar Trsic22b43512017-05-05 14:29:34 +020091#define SECONDARY_ARCH AUDIT_ARCH_MIPSEL
Victor Hsieh4f02dd52017-12-20 09:19:22 -080092static const struct sock_filter* secondary_app_filter = mips_app_filter;
93static const size_t secondary_app_filter_size = mips_app_filter_size;
Martijn Coenenc3752be2019-01-09 16:19:57 +010094static const struct sock_filter* secondary_app_zygote_filter = mips_app_zygote_filter;
95static const size_t secondary_app_zygote_filter_size = mips_app_zygote_filter_size;
Victor Hsieh4f02dd52017-12-20 09:19:22 -080096static const struct sock_filter* secondary_system_filter = mips_system_filter;
97static const size_t secondary_system_filter_size = mips_system_filter_size;
Paul Lawrence89fa81f2017-02-17 10:22:03 -080098
Martijn Coenend269d9b2018-11-08 16:41:42 +010099static const long secondary_setresgid = __mips_setresgid;
100static const long secondary_setresuid = __mips_setresuid;
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800101#else
102#error No architecture was defined!
103#endif
104
Paul Lawrencedfe84342017-02-16 09:24:39 -0800105
106#define syscall_nr (offsetof(struct seccomp_data, nr))
Martijn Coenend269d9b2018-11-08 16:41:42 +0100107#define syscall_arg(_n) (offsetof(struct seccomp_data, args[_n]))
Paul Lawrencedfe84342017-02-16 09:24:39 -0800108#define arch_nr (offsetof(struct seccomp_data, arch))
109
110typedef std::vector<sock_filter> filter;
111
Martijn Coenend269d9b2018-11-08 16:41:42 +0100112inline void Allow(filter& f) {
113 f.push_back(BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW));
114}
115
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800116inline void Disallow(filter& f) {
Paul Lawrencedfe84342017-02-16 09:24:39 -0800117 f.push_back(BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRAP));
118}
119
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800120static void ExamineSyscall(filter& f) {
Paul Lawrencedfe84342017-02-16 09:24:39 -0800121 f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, syscall_nr));
122}
123
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800124#ifdef DUAL_ARCH
125static bool SetValidateArchitectureJumpTarget(size_t offset, filter& f) {
Paul Lawrencedfe84342017-02-16 09:24:39 -0800126 size_t jump_length = f.size() - offset - 1;
127 auto u8_jump_length = (__u8) jump_length;
128 if (u8_jump_length != jump_length) {
129 LOG(FATAL)
130 << "Can't set jump greater than 255 - actual jump is " << jump_length;
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800131 return false;
Paul Lawrencedfe84342017-02-16 09:24:39 -0800132 }
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800133 f[offset] = BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SECONDARY_ARCH, u8_jump_length, 0);
134 return true;
Paul Lawrencedfe84342017-02-16 09:24:39 -0800135}
136
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800137static size_t ValidateArchitectureAndJumpIfNeeded(filter& f) {
Paul Lawrencedfe84342017-02-16 09:24:39 -0800138 f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, arch_nr));
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800139 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, PRIMARY_ARCH, 2, 0));
140 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SECONDARY_ARCH, 1, 0));
141 Disallow(f);
Paul Lawrencedfe84342017-02-16 09:24:39 -0800142 return f.size() - 2;
143}
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800144#else
145static void ValidateArchitecture(filter& f) {
146 f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, arch_nr));
147 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, PRIMARY_ARCH, 1, 0));
148 Disallow(f);
149}
150#endif
Paul Lawrencedfe84342017-02-16 09:24:39 -0800151
Martijn Coenend269d9b2018-11-08 16:41:42 +0100152static void ValidateSyscallArgInRange(filter& f, __u32 arg_num, __u32 range_min, __u32 range_max) {
153 const __u32 syscall_arg = syscall_arg(arg_num);
154
155 if (range_max == UINT32_MAX) {
156 LOG(FATAL) << "range_max exceeds maximum argument range.";
157 return;
158 }
159
160 f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, syscall_arg));
161 f.push_back(BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, range_min, 0, 1));
162 f.push_back(BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, range_max + 1, 0, 1));
163 Disallow(f);
164}
165
166// This filter is meant to be installed in addition to a regular whitelist filter.
167// Therefore, it's default action has to be Allow, except when the evaluated
168// system call matches setresuid/setresgid and the arguments don't fall within the
169// passed in range.
170//
171// The regular whitelist only allows setresuid/setresgid for UID/GID changes, so
172// that's the only system call we need to check here. A CTS test ensures the other
173// calls will remain blocked.
174static void ValidateSetUidGid(filter& f, uint32_t uid_gid_min, uint32_t uid_gid_max, bool primary) {
175 // Check setresuid(ruid, euid, sguid) fall within range
176 ExamineSyscall(f);
177 __u32 setresuid_nr = primary ? primary_setresuid : secondary_setresuid;
178 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, setresuid_nr, 0, 12));
179 for (int arg = 0; arg < 3; arg++) {
180 ValidateSyscallArgInRange(f, arg, uid_gid_min, uid_gid_max);
181 }
182
183 // Check setresgid(rgid, egid, sgid) fall within range
184 ExamineSyscall(f);
185 __u32 setresgid_nr = primary ? primary_setresgid : secondary_setresgid;
186 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, setresgid_nr, 0, 12));
187 for (int arg = 0; arg < 3; arg++) {
188 ValidateSyscallArgInRange(f, arg, uid_gid_min, uid_gid_max);
189 }
190
191 // Default is to allow; other filters may still reject this call.
192 Allow(f);
193}
194
Paul Lawrencedfe84342017-02-16 09:24:39 -0800195static bool install_filter(filter const& f) {
196 struct sock_fprog prog = {
197 static_cast<unsigned short>(f.size()),
198 const_cast<struct sock_filter*>(&f[0]),
199 };
Victor Hsiehdab45ad2018-01-15 11:04:26 -0800200 // 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 -0800201 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) < 0) {
202 PLOG(FATAL) << "Could not set seccomp filter of size " << f.size();
203 return false;
204 }
Paul Lawrencedfe84342017-02-16 09:24:39 -0800205 return true;
206}
207
Martijn Coenend269d9b2018-11-08 16:41:42 +0100208bool _install_setuidgid_filter(uint32_t uid_gid_min, uint32_t uid_gid_max) {
209 filter f;
210#ifdef DUAL_ARCH
211 // Note that for mixed 64/32 bit architectures, ValidateArchitecture inserts a
212 // jump that must be changed to point to the start of the 32-bit policy
213 // 32 bit syscalls will not hit the policy between here and the call to SetJump
214 auto offset_to_secondary_filter = ValidateArchitectureAndJumpIfNeeded(f);
215#else
216 ValidateArchitecture(f);
217#endif
218
219 ValidateSetUidGid(f, uid_gid_min, uid_gid_max, true /* primary */);
220
221#ifdef DUAL_ARCH
222 if (!SetValidateArchitectureJumpTarget(offset_to_secondary_filter, f)) {
223 return false;
224 }
225
226 ValidateSetUidGid(f, uid_gid_min, uid_gid_max, false /* primary */);
227#endif
228
229 return install_filter(f);
230}
231
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800232enum FilterType {
233 APP,
Martijn Coenenc3752be2019-01-09 16:19:57 +0100234 APP_ZYGOTE,
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800235 SYSTEM,
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800236};
237
238bool _set_seccomp_filter(FilterType type) {
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700239 const sock_filter *p, *s;
240 size_t p_size, s_size;
Paul Lawrencedfe84342017-02-16 09:24:39 -0800241 filter f;
242
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800243 switch (type) {
244 case APP:
245 p = primary_app_filter;
246 p_size = primary_app_filter_size;
247 s = secondary_app_filter;
248 s_size = secondary_app_filter_size;
249 break;
Martijn Coenenc3752be2019-01-09 16:19:57 +0100250 case APP_ZYGOTE:
251 p = primary_app_zygote_filter;
252 p_size = primary_app_zygote_filter_size;
253 s = secondary_app_zygote_filter;
254 s_size = secondary_app_zygote_filter_size;
255 break;
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800256 case SYSTEM:
257 p = primary_system_filter;
258 p_size = primary_system_filter_size;
259 s = secondary_system_filter;
260 s_size = secondary_system_filter_size;
261 break;
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700262 }
263
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800264#ifdef DUAL_ARCH
Paul Lawrencedfe84342017-02-16 09:24:39 -0800265 // Note that for mixed 64/32 bit architectures, ValidateArchitecture inserts a
266 // jump that must be changed to point to the start of the 32-bit policy
267 // 32 bit syscalls will not hit the policy between here and the call to SetJump
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800268 auto offset_to_secondary_filter = ValidateArchitectureAndJumpIfNeeded(f);
269#else
270 ValidateArchitecture(f);
271#endif
Paul Lawrencedfe84342017-02-16 09:24:39 -0800272
Paul Lawrencedfe84342017-02-16 09:24:39 -0800273 ExamineSyscall(f);
274
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700275 for (size_t i = 0; i < p_size; ++i) {
276 f.push_back(p[i]);
Paul Lawrencedfe84342017-02-16 09:24:39 -0800277 }
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800278 Disallow(f);
Paul Lawrencedfe84342017-02-16 09:24:39 -0800279
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800280#ifdef DUAL_ARCH
281 if (!SetValidateArchitectureJumpTarget(offset_to_secondary_filter, f)) {
282 return false;
Paul Lawrencedfe84342017-02-16 09:24:39 -0800283 }
284
Paul Lawrencedfe84342017-02-16 09:24:39 -0800285 ExamineSyscall(f);
286
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700287 for (size_t i = 0; i < s_size; ++i) {
288 f.push_back(s[i]);
Paul Lawrencedfe84342017-02-16 09:24:39 -0800289 }
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800290 Disallow(f);
291#endif
Paul Lawrencedfe84342017-02-16 09:24:39 -0800292
293 return install_filter(f);
294}
Paul Lawrence26f57b62017-03-27 15:38:37 -0700295
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800296bool set_app_seccomp_filter() {
297 return _set_seccomp_filter(FilterType::APP);
298}
299
Martijn Coenenc3752be2019-01-09 16:19:57 +0100300bool set_app_zygote_seccomp_filter() {
301 return _set_seccomp_filter(FilterType::APP_ZYGOTE);
302}
303
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800304bool set_system_seccomp_filter() {
305 return _set_seccomp_filter(FilterType::SYSTEM);
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700306}
307
Martijn Coenend269d9b2018-11-08 16:41:42 +0100308bool install_setuidgid_seccomp_filter(uint32_t uid_gid_min, uint32_t uid_gid_max) {
309 return _install_setuidgid_filter(uid_gid_min, uid_gid_max);
310}