blob: fde1a9f82bc076bac00cb61c6a104a5c97edea58 [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
30
Paul Lawrencedfe84342017-02-16 09:24:39 -080031#if defined __arm__ || defined __aarch64__
32
Paul Lawrence89fa81f2017-02-17 10:22:03 -080033#define DUAL_ARCH
34#define PRIMARY_ARCH AUDIT_ARCH_AARCH64
Victor Hsieh4f02dd52017-12-20 09:19:22 -080035static const struct sock_filter* primary_app_filter = arm64_app_filter;
36static const size_t primary_app_filter_size = arm64_app_filter_size;
37static const struct sock_filter* primary_system_filter = arm64_system_filter;
38static const size_t primary_system_filter_size = arm64_system_filter_size;
Steve Muckleaa3f96c2017-07-20 13:11:54 -070039static const struct sock_filter* primary_global_filter = arm64_global_filter;
40static const size_t primary_global_filter_size = arm64_global_filter_size;
Paul Lawrence89fa81f2017-02-17 10:22:03 -080041#define SECONDARY_ARCH AUDIT_ARCH_ARM
Victor Hsieh4f02dd52017-12-20 09:19:22 -080042static const struct sock_filter* secondary_app_filter = arm_app_filter;
43static const size_t secondary_app_filter_size = arm_app_filter_size;
44static const struct sock_filter* secondary_system_filter = arm_system_filter;
45static const size_t secondary_system_filter_size = arm_system_filter_size;
Steve Muckleaa3f96c2017-07-20 13:11:54 -070046static const struct sock_filter* secondary_global_filter = arm_global_filter;
47static const size_t secondary_global_filter_size = arm_global_filter_size;
Paul Lawrence89fa81f2017-02-17 10:22:03 -080048
49#elif defined __i386__ || defined __x86_64__
50
51#define DUAL_ARCH
52#define PRIMARY_ARCH AUDIT_ARCH_X86_64
Victor Hsieh4f02dd52017-12-20 09:19:22 -080053static const struct sock_filter* primary_app_filter = x86_64_app_filter;
54static const size_t primary_app_filter_size = x86_64_app_filter_size;
55static const struct sock_filter* primary_system_filter = x86_64_system_filter;
56static const size_t primary_system_filter_size = x86_64_system_filter_size;
Steve Muckleaa3f96c2017-07-20 13:11:54 -070057static const struct sock_filter* primary_global_filter = x86_64_global_filter;
58static const size_t primary_global_filter_size = x86_64_global_filter_size;
Paul Lawrence89fa81f2017-02-17 10:22:03 -080059#define SECONDARY_ARCH AUDIT_ARCH_I386
Victor Hsieh4f02dd52017-12-20 09:19:22 -080060static const struct sock_filter* secondary_app_filter = x86_app_filter;
61static const size_t secondary_app_filter_size = x86_app_filter_size;
62static const struct sock_filter* secondary_system_filter = x86_system_filter;
63static const size_t secondary_system_filter_size = x86_system_filter_size;
Steve Muckleaa3f96c2017-07-20 13:11:54 -070064static const struct sock_filter* secondary_global_filter = x86_global_filter;
65static const size_t secondary_global_filter_size = x86_global_filter_size;
Paul Lawrence89fa81f2017-02-17 10:22:03 -080066
67#elif defined __mips__ || defined __mips64__
68
69#define DUAL_ARCH
Lazar Trsic22b43512017-05-05 14:29:34 +020070#define PRIMARY_ARCH AUDIT_ARCH_MIPSEL64
Victor Hsieh4f02dd52017-12-20 09:19:22 -080071static const struct sock_filter* primary_app_filter = mips64_app_filter;
72static const size_t primary_app_filter_size = mips64_app_filter_size;
73static const struct sock_filter* primary_system_filter = mips64_system_filter;
74static const size_t primary_system_filter_size = mips64_system_filter_size;
Steve Muckleaa3f96c2017-07-20 13:11:54 -070075static const struct sock_filter* primary_global_filter = mips64_global_filter;
76static const size_t primary_global_filter_size = mips64_global_filter_size;
Lazar Trsic22b43512017-05-05 14:29:34 +020077#define SECONDARY_ARCH AUDIT_ARCH_MIPSEL
Victor Hsieh4f02dd52017-12-20 09:19:22 -080078static const struct sock_filter* secondary_app_filter = mips_app_filter;
79static const size_t secondary_app_filter_size = mips_app_filter_size;
80static const struct sock_filter* secondary_system_filter = mips_system_filter;
81static const size_t secondary_system_filter_size = mips_system_filter_size;
Steve Muckleaa3f96c2017-07-20 13:11:54 -070082static const struct sock_filter* secondary_global_filter = mips_global_filter;
83static const size_t secondary_global_filter_size = mips_global_filter_size;
Paul Lawrence89fa81f2017-02-17 10:22:03 -080084
85#else
86#error No architecture was defined!
87#endif
88
Paul Lawrencedfe84342017-02-16 09:24:39 -080089
90#define syscall_nr (offsetof(struct seccomp_data, nr))
91#define arch_nr (offsetof(struct seccomp_data, arch))
92
93typedef std::vector<sock_filter> filter;
94
Paul Lawrence89fa81f2017-02-17 10:22:03 -080095inline void Disallow(filter& f) {
Paul Lawrencedfe84342017-02-16 09:24:39 -080096 f.push_back(BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRAP));
97}
98
Paul Lawrence89fa81f2017-02-17 10:22:03 -080099static void ExamineSyscall(filter& f) {
Paul Lawrencedfe84342017-02-16 09:24:39 -0800100 f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, syscall_nr));
101}
102
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800103#ifdef DUAL_ARCH
104static bool SetValidateArchitectureJumpTarget(size_t offset, filter& f) {
Paul Lawrencedfe84342017-02-16 09:24:39 -0800105 size_t jump_length = f.size() - offset - 1;
106 auto u8_jump_length = (__u8) jump_length;
107 if (u8_jump_length != jump_length) {
108 LOG(FATAL)
109 << "Can't set jump greater than 255 - actual jump is " << jump_length;
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800110 return false;
Paul Lawrencedfe84342017-02-16 09:24:39 -0800111 }
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800112 f[offset] = BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SECONDARY_ARCH, u8_jump_length, 0);
113 return true;
Paul Lawrencedfe84342017-02-16 09:24:39 -0800114}
115
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800116static size_t ValidateArchitectureAndJumpIfNeeded(filter& f) {
Paul Lawrencedfe84342017-02-16 09:24:39 -0800117 f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, arch_nr));
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800118 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, PRIMARY_ARCH, 2, 0));
119 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SECONDARY_ARCH, 1, 0));
120 Disallow(f);
Paul Lawrencedfe84342017-02-16 09:24:39 -0800121 return f.size() - 2;
122}
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800123#else
124static void ValidateArchitecture(filter& f) {
125 f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, arch_nr));
126 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, PRIMARY_ARCH, 1, 0));
127 Disallow(f);
128}
129#endif
Paul Lawrencedfe84342017-02-16 09:24:39 -0800130
131static bool install_filter(filter const& f) {
132 struct sock_fprog prog = {
133 static_cast<unsigned short>(f.size()),
134 const_cast<struct sock_filter*>(&f[0]),
135 };
Victor Hsiehdab45ad2018-01-15 11:04:26 -0800136 // 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 -0800137 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) < 0) {
138 PLOG(FATAL) << "Could not set seccomp filter of size " << f.size();
139 return false;
140 }
Paul Lawrencedfe84342017-02-16 09:24:39 -0800141 return true;
142}
143
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800144enum FilterType {
145 APP,
146 SYSTEM,
147 GLOBAL
148};
149
150bool _set_seccomp_filter(FilterType type) {
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700151 const sock_filter *p, *s;
152 size_t p_size, s_size;
Paul Lawrencedfe84342017-02-16 09:24:39 -0800153 filter f;
154
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800155 switch (type) {
156 case APP:
157 p = primary_app_filter;
158 p_size = primary_app_filter_size;
159 s = secondary_app_filter;
160 s_size = secondary_app_filter_size;
161 break;
162 case SYSTEM:
163 p = primary_system_filter;
164 p_size = primary_system_filter_size;
165 s = secondary_system_filter;
166 s_size = secondary_system_filter_size;
167 break;
168 case GLOBAL:
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700169 p = primary_global_filter;
170 p_size = primary_global_filter_size;
171 s = secondary_global_filter;
172 s_size = secondary_global_filter_size;
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800173 break;
174
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700175 }
176
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800177#ifdef DUAL_ARCH
Paul Lawrencedfe84342017-02-16 09:24:39 -0800178 // Note that for mixed 64/32 bit architectures, ValidateArchitecture inserts a
179 // jump that must be changed to point to the start of the 32-bit policy
180 // 32 bit syscalls will not hit the policy between here and the call to SetJump
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800181 auto offset_to_secondary_filter = ValidateArchitectureAndJumpIfNeeded(f);
182#else
183 ValidateArchitecture(f);
184#endif
Paul Lawrencedfe84342017-02-16 09:24:39 -0800185
Paul Lawrencedfe84342017-02-16 09:24:39 -0800186 ExamineSyscall(f);
187
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700188 for (size_t i = 0; i < p_size; ++i) {
189 f.push_back(p[i]);
Paul Lawrencedfe84342017-02-16 09:24:39 -0800190 }
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800191 Disallow(f);
Paul Lawrencedfe84342017-02-16 09:24:39 -0800192
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800193#ifdef DUAL_ARCH
194 if (!SetValidateArchitectureJumpTarget(offset_to_secondary_filter, f)) {
195 return false;
Paul Lawrencedfe84342017-02-16 09:24:39 -0800196 }
197
Paul Lawrencedfe84342017-02-16 09:24:39 -0800198 ExamineSyscall(f);
199
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700200 for (size_t i = 0; i < s_size; ++i) {
201 f.push_back(s[i]);
Paul Lawrencedfe84342017-02-16 09:24:39 -0800202 }
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800203 Disallow(f);
204#endif
Paul Lawrencedfe84342017-02-16 09:24:39 -0800205
206 return install_filter(f);
207}
Paul Lawrence26f57b62017-03-27 15:38:37 -0700208
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700209bool set_seccomp_filter() {
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800210 return _set_seccomp_filter(FilterType::APP);
211}
212
213bool set_app_seccomp_filter() {
214 return _set_seccomp_filter(FilterType::APP);
215}
216
217bool set_system_seccomp_filter() {
218 return _set_seccomp_filter(FilterType::SYSTEM);
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700219}
220
221bool set_global_seccomp_filter() {
Victor Hsieh4f02dd52017-12-20 09:19:22 -0800222 return _set_seccomp_filter(FilterType::GLOBAL);
Paul Lawrence26f57b62017-03-27 15:38:37 -0700223}