strncpy() does not NUL-terminate when the source is at least as long
as the bound. uleds_dev.name is passed to the kernel with
write(fd, &uleds_dev, sizeof(uleds_dev)), so a >= LED_MAX_NAME_SIZE
byte argv[1] leaves name without a terminator and the write sends
trailing bytes that are not part of a valid device name.
Silently truncating such a name would create a device the user did
not ask for, so error out instead, like the other argument checks
already do.
Fixes: fa7f32422ea1 ("tools/leds: Add uledmon program for monitoring userspace LEDs")
Suggested-by: David Lechner <david@lechnology.com>
Signed-off-by: Gu Bin <gubin@cmss.chinamobile.com>
---
Changes in v3: keep strncpy() instead of switching to strcpy(), since
strcpy() would leave the tail of uleds_dev.name uninitialised and the
whole struct is passed to write(), leaking stack memory to the kernel,
as pointed out by Lee Jones.
tools/leds/uledmon.c | 6 ++++++
1 file changed, 6 insertions(+)
diff --git a/tools/leds/uledmon.c b/tools/leds/uledmon.c
index c15a39c1f2714..4b40729a13e93 100644
--- a/tools/leds/uledmon.c
+++ b/tools/leds/uledmon.c
@@ -31,6 +31,12 @@ int main(int argc, char const *argv[])
return 1;
}
+ if (strlen(argv[1]) >= LED_MAX_NAME_SIZE) {
+ fprintf(stderr, "Device name too long (max %d bytes)\n",
+ LED_MAX_NAME_SIZE - 1);
+ return 1;
+ }
+
strncpy(uleds_dev.name, argv[1], LED_MAX_NAME_SIZE);
uleds_dev.max_brightness = 100;
--
2.50.1.windows.1