Skip to content

Commit eedd38a

Browse files
committed
Let the kernel hwrng thread manage hw_random mixing.
This has been something the kernel does automatically since 2014, so there's no obvious reason to add extra work during boot to duplicate that effort. Bug: http://b/179086242 Test: treehugger Change-Id: I44cce99a892e4f2a6a303c2126bd29f955f5fb23
1 parent 3ce24b8 commit eedd38a

File tree

3 files changed

+0
-59
lines changed

3 files changed

+0
-59
lines changed

init/init.cpp

-5
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,6 @@ int SecondStageMain(int argc, char** argv) {
902902
am.QueueBuiltinAction(wait_for_coldboot_done_action, "wait_for_coldboot_done");
903903
am.QueueBuiltinAction(TransitionSnapuserdAction, "TransitionSnapuserd");
904904
// ... so that we can start queuing up actions that require stuff from /dev.
905-
am.QueueBuiltinAction(MixHwrngIntoLinuxRngAction, "MixHwrngIntoLinuxRng");
906905
am.QueueBuiltinAction(SetMmapRndBitsAction, "SetMmapRndBits");
907906
Keychords keychords;
908907
am.QueueBuiltinAction(
@@ -918,10 +917,6 @@ int SecondStageMain(int argc, char** argv) {
918917
// Trigger all the boot actions to get us started.
919918
am.QueueEventTrigger("init");
920919

921-
// Repeat mix_hwrng_into_linux_rng in case /dev/hw_random or /dev/random
922-
// wasn't ready immediately after wait_for_coldboot_done
923-
am.QueueBuiltinAction(MixHwrngIntoLinuxRngAction, "MixHwrngIntoLinuxRng");
924-
925920
// Don't mount filesystems or start core system services in charger mode.
926921
std::string bootmode = GetProperty("ro.bootmode", "");
927922
if (bootmode == "charger") {

init/security.cpp

-53
Original file line numberDiff line numberDiff line change
@@ -36,59 +36,6 @@ using android::base::SetProperty;
3636
namespace android {
3737
namespace init {
3838

39-
// Writes 512 bytes of output from Hardware RNG (/dev/hw_random, backed
40-
// by Linux kernel's hw_random framework) into Linux RNG's via /dev/urandom.
41-
// Does nothing if Hardware RNG is not present.
42-
//
43-
// Since we don't yet trust the quality of Hardware RNG, these bytes are not
44-
// mixed into the primary pool of Linux RNG and the entropy estimate is left
45-
// unmodified.
46-
//
47-
// If the HW RNG device /dev/hw_random is present, we require that at least
48-
// 512 bytes read from it are written into Linux RNG. QA is expected to catch
49-
// devices/configurations where these I/O operations are blocking for a long
50-
// time. We do not reboot or halt on failures, as this is a best-effort
51-
// attempt.
52-
Result<void> MixHwrngIntoLinuxRngAction(const BuiltinArguments&) {
53-
unique_fd hwrandom_fd(
54-
TEMP_FAILURE_RETRY(open("/dev/hw_random", O_RDONLY | O_NOFOLLOW | O_CLOEXEC)));
55-
if (hwrandom_fd == -1) {
56-
if (errno == ENOENT) {
57-
LOG(INFO) << "/dev/hw_random not found";
58-
// It's not an error to not have a Hardware RNG.
59-
return {};
60-
}
61-
return ErrnoError() << "Failed to open /dev/hw_random";
62-
}
63-
64-
unique_fd urandom_fd(
65-
TEMP_FAILURE_RETRY(open("/dev/urandom", O_WRONLY | O_NOFOLLOW | O_CLOEXEC)));
66-
if (urandom_fd == -1) {
67-
return ErrnoError() << "Failed to open /dev/urandom";
68-
}
69-
70-
char buf[512];
71-
size_t total_bytes_written = 0;
72-
while (total_bytes_written < sizeof(buf)) {
73-
ssize_t chunk_size =
74-
TEMP_FAILURE_RETRY(read(hwrandom_fd, buf, sizeof(buf) - total_bytes_written));
75-
if (chunk_size == -1) {
76-
return ErrnoError() << "Failed to read from /dev/hw_random";
77-
} else if (chunk_size == 0) {
78-
return Error() << "Failed to read from /dev/hw_random: EOF";
79-
}
80-
81-
chunk_size = TEMP_FAILURE_RETRY(write(urandom_fd, buf, chunk_size));
82-
if (chunk_size == -1) {
83-
return ErrnoError() << "Failed to write to /dev/urandom";
84-
}
85-
total_bytes_written += chunk_size;
86-
}
87-
88-
LOG(INFO) << "Mixed " << total_bytes_written << " bytes from /dev/hw_random into /dev/urandom";
89-
return {};
90-
}
91-
9239
static bool SetHighestAvailableOptionValue(const std::string& path, int min, int max) {
9340
std::ifstream inf(path, std::fstream::in);
9441
if (!inf) {

init/security.h

-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
namespace android {
2727
namespace init {
2828

29-
Result<void> MixHwrngIntoLinuxRngAction(const BuiltinArguments&);
3029
Result<void> SetMmapRndBitsAction(const BuiltinArguments&);
3130
Result<void> SetKptrRestrictAction(const BuiltinArguments&);
3231
Result<void> TestPerfEventSelinuxAction(const BuiltinArguments&);

0 commit comments

Comments
 (0)