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 2 participants 2 messages Aug 18

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

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

Previously fsg_num_buffers_validate() was removed as it was not necessary due to Kconfig setting the limits for n from 2 to 256 with default as 2. However, setting the page content in such a way that kstrtou8() reflects n value as either 0 or 1 bypasses these restrictions leading to a null pointer dereference if n is 0. Fix this by adding a check for n < 2 and returning -EINVAL if n is either 0 or 1 consistent with Kconfig logic.

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

---
Changes in v2:
- Change check from n = 0 to n < 2 consistent with Kconfig logic

---
 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..4e743be220cc 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 < 2)
+		return -EINVAL;
+
 	buffhds = kzalloc_objs(*buffhds, n);
 	if (!buffhds)
 		return -ENOMEM;
Signature · 1 line
2.55.0
Acked-by on v2 1/1
#1 lore raw
Quoted 8 lines · Jeffin Philip

Previously fsg_num_buffers_validate() was removed as it was not necessary due to Kconfig setting the limits for n from 2 to 256 with default as 2. However, setting the page content in such a way that kstrtou8() reflects n value as either 0 or 1 bypasses these restrictions leading to a null pointer dereference if n is 0. Fix this by adding a check for n < 2 and returning -EINVAL if n is either 0 or 1 consistent with Kconfig logic.

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

---
Changes in v2:
- Change check from n = 0 to n < 2 consistent with Kconfig logic

---
 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..4e743be220cc 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 < 2)
+		return -EINVAL;
+
 	buffhds = kzalloc_objs(*buffhds, n);
 	if (!buffhds)
 		return -ENOMEM;
Signature · 1 line
2.55.0