Skip to contentSkip to conversation

Console/stdio use after free

PATCH New Nicolas Saenz Julienne 5 participants 14 messages Jan 20, 2021 – Jan 27, 2021

lore
Share & tools
lore
#0 lore raw

cover Console/stdio use after free

With today's master, 70c2525c0d3c ('IOMUX: Stop dropped consoles') introduces a use after free in usb_kbd_remove():

- usbkbd's stdio device is de-registered with stdio_deregister_dev(), the struct stdio_dev is freed.

- iomux_doenv() is called, usbkbd removed from the console list, and console_stop() is called on the struct stdio_dev pointer that no longer exists.

This series mitigates this by making sure the pointer is really a stdio device prior performing the stop operation. It's not ideal, but I couldn't figure out a nicer way to fix this.

Regards,
Nicolas

---

Nicolas Saenz Julienne (2):
stdio: Introduce stdio_valid()
console: Don't start/stop console if stdio device invalid

 common/console.c    |  3 +++
 common/stdio.c      | 11 +++++++++++
 include/stdio_dev.h |  1 +
 3 files changed, 15 insertions(+)
Signature · 1 line
2.30.0
#1 lore raw

PATCH 1/2 stdio: Introduce stdio_valid()

stdio_valid() will confirm that a struct stdio_dev pointer is indeed valid.

---
 common/stdio.c      | 11 +++++++++++
 include/stdio_dev.h |  1 +
 2 files changed, 12 insertions(+)
common/stdio.c +11 -0
index abf9b1e915..69b7d2692d 100644
@@ -157,6 +157,17 @@ static int stdio_probe_device(const char *name, enum uclass_id id,
 	return 0;
 }
 
