[PATCH] tools/leds: Build the full hardware brightness path

From: Zhu Jun

Date: Fri Nov 20 2026 - 01:18:58 EST


The path includes both the LED class directory and the attribute
name, but snprintf() limits the entire result to the LED name size.
This truncates valid paths and causes open() to fail. Allow room
for the whole path and check for truncation before opening it.

Fixes: ae3473231e77 ("tools/leds: Add led_hw_brightness_mon program")
Signed-off-by: Zhu Jun <zhujun2@xxxxxxxxxxxxxxxxxxxx>
---
tools/leds/led_hw_brightness_mon.c | 13 ++++++++-----
1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/tools/leds/led_hw_brightness_mon.c b/tools/leds/led_hw_brightness_mon.c
index eb65ae988839..c26be496a00d 100644
--- a/tools/leds/led_hw_brightness_mon.c
+++ b/tools/leds/led_hw_brightness_mon.c
@@ -14,6 +14,7 @@

#include <errno.h>
#include <fcntl.h>
+#include <limits.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
@@ -21,12 +22,10 @@
#include <time.h>
#include <unistd.h>

-#include <linux/uleds.h>
-
int main(int argc, char const *argv[])
{
int fd, ret;
- char brightness_file_path[LED_MAX_NAME_SIZE + 11];
+ char brightness_file_path[PATH_MAX];
struct pollfd pollfd;
struct timespec ts;
char buf[11];
@@ -36,8 +35,12 @@ int main(int argc, char const *argv[])
return 1;
}

- snprintf(brightness_file_path, LED_MAX_NAME_SIZE,
- "/sys/class/leds/%s/brightness_hw_changed", argv[1]);
+ ret = snprintf(brightness_file_path, sizeof(brightness_file_path),
+ "/sys/class/leds/%s/brightness_hw_changed", argv[1]);
+ if (ret < 0 || (size_t)ret >= sizeof(brightness_file_path)) {
+ fprintf(stderr, "Invalid LED brightness path\n");
+ return 1;
+ }

fd = open(brightness_file_path, O_RDONLY);
if (fd == -1) {