From 24a47eccf7666e0c68d56bfa9dba972c92ab603e Mon Sep 17 00:00:00 2001 From: Kalesh Singh Date: Fri, 14 Jul 2023 14:29:42 -0700 Subject: [PATCH] x86_64: mmap rnd bits: Allow min bits of less than 32 With a regular x86 kernel we are always able to get 32 min bits for mmap randomization. However to emulate 16KB for x86 app developers the kernel can only provide 30 bits for the randomization due the to larger page size (PAGE_SHIFT =+ 2). Allow the min rand bits to be (32 - (PAGE_SHIFT - 12)) in order to generically support larger than 4KB page sizes. This should be a no-op change for all devices, except the x86 16KB emulator, since they will always be able to get 32 random bits. Bug: 309816695 Test: Boot test 4k x86 device Test: Boot test emulated 16k x86 device Change-Id: I48d47d56ac3aecb71a9e0093a7033bb60b89c2b7 Signed-off-by: Kalesh Singh --- init/security.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/init/security.cpp b/init/security.cpp index 0c73faedc..3e1544750 100644 --- a/init/security.cpp +++ b/init/security.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include #include @@ -121,8 +122,12 @@ Result SetMmapRndBitsAction(const BuiltinArguments&) { } #elif defined(__x86_64__) // x86_64 supports 28 - 32 rnd bits, but Android wants to ensure that the - // theoretical maximum of 32 bits is always supported and used. - if (SetMmapRndBitsMin(32, 32, false) && (!Has32BitAbi() || SetMmapRndBitsMin(16, 16, true))) { + // theoretical maximum of 32 bits is always supported and used; except in + // the case of the x86 page size emulator which supports a maximum + // of 30 bits for 16k page size, or 28 bits for 64k page size. + int max_bits = 32 - (static_cast(log2(getpagesize())) - 12); + if (SetMmapRndBitsMin(max_bits, max_bits, false) && + (!Has32BitAbi() || SetMmapRndBitsMin(16, 16, true))) { return {}; } #elif defined(__arm__) || defined(__i386__)