From 52fd508feac4210b6640b4ebca984726a19f4747 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Sun, 14 Jul 2019 21:56:21 -0700 Subject: [PATCH] Do not use std::random_device Directly read from urandom instead of using std::random_device. libc++ will use iostream under-the-hood, which brings significant binary size increase that is not welcomed, especially in magiskinit. --- native/jni/init/rootdir.cpp | 2 +- native/jni/utils/misc.cpp | 16 ++++++++-------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/native/jni/init/rootdir.cpp b/native/jni/init/rootdir.cpp index c7e9ad96d..9e9cd0ada 100644 --- a/native/jni/init/rootdir.cpp +++ b/native/jni/init/rootdir.cpp @@ -48,7 +48,7 @@ static void patch_init_rc(FILE *rc) { fprintf(rc, "%s", line.data()); return true; }); - char pfd_svc[32], ls_svc[32], bc_svc[32]; + char pfd_svc[16], ls_svc[16], bc_svc[16]; gen_rand_str(pfd_svc, sizeof(pfd_svc)); gen_rand_str(ls_svc, sizeof(ls_svc)); gen_rand_str(bc_svc, sizeof(bc_svc)); diff --git a/native/jni/utils/misc.cpp b/native/jni/utils/misc.cpp index f0a270568..2d788b61b 100644 --- a/native/jni/utils/misc.cpp +++ b/native/jni/utils/misc.cpp @@ -53,16 +53,16 @@ int fork_no_zombie() { constexpr char ALPHANUM[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; static bool seeded = false; static std::mt19937 gen; -static std::uniform_int_distribution dist(0, sizeof(ALPHANUM) - 1); +static std::uniform_int_distribution dist(0, sizeof(ALPHANUM) - 2); void gen_rand_str(char *buf, int len, bool varlen) { if (!seeded) { - if (access("/dev/urandom", F_OK) == 0) { - std::random_device rdev; - gen.seed(rdev()); - } else { - // In magiskinit - gen.seed(time(nullptr)); - } + if (access("/dev/urandom", F_OK) != 0) + mknod("/dev/urandom", 0600 | S_IFCHR, makedev(1, 9)); + int fd = xopen("/dev/urandom", O_RDONLY | O_CLOEXEC); + unsigned seed; + xxread(fd, &seed, sizeof(seed)); + gen.seed(seed); + close(fd); seeded = true; } if (varlen) {