KASAN: stack-out-of-bounds Write in append_midi1_out_jack
syzbot
syzbotHello,
syzbot found the following issue on:
HEAD commit: 818bebeb63dd drm/xe: Don't hand out the flat CCS storage a.. git tree: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git console output: https://syzkaller.appspot.com/x/log.txt?x=12dd6979580000 kernel config: https://syzkaller.appspot.com/x/.config?x=ccca94d2c01b9e78 dashboard link: https://syzkaller.appspot.com/bug?extid=c35f34092a4bc9855be6 compiler: gcc (Debian 14.2.0-19) 14.2.0, GNU ld (GNU Binutils for Debian) 2.44 C reproducer: https://syzkaller.appspot.com/x/repro.c?x=17040579580000
IMPORTANT: if you fix the issue, please add the following tag to the commit:
==================================================================
BUG: KASAN: stack-out-of-bounds in append_midi1_out_jack.isra.0+0x38c/0x470 drivers/usb/gadget/function/f_midi2.c:1713 Write of size 1 at addr ffffc90003277880 by task syz-executor119/6000 CPU: 2 UID: 0 PID: 6000 Comm: syz-executor119 Not tainted syzkaller #0 PREEMPT(full) Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS 1.16.3-debian-1.16.3-2 04/01/2014 Call Trace: <TASK> __dump_stack lib/dump_stack.c:94 [inline] dump_stack_lvl+0x100/0x190 lib/dump_stack.c:120 print_address_description mm/kasan/report.c:378 [inline] print_report+0x13d/0x4b0 mm/kasan/report.c:482 kasan_report+0xdf/0x1c0 mm/kasan/report.c:595 append_midi1_out_jack.isra.0+0x38c/0x470 drivers/usb/gadget/function/f_midi2.c:1713 f_midi2_create_usb_configs+0x748/0xdf0 drivers/usb/gadget/function/f_midi2.c:1815 f_midi2_bind+0x1d12/0x2770 drivers/usb/gadget/function/f_midi2.c:2013 usb_add_function+0x219/0x890 drivers/usb/gadget/composite.c:333 configfs_composite_bind+0xd83/0x1960 drivers/usb/gadget/configfs.c:1802 gadget_bind_driver+0x28c/0xbf0 drivers/usb/gadget/udc/core.c:1662 call_driver_probe drivers/base/dd.c:628 [inline] really_probe+0x241/0xa60 drivers/base/dd.c:706 __driver_probe_device+0x210/0x460 drivers/base/dd.c:868 driver_probe_device+0x4a/0x140 drivers/base/dd.c:898 __driver_attach+0x21f/0x5b0 drivers/base/dd.c:1292 bus_for_each_dev+0x13e/0x1d0 drivers/base/bus.c:383 bus_add_driver+0x305/0x5b0 drivers/base/bus.c:763 driver_register+0x1e2/0x360 drivers/base/driver.c:174 usb_gadget_register_driver_owner+0x132/0x210 drivers/usb/gadget/udc/core.c:1752 gadget_dev_desc_UDC_store+0x1b0/0x2e0 drivers/usb/gadget/configfs.c:300 flush_write_buffer fs/configfs/file.c:207 [inline] configfs_write_iter+0x302/0x4e0 fs/configfs/file.c:229 new_sync_write fs/read_write.c:595 [inline] vfs_write+0x6af/0x1050 fs/read_write.c:687 ksys_write+0x12a/0x250 fs/read_write.c:739 do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline] do_syscall_64+0x123/0x790 arch/x86/entry/syscall_64.c:84 entry_SYSCALL_64_after_hwframe+0x77/0x7f RIP: 0033:0x7fe46da0d9f7 Code: 48 89 fa 4c 89 df e8 98 1d 00 00 8b 93 08 03 00 00 59 5e 48 83 f8 fc 74 1a 5b c3 0f 1f 84 00 00 00 00 00 48 8b 44 24 10 0f 05 <5b> c3 0f 1f 80 00 00 00 00 83 e2 39 83 fa 08 75 de e8 23 ff ff ff RSP: 002b:00007fff00e4dd80 EFLAGS: 00000202 ORIG_RAX: 0000000000000001 RAX: ffffffffffffffda RBX: 0000555572ba4400 RCX: 00007fe46da0d9f7 RDX: 000000000000000b RSI: 0000555572ba67e3 RDI: 0000000000000004 RBP: 0000555572ba67e3 R08: 0000000000000000 R09: 0000000000000000 R10: 0000000000000000 R11: 0000000000000202 R12: 00007fff00e4ddf0 R13: 0000555572ba6770 R14: 00007fe46da70cc0 R15: 0000000000000002 </TASK> The buggy address belongs to stack of task syz-executor119/6000 and is located at offset 440 in frame: f_midi2_bind+0x0/0x2770 drivers/usb/gadget/function/f_midi2.c:1768
This frame has 6 objects:
[32, 36) 'sw'
[48, 56) 'card'
[80, 88) 'ump'
[112, 128) 'string_fn'
[144, 160) 'strings'
[176, 440) 'config'
The buggy address belongs to a 8-page vmalloc region starting at 0xffffc90003270000 allocated at kernel_clone+0x176/0x9d0 kernel/fork.c:2772
The buggy address belongs to the physical page:
page: refcount:1 mapcount:0 mapping:0000000000000000 index:0x7ff248b6b pfn:0x50a87
flags: 0xfff00000000000(node=0|zone=1|lastcpupid=0x7ff)
raw: 00fff00000000000 dead000000000100 dead000000000122 0000000000000000
raw: 00000007ff248b6b 0000000000000000 00000001ffffffff 0000000000000000
page dumped because: kasan: bad access detected
page_owner tracks the page as allocated
page last allocated via order 0, migratetype Unmovable, gfp_mask 0x29c2(GFP_NOWAIT|__GFP_HIGHMEM|__GFP_IO|__GFP_FS|__GFP_ZERO), pid 5768, tgid 5768 (syz-executor), ts 51046718723
set_page_owner include/linux/page_owner.h:33 [inline]
post_alloc_hook+0xfd/0x120 mm/page_alloc.c:1871
prep_new_page mm/page_alloc.c:1879 [inline]
get_page_from_freelist+0x110d/0x35b0 mm/page_alloc.c:3943
__alloc_frozen_pages_noprof+0x2eb/0x3300 mm/page_alloc.c:5436
alloc_pages_mpol+0x201/0x550 mm/mempolicy.c:2495
alloc_pages_noprof+0x1a/0x160 mm/mempolicy.c:2586
vm_area_alloc_pages mm/vmalloc.c:3690 [inline]
__vmalloc_area_node mm/vmalloc.c:3915 [inline]
__vmalloc_node_range_noprof+0xdcc/0x1460 mm/vmalloc.c:4105
__vmalloc_node_noprof+0xad/0xf0 mm/vmalloc.c:4166
alloc_thread_stack_node kernel/fork.c:359 [inline]
dup_task_struct kernel/fork.c:930 [inline]
copy_process+0x803/0x8370 kernel/fork.c:2139
kernel_clone+0x176/0x9d0 kernel/fork.c:2772
__do_sys_clone+0xd9/0x120 kernel/fork.c:2914
do_syscall_x64 arch/x86/entry/syscall_64.c:61 [inline]
do_syscall_64+0x123/0x790 arch/x86/entry/syscall_64.c:84
entry_SYSCALL_64_after_hwframe+0x77/0x7f
page last free pid 5748 tgid 5748 ts 50853501051 stack trace:
reset_page_owner include/linux/page_owner.h:26 [inline]
__free_pages_prepare mm/page_alloc.c:1418 [inline]
free_pages_prepare+0x586/0xe00 mm/page_alloc.c:1463
__free_contig_range_common+0x14f/0x250 mm/page_alloc.c:7060
__free_contig_range mm/page_alloc.c:7105 [inline]
free_pages_bulk+0x12a/0x200 mm/page_alloc.c:5322
vm_area_free_pages+0xad/0x2b0 mm/vmalloc.c:3462
vfree mm/vmalloc.c:3511 [inline]
vfree+0x108/0x730 mm/vmalloc.c:3485
kcov_put kernel/kcov.c:447 [inline]
kcov_put kernel/kcov.c:443 [inline]
kcov_close+0x34/0x60 kernel/kcov.c:554
__fput+0x3ff/0xb50 fs/file_table.c:512
task_work_run+0x150/0x240 kernel/task_work.c:233
exit_task_work include/linux/task_work.h:40 [inline]
do_exit+0x951/0x2ae0 kernel/exit.c:1008
do_group_exit+0xd5/0x2a0 kernel/exit.c:1151
get_signal+0x1ec7/0x21e0 kernel/signal.c:3046
arch_do_signal_or_restart+0x91/0x7e0 arch/x86/kernel/signal.c:337
__exit_to_user_mode_loop kernel/entry/common.c:66 [inline]
exit_to_user_mode_loop+0x139/0x700 kernel/entry/common.c:101
__exit_to_user_mode_prepare include/linux/irq-entry-common.h:207 [inline]
syscall_exit_to_user_mode_prepare include/linux/irq-entry-common.h:230 [inline]
syscall_exit_to_user_mode include/linux/entry-common.h:336 [inline]
do_syscall_64+0x661/0x790 arch/x86/entry/syscall_64.c:89
entry_SYSCALL_64_after_hwframe+0x77/0x7f
Memory state around the buggy address:
ffffc90003277780: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
ffffc90003277800: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
>ffffc90003277880: f3 f3 f3 f3 f3 f3 f3 f3 f3 00 00 00 00 00 00 00
^
ffffc90003277900: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
ffffc90003277980: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
==================================================================---
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 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.
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
From: Edward Aadm Davis <[email protected]>
#syz test
drivers/usb/gadget/function/f_midi2.c +14 -6
@@ -1675,11 +1675,15 @@ static int append_midi1_in_jack(struct f_midi2 *midi2, struct midi1_cable_mapping *map, unsigned int type) { -struct usb_midi_in_jack_descriptor *jack = - &config->jack_ins[config->jack_in++]; -int id = ++config->jack_id; +struct usb_midi_in_jack_descriptor *jack; +int id;int err; +if (config->jack_in >=MAX_CABLES ) +return -EINVAL ; + + id = ++config->jack_id; + jack = &config->jack_ins[config->jack_in++]; jack->bLength =0x06 ; jack->bDescriptorType =USB_DT_CS_INTERFACE ; jack->bDescriptorSubtype =USB_MS_MIDI_IN_JACK ;
@@ -1700,11 +1704,15 @@ static int append_midi1_out_jack(struct f_midi2 *midi2,struct midi1_cable_mapping *map, unsigned int type, unsigned int source) { - struct usb_midi_out_jack_descriptor_1 *jack = - &config->jack_outs[config->jack_out++]; -int id = ++config->jack_id; + struct usb_midi_out_jack_descriptor_1 *jack; +int id;int err; +if (config->jack_out >=MAX_CABLES ) +return -EINVAL ; + + id = ++config->jack_id; + jack = &config->jack_outs[config->jack_out++]; jack->bLength =0x09 ; jack->bDescriptorType =USB_DT_CS_INTERFACE ; jack->bDescriptorSubtype =USB_MS_MIDI_OUT_JACK ;
syzbot
syzbot"" does not look like a valid git branch or commit.
Quoted 3 lines
From: Edward Aadm Davis <[email protected]>
#syz test
"" does not look like a valid git branch or commit.
Quoted 43 lines
drivers/usb/gadget/function/f_midi2.c +14
-6@@ -1675,11 +1675,15 @@ static int append_midi1_in_jack(struct f_midi2 *midi2, struct midi1_cable_mapping *map, unsigned int type) { -struct usb_midi_in_jack_descriptor *jack = - &config->jack_ins[config->jack_in++]; -int id = ++config->jack_id; +struct usb_midi_in_jack_descriptor *jack; +int id;int err; +if (config->jack_in >=MAX_CABLES ) +return -EINVAL ; + + id = ++config->jack_id; + jack = &config->jack_ins[config->jack_in++]; jack->bLength =0x06 ; jack->bDescriptorType =USB_DT_CS_INTERFACE ; jack->bDescriptorSubtype =USB_MS_MIDI_IN_JACK ;@@ -1700,11 +1704,15 @@ static int append_midi1_out_jack(struct f_midi2 *midi2,struct midi1_cable_mapping *map, unsigned int type, unsigned int source) { - struct usb_midi_out_jack_descriptor_1 *jack = - &config->jack_outs[config->jack_out++]; -int id = ++config->jack_id; + struct usb_midi_out_jack_descriptor_1 *jack; +int id;int err; +if (config->jack_out >=MAX_CABLES ) +return -EINVAL ; + + id = ++config->jack_id; + jack = &config->jack_outs[config->jack_out++]; jack->bLength =0x09 ; jack->bDescriptorType =USB_DT_CS_INTERFACE ; jack->bDescriptorSubtype =USB_MS_MIDI_OUT_JACK ;
From: Edward Aadm Davis <[email protected]>
#syz test: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git 818bebeb63dd
drivers/usb/gadget/function/f_midi2.c +14 -6
@@ -1675,11 +1675,15 @@ static int append_midi1_in_jack(struct f_midi2 *midi2, struct midi1_cable_mapping *map, unsigned int type) { -struct usb_midi_in_jack_descriptor *jack = - &config->jack_ins[config->jack_in++]; -int id = ++config->jack_id; +struct usb_midi_in_jack_descriptor *jack; +int id;int err; +if (config->jack_in >=MAX_CABLES ) +return -EINVAL ; + + id = ++config->jack_id; + jack = &config->jack_ins[config->jack_in++]; jack->bLength =0x06 ; jack->bDescriptorType =USB_DT_CS_INTERFACE ; jack->bDescriptorSubtype =USB_MS_MIDI_IN_JACK ;
@@ -1700,11 +1704,15 @@ static int append_midi1_out_jack(struct f_midi2 *midi2,struct midi1_cable_mapping *map, unsigned int type, unsigned int source) { - struct usb_midi_out_jack_descriptor_1 *jack = - &config->jack_outs[config->jack_out++]; -int id = ++config->jack_id; + struct usb_midi_out_jack_descriptor_1 *jack; +int id;int err; +if (config->jack_out >=MAX_CABLES ) +return -EINVAL ; + + id = ++config->jack_id; + jack = &config->jack_outs[config->jack_out++]; jack->bLength =0x09 ; jack->bDescriptorType =USB_DT_CS_INTERFACE ; jack->bDescriptorSubtype =USB_MS_MIDI_OUT_JACK ;
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: 818bebeb drm/xe: Don't hand out the flat CCS storage a.. git tree: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git console output: https://syzkaller.appspot.com/x/log.txt?x=104ed979580000 kernel config: https://syzkaller.appspot.com/x/.config?x=ccca94d2c01b9e78 dashboard link: https://syzkaller.appspot.com/bug?extid=c35f34092a4bc9855be6 compiler: gcc (Debian 14.2.0-19) 14.2.0, GNU ld (GNU Binutils for Debian) 2.44 patch: https://syzkaller.appspot.com/x/patch.diff?x=16b36979580000
Note: testing is done by a robot and is best-effort only.
PATCH 1/1 usb: gadget: midi2: prevent in/out jack from oob
In reply to syzbotThe increment of config->jack_out in append_midi1_out_jack() lacked bounds checking, triggering issue [1] when the value approached the limit MAX_CABLES.
A similar out-of-bounds issue exists in append_midi1_in_jack(), so it is being fixed as well.
Before incrementing jack_out/in, the code now checks if the value has reached the upper limit MAX_CABLES; if so, it exits and returns -EINVAL.
Additionally, the jack_id assignment is moved to occur after the jack_out bounds check to prevent wasting IDs on invalid increments.
[1]
BUG: KASAN: stack-out-of-bounds in append_midi1_out_jack.isra.0+0x38c/0x470 drivers/usb/gadget/function/f_midi2.c:1713 Write of size 1 at addr ffffc90003277880 by task syz-executor119/6000 Call Trace: append_midi1_out_jack.isra.0+0x38c/0x470 drivers/usb/gadget/function/f_midi2.c:1713 f_midi2_create_usb_configs+0x748/0xdf0 drivers/usb/gadget/function/f_midi2.c:1815 f_midi2_bind+0x1d12/0x2770 drivers/usb/gadget/function/f_midi2.c:2013 usb_add_function+0x219/0x890 drivers/usb/gadget/composite.c:333 configfs_composite_bind+0xd83/0x1960 drivers/usb/gadget/configfs.c:1802 gadget_bind_driver+0x28c/0xbf0 drivers/usb/gadget/udc/core.c:1662 call_driver_probe drivers/base/dd.c:628 [inline] really_probe+0x241/0xa60 drivers/base/dd.c:706
--- drivers/usb/gadget/function/f_midi2.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
drivers/usb/gadget/function/f_midi2.c +14 -6
@@ -1675,11 +1675,15 @@ static int append_midi1_in_jack(struct f_midi2 *midi2, struct midi1_cable_mapping *map, unsigned int type) { -struct usb_midi_in_jack_descriptor *jack = - &config->jack_ins[config->jack_in++]; -int id = ++config->jack_id; +struct usb_midi_in_jack_descriptor *jack; +int id;int err; +if (config->jack_in >=MAX_CABLES ) +return -EINVAL ; + + id = ++config->jack_id; + jack = &config->jack_ins[config->jack_in++]; jack->bLength =0x06 ; jack->bDescriptorType =USB_DT_CS_INTERFACE ; jack->bDescriptorSubtype =USB_MS_MIDI_IN_JACK ;
@@ -1700,11 +1704,15 @@ static int append_midi1_out_jack(struct f_midi2 *midi2,struct midi1_cable_mapping *map, unsigned int type, unsigned int source) { - struct usb_midi_out_jack_descriptor_1 *jack = - &config->jack_outs[config->jack_out++]; -int id = ++config->jack_id; + struct usb_midi_out_jack_descriptor_1 *jack; +int id;int err; +if (config->jack_out >=MAX_CABLES ) +return -EINVAL ; + + id = ++config->jack_id; + jack = &config->jack_outs[config->jack_out++]; jack->bLength =0x09 ; jack->bDescriptorType =USB_DT_CS_INTERFACE ; jack->bDescriptorSubtype =USB_MS_MIDI_OUT_JACK ;
Signature · 1 line
2.43.0
Re: [PATCH] usb: gadget: midi2: prevent in/out jack from oob
Quoted 5 lines · Edward Adam Davis
The increment of
config->jack_outinappend_midi1_out_jack()lacked bounds checking, triggering issue [1] when the value approached the limitMAX_CABLES.
Quoted 8 lines
A similar out-of-bounds issue exists in
append_midi1_in_jack(), so it is being fixed as well.Before incrementing jack_out/in, the code now checks if the value has reached the upper limit
MAX_CABLES; if so, it exits and returns-EINVAL.Additionally, the
jack_idassignment is moved to occur after thejack_outbounds check to prevent wasting IDs on invalid increments.
I think the bug is rather the arrays are too small; they should have been twice as the jacks can be added from both input and output.
Both append_midi1_out_jack() and append_midi1_in_jack() are called from the loops of midi2->num_midi1_out and midi2->num_midi1_in counts, and they are properly upper-bound to MAX_CABLES.
Could you check whether the fix below works instead?
thanks,
Takashi
drivers/usb/gadget/function/f_midi2.c +2 -2
@@ -1634,8 +1634,8 @@ struct f_midi2_usb_config {/* MIDI 1.0 jacks */ unsigned char jack_in, jack_out, jack_id; -struct usb_midi_in_jack_descriptor jack_ins[MAX_CABLES ]; -struct usb_midi_out_jack_descriptor_1 jack_outs[MAX_CABLES ]; +struct usb_midi_in_jack_descriptor jack_ins[MAX_CABLES *2 ]; +struct usb_midi_out_jack_descriptor_1 jack_outs[MAX_CABLES *2 ]; };static int append_config (struct f_midi2_usb_config *config,void *d)
Re: [PATCH] usb: gadget: midi2: prevent in/out jack from oob
From: Edward Aadm Davis <[email protected]>
Quoted 13 lines · Takashi Iwai
Quoted 4 lines
The increment of
config->jack_outinappend_midi1_out_jack()lacked bounds checking, triggering issue [1] when the value approached the limitMAX_CABLES.Quoted 8 lines
A similar out-of-bounds issue exists in
append_midi1_in_jack(), so it is being fixed as well.Before incrementing jack_out/in, the code now checks if the value has reached the upper limit
MAX_CABLES; if so, it exits and returns-EINVAL.Additionally, the
jack_idassignment is moved to occur after thejack_outbounds check to prevent wasting IDs on invalid increments.
Quoted 6 lines
I think the bug is rather the arrays are too small; they should have been twice as the jacks can be added from both input and output.
Both
append_midi1_out_jack()andappend_midi1_in_jack()are called from the loops ofmidi2->num_midi1_outandmidi2->num_midi1_incounts, and they are properly upper-bound toMAX_CABLES.
In reproducer, set num_groups and midi1_num_groups to 16, this will cause num_midi1_in and num_midi1_out to both be 16 (total 32), overflowing the MAX_CABLES (16) sized arrays on the stack during bind.
Quoted 2 lines
Could you check whether the fix below works instead?
Yes, the fix below works.
cheers,
Edward
Quoted 19 lines
thanks,
Takashi
drivers/usb/gadget/function/f_midi2.c +2
-2@@ -1634,8 +1634,8 @@ struct f_midi2_usb_config {/* MIDI 1.0 jacks */ unsigned char jack_in, jack_out, jack_id; -struct usb_midi_in_jack_descriptor jack_ins[MAX_CABLES ]; -struct usb_midi_out_jack_descriptor_1 jack_outs[MAX_CABLES ]; +struct usb_midi_in_jack_descriptor jack_ins[MAX_CABLES *2 ]; +struct usb_midi_out_jack_descriptor_1 jack_outs[MAX_CABLES *2 ]; };static int append_config (struct f_midi2_usb_config *config,void *d)