Skip to contentSkip to conversation

led/led-class: Handle LEDs with the same name

PATCH In review Ricardo Ribalda Delgado 6 participants 17 messages Mar 13, 2015 – Jun 26, 2015

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

PATCH v3 1/1 led/led-class: Handle LEDs with the same name

The current code expected that every LED had an unique name. This is a legit expectation when the device tree can no be modified or extended. But with device tree overlays this requirement can be easily broken.

This patch finds out if the name is already in use and adds the suffix _1, _2... if not.

---
v3: Suggested by Bryan Wu <[email protected]>

-Do not allocate dynamically the name

 drivers/leds/led-class.c | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)
drivers/leds/led-class.c +29 -2
index 768d33a..4ca37b8 100644
@@ -212,6 +212,26 @@ static const struct dev_pm_ops leds_class_dev_pm_ops = {
 	.resume         = led_resume,
 };
 
+static int match_name(struct device *dev, const void *data)
+{
+	if (!dev_name(dev))
+		return 0;
+	return !strcmp(dev_name(dev), (char *)data);
+}
+
+static int led_classdev_next_name(const char *init_name, char *name,
+				  size_t len)
+{
+	int i = 0;
+
+	strncpy(name, init_name, len);
+
+	while (class_find_device(leds_class, NULL, name, match_name))
+		snprintf(name, len, "%s_%d", init_name, ++i);
+
+	return i;
+}
+
 /**
  * led_classdev_register - register a new object of led_classdev class.
  * @parent: The device to register.
@@ -219,12 +239,19 @@ static const struct dev_pm_ops leds_class_dev_pm_ops = {
  */
 int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)
 {
+	char name[64];
+	int ret;
+
+	ret = led_classdev_next_name(led_cdev->name, name, sizeof(name));
 	led_cdev->dev = device_create_with_groups(leds_class, parent, 0,
-					led_cdev, led_cdev->groups,
-					"%s", led_cdev->name);
+					led_cdev, led_cdev->groups, name);
 	if (IS_ERR(led_cdev->dev))
 		return PTR_ERR(led_cdev->dev);
 
+	if (ret)
+		dev_info(parent, "Led %s renamed to %s due to name collision",
+				led_cdev->name, dev_name(led_cdev->dev));
+
 #ifdef CONFIG_LEDS_TRIGGERS
 	init_rwsem(&led_cdev->trigger_lock);
 #endif
Signature · 1 line
2.1.4
#1 lore raw

Hi Ricardo,

(Cc Jacek.)

I had similar thoughts when reviewing Jacek's V4L2 flash API patches. See below.

Quoted 54 lines · Ricardo Ribalda Delgado

The current code expected that every LED had an unique name. This is a legit expectation when the device tree can no be modified or extended. But with device tree overlays this requirement can be easily broken.

This patch finds out if the name is already in use and adds the suffix _1, _2... if not.

---
v3: Suggested by Bryan Wu <[email protected]>

(quoted diff) +0 -1
-Do not allocate dynamically the name
 drivers/leds/led-class.c | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)
drivers/leds/led-class.c +24 -0
index 768d33a..4ca37b8 100644
@@ -212,6 +212,26 @@ static const struct dev_pm_ops leds_class_dev_pm_ops = {
 	.resume         = led_resume,
 };
 
