mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] init: fix allocated page overlapping with PTR_ERR
@ 2024-04-18 10:29 Nam Cao
  2024-04-18 10:54 ` Mike Rapoport
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Nam Cao @ 2024-04-18 10:29 UTC (permalink / raw)
  To: Mike Rapoport, Andreas Dilger, Björn Töpel,
	linux-riscv, Thomas Gleixner, Andrew Morton,
	ndesaulniers @ google . com, Luis Chamberlain, Ingo Molnar,
	Christophe Leroy, Tejun Heo, Krister Johansen, Changbin Du,
	Arnd Bergmann, Geert Uytterhoeven, linux-kernel
  Cc: Nam Cao, stable

There is nothing preventing kernel memory allocators from allocating a
page that overlaps with PTR_ERR(), except for architecture-specific
code that setup memblock.

It was discovered that RISCV architecture doesn't setup memblock
corectly, leading to a page overlapping with PTR_ERR() being allocated,
and subsequently crashing the kernel (link in Close: )

The reported crash has nothing to do with PTR_ERR(): the last page
(at address 0xfffff000) being allocated leads to an unexpected
arithmetic overflow in ext4; but still, this page shouldn't be
allocated in the first place.

Because PTR_ERR() is an architecture-independent thing, we shouldn't
ask every single architecture to set this up. There may be other
architectures beside RISCV that have the same problem.

Fix this one and for all by reserving the physical memory page that
may be mapped to the last virtual memory page as part of low memory.

Unfortunately, this means if there is actual memory at this reserved
location, that memory will become inaccessible. However, if this page
is not reserved, it can only be accessed as high memory, so this
doesn't matter if high memory is not supported. Even if high memory is
supported, it is still only one page.

Closes: https://lore.kernel.org/linux-riscv/878r1ibpdn.fsf@all.your.base.are.belong.to.us
Signed-off-by: Nam Cao <namcao@linutronix.de>
Cc: <stable@vger.kernel.org> # all versions
---
 init/main.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/init/main.c b/init/main.c
index 881f6230ee59..f8d2793c4641 100644
--- a/init/main.c
+++ b/init/main.c
@@ -900,6 +900,7 @@ void start_kernel(void)
 	page_address_init();
 	pr_notice("%s", linux_banner);
 	early_security_init();
+	memblock_reserve(__pa(-PAGE_SIZE), PAGE_SIZE); /* reserve last page for ERR_PTR */
 	setup_arch(&command_line);
 	setup_boot_config();
 	setup_command_line(command_line);
-- 
2.39.2


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2024-05-10  6:35 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-04-18 10:29 [PATCH] init: fix allocated page overlapping with PTR_ERR Nam Cao
2024-04-18 10:54 ` Mike Rapoport
2024-04-18 11:12 ` Nam Cao
2024-04-18 12:41   ` Björn Töpel
2024-04-18 13:01     ` Nam Cao
2024-04-18 13:07       ` Nam Cao
     [not found] ` <CGME20240429125236eucas1p24219f2d332e0267794a2f87dea9f39c4@eucas1p2.samsung.com>
2024-04-29 12:52   ` Joel Granados
2024-04-30  7:31     ` Nam Cao
2024-04-30  8:37       ` Alexandre Ghiti
2024-04-30 13:35         ` Joel Granados
2024-04-30 15:42         ` Joel Granados
2024-05-10  6:35           ` Nam Cao

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®