blob: f8976dec2f021cfbc755ae8d26e272d840232e26 [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"
18
19#include <errno.h>
20#include <fcntl.h>
21#include <unistd.h>
22
23#include <fstream>
24
25#include <android-base/logging.h>
26#include <android-base/unique_fd.h>
27
28#include "util.h"
29
30using android::base::unique_fd;
31
32namespace android {
33namespace init {
34
35// Writes 512 bytes of output from Hardware RNG (/dev/hw_random, backed
36// by Linux kernel's hw_random framework) into Linux RNG's via /dev/urandom.
37// Does nothing if Hardware RNG is not present.
38//
39// Since we don't yet trust the quality of Hardware RNG, these bytes are not
40// mixed into the primary pool of Linux RNG and the entropy estimate is left
41// unmodified.
42//
43// If the HW RNG device /dev/hw_random is present, we require that at least
44// 512 bytes read from it are written into Linux RNG. QA is expected to catch
45// devices/configurations where these I/O operations are blocking for a long
46// time. We do not reboot or halt on failures, as this is a best-effort
47// attempt.
Tom Cherry557946e2017-08-01 13:50:23 -070048Result<Success> MixHwrngIntoLinuxRngAction(const std::vector<std::string>& args) {
Tom Cherry0c8d6d22017-08-10 12:22:44 -070049 unique_fd hwrandom_fd(
50 TEMP_FAILURE_RETRY(open("/dev/hw_random", O_RDONLY | O_NOFOLLOW | O_CLOEXEC)));
51 if (hwrandom_fd == -1) {
52 if (errno == ENOENT) {
53 LOG(INFO) << "/dev/hw_random not found";
54 // It's not an error to not have a Hardware RNG.
Tom Cherry557946e2017-08-01 13:50:23 -070055 return Success();
Tom Cherry0c8d6d22017-08-10 12:22:44 -070056 }
Tom Cherry557946e2017-08-01 13:50:23 -070057 return ErrnoError() << "Failed to open /dev/hw_random";
Tom Cherry0c8d6d22017-08-10 12:22:44 -070058 }
59
60 unique_fd urandom_fd(
61 TEMP_FAILURE_RETRY(open("/dev/urandom", O_WRONLY | O_NOFOLLOW | O_CLOEXEC)));
62 if (urandom_fd == -1) {
Tom Cherry557946e2017-08-01 13:50:23 -070063 return ErrnoError() << "Failed to open /dev/urandom";
Tom Cherry0c8d6d22017-08-10 12:22:44 -070064 }
65
66 char buf[512];
67 size_t total_bytes_written = 0;
68 while (total_bytes_written < sizeof(buf)) {
69 ssize_t chunk_size =
70 TEMP_FAILURE_RETRY(read(hwrandom_fd, buf, sizeof(buf) - total_bytes_written));
71 if (chunk_size == -1) {
Tom Cherry557946e2017-08-01 13:50:23 -070072 return ErrnoError() << "Failed to read from /dev/hw_random";
Tom Cherry0c8d6d22017-08-10 12:22:44 -070073 } else if (chunk_size == 0) {
Tom Cherry557946e2017-08-01 13:50:23 -070074 return Error() << "Failed to read from /dev/hw_random: EOF";
Tom Cherry0c8d6d22017-08-10 12:22:44 -070075 }
76
77 chunk_size = TEMP_FAILURE_RETRY(write(urandom_fd, buf, chunk_size));
78 if (chunk_size == -1) {
Tom Cherry557946e2017-08-01 13:50:23 -070079 return ErrnoError() << "Failed to write to /dev/urandom";
Tom Cherry0c8d6d22017-08-10 12:22:44 -070080 }
81 total_bytes_written += chunk_size;
82 }
83
84 LOG(INFO) << "Mixed " << total_bytes_written << " bytes from /dev/hw_random into /dev/urandom";
Tom Cherry557946e2017-08-01 13:50:23 -070085 return Success();
Tom Cherry0c8d6d22017-08-10 12:22:44 -070086}
87
88static bool SetHighestAvailableOptionValue(std::string path, int min, int max) {
89 std::ifstream inf(path, std::fstream::in);
90 if (!inf) {
91 LOG(ERROR) << "Cannot open for reading: " << path;
92 return false;
93 }
94
95 int current = max;
96 while (current >= min) {
97 // try to write out new value
98 std::string str_val = std::to_string(current);
99 std::ofstream of(path, std::fstream::out);
100 if (!of) {
101 LOG(ERROR) << "Cannot open for writing: " << path;
102 return false;
103 }
104 of << str_val << std::endl;
105 of.close();
106
107 // check to make sure it was recorded
108 inf.seekg(0);
109 std::string str_rec;
110 inf >> str_rec;
111 if (str_val.compare(str_rec) == 0) {
112 break;
113 }
114 current--;
115 }
116 inf.close();
117
118 if (current < min) {
119 LOG(ERROR) << "Unable to set minimum option value " << min << " in " << path;
120 return false;
121 }
122 return true;
123}
124
125#define MMAP_RND_PATH "/proc/sys/vm/mmap_rnd_bits"
126#define MMAP_RND_COMPAT_PATH "/proc/sys/vm/mmap_rnd_compat_bits"
127
128// __attribute__((unused)) due to lack of mips support: see mips block in SetMmapRndBitsAction
129static bool __attribute__((unused)) SetMmapRndBitsMin(int start, int min, bool compat) {
130 std::string path;
131 if (compat) {
132 path = MMAP_RND_COMPAT_PATH;
133 } else {
134 path = MMAP_RND_PATH;
135 }
136
137 return SetHighestAvailableOptionValue(path, min, start);
138}
139
140// Set /proc/sys/vm/mmap_rnd_bits and potentially
141// /proc/sys/vm/mmap_rnd_compat_bits to the maximum supported values.
142// Returns -1 if unable to set these to an acceptable value.
143//
144// To support this sysctl, the following upstream commits are needed:
145//
146// d07e22597d1d mm: mmap: add new /proc tunable for mmap_base ASLR
147// e0c25d958f78 arm: mm: support ARCH_MMAP_RND_BITS
148// 8f0d3aa9de57 arm64: mm: support ARCH_MMAP_RND_BITS
149// 9e08f57d684a x86: mm: support ARCH_MMAP_RND_BITS
150// ec9ee4acd97c drivers: char: random: add get_random_long()
151// 5ef11c35ce86 mm: ASLR: use get_random_long()
Tom Cherry557946e2017-08-01 13:50:23 -0700152Result<Success> SetMmapRndBitsAction(const std::vector<std::string>& args) {
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700153// values are arch-dependent
154#if defined(USER_MODE_LINUX)
155 // uml does not support mmap_rnd_bits
Tom Cherry557946e2017-08-01 13:50:23 -0700156 return Success();
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700157#elif defined(__aarch64__)
158 // arm64 supports 18 - 33 bits depending on pagesize and VA_SIZE
159 if (SetMmapRndBitsMin(33, 24, false) && SetMmapRndBitsMin(16, 16, true)) {
Tom Cherry557946e2017-08-01 13:50:23 -0700160 return Success();
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700161 }
162#elif defined(__x86_64__)
163 // x86_64 supports 28 - 32 bits
164 if (SetMmapRndBitsMin(32, 32, false) && SetMmapRndBitsMin(16, 16, true)) {
Tom Cherry557946e2017-08-01 13:50:23 -0700165 return Success();
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700166 }
167#elif defined(__arm__) || defined(__i386__)
168 // check to see if we're running on 64-bit kernel
169 bool h64 = !access(MMAP_RND_COMPAT_PATH, F_OK);
170 // supported 32-bit architecture must have 16 bits set
171 if (SetMmapRndBitsMin(16, 16, h64)) {
Tom Cherry557946e2017-08-01 13:50:23 -0700172 return Success();
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700173 }
174#elif defined(__mips__) || defined(__mips64__)
175 // TODO: add mips support b/27788820
Tom Cherry557946e2017-08-01 13:50:23 -0700176 return Success();
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700177#else
178 LOG(ERROR) << "Unknown architecture";
179#endif
180
181 LOG(ERROR) << "Unable to set adequate mmap entropy value!";
182 panic();
Tom Cherry557946e2017-08-01 13:50:23 -0700183 return Error();
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700184}
185
186#define KPTR_RESTRICT_PATH "/proc/sys/kernel/kptr_restrict"
187#define KPTR_RESTRICT_MINVALUE 2
188#define KPTR_RESTRICT_MAXVALUE 4
189
190// Set kptr_restrict to the highest available level.
191//
192// Aborts if unable to set this to an acceptable value.
Tom Cherry557946e2017-08-01 13:50:23 -0700193Result<Success> SetKptrRestrictAction(const std::vector<std::string>& args) {
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700194 std::string path = KPTR_RESTRICT_PATH;
195
196 if (!SetHighestAvailableOptionValue(path, KPTR_RESTRICT_MINVALUE, KPTR_RESTRICT_MAXVALUE)) {
197 LOG(ERROR) << "Unable to set adequate kptr_restrict value!";
198 panic();
Tom Cherry557946e2017-08-01 13:50:23 -0700199 return Error();
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700200 }
Tom Cherry557946e2017-08-01 13:50:23 -0700201 return Success();
Tom Cherry0c8d6d22017-08-10 12:22:44 -0700202}
203
204} // namespace init
205} // namespace android