Skip to contentSkip to conversation

HID: hid-sensor-custom: Use scnprintf() for avoiding potential buffer overflow

PATCH Applied Takashi Iwai 2 participants 2 messages Mar 11, 2020

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

PATCH 1/1 HID: hid-sensor-custom: Use scnprintf() for avoiding potential buffer overflow

Since snprintf() returns the would-be-output size instead of the actual output size, the succeeding calls may go beyond the given buffer limit. Fix it by replacing with scnprintf().

---
 drivers/hid/hid-sensor-custom.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/hid/hid-sensor-custom.c +3 -3
index fb827c295842..4d25577a8573 100644
@@ -313,7 +313,7 @@ static ssize_t show_value(struct device *dev, struct device_attribute *attr,
 
 			while (i < ret) {
 				if (i + attribute->size > ret) {
-					len += snprintf(&buf[len],
+					len += scnprintf(&buf[len],
 							PAGE_SIZE - len,
 							"%d ", values[i]);
 					break;
@@ -336,10 +336,10 @@ static ssize_t show_value(struct device *dev, struct device_attribute *attr,
 					++i;
 					break;
 				}
-				len += snprintf(&buf[len], PAGE_SIZE - len,
+				len += scnprintf(&buf[len], PAGE_SIZE - len,
 						"%lld ", value);
 			}
-			len += snprintf(&buf[len], PAGE_SIZE - len, "\n");
+			len += scnprintf(&buf[len], PAGE_SIZE - len, "\n");
 
 			return len;
 		} else if (input)
Signature · 1 line
2.16.4
maintainer Applied
#1 lore raw
Quoted 3 lines · Takashi Iwai

Since snprintf() returns the would-be-output size instead of the actual output size, the succeeding calls may go beyond the given buffer limit. Fix it by replacing with scnprintf().

Queued this one in for-5.6/upstream-fixes as well. Thank you Takashi,

Signature · 2 lines
Jiri Kosina
SUSE Labs