Skip to contentSkip to conversation

lib: vsprintf: check for NULL device_node name in device_node_string()

PATCH In review Enrico Weigelt, metux IT consult 5 participants 6 messages Feb 17, 2021 – Feb 23, 2021

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

PATCH 1/1 lib: vsprintf: check for NULL device_node name in device_node_string()

Under rare circumstances it may happen that a device node's name is NULL (most likely kernel bug in some other place). In such situations anything but helpful, if the debug printout crashes, and nobody knows what actually happened here.

Therefore protect it by an explicit NULL check and print out an extra warning.

---
 lib/vsprintf.c | 4 ++++
 1 file changed, 4 insertions(+)
lib/vsprintf.c +4 -0
index 3b53c73580c5..050a60b88073 100644
@@ -2013,6 +2013,10 @@ char *device_node_string(char *buf, char *end, struct device_node *dn,
 			break;
 		case 'n':	/* name */
 			p = fwnode_get_name(of_fwnode_handle(dn));
+			if (!p) {
+				pr_warn("device_node without name. Kernel bug ?\n");
+				p = "<NULL>";
+			}
 			precision = str_spec.precision;
 			str_spec.precision = strchrnul(p, '@') - p;
 			buf = string(buf, end, p, str_spec);
Signature · 1 line
2.11.0
#1 lore raw
Quoted 2 lines · metux IT consult

Under rare circumstances it may happen that a device node's name is NULL (most likely kernel bug in some other place).

What circumstances? How can I reproduce this? More information, please!

Quoted 6 lines

In such situations anything
but helpful, if the debug printout crashes, and nobody knows what actually happened here.

Therefore protect it by an explicit NULL check and print out an extra warning.

...

lib/vsprintf.c char *device_node_string(char *buf, char *end, struct device_node *dn, · line 2017

Quoted 1 line
lib/vsprintf.c +1 -0
+				pr_warn("device_node without name. Kernel bug ?\n");

If it's not once, then it's possible to have log spammed with this, right?

...

lib/vsprintf.c char *device_node_string(char *buf, char *end, struct device_node *dn, · line 2018

Quoted 1 line
lib/vsprintf.c +1 -0
+				p = "<NULL>";

We have different standard de facto for NULL pointers to be printed. Actually if you wish, you may gather them under one definition (maybe somewhere under printk) and export to everybody to use.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
maintainer
#2 lore raw
Quoted 19 lines · Andy Shevchenko
Quoted 2 lines · metux IT consult

Under rare circumstances it may happen that a device node's name is NULL (most likely kernel bug in some other place).

What circumstances? How can I reproduce this? More information, please!

Quoted 6 lines

In such situations anything
but helpful, if the debug printout crashes, and nobody knows what actually happened here.

Therefore protect it by an explicit NULL check and print out an extra warning.

...

lib/vsprintf.c char *device_node_string(char *buf, char *end, struct device_node *dn, · line 2017

Quoted 1 line
lib/vsprintf.c +1 -0
+				pr_warn("device_node without name. Kernel bug ?\n");

If it's not once, then it's possible to have log spammed with this, right?

Quoted 7 lines

...

lib/vsprintf.c char *device_node_string(char *buf, char *end, struct device_node *dn, · line 2018

Quoted 1 line
lib/vsprintf.c +1 -0
+				p = "<NULL>";

We have different standard de facto for NULL pointers to be printed. Actually if you wish, you may gather them under one definition (maybe somewhere under printk) and export to everybody to use.

Please, use

	if (check_pointer(&buf, end, p, spec))
		return buf;

It will print "(null)" instead of the name. It should be enough to inform the user this way. The extra pr_warn() does not help much to localize the problem anyway. And it is better to avoid recursion in this path.

Best Regards,
Petr

#3 lore raw
Quoted 9 lines · Petr Mladek

Please, use

	if (check_pointer(&buf, end, p, spec))
		return buf;

It will print "(null)" instead of the name. It should be enough to inform the user this way. The extra pr_warn() does not help much to localize the problem anyway. And it is better to avoid recursion in this path.

thx, going to use it in v2.

--mtx

Signature · 8 lines
---
Hinweis: unverschlüsselte E-Mails können leicht abgehört und manipuliert
werden ! Für eine vertrauliche Kommunikation senden Sie bitte ihren
GPG/PGP-Schlüssel zu.
---
Enrico Weigelt, metux IT consult
Free software and Linux embedded engineering
[email protected] -- +49-151-27565287
#4 lore raw
In reply to Andy Shevchenko
Quoted 5 lines · Andy Shevchenko
Quoted 2 lines · metux IT consult

Under rare circumstances it may happen that a device node's name is NULL (most likely kernel bug in some other place).

What circumstances? How can I reproduce this? More information, please!

Observed it when applying a broken overlay. (sorry, didn't keep that broken code :o). In this case, the device_node was left without a name (pointing to NULL).

Quoted 3 lines

lib/vsprintf.c char *device_node_string(char *buf, char *end, struct device_node *dn, · line 2017

Quoted 1 line
lib/vsprintf.c +1 -0
+				pr_warn("device_node without name. Kernel bug ?\n");

If it's not once, then it's possible to have log spammed with this, right?

It only has occoured once for me. I don't think spamming could happen, unless one's hacking deeply in the oftree code.

Quoted 5 lines

lib/vsprintf.c char *device_node_string(char *buf, char *end, struct device_node *dn, · line 2018

Quoted 1 line
lib/vsprintf.c +1 -0
+				p = "<NULL>";

We have different standard de facto for NULL pointers to be printed. Actually if you wish, you may gather them under one definition (maybe somewhere under printk) and export to everybody to use.

Seen it in Petr's reply ... going to use that in v2.

--mtx

Signature · 8 lines
---
Hinweis: unverschlüsselte E-Mails können leicht abgehört und manipuliert
werden ! Für eine vertrauliche Kommunikation senden Sie bitte ihren
GPG/PGP-Schlüssel zu.
---
Enrico Weigelt, metux IT consult
Free software and Linux embedded engineering
[email protected] -- +49-151-27565287
#5 lore raw
In reply to Enrico Weigelt, metux IT consult
Quoted 18 lines · metux IT consult

Under rare circumstances it may happen that a device node's name is NULL (most likely kernel bug in some other place). In such situations anything but helpful, if the debug printout crashes, and nobody knows what actually happened here.

Therefore protect it by an explicit NULL check and print out an extra warning.

---
 lib/vsprintf.c | 4 ++++
 1 file changed, 4 insertions(+)
lib/vsprintf.c +0 -0
index 3b53c73580c5..050a60b88073 100644

lib/vsprintf.c char *device_node_string(char *buf, char *end, struct device_node *dn, · line 2022

Quoted 10 lines
lib/vsprintf.c +4 -0
@@ -2013,6 +2013,10 @@ char *device_node_string(char *buf, char *end, struct device_node *dn,
 			break;
 		case 'n':	/* name */
 			p = fwnode_get_name(of_fwnode_handle(dn));
+			if (!p) {
+				pr_warn("device_node without name. Kernel bug ?\n");
+				p = "<NULL>";
+			}
 			precision = str_spec.precision;
 			str_spec.precision = strchrnul(p, '@') - p;
 			buf = string(buf, end, p, str_spec);

What about other fwnode_get_name() calls in vsprintf?

	-ss