Skip to contentSkip to conversation

kernel/cpu: fix: use scnprintf or sprintf.

PATCH In review YANG LI 2 participants 2 messages Dec 22, 2020 – Dec 23, 2020 CPU hotplug

lore
Share & tools
lore

YANG LI

author
#0 lore raw

PATCH 1/1 kernel/cpu: fix: use scnprintf or sprintf.

The snprintf() function returns the number of characters which would have been printed if there were enough space, but the scnprintf() returns the number of characters which were actually printed. If the buffer is not large enough, then using snprintf() would result in a read overflow and an information leak.

---
 kernel/cpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/cpu.c +1 -1
index 4e11e91..c123741 100644
@@ -2345,7 +2345,7 @@ static ssize_t show_cpuhp_states(struct device *dev,
 {
 	const char *state = smt_states[cpu_smt_control];
 
-	return snprintf(buf, PAGE_SIZE - 2, "%s\n", state);
+	return scnprintf(buf, PAGE_SIZE - 2, "%s\n", state);
 }
 
 static ssize_t
Signature · 1 line
1.8.3.1
#1 lore raw

Hi Yang

'or sprintf' in the subject line doesn't make much sense for what's done in this patch. Perhaps you meant "Use scnprintf instead of snprintf"?

Quoted 8 lines · YANG LI

The snprintf() function returns the number of characters which would have been printed if there were enough space, but the scnprintf() returns the number of characters which were actually printed. If the buffer is not large enough, then using snprintf() would result in a read overflow and an information leak.

Two different yet very similar email addresses, it seems both are you? The Reported-by is unnecessary.

Quoted 9 lines
---
 kernel/cpu.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/cpu.c +0 -0
index 4e11e91..c123741 100644

kernel/cpu.c static ssize_t show_cpuhp_states(struct device *dev, · line 2348

Quoted 5 lines
kernel/cpu.c +1 -1
@@ -2345,7 +2345,7 @@ static ssize_t show_cpuhp_states(struct device *dev,
 {
 	const char *state = smt_states[cpu_smt_control];
 
-	return snprintf(buf, PAGE_SIZE - 2, "%s\n", state);
+	return scnprintf(buf, PAGE_SIZE - 2, "%s\n", state);

show_cpuhp_states() doesn't have snprintf() in Linus' master. Which tree is this based on?

I can see two snprintf() in cpu.c, show_smt_active/control().

Mind resend to fix them both?

Thanks

--
Qais Yousef