MIPS: relocatable: fix possible boot hangup with KASLR enabled
authorAlexander Lobakin <alobakin@pm.me>
Sun, 10 Jan 2021 14:21:05 +0000 (14:21 +0000)
committerThomas Bogendoerfer <tsbogend@alpha.franken.de>
Wed, 13 Jan 2021 09:59:04 +0000 (10:59 +0100)
LLVM-built Linux triggered a boot hangup with KASLR enabled.

arch/mips/kernel/relocate.c:get_random_boot() uses linux_banner,
which is a string constant, as a random seed, but accesses it
as an array of unsigned long (in rotate_xor()).
When the address of linux_banner is not aligned to sizeof(long),
such access emits unaligned access exception and hangs the kernel.

Use PTR_ALIGN() to align input address to sizeof(long) and also
align down the input length to prevent possible access-beyond-end.

Fixes: 405bc8fd12f5 ("MIPS: Kernel: Implement KASLR using CONFIG_RELOCATABLE")
Cc: stable@vger.kernel.org # 4.7+
Signed-off-by: Alexander Lobakin <alobakin@pm.me>
Tested-by: Nathan Chancellor <natechancellor@gmail.com>
Reviewed-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Thomas Bogendoerfer <tsbogend@alpha.franken.de>
arch/mips/kernel/relocate.c

index 47aeb3350a76029edaf96a03d29d8ce975058232..0e365b7c742d93b0a0c1ce06fdbcb0aa8b180dff 100644 (file)
@@ -187,8 +187,14 @@ static int __init relocate_exception_table(long offset)
 static inline __init unsigned long rotate_xor(unsigned long hash,
                                              const void *area, size_t size)
 {
-       size_t i;
-       unsigned long *ptr = (unsigned long *)area;
+       const typeof(hash) *ptr = PTR_ALIGN(area, sizeof(hash));
+       size_t diff, i;
+
+       diff = (void *)ptr - area;
+       if (unlikely(size < diff + sizeof(hash)))
+               return hash;
+
+       size = ALIGN_DOWN(size - diff, sizeof(hash));
 
        for (i = 0; i < size / sizeof(hash); i++) {
                /* Rotate by odd number of bits and XOR. */