Re: [PATCH] USB: gadget: fix NULL pointer dereference in gadget_dev_ioctl()

From: Alan Stern

Date: Tue Aug 25 2026 - 11:43:00 EST


On Tue, Aug 25, 2026 at 07:59:43PM +0530, Lovekesh Solanki wrote:
> Thanks for the review,
>
> On Tue, Aug 25, 2026 at 09:14:37AM -0400, Alan Stern wrote:
> > Why does it matter that you read dev->gadget before the state check
> > rather than after? If it doesn't matter, there's no reason to mention
> > it in the patch description.
> The order of reading it doesn't matter. The important part is to read it
> while holding the lock, perhaps the wording is unclear, I'll reword it
> in v2.
>
> > Also, why does it matter that gadgetfs_bind() writes dev->gadget without
> > holding the lock? Again, the description shouldn't mention things that
> > don't matter.
> Because ioctl can get a stale dev->gadget before dev->lock, while
> dev->state is checked after acquiring the lock, which is the cause.

But those facts would remain true even if gadgetfs_bind() were to write
dev->gadget while holding the lock, wouldn't they? So the fact that the
lock isn't held during the write makes no difference to your patch.

> Is the reference to gadgetfs_bind() unncessary? Or this part of the
> explanation is irrelvant?

Yes, it is irrelevant, for the reason just explained.

Alan Stern

> > Why did you add this test for gadget being non-NULL? Is there any way
> > it could possibly be NULL at this point?
> It seems it doesn't matter since if read is correct it can't be NULL, it
> was an initial attempt to fix but its unnecessary now, I'll remove that
> as well.
>
> Regards,
> Lovekesh