Linux USB
 help / color / mirror / Atom feed
* [PATCH] usb: gadget: f_mass_storage: fix null pointer dereference in fsg_common_set_num_buffers()
@ 2026-08-17 11:08 Jeffin Philip
  2026-08-17 11:10 ` Greg KH
  2026-08-17 14:01 ` Alan Stern
  0 siblings, 2 replies; 5+ messages in thread
From: Jeffin Philip @ 2026-08-17 11:08 UTC (permalink / raw)
  To: gregkh, felipe.balbi
  Cc: christophe.jaillet, kees, linux-usb, linux-kernel,
	syzbot+791be35f1fbcc85d06d7, Jeffin Philip, stable

In fsg_common_set_num_buffers(), n can be 0 as conversion to u8
using kstrtou8() in fsg_opts_num_buffers_store() can return
values from _0_ to 255. When passing 0 as "n" value to kzalloc_objs,
it can return a ZERO_SIZE_PTR, which passes the null check for
buffhds. This leads to a null pointer dereference later in bh->next
in the do while loop. Fix this by adding a check for n = 0 case and
returning -EINVAL if n is 0.

Reported-by: syzbot+791be35f1fbcc85d06d7@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=791be35f1fbcc85d06d7
Fixes: fe5a6c48fd95 ("usb: gadget: storage: get rid of fsg_num_buffers_validate()")
Cc: stable@vger.kernel.org
Signed-off-by: Jeffin Philip <jeffinphilip14@gmail.com>
---
 drivers/usb/gadget/function/f_mass_storage.c | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/drivers/usb/gadget/function/f_mass_storage.c b/drivers/usb/gadget/function/f_mass_storage.c
index a50743caf083..640d3bcb7bf0 100644
--- a/drivers/usb/gadget/function/f_mass_storage.c
+++ b/drivers/usb/gadget/function/f_mass_storage.c
@@ -2747,6 +2747,9 @@ int fsg_common_set_num_buffers(struct fsg_common *common, unsigned int n)
 	struct fsg_buffhd *bh, *buffhds;
 	int i;
 
+	if (!n)
+		return -EINVAL;
+
 	buffhds = kzalloc_objs(*buffhds, n);
 	if (!buffhds)
 		return -ENOMEM;
-- 
2.55.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] usb: gadget: f_mass_storage: fix null pointer dereference in fsg_common_set_num_buffers()
  2026-08-17 11:08 [PATCH] usb: gadget: f_mass_storage: fix null pointer dereference in fsg_common_set_num_buffers() Jeffin Philip
@ 2026-08-17 11:10 ` Greg KH
  2026-08-17 13:02   ` Jeffin Philip
  2026-08-17 14:01 ` Alan Stern
  1 sibling, 1 reply; 5+ messages in thread
From: Greg KH @ 2026-08-17 11:10 UTC (permalink / raw)
  To: Jeffin Philip
  Cc: felipe.balbi, christophe.jaillet, kees, linux-usb, linux-kernel,
	syzbot+791be35f1fbcc85d06d7, stable

On Mon, Aug 17, 2026 at 04:38:39PM +0530, Jeffin Philip wrote:
> In fsg_common_set_num_buffers(), n can be 0 as conversion to u8
> using kstrtou8() in fsg_opts_num_buffers_store() can return
> values from _0_ to 255. When passing 0 as "n" value to kzalloc_objs,
> it can return a ZERO_SIZE_PTR, which passes the null check for
> buffhds.

But that is probably a bad idea, right?  Shouldn't we fix that error
first?

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] usb: gadget: f_mass_storage: fix null pointer dereference in fsg_common_set_num_buffers()
  2026-08-17 11:10 ` Greg KH
@ 2026-08-17 13:02   ` Jeffin Philip
  2026-08-17 13:15     ` Greg KH
  0 siblings, 1 reply; 5+ messages in thread
From: Jeffin Philip @ 2026-08-17 13:02 UTC (permalink / raw)
  To: gregkh
  Cc: christophe.jaillet, felipe.balbi, jeffinphilip14, kees,
	linux-kernel, linux-usb, stable, syzbot+791be35f1fbcc85d06d7

On Mon, 17 Aug 2026 13:10:27 +0200, Greg KH wrote:
>But that is probably a bad idea, right?  Shouldn't we fix that error
>first?

Thanks for the review. Don't quite understand what I need to fix here
(respectfully), should we harden the buffhds null check to include 
ZERO_SIZE_PTRs too, something like this should suffice?:

diff --git a/drivers/usb/gadget/function/f_mass_storage.c b/drivers/usb/gadget/function/f_mass_storage.c
index 640d3bcb7bf0..98643d53cc7a 100644
--- a/drivers/usb/gadget/function/f_mass_storage.c
+++ b/drivers/usb/gadget/function/f_mass_storage.c
@@ -2747,11 +2747,11 @@ int fsg_common_set_num_buffers(struct fsg_common *common, unsigned int n)
 	struct fsg_buffhd *bh, *buffhds;
 	int i;
  
 	buffhds = kzalloc_objs(*buffhds, n);
-	if (!buffhds)
+	if (ZERO_OR_NULL_PTR(buffhds))
 		return -ENOMEM;
 
 	/* Data buffers cyclic list */

