usb: gadget: f_mass_storage: fix null pointer dereference in fsg_common_set_num_buffers()
Subsystems (2)
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.
--- drivers/usb/gadget/function/f_mass_storage.c | 3 +++ 1 file changed, 3 insertions(+)
drivers/usb/gadget/function/f_mass_storage.c +3 -0
@@ -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
Greg KH
maintainerQuoted 5 lines · Jeffin Philip
In
fsg_common_set_num_buffers(), n can be 0 as conversion to u8 usingkstrtou8()infsg_opts_num_buffers_store()can return values from _0_ to 255. When passing 0 as "n" value tokzalloc_objs, it can return aZERO_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
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
@@ -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.
Greg KH
maintainerQuoted 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_PTRstoo, something like this should suffice?:drivers/usb/gadget/function/f_mass_storage.c +0
-0
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
Quoted 8 lines · Jeffin Philip
In
fsg_common_set_num_buffers(), n can be 0 as conversion to u8 usingkstrtou8()infsg_opts_num_buffers_store()can return values from _0_ to 255. When passing 0 as "n" value tokzalloc_objs, it can return aZERO_SIZE_PTR, which passes the null check for buffhds. This leads to a null pointer dereference later inbh->nextin the do while loop. Fix this by adding a check for n = 0 case and returning-EINVALif n is 0.
Quoted 5 lines
Reported-by [email protected]Fixes fe5a6c48fd95 ("usb: gadget: storage: get rid of fsg_num_buffers_validate()")Signed-off-by Jeffin Philip <[email protected]>
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@@ -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
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 offsg_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.
PATCH 1/1 usb: gadget: f_mass_storage: fix null pointer dereference in fsg_common_set_num_buffers()
Quoted 5 lines · Jeffin Philip
Kconfigcurrently 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 infsg_opts_num_buffers_store()and return EINVAL? Upper bound is 256 which is below whatkstrtou8()can return anyway.
Correction on the last sentence: Upper bound is 256 which is _above_ what kstrtou8() can return anyway.
Quoted 6 lines · Jeffin Philip
Quoted 5 lines · Jeffin Philip
Kconfigcurrently 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 infsg_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.