Skip to contentSkip to conversation

usb: gadget: f_mass_storage: fix null pointer dereference in fsg_common_set_num_buffers()

PATCH In review Jeffin Philip 3 participants 8 messages Aug 17

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 usb: gadget: f_mass_storage: fix null pointer dereference in fsg_common_set_num_buffers()

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 [email protected]
Fixes fe5a6c48fd95 ("usb: gadget: storage: get rid of fsg_num_buffers_validate()")
---
 drivers/usb/gadget/function/f_mass_storage.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/usb/gadget/function/f_mass_storage.c +3 -0
index a50743caf083..640d3bcb7bf0 100644
@@ -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;
Signature · 1 line
2.55.0
maintainer
#1 lore raw
Quoted 5 lines · Jeffin Philip

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

#2 lore raw
Quoted 2 lines · Greg KH

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?:

drivers/usb/gadget/function/f_mass_storage.c +1 -1
index 640d3bcb7bf0..98643d53cc7a 100644
@@ -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.

maintainer
#3 lore raw
Quoted 13 lines · Jeffin Philip
Quoted 2 lines · Greg KH

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?:

drivers/usb/gadget/function/f_mass_storage.c +0 -0
index 640d3bcb7bf0..98643d53cc7a 100644

drivers/usb/gadget/function/f_mass_storage.c int fsg_common_set_num_buffers(struct fsg_common *common, unsigned int n) · line 2751

Quoted 6 lines
drivers/usb/gadget/function/f_mass_storage.c +1 -1
@@ -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

#4 lore raw
In reply to Jeffin Philip
Quoted 8 lines · Jeffin Philip

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.

Quoted 5 lines
Reported-by [email protected]
Fixes fe5a6c48fd95 ("usb: gadget: storage: get rid of fsg_num_buffers_validate()")

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

Quoted 22 lines
---
 drivers/usb/gadget/function/f_mass_storage.c | 3 +++
 1 file changed, 3 insertions(+)
drivers/usb/gadget/function/f_mass_storage.c +3 -0
index a50743caf083..640d3bcb7bf0 100644
@@ -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;
Signature · 1 line
2.55.0
#5 lore raw

PATCH 1/1 usb: gadget: f_mass_storage: fix null pointer dereference in fsg_common_set_num_buffers()

Quoted 3 lines · Alan Stern

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.

Kconfig currently sets the limit from 2 to 256 with default as 2. However, we use that only at build time. So, if we enter during runtime via configfs, we set page content to 0\0 and get the null pointer dereference. So we will need to add the check for num < 2 in fsg_opts_num_buffers_store() and return EINVAL? Upper bound is 256 which is below what kstrtou8() can return anyway.

Thanks,
Jeffin.

#6 lore raw

PATCH 1/1 usb: gadget: f_mass_storage: fix null pointer dereference in fsg_common_set_num_buffers()

Quoted 5 lines · Jeffin Philip

Kconfig currently sets the limit from 2 to 256 with default as 2. However, we use that only at build time. So, if we enter during runtime via configfs, we set page content to 0\0 and get the null pointer dereference. So we will need to add the check for num < 2 in fsg_opts_num_buffers_store() and return EINVAL? Upper bound is 256 which is below what kstrtou8() can return anyway.

Correction on the last sentence: Upper bound is 256 which is _above_ what kstrtou8() can return anyway.

#7 lore raw
Quoted 6 lines · Jeffin Philip
Quoted 5 lines · Jeffin Philip

Kconfig currently sets the limit from 2 to 256 with default as 2. However, we use that only at build time. So, if we enter during runtime via configfs, we set page content to 0\0 and get the null pointer dereference. So we will need to add the check for num < 2 in fsg_opts_num_buffers_store() and return EINVAL?

Or put the check in fsg_common_set_num_buffers(), which gets called on all the pathways.

Alan Stern

Quoted 4 lines

Upper bound is 256 which is below what kstrtou8() can return anyway.

Correction on the last sentence: Upper bound is 256 which is _above_ what kstrtou8() can return anyway.