blob: a42816eb695532317f9f99072cf36df3f25d62dd [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#else
79#error No architecture was defined!
80#endif
81
Paul Lawrencedfe84342017-02-16 09:24:39 -080082
83#define syscall_nr (offsetof(struct seccomp_data, nr))
Martijn Coenend269d9b2018-11-08 16:41:42 +010084#define syscall_arg(_n) (offsetof(struct seccomp_data, args[_n]))
Paul Lawrencedfe84342017-02-16 09:24:39 -080085#define arch_nr (offsetof(struct seccomp_data, arch))
86
87typedef std::vector<sock_filter> filter;
88
Martijn Coenend269d9b2018-11-08 16:41:42 +010089inline void Allow(filter& f) {
90 f.push_back(BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW));
91}
92
Paul Lawrence89fa81f2017-02-17 10:22:03 -080093inline void Disallow(filter& f) {
Paul Lawrencedfe84342017-02-16 09:24:39 -080094 f.push_back(BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRAP));
95}
96
Paul Lawrence89fa81f2017-02-17 10:22:03 -080097static void ExamineSyscall(filter& f) {
Paul Lawrencedfe84342017-02-16 09:24:39 -080098 f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, syscall_nr));
99}
100
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800101#ifdef DUAL_ARCH
102static bool SetValidateArchitectureJumpTarget(size_t offset, filter& f) {
Paul Lawrencedfe84342017-02-16 09:24:39 -0800103 size_t jump_length = f.size() - offset - 1;
104 auto u8_jump_length = (__u8) jump_length;
105 if (u8_jump_length != jump_length) {
106 LOG(FATAL)
107 << "Can't set jump greater than 255 - actual jump is " << jump_length;
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800108 return false;
Paul Lawrencedfe84342017-02-16 09:24:39 -0800109 }
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800110 f[offset] = BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SECONDARY_ARCH, u8_jump_length, 0);
111 return true;
Paul Lawrencedfe84342017-02-16 09:24:39 -0800112}
113
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800114static size_t ValidateArchitectureAndJumpIfNeeded(filter& f) {
Paul Lawrencedfe84342017-02-16 09:24:39 -0800115 f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, arch_nr));
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800116 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, PRIMARY_ARCH, 2, 0));
117 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SECONDARY_ARCH, 1, 0));
118 Disallow(f);
Paul Lawrencedfe84342017-02-16 09:24:39 -0800119 return f.size() - 2;
120}
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800121#else
122static void ValidateArchitecture(filter& f) {
123 f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, arch_nr));
124 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, PRIMARY_ARCH, 1, 0));
125 Disallow(f);
126}
127#endif
Paul Lawrencedfe84342017-02-16 09:24:39 -0800128
Martijn Coenend269d9b2018-11-08 16:41:42 +0100129static void ValidateSyscallArgInRange(filter& f, __u32 arg_num, __u32 range_min, __u32 range_max) {
130 const __u32 syscall_arg = syscall_arg(arg_num);
131
132 if (range_max == UINT32_MAX) {
133 LOG(FATAL) << "range_max exceeds maximum argument range.";
134 return;
135 }
136
137 f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, syscall_arg));
138 f.push_back(BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, range_min, 0, 1));
139 f.push_back(BPF_JUMP(BPF_JMP|BPF_JGE|BPF_K, range_max + 1, 0, 1));
140 Disallow(f);
141}
142
Victor Hsiehdbb86702020-06-15 09:29:07 -0700143// This filter is meant to be installed in addition to a regular allowlist filter.
Martijn Coenend269d9b2018-11-08 16:41:42 +0100144// Therefore, it's default action has to be Allow, except when the evaluated
145// system call matches setresuid/setresgid and the arguments don't fall within the
146// passed in range.
147//
Victor Hsiehdbb86702020-06-15 09:29:07 -0700148// The regular allowlist only allows setresuid/setresgid for UID/GID changes, so
Martijn Coenend269d9b2018-11-08 16:41:42 +0100149// that's the only system call we need to check here. A CTS test ensures the other
150// calls will remain blocked.
151static void ValidateSetUidGid(filter& f, uint32_t uid_gid_min, uint32_t uid_gid_max, bool primary) {
152 // Check setresuid(ruid, euid, sguid) fall within range
153 ExamineSyscall(f);
154 __u32 setresuid_nr = primary ? primary_setresuid : secondary_setresuid;
155 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, setresuid_nr, 0, 12));
156 for (int arg = 0; arg < 3; arg++) {
157 ValidateSyscallArgInRange(f, arg, uid_gid_min, uid_gid_max);
158 }
159
160 // Check setresgid(rgid, egid, sgid) fall within range
161 ExamineSyscall(f);
162 __u32 setresgid_nr = primary ? primary_setresgid : secondary_setresgid;
163 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, setresgid_nr, 0, 12));
164 for (int arg = 0; arg < 3; arg++) {
165 ValidateSyscallArgInRange(f, arg, uid_gid_min, uid_gid_max);
166 }
167
168 // Default is to allow; other filters may still reject this call.
169 Allow(f);
170}
171
Paul Lawrencedfe84342017-02-16 09:24:39 -0800172static bool install_filter(filter const& f) {
173 struct sock_fprog prog = {
174 static_cast<unsigned short>(f.size()),
175 const_cast<struct sock_filter*>(&f[0]),
176 };
Victor Hsiehdab45ad2018-01-15 11:04:26 -0800177 // 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 -0800178 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) < 0) {
179 PLOG(FATAL) << "Could not set seccomp filter of size " << f.size();
180 return false;
181 }
Paul Lawrencedfe84342017-02-16 09:24:39 -0800182 return true;
183}
184
Martijn Coenend269d9b2018-11-08 16:41:42 +0100185bool _install_setuidgid_filter(uint32_t uid_gid_min, uint32_t uid_gid_max) {
186 filter f;
187#ifdef DUAL_ARCH
188 // Note that for mixed 64/32 bit architectures, ValidateArchitecture inserts a
189 // jump that must be changed to point to the start of the 32-bit policy
190 // 32 bit syscalls will not hit the policy between here and the call to SetJump
191 auto offset_to_secondary_filter = ValidateArchitectureAndJumpIfNeeded(f);
192#else
193 ValidateArchitecture(f);
194#endif
195
196 ValidateSetUidGid(f, uid_gid_min, uid_gid_max, true /* primary */);
197
198#ifdef DUAL_ARCH
199 if (!SetValidateArchitectureJumpTarget(offset_to_secondary_filter, f)) {
200 return false;
201 }
202
203 ValidateSetUidGid(f, uid_gid_min, uid_gid_max, false /* primary */);
204#endif
205
206 return install_filter(f);
207}
208
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800209enum FilterType {
210 APP,
Martijn Coenenc3752be2019-01-09 16:19:57 +0100211 APP_ZYGOTE,
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800212 SYSTEM,
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800213};
214
215bool _set_seccomp_filter(FilterType type) {
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700216 const sock_filter *p, *s;
217 size_t p_size, s_size;
Paul Lawrencedfe84342017-02-16 09:24:39 -0800218 filter f;
219
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800220 switch (type) {
221 case APP:
222 p = primary_app_filter;
223 p_size = primary_app_filter_size;
224 s = secondary_app_filter;
225 s_size = secondary_app_filter_size;
226 break;
Martijn Coenenc3752be2019-01-09 16:19:57 +0100227 case APP_ZYGOTE:
228 p = primary_app_zygote_filter;
229 p_size = primary_app_zygote_filter_size;
230 s = secondary_app_zygote_filter;
231 s_size = secondary_app_zygote_filter_size;
232 break;
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800233 case SYSTEM:
234 p = primary_system_filter;
235 p_size = primary_system_filter_size;
236 s = secondary_system_filter;
237 s_size = secondary_system_filter_size;
238 break;
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700239 }
240
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800241#ifdef DUAL_ARCH
Paul Lawrencedfe84342017-02-16 09:24:39 -0800242 // Note that for mixed 64/32 bit architectures, ValidateArchitecture inserts a
243 // jump that must be changed to point to the start of the 32-bit policy
244 // 32 bit syscalls will not hit the policy between here and the call to SetJump
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800245 auto offset_to_secondary_filter = ValidateArchitectureAndJumpIfNeeded(f);
246#else
247 ValidateArchitecture(f);
248#endif
Paul Lawrencedfe84342017-02-16 09:24:39 -0800249
Paul Lawrencedfe84342017-02-16 09:24:39 -0800250 ExamineSyscall(f);
251
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700252 for (size_t i = 0; i < p_size; ++i) {
253 f.push_back(p[i]);
Paul Lawrencedfe84342017-02-16 09:24:39 -0800254 }
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800255 Disallow(f);
Paul Lawrencedfe84342017-02-16 09:24:39 -0800256
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800257#ifdef DUAL_ARCH
258 if (!SetValidateArchitectureJumpTarget(offset_to_secondary_filter, f)) {
259 return false;
Paul Lawrencedfe84342017-02-16 09:24:39 -0800260 }
261
Paul Lawrencedfe84342017-02-16 09:24:39 -0800262 ExamineSyscall(f);
263
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700264 for (size_t i = 0; i < s_size; ++i) {
265 f.push_back(s[i]);
Paul Lawrencedfe84342017-02-16 09:24:39 -0800266 }
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800267 Disallow(f);
268#endif
Paul Lawrencedfe84342017-02-16 09:24:39 -0800269
270 return install_filter(f);
271}
Paul Lawrence26f57b62017-03-27 15:38:37 -0700272
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800273bool set_app_seccomp_filter() {
274 return _set_seccomp_filter(FilterType::APP);
275}
276
Martijn Coenenc3752be2019-01-09 16:19:57 +0100277bool set_app_zygote_seccomp_filter() {
278 return _set_seccomp_filter(FilterType::APP_ZYGOTE);
279}
280
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800281bool set_system_seccomp_filter() {
282 return _set_seccomp_filter(FilterType::SYSTEM);
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700283}
284
Martijn Coenend269d9b2018-11-08 16:41:42 +0100285bool install_setuidgid_seccomp_filter(uint32_t uid_gid_min, uint32_t uid_gid_max) {
286 return _install_setuidgid_filter(uid_gid_min, uid_gid_max);
287}