Re: [PATCH] device_core: check null pointer
From: Zhengqiao Xia
Date: Sun Nov 16 2025 - 21:56:08 EST
Hi Greg,
Based on my analysis,
usb_new_device -> device_add -> device_private_init
->
bus_probe_device -> usb_generic_driver_probe ->
usb_set_configuration -> "add each interface" ->
device_add(&intf->dev);
--> if of_node status is disabled
-> of_device_is_available is false -> not running device_add ->
interface dev->p is null
usbdev_ioctl -> proc_ioctl -> device_attach -> __device_attach ->
device_attach -> if (dev->p->dead)
Since dev->p has not been created at this time, dev->p is NULL, So I
think we should add a check here.
Here is my current analysis, but there may be some misunderstandings
or mistakes in it. Could you please help clarify my confusion?
thanks
Greg KH <gregkh@xxxxxxxxxxxxxxxxxxx> 于2025年11月16日周日 04:16写道:
>
> On Fri, Nov 14, 2025 at 10:18:21PM +0800, Zhengqiao Xia wrote:
> > Add null pointer check to avoid null pointer.
> > When the USB device's interface is disabled, 'device_add' will not
> > be called, and 'dev->p' will be NULL.
>
> Wait, how does that happen? Shouldn't we prevent that?
>
> > When you use 'usbdev_ioctl' to
> > call this USB interface at this point,'__device_attach' will be invoked.
> > Then a null pointer will be generated.
> >
> > Signed-off-by: Zhengqiao Xia <xiazhengqiao@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx>
>
> Not a valid email address :(
>
> > ---
> > drivers/base/dd.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/drivers/base/dd.c b/drivers/base/dd.c
> > index 3328101e0e106..cfdeb420fd12a 100644
> > --- a/drivers/base/dd.c
> > +++ b/drivers/base/dd.c
> > @@ -1033,7 +1033,7 @@ static int __device_attach(struct device *dev, bool allow_async)
> > bool async = false;
> >
> > device_lock(dev);
> > - if (dev->p->dead) {
> > + if (dev->p && dev->p->dead) {
>
> If dev->p is NULL here, something else went wrong with the caller,
> please, let's fix the root problem here instead.
>
> What suddenly changed to cause this to happen?
>
> thanks,
>
> greg k-h