Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 1 | /* |
| 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 Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 19 | #include <assert.h> |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 20 | #include <linux/audit.h> |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 21 | #include <linux/seccomp.h> |
| 22 | #include <sys/prctl.h> |
| 23 | |
| 24 | #include <vector> |
| 25 | |
| 26 | #include <android-base/logging.h> |
| 27 | |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 28 | #include "seccomp_bpfs.h" |
| 29 | |
| 30 | |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 31 | #if defined __arm__ || defined __aarch64__ |
| 32 | |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 33 | #define DUAL_ARCH |
| 34 | #define PRIMARY_ARCH AUDIT_ARCH_AARCH64 |
| 35 | static const struct sock_filter* primary_filter = arm64_filter; |
| 36 | static const size_t primary_filter_size = arm64_filter_size; |
Steve Muckle | aa3f96c | 2017-07-20 13:11:54 -0700 | [diff] [blame^] | 37 | static const struct sock_filter* primary_global_filter = arm64_global_filter; |
| 38 | static const size_t primary_global_filter_size = arm64_global_filter_size; |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 39 | #define SECONDARY_ARCH AUDIT_ARCH_ARM |
| 40 | static const struct sock_filter* secondary_filter = arm_filter; |
| 41 | static const size_t secondary_filter_size = arm_filter_size; |
Steve Muckle | aa3f96c | 2017-07-20 13:11:54 -0700 | [diff] [blame^] | 42 | static const struct sock_filter* secondary_global_filter = arm_global_filter; |
| 43 | static const size_t secondary_global_filter_size = arm_global_filter_size; |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 44 | |
| 45 | #elif defined __i386__ || defined __x86_64__ |
| 46 | |
| 47 | #define DUAL_ARCH |
| 48 | #define PRIMARY_ARCH AUDIT_ARCH_X86_64 |
| 49 | static const struct sock_filter* primary_filter = x86_64_filter; |
| 50 | static const size_t primary_filter_size = x86_64_filter_size; |
Steve Muckle | aa3f96c | 2017-07-20 13:11:54 -0700 | [diff] [blame^] | 51 | static const struct sock_filter* primary_global_filter = x86_64_global_filter; |
| 52 | static const size_t primary_global_filter_size = x86_64_global_filter_size; |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 53 | #define SECONDARY_ARCH AUDIT_ARCH_I386 |
| 54 | static const struct sock_filter* secondary_filter = x86_filter; |
| 55 | static const size_t secondary_filter_size = x86_filter_size; |
Steve Muckle | aa3f96c | 2017-07-20 13:11:54 -0700 | [diff] [blame^] | 56 | static const struct sock_filter* secondary_global_filter = x86_global_filter; |
| 57 | static const size_t secondary_global_filter_size = x86_global_filter_size; |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 58 | |
| 59 | #elif defined __mips__ || defined __mips64__ |
| 60 | |
| 61 | #define DUAL_ARCH |
Lazar Trsic | 22b4351 | 2017-05-05 14:29:34 +0200 | [diff] [blame] | 62 | #define PRIMARY_ARCH AUDIT_ARCH_MIPSEL64 |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 63 | static const struct sock_filter* primary_filter = mips64_filter; |
| 64 | static const size_t primary_filter_size = mips64_filter_size; |
Steve Muckle | aa3f96c | 2017-07-20 13:11:54 -0700 | [diff] [blame^] | 65 | static const struct sock_filter* primary_global_filter = mips64_global_filter; |
| 66 | static const size_t primary_global_filter_size = mips64_global_filter_size; |
Lazar Trsic | 22b4351 | 2017-05-05 14:29:34 +0200 | [diff] [blame] | 67 | #define SECONDARY_ARCH AUDIT_ARCH_MIPSEL |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 68 | static const struct sock_filter* secondary_filter = mips_filter; |
| 69 | static const size_t secondary_filter_size = mips_filter_size; |
Steve Muckle | aa3f96c | 2017-07-20 13:11:54 -0700 | [diff] [blame^] | 70 | static const struct sock_filter* secondary_global_filter = mips_global_filter; |
| 71 | static const size_t secondary_global_filter_size = mips_global_filter_size; |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 72 | |
| 73 | #else |
| 74 | #error No architecture was defined! |
| 75 | #endif |
| 76 | |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 77 | |
| 78 | #define syscall_nr (offsetof(struct seccomp_data, nr)) |
| 79 | #define arch_nr (offsetof(struct seccomp_data, arch)) |
| 80 | |
| 81 | typedef std::vector<sock_filter> filter; |
| 82 | |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 83 | inline void Disallow(filter& f) { |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 84 | f.push_back(BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRAP)); |
| 85 | } |
| 86 | |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 87 | static void ExamineSyscall(filter& f) { |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 88 | f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, syscall_nr)); |
| 89 | } |
| 90 | |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 91 | #ifdef DUAL_ARCH |
| 92 | static bool SetValidateArchitectureJumpTarget(size_t offset, filter& f) { |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 93 | 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 Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 98 | return false; |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 99 | } |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 100 | f[offset] = BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SECONDARY_ARCH, u8_jump_length, 0); |
| 101 | return true; |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 102 | } |
| 103 | |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 104 | static size_t ValidateArchitectureAndJumpIfNeeded(filter& f) { |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 105 | f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, arch_nr)); |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 106 | 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 Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 109 | return f.size() - 2; |
| 110 | } |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 111 | #else |
| 112 | static 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 Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 118 | |
| 119 | static 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 Muckle | aa3f96c | 2017-07-20 13:11:54 -0700 | [diff] [blame^] | 134 | bool _set_seccomp_filter(bool global) { |
| 135 | const sock_filter *p, *s; |
| 136 | size_t p_size, s_size; |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 137 | filter f; |
| 138 | |
Steve Muckle | aa3f96c | 2017-07-20 13:11:54 -0700 | [diff] [blame^] | 139 | 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 Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 151 | #ifdef DUAL_ARCH |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 152 | // 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 Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 155 | auto offset_to_secondary_filter = ValidateArchitectureAndJumpIfNeeded(f); |
| 156 | #else |
| 157 | ValidateArchitecture(f); |
| 158 | #endif |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 159 | |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 160 | ExamineSyscall(f); |
| 161 | |
Steve Muckle | aa3f96c | 2017-07-20 13:11:54 -0700 | [diff] [blame^] | 162 | for (size_t i = 0; i < p_size; ++i) { |
| 163 | f.push_back(p[i]); |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 164 | } |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 165 | Disallow(f); |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 166 | |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 167 | #ifdef DUAL_ARCH |
| 168 | if (!SetValidateArchitectureJumpTarget(offset_to_secondary_filter, f)) { |
| 169 | return false; |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 170 | } |
| 171 | |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 172 | ExamineSyscall(f); |
| 173 | |
Steve Muckle | aa3f96c | 2017-07-20 13:11:54 -0700 | [diff] [blame^] | 174 | for (size_t i = 0; i < s_size; ++i) { |
| 175 | f.push_back(s[i]); |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 176 | } |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 177 | Disallow(f); |
| 178 | #endif |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 179 | |
| 180 | return install_filter(f); |
| 181 | } |
Paul Lawrence | 26f57b6 | 2017-03-27 15:38:37 -0700 | [diff] [blame] | 182 | |
Steve Muckle | aa3f96c | 2017-07-20 13:11:54 -0700 | [diff] [blame^] | 183 | bool set_seccomp_filter() { |
| 184 | return _set_seccomp_filter(false); |
| 185 | } |
| 186 | |
| 187 | bool set_global_seccomp_filter() { |
| 188 | return _set_seccomp_filter(true); |
| 189 | } |
| 190 | |
Paul Lawrence | 26f57b6 | 2017-03-27 15:38:37 -0700 | [diff] [blame] | 191 | void 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 | } |