From 8062a791962db584ac0bb1b0f5d6fce54d8f9d54 Mon Sep 17 00:00:00 2001 From: Utkarsh Adhran Date: Thu, 9 Jul 2026 18:11:30 +0530 Subject: [PATCH 1/2] kernel: fix dangling pointers for multiboot cmdline and libc env MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Store the bootloader-provided multiboot cmdline pointer instead of .data() from a temporary pmr::string. Keep early-boot free of strdup/malloc. Use static storage for init_libc env strings. Tested: nix-build unittests.nix — 85/85 passed Closes: #2368 --- src/kernel/multiboot.cpp | 7 ++++++- src/platform/x86_pc/init_libc.cpp | 11 +++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/src/kernel/multiboot.cpp b/src/kernel/multiboot.cpp index 6e8bf3b21..9edd43768 100644 --- a/src/kernel/multiboot.cpp +++ b/src/kernel/multiboot.cpp @@ -181,7 +181,12 @@ void kernel::multiboot(uint32_t boot_addr) if (info->flags & MULTIBOOT_INFO_CMDLINE) { const auto* cmdline = (const char*) (uintptr_t) info->cmdline; INFO2("* Booted with parameters @ {}: {}", (const void*)(uintptr_t)info->cmdline, cmdline); - kernel::state().cmdline = std::pmr::string(cmdline).data(); + // Multiboot cmdline lives in bootloader-provided memory for the lifetime of + // the kernel. _multiboot_free_begin() reserves it when it sits past _end + // (and low-memory cmdlines are outside the free region). Do not copy into a + // temporary and store .data() — that dangles (see issue #2368 / #2273). + // Also avoid strdup/malloc here: early boot must not call into libc (#2252). + kernel::state().cmdline = cmdline; } if (info->flags & MULTIBOOT_INFO_MEM_MAP) { diff --git a/src/platform/x86_pc/init_libc.cpp b/src/platform/x86_pc/init_libc.cpp index ae39c919a..2e2c86eec 100644 --- a/src/platform/x86_pc/init_libc.cpp +++ b/src/platform/x86_pc/init_libc.cpp @@ -110,10 +110,13 @@ namespace x86 argv[1] = 0x0; int argc = 1; - // Env vars - argv[2] = std::pmr::string("LC_CTYPE=C").data(); - argv[3] = std::pmr::string("LC_ALL=C").data(); - argv[4] = std::pmr::string("USER=root").data(); + // Env vars (static storage — not .data() of a temporary pmr::string) + static char env_lc_ctype[] = "LC_CTYPE=C"; + static char env_lc_all[] = "LC_ALL=C"; + static char env_user[] = "USER=root"; + argv[2] = env_lc_ctype; + argv[3] = env_lc_all; + argv[4] = env_user; argv[5] = 0x0; // auxiliary vector From 3cef0ad1ec44a882367bddb1b0669dfa220c9196 Mon Sep 17 00:00:00 2001 From: Utkarsh Adhran Date: Fri, 10 Jul 2026 17:48:09 +0530 Subject: [PATCH 2/2] kernel: shorten multiboot cmdline lifetime comment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Tested: nix-build unittests.nix — 85/85 passed --- src/kernel/multiboot.cpp | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/kernel/multiboot.cpp b/src/kernel/multiboot.cpp index 9edd43768..fb1d59a81 100644 --- a/src/kernel/multiboot.cpp +++ b/src/kernel/multiboot.cpp @@ -181,11 +181,7 @@ void kernel::multiboot(uint32_t boot_addr) if (info->flags & MULTIBOOT_INFO_CMDLINE) { const auto* cmdline = (const char*) (uintptr_t) info->cmdline; INFO2("* Booted with parameters @ {}: {}", (const void*)(uintptr_t)info->cmdline, cmdline); - // Multiboot cmdline lives in bootloader-provided memory for the lifetime of - // the kernel. _multiboot_free_begin() reserves it when it sits past _end - // (and low-memory cmdlines are outside the free region). Do not copy into a - // temporary and store .data() — that dangles (see issue #2368 / #2273). - // Also avoid strdup/malloc here: early boot must not call into libc (#2252). + // Bootloader memory (_multiboot_free_begin); no temp .data()/strdup. kernel::state().cmdline = cmdline; }