+static int match_name(struct device *dev, const void *data)
+{
+	if (!dev_name(dev))
+		return 0;
+	return !strcmp(dev_name(dev), (char *)data);
+}
+
+static int led_classdev_next_name(const char *init_name, char *name,
+				  size_t len)
+{
+	int i = 0;
+
+	strncpy(name, init_name, len);
+
+	while (class_find_device(leds_class, NULL, name, match_name))
+		snprintf(name, len, "%s_%d", init_name, ++i);
+
+	return i;
+}
+
 /**
  * led_classdev_register - register a new object of led_classdev class.
  * @parent: The device to register.
@@ -219,12 +239,19 @@ static const struct dev_pm_ops leds_class_dev_pm_ops = {
  */
 int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)
 {
+	char name[64];
+	int ret;
+
+	ret = led_classdev_next_name(led_cdev->name, name, sizeof(name));

drivers/leds/led-class.c static const struct dev_pm_ops leds_class_dev_pm_ops = { · line 253

Quoted 10 lines
drivers/leds/led-class.c +4 -2
 	led_cdev->dev = device_create_with_groups(leds_class, parent, 0,
-					led_cdev, led_cdev->groups,
-					"%s", led_cdev->name);
+					led_cdev, led_cdev->groups, name);
 	if (IS_ERR(led_cdev->dev))
 		return PTR_ERR(led_cdev->dev);
 
+	if (ret)
+		dev_info(parent, "Led %s renamed to %s due to name collision",
+				led_cdev->name, dev_name(led_cdev->dev));

I'd use dev_warn() or even WARN_ON(ret). This is a rather serious issue as now the probe order defines the name of the device. There might be something better such as the I2C bus/address.

If the registration order for some reason changes, that'd revert the device names. In Media controller this is part of the user space API.

I suggested Jacek adding the uniqueness requirement to documentation as well, ePAPR by itself does not require it for label properties.

Quoted 4 lines
drivers/leds/led-class.c +1 -0
+
 #ifdef CONFIG_LEDS_TRIGGERS
 	init_rwsem(&led_cdev->trigger_lock);
 #endif
Signature · 4 lines
Kind regards,

Sakari Ailus
e-mail: [email protected]	XMPP: [email protected]
#2 lore raw

Hi Sakari

Quoted 3 lines · Sakari Ailus

I'd use dev_warn() or even WARN_ON(ret). This is a rather serious issue as now the probe order defines the name of the device. There might be something better such as the I2C bus/address.

I have no problem in sending a dev_warn, WARN_ON looks too much to me.

In my example (dt overlays), it is perfectly ok to have duplicated names.

Quoted 3 lines

If the registration order for some reason changes, that'd revert the device names. In Media controller this is part of the user space API.

Would it be possible for media controller leds to have a name based on their address?

Regarding s/dev_info/dev_warn : Do you want to send the patch or I should do it?

Regards!

Signature · 1 line
Ricardo Ribalda
#3 lore raw

Hi Ricardo,

Quoted 9 lines · Ricardo Ribalda Delgado

Hi Sakari

Quoted 3 lines · Sakari Ailus

I'd use dev_warn() or even WARN_ON(ret). This is a rather serious issue as now the probe order defines the name of the device. There might be something better such as the I2C bus/address.

I have no problem in sending a dev_warn, WARN_ON looks too much to me.

Quoted 8 lines

In my example (dt overlays), it is perfectly ok to have duplicated names.

Quoted 3 lines

If the registration order for some reason changes, that'd revert the device names. In Media controller this is part of the user space API.

Would it be possible for media controller leds to have a name based on their address?

It's included where there's a stable address. That's not true for GPIO controlled devices though, it's a GPIO controller plus a number. Ramming lots of information to the media entity's name field is not possible since it's just 32 bytes; for this reason we're looking to implement a better way to recognise devices in MC. But for now, we got to cope with what we have.

Quoted 1 line

Regarding s/dev_info/dev_warn : Do you want to send the patch or I should do it?

Is this one merged already? If yes, sure I can. Otherwise a new version would be nice.

Signature · 4 lines
Cheers,

Sakari Ailus
e-mail: [email protected]	XMPP: [email protected]
#4 lore raw

Hi Sakari!

Quoted 4 lines
Quoted 1 line

Regarding s/dev_info/dev_warn : Do you want to send the patch or I should do it?

Is this one merged already? If yes, sure I can. Otherwise a new version would be nice.

It is at least here

https://git.kernel.org/cgit/linux/kernel/git/cooloney/linux-leds.git/commit/?h=devel&id=0767f8c5c53018f8cc7d87871abb7d3290be7596

and here

https://git.kernel.org/cgit/linux/kernel/git/cooloney/linux-leds.git/commit/?h=for-next&id=0767f8c5c53018f8cc7d87871abb7d3290be7596

Regards.

Signature · 1 line
Ricardo Ribalda
#5 lore raw
Quoted 5 lines · Ricardo Ribalda Delgado

Hi Sakari!

Quoted 4 lines
Quoted 1 line

Regarding s/dev_info/dev_warn : Do you want to send the patch or I should do it?

Is this one merged already? If yes, sure I can. Otherwise a new version would be nice.

Quoted 7 lines

It is at least here

https://git.kernel.org/cgit/linux/kernel/git/cooloney/linux-leds.git/commit/?h=devel&id=0767f8c5c53018f8cc7d87871abb7d3290be7596

and here

https://git.kernel.org/cgit/linux/kernel/git/cooloney/linux-leds.git/commit/?h=for-next&id=0767f8c5c53018f8cc7d87871abb7d3290be7596

I'll send a patch. Thanks!

Signature · 2 lines
Sakari Ailus
e-mail: [email protected]	XMPP: [email protected]
#6 lore raw
Quoted 8 lines · Sakari Ailus
Quoted 7 lines · Ricardo Ribalda Delgado

Hi Sakari!

Quoted 4 lines
Quoted 1 line

Regarding s/dev_info/dev_warn : Do you want to send the patch or I should do it?

Is this one merged already? If yes, sure I can. Otherwise a new version would be nice.

It is at least here

Quoted 8 lines
Quoted 5 lines

https://git.kernel.org/cgit/linux/kernel/git/cooloney/linux-leds.git/commit/?h=devel&id=0767f8c5c53018f8cc7d87871abb7d3290be7596

and here

https://git.kernel.org/cgit/linux/kernel/git/cooloney/linux-leds.git/commit/?h=for-next&id=0767f8c5c53018f8cc7d87871abb7d3290be7596

I'll send a patch. Thanks!

Cool, please!
Thanks,
-Bryan

#7 lore raw
In reply to Ricardo Ribalda Delgado

Hi Ricardo,

drivers/leds/led-class.c static const struct dev_pm_ops leds_class_dev_pm_ops = { · line 247

Quoted 12 lines · Ricardo Ribalda Delgado
drivers/leds/led-class.c +5 -2
@@ -219,12 +239,19 @@ static const struct dev_pm_ops leds_class_dev_pm_ops = {
  */
 int led_classdev_register(struct device *parent, struct led_classdev *led_cdev)
 {
+	char name[64];
+	int ret;
+
+	ret = led_classdev_next_name(led_cdev->name, name, sizeof(name));
 	led_cdev->dev = device_create_with_groups(leds_class, parent, 0,
-					led_cdev, led_cdev->groups,
-					"%s", led_cdev->name);
+					led_cdev, led_cdev->groups, name);

I just realised there was another issue --- the name is now interpreted as format string. Bad things will happen if there's e.g. %s in the name itself --- perhaps unlikely, but possible.

Now that I was about to submit a patch, I'll submit one for this one as well. :-)

Signature · 4 lines
Regards,

Sakari Ailus
e-mail: [email protected]	XMPP: [email protected]
#8 lore raw

Hi Sakari

cc adding Greg (core and FormatGuard) and Chistopher (sparse)
Quoted 4 lines

I just realised there was another issue --- the name is now interpreted as format string. Bad things will happen if there's e.g. %s in the name itself --- perhaps unlikely, but possible.

Good catch!

Would it be possible to add a sparse check to avoid this in all the kernel?

And what about a macro protection like FormatGuard?

https://www.usenix.org/legacy/events/sec01/full_papers/cowanbarringer/cowanbarringer.pdf

Thanks!

Signature · 1 line
Ricardo Ribalda
#9 lore raw
Quoted 8 lines · Ricardo Ribalda Delgado

Hi Sakari

cc adding Greg (core and FormatGuard) and Chistopher (sparse)
Quoted 4 lines

I just realised there was another issue --- the name is now interpreted as format string. Bad things will happen if there's e.g. %s in the name itself --- perhaps unlikely, but possible.

Quoted 9 lines

Good catch!

Would it be possible to add a sparse check to avoid this in all the kernel?

And what about a macro protection like FormatGuard?

https://www.usenix.org/legacy/events/sec01/full_papers/cowanbarringer/cowanbarringer.pdf

I think Fengguang's 0-DAY kernel test infrastructure can help this. -Bryan

#10 lore raw

+ Kees Cook

Quoted 14 lines · Bryan Wu
Quoted 12 lines · Ricardo Ribalda Delgado

Hi Sakari

cc adding Greg (core and FormatGuard) and Chistopher (sparse)
Quoted 4 lines

I just realised there was another issue --- the name is now interpreted as format string. Bad things will happen if there's e.g. %s in the name itself --- perhaps unlikely, but possible.

Good catch!

Would it be possible to add a sparse check to avoid this in all the kernel?

Quoted 7 lines
Quoted 5 lines

And what about a macro protection like FormatGuard?

https://www.usenix.org/legacy/events/sec01/full_papers/cowanbarringer/cowanbarringer.pdf

I think Fengguang's 0-DAY kernel test infrastructure can help this.

Kees' format-security branch has a check on dynamic printf format string, which has been effective in finding errors like:

drivers/tty/serial/sb1250-duart.c: In function 'sbd_map_port':

Quoted 1 line
Quoted 1 line

drivers/tty/serial/sb1250-duart.c:680:3: error: format not a string literal and no format arguments [-Werror=format-security]

      printk(err);                                                                                                              
      ^           

I wonder if Kees has the plan to include the patch into upstream and make it a kconfig option. For your convenience, the patch is pasted below.

Thanks,
Fengguang
---

commit 95420c349194d1b570270ba1b1567d85461761c3

Author:     Kees Cook <[email protected]>
AuthorDate: Mon Sep 16 11:15:54 2013 -0700
Commit:     Kees Cook <[email protected]>
CommitDate: Wed Mar 4 14:07:18 2015 -0800

Make all format string problems fail the build

In an effort to stop format strings from leaking into various callers, have gcc stop the build when this gets detected.

Makefile +5 -1
index e6a9b1b..b7684d2 100644
@@ -402,7 +402,6 @@ KBUILD_CPPFLAGS := -D__KERNEL__
 KBUILD_CFLAGS   := -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs \
                   -fno-strict-aliasing -fno-common \
                   -Werror-implicit-function-declaration \
-                  -Wno-format-security \
                   -std=gnu89
 
 KBUILD_AFLAGS_KERNEL :=
@@ -752,6 +751,11 @@ endif
 NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include)
 CHECKFLAGS     += $(NOSTDINC_FLAGS)
 
+# Enable format-security when it can stop the build, otherwise disable.
+KBUILD_CFLAGS  += $(call cc-option,\
+                       -Wformat -Wformat-security -Werror=format-security,\
+                       -Wno-format-security)
+
 # warn about C99 declaration after statement
 KBUILD_CFLAGS += $(call cc-option,-Wdeclaration-after-statement,)
#14 lore raw
In reply to Ricardo Ribalda Delgado
Quoted 26 lines · Ricardo Ribalda Delgado

The current code expected that every LED had an unique name. This is a legit expectation when the device tree can no be modified or extended. But with device tree overlays this requirement can be easily broken.

This patch finds out if the name is already in use and adds the suffix _1, _2... if not.

---
v3: Suggested by Bryan Wu <[email protected]>

(quoted diff) +0 -1
-Do not allocate dynamically the name
 drivers/leds/led-class.c | 31 +++++++++++++++++++++++++++++--
 1 file changed, 29 insertions(+), 2 deletions(-)
drivers/leds/led-class.c +3 -0
index 768d33a..4ca37b8 100644
@@ -212,6 +212,26 @@ static const struct dev_pm_ops leds_class_dev_pm_ops = {
        .resume         = led_resume,
 };
 
+static int match_name(struct device *dev, const void *data)
+{
+       if (!dev_name(dev))

drivers/leds/led-class.c static const struct dev_pm_ops leds_class_dev_pm_ops = { · line 227

Quoted 10 lines
drivers/leds/led-class.c +10 -0
+               return 0;
+       return !strcmp(dev_name(dev), (char *)data);
+}
+
+static int led_classdev_next_name(const char *init_name, char *name,
+                                 size_t len)
+{
+       int i = 0;
+
+       strncpy(name, init_name, len);

What's the maximum length of init_name?
strncpy() is _not_ guaranteed to zero-terminate the destination buffer.

There are two ways to fix this:
1. Use strlcpy() instead of strncpy(),
2. Explicitly set name[len-1] to 0 after the call to strncpy().

As we don't need the "security" feature of strncpy() that fills the remaining part of the buffer with zeroes, I think using strlcpy() is the best solution.

drivers/leds/led-class.c static const struct dev_pm_ops leds_class_dev_pm_ops = { · line 230

Quoted 2 lines
drivers/leds/led-class.c +2 -0
+       while (class_find_device(leds_class, NULL, name, match_name))
+               snprintf(name, len, "%s_%d", init_name, ++i);

This will become an infinite loop once the resulting string no longer fits in the target buffer. Hence the loop should be terminated, and an error code should be returned, once snprintf() returns a value >= len.

drivers/leds/led-class.c static const struct dev_pm_ops leds_class_dev_pm_ops = { · line 233

Quoted 3 lines
drivers/leds/led-class.c +3 -0
+
+       return i;
+}

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#15 lore raw

Hello Geert:

Thanks for your comments!

Quoted 2 lines · Geert Uytterhoeven

What's the maximum length of init_name?

It is "driver defined", this is why the initial patch used kasprintf, but the Maintainer preferred to avoid the memory handling.

Quoted 1 line

strncpy() is _not_ guaranteed to zero-terminate the destination buffer.

Good catch. I will fix this.

Quoted 8 lines

There are two ways to fix this:
1. Use strlcpy() instead of strncpy(),
2. Explicitly set name[len-1] to 0 after the call to strncpy().

As we don't need the "security" feature of strncpy() that fills the remaining part of the buffer with zeroes, I think using strlcpy() is the best solution.

Quoted 7 lines

drivers/leds/led-class.c static const struct dev_pm_ops leds_class_dev_pm_ops = { · line 230

Quoted 2 lines
drivers/leds/led-class.c +2 -0
+       while (class_find_device(leds_class, NULL, name, match_name))
+               snprintf(name, len, "%s_%d", init_name, ++i);

This will become an infinite loop once the resulting string no longer fits in the target buffer. Hence the loop should be terminated, and an error code should be returned, once snprintf() returns a value >= len.

Will send a patch right away. Thanks

Quoted 14 lines

drivers/leds/led-class.c static const struct dev_pm_ops leds_class_dev_pm_ops = { · line 233

Quoted 3 lines
drivers/leds/led-class.c +3 -0
+
+       return i;
+}

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

Signature · 1 line
Ricardo Ribalda
#16 lore raw
In reply to Ricardo Ribalda Delgado

Hi Ricardo,

I forgot one thing:

Quoted 7 lines · Ricardo Ribalda Delgado
drivers/leds/led-class.c +1 -0
@@ -212,6 +212,26 @@ static const struct dev_pm_ops leds_class_dev_pm_ops = {
        .resume         = led_resume,
 };
 
+static int match_name(struct device *dev, const void *data)

drivers/leds/led-class.c static const struct dev_pm_ops leds_class_dev_pm_ops = { · line 225

Quoted 10 lines
drivers/leds/led-class.c +10 -0
+{
+       if (!dev_name(dev))
+               return 0;
+       return !strcmp(dev_name(dev), (char *)data);
+}
+
+static int led_classdev_next_name(const char *init_name, char *name,
+                                 size_t len)
+{
+       int i = 0;

unsigned int

drivers/leds/led-class.c static const struct dev_pm_ops leds_class_dev_pm_ops = { · line 230

Quoted 5 lines
drivers/leds/led-class.c +5 -0
+
+       strncpy(name, init_name, len);
+
+       while (class_find_device(leds_class, NULL, name, match_name))
+               snprintf(name, len, "%s_%d", init_name, ++i);

%u

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#11 lore raw
In reply to Fengguang Wu
Quoted 68 lines · Fengguang Wu

+ Kees Cook

Quoted 14 lines · Bryan Wu
Quoted 12 lines · Ricardo Ribalda Delgado

Hi Sakari

cc adding Greg (core and FormatGuard) and Chistopher (sparse)
Quoted 4 lines

I just realised there was another issue --- the name is now interpreted as format string. Bad things will happen if there's e.g. %s in the name itself --- perhaps unlikely, but possible.

Good catch!

Would it be possible to add a sparse check to avoid this in all the kernel?

Quoted 7 lines
Quoted 5 lines

And what about a macro protection like FormatGuard?

https://www.usenix.org/legacy/events/sec01/full_papers/cowanbarringer/cowanbarringer.pdf

I think Fengguang's 0-DAY kernel test infrastructure can help this.

Kees' format-security branch has a check on dynamic printf format string, which has been effective in finding errors like:

drivers/tty/serial/sb1250-duart.c: In function 'sbd_map_port':

Quoted 1 line
Quoted 1 line

drivers/tty/serial/sb1250-duart.c:680:3: error: format not a string literal and no format arguments [-Werror=format-security]

      printk(err);
      ^

I wonder if Kees has the plan to include the patch into upstream and make it a kconfig option. For your convenience, the patch is pasted below.

Thanks,
Fengguang
---

commit 95420c349194d1b570270ba1b1567d85461761c3

Author:     Kees Cook <[email protected]>
AuthorDate: Mon Sep 16 11:15:54 2013 -0700
Commit:     Kees Cook <[email protected]>
CommitDate: Wed Mar 4 14:07:18 2015 -0800

Make all format string problems fail the build

In an effort to stop format strings from leaking into various callers, have gcc stop the build when this gets detected.

Makefile +0 -1
index e6a9b1b..b7684d2 100644
@@ -402,7 +402,6 @@ KBUILD_CPPFLAGS := -D__KERNEL__
 KBUILD_CFLAGS   := -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs \
                   -fno-strict-aliasing -fno-common \
                   -Werror-implicit-function-declaration \
-                  -Wno-format-security \
                   -std=gnu89
 
 KBUILD_AFLAGS_KERNEL :=
@@ -752,6 +751,11 @@ endif
 NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include)

Makefile endif · line 761

Quoted 10 lines
Makefile +5 -0
 CHECKFLAGS     += $(NOSTDINC_FLAGS)
 
+# Enable format-security when it can stop the build, otherwise disable.
+KBUILD_CFLAGS  += $(call cc-option,\
+                       -Wformat -Wformat-security -Werror=format-security,\
+                       -Wno-format-security)
+
 # warn about C99 declaration after statement
 KBUILD_CFLAGS += $(call cc-option,-Wdeclaration-after-statement,)
 

The trouble is that gcc still has a terrible time with false positives on these warnings. It doesn't know to silence const char strings. For example:

const char * version = "SomeThing driver v1.3\n";
...
printk(version);

We don't care about this, since the string is not dynamic, but gcc still warns. My intention is that when gcc fixes this bug, then I'd upstream this patch. Right now I have to carry a patch to silence false positives. :(

You can see it here:
http://git.kernel.org/cgit/linux/kernel/git/kees/linux.git/log/?h=format-security

-Kees

Signature · 2 lines
Kees Cook
Chrome OS Security
#12 lore raw
Quoted 42 lines · Kees Cook
Quoted 27 lines

commit 95420c349194d1b570270ba1b1567d85461761c3

Author:     Kees Cook <[email protected]>
AuthorDate: Mon Sep 16 11:15:54 2013 -0700
Commit:     Kees Cook <[email protected]>
CommitDate: Wed Mar 4 14:07:18 2015 -0800

Make all format string problems fail the build

In an effort to stop format strings from leaking into various callers, have gcc stop the build when this gets detected.

Makefile +0 -1
index e6a9b1b..b7684d2 100644
@@ -402,7 +402,6 @@ KBUILD_CPPFLAGS := -D__KERNEL__
 KBUILD_CFLAGS   := -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs \
                   -fno-strict-aliasing -fno-common \
                   -Werror-implicit-function-declaration \
-                  -Wno-format-security \
                   -std=gnu89
 
 KBUILD_AFLAGS_KERNEL :=
@@ -752,6 +751,11 @@ endif
 NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include)

Makefile endif · line 761

Quoted 10 lines
Makefile +5 -0
 CHECKFLAGS     += $(NOSTDINC_FLAGS)
 
+# Enable format-security when it can stop the build, otherwise disable.
+KBUILD_CFLAGS  += $(call cc-option,\
+                       -Wformat -Wformat-security -Werror=format-security,\
+                       -Wno-format-security)
+
 # warn about C99 declaration after statement
 KBUILD_CFLAGS += $(call cc-option,-Wdeclaration-after-statement,)
 

The trouble is that gcc still has a terrible time with false positives on these warnings. It doesn't know to silence const char strings. For example:

Quoted 1 line

const char * version = "SomeThing driver v1.3\n";

While the contents of the string are const, the pointer isn't...

Quoted 2 lines

...
printk(version);

... hence gcc cannot be sure version hasn't been changed to point to something different.

Quoted 4 lines

We don't care about this, since the string is not dynamic, but gcc still warns. My intention is that when gcc fixes this bug, then I'd upstream this patch. Right now I have to carry a patch to silence false positives. :(

Does it help if you change it to:

    const char * const version = "SomeThing driver v1.3\n";

???

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

#13 lore raw
Quoted 53 lines · Geert Uytterhoeven
Quoted 42 lines · Kees Cook
Quoted 27 lines

commit 95420c349194d1b570270ba1b1567d85461761c3

Author:     Kees Cook <[email protected]>
AuthorDate: Mon Sep 16 11:15:54 2013 -0700
Commit:     Kees Cook <[email protected]>
CommitDate: Wed Mar 4 14:07:18 2015 -0800

Make all format string problems fail the build

In an effort to stop format strings from leaking into various callers, have gcc stop the build when this gets detected.

Makefile +0 -1
index e6a9b1b..b7684d2 100644
@@ -402,7 +402,6 @@ KBUILD_CPPFLAGS := -D__KERNEL__
 KBUILD_CFLAGS   := -Wall -Wundef -Wstrict-prototypes -Wno-trigraphs \
                   -fno-strict-aliasing -fno-common \
                   -Werror-implicit-function-declaration \
-                  -Wno-format-security \
                   -std=gnu89
 
 KBUILD_AFLAGS_KERNEL :=
@@ -752,6 +751,11 @@ endif
 NOSTDINC_FLAGS += -nostdinc -isystem $(shell $(CC) -print-file-name=include)

Makefile endif · line 761

Quoted 10 lines
Makefile +5 -0
 CHECKFLAGS     += $(NOSTDINC_FLAGS)
 
+# Enable format-security when it can stop the build, otherwise disable.
+KBUILD_CFLAGS  += $(call cc-option,\
+                       -Wformat -Wformat-security -Werror=format-security,\
+                       -Wno-format-security)
+
 # warn about C99 declaration after statement
 KBUILD_CFLAGS += $(call cc-option,-Wdeclaration-after-statement,)
 

The trouble is that gcc still has a terrible time with false positives on these warnings. It doesn't know to silence const char strings. For example:

Quoted 1 line

const char * version = "SomeThing driver v1.3\n";

While the contents of the string are const, the pointer isn't...

Quoted 2 lines

...
printk(version);

... hence gcc cannot be sure version hasn't been changed to point to something different.

Quoted 8 lines
Quoted 4 lines

We don't care about this, since the string is not dynamic, but gcc still warns. My intention is that when gcc fixes this bug, then I'd upstream this patch. Right now I have to carry a patch to silence false positives. :(

Does it help if you change it to:

    const char * const version = "SomeThing driver v1.3\n";

Nope. :(

Here's what I use when examining gcc's behavior:

https://gist.github.com/kees/e561143ba0bd0ca163bc

-Kees

Quoted 13 lines

???

Gr{oetje,eeting}s,

Geert

--
Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- [email protected]

In personal conversations with technical people, I call myself a hacker. But when I'm talking to journalists I just say "programmer" or something like that.
-- Linus Torvalds

Signature · 2 lines
Kees Cook
Chrome OS Security