ttyname_store() keeps whatever the user writes, without a length limit. sysfs passes up to PAGE_SIZE bytes to a store callback, so the stored name can be 4096 characters long. ttyname_show() prints it back with sprintf(buf, "%s\n", ...). With a 4095-byte name that is 4097 bytes, with 4096 bytes it is 4098, both past the PAGE_SIZE sysfs buffer. Reading the attribute after writing a 4095-byte name gives: BUG: KASAN: slab-out-of-bounds in vsnprintf+0xdd8/0xdfc Write of size 1 at addr ffff00001281f000 by task cat/114 ... sprintf+0xb0/0xec ttyname_show+0xec/0x140 dev_attr_show+0x48/0xbc sysfs_kf_seq_show+0x160/0x314 followed by the "OOB write or bad count" warning from sysfs_kf_seq_show(). Use sysfs_emit(), which never writes past PAGE_SIZE. The other show callbacks in this file already use it. Fixes: fd4a641ac88f ("leds: trigger: implement a tty trigger") Cc: stable@vger.kernel.org Signed-off-by: Lin Tzu Yang --- The bug was found with a small fuzzing script and the fix was written with AI assistance (Claude); I reviewed and tested it. The patch is against leds-next; it was tested in QEMU (arm64, KASAN) on configfs-next (2e0fad9d2eae), where this file is identical, using a uleds device with the tty trigger. Before: KASAN reports and the sysfs warning on reading back a 4095- or 4096-byte ttyname. After: no reports, the read returns 4095 bytes. drivers/leds/trigger/ledtrig-tty.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drivers/leds/trigger/ledtrig-tty.c b/drivers/leds/trigger/ledtrig-tty.c index 3725571144..3b8b269891 100644 --- a/drivers/leds/trigger/ledtrig-tty.c +++ b/drivers/leds/trigger/ledtrig-tty.c @@ -67,7 +67,7 @@ static ssize_t ttyname_show(struct device *dev, return completion; if (trigger_data->ttyname) - len = sprintf(buf, "%s\n", trigger_data->ttyname); + len = sysfs_emit(buf, "%s\n", trigger_data->ttyname); return len; } -- 2.47.3