Skip to contentSkip to conversation

fbdev: fix snprintf() limit in show_bl_curve()

PATCH Superseded Dan Carpenter 2 participants 4 messages Aug 21, 2015

Subsystems (3)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 fbdev: fix snprintf() limit in show_bl_curve()

The limit should be "PAGE_SIZE - len" instead of PAGE_SIZE.

drivers/video/fbdev/core/fbsysfs.c +1 -1
index 60c3f0a..827098d 100644
@@ -485,7 +485,7 @@ static ssize_t show_bl_curve(struct device *device,
 
 	mutex_lock(&fb_info->bl_curve_mutex);
 	for (i = 0; i < FB_BACKLIGHT_LEVELS; i += 8)
-		len += snprintf(&buf[len], PAGE_SIZE, "%8ph\n",
+		len += snprintf(&buf[len], PAGE_SIZE - len, "%8ph\n",
 				fb_info->bl_curve + i);
 	mutex_unlock(&fb_info->bl_curve_mutex);
 
#1 lore raw
Quoted 2 lines · Dan Carpenter

The limit should be "PAGE_SIZE - len" instead of PAGE_SIZE.

Besides that was in the original code, the problem still might happen when FB_BACKLIGHT_LEVELS is set to 171+ since snprintf() returns desired length. I suppose you would change this to check len on each iteration or change to scnprintf() if I get it correct.

Quoted 16 lines
drivers/video/fbdev/core/fbsysfs.c +1 -1
index 60c3f0a..827098d 100644
@@ -485,7 +485,7 @@ static ssize_t show_bl_curve(struct device 
 *device,
 
 	mutex_lock(&fb_info->bl_curve_mutex);
 	for (i = 0; i < FB_BACKLIGHT_LEVELS; i += 8)
-		len += snprintf(&buf[len], PAGE_SIZE, "%8ph\n",
+		len += snprintf(&buf[len], PAGE_SIZE - len, 
 "%8ph\n",
 				fb_info->bl_curve + i);

mutex_unlock(&fb_info->bl_curve_mutex);

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy
#2 lore raw
In reply to Dan Carpenter
Quoted 9 lines · Andy Shevchenko
Quoted 2 lines · Dan Carpenter

The limit should be "PAGE_SIZE - len" instead of PAGE_SIZE.

Besides that was in the original code, the problem still might happen when FB_BACKLIGHT_LEVELS is set to 171+ since snprintf() returns desired length. I suppose you would change this to check len on each iteration or change to scnprintf() if I get it correct.

Yeah, you're right.

If you pass a negative size to snprintf() it won't overflow, but it will print an error. I will send a v2 and change it to scnprintf(). I don't think we will have 171 levels so it's not worth adding the check?

regards,
dan carpenter

#3 lore raw
In reply to Dan Carpenter
Quoted 13 lines · Dan Carpenter
Quoted 11 lines · Andy Shevchenko
Quoted 2 lines · Dan Carpenter

The limit should be "PAGE_SIZE - len" instead of PAGE_SIZE.

Besides that was in the original code, the problem still might happen
when FB_BACKLIGHT_LEVELS is set to 171+ since snprintf() returns desired length. I suppose you would change this to check len on each
iteration or change to scnprintf() if I get it correct.

Quoted 7 lines

Yeah, you're right.

If you pass a negative size to snprintf() it won't overflow, but it will
print an error. I will send a v2 and change it to scnprintf(). I don't
think we will have 171 levels so it's not worth adding the check?

I think there is no need to check for that. Especially it might be changed in the future (if, for example, they decide to have 16 levels per line instead of 8).

Quoted 4 lines

regards,
dan carpenter

Signature · 2 lines
Andy Shevchenko <[email protected]>
Intel Finland Oy