[DragonFlyBSD - Bug #3466] (New) kgetenv(): stack buffer overflow on a long loader environment value
sigttou
bugtracker-admin at leaf.dragonflybsd.org
Tue Nov 10 02:03:26 PST 2026
Issue #3466 has been reported by sigttou.
----------------------------------------
Bug #3466: kgetenv(): stack buffer overflow on a long loader environment value
http://bugs.dragonflybsd.org/issues/3466
* Author: sigttou
* Status: New
* Priority: Normal
* Category: Kernel
* Target version: 6.4
* Start date: 2026-11-10
----------------------------------------
@kenv_init()@ copies every @name=value@ string of the loader environment into the dynamic environment without a length check. @kgetenv()@ then copies the value found there with @strcpy()@ into a stack buffer of @KENV_MNAMELEN + 1 + KENV_MVALLEN + 1@ (258) bytes, so a longer value overflows the kernel stack. @ksetenv()@ and the KENV_SET path enforce the limits; the strings from the loader do not. KENV_GET has no privilege check, so any user can trigger it with @kenv name at .
Reproduce: add a variable with a long value to @/boot/loader.conf@, for example @nx.l300="xxxx..."@ (300 characters), boot, and run @kenv nx.l300@ as any user.
Tested on 6.4.2-RELEASE guests (QEMU, UEFI and BIOS, release loader), value lengths from @loader.conf@:
|_. value length |_. result |
| 127, 200, 257 | works (257 characters plus NUL fill the buffer exactly) |
| 258 | no trap, the value reads back correctly; it writes one byte past the buffer |
| 300, 900 | "Fatal trap 9: general protection fault", the return address is overwritten by the value bytes, then the debugger |
The same trap with @su -m nobody -c "kenv nx.l900"@. Plain @kenv@ (KENV_DUMP) is not affected. A kernel built from master (@94448f622a06@, X86_64_GENERIC, UEFI guest with the 6.4.2 userland) behaves the same: 127, 200 and 257 characters work, 258 silently writes one byte past the buffer, 300 and 900 trap, also as an unprivileged user.
Patch:
@kenv_init()@ now skips loader environment entries that are too long, with a warning at boot, the way FreeBSD's @init_dynamic_kenv()@ does ("too long kenv string, ignoring"). An entry (name, '=' and value, plus the terminating NUL) longer than @KENV_MNAMELEN + KENV_MVALLEN + 2@ = 258 bytes is not copied into the dynamic environment, so @kgetenv()@ never finds a value that does not fit its 258 byte stack buffer. The bound is the same as in FreeBSD.
---Files--------------------------------
0001-kernel-Skip-oversized-loader-environment-entries.patch (1.42 KB)
--
You have received this notification because you have either subscribed to it, or are involved in it.
To change your notification preferences, please click here: http://bugs.dragonflybsd.org/my/account
More information about the Bugs
mailing list