Thanks,
Jeffin.

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] usb: gadget: f_mass_storage: fix null pointer dereference in fsg_common_set_num_buffers()
  2026-08-17 13:02   ` Jeffin Philip
@ 2026-08-17 13:15     ` Greg KH
  0 siblings, 0 replies; 5+ messages in thread
From: Greg KH @ 2026-08-17 13:15 UTC (permalink / raw)
  To: Jeffin Philip
  Cc: christophe.jaillet, felipe.balbi, kees, linux-kernel, linux-usb,
	stable, syzbot+791be35f1fbcc85d06d7

On Mon, Aug 17, 2026 at 06:32:08PM +0530, Jeffin Philip wrote:
> On Mon, 17 Aug 2026 13:10:27 +0200, Greg KH wrote:
> >But that is probably a bad idea, right?  Shouldn't we fix that error
> >first?
> 
> Thanks for the review. Don't quite understand what I need to fix here
> (respectfully), should we harden the buffhds null check to include 
> ZERO_SIZE_PTRs too, something like this should suffice?:
> 
> diff --git a/drivers/usb/gadget/function/f_mass_storage.c b/drivers/usb/gadget/function/f_mass_storage.c
> index 640d3bcb7bf0..98643d53cc7a 100644
> --- a/drivers/usb/gadget/function/f_mass_storage.c
> +++ b/drivers/usb/gadget/function/f_mass_storage.c
> @@ -2747,11 +2747,11 @@ int fsg_common_set_num_buffers(struct fsg_common *common, unsigned int n)
>  	struct fsg_buffhd *bh, *buffhds;
>  	int i;
>   
>  	buffhds = kzalloc_objs(*buffhds, n);
> -	if (!buffhds)
> +	if (ZERO_OR_NULL_PTR(buffhds))

That doesn't look right, think about what your previous commit said it
was doing and why I thought that was a bad idea.

Was it created by a LLM?

thanks,

greg k-h

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] usb: gadget: f_mass_storage: fix null pointer dereference in fsg_common_set_num_buffers()
  2026-08-17 11:08 [PATCH] usb: gadget: f_mass_storage: fix null pointer dereference in fsg_common_set_num_buffers() Jeffin Philip
  2026-08-17 11:10 ` Greg KH
@ 2026-08-17 14:01 ` Alan Stern
  1 sibling, 0 replies; 5+ messages in thread
From: Alan Stern @ 2026-08-17 14:01 UTC (permalink / raw)
  To: Jeffin Philip
  Cc: gregkh, felipe.balbi, christophe.jaillet, kees, linux-usb,
	linux-kernel, syzbot+791be35f1fbcc85d06d7, stable

On Mon, Aug 17, 2026 at 04:38:39PM +0530, Jeffin Philip wrote:
> In fsg_common_set_num_buffers(), n can be 0 as conversion to u8
> using kstrtou8() in fsg_opts_num_buffers_store() can return
> values from _0_ to 255. When passing 0 as "n" value to kzalloc_objs,
> it can return a ZERO_SIZE_PTR, which passes the null check for
> buffhds. This leads to a null pointer dereference later in bh->next
> in the do while loop. Fix this by adding a check for n = 0 case and
> returning -EINVAL if n is 0.
> 
> Reported-by: syzbot+791be35f1fbcc85d06d7@syzkaller.appspotmail.com
> Closes: https://syzkaller.appspot.com/bug?extid=791be35f1fbcc85d06d7
> Fixes: fe5a6c48fd95 ("usb: gadget: storage: get rid of fsg_num_buffers_validate()")
> Cc: stable@vger.kernel.org
> Signed-off-by: Jeffin Philip <jeffinphilip14@gmail.com>

In fe5a6c48fd95 ("usb: gadget: storage: get rid of 
fsg_num_buffers_validate()"), the code that was changed originally 
required the number to lie between 2 and 32.  Even 1 was not acceptable.

Alan Stern

> ---
>  drivers/usb/gadget/function/f_mass_storage.c | 3 +++
>  1 file changed, 3 insertions(+)
> 
> diff --git a/drivers/usb/gadget/function/f_mass_storage.c b/drivers/usb/gadget/function/f_mass_storage.c
> index a50743caf083..640d3bcb7bf0 100644
> --- a/drivers/usb/gadget/function/f_mass_storage.c
> +++ b/drivers/usb/gadget/function/f_mass_storage.c
> @@ -2747,6 +2747,9 @@ int fsg_common_set_num_buffers(struct fsg_common *common, unsigned int n)
>  	struct fsg_buffhd *bh, *buffhds;
>  	int i;
>  
> +	if (!n)
> +		return -EINVAL;
> +
>  	buffhds = kzalloc_objs(*buffhds, n);
>  	if (!buffhds)
>  		return -ENOMEM;
> -- 
> 2.55.0
> 
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2026-08-17 14:01 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-17 11:08 [PATCH] usb: gadget: f_mass_storage: fix null pointer dereference in fsg_common_set_num_buffers() Jeffin Philip
2026-08-17 11:10 ` Greg KH
2026-08-17 13:02   ` Jeffin Philip
2026-08-17 13:15     ` Greg KH
2026-08-17 14:01 ` Alan Stern

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox