Re: [PATCH 1/2] reboot: fix platform sys_off handler reuse with NULL cb_data
From: Andrew Morton
Date: Thu Dec 03 2026 - 00:54:03 EST
On Wed, 2 Dec 2026 21:50:35 -0800 Andrew Morton <akpm@xxxxxxxxxxxxxxxxxxxx> wrote:
> On Thu, 26 Nov 2026 21:47:53 +0000 Bradley Morgan <bradley.morgan@xxxxxxxxx> wrote:
>
> > In alloc_sys_off_handler(), the check for whether the static
> > platform_sys_off_handler is already in use checks handler->cb_data,
> > which is a weird assumption since cb_data is optional and callers can
> > just pass NULL. If a caller registers at SYS_OFF_PRIO_PLATFORM with
> > NULL cb_data, a second SYS_OFF_PRIO_PLATFORM registration gets the same
> > struct back, overwrites its fields, and links the same notifier_block
> > into another chain while it is still on the first one. For example, on
> > xtensa ISS platform_setup() registers iss_restart and iss_power_off
> > this way, so restarting calls iss_power_off() instead and splices the
> > two lists together if any lower priority poweroff handler is around.
> >
> > To fix this, resolve the mode and list first, keep one static handler
> > per mode in platform_sys_off_handlers[], and check handler->list instead
> > of handler->cb_data so a NULL cb_data cannot slip past -EBUSY.
> >
> > ...
> >
> > --- a/kernel/reboot.c
> > +++ b/kernel/reboot.c
> > @@ -394,7 +396,9 @@ static struct sys_off_handler *alloc_sys_off_handler(int priority)
> >
> > static void free_sys_off_handler(struct sys_off_handler *handler)
> > {
> > - if (handler == &platform_sys_off_handler)
> > + if (handler >= platform_sys_off_handlers &&
> > + handler < platform_sys_off_handlers +
> > + ARRAY_SIZE(platform_sys_off_handlers))
>
> But `handler' can be allocated with kzalloc_obj(), so comparing that
> pointer with the statically allocated array members isn't meaningful?
>
Well. The comparison is officially undefined in C. But I can see what
you're doing there and I'm sure it's OK in kernel ;) But can the
comparisons easily be avoided? Or at least commented.