// Copyright 2012 The Chromium Authors // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "partition_alloc/partition_alloc_base/rand_util.h" #include <fcntl.h> #include <sys/syscall.h> #include <unistd.h> #include <cerrno> #include <cstddef> #include <cstdint> #include <sstream> #include "partition_alloc/build_config.h" #include "partition_alloc/partition_alloc_base/check.h" #include "partition_alloc/partition_alloc_base/compiler_specific.h" #include "partition_alloc/partition_alloc_base/files/file_util.h" #include "partition_alloc/partition_alloc_base/no_destructor.h" #include "partition_alloc/partition_alloc_base/posix/eintr_wrapper.h" #if PA_BUILDFLAG(IS_MAC) // TODO(crbug.com/40641285): Waiting for this header to appear in the iOS SDK. // (See below.) #include <sys/random.h> #endif namespace { #if PA_BUILDFLAG(IS_AIX) // AIX has no 64-bit support for O_CLOEXEC. static constexpr int kOpenFlags = O_RDONLY; #else static constexpr int kOpenFlags = …; #endif // On Android the 'open' function has two versions: // int open(const char *pathname, int flags); // int open(const char *pathname, int flags, mode_t mode); // // This doesn't play well with WrapEINTR template. This alias helps the compiler // to make a decision. int OpenFile(const char* pathname, int flags) { … } // We keep the file descriptor for /dev/urandom around so we don't need to // reopen it (which is expensive), and since we may not even be able to reopen // it if we are later put in a sandbox. This class wraps the file descriptor so // we can use a static-local variable to handle opening it on the first access. class URandomFd { … }; int GetUrandomFD() { … } } // namespace namespace partition_alloc::internal::base { // NOTE: In an ideal future, all implementations of this function will just // wrap BoringSSL's `RAND_bytes`. TODO(crbug.com/40641285): Figure out the // build/test/performance issues with dcheng's CL // (https://chromium-review.googlesource.com/c/chromium/src/+/1545096) and land // it or some form of it. void RandBytes(void* output, size_t output_length) { … } } // namespace partition_alloc::internal::base