blob: 19ef299dfa3997ee1084bf3298e7fbde25c521a9 [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
35static const struct sock_filter* primary_filter = arm64_filter;
36static const size_t primary_filter_size = arm64_filter_size;
Steve Muckleaa3f96c2017-07-20 13:11:54 -070037static const struct sock_filter* primary_global_filter = arm64_global_filter;
38static const size_t primary_global_filter_size = arm64_global_filter_size;
Paul Lawrence89fa81f2017-02-17 10:22:03 -080039#define SECONDARY_ARCH AUDIT_ARCH_ARM
40static const struct sock_filter* secondary_filter = arm_filter;
41static const size_t secondary_filter_size = arm_filter_size;
Steve Muckleaa3f96c2017-07-20 13:11:54 -070042static const struct sock_filter* secondary_global_filter = arm_global_filter;
43static const size_t secondary_global_filter_size = arm_global_filter_size;
Paul Lawrence89fa81f2017-02-17 10:22:03 -080044
45#elif defined __i386__ || defined __x86_64__
46
47#define DUAL_ARCH
48#define PRIMARY_ARCH AUDIT_ARCH_X86_64
49static const struct sock_filter* primary_filter = x86_64_filter;
50static const size_t primary_filter_size = x86_64_filter_size;
Steve Muckleaa3f96c2017-07-20 13:11:54 -070051static const struct sock_filter* primary_global_filter = x86_64_global_filter;
52static const size_t primary_global_filter_size = x86_64_global_filter_size;
Paul Lawrence89fa81f2017-02-17 10:22:03 -080053#define SECONDARY_ARCH AUDIT_ARCH_I386
54static const struct sock_filter* secondary_filter = x86_filter;
55static const size_t secondary_filter_size = x86_filter_size;
Steve Muckleaa3f96c2017-07-20 13:11:54 -070056static const struct sock_filter* secondary_global_filter = x86_global_filter;
57static const size_t secondary_global_filter_size = x86_global_filter_size;
Paul Lawrence89fa81f2017-02-17 10:22:03 -080058
59#elif defined __mips__ || defined __mips64__
60
61#define DUAL_ARCH
Lazar Trsic22b43512017-05-05 14:29:34 +020062#define PRIMARY_ARCH AUDIT_ARCH_MIPSEL64
Paul Lawrence89fa81f2017-02-17 10:22:03 -080063static const struct sock_filter* primary_filter = mips64_filter;
64static const size_t primary_filter_size = mips64_filter_size;
Steve Muckleaa3f96c2017-07-20 13:11:54 -070065static const struct sock_filter* primary_global_filter = mips64_global_filter;
66static const size_t primary_global_filter_size = mips64_global_filter_size;
Lazar Trsic22b43512017-05-05 14:29:34 +020067#define SECONDARY_ARCH AUDIT_ARCH_MIPSEL
Paul Lawrence89fa81f2017-02-17 10:22:03 -080068static const struct sock_filter* secondary_filter = mips_filter;
69static const size_t secondary_filter_size = mips_filter_size;
Steve Muckleaa3f96c2017-07-20 13:11:54 -070070static const struct sock_filter* secondary_global_filter = mips_global_filter;
71static const size_t secondary_global_filter_size = mips_global_filter_size;
Paul Lawrence89fa81f2017-02-17 10:22:03 -080072
73#else
74#error No architecture was defined!
75#endif
76
Paul Lawrencedfe84342017-02-16 09:24:39 -080077
78#define syscall_nr (offsetof(struct seccomp_data, nr))
79#define arch_nr (offsetof(struct seccomp_data, arch))
80
81typedef std::vector<sock_filter> filter;
82
Paul Lawrence89fa81f2017-02-17 10:22:03 -080083inline void Disallow(filter& f) {
Paul Lawrencedfe84342017-02-16 09:24:39 -080084 f.push_back(BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRAP));
85}
86
Paul Lawrence89fa81f2017-02-17 10:22:03 -080087static void ExamineSyscall(filter& f) {
Paul Lawrencedfe84342017-02-16 09:24:39 -080088 f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, syscall_nr));
89}
90
Paul Lawrence89fa81f2017-02-17 10:22:03 -080091#ifdef DUAL_ARCH
92static bool SetValidateArchitectureJumpTarget(size_t offset, filter& f) {
Paul Lawrencedfe84342017-02-16 09:24:39 -080093 size_t jump_length = f.size() - offset - 1;
94 auto u8_jump_length = (__u8) jump_length;
95 if (u8_jump_length != jump_length) {
96 LOG(FATAL)
97 << "Can't set jump greater than 255 - actual jump is " << jump_length;
Paul Lawrence89fa81f2017-02-17 10:22:03 -080098 return false;
Paul Lawrencedfe84342017-02-16 09:24:39 -080099 }
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800100 f[offset] = BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SECONDARY_ARCH, u8_jump_length, 0);
101 return true;
Paul Lawrencedfe84342017-02-16 09:24:39 -0800102}
103
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800104static size_t ValidateArchitectureAndJumpIfNeeded(filter& f) {
Paul Lawrencedfe84342017-02-16 09:24:39 -0800105 f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, arch_nr));
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800106 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, PRIMARY_ARCH, 2, 0));
107 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SECONDARY_ARCH, 1, 0));
108 Disallow(f);
Paul Lawrencedfe84342017-02-16 09:24:39 -0800109 return f.size() - 2;
110}
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800111#else
112static void ValidateArchitecture(filter& f) {
113 f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, arch_nr));
114 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, PRIMARY_ARCH, 1, 0));
115 Disallow(f);
116}
117#endif
Paul Lawrencedfe84342017-02-16 09:24:39 -0800118
119static bool install_filter(filter const& f) {
120 struct sock_fprog prog = {
121 static_cast<unsigned short>(f.size()),
122 const_cast<struct sock_filter*>(&f[0]),
123 };
124
125 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) < 0) {
126 PLOG(FATAL) << "Could not set seccomp filter of size " << f.size();
127 return false;
128 }
129
130 LOG(INFO) << "Global filter of size " << f.size() << " installed";
131 return true;
132}
133
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700134bool _set_seccomp_filter(bool global) {
135 const sock_filter *p, *s;
136 size_t p_size, s_size;
Paul Lawrencedfe84342017-02-16 09:24:39 -0800137 filter f;
138
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700139 if (global) {
140 p = primary_global_filter;
141 p_size = primary_global_filter_size;
142 s = secondary_global_filter;
143 s_size = secondary_global_filter_size;
144 } else {
145 p = primary_filter;
146 p_size = primary_filter_size;
147 s = secondary_filter;
148 s_size = secondary_filter_size;
149 }
150
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800151#ifdef DUAL_ARCH
Paul Lawrencedfe84342017-02-16 09:24:39 -0800152 // Note that for mixed 64/32 bit architectures, ValidateArchitecture inserts a
153 // jump that must be changed to point to the start of the 32-bit policy
154 // 32 bit syscalls will not hit the policy between here and the call to SetJump
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800155 auto offset_to_secondary_filter = ValidateArchitectureAndJumpIfNeeded(f);
156#else
157 ValidateArchitecture(f);
158#endif
Paul Lawrencedfe84342017-02-16 09:24:39 -0800159
Paul Lawrencedfe84342017-02-16 09:24:39 -0800160 ExamineSyscall(f);
161
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700162 for (size_t i = 0; i < p_size; ++i) {
163 f.push_back(p[i]);
Paul Lawrencedfe84342017-02-16 09:24:39 -0800164 }
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800165 Disallow(f);
Paul Lawrencedfe84342017-02-16 09:24:39 -0800166
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800167#ifdef DUAL_ARCH
168 if (!SetValidateArchitectureJumpTarget(offset_to_secondary_filter, f)) {
169 return false;
Paul Lawrencedfe84342017-02-16 09:24:39 -0800170 }
171
Paul Lawrencedfe84342017-02-16 09:24:39 -0800172 ExamineSyscall(f);
173
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700174 for (size_t i = 0; i < s_size; ++i) {
175 f.push_back(s[i]);
Paul Lawrencedfe84342017-02-16 09:24:39 -0800176 }
Paul Lawrence89fa81f2017-02-17 10:22:03 -0800177 Disallow(f);
178#endif
Paul Lawrencedfe84342017-02-16 09:24:39 -0800179
180 return install_filter(f);
181}
Paul Lawrence26f57b62017-03-27 15:38:37 -0700182
Steve Muckleaa3f96c2017-07-20 13:11:54 -0700183bool set_seccomp_filter() {
184 return _set_seccomp_filter(false);
185}
186
187bool set_global_seccomp_filter() {
188 return _set_seccomp_filter(true);
189}
190
Paul Lawrence26f57b62017-03-27 15:38:37 -0700191void get_seccomp_filter(const sock_filter*& filter, size_t& filter_size) {
192#if defined __aarch64__ || defined __x86_64__ || defined __mips64__
193 filter = primary_filter;
194 filter_size = primary_filter_size;
195#else
196 filter = secondary_filter;
197 filter_size = secondary_filter_size;
198#endif
199}