Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame^] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/rand_util.h" |
| 6 | |
[email protected] | 09e5f47a | 2009-06-26 10:00:02 | [diff] [blame] | 7 | #include <errno.h> |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 8 | #include <fcntl.h> |
avi | 9b6f4293 | 2015-12-26 22:15:14 | [diff] [blame] | 9 | #include <stddef.h> |
| 10 | #include <stdint.h> |
Egor Pasko | 75c09cc0 | 2022-08-08 18:14:13 | [diff] [blame] | 11 | #include <sys/syscall.h> |
| 12 | #include <sys/utsname.h> |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 13 | #include <unistd.h> |
| 14 | |
Hans Wennborg | c3cffa6 | 2020-04-27 10:09:12 | [diff] [blame] | 15 | #include "base/check.h" |
Chris Palmer | 3b3588b | 2020-08-28 17:04:21 | [diff] [blame] | 16 | #include "base/compiler_specific.h" |
Egor Pasko | 75c09cc0 | 2022-08-08 18:14:13 | [diff] [blame] | 17 | #include "base/feature_list.h" |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 18 | #include "base/files/file_util.h" |
Egor Pasko | 75c09cc0 | 2022-08-08 18:14:13 | [diff] [blame] | 19 | #include "base/metrics/histogram_macros.h" |
Lei Zhang | e3e126d78 | 2020-01-07 21:36:00 | [diff] [blame] | 20 | #include "base/no_destructor.h" |
mark | 4cec494 | 2017-02-28 23:56:00 | [diff] [blame] | 21 | #include "base/posix/eintr_wrapper.h" |
Egor Pasko | 75c09cc0 | 2022-08-08 18:14:13 | [diff] [blame] | 22 | #include "base/time/time.h" |
Chris Palmer | 254cd5f | 2020-08-12 20:37:07 | [diff] [blame] | 23 | #include "build/build_config.h" |
| 24 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 25 | #if (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)) && !BUILDFLAG(IS_NACL) |
Chris Palmer | 3b3588b | 2020-08-28 17:04:21 | [diff] [blame] | 26 | #include "third_party/lss/linux_syscall_support.h" |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 27 | #elif BUILDFLAG(IS_MAC) |
Chris Palmer | 254cd5f | 2020-08-12 20:37:07 | [diff] [blame] | 28 | // TODO(crbug.com/995996): Waiting for this header to appear in the iOS SDK. |
Chris Palmer | 3b3588b | 2020-08-28 17:04:21 | [diff] [blame] | 29 | // (See below.) |
Chris Palmer | 254cd5f | 2020-08-12 20:37:07 | [diff] [blame] | 30 | #include <sys/random.h> |
| 31 | #endif |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 32 | |
[email protected] | 09e5f47a | 2009-06-26 10:00:02 | [diff] [blame] | 33 | namespace { |
| 34 | |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 35 | #if BUILDFLAG(IS_AIX) |
Chris Palmer | 3b3588b | 2020-08-28 17:04:21 | [diff] [blame] | 36 | // AIX has no 64-bit support for O_CLOEXEC. |
| 37 | static constexpr int kOpenFlags = O_RDONLY; |
| 38 | #else |
| 39 | static constexpr int kOpenFlags = O_RDONLY | O_CLOEXEC; |
| 40 | #endif |
| 41 | |
[email protected] | 09e5f47a | 2009-06-26 10:00:02 | [diff] [blame] | 42 | // We keep the file descriptor for /dev/urandom around so we don't need to |
| 43 | // reopen it (which is expensive), and since we may not even be able to reopen |
| 44 | // it if we are later put in a sandbox. This class wraps the file descriptor so |
Lei Zhang | e3e126d78 | 2020-01-07 21:36:00 | [diff] [blame] | 45 | // we can use a static-local variable to handle opening it on the first access. |
[email protected] | 09e5f47a | 2009-06-26 10:00:02 | [diff] [blame] | 46 | class URandomFd { |
| 47 | public: |
Chris Palmer | 3b3588b | 2020-08-28 17:04:21 | [diff] [blame] | 48 | URandomFd() : fd_(HANDLE_EINTR(open("/dev/urandom", kOpenFlags))) { |
| 49 | CHECK(fd_ >= 0) << "Cannot open /dev/urandom"; |
rayb | 0088ee5 | 2017-04-26 22:35:08 | [diff] [blame] | 50 | } |
[email protected] | 09e5f47a | 2009-06-26 10:00:02 | [diff] [blame] | 51 | |
[email protected] | c910c5a | 2014-01-23 02:14:28 | [diff] [blame] | 52 | ~URandomFd() { close(fd_); } |
[email protected] | 09e5f47a | 2009-06-26 10:00:02 | [diff] [blame] | 53 | |
| 54 | int fd() const { return fd_; } |
| 55 | |
| 56 | private: |
[email protected] | c910c5a | 2014-01-23 02:14:28 | [diff] [blame] | 57 | const int fd_; |
[email protected] | 09e5f47a | 2009-06-26 10:00:02 | [diff] [blame] | 58 | }; |
| 59 | |
Egor Pasko | 75c09cc0 | 2022-08-08 18:14:13 | [diff] [blame] | 60 | #if (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || \ |
| 61 | BUILDFLAG(IS_ANDROID)) && \ |
| 62 | !BUILDFLAG(IS_NACL) |
| 63 | // TODO(pasko): Unify reading kernel version numbers in: |
| 64 | // mojo/core/channel_linux.cc |
| 65 | // chrome/browser/android/seccomp_support_detector.cc |
| 66 | void KernelVersionNumbers(int32_t* major_version, |
| 67 | int32_t* minor_version, |
| 68 | int32_t* bugfix_version) { |
| 69 | struct utsname info; |
| 70 | if (uname(&info) < 0) { |
| 71 | NOTREACHED(); |
| 72 | *major_version = 0; |
| 73 | *minor_version = 0; |
| 74 | *bugfix_version = 0; |
| 75 | return; |
| 76 | } |
| 77 | int num_read = sscanf(info.release, "%d.%d.%d", major_version, minor_version, |
| 78 | bugfix_version); |
| 79 | if (num_read < 1) |
| 80 | *major_version = 0; |
| 81 | if (num_read < 2) |
| 82 | *minor_version = 0; |
| 83 | if (num_read < 3) |
| 84 | *bugfix_version = 0; |
| 85 | } |
| 86 | |
| 87 | bool KernelSupportsGetRandom() { |
| 88 | int32_t major = 0; |
| 89 | int32_t minor = 0; |
| 90 | int32_t bugfix = 0; |
| 91 | KernelVersionNumbers(&major, &minor, &bugfix); |
| 92 | if (major >= 3 && minor >= 17) |
| 93 | return true; |
| 94 | return false; |
| 95 | } |
| 96 | |
| 97 | bool GetRandomSyscall(void* output, size_t output_length) { |
| 98 | // We have to call `getrandom` via Linux Syscall Support, rather than through |
| 99 | // the libc wrapper, because we might not have an up-to-date libc (e.g. on |
| 100 | // some bots). |
| 101 | const ssize_t r = |
| 102 | HANDLE_EINTR(syscall(__NR_getrandom, output, output_length, 0)); |
| 103 | |
| 104 | // Return success only on total success. In case errno == ENOSYS (or any other |
| 105 | // error), we'll fall through to reading from urandom below. |
| 106 | if (output_length == static_cast<size_t>(r)) { |
| 107 | MSAN_UNPOISON(output, output_length); |
| 108 | return true; |
| 109 | } |
| 110 | return false; |
| 111 | } |
| 112 | #endif // (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || |
| 113 | // BUILDFLAG(IS_ANDROID)) && !BUILDFLAG(IS_NACL) |
| 114 | |
| 115 | #if BUILDFLAG(IS_ANDROID) |
| 116 | std::atomic<bool> g_use_getrandom; |
| 117 | |
| 118 | const base::Feature kUseGetrandomForRandBytes{ |
| 119 | "UseGetrandomForRandBytes", base::FEATURE_DISABLED_BY_DEFAULT}; |
| 120 | |
| 121 | bool UseGetrandom() { |
| 122 | return g_use_getrandom.load(std::memory_order_relaxed); |
| 123 | } |
| 124 | #elif (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS)) && !BUILDFLAG(IS_NACL) |
| 125 | bool UseGetrandom() { |
| 126 | return true; |
| 127 | } |
| 128 | #endif |
| 129 | |
[email protected] | 09e5f47a | 2009-06-26 10:00:02 | [diff] [blame] | 130 | } // namespace |
| 131 | |
[email protected] | 05f9b68 | 2008-09-29 22:18:01 | [diff] [blame] | 132 | namespace base { |
| 133 | |
Egor Pasko | 75c09cc0 | 2022-08-08 18:14:13 | [diff] [blame] | 134 | #if BUILDFLAG(IS_ANDROID) |
| 135 | void ConfigureRandBytesFieldTrial() { |
| 136 | g_use_getrandom.store(FeatureList::IsEnabled(kUseGetrandomForRandBytes), |
| 137 | std::memory_order_relaxed); |
| 138 | } |
| 139 | #endif |
| 140 | |
Chris Palmer | 3b3588b | 2020-08-28 17:04:21 | [diff] [blame] | 141 | // NOTE: In an ideal future, all implementations of this function will just |
| 142 | // wrap BoringSSL's `RAND_bytes`. TODO(crbug.com/995996): Figure out the |
| 143 | // build/test/performance issues with dcheng's CL |
| 144 | // (https://chromium-review.googlesource.com/c/chromium/src/+/1545096) and land |
| 145 | // it or some form of it. |
[email protected] | c910c5a | 2014-01-23 02:14:28 | [diff] [blame] | 146 | void RandBytes(void* output, size_t output_length) { |
Egor Pasko | 75c09cc0 | 2022-08-08 18:14:13 | [diff] [blame] | 147 | #if (BUILDFLAG(IS_LINUX) || BUILDFLAG(IS_CHROMEOS) || \ |
| 148 | BUILDFLAG(IS_ANDROID)) && \ |
| 149 | !BUILDFLAG(IS_NACL) |
| 150 | if (UseGetrandom()) { |
| 151 | // On Android it is mandatory to check that the kernel _version_ has the |
| 152 | // support for a syscall before calling. The same check is made on Linux and |
| 153 | // ChromeOS to avoid making a syscall that predictably returns ENOSYS. |
| 154 | static const bool kernel_has_support = KernelSupportsGetRandom(); |
| 155 | if (kernel_has_support && GetRandomSyscall(output, output_length)) |
| 156 | return; |
Chris Palmer | 3b3588b | 2020-08-28 17:04:21 | [diff] [blame] | 157 | } |
Xiaohan Wang | 38e4ebb | 2022-01-19 06:57:43 | [diff] [blame] | 158 | #elif BUILDFLAG(IS_MAC) |
Chris Palmer | 254cd5f | 2020-08-12 20:37:07 | [diff] [blame] | 159 | // TODO(crbug.com/995996): Enable this on iOS too, when sys/random.h arrives |
| 160 | // in its SDK. |
Avi Drissman | ba195b3 | 2022-05-19 02:53:34 | [diff] [blame] | 161 | if (getentropy(output, output_length) == 0) { |
| 162 | return; |
Chris Palmer | 254cd5f | 2020-08-12 20:37:07 | [diff] [blame] | 163 | } |
Chris Palmer | 254cd5f | 2020-08-12 20:37:07 | [diff] [blame] | 164 | #endif |
| 165 | |
Chris Palmer | 3b3588b | 2020-08-28 17:04:21 | [diff] [blame] | 166 | // If the OS-specific mechanisms didn't work, fall through to reading from |
| 167 | // urandom. |
| 168 | // |
| 169 | // TODO(crbug.com/995996): When we no longer need to support old Linux |
| 170 | // kernels, we can get rid of this /dev/urandom branch altogether. |
Lei Zhang | e3e126d78 | 2020-01-07 21:36:00 | [diff] [blame] | 171 | const int urandom_fd = GetUrandomFD(); |
[email protected] | c910c5a | 2014-01-23 02:14:28 | [diff] [blame] | 172 | const bool success = |
| 173 | ReadFromFD(urandom_fd, static_cast<char*>(output), output_length); |
| 174 | CHECK(success); |
| 175 | } |
| 176 | |
Lei Zhang | e3e126d78 | 2020-01-07 21:36:00 | [diff] [blame] | 177 | int GetUrandomFD() { |
| 178 | static NoDestructor<URandomFd> urandom_fd; |
| 179 | return urandom_fd->fd(); |
[email protected] | 1d87fad | 2010-03-04 20:18:55 | [diff] [blame] | 180 | } |
[email protected] | ead8c1f | 2012-05-30 14:26:13 | [diff] [blame] | 181 | |
| 182 | } // namespace base |