blob: 75871c03bf4e8f5cacf5267a107666ea0331e731 [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>
23
24#include <vector>
25
26#include <android-base/logging.h>
27
Paul Lawrence89fa81f2017-02-17 10:22:03 -080028#include "seccomp_bpfs.h"
29
Paul Lawrencedfe84342017-02-16 09:24:39 -080030#if defined __arm__ || defined __aarch64__
31
Paul Lawrence89fa81f2017-02-17 10:22:03 -080032#define DUAL_ARCH
33#define PRIMARY_ARCH AUDIT_ARCH_AARCH64
Victor Hsieh4f02dd52017-12-20 09:19:22 -080034static const struct sock_filter* primary_app_filter = arm64_app_filter;
35static const size_t primary_app_filter_size = arm64_app_filter_size;
36static const struct sock_filter* primary_system_filter = arm64_system_filter;
37static const size_t primary_system_filter_size = arm64_system_filter_size;
Steve Muckleaa3f96c2017-07-20 13:11:54 -070038static const struct sock_filter* primary_global_filter = arm64_global_filter;
39static const size_t primary_global_filter_size = arm64_global_filter_size;
Paul Lawrence89fa81f2017-02-17 10:22:03 -080040#define SECONDARY_ARCH AUDIT_ARCH_ARM
Victor Hsieh4f02dd52017-12-20 09:19:22 -080041static const struct sock_filter* secondary_app_filter = arm_app_filter;
42static const size_t secondary_app_filter_size = arm_app_filter_size;
43static const struct sock_filter* secondary_system_filter = arm_system_filter;
44static const size_t secondary_system_filter_size = arm_system_filter_size;
Steve Muckleaa3f96c2017-07-20 13:11:54 -070045static const struct sock_filter* secondary_global_filter = arm_global_filter;
46static const size_t secondary_global_filter_size = arm_global_filter_size;
Paul Lawrence89fa81f2017-02-17 10:22:03 -080047
48#elif defined __i386__ || defined __x86_64__
49
50#define DUAL_ARCH
51#define PRIMARY_ARCH AUDIT_ARCH_X86_64
Victor Hsieh4f02dd52017-12-20 09:19:22 -080052static const struct sock_filter* primary_app_filter = x86_64_app_filter;
53static const size_t primary_app_filter_size = x86_64_app_filter_size;
54static const struct sock_filter* primary_system_filter = x86_64_system_filter;
55static const size_t primary_system_filter_size = x86_64_system_filter_size;
Steve Muckleaa3f96c2017-07-20 13:11:54 -070056static const struct sock_filter* primary_global_filter = x86_64_global_filter;
57static const size_t primary_global_filter_size = x86_64_global_filter_size;
Paul Lawrence89fa81f2017-02-17 10:22:03 -080058#define SECONDARY_ARCH AUDIT_ARCH_I386
Victor Hsieh4f02dd52017-12-20 09:19:22 -080059static const struct sock_filter* secondary_app_filter = x86_app_filter;
60static const size_t secondary_app_filter_size = x86_app_filter_size;
61static const struct sock_filter* secondary_system_filter = x86_system_filter;
62static const size_t secondary_system_filter_size = x86_system_filter_size;
Steve Muckleaa3f96c2017-07-20 13:11:54 -070063static const struct sock_filter* secondary_global_filter = x86_global_filter;
64static const size_t secondary_global_filter_size = x86_global_filter_size;
Paul Lawrence89fa81f2017-02-17 10:22:03 -080065
66#elif defined __mips__ || defined __mips64__
67
68#define DUAL_ARCH
Lazar Trsic22b43512017-05-05 14:29:34 +020069#define PRIMARY_ARCH AUDIT_ARCH_MIPSEL64
Victor Hsieh4f02dd52017-12-20 09:19:22 -080070static const struct sock_filter* primary_app_filter = mips64_app_filter;
71static const size_t primary_app_filter_size = mips64_app_filter_size;
72static const struct sock_filter* primary_system_filter = mips64_system_filter;
73static const size_t primary_system_filter_size = mips64_system_filter_size;
Steve Muckleaa3f96c2017-07-20 13:11:54 -070074static const struct sock_filter* primary_global_filter = mips64_global_filter;
75static const size_t primary_global_filter_size = mips64_global_filter_size;
Lazar Trsic22b43512017-05-05 14:29:34 +020076#define SECONDARY_ARCH AUDIT_ARCH_MIPSEL
Victor Hsieh4f02dd52017-12-20 09:19:22 -080077static const struct sock_filter* secondary_app_filter = mips_app_filter;
78static const size_t secondary_app_filter_size = mips_app_filter_size;
79static const struct sock_filter* secondary_system_filter = mips_system_filter;
80static const size_t secondary_system_filter_size = mips_system_filter_size;
Steve Muckleaa3f96c2017-07-20 13:11:54 -070081static const struct sock_filter* secondary_global_filter = mips_global_filter;
82static const size_t secondary_global_filter_size = mips_global_filter_size;
Paul Lawrence89fa81f2017-02-17 10:22:03 -080083
84#else
85#error No architecture was defined!
86#endif
87
Paul Lawrencedfe84342017-02-16 09:24:39 -080088
89#define syscall_nr (offsetof(struct seccomp_data, nr))
90#define arch_nr (offsetof(struct seccomp_data, arch))
91
92typedef std::vector<sock_filter> filter;
93
Paul Lawrence89fa81f2017-02-17 10:22:03 -080094inline void Disallow(filter& f) {
Paul Lawrencedfe84342017-02-16 09:24:39 -080095 f.push_back(BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRAP));
96}
97
Paul Lawrence89fa81f2017-02-17 10:22:03 -080098static void ExamineSyscall(filter& f) {
Paul Lawrencedfe84342017-02-16 09:24:39 -080099 f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, syscall_nr));
100}
101
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800102#ifdef DUAL_ARCH
103static bool SetValidateArchitectureJumpTarget(size_t offset, filter& f) {
Paul Lawrencedfe84342017-02-16 09:24:39 -0800104 size_t jump_length = f.size() - offset - 1;
105 auto u8_jump_length = (__u8) jump_length;
106 if (u8_jump_length != jump_length) {
107 LOG(FATAL)
108 << "Can't set jump greater than 255 - actual jump is " << jump_length;
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800109 return false;
Paul Lawrencedfe84342017-02-16 09:24:39 -0800110 }
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800111 f[offset] = BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SECONDARY_ARCH, u8_jump_length, 0);
112 return true;
Paul Lawrencedfe84342017-02-16 09:24:39 -0800113}
114
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800115static size_t ValidateArchitectureAndJumpIfNeeded(filter& f) {
Paul Lawrencedfe84342017-02-16 09:24:39 -0800116 f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, arch_nr));
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800117 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, PRIMARY_ARCH, 2, 0));
118 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SECONDARY_ARCH, 1, 0));
119 Disallow(f);
Paul Lawrencedfe84342017-02-16 09:24:39 -0800120 return f.size() - 2;
121}
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800122#else
123static void ValidateArchitecture(filter& f) {
124 f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, arch_nr));
125 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, PRIMARY_ARCH, 1, 0));
126 Disallow(f);
127}
128#endif
Paul Lawrencedfe84342017-02-16 09:24:39 -0800129
130static bool install_filter(filter const& f) {
131 struct sock_fprog prog = {
132 static_cast<unsigned short>(f.size()),
133 const_cast<struct sock_filter*>(&f[0]),
134 };
Victor Hsiehdab45ad2018-01-15 11:04:26 -0800135 // 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 -0800136 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) < 0) {
137 PLOG(FATAL) << "Could not set seccomp filter of size " << f.size();
138 return false;
139 }
Paul Lawrencedfe84342017-02-16 09:24:39 -0800140 return true;
141}
142
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800143enum FilterType {
144 APP,
145 SYSTEM,
146 GLOBAL
147};
148
149bool _set_seccomp_filter(FilterType type) {
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700150 const sock_filter *p, *s;
151 size_t p_size, s_size;
Paul Lawrencedfe84342017-02-16 09:24:39 -0800152 filter f;
153
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800154 switch (type) {
155 case APP:
156 p = primary_app_filter;
157 p_size = primary_app_filter_size;
158 s = secondary_app_filter;
159 s_size = secondary_app_filter_size;
160 break;
161 case SYSTEM:
162 p = primary_system_filter;
163 p_size = primary_system_filter_size;
164 s = secondary_system_filter;
165 s_size = secondary_system_filter_size;
166 break;
167 case GLOBAL:
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700168 p = primary_global_filter;
169 p_size = primary_global_filter_size;
170 s = secondary_global_filter;
171 s_size = secondary_global_filter_size;
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800172 break;
173
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700174 }
175
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800176#ifdef DUAL_ARCH
Paul Lawrencedfe84342017-02-16 09:24:39 -0800177 // Note that for mixed 64/32 bit architectures, ValidateArchitecture inserts a
178 // jump that must be changed to point to the start of the 32-bit policy
179 // 32 bit syscalls will not hit the policy between here and the call to SetJump
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800180 auto offset_to_secondary_filter = ValidateArchitectureAndJumpIfNeeded(f);
181#else
182 ValidateArchitecture(f);
183#endif
Paul Lawrencedfe84342017-02-16 09:24:39 -0800184
Paul Lawrencedfe84342017-02-16 09:24:39 -0800185 ExamineSyscall(f);
186
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700187 for (size_t i = 0; i < p_size; ++i) {
188 f.push_back(p[i]);
Paul Lawrencedfe84342017-02-16 09:24:39 -0800189 }
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800190 Disallow(f);
Paul Lawrencedfe84342017-02-16 09:24:39 -0800191
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800192#ifdef DUAL_ARCH
193 if (!SetValidateArchitectureJumpTarget(offset_to_secondary_filter, f)) {
194 return false;
Paul Lawrencedfe84342017-02-16 09:24:39 -0800195 }
196
Paul Lawrencedfe84342017-02-16 09:24:39 -0800197 ExamineSyscall(f);
198
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700199 for (size_t i = 0; i < s_size; ++i) {
200 f.push_back(s[i]);
Paul Lawrencedfe84342017-02-16 09:24:39 -0800201 }
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800202 Disallow(f);
203#endif
Paul Lawrencedfe84342017-02-16 09:24:39 -0800204
205 return install_filter(f);
206}
Paul Lawrence26f57b62017-03-27 15:38:37 -0700207
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800208bool set_app_seccomp_filter() {
209 return _set_seccomp_filter(FilterType::APP);
210}
211
212bool set_system_seccomp_filter() {
213 return _set_seccomp_filter(FilterType::SYSTEM);
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700214}
215
216bool set_global_seccomp_filter() {
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800217 return _set_seccomp_filter(FilterType::GLOBAL);
Paul Lawrence26f57b62017-03-27 15:38:37 -0700218}