Skip to contentSkip to conversation

sparc: led: reject zero-length proc writes

PATCH Superseded Pengpeng Hou 2 participants 4 messages Mar 30 – Jun 15 SPARC + UltraSPARC (sparc/sparc64)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 2/4 sparc: led: reject zero-length proc writes

led_proc_write() passes count straight through memdup_user_nul() and then unconditionally inspects buf[count - 1] to strip a trailing newline. A zero-length write therefore dereferences one byte before the allocated buffer.

Reject empty writes before touching buf[count - 1].

Fixes ee1858d3122d ("[SPARC]: Add sun4m LED driver.")
---
 arch/sparc/kernel/led.c | 3 +++
 1 file changed, 3 insertions(+)
arch/sparc/kernel/led.c +3 -0
index f4fb82b019bb..b9758fdbc054 100644
@@ -70,6 +70,9 @@ static ssize_t led_proc_write(struct file *file, const char __user *buffer,
 {
 	char *buf = NULL;
 
+	if (!count)
+		return -EINVAL;
+
 	if (count > LED_MAX_LENGTH)
 		count = LED_MAX_LENGTH;
 
Signature · 1 line
2.50.1 (Apple Git-155)
maintainer
#1 lore raw
Quoted 18 lines · Pengpeng Hou

led_proc_write() passes count straight through memdup_user_nul() and then unconditionally inspects buf[count - 1] to strip a trailing newline. A zero-length write therefore dereferences one byte before the allocated buffer.

Reject empty writes before touching buf[count - 1].

Fixes ee1858d3122d ("[SPARC]: Add sun4m LED driver.")
---
 arch/sparc/kernel/led.c | 3 +++
 1 file changed, 3 insertions(+)
arch/sparc/kernel/led.c +0 -0
index f4fb82b019bb..b9758fdbc054 100644

arch/sparc/kernel/led.c static ssize_t led_proc_write(struct file *file, const char __user *buffer, · line 78

Quoted 9 lines
arch/sparc/kernel/led.c +3 -0
@@ -70,6 +70,9 @@ static ssize_t led_proc_write(struct file *file, const char __user *buffer,
 {
 	char *buf = NULL;
 
+	if (!count)
+		return -EINVAL;
+
 	if (count > LED_MAX_LENGTH)
 		count = LED_MAX_LENGTH;
 

I see no need to fail on the empty string in particular when further down we have a default case:

	} else {
		auxio_set_led(AUXIO_LED_OFF);
	}

for any string not matching particular cases.

Instead, please stop the incorrect buffer access with something like:

arch/sparc/kernel/led.c +1 -1
index f4fb82b019bb9..9b53ac1fe533d 100644
@@ -78,7 +78,7 @@ static ssize_t led_proc_write(struct file *file, const char __user *buffer,
                return PTR_ERR(buf);
 
        /* work around \n when echo'ing into proc */
-       if (buf[count - 1] == '\n')
+       if (count > 0 && buf[count - 1] == '\n')
                buf[count - 1] = '\0';
 
        /* before we change anything we want to stop any running timers,

Thanks,
Andreas

#2 lore raw

PATCH v2 1/1 sparc: led: avoid trimming a newline from empty writes

led_proc_write() duplicates up to LED_MAX_LENGTH bytes with memdup_user_nul() and then unconditionally inspects buf[count - 1] to strip a trailing newline. A zero-length write therefore reads one byte before the duplicated buffer.

The previous version rejected empty writes, but empty input already falls through to the existing default case and turns the LED off like any other unrecognized string. Preserve that behavior and only skip the newline trim when there is no input byte to inspect.

Fixes ee1858d3122d ("[SPARC]: Add sun4m LED driver.")

---
Changes since v1:
- Preserve the existing empty-write/default LED-off behavior instead of returning -EINVAL, as suggested by Andreas.
- Guard only the trailing-newline trim.

 arch/sparc/kernel/led.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/sparc/kernel/led.c +1 -1
index f4fb82b019bb..9b53ac1fe533 100644
@@ -78,7 +78,7 @@ static ssize_t led_proc_write(struct file *file, const char __user *buffer,
 		return PTR_ERR(buf);
 
 	/* work around \n when echo'ing into proc */
-	if (buf[count - 1] == '\n')
+	if (count > 0 && buf[count - 1] == '\n')
 		buf[count - 1] = '\0';
 
 	/* before we change anything we want to stop any running timers,
Signature · 1 line
2.50.1 (Apple Git-155)
maintainer
#3 lore raw

Re: [PATCH v2] sparc: led: avoid trimming a newline from empty writes

Quoted 27 lines · Pengpeng Hou

led_proc_write() duplicates up to LED_MAX_LENGTH bytes with memdup_user_nul() and then unconditionally inspects buf[count - 1] to strip a trailing newline. A zero-length write therefore reads one byte before the duplicated buffer.

The previous version rejected empty writes, but empty input already falls through to the existing default case and turns the LED off like any other unrecognized string. Preserve that behavior and only skip the newline trim when there is no input byte to inspect.

Fixes ee1858d3122d ("[SPARC]: Add sun4m LED driver.")

---
Changes since v1:
- Preserve the existing empty-write/default LED-off behavior instead of returning -EINVAL, as suggested by Andreas.
- Guard only the trailing-newline trim.

 arch/sparc/kernel/led.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
arch/sparc/kernel/led.c +0 -0
index f4fb82b019bb..9b53ac1fe533 100644

arch/sparc/kernel/led.c static ssize_t led_proc_write(struct file *file, const char __user *buffer, · line 84

Quoted 8 lines
arch/sparc/kernel/led.c +1 -1
@@ -78,7 +78,7 @@ static ssize_t led_proc_write(struct file *file, const char __user *buffer,
 		return PTR_ERR(buf);
 
 	/* work around \n when echo'ing into proc */
-	if (buf[count - 1] == '\n')
+	if (count > 0 && buf[count - 1] == '\n')
 		buf[count - 1] = '\0';
 
 	/* before we change anything we want to stop any running timers,

Picking this up to my for-next.

Thanks,
Andreas