sparc/led: prevent buffer underflow on zero-length write
Subsystems (2)
PATCH 1/1 sparc/led: prevent buffer underflow on zero-length write
Fix out-of-bounds access in led_proc_write() when count is 0. Accessing buf[count - 1] with count=0 reads/writes buf[-1].
Check for count==0 and return -EINVAL early to fix this.
Found via static analysis and code review.
--- arch/sparc/kernel/led.c | 3 +++ 1 file changed, 3 insertions(+)
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 ==0 ) +return -EINVAL ; +if (count >LED_MAX_LENGTH ) count =LED_MAX_LENGTH ;
Signature · 1 line
2.39.5 (Apple Git-154)
Andreas Larsson
maintainerQuoted 19 lines · Miaoqian Lin
Fix out-of-bounds access in
led_proc_write()when count is 0. Accessing buf[count - 1] with count=0 reads/writes buf[-1].Check for count==0 and return
-EINVALearly to fix this.Found via static analysis and code review.
Fixes ee1858d3122d ("[SPARC]: Add sun4m LED driver.")Signed-off-by Miaoqian Lin <[email protected]>--- arch/sparc/kernel/led.c | 3 +++ 1 file changed, 3 insertions(+)arch/sparc/kernel/led.c +0
-0
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 ==0 ) +return -EINVAL ; +if (count >LED_MAX_LENGTH ) count =LED_MAX_LENGTH ;
Thank you for the patch.
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
@@ -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