Skip to content

Commit 9fea59b

Browse files
committed
powerpc/mm: Add support for runtime configuration of ASLR limits
Add powerpc support for mmap_rnd_bits and mmap_rnd_compat_bits, which are two sysctls that allow a user to configure the number of bits of randomness used for ASLR. Because of the way the Kconfig for ARCH_MMAP_RND_BITS is defined, we have to construct at least the MIN value in Kconfig, vs in a header which would be more natural. Given that we just go ahead and do it all in Kconfig. At least according to the code (the documentation makes no mention of it), the value is defined as the number of bits of randomisation *of the page*, not the address. This makes some sense, with larger page sizes more of the low bits are forced to zero, which would reduce the randomisation if we didn't take the PAGE_SIZE into account. However it does mean the min/max values have to change depending on the PAGE_SIZE in order to actually limit the amount of address space consumed by the randomisation. The result of that is that we have to define the default values based on both 32-bit vs 64-bit, but also the configured PAGE_SIZE. Furthermore now that we have 128TB address space support on Book3S, we also have to take that into account. Finally we can wire up the value in arch_mmap_rnd(). Signed-off-by: Michael Ellerman <[email protected]> Signed-off-by: Bhupesh Sharma <[email protected]> Tested-by: Bhupesh Sharma <[email protected]> Reviewed-by: Kees Cook <[email protected]> Reviewed-by: Aneesh Kumar K.V <[email protected]>
1 parent f855b2f commit 9fea59b

File tree

2 files changed

+50
-5
lines changed

2 files changed

+50
-5
lines changed

arch/powerpc/Kconfig

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,48 @@ config MMU
2222
bool
2323
default y
2424

25+
config ARCH_MMAP_RND_BITS_MAX
26+
# On Book3S 64, the default virtual address space for 64-bit processes
27+
# is 2^47 (128TB). As a maximum, allow randomisation to consume up to
28+
# 32T of address space (2^45), which should ensure a reasonable gap
29+
# between bottom-up and top-down allocations for applications that
30+
# consume "normal" amounts of address space. Book3S 64 only supports 64K
31+
# and 4K page sizes.
32+
default 29 if PPC_BOOK3S_64 && PPC_64K_PAGES # 29 = 45 (32T) - 16 (64K)
33+
default 33 if PPC_BOOK3S_64 # 33 = 45 (32T) - 12 (4K)
34+
#
35+
# On all other 64-bit platforms (currently only Book3E), the virtual
36+
# address space is 2^46 (64TB). Allow randomisation to consume up to 16T
37+
# of address space (2^44). Only 4K page sizes are supported.
38+
default 32 if 64BIT # 32 = 44 (16T) - 12 (4K)
39+
#
40+
# For 32-bit, use the compat values, as they're the same.
41+
default ARCH_MMAP_RND_COMPAT_BITS_MAX
42+
43+
config ARCH_MMAP_RND_BITS_MIN
44+
# Allow randomisation to consume up to 1GB of address space (2^30).
45+
default 14 if 64BIT && PPC_64K_PAGES # 14 = 30 (1GB) - 16 (64K)
46+
default 18 if 64BIT # 18 = 30 (1GB) - 12 (4K)
47+
#
48+
# For 32-bit, use the compat values, as they're the same.
49+
default ARCH_MMAP_RND_COMPAT_BITS_MIN
50+
51+
config ARCH_MMAP_RND_COMPAT_BITS_MAX
52+
# Total virtual address space for 32-bit processes is 2^31 (2GB).
53+
# Allow randomisation to consume up to 512MB of address space (2^29).
54+
default 11 if PPC_256K_PAGES # 11 = 29 (512MB) - 18 (256K)
55+
default 13 if PPC_64K_PAGES # 13 = 29 (512MB) - 16 (64K)
56+
default 15 if PPC_16K_PAGES # 15 = 29 (512MB) - 14 (16K)
57+
default 17 # 17 = 29 (512MB) - 12 (4K)
58+
59+
config ARCH_MMAP_RND_COMPAT_BITS_MIN
60+
# Total virtual address space for 32-bit processes is 2^31 (2GB).
61+
# Allow randomisation to consume up to 8MB of address space (2^23).
62+
default 5 if PPC_256K_PAGES # 5 = 23 (8MB) - 18 (256K)
63+
default 7 if PPC_64K_PAGES # 7 = 23 (8MB) - 16 (64K)
64+
default 9 if PPC_16K_PAGES # 9 = 23 (8MB) - 14 (16K)
65+
default 11 # 11 = 23 (8MB) - 12 (4K)
66+
2567
config HAVE_SETUP_PER_CPU_AREA
2668
def_bool PPC64
2769

@@ -120,6 +162,8 @@ config PPC
120162
select HAVE_ARCH_HARDENED_USERCOPY
121163
select HAVE_ARCH_JUMP_LABEL
122164
select HAVE_ARCH_KGDB
165+
select HAVE_ARCH_MMAP_RND_BITS
166+
select HAVE_ARCH_MMAP_RND_COMPAT_BITS if COMPAT
123167
select HAVE_ARCH_SECCOMP_FILTER
124168
select HAVE_ARCH_TRACEHOOK
125169
select HAVE_CBPF_JIT if !PPC64

arch/powerpc/mm/mmap.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,14 @@ static inline int mmap_is_legacy(void)
5959

6060
unsigned long arch_mmap_rnd(void)
6161
{
62-
unsigned long rnd;
62+
unsigned long shift, rnd;
6363

64-
/* 8MB for 32bit, 1GB for 64bit */
64+
shift = mmap_rnd_bits;
65+
#ifdef CONFIG_COMPAT
6566
if (is_32bit_task())
66-
rnd = get_random_long() % (1<<(23-PAGE_SHIFT));
67-
else
68-
rnd = get_random_long() % (1UL<<(30-PAGE_SHIFT));
67+
shift = mmap_rnd_compat_bits;
68+
#endif
69+
rnd = get_random_long() % (1 << shift);
6970

7071
return rnd << PAGE_SHIFT;
7172
}

0 commit comments

Comments
 (0)