+bool stdio_valid(struct stdio_dev *dev)
+{
+	struct stdio_dev *sdev;
+
+	list_for_each_entry(sdev, &devs.list, list)
+		if (sdev == dev)
+			return true;
+
+	return false;
+}
+
 struct stdio_dev *stdio_get_by_name(const char *name)
 {
 	struct list_head *pos;
include/stdio_dev.h +1 -0
index 48871a6a22..f341439b03 100644
@@ -97,6 +97,7 @@ int stdio_deregister_dev(struct stdio_dev *dev, int force);
 struct list_head *stdio_get_list(void);
 struct stdio_dev *stdio_get_by_name(const char *name);
 struct stdio_dev *stdio_clone(struct stdio_dev *dev);
+bool stdio_valid(struct stdio_dev *dev);
 
 int drv_lcd_init(void);
 int drv_video_init(void);
Signature · 1 line
2.30.0
#2 lore raw

PATCH 1/2 stdio: Introduce stdio_valid()

Hi Nicolas,

Quoted 37 lines · Nicolas Saenz Julienne

stdio_valid() will confirm that a struct stdio_dev pointer is indeed valid.

---
 common/stdio.c      | 11 +++++++++++
 include/stdio_dev.h |  1 +
 2 files changed, 12 insertions(+)
common/stdio.c +11 -0
index abf9b1e915..69b7d2692d 100644
@@ -157,6 +157,17 @@ static int stdio_probe_device(const char *name, enum uclass_id id,
        return 0;
 }
 
+bool stdio_valid(struct stdio_dev *dev)
+{
+       struct stdio_dev *sdev;
+
+       list_for_each_entry(sdev, &devs.list, list)
+               if (sdev == dev)
+                       return true;
+
+       return false;
+}
+
 struct stdio_dev *stdio_get_by_name(const char *name)
 {
        struct list_head *pos;
include/stdio_dev.h +0 -0
index 48871a6a22..f341439b03 100644

include/stdio_dev.h int stdio_deregister_dev(struct stdio_dev *dev, int force); · line 100

Quoted 4 lines
include/stdio_dev.h +1 -0
@@ -97,6 +97,7 @@ int stdio_deregister_dev(struct stdio_dev *dev, int force);
 struct list_head *stdio_get_list(void);
 struct stdio_dev *stdio_get_by_name(const char *name);
 struct stdio_dev *stdio_clone(struct stdio_dev *dev);
+bool stdio_valid(struct stdio_dev *dev);

Please add a full function comment and explain what valid means.

Quoted 6 lines
include/stdio_dev.h +0 -1
 int drv_lcd_init(void);
 int drv_video_init(void);
--

2.30.0

Regards,
Simon

#3 lore raw

PATCH 1/2 stdio: Introduce stdio_valid()

Quoted 39 lines · Simon Glass

Hi Nicolas,

Quoted 35 lines · Nicolas Saenz Julienne

stdio_valid() will confirm that a struct stdio_dev pointer is indeed valid.

---
?common/stdio.c | 11 +++++++++++
?include/stdio_dev.h | 1 +
?2 files changed, 12 insertions(+)

common/stdio.c +11 -0
index abf9b1e915..69b7d2692d 100644
@@ -157,6 +157,17 @@ static int stdio_probe_device(const char *name, enum uclass_id id,
 ????????return 0;
 ?}
 
+bool stdio_valid(struct stdio_dev *dev)
+{
+       struct stdio_dev *sdev;
+
+       list_for_each_entry(sdev, &devs.list, list)
+               if (sdev == dev)
+                       return true;
+
+       return false;
+}
+
 ?struct stdio_dev *stdio_get_by_name(const char *name)
 ?{
 ????????struct list_head *pos;
include/stdio_dev.h +0 -1
index 48871a6a22..f341439b03 100644
--- a/include/stdio_dev.h
Quoted 8 lines

include/stdio_dev.h int stdio_deregister_dev(struct stdio_dev *dev, int force); · line 100

Quoted 6 lines

+++ b/include/stdio_dev.h

include/stdio_dev.h +1 -0
@@ -97,6 +97,7 @@ int stdio_deregister_dev(struct stdio_dev *dev, int force);
 ?struct list_head *stdio_get_list(void);
 ?struct stdio_dev *stdio_get_by_name(const char *name);
 ?struct stdio_dev *stdio_clone(struct stdio_dev *dev);
+bool stdio_valid(struct stdio_dev *dev);

Please add a full function comment and explain what valid means.

As discussed with Andy, this is a workaround that doesn't address the underlying issue. If it's good enough for the time being I'll be happy to send a v2.

I'll leave a comment stating that it's something to fix.

Regards,
Nicolas

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: This is a digitally signed message part
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210125/dd853625/attachment.sig>

#4 lore raw

PATCH 1/2 stdio: Introduce stdio_valid()

Quoted 49 lines · Nicolas Saenz Julienne
Quoted 39 lines · Simon Glass

Hi Nicolas,

Quoted 35 lines · Nicolas Saenz Julienne

stdio_valid() will confirm that a struct stdio_dev pointer is indeed valid.

---
?common/stdio.c | 11 +++++++++++
?include/stdio_dev.h | 1 +
?2 files changed, 12 insertions(+)

common/stdio.c +11 -0
index abf9b1e915..69b7d2692d 100644
@@ -157,6 +157,17 @@ static int stdio_probe_device(const char *name, enum uclass_id id,
 ????????return 0;
 ?}
 
+bool stdio_valid(struct stdio_dev *dev)
+{
+       struct stdio_dev *sdev;
+
+       list_for_each_entry(sdev, &devs.list, list)
+               if (sdev == dev)
+                       return true;
+
+       return false;
+}
+
 ?struct stdio_dev *stdio_get_by_name(const char *name)
 ?{
 ????????struct list_head *pos;
include/stdio_dev.h +0 -1
index 48871a6a22..f341439b03 100644
--- a/include/stdio_dev.h
Quoted 8 lines

include/stdio_dev.h int stdio_deregister_dev(struct stdio_dev *dev, int force); · line 100

Quoted 6 lines

+++ b/include/stdio_dev.h

include/stdio_dev.h +1 -0
@@ -97,6 +97,7 @@ int stdio_deregister_dev(struct stdio_dev *dev, int force);
 ?struct list_head *stdio_get_list(void);
 ?struct stdio_dev *stdio_get_by_name(const char *name);
 ?struct stdio_dev *stdio_clone(struct stdio_dev *dev);
+bool stdio_valid(struct stdio_dev *dev);

Please add a full function comment and explain what valid means.

Quoted 5 lines

As discussed with Andy, this is a workaround that doesn't address the underlying issue. If it's good enough for the time being I'll be happy to send a v2.

I'll leave a comment stating that it's something to fix.

Please do, thanks.

Signature · 8 lines
Tom
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 659 bytes
Desc: not available
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210127/c3fe2f1e/attachment.sig>
#5 lore raw

PATCH 2/2 console: Don't start/stop console if stdio device invalid

In reply to Nicolas Saenz Julienne

Don't start/stop an stdio device that might have been already freed.

Fixes 70c2525c0d3c ("IOMUX: Stop dropped consoles")
---
 common/console.c | 3 +++
 1 file changed, 3 insertions(+)
common/console.c +3 -0
index f3cc45cab5..5c6b74b351 100644
@@ -252,6 +252,9 @@ static bool console_needs_start_stop(int file, struct stdio_dev *sdev)
 {
 	int i, j;
 
+	if (!stdio_valid(sdev))
+		return false;
+
 	for (i = 0; i < ARRAY_SIZE(cd_count); i++) {
 		if (i == file)
 			continue;
Signature · 1 line
2.30.0
#7 lore raw

cover Console/stdio use after free

In reply to Nicolas Saenz Julienne

Hi Nicolas,

Quoted 7 lines · Nicolas Saenz Julienne

With today's master, 70c2525c0d3c ('IOMUX: Stop dropped consoles') introduces a use after free in usb_kbd_remove():

- usbkbd's stdio device is de-registered with stdio_deregister_dev(), the struct stdio_dev is freed.

Quoted 7 lines

- iomux_doenv() is called, usbkbd removed from the console list, and console_stop() is called on the struct stdio_dev pointer that no longer exists.

This series mitigates this by making sure the pointer is really a stdio device prior performing the stop operation. It's not ideal, but I couldn't figure out a nicer way to fix this.

Your 'from' address is coming through as just your email. Could you please update it to include your name as well?

Regards,
Simon

#8 lore raw

cover Console/stdio use after free

Quoted 15 lines · Simon Glass

Hi Nicolas,

Quoted 11 lines · Nicolas Saenz Julienne

With today's master, 70c2525c0d3c ('IOMUX: Stop dropped consoles') introduces a use after free in usb_kbd_remove():

- usbkbd's stdio device is de-registered with stdio_deregister_dev(), ??the struct stdio_dev is freed.

- iomux_doenv() is called, usbkbd removed from the console list, and ??console_stop() is called on the struct stdio_dev pointer that no ??longer exists.

Quoted 6 lines
Quoted 3 lines

This series mitigates this by making sure the pointer is really a stdio device prior performing the stop operation. It's not ideal, but I couldn't figure out a nicer way to fix this.

Your 'from' address is coming through as just your email. Could you please update it to include your name as well?

OK, do you want me to re-send the series?

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: This is a digitally signed message part
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210120/a65fbb52/attachment.sig>

#9 lore raw

cover Console/stdio use after free

Hi Nicolas,

Quoted 17 lines · Nicolas Saenz Julienne
Quoted 15 lines · Simon Glass

Hi Nicolas,

Quoted 11 lines · Nicolas Saenz Julienne

With today's master, 70c2525c0d3c ('IOMUX: Stop dropped consoles') introduces a use after free in usb_kbd_remove():

- usbkbd's stdio device is de-registered with stdio_deregister_dev(), the struct stdio_dev is freed.

- iomux_doenv() is called, usbkbd removed from the console list, and console_stop() is called on the struct stdio_dev pointer that no longer exists.

Quoted 8 lines
Quoted 6 lines
Quoted 3 lines

This series mitigates this by making sure the pointer is really a stdio device prior performing the stop operation. It's not ideal, but I couldn't figure out a nicer way to fix this.

Your 'from' address is coming through as just your email. Could you please update it to include your name as well?

OK, do you want me to re-send the series?

Not just for that, no.

Regards,
Simon

#10 lore raw

cover Console/stdio use after free

In reply to Simon Glass
Quoted 15 lines · Simon Glass

Hi Nicolas,

Quoted 11 lines · Nicolas Saenz Julienne

With today's master, 70c2525c0d3c ('IOMUX: Stop dropped consoles') introduces a use after free in usb_kbd_remove():

- usbkbd's stdio device is de-registered with stdio_deregister_dev(), the struct stdio_dev is freed.

- iomux_doenv() is called, usbkbd removed from the console list, and console_stop() is called on the struct stdio_dev pointer that no longer exists.

Quoted 6 lines
Quoted 3 lines

This series mitigates this by making sure the pointer is really a stdio device prior performing the stop operation. It's not ideal, but I couldn't figure out a nicer way to fix this.

Your 'from' address is coming through as just your email. Could you please update it to include your name as well?

From shows the full name on my email client. For everybody apart from Nicholas it shows just the email, but for Nicholas I can see full name in both From and Cc.

Maybe something wrong with your email client settings.

Quoted 2 lines

Regards,
Simon

Signature · 3 lines
Regards,
Pratyush Yadav
Texas Instruments India
#11 lore raw

cover Console/stdio use after free

Hi Pratyush,

Quoted 21 lines · Pratyush Yadav
Quoted 19 lines · Simon Glass

Hi Nicolas,

Quoted 14 lines · Nicolas Saenz Julienne

With today's master, 70c2525c0d3c ('IOMUX: Stop dropped consoles') introduces a use after free in usb_kbd_remove():

- usbkbd's stdio device is de-registered with stdio_deregister_dev(), the struct stdio_dev is freed.

- iomux_doenv() is called, usbkbd removed from the console list, and console_stop() is called on the struct stdio_dev pointer that no longer exists.

This series mitigates this by making sure the pointer is really a stdio device prior performing the stop operation. It's not ideal, but I couldn't figure out a nicer way to fix this.

Quoted 8 lines
Quoted 2 lines

Your 'from' address is coming through as just your email. Could you please update it to include your name as well?

From shows the full name on my email client. For everybody apart from Nicholas it shows just the email, but for Nicholas I can see full name in both From and Cc.

Maybe something wrong with your email client settings.

Yes, perhaps you are right. Looking at the list I also have this problem for SkyLake Huang <[email protected]>

Regards,
Simon

#12 lore raw

cover Console/stdio use after free

In reply to Nicolas Saenz Julienne
Quoted 7 lines · Nicolas Saenz Julienne

With today's master, 70c2525c0d3c ('IOMUX: Stop dropped consoles') introduces a use after free in usb_kbd_remove():

- usbkbd's stdio device is de-registered with stdio_deregister_dev(), the struct stdio_dev is freed.

Quoted 7 lines

- iomux_doenv() is called, usbkbd removed from the console list, and console_stop() is called on the struct stdio_dev pointer that no longer exists.

This series mitigates this by making sure the pointer is really a stdio device prior performing the stop operation. It's not ideal, but I couldn't figure out a nicer way to fix this.

Thanks for the report and indeed this sounds like a papering over the real issue somewhere else.
If we have a device in the console_list, IOMUX may access it. So, whenever we drop device, we must update console_list accordingly.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#13 lore raw

cover Console/stdio use after free

Hi Andy, Simon

Quoted 13 lines · Andy Shevchenko
Quoted 11 lines · Nicolas Saenz Julienne

With today's master, 70c2525c0d3c ('IOMUX: Stop dropped consoles') introduces a use after free in usb_kbd_remove():

- usbkbd's stdio device is de-registered with stdio_deregister_dev(), ??the struct stdio_dev is freed.

- iomux_doenv() is called, usbkbd removed from the console list, and ??console_stop() is called on the struct stdio_dev pointer that no ??longer exists.

Quoted 8 lines
Quoted 3 lines

This series mitigates this by making sure the pointer is really a stdio device prior performing the stop operation. It's not ideal, but I couldn't figure out a nicer way to fix this.

Thanks for the report and indeed this sounds like a papering over the real issue somewhere else.
If we have a device in the console_list, IOMUX may access it. So, whenever we drop device, we must update console_list accordingly.

Sorry, but I don't have time to address this ATM. If someone else can it'd be nice.

Regards,
Nicolas

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 488 bytes
Desc: This is a digitally signed message part
URL: <https://lists.denx.de/pipermail/u-boot/attachments/20210125/4c4d4567/attachment.sig>