media: em28xx: fix corrupted list
Subsystems (2)
PATCH 1/1 media: em28xx: fix corrupted list
Syzbot reported corrupted list in em28xx driver. The problem was in non-reinitialized lists on disconnect. Since all 2 lists are global variables and driver can be connected and disconnected many times we should call INIT_LIST_HEAD() in .disconnect method to prevent corrupted list entries.
--- drivers/media/usb/em28xx/em28xx-cards.c | 2 ++ drivers/media/usb/em28xx/em28xx-core.c | 6 ++++++ drivers/media/usb/em28xx/em28xx.h | 1 + 3 files changed, 9 insertions(+)
drivers/media/usb/em28xx/em28xx-cards.c +2 -0
@@ -4148,6 +4148,8 @@ static void em28xx_usb_disconnect(struct usb_interface *intf) dev->dev_next =NULL ; }kref_put (&dev->ref, em28xx_free_device); + +em28xx_reset_lists (); } static int em28xx_usb_suspend(struct usb_interface *intf,
drivers/media/usb/em28xx/em28xx-core.c +6 -0
@@ -1131,6 +1131,12 @@ void em28xx_init_extension(struct em28xx *dev)mutex_unlock (&em28xx_devlist_mutex); } +void em28xx_reset_lists (void ) +{ +INIT_LIST_HEAD (&em28xx_devlist); +INIT_LIST_HEAD (&em28xx_extension_devlist); +} +void em28xx_close_extension (struct em28xx *dev) {const struct em28xx_ops *ops =NULL ;
drivers/media/usb/em28xx/em28xx.h +1 -0
@@ -835,6 +835,7 @@ void em28xx_stop_urbs(struct em28xx *dev);int em28xx_set_mode (struct em28xx *dev,enum em28xx_mode set_mode);int em28xx_gpio_set (struct em28xx *dev,const struct em28xx_reg_seq *gpio);int em28xx_register_extension (struct em28xx_ops *dev); +void em28xx_reset_lists (void );void em28xx_unregister_extension (struct em28xx_ops *dev);void em28xx_init_extension (struct em28xx *dev);void em28xx_close_extension (struct em28xx *dev);
Signature · 1 line
2.32.0
Quoted 33 lines · Pavel Skripkin
Syzbot reported corrupted list in em28xx driver. The problem was in non-reinitialized lists on disconnect. Since all 2 lists are global variables and driver can be connected and disconnected many times we should call
INIT_LIST_HEAD()in .disconnect method to prevent corrupted list entries.Fixes 1a23f81b7dc3 ("V4L/DVB (9979): em28xx: move usb probe code to a proper place")Reported-by [email protected]Signed-off-by Pavel Skripkin <[email protected]>--- drivers/media/usb/em28xx/em28xx-cards.c | 2 ++ drivers/media/usb/em28xx/em28xx-core.c | 6 ++++++ drivers/media/usb/em28xx/em28xx.h | 1 + 3 files changed, 9 insertions(+)drivers/media/usb/em28xx/em28xx-cards.c +2
-0@@ -4148,6 +4148,8 @@ static void em28xx_usb_disconnect(struct usb_interface *intf) dev->dev_next =NULL ; }kref_put (&dev->ref, em28xx_free_device); + +em28xx_reset_lists (); } static int em28xx_usb_suspend(struct usb_interface *intf,drivers/media/usb/em28xx/em28xx-core.c +0
-0
drivers/media/usb/em28xx/em28xx-core.c void em28xx_init_extension(struct em28xx *dev) · line 1137
Quoted 7 lines
drivers/media/usb/em28xx/em28xx-core.c +4
-0@@ -1131,6 +1131,12 @@ void em28xx_init_extension(struct em28xx *dev)mutex_unlock (&em28xx_devlist_mutex); } +void em28xx_reset_lists (void ) +{ +INIT_LIST_HEAD (&em28xx_devlist); +INIT_LIST_HEAD (&em28xx_extension_devlist);
This needs a mutex_lock(&em28xx_devlist_mutex);
But actually, I don't think this is right: if there are multiple em28xx devices, then I think if you disconnect one, then the other is - with this code - also removed from the list.
Can you give a link to the actual syzbot bug? I'm not at all sure you are fixing the right thing here.
Regards,
Hans
Quoted 18 lines
drivers/media/usb/em28xx/em28xx-core.c +2
-0+} +void em28xx_close_extension (struct em28xx *dev) {const struct em28xx_ops *ops =NULL ;drivers/media/usb/em28xx/em28xx.h +1
-0@@ -835,6 +835,7 @@ void em28xx_stop_urbs(struct em28xx *dev);int em28xx_set_mode (struct em28xx *dev,enum em28xx_mode set_mode);int em28xx_gpio_set (struct em28xx *dev,const struct em28xx_reg_seq *gpio);int em28xx_register_extension (struct em28xx_ops *dev); +void em28xx_reset_lists (void );void em28xx_unregister_extension (struct em28xx_ops *dev);void em28xx_init_extension (struct em28xx *dev);void em28xx_close_extension (struct em28xx *dev);
Quoted 44 lines · On Tue, 20 Jul 2021 14:13:13 +0200 Hans Verkuil <[email protected]> wrote:
Quoted 36 lines · Pavel Skripkin
Syzbot reported corrupted list in em28xx driver. The problem was in non-reinitialized lists on disconnect. Since all 2 lists are global variables and driver can be connected and disconnected many times we should call
INIT_LIST_HEAD()in .disconnect method to prevent corrupted list entries.Fixes 1a23f81b7dc3 ("V4L/DVB (9979): em28xx: move usb probe codeto a proper place") Reported-by:
[email protected]Signed-off-by Pavel Skripkin <[email protected]> ---drivers/media/usb/em28xx/em28xx-cards.c | 2 ++ drivers/media/usb/em28xx/em28xx-core.c | 6 ++++++ drivers/media/usb/em28xx/em28xx.h | 1 + 3 files changed, 9 insertions(+)(quoted diff) +0
-0
b/drivers/media/usb/em28xx/em28xx-cards.cindexba9292e2a587..8b1ff79c37a0100644 ---a/drivers/media/usb/em28xx/em28xx-cards.c+++b/drivers/media/usb/em28xx/em28xx-cards.c@@ -4148,6 +4148,8 @@ static voidem28xx_usb_disconnect(struct usb_interface *intf)dev->dev_next= NULL; }(quoted diff) +2
-0kref_put(&dev->ref, em28xx_free_device); + + em28xx_reset_lists(); } static int em28xx_usb_suspend(struct usb_interface *intf,(quoted diff) +0
-0
b/drivers/media/usb/em28xx/em28xx-core.cindex584fa400cd7d..03970ed00dba100644 ---a/drivers/media/usb/em28xx/em28xx-core.c+++b/drivers/media/usb/em28xx/em28xx-core.c@@ -1131,6 +1131,12 @@ voidem28xx_init_extension(struct em28xx *dev)mutex_unlock(&em28xx_devlist_mutex); }Review
Quoted 4 lines
(quoted diff) +4
-0+void em28xx_reset_lists(void) +{ + INIT_LIST_HEAD(&em28xx_devlist); + INIT_LIST_HEAD(&em28xx_extension_devlist);This needs a
mutex_lock(&em28xx_devlist_mutex);
Quoted 6 lines
But actually, I don't think this is right: if there are multiple em28xx devices, then I think if you disconnect one, then the other is
- with this code - also removed from the list.Can you give a link to the actual syzbot bug? I'm not at all sure you are fixing the right thing here.
Hi, Hans!
I guess, I missed my coffee that morning, sorry :) This patch looks complety wrong, of course.
I've took a close look at this bug one more time, and I found the true root case of this bug.
If em28xx dev has dev_next pointer we need to close dev->next extension on disconnect to avoid UAF and corrupted list bug. So, something like this should work:
drivers/media/usb/em28xx/em28xx-cards.c +3 -1
@@ -4139,8 +4139,10 @@ static void em28xx_usb_disconnect(struct usb_interface *intf)em28xx_close_extension (dev); -if (dev->dev_next) +if (dev->dev_next) {em28xx_release_resources (dev->dev_next); +em28xx_close_extension (dev->dev_next); + }em28xx_release_resources (dev);if (dev->dev_next) {
How to You feel about it? I am going to send this patch for syzbot testing
Syzbot link:
https://syzkaller.appspot.com/bug?id=3609bbf45bf63a8f6032f330eb3d34f51cc81493
Again, sorry for v1 patch, I don't know how I came up with this fix :(
With regards,
Pavel Skripkin
Quoted 83 lines · Pavel Skripkin
Quoted 44 lines · On Tue, 20 Jul 2021 14:13:13 +0200 Hans Verkuil <[email protected]> wrote:
Quoted 36 lines · Pavel Skripkin
Syzbot reported corrupted list in em28xx driver. The problem was in non-reinitialized lists on disconnect. Since all 2 lists are global variables and driver can be connected and disconnected many times we should call
INIT_LIST_HEAD()in .disconnect method to prevent corrupted list entries.Fixes 1a23f81b7dc3 ("V4L/DVB (9979): em28xx: move usb probe codeto a proper place") Reported-by:
[email protected]Signed-off-by Pavel Skripkin <[email protected]> ---drivers/media/usb/em28xx/em28xx-cards.c | 2 ++ drivers/media/usb/em28xx/em28xx-core.c | 6 ++++++ drivers/media/usb/em28xx/em28xx.h | 1 + 3 files changed, 9 insertions(+)(quoted diff) +0
-0
b/drivers/media/usb/em28xx/em28xx-cards.cindexba9292e2a587..8b1ff79c37a0100644 ---a/drivers/media/usb/em28xx/em28xx-cards.c+++b/drivers/media/usb/em28xx/em28xx-cards.c@@ -4148,6 +4148,8 @@ static voidem28xx_usb_disconnect(struct usb_interface *intf)dev->dev_next= NULL; }(quoted diff) +2
-0kref_put(&dev->ref, em28xx_free_device); + + em28xx_reset_lists(); } static int em28xx_usb_suspend(struct usb_interface *intf,(quoted diff) +0
-0
b/drivers/media/usb/em28xx/em28xx-core.cindex584fa400cd7d..03970ed00dba100644 ---a/drivers/media/usb/em28xx/em28xx-core.c+++b/drivers/media/usb/em28xx/em28xx-core.c@@ -1131,6 +1131,12 @@ voidem28xx_init_extension(struct em28xx *dev)mutex_unlock(&em28xx_devlist_mutex); }Review
Quoted 4 lines
(quoted diff) +4
-0+void em28xx_reset_lists(void) +{ + INIT_LIST_HEAD(&em28xx_devlist); + INIT_LIST_HEAD(&em28xx_extension_devlist);This needs a
mutex_lock(&em28xx_devlist_mutex);Quoted 6 lines
But actually, I don't think this is right: if there are multiple em28xx devices, then I think if you disconnect one, then the other is
- with this code - also removed from the list.Can you give a link to the actual syzbot bug? I'm not at all sure you are fixing the right thing here.
Hi, Hans!
I guess, I missed my coffee that morning, sorry :) This patch looks complety wrong, of course.
I've took a close look at this bug one more time, and I found the true root case of this bug.
If em28xx dev has
dev_nextpointer we need to closedev->nextextension on disconnect to avoid UAF and corrupted list bug. So, something like this should work:drivers/media/usb/em28xx/em28xx-cards.c +3
-1@@ -4139,8 +4139,10 @@ static void em28xx_usb_disconnect(struct usb_interface *intf)em28xx_close_extension (dev); -if (dev->dev_next) +if (dev->dev_next) {em28xx_release_resources (dev->dev_next); +em28xx_close_extension (dev->dev_next); + }em28xx_release_resources (dev);if (dev->dev_next) {
Quoted 2 lines
How to You feel about it? I am going to send this patch for syzbot testing
That looks a lot saner :-)
Quoted 7 lines
Syzbot link:
https://syzkaller.appspot.com/bug?id=3609bbf45bf63a8f6032f330eb3d34f51cc81493Again, sorry for v1 patch, I don't know how I came up with this fix :(
No problem, just post a v2 if syzbot passes.
Regards,
Hans
Quoted 6 lines
With regards,
Pavel Skripkin
PATCH v2 1/1 media: em28xx: add missing em28xx_close_extension
If em28xx dev has ->dev_next pointer, we need to delete dev_next list node from em28xx_extension_devlist on disconnect to avoid UAF bugs and corrupted list bugs, since driver frees this pointer on disconnect.
---
Changes in v2:
Previous patch was completely broken. I've done some debugging again and found true root case of the reported bug.
--- drivers/media/usb/em28xx/em28xx-cards.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/media/usb/em28xx/em28xx-cards.c +3 -1
@@ -4139,8 +4139,10 @@ static void em28xx_usb_disconnect(struct usb_interface *intf)em28xx_close_extension (dev); -if (dev->dev_next) +if (dev->dev_next) {em28xx_release_resources (dev->dev_next); +em28xx_close_extension (dev->dev_next); + }em28xx_release_resources (dev);if (dev->dev_next) {
Signature · 1 line
2.32.0
Re: [PATCH v2] media: em28xx: add missing em28xx_close_extension
Quoted 22 lines · Pavel Skripkin
If em28xx dev has ->dev_next pointer, we need to delete
dev_nextlist node fromem28xx_extension_devliston disconnect to avoid UAF bugs and corrupted list bugs, since driver frees this pointer on disconnect.Fixes 1a23f81b7dc3 ("V4L/DVB (9979): em28xx: move usb probe code to a proper place")Reported-and-tested-by [email protected]Signed-off-by Pavel Skripkin <[email protected]>---
Changes in v2:
Previous patch was completely broken. I've done some debugging again and found true root case of the reported bug.--- drivers/media/usb/em28xx/em28xx-cards.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)drivers/media/usb/em28xx/em28xx-cards.c +0
-0
drivers/media/usb/em28xx/em28xx-cards.c static void em28xx_usb_disconnect(struct usb_interface *intf) · line 4144
Quoted 7 lines
drivers/media/usb/em28xx/em28xx-cards.c +2
-1@@ -4139,8 +4139,10 @@ static void em28xx_usb_disconnect(struct usb_interface *intf)em28xx_close_extension (dev); -if (dev->dev_next) +if (dev->dev_next) {em28xx_release_resources (dev->dev_next); +em28xx_close_extension (dev->dev_next);
Wouldn't it be better to swap these two?
That order is also used for em28xx_close_extension(dev) and em28xx_release_resources(dev).
You do need to store dev->dev_next in a temp variable, though.
Regards,
Hans
Quoted 5 lines
drivers/media/usb/em28xx/em28xx-cards.c +1
-0+ }em28xx_release_resources (dev);if (dev->dev_next) {
Re: [PATCH v2] media: em28xx: add missing em28xx_close_extension
Quoted 34 lines · On Thu, 29 Jul 2021 11:45:19 +0200 Hans Verkuil <[email protected]> wrote:
Quoted 24 lines · Pavel Skripkin
If em28xx dev has ->dev_next pointer, we need to delete
dev_nextlist node fromem28xx_extension_devliston disconnect to avoid UAF bugs and corrupted list bugs, since driver frees this pointer on disconnect.Fixes 1a23f81b7dc3 ("V4L/DVB (9979): em28xx: move usb probe codeto a proper place") Reported-and-tested-by:
[email protected]Signed-off-by Pavel Skripkin <[email protected]> ---Changes in v2:
Previous patch was completely broken. I've done some
debugging again and found true root case of the reported bug.--- drivers/media/usb/em28xx/em28xx-cards.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)(quoted diff) +0
-0
b/drivers/media/usb/em28xx/em28xx-cards.cindexc1e0dccb7408..d56b040e1bd7100644 ---a/drivers/media/usb/em28xx/em28xx-cards.c+++b/drivers/media/usb/em28xx/em28xx-cards.c@@ -4139,8 +4139,10 @@ static voidem28xx_usb_disconnect(struct usb_interface *intf)drivers/media/usb/em28xx/em28xx-cards.c static void em28xx_usb_disconnect(struct usb_interface *intf) · line 4144
Quoted 6 lines
drivers/media/usb/em28xx/em28xx-cards.c +2
-1em28xx_close_extension (dev); -if (dev->dev_next) +if (dev->dev_next) {em28xx_release_resources (dev->dev_next); +em28xx_close_extension (dev->dev_next);Wouldn't it be better to swap these two?
Quoted 5 lines
That order is also used for
em28xx_close_extension(dev)andem28xx_release_resources(dev).You do need to store
dev->dev_nextin a temp variable, though.
Hi, Hans!
I don't understand why I need to store dev->dev_next in a temp variable. I don't see code in em28xx_release_resources() or em28xx_close_extension() that zeroes this pointer.
With regards,
Pavel Skripkin
Re: [PATCH v2] media: em28xx: add missing em28xx_close_extension
Quoted 40 lines · Pavel Skripkin
Quoted 37 lines · On Thu, 29 Jul 2021 11:45:19 +0200 Hans Verkuil <[email protected]> wrote:
Quoted 24 lines · Pavel Skripkin
If em28xx dev has ->dev_next pointer, we need to delete
dev_nextlist node fromem28xx_extension_devliston disconnect to avoid UAF bugs and corrupted list bugs, since driver frees this pointer on disconnect.Fixes 1a23f81b7dc3 ("V4L/DVB (9979): em28xx: move usb probe codeto a proper place") Reported-and-tested-by:
[email protected]Signed-off-by Pavel Skripkin <[email protected]> ---Changes in v2:
Previous patch was completely broken. I've done some
debugging again and found true root case of the reported bug.--- drivers/media/usb/em28xx/em28xx-cards.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-)(quoted diff) +0
-0
b/drivers/media/usb/em28xx/em28xx-cards.cindexc1e0dccb7408..d56b040e1bd7100644 ---a/drivers/media/usb/em28xx/em28xx-cards.c+++b/drivers/media/usb/em28xx/em28xx-cards.c@@ -4139,8 +4139,10 @@ static voidem28xx_usb_disconnect(struct usb_interface *intf)drivers/media/usb/em28xx/em28xx-cards.c static void em28xx_usb_disconnect(struct usb_interface *intf) · line 4144
Quoted 6 lines
drivers/media/usb/em28xx/em28xx-cards.c +2
-1em28xx_close_extension (dev); -if (dev->dev_next) +if (dev->dev_next) {em28xx_release_resources (dev->dev_next); +em28xx_close_extension (dev->dev_next);Wouldn't it be better to swap these two?
That order is also used for
em28xx_close_extension(dev)andem28xx_release_resources(dev).
Quoted 8 lines
Quoted 2 lines
You do need to store
dev->dev_nextin a temp variable, though.Hi, Hans!
I don't understand why I need to store
dev->dev_nextin a temp variable. I don't see code inem28xx_release_resources()orem28xx_close_extension()that zeroes this pointer.
Apologies for the confusion, just ignore that bit. I misunderstood what dev_next was for.
So check if swapping these two lines passes syzbot, and then that's the final version.
Regards,
Hans
Quoted 7 lines
With regards,
Pavel Skripkin
PATCH v3 1/1 media: em28xx: add missing em28xx_close_extension
If em28xx dev has ->dev_next pointer, we need to delete ->dev_next list node from em28xx_extension_devlist on disconnect to avoid UAF bugs and corrupted list bugs, since driver frees this pointer on disconnect.
---
Changes in v3:
Changed order of em28xx_close_extension() and em28xx_release_resources() as Hans suggested
Changes in v2:
Previous patch was completely broken. I've done some debugging again and found true root case of the reported bug.
--- drivers/media/usb/em28xx/em28xx-cards.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
drivers/media/usb/em28xx/em28xx-cards.c +4 -1
@@ -4139,8 +4139,11 @@ static void em28xx_usb_disconnect(struct usb_interface *intf)em28xx_close_extension (dev); -if (dev->dev_next) +if (dev->dev_next) { +em28xx_close_extension (dev->dev_next);em28xx_release_resources (dev->dev_next); + } +em28xx_release_resources (dev);if (dev->dev_next) {
Signature · 1 line
2.32.0