leds: do not overflow sysfs buffer in led_trigger_show
authorNathan Sullivan <nathan.sullivan@ni.com>
Mon, 15 Aug 2016 22:20:14 +0000 (17:20 -0500)
committerJacek Anaszewski <j.anaszewski@samsung.com>
Tue, 16 Aug 2016 19:34:30 +0000 (21:34 +0200)
Per the documentation, use scnprintf instead of sprintf to ensure there
is never more than PAGE_SIZE bytes of trigger names put into the
buffer.

Signed-off-by: Nathan Sullivan <nathan.sullivan@ni.com>
Signed-off-by: Zach Brown <zach.brown@ni.com>
Signed-off-by: Jacek Anaszewski <j.anaszewski@samsung.com>
drivers/leds/led-triggers.c

index f0b3d101994b5e411a1e8f30db1fefeb430aa8f8..59d6a3e58a3d9c72074f012b9c3cbc74bcecb1ba 100644 (file)
@@ -81,21 +81,23 @@ ssize_t led_trigger_show(struct device *dev, struct device_attribute *attr,
        down_read(&led_cdev->trigger_lock);
 
        if (!led_cdev->trigger)
-               len += sprintf(buf+len, "[none] ");
+               len += scnprintf(buf+len, PAGE_SIZE - len, "[none] ");
        else
-               len += sprintf(buf+len, "none ");
+               len += scnprintf(buf+len, PAGE_SIZE - len, "none ");
 
        list_for_each_entry(trig, &trigger_list, next_trig) {
                if (led_cdev->trigger && !strcmp(led_cdev->trigger->name,
                                                        trig->name))
-                       len += sprintf(buf+len, "[%s] ", trig->name);
+                       len += scnprintf(buf+len, PAGE_SIZE - len, "[%s] ",
+                                        trig->name);
                else
-                       len += sprintf(buf+len, "%s ", trig->name);
+                       len += scnprintf(buf+len, PAGE_SIZE - len, "%s ",
+                                        trig->name);
        }
        up_read(&led_cdev->trigger_lock);
        up_read(&triggers_list_lock);
 
-       len += sprintf(len+buf, "\n");
+       len += scnprintf(len+buf, PAGE_SIZE - len, "\n");
        return len;
 }
 EXPORT_SYMBOL_GPL(led_trigger_show);