KMSAN: kernel-usb-infoleak in usbtmc_write
syzbot
syzbotHello,
syzbot found the following issue on:
HEAD commit: 28bbe4ea686a Merge tag 'i2c-for-6.11-rc1-second-batch' of .. git tree: upstream console output: https://syzkaller.appspot.com/x/log.txt?x=1519f2b1980000 kernel config: https://syzkaller.appspot.com/x/.config?x=fcd331f2f7452dee dashboard link: https://syzkaller.appspot.com/bug?extid=9d34f80f841e948c3fdb compiler: Debian clang version 15.0.6, GNU ld (GNU Binutils for Debian) 2.40 userspace arch: i386
Unfortunately, I don't have any reproducer for this issue yet.
Downloadable assets:
disk image: https://storage.googleapis.com/syzbot-assets/5547f17fee20/disk-28bbe4ea.raw.xz
vmlinux: https://storage.googleapis.com/syzbot-assets/d509f262f4fb/vmlinux-28bbe4ea.xz
kernel image: https://storage.googleapis.com/syzbot-assets/1b4cf478d209/bzImage-28bbe4ea.xz
IMPORTANT: if you fix the issue, please add the following tag to the commit:
=====================================================
BUG: KMSAN: kernel-usb-infoleak in usb_submit_urb+0x597/0x2350 drivers/usb/core/urb.c:430 usb_submit_urb+0x597/0x2350 drivers/usb/core/urb.c:430 usbtmc_write+0xc32/0x1220 drivers/usb/class/usbtmc.c:1606 vfs_write+0x493/0x1550 fs/read_write.c:588 ksys_write+0x20f/0x4c0 fs/read_write.c:643 __do_sys_write fs/read_write.c:655 [inline] __se_sys_write fs/read_write.c:652 [inline] __ia32_sys_write+0x91/0xe0 fs/read_write.c:652 ia32_sys_call+0x2e34/0x40d0 arch/x86/include/generated/asm/syscalls_32.h:5 do_syscall_32_irqs_on arch/x86/entry/common.c:165 [inline] __do_fast_syscall_32+0xb0/0x110 arch/x86/entry/common.c:386 do_fast_syscall_32+0x38/0x80 arch/x86/entry/common.c:411 do_SYSENTER_32+0x1f/0x30 arch/x86/entry/common.c:449 entry_SYSENTER_compat_after_hwframe+0x84/0x8e
Uninit was created at:
slab_post_alloc_hook mm/slub.c:3994 [inline] slab_alloc_node mm/slub.c:4037 [inline] __kmalloc_cache_noprof+0x4f0/0xb00 mm/slub.c:4184 kmalloc_noprof include/linux/slab.h:681 [inline] usbtmc_create_urb drivers/usb/class/usbtmc.c:757 [inline] usbtmc_write+0x3d3/0x1220 drivers/usb/class/usbtmc.c:1547 vfs_write+0x493/0x1550 fs/read_write.c:588 ksys_write+0x20f/0x4c0 fs/read_write.c:643 __do_sys_write fs/read_write.c:655 [inline] __se_sys_write fs/read_write.c:652 [inline] __ia32_sys_write+0x91/0xe0 fs/read_write.c:652 ia32_sys_call+0x2e34/0x40d0 arch/x86/include/generated/asm/syscalls_32.h:5 do_syscall_32_irqs_on arch/x86/entry/common.c:165 [inline] __do_fast_syscall_32+0xb0/0x110 arch/x86/entry/common.c:386 do_fast_syscall_32+0x38/0x80 arch/x86/entry/common.c:411 do_SYSENTER_32+0x1f/0x30 arch/x86/entry/common.c:449 entry_SYSENTER_compat_after_hwframe+0x84/0x8e
Byte 3587 of 3588 is uninitialized
Memory access of size 3588 starts at ffff88804d338000
CPU: 0 UID: 0 PID: 5392 Comm: syz.1.48 Not tainted 6.10.0-syzkaller-12084-g28bbe4ea686a #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 06/27/2024 =====================================================
---
This report is generated by a bot. It may contain errors. See https://goo.gl/tpsmEJ for more information about syzbot. syzbot engineers can be reached at [email protected].
syzbot will keep track of this issue. See:
https://goo.gl/tpsmEJ#status for how to communicate with syzbot.
If the report is already addressed, let syzbot know by replying with:
#syz fix: exact-commit-title
If you want to overwrite report's subsystems, reply with:
#syz set subsystems: new-subsystem
(See the list of subsystem names on the web dashboard)
If the report is a duplicate of another one, reply with:
#syz dup: exact-subject-of-another-report
If you want to undo deduplication, reply with:
#syz undup
syzbot
syzbotsyzbot has found a reproducer for the following issue on:
syzbot has found a reproducer for the following issue on:
HEAD commit: 88fac17500f4 Merge tag 'fuse-fixes-6.11-rc7' of git://git... git tree: upstream console+strace: https://syzkaller.appspot.com/x/log.txt?x=12281339980000 kernel config: https://syzkaller.appspot.com/x/.config?x=35c699864e165c51 dashboard link: https://syzkaller.appspot.com/bug?extid=9d34f80f841e948c3fdb compiler: Debian clang version 15.0.6, GNU ld (GNU Binutils for Debian) 2.40 syz repro: https://syzkaller.appspot.com/x/repro.syz?x=1702531f980000 C reproducer: https://syzkaller.appspot.com/x/repro.c?x=150dd8af980000
Downloadable assets:
disk image: https://storage.googleapis.com/syzbot-assets/6e5a9ba13ba0/disk-88fac175.raw.xz
vmlinux: https://storage.googleapis.com/syzbot-assets/229238ec073e/vmlinux-88fac175.xz
kernel image: https://storage.googleapis.com/syzbot-assets/64327bdcda24/bzImage-88fac175.xz
IMPORTANT: if you fix the issue, please add the following tag to the commit:
=====================================================
BUG: KMSAN: kernel-usb-infoleak in usb_submit_urb+0x597/0x2350 drivers/usb/core/urb.c:430 usb_submit_urb+0x597/0x2350 drivers/usb/core/urb.c:430 usbtmc_write+0xc32/0x1220 drivers/usb/class/usbtmc.c:1606 vfs_write+0x493/0x1550 fs/read_write.c:588 ksys_write+0x20f/0x4c0 fs/read_write.c:643 __do_sys_write fs/read_write.c:655 [inline] __se_sys_write fs/read_write.c:652 [inline] __x64_sys_write+0x93/0xe0 fs/read_write.c:652 x64_sys_call+0x306a/0x3ba0 arch/x86/include/generated/asm/syscalls_64.h:2 do_syscall_x64 arch/x86/entry/common.c:52 [inline] do_syscall_64+0xcd/0x1e0 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x77/0x7f
Uninit was created at:
slab_post_alloc_hook mm/slub.c:3994 [inline] slab_alloc_node mm/slub.c:4037 [inline] __kmalloc_cache_noprof+0x4f0/0xb00 mm/slub.c:4184 kmalloc_noprof include/linux/slab.h:681 [inline] usbtmc_create_urb drivers/usb/class/usbtmc.c:757 [inline] usbtmc_write+0x3d3/0x1220 drivers/usb/class/usbtmc.c:1547 vfs_write+0x493/0x1550 fs/read_write.c:588 ksys_write+0x20f/0x4c0 fs/read_write.c:643 __do_sys_write fs/read_write.c:655 [inline] __se_sys_write fs/read_write.c:652 [inline] __x64_sys_write+0x93/0xe0 fs/read_write.c:652 x64_sys_call+0x306a/0x3ba0 arch/x86/include/generated/asm/syscalls_64.h:2 do_syscall_x64 arch/x86/entry/common.c:52 [inline] do_syscall_64+0xcd/0x1e0 arch/x86/entry/common.c:83 entry_SYSCALL_64_after_hwframe+0x77/0x7f
Byte 15 of 16 is uninitialized
Memory access of size 16 starts at ffff88810bce7000
CPU: 0 UID: 0 PID: 5229 Comm: syz-executor195 Not tainted 6.11.0-rc6-syzkaller-00026-g88fac17500f4 #0 Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 08/06/2024 =====================================================
---
If you want syzbot to run the reproducer, reply with:
#syz test: git://repo/address.git branch-or-commit-hash
If you attach or paste a git patch, syzbot will apply it before testing.
prevent kernel-infoleak.
We need to clear the structure before filling fields.
#syz test
drivers/usb/class/usbtmc.c +1 -0
@@ -759,6 +759,7 @@ static struct urb *usbtmc_create_urb(void)usb_free_urb (urb);return NULL ; } +memset (dmabuf,0 , bufsize); urb->transfer_buffer = dmabuf; urb->transfer_buffer_length = bufsize;
syzbot has tested the proposed patch and the reproducer did not trigger any issue:
Hello,
syzbot has tested the proposed patch and the reproducer did not trigger any issue:
Tested on:
commit: 88fac175 Merge tag 'fuse-fixes-6.11-rc7' of git://git... git tree: upstream console output: https://syzkaller.appspot.com/x/log.txt?x=14ac62ab980000 kernel config: https://syzkaller.appspot.com/x/.config?x=35c699864e165c51 dashboard link: https://syzkaller.appspot.com/bug?extid=9d34f80f841e948c3fdb compiler: Debian clang version 15.0.6, GNU ld (GNU Binutils for Debian) 2.40 patch: https://syzkaller.appspot.com/x/patch.diff?x=17bf7f2f980000
Note: testing is done by a robot and is best-effort only.
PATCH 1/1 USB: usbtmc: prevent kernel-infoleak
In reply to syzbotThe syzbot reported a kernel-usb-infoleak in usbtmc_write, we need to clear the structure before filling fields.
--- drivers/usb/class/usbtmc.c | 1 + 1 file changed, 1 insertion(+)
drivers/usb/class/usbtmc.c +1 -0
@@ -759,6 +759,7 @@ static struct urb *usbtmc_create_urb(void)usb_free_urb (urb);return NULL ; } +memset (dmabuf,0 , bufsize); urb->transfer_buffer = dmabuf; urb->transfer_buffer_length = bufsize;
Signature · 1 line
2.43.0
Greg KH
maintainerRe: [PATCH] USB: usbtmc: prevent kernel-infoleak
Quoted 2 lines · Edward Adam Davis
The syzbot reported a kernel-usb-infoleak in
usbtmc_write, we need to clear the structure before filling fields.
Really?
Quoted 14 lines
Fixes 4ddc645f40e9 ("usb: usbtmc: Add ioctl for vendor specific write")Reported-and-tested-by [email protected]Signed-off-by Edward Adam Davis <[email protected]>--- drivers/usb/class/usbtmc.c | 1 + 1 file changed, 1 insertion(+)drivers/usb/class/usbtmc.c +0
-0
drivers/usb/class/usbtmc.c static struct urb *usbtmc_create_urb(void) · line 762
Quoted 4 lines
drivers/usb/class/usbtmc.c +1
-0@@ -759,6 +759,7 @@ static struct urb *usbtmc_create_urb(void)usb_free_urb (urb);return NULL ; } +memset (dmabuf,0 , bufsize);
To do this simpler, kzmalloc() above this would be nice.
But, this feels odd, where is the data leaking from? This is used for both the read and write path, but where is the leak happening? A short read? If so, we need to properly truncate the buffer being sent to userspace and not send the unread data. If a short write, that makes no sense.
So this needs a bit more work, please find the real issue here and don't paper over the problem with "set it all to 0" like this.
thanks,
greg k-h
Greg KH
maintainerRe: [PATCH] USB: usbtmc: prevent kernel-infoleak
Quoted 28 lines · Greg KH
Quoted 2 lines · Edward Adam Davis
The syzbot reported a kernel-usb-infoleak in
usbtmc_write, we need to clear the structure before filling fields.Really?
Quoted 14 lines
Fixes 4ddc645f40e9 ("usb: usbtmc: Add ioctl for vendor specific write")Reported-and-tested-by [email protected]Signed-off-by Edward Adam Davis <[email protected]>--- drivers/usb/class/usbtmc.c | 1 + 1 file changed, 1 insertion(+)drivers/usb/class/usbtmc.c +0
-0drivers/usb/class/usbtmc.c static struct urb *usbtmc_create_urb(void) · line 762
Quoted 4 lines
drivers/usb/class/usbtmc.c +1
-0@@ -759,6 +759,7 @@ static struct urb *usbtmc_create_urb(void)usb_free_urb (urb);return NULL ; } +memset (dmabuf,0 , bufsize);To do this simpler,
kzmalloc()above this would be nice.
Quoted 5 lines
But, this feels odd, where is the data leaking from? This is used for both the read and write path, but where is the leak happening? A short read? If so, we need to properly truncate the buffer being sent to userspace and not send the unread data. If a short write, that makes no sense.
I looked at the report and this seems to be data sent to the device, so somehow we aren't setting the length to send to the device correctly.
Good luck!
greg k-h
Re: [PATCH] USB: usbtmc: prevent kernel-infoleak
Quoted 29 lines · Greg KH
Quoted 28 lines · Greg KH
Quoted 2 lines · Edward Adam Davis
The syzbot reported a kernel-usb-infoleak in
usbtmc_write, we need to clear the structure before filling fields.Really?
Quoted 14 lines
Fixes 4ddc645f40e9 ("usb: usbtmc: Add ioctl for vendor specific write")Reported-and-tested-by [email protected]Signed-off-by Edward Adam Davis <[email protected]>--- drivers/usb/class/usbtmc.c | 1 + 1 file changed, 1 insertion(+)drivers/usb/class/usbtmc.c +0
-0drivers/usb/class/usbtmc.c static struct urb *usbtmc_create_urb(void) · line 762
Quoted 4 lines
drivers/usb/class/usbtmc.c +1
-0@@ -759,6 +759,7 @@ static struct urb *usbtmc_create_urb(void)usb_free_urb (urb);return NULL ; } +memset (dmabuf,0 , bufsize);To do this simpler,
kzmalloc()above this would be nice.
Quoted 5 lines
Quoted 5 lines
But, this feels odd, where is the data leaking from? This is used for both the read and write path, but where is the leak happening? A short read? If so, we need to properly truncate the buffer being sent to userspace and not send the unread data. If a short write, that makes no sense.
A short write.
Quoted 3 lines
I looked at the report and this seems to be data sent to the device, so somehow we aren't setting the length to send to the device correctly.
The length of the data passed in by the user is 3 bytes, plus a TMC header length of 12 bytes and an additional 3 bytes. The actual length of the data sent to the device is 16 bytes((3 + 12 + 3)~3 = 16).
Normally, when executing copy_from_user, the 3 bytes data passed in by the user is written after the TMC header, which initializes the first 15 bytes of the 16 bytes. But it is not yet clear why the 15th byte is not initialized. The kernel data leaked to user space reported by Syzbot should be it.
Quoted 2 lines
Good luck!
BR,
Edward
Greg KH
maintainerRe: [PATCH] USB: usbtmc: prevent kernel-infoleak
Quoted 43 lines · Edward Adam Davis
Quoted 29 lines · Greg KH
Quoted 28 lines · Greg KH
Quoted 2 lines · Edward Adam Davis
The syzbot reported a kernel-usb-infoleak in
usbtmc_write, we need to clear the structure before filling fields.Really?
Quoted 14 lines
Fixes 4ddc645f40e9 ("usb: usbtmc: Add ioctl for vendor specific write")Reported-and-tested-by [email protected]Signed-off-by Edward Adam Davis <[email protected]>--- drivers/usb/class/usbtmc.c | 1 + 1 file changed, 1 insertion(+)drivers/usb/class/usbtmc.c +0
-0drivers/usb/class/usbtmc.c static struct urb *usbtmc_create_urb(void) · line 762
Quoted 4 lines
drivers/usb/class/usbtmc.c +1
-0@@ -759,6 +759,7 @@ static struct urb *usbtmc_create_urb(void)usb_free_urb (urb);return NULL ; } +memset (dmabuf,0 , bufsize);To do this simpler,
kzmalloc()above this would be nice.Quoted 5 lines
Quoted 5 lines
But, this feels odd, where is the data leaking from? This is used for both the read and write path, but where is the leak happening? A short read? If so, we need to properly truncate the buffer being sent to userspace and not send the unread data. If a short write, that makes no sense.
A short write.
Quoted 3 lines
I looked at the report and this seems to be data sent to the device, so somehow we aren't setting the length to send to the device correctly.
The length of the data passed in by the user is 3 bytes, plus a TMC header length of 12 bytes and an additional 3 bytes. The actual length of the data sent to the device is 16 bytes((3 + 12 + 3)~3 = 16).
Quoted 5 lines
Normally, when executing
copy_from_user, the 3 bytes data passed in by the user is written after the TMC header, which initializes the first 15 bytes of the 16 bytes. But it is not yet clear why the 15th byte is not initialized. The kernel data leaked to user space reported by Syzbot should be it.
But why are we sending 16 bytes to the device? Is that the format of the message it expects? If so, that's fine, just set that byte to 0.
And as the device is the thing that is getting the kernel memory, that really isn't a big deal as we "trust" hardware once it is up and talking to the kernel.
thanks,
greg k-h
Re: [PATCH] USB: usbtmc: prevent kernel-infoleak
Quoted 44 lines · Greg KH
Quoted 43 lines · Edward Adam Davis
Quoted 29 lines · Greg KH
Quoted 28 lines · Greg KH
Quoted 2 lines · Edward Adam Davis
The syzbot reported a kernel-usb-infoleak in
usbtmc_write, we need to clear the structure before filling fields.Really?
Quoted 14 lines
Fixes 4ddc645f40e9 ("usb: usbtmc: Add ioctl for vendor specific write")Reported-and-tested-by [email protected]Signed-off-by Edward Adam Davis <[email protected]>--- drivers/usb/class/usbtmc.c | 1 + 1 file changed, 1 insertion(+)drivers/usb/class/usbtmc.c +0
-0drivers/usb/class/usbtmc.c static struct urb *usbtmc_create_urb(void) · line 762
Quoted 4 lines
drivers/usb/class/usbtmc.c +1
-0@@ -759,6 +759,7 @@ static struct urb *usbtmc_create_urb(void)usb_free_urb (urb);return NULL ; } +memset (dmabuf,0 , bufsize);To do this simpler,
kzmalloc()above this would be nice.Quoted 5 lines
Quoted 5 lines
But, this feels odd, where is the data leaking from? This is used for both the read and write path, but where is the leak happening? A short read? If so, we need to properly truncate the buffer being sent to userspace and not send the unread data. If a short write, that makes no sense.
A short write.
Quoted 3 lines
I looked at the report and this seems to be data sent to the device, so somehow we aren't setting the length to send to the device correctly.
The length of the data passed in by the user is 3 bytes, plus a TMC header length of 12 bytes and an additional 3 bytes. The actual length of the data sent to the device is 16 bytes((3 + 12 + 3)~3 = 16).
Quoted 8 lines
Quoted 5 lines
Normally, when executing
copy_from_user, the 3 bytes data passed in by the user is written after the TMC header, which initializes the first 15 bytes of the 16 bytes. But it is not yet clear why the 15th byte is not initialized. The kernel data leaked to user space reported by Syzbot should be it.But why are we sending 16 bytes to the device? Is that the format of the message it expects? If so, that's fine, just set that byte to 0.
Yes. I have set them to 0 before calling copy_from_user, and now still running tests.
https://syzkaller.appspot.com/text?tag=Patch&x=14cb8f33980000
Quoted 4 lines
And as the device is the thing that is getting the kernel memory, that really isn't a big deal as we "trust" hardware once it is up and talking to the kernel.
:)
BR,
Edward
PATCH v2 1/1 USB: usbtmc: prevent kernel-usb-infoleak
The syzbot reported a kernel-usb-infoleak in usbtmc_write.
The expression "aligned = (transfersize + (USBTMC_HEADER_SIZE + 3)) & ~3;" in usbtmcw_write() follows the following pattern:
aligned = (1 + 12 + 3) & ~3 = 16 // 3 bytes have not been initialized aligned = (2 + 12 + 3) & ~3 = 16 // 2 bytes have not been initialized aligned = (3 + 12 + 3) & ~3 = 16 // 1 byte has not been initialized aligned = (4 + 12 + 3) & ~3 = 16 // All bytes have been initialized aligned = (5 + 12 + 3) & ~3 = 20 // 3 bytes have not been initialized aligned = (6 + 12 + 3) & ~3 = 20 // 2 bytes have not been initialized aligned = (7 + 12 + 3) & ~3 = 20 // 1 byte has not been initialized aligned = (8 + 12 + 3) & ~3 = 20 // All bytes have been initialized
aligned = (9 + 12 + 3) & ~3 = 24
...
Note: #define USBTMC_HEADER_SIZE 12
This results in the buffer[USBTMC_SEAD_SIZE+transfersize] and its subsequent memory not being initialized.
The condition aligned < buflen is used to avoid out of bounds access to the buffer[USBTMC_HEADER_SIZE + transfersize] when "transfersize = buflen - USBTMC_HEADER_SIZE".
--- drivers/usb/class/usbtmc.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/usb/class/usbtmc.c +4 -0
@@ -1591,6 +1591,10 @@ static ssize_t usbtmc_write(struct file *filp, const char __user *buf, goto exit; } +if (aligned < buflen && (transfersize %4 )) +memset (&buffer[USBTMC_HEADER_SIZE + transfersize],0 , + aligned -USBTMC_HEADER_SIZE - transfersize); +dev_dbg (&data->intf->dev,"%s(size:%u align:%u)\n" , __func__, (unsigned int )transfersize, (unsigned int )aligned);
Signature · 1 line
2.43.0
Re: [PATCH V2] USB: usbtmc: prevent kernel-usb-infoleak
Quoted 13 lines · Edward Adam Davis
The syzbot reported a kernel-usb-infoleak in
usbtmc_write.The expression "aligned = (transfersize + (
USBTMC_HEADER_SIZE+ 3)) & ~3;" inusbtmcw_write()follows the following pattern:aligned = (1 + 12 + 3) & ~3 = 16 // 3 bytes have not been initialized aligned = (2 + 12 + 3) & ~3 = 16 // 2 bytes have not been initialized aligned = (3 + 12 + 3) & ~3 = 16 // 1 byte has not been initialized aligned = (4 + 12 + 3) & ~3 = 16 // All bytes have been initialized aligned = (5 + 12 + 3) & ~3 = 20 // 3 bytes have not been initialized aligned = (6 + 12 + 3) & ~3 = 20 // 2 bytes have not been initialized aligned = (7 + 12 + 3) & ~3 = 20 // 1 byte has not been initialized aligned = (8 + 12 + 3) & ~3 = 20 // All bytes have been initialized
Quoted 2 lines
aligned = (9 + 12 + 3) & ~3 = 24
...
What is the purpose of aligned? Why doesn't the driver simply use USBTMC_HEADER_SIZE + transfersize instead of rounding it up to a multiple of 4?
Quoted 22 lines
Note: #define
USBTMC_HEADER_SIZE12This results in the buffer[USBTMC_SEAD_SIZE+transfersize] and its subsequent memory not being initialized.
The condition aligned < buflen is used to avoid out of bounds access to the buffer[USBTMC_HEADER_SIZE + transfersize] when "transfersize = buflen -
USBTMC_HEADER_SIZE".Fixes 4ddc645f40e9 ("usb: usbtmc: Add ioctl for vendor specific write")Reported-and-tested-by [email protected]Signed-off-by Edward Adam Davis <[email protected]>--- drivers/usb/class/usbtmc.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/usb/class/usbtmc.c +0
-0
drivers/usb/class/usbtmc.c static ssize_t usbtmc_write(struct file *filp, const char __user *buf, · line 1594
Quoted 4 lines
drivers/usb/class/usbtmc.c +1
-0@@ -1591,6 +1591,10 @@ static ssize_t usbtmc_write(struct file *filp, const char __user *buf, goto exit; } +if (aligned < buflen && (transfersize %4 ))
Shouldn't this be
if (USBTMC_HEADER_SIZE + transfersize < aligned)
?
Alan Stern
Quoted 8 lines
drivers/usb/class/usbtmc.c +3
-0+memset (&buffer[USBTMC_HEADER_SIZE + transfersize],0 , + aligned -USBTMC_HEADER_SIZE - transfersize); +dev_dbg (&data->intf->dev,"%s(size:%u align:%u)\n" , __func__, (unsigned int )transfersize, (unsigned int )aligned);Signature · 1 line
2.43.0
Re: [PATCH V2] USB: usbtmc: prevent kernel-usb-infoleak
Quoted 17 lines · Alan Stern
Quoted 13 lines · Edward Adam Davis
The syzbot reported a kernel-usb-infoleak in
usbtmc_write.The expression "aligned = (transfersize + (
USBTMC_HEADER_SIZE+ 3)) & ~3;" inusbtmcw_write()follows the following pattern:aligned = (1 + 12 + 3) & ~3 = 16 // 3 bytes have not been initialized aligned = (2 + 12 + 3) & ~3 = 16 // 2 bytes have not been initialized aligned = (3 + 12 + 3) & ~3 = 16 // 1 byte has not been initialized aligned = (4 + 12 + 3) & ~3 = 16 // All bytes have been initialized aligned = (5 + 12 + 3) & ~3 = 20 // 3 bytes have not been initialized aligned = (6 + 12 + 3) & ~3 = 20 // 2 bytes have not been initialized aligned = (7 + 12 + 3) & ~3 = 20 // 1 byte has not been initialized aligned = (8 + 12 + 3) & ~3 = 20 // All bytes have been initializedQuoted 2 lines
aligned = (9 + 12 + 3) & ~3 = 24
...
Quoted 3 lines
What is the purpose of aligned? Why doesn't the driver simply use
USBTMC_HEADER_SIZE+ transfersize instead of rounding it up to a multiple of 4?
I just found out that the logic of aligned calculation is like this. As for why it is calculated like this, perhaps Guido Kiener can provide a clearer explanation.
It was introduced by commit 4d5e18d9ed93 ("usb: usbtmc: Optimize usbtmc_write").
Quoted 26 lines
Quoted 26 lines
Note: #define
USBTMC_HEADER_SIZE12This results in the buffer[USBTMC_SEAD_SIZE+transfersize] and its subsequent memory not being initialized.
The condition aligned < buflen is used to avoid out of bounds access to the buffer[USBTMC_HEADER_SIZE + transfersize] when "transfersize = buflen -
USBTMC_HEADER_SIZE".Fixes 4ddc645f40e9 ("usb: usbtmc: Add ioctl for vendor specific write")Reported-and-tested-by [email protected]Signed-off-by Edward Adam Davis <[email protected]>--- drivers/usb/class/usbtmc.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/usb/class/usbtmc.c +0
-0@@ -1591,6 +1591,10 @@ static ssize_t usbtmc_write(struct file *filp, const char __user *buf, goto exit; }
Quoted 5 lines
drivers/usb/class/usbtmc.c static ssize_t usbtmc_write(struct file *filp, const char __user *buf, · line 1594
Quoted 1 line
drivers/usb/class/usbtmc.c +1
-0+if (aligned < buflen && (transfersize %4 ))Shouldn't this be
if (
USBTMC_HEADER_SIZE+ transfersize < aligned)
Logically, it seems possible to write it this way.
Quoted 10 lines
?
Alan Stern
drivers/usb/class/usbtmc.c static ssize_t usbtmc_write(struct file *filp, const char __user *buf, · line 1599
Quoted 5 lines
drivers/usb/class/usbtmc.c +3
-0+memset (&buffer[USBTMC_HEADER_SIZE + transfersize],0 , + aligned -USBTMC_HEADER_SIZE - transfersize); +dev_dbg (&data->intf->dev,"%s(size:%u align:%u)\n" , __func__, (unsigned int )transfersize, (unsigned int )aligned);
BR,
Edward
Re: [PATCH V2] USB: usbtmc: prevent kernel-usb-infoleak
Quoted 51 lines · Edward Adam Davis
Quoted 17 lines · Alan Stern
Quoted 13 lines · Edward Adam Davis
The syzbot reported a kernel-usb-infoleak in
usbtmc_write.The expression "aligned = (transfersize + (
USBTMC_HEADER_SIZE+ 3)) & ~3;" inusbtmcw_write()follows the following pattern:aligned = (1 + 12 + 3) & ~3 = 16 // 3 bytes have not been initialized aligned = (2 + 12 + 3) & ~3 = 16 // 2 bytes have not been initialized aligned = (3 + 12 + 3) & ~3 = 16 // 1 byte has not been initialized aligned = (4 + 12 + 3) & ~3 = 16 // All bytes have been initialized aligned = (5 + 12 + 3) & ~3 = 20 // 3 bytes have not been initialized aligned = (6 + 12 + 3) & ~3 = 20 // 2 bytes have not been initialized aligned = (7 + 12 + 3) & ~3 = 20 // 1 byte has not been initialized aligned = (8 + 12 + 3) & ~3 = 20 // All bytes have been initializedQuoted 2 lines
aligned = (9 + 12 + 3) & ~3 = 24
...Quoted 3 lines
What is the purpose of aligned? Why doesn't the driver simply use
USBTMC_HEADER_SIZE+ transfersize instead of rounding it up to a multiple of 4?I just found out that the logic of aligned calculation is like this. As for why it is calculated like this, perhaps Guido Kiener can provide a clearer explanation.
It was introduced by commit4d5e18d9ed93("usb: usbtmc: Optimizeusbtmc_write").Quoted 26 lines
Quoted 26 lines
Note: #define
USBTMC_HEADER_SIZE12This results in the buffer[USBTMC_SEAD_SIZE+transfersize] and its subsequent memory not being initialized.
The condition aligned < buflen is used to avoid out of bounds access to the buffer[USBTMC_HEADER_SIZE + transfersize] when "transfersize = buflen -
USBTMC_HEADER_SIZE".Fixes 4ddc645f40e9 ("usb: usbtmc: Add ioctl for vendor specific write")Reported-and-tested-by [email protected]Signed-off-by Edward Adam Davis <[email protected]>--- drivers/usb/class/usbtmc.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/usb/class/usbtmc.c +0
-0@@ -1591,6 +1591,10 @@ static ssize_t usbtmc_write(struct file *filp, const char __user *buf, goto exit; }
Quoted 6 lines
Quoted 5 lines
drivers/usb/class/usbtmc.c static ssize_t usbtmc_write(struct file *filp, const char __user *buf, · line 1594
Quoted 1 line
drivers/usb/class/usbtmc.c +1
-0+if (aligned < buflen && (transfersize %4 ))Shouldn't this be
if (
USBTMC_HEADER_SIZE+ transfersize < aligned)Logically, it seems possible to write it this way.
In fact, what you wrote is wrong. Consider the case where buflen is 32 and transfersize is 17. Then aligned = (12 + 17 + 3) & ~3 = 32, so your condition would fail to initialize the extra 3 bytes.
Alan Stern
Re: [PATCH V2] USB: usbtmc: prevent kernel-usb-infoleak
Quoted 56 lines · Alan Stern
Quoted 55 lines · Edward Adam Davis
Quoted 17 lines · Alan Stern
Quoted 13 lines · Edward Adam Davis
The syzbot reported a kernel-usb-infoleak in
usbtmc_write.The expression "aligned = (transfersize + (
USBTMC_HEADER_SIZE+ 3)) & ~3;" inusbtmcw_write()follows the following pattern:aligned = (1 + 12 + 3) & ~3 = 16 // 3 bytes have not been initialized aligned = (2 + 12 + 3) & ~3 = 16 // 2 bytes have not been initialized aligned = (3 + 12 + 3) & ~3 = 16 // 1 byte has not been initialized aligned = (4 + 12 + 3) & ~3 = 16 // All bytes have been initialized aligned = (5 + 12 + 3) & ~3 = 20 // 3 bytes have not been initialized aligned = (6 + 12 + 3) & ~3 = 20 // 2 bytes have not been initialized aligned = (7 + 12 + 3) & ~3 = 20 // 1 byte has not been initialized aligned = (8 + 12 + 3) & ~3 = 20 // All bytes have been initializedQuoted 2 lines
aligned = (9 + 12 + 3) & ~3 = 24
...Quoted 3 lines
What is the purpose of aligned? Why doesn't the driver simply use
USBTMC_HEADER_SIZE+ transfersize instead of rounding it up to a multiple of 4?I just found out that the logic of aligned calculation is like this. As for why it is calculated like this, perhaps Guido Kiener can provide a clearer explanation.
It was introduced by commit4d5e18d9ed93("usb: usbtmc: Optimizeusbtmc_write").Quoted 30 lines
Quoted 23 lines
Note: #define
USBTMC_HEADER_SIZE12This results in the buffer[USBTMC_SEAD_SIZE+transfersize] and its subsequent memory not being initialized.
The condition aligned < buflen is used to avoid out of bounds access to the buffer[USBTMC_HEADER_SIZE + transfersize] when "transfersize = buflen -
USBTMC_HEADER_SIZE".Fixes 4ddc645f40e9 ("usb: usbtmc: Add ioctl for vendor specific write")Reported-and-tested-by [email protected]Signed-off-by Edward Adam Davis <[email protected]>--- drivers/usb/class/usbtmc.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/usb/class/usbtmc.c +0
-0drivers/usb/class/usbtmc.c static ssize_t usbtmc_write(struct file *filp, const char __user *buf, · line 1594
Quoted 4 lines
drivers/usb/class/usbtmc.c +1
-0@@ -1591,6 +1591,10 @@ static ssize_t usbtmc_write(struct file *filp, const char __user *buf, goto exit; } +if (aligned < buflen && (transfersize %4 ))Shouldn't this be
Quoted 6 lines
Quoted 2 lines
Quoted 1 line
if (
USBTMC_HEADER_SIZE+ transfersize < aligned)Logically, it seems possible to write it this way.
In fact, what you wrote is wrong. Consider the case where buflen is 32 and transfersize is 17. Then aligned = (12 + 17 + 3) & ~3 = 32, so your condition would fail to initialize the extra 3 bytes.
The buflen is equal to USBTMC_BUFSIZE and can not equal to any other value. You can find it in usbtmc_create_urb() and usbtmc_write().
Note: #define USBTMC_BUFSIZE (4096)
BR,
Edward
Re: [PATCH V2] USB: usbtmc: prevent kernel-usb-infoleak
Quoted 60 lines · Edward Adam Davis
Quoted 59 lines · Alan Stern
Quoted 57 lines · Edward Adam Davis
Quoted 17 lines · Alan Stern
Quoted 13 lines · Edward Adam Davis
The syzbot reported a kernel-usb-infoleak in
usbtmc_write.The expression "aligned = (transfersize + (
USBTMC_HEADER_SIZE+ 3)) & ~3;" inusbtmcw_write()follows the following pattern:aligned = (1 + 12 + 3) & ~3 = 16 // 3 bytes have not been initialized aligned = (2 + 12 + 3) & ~3 = 16 // 2 bytes have not been initialized aligned = (3 + 12 + 3) & ~3 = 16 // 1 byte has not been initialized aligned = (4 + 12 + 3) & ~3 = 16 // All bytes have been initialized aligned = (5 + 12 + 3) & ~3 = 20 // 3 bytes have not been initialized aligned = (6 + 12 + 3) & ~3 = 20 // 2 bytes have not been initialized aligned = (7 + 12 + 3) & ~3 = 20 // 1 byte has not been initialized aligned = (8 + 12 + 3) & ~3 = 20 // All bytes have been initializedQuoted 2 lines
aligned = (9 + 12 + 3) & ~3 = 24
...Quoted 3 lines
What is the purpose of aligned? Why doesn't the driver simply use
USBTMC_HEADER_SIZE+ transfersize instead of rounding it up to a multiple of 4?I just found out that the logic of aligned calculation is like this. As for why it is calculated like this, perhaps Guido Kiener can provide a clearer explanation.
It was introduced by commit4d5e18d9ed93("usb: usbtmc: Optimizeusbtmc_write").Quoted 26 lines
Quoted 26 lines
Note: #define
USBTMC_HEADER_SIZE12This results in the buffer[USBTMC_SEAD_SIZE+transfersize] and its subsequent memory not being initialized.
The condition aligned < buflen is used to avoid out of bounds access to the buffer[USBTMC_HEADER_SIZE + transfersize] when "transfersize = buflen -
USBTMC_HEADER_SIZE".Fixes 4ddc645f40e9 ("usb: usbtmc: Add ioctl for vendor specific write")Reported-and-tested-by [email protected]Signed-off-by Edward Adam Davis <[email protected]>--- drivers/usb/class/usbtmc.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/usb/class/usbtmc.c +0
-0@@ -1591,6 +1591,10 @@ static ssize_t usbtmc_write(struct file *filp, const char __user *buf, goto exit; }Quoted 5 lines
drivers/usb/class/usbtmc.c static ssize_t usbtmc_write(struct file *filp, const char __user *buf, · line 1594
Quoted 1 line
drivers/usb/class/usbtmc.c +1
-0+if (aligned < buflen && (transfersize %4 ))Shouldn't this be
if (
USBTMC_HEADER_SIZE+ transfersize < aligned)Logically, it seems possible to write it this way.
Quoted 7 lines
Quoted 3 lines
In fact, what you wrote is wrong. Consider the case where buflen is 32 and transfersize is 17. Then aligned = (12 + 17 + 3) & ~3 = 32, so your condition would fail to initialize the extra 3 bytes.
The buflen is equal to
USBTMC_BUFSIZEand can not equal to any other value. You can find it inusbtmc_create_urb()andusbtmc_write().Note: #define
USBTMC_BUFSIZE(4096)
All right, so what happens if transfersize is 4081? Then aligned will be equal to 4096, so your condition would fail to initialize the extra 3 bytes.
Alan Stern
Re: [PATCH V2] USB: usbtmc: prevent kernel-usb-infoleak
Quoted 64 lines · Alan Stern
Quoted 63 lines · Edward Adam Davis
Quoted 62 lines · Alan Stern
Quoted 51 lines · Edward Adam Davis
Quoted 17 lines · Alan Stern
Quoted 13 lines · Edward Adam Davis
The syzbot reported a kernel-usb-infoleak in
usbtmc_write.The expression "aligned = (transfersize + (
USBTMC_HEADER_SIZE+ 3)) & ~3;" inusbtmcw_write()follows the following pattern:aligned = (1 + 12 + 3) & ~3 = 16 // 3 bytes have not been initialized aligned = (2 + 12 + 3) & ~3 = 16 // 2 bytes have not been initialized aligned = (3 + 12 + 3) & ~3 = 16 // 1 byte has not been initialized aligned = (4 + 12 + 3) & ~3 = 16 // All bytes have been initialized aligned = (5 + 12 + 3) & ~3 = 20 // 3 bytes have not been initialized aligned = (6 + 12 + 3) & ~3 = 20 // 2 bytes have not been initialized aligned = (7 + 12 + 3) & ~3 = 20 // 1 byte has not been initialized aligned = (8 + 12 + 3) & ~3 = 20 // All bytes have been initializedQuoted 2 lines
aligned = (9 + 12 + 3) & ~3 = 24
...Quoted 3 lines
What is the purpose of aligned? Why doesn't the driver simply use
USBTMC_HEADER_SIZE+ transfersize instead of rounding it up to a multiple of 4?I just found out that the logic of aligned calculation is like this. As for why it is calculated like this, perhaps Guido Kiener can provide a clearer explanation.
It was introduced by commit4d5e18d9ed93("usb: usbtmc: Optimizeusbtmc_write").Quoted 26 lines
Quoted 26 lines
Note: #define
USBTMC_HEADER_SIZE12This results in the buffer[USBTMC_SEAD_SIZE+transfersize] and its subsequent memory not being initialized.
The condition aligned < buflen is used to avoid out of bounds access to the buffer[USBTMC_HEADER_SIZE + transfersize] when "transfersize = buflen -
USBTMC_HEADER_SIZE".Fixes 4ddc645f40e9 ("usb: usbtmc: Add ioctl for vendor specific write")Reported-and-tested-by [email protected]Signed-off-by Edward Adam Davis <[email protected]>--- drivers/usb/class/usbtmc.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/usb/class/usbtmc.c +0
-0@@ -1591,6 +1591,10 @@ static ssize_t usbtmc_write(struct file *filp, const char __user *buf, goto exit; }Quoted 6 lines
Quoted 5 lines
drivers/usb/class/usbtmc.c static ssize_t usbtmc_write(struct file *filp, const char __user *buf, · line 1594
Quoted 1 line
drivers/usb/class/usbtmc.c +1
-0+if (aligned < buflen && (transfersize %4 ))Shouldn't this be
if (
USBTMC_HEADER_SIZE+ transfersize < aligned)Logically, it seems possible to write it this way.
In fact, what you wrote is wrong. Consider the case where buflen is 32 and transfersize is 17. Then aligned = (12 + 17 + 3) & ~3 = 32, so your condition would fail to initialize the extra 3 bytes.
Quoted 8 lines
Quoted 4 lines
The buflen is equal to
USBTMC_BUFSIZEand can not equal to any other value. You can find it inusbtmc_create_urb()andusbtmc_write().Note: #define
USBTMC_BUFSIZE(4096)All right, so what happens if transfersize is 4081? Then aligned will be equal to 4096, so your condition would fail to initialize the extra 3 bytes.
Not bad, when the transfersize values are 4081, 4082, and 4083, the aligned value is 4096, which will result in 1, 2, and 3 bytes not being initialized, respectively. I will update my patch.
BR,
Edward
PATCH v3 1/1 USB: usbtmc: prevent kernel-usb-infoleak
In reply to Alan SternThe syzbot reported a kernel-usb-infoleak in usbtmc_write.
The expression "aligned = (transfersize + (USBTMC_HEADER_SIZE + 3)) & ~3;" in usbtmcw_write() follows the following pattern:
aligned = (1 + 12 + 3) & ~3 = 16 // 3 bytes have not been initialized aligned = (2 + 12 + 3) & ~3 = 16 // 2 bytes have not been initialized aligned = (3 + 12 + 3) & ~3 = 16 // 1 byte has not been initialized aligned = (4 + 12 + 3) & ~3 = 16 // All bytes have been initialized aligned = (5 + 12 + 3) & ~3 = 20 // 3 bytes have not been initialized aligned = (6 + 12 + 3) & ~3 = 20 // 2 bytes have not been initialized aligned = (7 + 12 + 3) & ~3 = 20 // 1 byte has not been initialized aligned = (8 + 12 + 3) & ~3 = 20 // All bytes have been initialized
aligned = (9 + 12 + 3) & ~3 = 24
...
Note: #define USBTMC_HEADER_SIZE 12
This results in the buffer[USBTMC_SEAD_SIZE+transfersize] and its subsequent memory not being initialized.
---
V2 -> V3: Update condition and comments
drivers/usb/class/usbtmc.c | 4 ++++ 1 file changed, 4 insertions(+)
drivers/usb/class/usbtmc.c +4 -0
@@ -1591,6 +1591,10 @@ static ssize_t usbtmc_write(struct file *filp, const char __user *buf, goto exit; } +if (USBTMC_HEADER_SIZE + transfersize < aligned) +memset (&buffer[USBTMC_HEADER_SIZE + transfersize],0 , + aligned -USBTMC_HEADER_SIZE - transfersize); +dev_dbg (&data->intf->dev,"%s(size:%u align:%u)\n" , __func__, (unsigned int )transfersize, (unsigned int )aligned);
Signature · 1 line
2.43.0
Greg KH
maintainerRe: [PATCH V3] USB: usbtmc: prevent kernel-usb-infoleak
Quoted 36 lines · Edward Adam Davis
The syzbot reported a kernel-usb-infoleak in
usbtmc_write.The expression "aligned = (transfersize + (
USBTMC_HEADER_SIZE+ 3)) & ~3;" inusbtmcw_write()follows the following pattern:aligned = (1 + 12 + 3) & ~3 = 16 // 3 bytes have not been initialized aligned = (2 + 12 + 3) & ~3 = 16 // 2 bytes have not been initialized aligned = (3 + 12 + 3) & ~3 = 16 // 1 byte has not been initialized aligned = (4 + 12 + 3) & ~3 = 16 // All bytes have been initialized aligned = (5 + 12 + 3) & ~3 = 20 // 3 bytes have not been initialized aligned = (6 + 12 + 3) & ~3 = 20 // 2 bytes have not been initialized aligned = (7 + 12 + 3) & ~3 = 20 // 1 byte has not been initialized aligned = (8 + 12 + 3) & ~3 = 20 // All bytes have been initializedaligned = (9 + 12 + 3) & ~3 = 24
...Note: #define
USBTMC_HEADER_SIZE12This results in the buffer[USBTMC_SEAD_SIZE+transfersize] and its subsequent memory not being initialized.
Fixes 4ddc645f40e9 ("usb: usbtmc: Add ioctl for vendor specific write")Reported-and-tested-by [email protected]Signed-off-by Edward Adam Davis <[email protected]>---
V2 -> V3: Update condition and commentsdrivers/usb/class/usbtmc.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/usb/class/usbtmc.c +0
-0
drivers/usb/class/usbtmc.c static ssize_t usbtmc_write(struct file *filp, const char __user *buf, · line 1596
Quoted 6 lines
drivers/usb/class/usbtmc.c +3
-0@@ -1591,6 +1591,10 @@ static ssize_t usbtmc_write(struct file *filp, const char __user *buf, goto exit; } +if (USBTMC_HEADER_SIZE + transfersize < aligned) +memset (&buffer[USBTMC_HEADER_SIZE + transfersize],0 , + aligned -USBTMC_HEADER_SIZE - transfersize);
As this is now a pain to read/understand, and there's no comment describing it so we'll not really understand it in a few months, let alone years, how about we just do the trivial thing and make the allocation with kzalloc() to start with? And put a comment there saying why it's zeroed out.
Sorry, I thought this was going to be a lot simpler based on your first patch than this type of logic.
thanks,
greg k-h
Re: [PATCH V3] USB: usbtmc: prevent kernel-usb-infoleak
Quoted 40 lines · Greg KH
Quoted 39 lines · Edward Adam Davis
The syzbot reported a kernel-usb-infoleak in
usbtmc_write.The expression "aligned = (transfersize + (
USBTMC_HEADER_SIZE+ 3)) & ~3;" inusbtmcw_write()follows the following pattern:aligned = (1 + 12 + 3) & ~3 = 16 // 3 bytes have not been initialized aligned = (2 + 12 + 3) & ~3 = 16 // 2 bytes have not been initialized aligned = (3 + 12 + 3) & ~3 = 16 // 1 byte has not been initialized aligned = (4 + 12 + 3) & ~3 = 16 // All bytes have been initialized aligned = (5 + 12 + 3) & ~3 = 20 // 3 bytes have not been initialized aligned = (6 + 12 + 3) & ~3 = 20 // 2 bytes have not been initialized aligned = (7 + 12 + 3) & ~3 = 20 // 1 byte has not been initialized aligned = (8 + 12 + 3) & ~3 = 20 // All bytes have been initializedaligned = (9 + 12 + 3) & ~3 = 24
...Note: #define
USBTMC_HEADER_SIZE12This results in the buffer[USBTMC_SEAD_SIZE+transfersize] and its subsequent memory not being initialized.
Fixes 4ddc645f40e9 ("usb: usbtmc: Add ioctl for vendor specific write")Reported-and-tested-by [email protected]Signed-off-by Edward Adam Davis <[email protected]>---
V2 -> V3: Update condition and commentsdrivers/usb/class/usbtmc.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/usb/class/usbtmc.c +0
-0@@ -1591,6 +1591,10 @@ static ssize_t usbtmc_write(struct file *filp, const char __user *buf, goto exit; }
Quoted 9 lines
drivers/usb/class/usbtmc.c static ssize_t usbtmc_write(struct file *filp, const char __user *buf, · line 1596
Quoted 3 lines
drivers/usb/class/usbtmc.c +3
-0+if (USBTMC_HEADER_SIZE + transfersize < aligned) +memset (&buffer[USBTMC_HEADER_SIZE + transfersize],0 , + aligned -USBTMC_HEADER_SIZE - transfersize);As this is now a pain to read/understand, and there's no comment describing it so we'll not really understand it in a few months, let alone years, how about we just do the trivial thing and make the allocation with
kzalloc()to start with? And put a comment there saying why it's zeroed out.
Perhaps I wrote too much in my comments, but in essence, the logic behind this version's fix is:
When aligned is greater than (USBTMC_HEADER_SIZE+transfersize), there are (aligned - (USBTMC_HEADER_SIZE+transfersize) bytes after the header and data that have not been initialized, and these bytes are then set to 0.
Quoted 3 lines
Sorry, I thought this was going to be a lot simpler based on your first patch than this type of logic.
As you mentioned in my first version patch, this approach is simple and easy to understand, but it comes at the cost of losing the real issue, and KMSAN will not find similar problems again in the future, which is not conducive to making the program logic more robust.
BR,
Edward
Greg KH
maintainerRe: [PATCH V3] USB: usbtmc: prevent kernel-usb-infoleak
Quoted 56 lines · Edward Adam Davis
Quoted 40 lines · Greg KH
Quoted 39 lines · Edward Adam Davis
The syzbot reported a kernel-usb-infoleak in
usbtmc_write.The expression "aligned = (transfersize + (
USBTMC_HEADER_SIZE+ 3)) & ~3;" inusbtmcw_write()follows the following pattern:aligned = (1 + 12 + 3) & ~3 = 16 // 3 bytes have not been initialized aligned = (2 + 12 + 3) & ~3 = 16 // 2 bytes have not been initialized aligned = (3 + 12 + 3) & ~3 = 16 // 1 byte has not been initialized aligned = (4 + 12 + 3) & ~3 = 16 // All bytes have been initialized aligned = (5 + 12 + 3) & ~3 = 20 // 3 bytes have not been initialized aligned = (6 + 12 + 3) & ~3 = 20 // 2 bytes have not been initialized aligned = (7 + 12 + 3) & ~3 = 20 // 1 byte has not been initialized aligned = (8 + 12 + 3) & ~3 = 20 // All bytes have been initializedaligned = (9 + 12 + 3) & ~3 = 24
...Note: #define
USBTMC_HEADER_SIZE12This results in the buffer[USBTMC_SEAD_SIZE+transfersize] and its subsequent memory not being initialized.
Fixes 4ddc645f40e9 ("usb: usbtmc: Add ioctl for vendor specific write")Reported-and-tested-by [email protected]Signed-off-by Edward Adam Davis <[email protected]>---
V2 -> V3: Update condition and commentsdrivers/usb/class/usbtmc.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/usb/class/usbtmc.c +0
-0@@ -1591,6 +1591,10 @@ static ssize_t usbtmc_write(struct file *filp, const char __user *buf, goto exit; }Quoted 9 lines
drivers/usb/class/usbtmc.c static ssize_t usbtmc_write(struct file *filp, const char __user *buf, · line 1596
Quoted 3 lines
drivers/usb/class/usbtmc.c +3
-0+if (USBTMC_HEADER_SIZE + transfersize < aligned) +memset (&buffer[USBTMC_HEADER_SIZE + transfersize],0 , + aligned -USBTMC_HEADER_SIZE - transfersize);As this is now a pain to read/understand, and there's no comment describing it so we'll not really understand it in a few months, let alone years, how about we just do the trivial thing and make the allocation with
kzalloc()to start with? And put a comment there saying why it's zeroed out.Perhaps I wrote too much in my comments, but in essence, the logic behind this version's fix is:
When aligned is greater than (USBTMC_HEADER_SIZE+transfersize), there are (aligned - (USBTMC_HEADER_SIZE+transfersize) bytes after the header and data that have not been initialized, and these bytes are then set to 0.Quoted 1 line
Quoted 6 lines
Quoted 2 lines
Sorry, I thought this was going to be a lot simpler based on your first patch than this type of logic.
As you mentioned in my first version patch, this approach is simple and easy to understand, but it comes at the cost of losing the real issue, and KMSAN will not find similar problems again in the future, which is not conducive to making the program logic more robust.
There will not be similar problems in the future as you are explicitly setting everything to 0, so all should be fine :)
The real issue here is that the usbtmc logic of sending data is crazy, and unique to it for various reasons that well all really don't understand. Given the very small number of these devices in the world, it's probably best left to the maintainers of it to handle any real problems going forward, and just squash these types of fuzzing bugs now with a heavy hammer to make them happy.
thanks,
greg k-h
Re: [PATCH V3] USB: usbtmc: prevent kernel-usb-infoleak
Quoted 67 lines · Greg KH
Quoted 56 lines · Edward Adam Davis
Quoted 40 lines · Greg KH
Quoted 39 lines · Edward Adam Davis
The syzbot reported a kernel-usb-infoleak in
usbtmc_write.The expression "aligned = (transfersize + (
USBTMC_HEADER_SIZE+ 3)) & ~3;" inusbtmcw_write()follows the following pattern:aligned = (1 + 12 + 3) & ~3 = 16 // 3 bytes have not been initialized aligned = (2 + 12 + 3) & ~3 = 16 // 2 bytes have not been initialized aligned = (3 + 12 + 3) & ~3 = 16 // 1 byte has not been initialized aligned = (4 + 12 + 3) & ~3 = 16 // All bytes have been initialized aligned = (5 + 12 + 3) & ~3 = 20 // 3 bytes have not been initialized aligned = (6 + 12 + 3) & ~3 = 20 // 2 bytes have not been initialized aligned = (7 + 12 + 3) & ~3 = 20 // 1 byte has not been initialized aligned = (8 + 12 + 3) & ~3 = 20 // All bytes have been initializedaligned = (9 + 12 + 3) & ~3 = 24
...Note: #define
USBTMC_HEADER_SIZE12This results in the buffer[USBTMC_SEAD_SIZE+transfersize] and its subsequent memory not being initialized.
Fixes 4ddc645f40e9 ("usb: usbtmc: Add ioctl for vendor specific write")Reported-and-tested-by [email protected]Signed-off-by Edward Adam Davis <[email protected]>---
V2 -> V3: Update condition and commentsdrivers/usb/class/usbtmc.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/usb/class/usbtmc.c +0
-0@@ -1591,6 +1591,10 @@ static ssize_t usbtmc_write(struct file *filp, const char __user *buf, goto exit; }Quoted 9 lines
drivers/usb/class/usbtmc.c static ssize_t usbtmc_write(struct file *filp, const char __user *buf, · line 1596
Quoted 3 lines
drivers/usb/class/usbtmc.c +3
-0+if (USBTMC_HEADER_SIZE + transfersize < aligned) +memset (&buffer[USBTMC_HEADER_SIZE + transfersize],0 , + aligned -USBTMC_HEADER_SIZE - transfersize);As this is now a pain to read/understand, and there's no comment describing it so we'll not really understand it in a few months, let alone years, how about we just do the trivial thing and make the allocation with
kzalloc()to start with? And put a comment there saying why it's zeroed out.Perhaps I wrote too much in my comments, but in essence, the logic behind this version's fix is:
When aligned is greater than (USBTMC_HEADER_SIZE+transfersize), there are (aligned - (USBTMC_HEADER_SIZE+transfersize) bytes after the header and data that have not been initialized, and these bytes are then set to 0.Quoted 1 line
Quoted 6 lines
Quoted 2 lines
Sorry, I thought this was going to be a lot simpler based on your first patch than this type of logic.
As you mentioned in my first version patch, this approach is simple and easy to understand, but it comes at the cost of losing the real issue, and KMSAN will not find similar problems again in the future, which is not conducive to making the program logic more robust.
There will not be similar problems in the future as you are explicitly setting everything to 0, so all should be fine :)
Quoted 6 lines
The real issue here is that the usbtmc logic of sending data is crazy, and unique to it for various reasons that well all really don't understand. Given the very small number of these devices in the world, it's probably best left to the maintainers of it to handle any real problems going forward, and just squash these types of fuzzing bugs now with a heavy hammer to make them happy.
I reserve my opinion.
If you insist, you can use my first patch directly:
https://lore.kernel.org/all/[email protected]
BR,
Edward
Greg KH
maintainerRe: [PATCH V3] USB: usbtmc: prevent kernel-usb-infoleak
Quoted 74 lines · Edward Adam Davis
Quoted 67 lines · Greg KH
Quoted 56 lines · Edward Adam Davis
Quoted 40 lines · Greg KH
Quoted 39 lines · Edward Adam Davis
The syzbot reported a kernel-usb-infoleak in
usbtmc_write.The expression "aligned = (transfersize + (
USBTMC_HEADER_SIZE+ 3)) & ~3;" inusbtmcw_write()follows the following pattern:aligned = (1 + 12 + 3) & ~3 = 16 // 3 bytes have not been initialized aligned = (2 + 12 + 3) & ~3 = 16 // 2 bytes have not been initialized aligned = (3 + 12 + 3) & ~3 = 16 // 1 byte has not been initialized aligned = (4 + 12 + 3) & ~3 = 16 // All bytes have been initialized aligned = (5 + 12 + 3) & ~3 = 20 // 3 bytes have not been initialized aligned = (6 + 12 + 3) & ~3 = 20 // 2 bytes have not been initialized aligned = (7 + 12 + 3) & ~3 = 20 // 1 byte has not been initialized aligned = (8 + 12 + 3) & ~3 = 20 // All bytes have been initializedaligned = (9 + 12 + 3) & ~3 = 24
...Note: #define
USBTMC_HEADER_SIZE12This results in the buffer[USBTMC_SEAD_SIZE+transfersize] and its subsequent memory not being initialized.
Fixes 4ddc645f40e9 ("usb: usbtmc: Add ioctl for vendor specific write")Reported-and-tested-by [email protected]Signed-off-by Edward Adam Davis <[email protected]>---
V2 -> V3: Update condition and commentsdrivers/usb/class/usbtmc.c | 4 ++++ 1 file changed, 4 insertions(+)drivers/usb/class/usbtmc.c +0
-0@@ -1591,6 +1591,10 @@ static ssize_t usbtmc_write(struct file *filp, const char __user *buf, goto exit; }Quoted 9 lines
drivers/usb/class/usbtmc.c static ssize_t usbtmc_write(struct file *filp, const char __user *buf, · line 1596
Quoted 3 lines
drivers/usb/class/usbtmc.c +3
-0+if (USBTMC_HEADER_SIZE + transfersize < aligned) +memset (&buffer[USBTMC_HEADER_SIZE + transfersize],0 , + aligned -USBTMC_HEADER_SIZE - transfersize);As this is now a pain to read/understand, and there's no comment describing it so we'll not really understand it in a few months, let alone years, how about we just do the trivial thing and make the allocation with
kzalloc()to start with? And put a comment there saying why it's zeroed out.Perhaps I wrote too much in my comments, but in essence, the logic behind this version's fix is:
When aligned is greater than (USBTMC_HEADER_SIZE+transfersize), there are (aligned - (USBTMC_HEADER_SIZE+transfersize) bytes after the header and data that have not been initialized, and these bytes are then set to 0.Quoted 1 line
Quoted 6 lines
Quoted 2 lines
Sorry, I thought this was going to be a lot simpler based on your first patch than this type of logic.
As you mentioned in my first version patch, this approach is simple and easy to understand, but it comes at the cost of losing the real issue, and KMSAN will not find similar problems again in the future, which is not conducive to making the program logic more robust.
There will not be similar problems in the future as you are explicitly setting everything to 0, so all should be fine :)
Quoted 6 lines
The real issue here is that the usbtmc logic of sending data is crazy, and unique to it for various reasons that well all really don't understand. Given the very small number of these devices in the world, it's probably best left to the maintainers of it to handle any real problems going forward, and just squash these types of fuzzing bugs now with a heavy hammer to make them happy.
Quoted 4 lines
I reserve my opinion.
If you insist, you can use my first patch directly:
https://lore.kernel.org/all/[email protected]
No, that should be 'kzalloc()' instead of alocating and calling memset(), to save us the round-trip of someone coming afterward and cleaning up this common pattern to be a single call.
thanks,
greg k-h
PATCH v4 1/1 USB: usbtmc: prevent kernel-usb-infoleak
The syzbot reported a kernel-usb-infoleak in usbtmc_write, we need to clear the structure before filling fields.
---
V1 -> V2: Only clear uninitialized bytes after header and data V2 -> V3: Update condition and comments
V3 -> V4: Use kzalloc to clear up dmabuf
drivers/usb/class/usbtmc.c | 1 + 1 file changed, 1 insertion(+)
drivers/usb/class/usbtmc.c +1 -1
@@ -754,7 +754,7 @@ static struct urb *usbtmc_create_urb(void)if (!urb)return NULL ; - dmabuf =kmalloc (bufsize,GFP_KERNEL ); + dmabuf =kzalloc (bufsize,GFP_KERNEL );if (!dmabuf) {usb_free_urb (urb);return NULL ;
Signature · 1 line
2.43.0
clear save user data buf before copying data from user.
#syz test
drivers/usb/class/usbtmc.c +1 -0
@@ -1586,6 +1586,7 @@ static ssize_t usbtmc_write(struct file *filp, const char __user *buf, aligned = (transfersize + (USBTMC_HEADER_SIZE +3 )) & ~3 ; } +memset (&buffer[USBTMC_HEADER_SIZE ],0 , aligned - count);if (copy_from_user (&buffer[USBTMC_HEADER_SIZE ], buf, transfersize)) { retval = -EFAULT ;up (&file_data->limit_write_sem);
syzbot has tested the proposed patch and the reproducer did not trigger any issue:
Hello,
syzbot has tested the proposed patch and the reproducer did not trigger any issue:
Tested on:
commit: c763c433 Merge tag 'bcachefs-2024-09-04' of git://evil.. git tree: upstream console output: https://syzkaller.appspot.com/x/log.txt?x=139f168f980000 kernel config: https://syzkaller.appspot.com/x/.config?x=35c699864e165c51 dashboard link: https://syzkaller.appspot.com/bug?extid=9d34f80f841e948c3fdb compiler: Debian clang version 15.0.6, GNU ld (GNU Binutils for Debian) 2.40 patch: https://syzkaller.appspot.com/x/patch.diff?x=14cb8f33980000
Note: testing is done by a robot and is best-effort only.
clear save user data buf before copying data from user.
#syz test
drivers/usb/class/usbtmc.c +1 -0
@@ -1586,6 +1586,7 @@ static ssize_t usbtmc_write(struct file *filp, const char __user *buf, aligned = (transfersize + (USBTMC_HEADER_SIZE +3 )) & ~3 ; } +memset (&buffer[USBTMC_HEADER_SIZE ],0 , aligned -USBTMC_HEADER_SIZE );if (copy_from_user (&buffer[USBTMC_HEADER_SIZE ], buf, transfersize)) { retval = -EFAULT ;up (&file_data->limit_write_sem);
syzbot has tested the proposed patch and the reproducer did not trigger any issue:
Hello,
syzbot has tested the proposed patch and the reproducer did not trigger any issue:
Tested on:
commit: c763c433 Merge tag 'bcachefs-2024-09-04' of git://evil.. git tree: upstream console output: https://syzkaller.appspot.com/x/log.txt?x=17fdbd97980000 kernel config: https://syzkaller.appspot.com/x/.config?x=35c699864e165c51 dashboard link: https://syzkaller.appspot.com/bug?extid=9d34f80f841e948c3fdb compiler: Debian clang version 15.0.6, GNU ld (GNU Binutils for Debian) 2.40 patch: https://syzkaller.appspot.com/x/patch.diff?x=170fae53980000
Note: testing is done by a robot and is best-effort only.
Clear the buffer that has not been set, after copying data from user space.
#syz test
drivers/usb/class/usbtmc.c +2 -0
@@ -1590,6 +1590,8 @@ static ssize_t usbtmc_write(struct file *filp, const char __user *buf,up (&file_data->limit_write_sem); goto exit; } +memset (&buffer[USBTMC_HEADER_SIZE + transfersize],0 , + aligned -USBTMC_HEADER_SIZE - transfersize);dev_dbg (&data->intf->dev,"%s(size:%u align:%u)\n" , __func__, (unsigned int )transfersize, (unsigned int )aligned);
syzbot has tested the proposed patch and the reproducer did not trigger any issue:
Hello,
syzbot has tested the proposed patch and the reproducer did not trigger any issue:
Tested on:
commit: b831f83e Merge tag 'bpf-6.11-rc7' of git://git.kernel... git tree: upstream console output: https://syzkaller.appspot.com/x/log.txt?x=10f429ab980000 kernel config: https://syzkaller.appspot.com/x/.config?x=35c699864e165c51 dashboard link: https://syzkaller.appspot.com/bug?extid=9d34f80f841e948c3fdb compiler: Debian clang version 15.0.6, GNU ld (GNU Binutils for Debian) 2.40 patch: https://syzkaller.appspot.com/x/patch.diff?x=1404b6ab980000
Note: testing is done by a robot and is best-effort only.
Clear the buffer that has not been set, after copying data from user space.
#syz test
drivers/usb/class/usbtmc.c +4 -0
@@ -1591,6 +1591,10 @@ static ssize_t usbtmc_write(struct file *filp, const char __user *buf, goto exit; } +if (aligned < buflen) +memset (&buffer[USBTMC_HEADER_SIZE + transfersize],0 , + aligned -USBTMC_HEADER_SIZE - transfersize); +dev_dbg (&data->intf->dev,"%s(size:%u align:%u)\n" , __func__, (unsigned int )transfersize, (unsigned int )aligned);
syzbot has tested the proposed patch and the reproducer did not trigger any issue:
Hello,
syzbot has tested the proposed patch and the reproducer did not trigger any issue:
Tested on:
commit: b831f83e Merge tag 'bpf-6.11-rc7' of git://git.kernel... git tree: upstream console output: https://syzkaller.appspot.com/x/log.txt?x=1422348b980000 kernel config: https://syzkaller.appspot.com/x/.config?x=35c699864e165c51 dashboard link: https://syzkaller.appspot.com/bug?extid=9d34f80f841e948c3fdb compiler: Debian clang version 15.0.6, GNU ld (GNU Binutils for Debian) 2.40 patch: https://syzkaller.appspot.com/x/patch.diff?x=15b3458f980000
Note: testing is done by a robot and is best-effort only.
Clear the buffer that has not been set, after copying data from user space.
#syz test
drivers/usb/class/usbtmc.c +4 -0
@@ -1591,6 +1591,10 @@ static ssize_t usbtmc_write(struct file *filp, const char __user *buf, goto exit; } +if (transfersize <=3 ) +memset (&buffer[USBTMC_HEADER_SIZE + transfersize],0 , + aligned -USBTMC_HEADER_SIZE - transfersize); +dev_dbg (&data->intf->dev,"%s(size:%u align:%u)\n" , __func__, (unsigned int )transfersize, (unsigned int )aligned);
syzbot has tested the proposed patch and the reproducer did not trigger any issue:
Hello,
syzbot has tested the proposed patch and the reproducer did not trigger any issue:
Tested on:
commit: b831f83e Merge tag 'bpf-6.11-rc7' of git://git.kernel... git tree: upstream console output: https://syzkaller.appspot.com/x/log.txt?x=110e8d8f980000 kernel config: https://syzkaller.appspot.com/x/.config?x=35c699864e165c51 dashboard link: https://syzkaller.appspot.com/bug?extid=9d34f80f841e948c3fdb compiler: Debian clang version 15.0.6, GNU ld (GNU Binutils for Debian) 2.40 patch: https://syzkaller.appspot.com/x/patch.diff?x=16be13fb980000
Note: testing is done by a robot and is best-effort only.
Clear the buffer that has not been set, after copying data from user space.
#syz test
drivers/usb/class/usbtmc.c +4 -0
@@ -1591,6 +1591,10 @@ static ssize_t usbtmc_write(struct file *filp, const char __user *buf, goto exit; } +if (transfersize %4 ) +memset (&buffer[USBTMC_HEADER_SIZE + transfersize],0 , + aligned -USBTMC_HEADER_SIZE - transfersize); +dev_dbg (&data->intf->dev,"%s(size:%u align:%u)\n" , __func__, (unsigned int )transfersize, (unsigned int )aligned);
syzbot has tested the proposed patch and the reproducer did not trigger any issue:
Hello,
syzbot has tested the proposed patch and the reproducer did not trigger any issue:
Tested on:
commit: b831f83e Merge tag 'bpf-6.11-rc7' of git://git.kernel... git tree: upstream console output: https://syzkaller.appspot.com/x/log.txt?x=15a09167980000 kernel config: https://syzkaller.appspot.com/x/.config?x=35c699864e165c51 dashboard link: https://syzkaller.appspot.com/bug?extid=9d34f80f841e948c3fdb compiler: Debian clang version 15.0.6, GNU ld (GNU Binutils for Debian) 2.40 patch: https://syzkaller.appspot.com/x/patch.diff?x=12b20709980000
Note: testing is done by a robot and is best-effort only.
syzbot
syzbotFor archival purposes, forwarding an incoming command email to [email protected].
For archival purposes, forwarding an incoming command email to [email protected].
***
Subject: Re: [syzbot] [usb?] KMSAN: kernel-usb-infoleak in usbtmc_write
Author: [email protected]
Short write.
User input data maybe invalid, clear the data buf of the user data before copying from user.
#syz test
drivers/usb/class/usbtmc.c +1 -0
@@ -1586,6 +1586,7 @@ static ssize_t usbtmc_write(struct file *filp, const char __user *buf, aligned = (transfersize + (USBTMC_HEADER_SIZE +3 )) & ~3 ; } +memset (&buffer[USBTMC_HEADER_SIZE ],0 , transfersize);if (copy_from_user (&buffer[USBTMC_HEADER_SIZE ], buf, transfersize)) { retval = -EFAULT ;up (&file_data->limit_write_sem);
syzbot
syzbotFor archival purposes, forwarding an incoming command email to [email protected].
For archival purposes, forwarding an incoming command email to [email protected].
***
Subject: Re: [syzbot] [usb?] KMSAN: kernel-usb-infoleak in usbtmc_write
Author: [email protected]
Short write.
User input data maybe invalid, clear the data buf of the user data before copying from user.
#syz test
drivers/usb/class/usbtmc.c +2 -0
@@ -1586,6 +1586,8 @@ static ssize_t usbtmc_write(struct file *filp, const char __user *buf, aligned = (transfersize + (USBTMC_HEADER_SIZE +3 )) & ~3 ; } +printk ("xf size: %u, count: %u, %s\n" , transfersize, count, __func__); +memset (&buffer[USBTMC_HEADER_SIZE ],0 , count);if (copy_from_user (&buffer[USBTMC_HEADER_SIZE ], buf, transfersize)) { retval = -EFAULT ;up (&file_data->limit_write_sem);