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 |
Victor Hsieh | 4f02dd5 | 2017-12-20 09:19:22 -0800 | [diff] [blame^] | 35 | static const struct sock_filter* primary_app_filter = arm64_app_filter; |
| 36 | static const size_t primary_app_filter_size = arm64_app_filter_size; |
| 37 | static const struct sock_filter* primary_system_filter = arm64_system_filter; |
| 38 | static const size_t primary_system_filter_size = arm64_system_filter_size; |
Steve Muckle | aa3f96c | 2017-07-20 13:11:54 -0700 | [diff] [blame] | 39 | static const struct sock_filter* primary_global_filter = arm64_global_filter; |
| 40 | static const size_t primary_global_filter_size = arm64_global_filter_size; |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 41 | #define SECONDARY_ARCH AUDIT_ARCH_ARM |
Victor Hsieh | 4f02dd5 | 2017-12-20 09:19:22 -0800 | [diff] [blame^] | 42 | static const struct sock_filter* secondary_app_filter = arm_app_filter; |
| 43 | static const size_t secondary_app_filter_size = arm_app_filter_size; |
| 44 | static const struct sock_filter* secondary_system_filter = arm_system_filter; |
| 45 | static const size_t secondary_system_filter_size = arm_system_filter_size; |
Steve Muckle | aa3f96c | 2017-07-20 13:11:54 -0700 | [diff] [blame] | 46 | static const struct sock_filter* secondary_global_filter = arm_global_filter; |
| 47 | static const size_t secondary_global_filter_size = arm_global_filter_size; |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 48 | |
| 49 | #elif defined __i386__ || defined __x86_64__ |
| 50 | |
| 51 | #define DUAL_ARCH |
| 52 | #define PRIMARY_ARCH AUDIT_ARCH_X86_64 |
Victor Hsieh | 4f02dd5 | 2017-12-20 09:19:22 -0800 | [diff] [blame^] | 53 | static const struct sock_filter* primary_app_filter = x86_64_app_filter; |
| 54 | static const size_t primary_app_filter_size = x86_64_app_filter_size; |
| 55 | static const struct sock_filter* primary_system_filter = x86_64_system_filter; |
| 56 | static const size_t primary_system_filter_size = x86_64_system_filter_size; |
Steve Muckle | aa3f96c | 2017-07-20 13:11:54 -0700 | [diff] [blame] | 57 | static const struct sock_filter* primary_global_filter = x86_64_global_filter; |
| 58 | 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] | 59 | #define SECONDARY_ARCH AUDIT_ARCH_I386 |
Victor Hsieh | 4f02dd5 | 2017-12-20 09:19:22 -0800 | [diff] [blame^] | 60 | static const struct sock_filter* secondary_app_filter = x86_app_filter; |
| 61 | static const size_t secondary_app_filter_size = x86_app_filter_size; |
| 62 | static const struct sock_filter* secondary_system_filter = x86_system_filter; |
| 63 | static const size_t secondary_system_filter_size = x86_system_filter_size; |
Steve Muckle | aa3f96c | 2017-07-20 13:11:54 -0700 | [diff] [blame] | 64 | static const struct sock_filter* secondary_global_filter = x86_global_filter; |
| 65 | static const size_t secondary_global_filter_size = x86_global_filter_size; |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 66 | |
| 67 | #elif defined __mips__ || defined __mips64__ |
| 68 | |
| 69 | #define DUAL_ARCH |
Lazar Trsic | 22b4351 | 2017-05-05 14:29:34 +0200 | [diff] [blame] | 70 | #define PRIMARY_ARCH AUDIT_ARCH_MIPSEL64 |
Victor Hsieh | 4f02dd5 | 2017-12-20 09:19:22 -0800 | [diff] [blame^] | 71 | static const struct sock_filter* primary_app_filter = mips64_app_filter; |
| 72 | static const size_t primary_app_filter_size = mips64_app_filter_size; |
| 73 | static const struct sock_filter* primary_system_filter = mips64_system_filter; |
| 74 | static const size_t primary_system_filter_size = mips64_system_filter_size; |
Steve Muckle | aa3f96c | 2017-07-20 13:11:54 -0700 | [diff] [blame] | 75 | static const struct sock_filter* primary_global_filter = mips64_global_filter; |
| 76 | static const size_t primary_global_filter_size = mips64_global_filter_size; |
Lazar Trsic | 22b4351 | 2017-05-05 14:29:34 +0200 | [diff] [blame] | 77 | #define SECONDARY_ARCH AUDIT_ARCH_MIPSEL |
Victor Hsieh | 4f02dd5 | 2017-12-20 09:19:22 -0800 | [diff] [blame^] | 78 | static const struct sock_filter* secondary_app_filter = mips_app_filter; |
| 79 | static const size_t secondary_app_filter_size = mips_app_filter_size; |
| 80 | static const struct sock_filter* secondary_system_filter = mips_system_filter; |
| 81 | static const size_t secondary_system_filter_size = mips_system_filter_size; |
Steve Muckle | aa3f96c | 2017-07-20 13:11:54 -0700 | [diff] [blame] | 82 | static const struct sock_filter* secondary_global_filter = mips_global_filter; |
| 83 | static const size_t secondary_global_filter_size = mips_global_filter_size; |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 84 | |
| 85 | #else |
| 86 | #error No architecture was defined! |
| 87 | #endif |
| 88 | |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 89 | |
| 90 | #define syscall_nr (offsetof(struct seccomp_data, nr)) |
| 91 | #define arch_nr (offsetof(struct seccomp_data, arch)) |
| 92 | |
| 93 | typedef std::vector<sock_filter> filter; |
| 94 | |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 95 | inline void Disallow(filter& f) { |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 96 | f.push_back(BPF_STMT(BPF_RET|BPF_K, SECCOMP_RET_TRAP)); |
| 97 | } |
| 98 | |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 99 | static void ExamineSyscall(filter& f) { |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 100 | f.push_back(BPF_STMT(BPF_LD|BPF_W|BPF_ABS, syscall_nr)); |
| 101 | } |
| 102 | |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 103 | #ifdef DUAL_ARCH |
| 104 | static bool SetValidateArchitectureJumpTarget(size_t offset, filter& f) { |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 105 | 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 Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 110 | return false; |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 111 | } |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 112 | f[offset] = BPF_JUMP(BPF_JMP|BPF_JEQ|BPF_K, SECONDARY_ARCH, u8_jump_length, 0); |
| 113 | return true; |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 114 | } |
| 115 | |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 116 | static size_t ValidateArchitectureAndJumpIfNeeded(filter& f) { |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 117 | 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] | 118 | 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 Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 121 | return f.size() - 2; |
| 122 | } |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 123 | #else |
| 124 | static 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 Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 130 | |
| 131 | static 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 | }; |
| 136 | |
Victor Hsieh | 4f02dd5 | 2017-12-20 09:19:22 -0800 | [diff] [blame^] | 137 | if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) == -1) { |
| 138 | PLOG(FATAL) << "Could not set to no new privs"; |
| 139 | return false; |
| 140 | } |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 141 | if (prctl(PR_SET_SECCOMP, SECCOMP_MODE_FILTER, &prog) < 0) { |
| 142 | PLOG(FATAL) << "Could not set seccomp filter of size " << f.size(); |
| 143 | return false; |
| 144 | } |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 145 | return true; |
| 146 | } |
| 147 | |
Victor Hsieh | 4f02dd5 | 2017-12-20 09:19:22 -0800 | [diff] [blame^] | 148 | enum FilterType { |
| 149 | APP, |
| 150 | SYSTEM, |
| 151 | GLOBAL |
| 152 | }; |
| 153 | |
| 154 | bool _set_seccomp_filter(FilterType type) { |
Steve Muckle | aa3f96c | 2017-07-20 13:11:54 -0700 | [diff] [blame] | 155 | const sock_filter *p, *s; |
| 156 | size_t p_size, s_size; |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 157 | filter f; |
| 158 | |
Victor Hsieh | 4f02dd5 | 2017-12-20 09:19:22 -0800 | [diff] [blame^] | 159 | switch (type) { |
| 160 | case APP: |
| 161 | p = primary_app_filter; |
| 162 | p_size = primary_app_filter_size; |
| 163 | s = secondary_app_filter; |
| 164 | s_size = secondary_app_filter_size; |
| 165 | break; |
| 166 | case SYSTEM: |
| 167 | p = primary_system_filter; |
| 168 | p_size = primary_system_filter_size; |
| 169 | s = secondary_system_filter; |
| 170 | s_size = secondary_system_filter_size; |
| 171 | break; |
| 172 | case GLOBAL: |
Steve Muckle | aa3f96c | 2017-07-20 13:11:54 -0700 | [diff] [blame] | 173 | p = primary_global_filter; |
| 174 | p_size = primary_global_filter_size; |
| 175 | s = secondary_global_filter; |
| 176 | s_size = secondary_global_filter_size; |
Victor Hsieh | 4f02dd5 | 2017-12-20 09:19:22 -0800 | [diff] [blame^] | 177 | break; |
| 178 | |
Steve Muckle | aa3f96c | 2017-07-20 13:11:54 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 181 | #ifdef DUAL_ARCH |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 182 | // Note that for mixed 64/32 bit architectures, ValidateArchitecture inserts a |
| 183 | // jump that must be changed to point to the start of the 32-bit policy |
| 184 | // 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] | 185 | auto offset_to_secondary_filter = ValidateArchitectureAndJumpIfNeeded(f); |
| 186 | #else |
| 187 | ValidateArchitecture(f); |
| 188 | #endif |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 189 | |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 190 | ExamineSyscall(f); |
| 191 | |
Steve Muckle | aa3f96c | 2017-07-20 13:11:54 -0700 | [diff] [blame] | 192 | for (size_t i = 0; i < p_size; ++i) { |
| 193 | f.push_back(p[i]); |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 194 | } |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 195 | Disallow(f); |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 196 | |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 197 | #ifdef DUAL_ARCH |
| 198 | if (!SetValidateArchitectureJumpTarget(offset_to_secondary_filter, f)) { |
| 199 | return false; |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 202 | ExamineSyscall(f); |
| 203 | |
Steve Muckle | aa3f96c | 2017-07-20 13:11:54 -0700 | [diff] [blame] | 204 | for (size_t i = 0; i < s_size; ++i) { |
| 205 | f.push_back(s[i]); |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 206 | } |
Paul Lawrence | 89fa81f | 2017-02-17 10:22:03 -0800 | [diff] [blame] | 207 | Disallow(f); |
| 208 | #endif |
Paul Lawrence | dfe8434 | 2017-02-16 09:24:39 -0800 | [diff] [blame] | 209 | |
| 210 | return install_filter(f); |
| 211 | } |
Paul Lawrence | 26f57b6 | 2017-03-27 15:38:37 -0700 | [diff] [blame] | 212 | |
Steve Muckle | aa3f96c | 2017-07-20 13:11:54 -0700 | [diff] [blame] | 213 | bool set_seccomp_filter() { |
Victor Hsieh | 4f02dd5 | 2017-12-20 09:19:22 -0800 | [diff] [blame^] | 214 | return _set_seccomp_filter(FilterType::APP); |
| 215 | } |
| 216 | |
| 217 | bool set_app_seccomp_filter() { |
| 218 | return _set_seccomp_filter(FilterType::APP); |
| 219 | } |
| 220 | |
| 221 | bool set_system_seccomp_filter() { |
| 222 | return _set_seccomp_filter(FilterType::SYSTEM); |
Steve Muckle | aa3f96c | 2017-07-20 13:11:54 -0700 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | bool set_global_seccomp_filter() { |
Victor Hsieh | 4f02dd5 | 2017-12-20 09:19:22 -0800 | [diff] [blame^] | 226 | return _set_seccomp_filter(FilterType::GLOBAL); |
Paul Lawrence | 26f57b6 | 2017-03-27 15:38:37 -0700 | [diff] [blame] | 227 | } |