blob: 8bf4d5299a46ca0c16b9043636934d1860a14148 [file] [log] [blame]
Tom Cherry0c8d6d22017-08-10 12:22:44 -07001/*
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 Park11d7bc52022-07-15 13:44:14 +090018#include "util.h"
Tom Cherry0c8d6d22017-08-10 12:22:44 -070019
20#include <errno.h>
21#include <fcntl.h>
Ryan Savitskif0f7e702020-01-14 22:02:53 +000022#include <linux/perf_event.h>
Ryan Savitskiea93f112020-10-28 18:01:35 +000023#include <selinux/selinux.h>
Ryan Savitskif0f7e702020-01-14 22:02:53 +000024#include <sys/ioctl.h>
25#include <sys/syscall.h>
Tom Cherry0c8d6d22017-08-10 12:22:44 -070026#include <unistd.h>
27
28#include <fstream>
29
30#include <android-base/logging.h>
Ryan Savitskif0f7e702020-01-14 22:02:53 +000031#include <android-base/properties.h>
Tom Cherry0c8d6d22017-08-10 12:22:44 -070032#include <android-base/unique_fd.h>
33
Tom Cherry0c8d6d22017-08-10 12:22:44 -070034using android::base::unique_fd;
Ryan Savitskif0f7e702020-01-14 22:02:53 +000035using android::base::SetProperty;
Tom Cherry0c8d6d22017-08-10 12:22:44 -070036
37namespace android {
38namespace init {
39
Tom Cherry7c1d87e2019-07-10 11:18:24 -070040static bool SetHighestAvailableOptionValue(const std::string& path, int min, int max) {
Tom Cherry0c8d6d22017-08-10 12:22:44 -070041 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 Hughesf77f6f02020-02-21 13:25:54 -080080static bool SetMmapRndBitsMin(int start, int min, bool compat) {
Tom Cherry0c8d6d22017-08-10 12:22:44 -070081 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 Cherrybbcbc2f2019-06-10 11:08:01 -0700103Result<void> SetMmapRndBitsAction(const BuiltinArguments&) {
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700104// values are arch-dependent
105#if defined(USER_MODE_LINUX)
106 // uml does not support mmap_rnd_bits
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700107 return {};
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700108#elif defined(__aarch64__)
109 // arm64 supports 18 - 33 bits depending on pagesize and VA_SIZE
Jiyong Park11d7bc52022-07-15 13:44:14 +0900110 if (SetMmapRndBitsMin(33, 24, false) && (!Has32BitAbi() || SetMmapRndBitsMin(16, 16, true))) {
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700111 return {};
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700112 }
113#elif defined(__x86_64__)
114 // x86_64 supports 28 - 32 bits
Jiyong Park11d7bc52022-07-15 13:44:14 +0900115 if (SetMmapRndBitsMin(32, 32, false) && (!Has32BitAbi() || SetMmapRndBitsMin(16, 16, true))) {
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700116 return {};
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700117 }
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 Cherrybbcbc2f2019-06-10 11:08:01 -0700123 return {};
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700124 }
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700125#else
126 LOG(ERROR) << "Unknown architecture";
127#endif
128
Tom Cherryd8db7ab2017-08-17 17:28:30 -0700129 LOG(FATAL) << "Unable to set adequate mmap entropy value!";
Tom Cherry557946e2017-08-01 13:50:23 -0700130 return Error();
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700131}
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 Cherrybbcbc2f2019-06-10 11:08:01 -0700140Result<void> SetKptrRestrictAction(const BuiltinArguments&) {
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700141 std::string path = KPTR_RESTRICT_PATH;
142
143 if (!SetHighestAvailableOptionValue(path, KPTR_RESTRICT_MINVALUE, KPTR_RESTRICT_MAXVALUE)) {
Tom Cherryd8db7ab2017-08-17 17:28:30 -0700144 LOG(FATAL) << "Unable to set adequate kptr_restrict value!";
Tom Cherry557946e2017-08-01 13:50:23 -0700145 return Error();
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700146 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700147 return {};
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700148}
149
Ryan Savitskif0f7e702020-01-14 22:02:53 +0000150// 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).
173Result<void> TestPerfEventSelinuxAction(const BuiltinArguments&) {
Ryan Savitskiea93f112020-10-28 18:01:35 +0000174 // 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 Savitskif0f7e702020-01-14 22:02:53 +0000187 // 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 Cherry0c8d6d22017-08-10 12:22:44 -0700219} // namespace init
220} // namespace android