usbip: usbip_host: fix null pointer dereference in
Subsystems (3)
PATCH v2 1/1 usbip: usbip_host: fix null pointer dereference in
rebind_store calls do_rebind which dereferences udev without checking if it is NULL. If busid is registered using match_busid but the device is never bound to the driver or it is never present in the first place, it triggers a null pointer dereference when we attempt to rebind the device. Fix this by checking explicitly for udev first and returning -ENODEV if udev is NULL. Add usb_get_dev in addition to the null check to get a reference to udev to prevent udev from becoming NULL after the check. Drop the reference after using it in do_rebind().
---
Changes in v2:
- Addressed concerns raised by the maintainer in v1 discussion
- Added usb_get_dev() to get a reference to udev preventing it from becoming null after the null check. Drop the reference after using it in do_rebind()
--- drivers/usb/usbip/stub_main.c | 9 +++++++++ 1 file changed, 9 insertions(+)
drivers/usb/usbip/stub_main.c +9 -0
@@ -256,12 +256,21 @@ static ssize_t rebind_store(struct device_driver *dev, const char *buf,if (!bid)return -ENODEV ; +if (!bid->udev) { +put_busid_priv (bid); +return -ENODEV ; + } + +/* get a reference to udev to prevent it from becoming NULL */ +usb_get_dev (bid->udev);/* mark the device for deletion so probe ignores it during rescan */ bid->status =STUB_BUSID_OTHER ;/* release the busid lock */ put_busid_priv (bid); ret =do_rebind ((char *) buf, bid); +/* we finished using udev and don't need the reference anymore, drop it */ +usb_put_dev (bid->udev);if (ret <0 )return ret;
Signature · 1 line
2.55.0
Greg KH
maintainerQuoted 30 lines · Jeffin Philip
rebind_storecallsdo_rebindwhich dereferences udev without checking if it is NULL. If busid is registered usingmatch_busidbut the device is never bound to the driver or it is never present in the first place, it triggers a null pointer dereference when we attempt to rebind the device. Fix this by checking explicitly for udev first and returning-ENODEVif udev is NULL. Addusb_get_devin addition to the null check to get a reference to udev to prevent udev from becoming NULL after the check. Drop the reference after using it indo_rebind().Reported-by [email protected]Fixes 4bfb141bc013 ("usbip: usbip_host: fix to hold parent lock for device_attach() calls")Signed-off-by Jeffin Philip <[email protected]>---
Changes in v2:
- Addressed concerns raised by the maintainer in v1 discussion
- Addedusb_get_dev()to get a reference to udev preventing it from becoming null after the null check. Drop the reference after using it indo_rebind()--- drivers/usb/usbip/stub_main.c | 9 +++++++++ 1 file changed, 9 insertions(+)drivers/usb/usbip/stub_main.c +0
-0
drivers/usb/usbip/stub_main.c static ssize_t rebind_store(struct device_driver *dev, const char *buf, · line 265
Quoted 10 lines
drivers/usb/usbip/stub_main.c +7
-0@@ -256,12 +256,21 @@ static ssize_t rebind_store(struct device_driver *dev, const char *buf,if (!bid)return -ENODEV ; +if (!bid->udev) { +put_busid_priv (bid); +return -ENODEV ; + } + +/* get a reference to udev to prevent it from becoming NULL */ +usb_get_dev (bid->udev);
So what happens if udev becomes NULL after checking it and before grabbing the reference?
This is not how to handle this at all. Please step back and look at the root problem here and address that. This is just papering over the issue.
thanks,
greg k-h
PATCH v2 1/1 usbip: usbip_host: fix null pointer dereference in
Quoted 34 lines · Greg KH
Quoted 33 lines · Jeffin Philip
rebind_storecallsdo_rebindwhich dereferences udev without checking if it is NULL. If busid is registered usingmatch_busidbut the device is never bound to the driver or it is never present in the first place, it triggers a null pointer dereference when we attempt to rebind the device. Fix this by checking explicitly for udev first and returning-ENODEVif udev is NULL. Addusb_get_devin addition to the null check to get a reference to udev to prevent udev from becoming NULL after the check. Drop the reference after using it indo_rebind().Reported-by [email protected]Fixes 4bfb141bc013 ("usbip: usbip_host: fix to hold parent lock for device_attach() calls")Signed-off-by Jeffin Philip <[email protected]>---
Changes in v2:
- Addressed concerns raised by the maintainer in v1 discussion
- Addedusb_get_dev()to get a reference to udev preventing it from becoming null after the null check. Drop the reference after using it indo_rebind()--- drivers/usb/usbip/stub_main.c | 9 +++++++++ 1 file changed, 9 insertions(+)drivers/usb/usbip/stub_main.c +0
-0@@ -256,12 +256,21 @@ static ssize_t rebind_store(struct device_driver *dev, const char *buf,if (!bid)return -ENODEV ;
Quoted 10 lines
drivers/usb/usbip/stub_main.c static ssize_t rebind_store(struct device_driver *dev, const char *buf, · line 265
Quoted 7 lines
drivers/usb/usbip/stub_main.c +7
-0+if (!bid->udev) { +put_busid_priv (bid); +return -ENODEV ; + } + +/* get a reference to udev to prevent it from becoming NULL */ +usb_get_dev (bid->udev);So what happens if udev becomes NULL after checking it and before grabbing the reference?
I looked through the driver and could not find where udev becomes NULL after checking and before getting the reference since we do both of those under busid lock. There is a small window between releasing the lock and do_rebind, is that what what you are referring to? If so or otherwise, could you please advise me on how to move forward in correcting this patch?
Thanks,
Jeffin.
Greg KH
maintainerQuoted 46 lines · Jeffin Philip
Quoted 34 lines · Greg KH
Quoted 33 lines · Jeffin Philip
rebind_storecallsdo_rebindwhich dereferences udev without checking if it is NULL. If busid is registered usingmatch_busidbut the device is never bound to the driver or it is never present in the first place, it triggers a null pointer dereference when we attempt to rebind the device. Fix this by checking explicitly for udev first and returning-ENODEVif udev is NULL. Addusb_get_devin addition to the null check to get a reference to udev to prevent udev from becoming NULL after the check. Drop the reference after using it indo_rebind().Reported-by [email protected]Fixes 4bfb141bc013 ("usbip: usbip_host: fix to hold parent lock for device_attach() calls")Signed-off-by Jeffin Philip <[email protected]>---
Changes in v2:
- Addressed concerns raised by the maintainer in v1 discussion
- Addedusb_get_dev()to get a reference to udev preventing it from becoming null after the null check. Drop the reference after using it indo_rebind()--- drivers/usb/usbip/stub_main.c | 9 +++++++++ 1 file changed, 9 insertions(+)drivers/usb/usbip/stub_main.c +0
-0@@ -256,12 +256,21 @@ static ssize_t rebind_store(struct device_driver *dev, const char *buf,if (!bid)return -ENODEV ;Quoted 10 lines
drivers/usb/usbip/stub_main.c static ssize_t rebind_store(struct device_driver *dev, const char *buf, · line 265
Quoted 7 lines
drivers/usb/usbip/stub_main.c +7
-0+if (!bid->udev) { +put_busid_priv (bid); +return -ENODEV ; + } + +/* get a reference to udev to prevent it from becoming NULL */ +usb_get_dev (bid->udev);So what happens if udev becomes NULL after checking it and before grabbing the reference?
Quoted 5 lines
I looked through the driver and could not find where udev becomes NULL after checking and before getting the reference since we do both of those under busid lock. There is a small window between releasing the lock and
do_rebind, is that what what you are referring to? If so or otherwise, could you please advise me on how to move forward in correcting this patch?
If you do not hold a lock when testing and doing something based on a field, it will race and is broken.
Again, step back and try to determine what you are trying to fix here, and how that can be done in a race-free way. If you don't know, that's fine too, I sure don't! :)
But we can't take a change that doesn't actually fix the issue, you wouldn't want that, right?
Why are you looking at this issue anyway? Did someone assign it to you?
thanks,
greg k-h
Quoted 53 lines · Greg KH
Quoted 46 lines · Jeffin Philip
Quoted 34 lines · Greg KH
Quoted 33 lines · Jeffin Philip
rebind_storecallsdo_rebindwhich dereferences udev without checking if it is NULL. If busid is registered usingmatch_busidbut the device is never bound to the driver or it is never present in the first place, it triggers a null pointer dereference when we attempt to rebind the device. Fix this by checking explicitly for udev first and returning-ENODEVif udev is NULL. Addusb_get_devin addition to the null check to get a reference to udev to prevent udev from becoming NULL after the check. Drop the reference after using it indo_rebind().Reported-by [email protected]Fixes 4bfb141bc013 ("usbip: usbip_host: fix to hold parent lock for device_attach() calls")Signed-off-by Jeffin Philip <[email protected]>---
Changes in v2:
- Addressed concerns raised by the maintainer in v1 discussion
- Addedusb_get_dev()to get a reference to udev preventing it from becoming null after the null check. Drop the reference after using it indo_rebind()--- drivers/usb/usbip/stub_main.c | 9 +++++++++ 1 file changed, 9 insertions(+)drivers/usb/usbip/stub_main.c +0
-0@@ -256,12 +256,21 @@ static ssize_t rebind_store(struct device_driver *dev, const char *buf,if (!bid)return -ENODEV ;Quoted 10 lines
drivers/usb/usbip/stub_main.c static ssize_t rebind_store(struct device_driver *dev, const char *buf, · line 265
Quoted 7 lines
drivers/usb/usbip/stub_main.c +7
-0+if (!bid->udev) { +put_busid_priv (bid); +return -ENODEV ; + } + +/* get a reference to udev to prevent it from becoming NULL */ +usb_get_dev (bid->udev);So what happens if udev becomes NULL after checking it and before grabbing the reference?
Quoted 5 lines
I looked through the driver and could not find where udev becomes NULL after checking and before getting the reference since we do both of those under busid lock. There is a small window between releasing the lock and
do_rebind, is that what what you are referring to? If so or otherwise, could you please advise me on how to move forward in correcting this patch?
Quoted 6 lines
If you do not hold a lock when testing and doing something based on a field, it will race and is broken.
Again, step back and try to determine what you are trying to fix here, and how that can be done in a race-free way. If you don't know, that's fine too, I sure don't! :)
We could add a new flag(in bus_id_priv->status) that would prevent probe from scanning or any other function from nullifying udev and that would prevent race conditions. But I think that would be an unnecessarily large change just for a small issue like null udev. We could also use a local variable to store the udev so it does not become null after dropping locks. Apart from these, I don't have other ideas.
Quoted 2 lines
But we can't take a change that doesn't actually fix the issue, you wouldn't want that, right?
Yes, I don't want to send bad patches either.
Quoted 1 line
Why are you looking at this issue anyway? Did someone assign it to you?
I am relatively new to usb subsystem and have been wanting to learn it for quite some time. So I got on syzbot where I found this issue, I wanted to learn the subsystem in practice, that's all. Nobody assigned me this.
Thanks,
Jeffin.
PATCH v2 1/1 usbip: usbip_host: fix null pointer dereference in
In reply to Greg KHQuoted 6 lines · Greg KH
If you do not hold a lock when testing and doing something based on a field, it will race and is broken.
Again, step back and try to determine what you are trying to fix here, and how that can be done in a race-free way. If you don't know, that's fine too, I sure don't! :)
I read the whole driver, and udev does not seem to be nulled anywhere except in stub_probe(), where it is only assigned. So, we can safely check for null udev alone. The driver itself seems to hold itself together as currently I don't see a potential race. probe() also cannot run as we set our status to STUB_BUSID_OTHER under lock so probe would ignore that too. Am I missing something else here that could create a potential race? Please advise.
Thanks,
Jeffin.