kobject: reserve an envp slot for the terminator
Subsystems (2)
cover kobject: reserve an envp slot for the terminator
From: Ruide Cao <[email protected]>
Hi Linux kernel maintainers,
We found an issue in lib/kobject_uevent.c.
This bug is tracked at: https://bugtracker.nebusec.ai/f/16082
add_uevent_var() permits envp_idx to reach ARRAY_SIZE(env->envp), filling the final pointer slot without reserving a trailing NULL. With CONFIG_UEVENT_HELPER and a nonempty helper path, an event containing exactly 61 variables before SEQNUM causes SEQNUM, HOME, and PATH to fill all 64 slots. call_usermodehelper then asynchronously passes this unterminated array to kernel_execve(). count_strings_kernel() reads beyond envp into envp_idx and env->buf, interprets those bytes as pointers, and later dereferences invalid addresses while copying the environment. Synthetic arguments allow the environment count to be tuned, resulting in an out-of-bounds scan and kernel crash.
Privilege model: writing a synthetic uevent to /sys/module/kernel/uevent and setting /proc/sys/kernel/hotplug require CAP_SYS_ADMIN; the reproducer runs as root. The crash also depends on CONFIG_UEVENT_HELPER and a nonempty helper path.
Compatibility: no uAPI change.
Reproducer:
#!/bin/sh
set -eu
# This PoC must run as root inside the guest.
printf '%s' /bin/true > /proc/sys/kernel/hotplug
echo 0 > /proc/sys/kernel/panic_on_warn
msg='add 11111111-1111-1111-1111-111111111111'
i=1
while [ "$i" -le 57 ]; do
msg="$msg K$i=V$i" i=$((i + 1))
done
printf '%s\n' "$msg" > /sys/module/kernel/uevent
sleep 2
We run the PoC in a 2 GB RAM x86 QEMU environment.
------BEGIN PoC------
#!/bin/sh
set -eu
# This PoC must run as root inside the guest.
printf '%s' /bin/true > /proc/sys/kernel/hotplug
echo 0 > /proc/sys/kernel/panic_on_warn
msg='add 11111111-1111-1111-1111-111111111111'
i=1
while [ "$i" -le 57 ]; do
msg="$msg K$i=V$i" i=$((i + 1))
done
printf '%s\n' "$msg" > /sys/module/kernel/uevent
sleep 2
------END PoC--------
----BEGIN crash log----
[ 161.975900][ T1043] BUG: unable to handle page fault for address: 0000000000656c75 [ 161.977567][ T1043] #PF: supervisor read access in kernel mode [ 161.978836][ T1043] #PF: error_code(0x0000) - not-present page [ 161.979824][ T1043] PGD 0 P4D 0 [ 161.980319][ T1043] Oops: Oops: 0000 [#1] SMP KASAN NOPTI [ 161.981257][ T1043] CPU: 0 UID: 0 PID: 1043 Comm: kworker/u16:0 Not tainted 6.12.95 #4 53deed68f5e8c87eac9412ec2ca16e6c37d9fe62 [ 161.983040][ T1043] Hardware name: QEMU Ubuntu 24.04 PC v2 (i440FX + PIIX, arch_caps fix, 1996), BIOS 1.16.3-debian-1.16.3-2 04/01/2014 [ 161.985205][ T1043] RIP: 0010:strnlen+0x1b/0x40 [ 161.986083][ T1043] Code: 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 f3 0f 1e fa 48 8d 14 37 48 89 f8 48 85 f6 75 0b eb 21 48 83 c0 01 48 39 c2 74 0d <80> 38 00 75 f2 48 29 f8 e9 c3 bd 36 00 48 89 d0 48 29 f8 e9 b8 bd [ 161.989083][ T1043] RSP: 0018:ffffc90003127e30 EFLAGS: 00010206 [ 161.990058][ T1043] RAX: 0000000000656c75 RBX: ffff88810df296a8 RCX: ffffffff81f0516d [ 161.991261][ T1043] RDX: 0000000000676c75 RSI: 0000000000020000 RDI: 0000000000656c75 [ 161.992481][ T1043] RBP: ffff888110090040 R08: 0000000000000001 R09: fffff9400006ea00 [ 161.993709][ T1043] R10: ffffea00003751f7 R11: 0000000000000001 R12: 0000000000656c75 [ 161.994995][ T1043] R13: ffff888117b20200 R14: ffff88810df29010 R15: dffffc0000000000 [ 161.996234][ T1043] FS: 0000000000000000(0000) GS:ffff888119a00000(0000) knlGS:0000000000000000 [ 161.997606][ T1043] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 161.998664][ T1043] CR2: 0000000000656c75 CR3: 0000000008534002 CR4: 0000000000770ef0 [ 161.999992][ T1043] PKRU: 55555554 [ 162.000517][ T1043] Call Trace: [ 162.001100][ T1043] <TASK> [ 162.001523][ T1043] copy_string_kernel+0x27/0x440 [ 162.002422][ T1043] ? srso_alias_return_thunk+0x5/0xfbef5 [ 162.003347][ T1043] ? __asan_memcpy+0x3c/0x60 [ 162.004120][ T1043] copy_strings_kernel+0x92/0x130 [ 162.004951][ T1043] kernel_execve+0x201/0x2e0 [ 162.005615][ T1043] call_usermodehelper_exec_async+0x21f/0x440 [ 162.006658][ T1043] ? trace_hardirqs_on+0x5b/0x110 [ 162.007530][ T1043] ? __pfx_call_usermodehelper_exec_async+0x10/0x10 [ 162.008499][ T1043] ret_from_fork+0x31/0x70 [ 162.009271][ T1043] ? __pfx_call_usermodehelper_exec_async+0x10/0x10 [ 162.010331][ T1043] ret_from_fork_asm+0x1a/0x30 [ 162.011099][ T1043] </TASK> [ 162.011639][ T1043] Modules linked in: [ 162.012264][ T1043] CR2: 0000000000656c75 [ 162.012984][ T1043] ---[ end trace 0000000000000000 ]--- [ 162.013800][ T1043] RIP: 0010:strnlen+0x1b/0x40 [ 162.014558][ T1043] Code: 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 f3 0f 1e fa 48 8d 14 37 48 89 f8 48 85 f6 75 0b eb 21 48 83 c0 01 48 39 c2 74 0d <80> 38 00 75 f2 48 29 f8 e9 c3 bd 36 00 48 89 d0 48 29 f8 e9 b8 bd [ 162.017573][ T1043] RSP: 0018:ffffc90003127e30 EFLAGS: 00010206 [ 162.018548][ T1043] RAX: 0000000000656c75 RBX: ffff88810df296a8 RCX: ffffffff81f0516d [ 162.019831][ T1043] RDX: 0000000000676c75 RSI: 0000000000020000 RDI: 0000000000656c75 [ 162.021036][ T1043] RBP: ffff888110090040 R08: 0000000000000001 R09: fffff9400006ea00 [ 162.022264][ T1043] R10: ffffea00003751f7 R11: 0000000000000001 R12: 0000000000656c75 [ 162.023449][ T1043] R13: ffff888117b20200 R14: ffff88810df29010 R15: dffffc0000000000 [ 162.024845][ T1043] FS: 0000000000000000(0000) GS:ffff888119a00000(0000) knlGS:0000000000000000 [ 162.026300][ T1043] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [ 162.027378][ T1043] CR2: 0000000000656c75 CR3: 0000000008534002 CR4: 0000000000770ef0 [ 162.028229][ T1043] PKRU: 55555554 [ 162.028615][ T1043] Kernel panic - not syncing: Fatal exception [ 162.029525][ T1043] Kernel Offset: disabled [ 162.030001][ T1043] Rebooting in 10 seconds..
-----END crash log-----
Best regards,
Ruide Cao
Ruide Cao (1):
kobject: reserve an envp slot for the terminator
lib/kobject_uevent.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
base-commit: b6a9fa34ce4187cb69d3c588a4bbffc31ce5db8f
Signature · 1 line
2.47.3
PATCH 1/1 kobject: reserve an envp slot for the terminator
From: Ruide Cao <[email protected]>
add_uevent_var() currently allows envp_idx to reach ARRAY_SIZE(envp). This fills the final pointer slot without leaving room for the NULL terminator expected by usermode helper consumers.
A synthetic uevent can supply enough variables to make this happen. When CONFIG_UEVENT_HELPER is enabled, kernel_execve() then scans beyond the array while counting the environment and may dereference arbitrary data as a string pointer.
Reserve the final slot so an environment is always terminated.
--- lib/kobject_uevent.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
lib/kobject_uevent.c +1 -1
@@ -673,7 +673,7 @@ int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)va_list args;int len; -if (env->envp_idx >=ARRAY_SIZE (env->envp)) { +if (env->envp_idx >=ARRAY_SIZE (env->envp) -1 ) {WARN (1 ,KERN_ERR "add_uevent_var: too many keys\n" );return -ENOMEM ; }
Signature · 1 line
2.47.3
Greg KH
maintainerQuoted 19 lines · Ren Wei
From: Ruide Cao <[email protected]>
Hi Linux kernel maintainers,
We found an issue in
lib/kobject_uevent.c.This bug is tracked at: https://bugtracker.nebusec.ai/f/16082
add_uevent_var()permitsenvp_idxto reachARRAY_SIZE(env->envp), filling the final pointer slot without reserving a trailing NULL. WithCONFIG_UEVENT_HELPERand a nonempty helper path, an event containing exactly 61 variables before SEQNUM causes SEQNUM, HOME, and PATH to fill all 64 slots.call_usermodehelperthen asynchronously passes this unterminated array tokernel_execve().count_strings_kernel()reads beyond envp intoenvp_idxandenv->buf, interprets those bytes as pointers, and later dereferences invalid addresses while copying the environment. Synthetic arguments allow the environment count to be tuned, resulting in an out-of-bounds scan and kernel crash.
Quoted 4 lines
Privilege model: writing a synthetic uevent to
/sys/module/kernel/ueventand setting/proc/sys/kernel/hotplugrequireCAP_SYS_ADMIN; the reproducer runs as root. The crash also depends onCONFIG_UEVENT_HELPERand a nonempty helper path.
This has come up many times in the past, nice to see your LLM has read the mailing list archives :)
thanks,
greg k-h
Greg KH
maintainerQuoted 27 lines · Ren Wei
From: Ruide Cao <[email protected]>
add_uevent_var()currently allowsenvp_idxto reachARRAY_SIZE(envp). This fills the final pointer slot without leaving room for the NULL terminator expected by usermode helper consumers.A synthetic uevent can supply enough variables to make this happen. When
CONFIG_UEVENT_HELPERis enabled,kernel_execve()then scans beyond the array while counting the environment and may dereference arbitrary data as a string pointer.Reserve the final slot so an environment is always terminated.
Fixes 7eff2e7a8b65 ("Driver core: change add_uevent_var to use a struct")Reported-by Vega <[email protected]>Signed-off-by Ruide Cao <[email protected]>Signed-off-by Ren Wei <[email protected]>--- lib/kobject_uevent.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)lib/kobject_uevent.c +0
-0
lib/kobject_uevent.c int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...) · line 679
Quoted 8 lines
lib/kobject_uevent.c +1
-1@@ -673,7 +673,7 @@ int add_uevent_var(struct kobj_uevent_env *env, const char *format, ...)va_list args;int len; -if (env->envp_idx >=ARRAY_SIZE (env->envp)) { +if (env->envp_idx >=ARRAY_SIZE (env->envp) -1 ) {WARN (1 ,KERN_ERR "add_uevent_var: too many keys\n" );return -ENOMEM ; }
As "simple" as this looks, I think there's been problems with it in the past. Have you tested this?
thanks,
greg k-h