blob: 7f2cd91296f702a25513146e9fca6c6ebb98bdf5 [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
19#include <linux/audit.h>
20#include <linux/filter.h>
21#include <linux/seccomp.h>
22#include <sys/prctl.h>
23
24#include <vector>
25
26#include <android-base/logging.h>
27
28#if defined __arm__ || defined __aarch64__
29
30extern const struct sock_filter arm_filter[];
31extern const size_t arm_filter_size;
32extern const struct sock_filter arm64_filter[];
33extern const size_t arm64_filter_size;
34
35#define syscall_nr (offsetof(struct seccomp_data, nr))
36#define arch_nr (offsetof(struct seccomp_data, arch))
37
38typedef std::vector<sock_filter> filter;
39
40// We want to keep the below inline functions for debugging and future
41// development even though they are not all sed currently.
42#pragma clang diagnostic push
43#pragma clang diagnostic ignored "-Wunused-function"
44
45static inline void Kill(filter& f) {
46 f.push_back(BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_KILL));
47}
48
49static inline void Trap(filter& f) {
50 f.push_back(BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRAP));
51}
52
53static inline void Error(filter& f, __u16 retcode) {
54 f.push_back(BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ERRNO + retcode));
55}
56
57inline static void Trace(filter& f) {
58 f.push_back(BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRACE));
59}
60
61inline static void Allow(filter& f) {
62 f.push_back(BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_ALLOW));
63}
64
65#pragma clang diagnostic pop
66
67inline static void ExamineSyscall(filter& f) {
68 f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, syscall_nr));
69}
70
71inline static int SetValidateArchitectureJumpTarget(size_t offset, filter& f) {
72 size_t jump_length = f.size() - offset - 1;
73 auto u8_jump_length = (__u8) jump_length;
74 if (u8_jump_length != jump_length) {
75 LOG(FATAL)
76 << "Can't set jump greater than 255 - actual jump is " << jump_length;
77 return -1;
78 }
79 f[offset] = BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, AUDIT_ARCH_ARM, u8_jump_length, 0);
80 return 0;
81}
82
83inline static size_t ValidateArchitectureAndJumpIfNeeded(filter& f) {
84 f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, arch_nr));
85
86 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, AUDIT_ARCH_AARCH64, 2, 0));
87 f.push_back(BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, AUDIT_ARCH_ARM, 1, 0));
88 Trap(f);
89 return f.size() - 2;
90}
91
92static bool install_filter(filter const& f) {
93 struct sock_fprog prog = {
94 static_cast<unsigned short>(f.size()),
95 const_cast<struct sock_filter*>(&f[0]),
96 };
97
98 if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) < 0) {
99 PLOG(FATAL) << "Could not set seccomp filter of size " << f.size();
100 return false;
101 }
102
103 LOG(INFO) << "Global filter of size " << f.size() << " installed";
104 return true;
105}
106
107bool set_seccomp_filter() {
108 filter f;
109
110 // Note that for mixed 64/32 bit architectures, ValidateArchitecture inserts a
111 // jump that must be changed to point to the start of the 32-bit policy
112 // 32 bit syscalls will not hit the policy between here and the call to SetJump
113 auto offset_to_32bit_filter = ValidateArchitectureAndJumpIfNeeded(f);
114
115 // 64-bit filter
116 ExamineSyscall(f);
117
118 // arm64-only filter - autogenerated from bionic syscall usage
119 for (size_t i = 0; i < arm64_filter_size; ++i) {
120 f.push_back(arm64_filter[i]);
121 }
122 Trap(f);
123
124 if (SetValidateArchitectureJumpTarget(offset_to_32bit_filter, f) != 0) {
125 return -1;
126 }
127
128 // 32-bit filter
129 ExamineSyscall(f);
130
131 // arm32 filter - autogenerated from bionic syscall usage
132 for (size_t i = 0; i < arm_filter_size; ++i) {
133 f.push_back(arm_filter[i]);
134 }
135 Trap(f);
136
137 return install_filter(f);
138}
139
140#else // if defined __arm__ || defined __aarch64__
141
142bool set_seccomp_filter() {
143 LOG(INFO) << "Not setting seccomp filter - wrong architecture";
144 return true;
145}
146
147#endif