Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [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 "security.h" |
Jiyong Park | 11d7bc5 | 2022-07-15 13:44:14 +0900 | [diff] [blame^] | 18 | #include "util.h" |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 19 | |
| 20 | #include <errno.h> |
| 21 | #include <fcntl.h> |
Ryan Savitski | f0f7e70 | 2020-01-14 22:02:53 +0000 | [diff] [blame] | 22 | #include <linux/perf_event.h> |
Ryan Savitski | ea93f11 | 2020-10-28 18:01:35 +0000 | [diff] [blame] | 23 | #include <selinux/selinux.h> |
Ryan Savitski | f0f7e70 | 2020-01-14 22:02:53 +0000 | [diff] [blame] | 24 | #include <sys/ioctl.h> |
| 25 | #include <sys/syscall.h> |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 26 | #include <unistd.h> |
| 27 | |
| 28 | #include <fstream> |
| 29 | |
| 30 | #include <android-base/logging.h> |
Ryan Savitski | f0f7e70 | 2020-01-14 22:02:53 +0000 | [diff] [blame] | 31 | #include <android-base/properties.h> |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 32 | #include <android-base/unique_fd.h> |
| 33 | |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 34 | using android::base::unique_fd; |
Ryan Savitski | f0f7e70 | 2020-01-14 22:02:53 +0000 | [diff] [blame] | 35 | using android::base::SetProperty; |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 36 | |
| 37 | namespace android { |
| 38 | namespace init { |
| 39 | |
Tom Cherry | 7c1d87e | 2019-07-10 11:18:24 -0700 | [diff] [blame] | 40 | static bool SetHighestAvailableOptionValue(const std::string& path, int min, int max) { |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 41 | std::ifstream inf(path, std::fstream::in); |
| 42 | if (!inf) { |
| 43 | LOG(ERROR) << "Cannot open for reading: " << path; |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | int current = max; |
| 48 | while (current >= min) { |
| 49 | // try to write out new value |
| 50 | std::string str_val = std::to_string(current); |
| 51 | std::ofstream of(path, std::fstream::out); |
| 52 | if (!of) { |
| 53 | LOG(ERROR) << "Cannot open for writing: " << path; |
| 54 | return false; |
| 55 | } |
| 56 | of << str_val << std::endl; |
| 57 | of.close(); |
| 58 | |
| 59 | // check to make sure it was recorded |
| 60 | inf.seekg(0); |
| 61 | std::string str_rec; |
| 62 | inf >> str_rec; |
| 63 | if (str_val.compare(str_rec) == 0) { |
| 64 | break; |
| 65 | } |
| 66 | current--; |
| 67 | } |
| 68 | inf.close(); |
| 69 | |
| 70 | if (current < min) { |
| 71 | LOG(ERROR) << "Unable to set minimum option value " << min << " in " << path; |
| 72 | return false; |
| 73 | } |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | #define MMAP_RND_PATH "/proc/sys/vm/mmap_rnd_bits" |
| 78 | #define MMAP_RND_COMPAT_PATH "/proc/sys/vm/mmap_rnd_compat_bits" |
| 79 | |
Elliott Hughes | f77f6f0 | 2020-02-21 13:25:54 -0800 | [diff] [blame] | 80 | static bool SetMmapRndBitsMin(int start, int min, bool compat) { |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 81 | std::string path; |
| 82 | if (compat) { |
| 83 | path = MMAP_RND_COMPAT_PATH; |
| 84 | } else { |
| 85 | path = MMAP_RND_PATH; |
| 86 | } |
| 87 | |
| 88 | return SetHighestAvailableOptionValue(path, min, start); |
| 89 | } |
| 90 | |
| 91 | // Set /proc/sys/vm/mmap_rnd_bits and potentially |
| 92 | // /proc/sys/vm/mmap_rnd_compat_bits to the maximum supported values. |
| 93 | // Returns -1 if unable to set these to an acceptable value. |
| 94 | // |
| 95 | // To support this sysctl, the following upstream commits are needed: |
| 96 | // |
| 97 | // d07e22597d1d mm: mmap: add new /proc tunable for mmap_base ASLR |
| 98 | // e0c25d958f78 arm: mm: support ARCH_MMAP_RND_BITS |
| 99 | // 8f0d3aa9de57 arm64: mm: support ARCH_MMAP_RND_BITS |
| 100 | // 9e08f57d684a x86: mm: support ARCH_MMAP_RND_BITS |
| 101 | // ec9ee4acd97c drivers: char: random: add get_random_long() |
| 102 | // 5ef11c35ce86 mm: ASLR: use get_random_long() |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 103 | Result<void> SetMmapRndBitsAction(const BuiltinArguments&) { |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 104 | // values are arch-dependent |
| 105 | #if defined(USER_MODE_LINUX) |
| 106 | // uml does not support mmap_rnd_bits |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 107 | return {}; |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 108 | #elif defined(__aarch64__) |
| 109 | // arm64 supports 18 - 33 bits depending on pagesize and VA_SIZE |
Jiyong Park | 11d7bc5 | 2022-07-15 13:44:14 +0900 | [diff] [blame^] | 110 | if (SetMmapRndBitsMin(33, 24, false) && (!Has32BitAbi() || SetMmapRndBitsMin(16, 16, true))) { |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 111 | return {}; |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 112 | } |
| 113 | #elif defined(__x86_64__) |
| 114 | // x86_64 supports 28 - 32 bits |
Jiyong Park | 11d7bc5 | 2022-07-15 13:44:14 +0900 | [diff] [blame^] | 115 | if (SetMmapRndBitsMin(32, 32, false) && (!Has32BitAbi() || SetMmapRndBitsMin(16, 16, true))) { |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 116 | return {}; |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 117 | } |
| 118 | #elif defined(__arm__) || defined(__i386__) |
| 119 | // check to see if we're running on 64-bit kernel |
| 120 | bool h64 = !access(MMAP_RND_COMPAT_PATH, F_OK); |
| 121 | // supported 32-bit architecture must have 16 bits set |
| 122 | if (SetMmapRndBitsMin(16, 16, h64)) { |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 123 | return {}; |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 124 | } |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 125 | #else |
| 126 | LOG(ERROR) << "Unknown architecture"; |
| 127 | #endif |
| 128 | |
Tom Cherry | d8db7ab | 2017-08-17 17:28:30 -0700 | [diff] [blame] | 129 | LOG(FATAL) << "Unable to set adequate mmap entropy value!"; |
Tom Cherry | 557946e | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 130 | return Error(); |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | #define KPTR_RESTRICT_PATH "/proc/sys/kernel/kptr_restrict" |
| 134 | #define KPTR_RESTRICT_MINVALUE 2 |
| 135 | #define KPTR_RESTRICT_MAXVALUE 4 |
| 136 | |
| 137 | // Set kptr_restrict to the highest available level. |
| 138 | // |
| 139 | // Aborts if unable to set this to an acceptable value. |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 140 | Result<void> SetKptrRestrictAction(const BuiltinArguments&) { |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 141 | std::string path = KPTR_RESTRICT_PATH; |
| 142 | |
| 143 | if (!SetHighestAvailableOptionValue(path, KPTR_RESTRICT_MINVALUE, KPTR_RESTRICT_MAXVALUE)) { |
Tom Cherry | d8db7ab | 2017-08-17 17:28:30 -0700 | [diff] [blame] | 144 | LOG(FATAL) << "Unable to set adequate kptr_restrict value!"; |
Tom Cherry | 557946e | 2017-08-01 13:50:23 -0700 | [diff] [blame] | 145 | return Error(); |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 146 | } |
Tom Cherry | bbcbc2f | 2019-06-10 11:08:01 -0700 | [diff] [blame] | 147 | return {}; |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Ryan Savitski | f0f7e70 | 2020-01-14 22:02:53 +0000 | [diff] [blame] | 150 | // Test for whether the kernel has SELinux hooks for the perf_event_open() |
| 151 | // syscall. If the hooks are present, we can stop using the other permission |
| 152 | // mechanism (perf_event_paranoid sysctl), and use only the SELinux policy to |
| 153 | // control access to the syscall. The hooks are expected on all Android R |
| 154 | // release kernels, but might be absent on devices that upgrade while keeping an |
| 155 | // older kernel. |
| 156 | // |
| 157 | // There is no direct/synchronous way of finding out that a syscall failed due |
| 158 | // to SELinux. Therefore we test for a combination of a success and a failure |
| 159 | // that are explained by the platform's SELinux policy for the "init" domain: |
| 160 | // * cpu-scoped perf_event is allowed |
| 161 | // * ioctl() on the event fd is disallowed with EACCES |
| 162 | // |
| 163 | // Since init has CAP_SYS_ADMIN, these tests are not affected by the system-wide |
| 164 | // perf_event_paranoid sysctl. |
| 165 | // |
| 166 | // If the SELinux hooks are detected, a special sysprop |
| 167 | // (sys.init.perf_lsm_hooks) is set, which translates to a modification of |
| 168 | // perf_event_paranoid (through init.rc sysprop actions). |
| 169 | // |
| 170 | // TODO(b/137092007): this entire test can be removed once the platform stops |
| 171 | // supporting kernels that precede the perf_event_open hooks (Android common |
| 172 | // kernels 4.4 and 4.9). |
| 173 | Result<void> TestPerfEventSelinuxAction(const BuiltinArguments&) { |
Ryan Savitski | ea93f11 | 2020-10-28 18:01:35 +0000 | [diff] [blame] | 174 | // Special case: for *development devices* that boot with permissive |
| 175 | // SELinux, treat the LSM hooks as present for the effect of lowering the |
| 176 | // perf_event_paranoid sysctl. The sysprop is reused for pragmatic reasons, |
| 177 | // as there no existing way for init rules to check for permissive boot at |
| 178 | // the time of writing. |
| 179 | if (ALLOW_PERMISSIVE_SELINUX) { |
| 180 | if (!security_getenforce()) { |
| 181 | LOG(INFO) << "Permissive SELinux boot, forcing sys.init.perf_lsm_hooks to 1."; |
| 182 | SetProperty("sys.init.perf_lsm_hooks", "1"); |
| 183 | return {}; |
| 184 | } |
| 185 | } |
| 186 | |
Ryan Savitski | f0f7e70 | 2020-01-14 22:02:53 +0000 | [diff] [blame] | 187 | // Use a trivial event that will be configured, but not started. |
| 188 | struct perf_event_attr pe = { |
| 189 | .type = PERF_TYPE_SOFTWARE, |
| 190 | .size = sizeof(struct perf_event_attr), |
| 191 | .config = PERF_COUNT_SW_TASK_CLOCK, |
| 192 | .disabled = 1, |
| 193 | .exclude_kernel = 1, |
| 194 | }; |
| 195 | |
| 196 | // Open the above event targeting cpu 0. (EINTR not possible.) |
| 197 | unique_fd fd(static_cast<int>(syscall(__NR_perf_event_open, &pe, /*pid=*/-1, |
| 198 | /*cpu=*/0, |
| 199 | /*group_fd=*/-1, /*flags=*/0))); |
| 200 | if (fd == -1) { |
| 201 | PLOG(ERROR) << "Unexpected perf_event_open error"; |
| 202 | return {}; |
| 203 | } |
| 204 | |
| 205 | int ioctl_ret = ioctl(fd, PERF_EVENT_IOC_RESET); |
| 206 | if (ioctl_ret != -1) { |
| 207 | // Success implies that the kernel doesn't have the hooks. |
| 208 | return {}; |
| 209 | } else if (errno != EACCES) { |
| 210 | PLOG(ERROR) << "Unexpected perf_event ioctl error"; |
| 211 | return {}; |
| 212 | } |
| 213 | |
| 214 | // Conclude that the SELinux hooks are present. |
| 215 | SetProperty("sys.init.perf_lsm_hooks", "1"); |
| 216 | return {}; |
| 217 | } |
| 218 | |
Tom Cherry | 0c8d6d2 | 2017-08-10 12:22:44 -0700 | [diff] [blame] | 219 | } // namespace init |
| 220 | } // namespace android |