Skip to contentSkip to conversation

leds: trigger: tty: fix overflow in ttyname show

PATCH New Lin Tzu Yang 2 participants 2 messages Dec 1

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 leds: trigger: tty: fix overflow in ttyname show

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")

---
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(-)
drivers/leds/trigger/ledtrig-tty.c +1 -1
index 3725571144..3b8b269891 100644
@@ -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;
 }
Signature · 1 line
2.47.3
#1 lore raw
Sashiko has reviewed this patch and found no issues.
Quoted 20 lines

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().

Quoted 6 lines

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")

Sashiko has reviewed this patch and found no issues.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1