Re: [PATCH driver-core 1/1] kobject: reserve an envp slot for the terminator

From: Ruide Cao

Date: Wed Nov 25 2026 - 04:13:38 EST


Hi Greg,

You are right to be suspicious of this one, thanks. v1 was not good
enough: the "- 1" lowers the limit for every uevent from 64 to 63,
including events that never go near the usermode helper. In testing,
a currently valid 64-variable event was rejected (with the "too many
keys" WARN) on v1, on both the helper and the netlink-only path.

The underlying bug is still real: when env->envp[] is completely full
there is no slot left for the NULL terminator, so with
CONFIG_UEVENT_HELPER the helper's envp is not terminated and
kernel_execve() walks past the array (KASAN: strnlen <-
copy_string_kernel <- kernel_execve, then panic).

v2 keeps the 64-variable capacity and instead adds one dedicated
terminator slot (envp[UEVENT_NUM_ENVP + 1]), writes NULL after every
successful add_uevent_var(), and also after zap_modalias_env() drops
MODALIAS. struct kobj_uevent_env is kernel-internal, so the only
visible change is 8 bytes.

Tested on 7.4-rc4 (b6a9fa34ce41) in x86 QEMU, 2 GB RAM, KASAN,
CONFIG_UEVENT_HELPER=y, systemd-udevd 257:
- unpatched: reproducer triggers the KASAN report above
- v2: no crash, no WARN
- exactly 64 variables: helper receives all 64 followed by NULL;
netlink-only delivery unchanged; the 65th variable is rejected
as before
- boot + udevadm trigger/settle, virtio_net unbind/bind: no
"too many keys" in dmesg
- unbind with the helper enabled (MODALIAS removal path): envp
stays NULL-terminated

v2 is here: https://lore.kernel.org/all/39d1eae0d7c8b235f5d4b68d94205b70832ab786.1795506878.git.caoruide123@xxxxxxxxx/

You mentioned this "simple" change caused problems before. I found
7eff2e7a8b65, which reserved a terminator slot, but if you meant
another earlier attempt, could you point me to it so I can make sure
v2 does not repeat it?

thanks,
Ruide

Greg KH <gregkh@xxxxxxxxxxxxxxxxxxx> 于2026年11月23日周一 20:57写道:
>
> On Mon, Nov 23, 2026 at 08:16:37PM +0800, Ren Wei wrote:
> > From: Ruide Cao <caoruide123@xxxxxxxxx>
> >
> > 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.
> >
> > Fixes: 7eff2e7a8b65 ("Driver core: change add_uevent_var to use a struct")
> > Cc: stable@xxxxxxxxxxxxxxx
> > Reported-by: Vega <vega@xxxxxxxxxx>
> > Signed-off-by: Ruide Cao <caoruide123@xxxxxxxxx>
> > Signed-off-by: Ren Wei <weir@xxxxxxxxxx>
> > ---
> > lib/kobject_uevent.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/lib/kobject_uevent.c b/lib/kobject_uevent.c
> > index 1d29378d4f55..f7aa67d6ee54 100644
> > --- a/lib/kobject_uevent.c
> > +++ b/lib/kobject_uevent.c
> > @@ -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