Linux kernel -stable discussions
 help / color / mirror / Atom feed
From: Takashi Iwai <tiwai@suse.de>
To: Jaegyun Lee <dreams200260@gmail.com>
Cc: Takashi Iwai <tiwai@suse.de>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Neill Kapron <nkapron@google.com>,
	linux-usb@vger.kernel.org, linux-sound@vger.kernel.org,
	linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH v3] usb: gadget: midi2: serialize the IN endpoint transmit path
Date: Thu, 03 Dec 2026 18:30:40 +0100	[thread overview]
Message-ID: <871p7yuv9b.wl-tiwai@suse.de> (raw)
In-Reply-To: <20261203135832.3396094-1-dreams200260@gmail.com>

On Thu, 03 Dec 2026 14:58:32 +0100,
Jaegyun Lee wrote:
> 
> process_midi1_transmit() and process_ump_transmit() run unlocked from
> both the ALSA output trigger and the IN request completion handler,
> and f_midi2_stop_eps() frees IN requests on SET_INTERFACE without
> serializing against them.
> 
> In MIDI 1.0 mode, two concurrent callers of process_midi1_transmit()
> race on port->pending in process_midi1_pending_buf(), underflowing
> port->pending into a huge memmove(). When SET_INTERFACE calls
> f_midi2_stop_eps() while an IN transmit is running, the request being
> filled is freed under the transmitter.
> 
> Reproduced on dummy_hcd in altset 0 by writing to the UMP device.
> 
> OOB write because of the underflow:
> 
> [   48.568314][ T9607] ------------[ cut here ]------------
> [   48.568330][ T9607] memmove: detected field-spanning write (size 4294967294) of single field "port->buf" at drivers/usb/gadget/function/f_midi2.c:957 (size 32)
> [   48.568336][    C3] BUG: unable to handle page fault for address: ffffed102908202d
> [   48.568344][ T9607] WARNING: drivers/usb/gadget/function/f_midi2.c:957 at process_midi1_transmit+0x1394/0x1930, CPU#1: midi2_m1_race/9607
> [   48.568383][ T9607] Modules linked in:
> [   48.568395][ T9607] CPU: 1 UID: 0 PID: 9607 Comm: midi2_m1_race Not tainted 7.4.0-rc3-g666539eecdf4 #1 PREEMPT(full) 
> [   48.568411][ T9607] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014
> [   48.568420][ T9607] RIP: 0010:process_midi1_transmit+0x13a3/0x1930
> [   48.568441][ T9607] Code: fc 07 0f 86 18 fe ff ff 31 ed e9 eb f2 ff ff e8 f3 10 e7 f9 48 8d 3d 5c c0 7c 0a b9 20 00 00 00 48 c7 c2 80 ea e4 8c 48 89 ee <67> 48 0f b9 3a e9 ed f8 ff ff e8 ce 10 e7 f9 48 8b 04 24 48 8d 78
> [   48.568464][ T9607] RSP: 0018:ffffc9001279f9c0 EFLAGS: 00010293
> [   48.568478][ T9607] RAX: 0000000000000000 RBX: 0000000000000000 RCX: 0000000000000020
> [   48.568487][ T9607] RDX: ffffffff8ce4ea80 RSI: 00000000fffffffe RDI: ffffffff92592ac0
> [   48.568496][ T9607] RBP: 00000000fffffffe R08: ffffffff87dc635e R09: 00000000fffffffe
> [   48.568504][ T9607] R10: ffffffff924c579f R11: 0000000000000000 R12: ffff88804841016c
> [   48.568513][ T9607] R13: ffff888048410000 R14: dffffc0000000000 R15: ffff88804841016e
> [   48.568524][ T9607] FS:  00007fc67a40f640(0000) GS:ffff8880e9493000(0000) knlGS:0000000000000000
> [   48.568536][ T9607] CS:  0010 DS: 0000 ES: 0000 CR0: 0000000080050033
> [   48.568545][ T9607] CR2: 00007fc67bc17000 CR3: 000000004b3ca000 CR4: 0000000000350ef0
> [   48.568553][ T9607] Call Trace:
> [   48.568558][ T9607]  <TASK>
> [   48.568568][ T9607]  ? __pfx_process_midi1_transmit+0x10/0x10
> [   48.568587][ T9607]  ? do_raw_spin_lock+0x12b/0x270
> [   48.568603][ T9607]  ? find_held_lock+0x2b/0x80
> [   48.568619][ T9607]  f_midi2_ump_trigger+0x102/0x120
> [   48.568638][ T9607]  snd_rawmidi_kernel_write1+0x62c/0x8f0
> [   48.568657][ T9607]  snd_rawmidi_write+0x26f/0xc80
> [   48.568683][ T9607]  ? __pfx_snd_rawmidi_write+0x10/0x10
> [   48.568701][ T9607]  ? common_file_perm+0x278/0x4f0
> [   48.568725][ T9607]  ? bpf_lsm_file_permission+0x9/0x10
> [   48.568739][ T9607]  ? security_file_permission+0x75/0x230
> [   48.568754][ T9607]  ? rw_verify_area+0xcf/0x6e0
> [   48.568773][ T9607]  vfs_write+0x2b1/0x1140
> [   48.568785][ T9607]  ? __pfx_snd_rawmidi_write+0x10/0x10
> [   48.568800][ T9607]  ? __pfx_vfs_write+0x10/0x10
> [   48.568811][ T9607]  ? __fget_files+0x1f1/0x3b0
> [   48.568826][ T9607]  ? __fget_files+0x1fb/0x3b0
> [   48.568841][ T9607]  ksys_write+0x1ef/0x240
> [   48.568863][ T9607]  ? __pfx_ksys_write+0x10/0x10
> [   48.568881][ T9607]  do_syscall_64+0x132/0x7c0
> [   48.568908][ T9607]  entry_SYSCALL_64_after_hwframe+0x77/0x7f
> [   48.568923][ T9607] RIP: 0033:0x45694f
> [   48.568933][ T9607] Code: 89 54 24 18 48 89 74 24 10 89 7c 24 08 e8 79 8f 03 00 48 8b 54 24 18 48 8b 74 24 10 41 89 c0 8b 7c 24 08 b8 01 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 31 44 89 c7 48 89 44 24 08 e8 bc 8f 03 00 48
> [   48.568945][ T9607] RSP: 002b:00007fc67a40f110 EFLAGS: 00000293 ORIG_RAX: 0000000000000001
> [   48.568958][ T9607] RAX: ffffffffffffffda RBX: 00000000004fe400 RCX: 000000000045694f
> [   48.568966][ T9607] RDX: 0000000000001000 RSI: 00000000004fe400 RDI: 0000000000000003
> [   48.568975][ T9607] RBP: 0000000000000000 R08: 0000000000000000 R09: 00007ffde13a563f
> [   48.568983][ T9607] R10: 0000000000000008 R11: 0000000000000293 R12: 00007fc67a40f640
> [   48.568991][ T9607] R13: 0000000000000010 R14: 000000000041ee60 R15: 00007fc679c0f000
> [   48.569003][ T9607]  </TASK>
> 
> UAF while the host toggles altset 0 and 1 rapidly:
> 
> [   37.649231][ T9613] ==================================================================
> [   37.649247][ T9613] BUG: KASAN: slab-use-after-free in process_midi1_transmit+0x165f/0x1930
> [   37.649288][ T9613] Read of size 8 at addr ffff888048083218 by task midi2_m1_race/9613
> [   37.649304][ T9613] 
> [   37.649314][ T9613] CPU: 1 UID: 0 PID: 9613 Comm: midi2_m1_race Not tainted 7.4.0-rc3-g666539eecdf4 #1 PREEMPT(full) 
> [   37.649339][ T9613] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014
> [   37.649360][ T9613] Call Trace:
> [   37.649367][ T9613]  <TASK>
> [   37.649374][ T9613]  dump_stack_lvl+0x116/0x1b0
> [   37.649398][ T9613]  print_report+0xf1/0x5c0
> [   37.649418][ T9613]  ? __virt_addr_valid+0x238/0x430
> [   37.649446][ T9613]  ? process_midi1_transmit+0x165f/0x1930
> [   37.649475][ T9613]  kasan_report+0xca/0x100
> [   37.649494][ T9613]  ? process_midi1_transmit+0x165f/0x1930
> [   37.649525][ T9613]  process_midi1_transmit+0x165f/0x1930
> [   37.649558][ T9613]  ? __pfx_process_midi1_transmit+0x10/0x10
> [   37.649587][ T9613]  ? do_raw_spin_lock+0x12b/0x270
> [   37.649610][ T9613]  ? find_held_lock+0x2b/0x80
> [   37.649635][ T9613]  f_midi2_ump_trigger+0x102/0x120
> [   37.649664][ T9613]  snd_rawmidi_kernel_write1+0x62c/0x8f0
> [   37.649692][ T9613]  snd_rawmidi_write+0x26f/0xc80
> [   37.649716][ T9613]  ? __pfx_snd_rawmidi_write+0x10/0x10
> [   37.649737][ T9613]  ? common_file_perm+0x278/0x4f0
> [   37.649767][ T9613]  ? bpf_lsm_file_permission+0x9/0x10
> [   37.649786][ T9613]  ? security_file_permission+0x75/0x230
> [   37.649806][ T9613]  ? rw_verify_area+0xcf/0x6e0
> [   37.649836][ T9613]  vfs_write+0x2b1/0x1140
> [   37.649853][ T9613]  ? __pfx_snd_rawmidi_write+0x10/0x10
> [   37.649876][ T9613]  ? __pfx_vfs_write+0x10/0x10
> [   37.649894][ T9613]  ? __fget_files+0x1f1/0x3b0
> [   37.649917][ T9613]  ? __fget_files+0x1fb/0x3b0
> [   37.649940][ T9613]  ksys_write+0x1ef/0x240
> [   37.649958][ T9613]  ? __pfx_ksys_write+0x10/0x10
> [   37.649980][ T9613]  do_syscall_64+0x132/0x7c0
> [   37.650009][ T9613]  entry_SYSCALL_64_after_hwframe+0x77/0x7f
> [   37.650030][ T9613] RIP: 0033:0x45698f
> [   37.650045][ T9613] Code: 89 54 24 18 48 89 74 24 10 89 7c 24 08 e8 79 8f 03 00 48 8b 54 24 18 48 8b 74 24 10 41 89 c0 8b 7c 24 08 b8 01 00 00 00 0f 05 <48> 3d 00 f0 ff ff 77 31 44 89 c7 48 89 44 24 08 e8 bc 8f 03 00 48
> [   37.650065][ T9613] RSP: 002b:00007f185b0500f0 EFLAGS: 00000293 ORIG_RAX: 0000000000000001
> [   37.650084][ T9613] RAX: ffffffffffffffda RBX: 00000000004fe400 RCX: 000000000045698f
> [   37.650098][ T9613] RDX: 0000000000001000 RSI: 00000000004fe400 RDI: 0000000000000003
> [   37.650110][ T9613] RBP: 00007f185b05012c R08: 0000000000000000 R09: 00007fff022429ef
> [   37.650123][ T9613] R10: 0000000000000008 R11: 0000000000000293 R12: 00007f185b050640
> [   37.650135][ T9613] R13: 0000000000000010 R14: 000000000041eea0 R15: 00007f185a850000
> [   37.650154][ T9613]  </TASK>
> [   37.650161][ T9613] 
> [   37.650164][ T9613] Allocated by task 9578:
> [   37.650173][ T9613]  kasan_save_stack+0x24/0x50
> [   37.650200][ T9613]  kasan_save_track+0x14/0x30
> [   37.650226][ T9613]  __kasan_kmalloc+0xaa/0xb0
> [   37.650251][ T9613]  __kmalloc_cache_noprof+0x2da/0x6e0
> [   37.650272][ T9613]  dummy_alloc_request+0xa9/0x1b0
> [   37.650289][ T9613]  usb_ep_alloc_request+0x6e/0x2e0
> [   37.650308][ T9613]  alloc_ep_req+0x28/0x1e0
> [   37.650333][ T9613]  f_midi2_alloc_ep_reqs+0x19b/0x2c0
> [   37.650361][ T9613]  f_midi2_start_eps+0x23a/0x610
> [   37.650382][ T9613]  f_midi2_set_alt+0x2fc/0x610
> [   37.650403][ T9613]  composite_setup+0xb1b/0x9130
> [   37.650419][ T9613]  configfs_composite_setup+0x10f/0x150
> [   37.650445][ T9613]  dummy_timer+0x12ce/0x35c0
> [   37.650463][ T9613]  __hrtimer_run_queues+0x17f/0xa00
> [   37.650491][ T9613]  hrtimer_run_softirq+0x1c9/0x360
> [   37.650517][ T9613]  handle_softirqs+0x20e/0x9e0
> [   37.650541][ T9613]  __irq_exit_rcu+0x184/0x240
> [   37.650563][ T9613]  irq_exit_rcu+0x9/0x30
> [   37.650584][ T9613]  sysvec_apic_timer_interrupt+0xa8/0xc0
> [   37.650611][ T9613]  asm_sysvec_apic_timer_interrupt+0x1a/0x20
> [   37.650630][ T9613] 
> [   37.650634][ T9613] Freed by task 0:
> [   37.650642][ T9613]  kasan_save_stack+0x24/0x50
> [   37.650667][ T9613]  kasan_save_track+0x14/0x30
> [   37.650693][ T9613]  kasan_save_free_info+0x3b/0x60
> [   37.650715][ T9613]  __kasan_slab_free+0x61/0x80
> [   37.650730][ T9613]  kfree+0x2ca/0x6e0
> [   37.650747][ T9613]  dummy_free_request+0x56/0x90
> [   37.650763][ T9613]  f_midi2_free_ep_reqs+0x151/0x270
> [   37.650782][ T9613]  f_midi2_set_alt+0x44d/0x610
> [   37.650803][ T9613]  composite_setup+0xb1b/0x9130
> [   37.650818][ T9613]  configfs_composite_setup+0x10f/0x150
> [   37.650840][ T9613]  dummy_timer+0x12ce/0x35c0
> [   37.650858][ T9613]  __hrtimer_run_queues+0x17f/0xa00
> [   37.650884][ T9613]  hrtimer_run_softirq+0x1c9/0x360
> [   37.650910][ T9613]  handle_softirqs+0x20e/0x9e0
> [   37.650932][ T9613]  __irq_exit_rcu+0x184/0x240
> [   37.650954][ T9613]  irq_exit_rcu+0x9/0x30
> [   37.650976][ T9613]  sysvec_apic_timer_interrupt+0xa8/0xc0
> [   37.651002][ T9613]  asm_sysvec_apic_timer_interrupt+0x1a/0x20
> [   37.651020][ T9613] 
> [   37.651024][ T9613] The buggy address belongs to the object at ffff888048083200
> [   37.651024][ T9613]  which belongs to the cache kmalloc-128 of size 128
> [   37.651039][ T9613] The buggy address is located 24 bytes inside of
> [   37.651039][ T9613]  freed 128-byte region [ffff888048083200, ffff888048083280)
> [   37.651058][ T9613] 
> [   37.651062][ T9613] The buggy address belongs to the physical page:
> [   37.651071][ T9613] page: refcount:0 mapcount:0 mapping:0000000000000000 index:0x0 pfn:0x48083
> [   37.651088][ T9613] flags: 0x4fff00000000000(node=1|zone=1|lastcpupid=0x7ff)
> [   37.651104][ T9613] page_type: f5(slab)
> [   37.651120][ T9613] raw: 04fff00000000000 ffff88801d042a00 dead000000000122 0000000000000000
> [   37.651137][ T9613] raw: 0000000000000000 0000000000100010 00000000f5000000 0000000000000000
> [   37.651148][ T9613] page dumped because: kasan: bad access detected
> [   37.651157][ T9613] page_owner tracks the page as allocated
> [   37.651163][ T9613] page last allocated via order 0, migratetype Unmovable, gfp_mask 0xd2820(GFP_ATOMIC|__GFP_NOWARN|__GFP_NORETRY|__GFP_COMP|__GFP_NOMEMALLOC), pid 40, tgid 40 (kworker/1:1), ts 37471649956
> [   37.651195][ T9613]  post_alloc_hook+0xff/0x130
> [   37.651214][ T9613]  get_page_from_freelist+0xdf0/0x2a10
> [   37.651234][ T9613]  __alloc_frozen_pages_noprof+0x28b/0x2dd0
> [   37.651257][ T9613]  new_slab+0xb9/0x6b0
> [   37.651278][ T9613]  refill_objects+0x10e/0x3d0
> [   37.651299][ T9613]  __pcs_replace_empty_main+0x35a/0x680
> [   37.651319][ T9613]  __kmalloc_cache_noprof+0x592/0x6e0
> [   37.651339][ T9613]  dummy_urb_enqueue+0xa3/0x9a0
> [   37.651366][ T9613]  usb_hcd_submit_urb+0x266/0x1fe0
> [   37.651391][ T9613]  usb_submit_urb+0x8a6/0x18d0
> [   37.651417][ T9613]  do_submit_urbs_locked.part.0+0x197/0x370
> [   37.651440][ T9613]  submit_io_urbs.part.0+0xac/0x130
> [   37.651462][ T9613]  snd_usb_midi_v2_trigger+0x15f/0x1b0
> [   37.651484][ T9613]  snd_rawmidi_kernel_write1+0x62c/0x8f0
> [   37.651503][ T9613]  ump_req_msg+0x158/0x570
> [   37.651523][ T9613]  snd_ump_parse_endpoint+0x104/0xc90
> [   37.651546][ T9613] page last free pid 24 tgid 24 ts 37070428769 stack trace:
> [   37.651559][ T9613]  __free_frozen_pages+0x75e/0x1190
> [   37.651575][ T9613]  rcu_core+0x5f0/0x1260
> [   37.651597][ T9613]  handle_softirqs+0x20e/0x9e0
> [   37.651619][ T9613]  run_ksoftirqd+0x3a/0x60
> [   37.651640][ T9613]  smpboot_thread_fn+0x2bf/0xb40
> [   37.651658][ T9613]  kthread+0x390/0x4a0
> [   37.651677][ T9613]  ret_from_fork+0xb0c/0xdb0
> [   37.651702][ T9613]  ret_from_fork_asm+0x1a/0x30
> [   37.651725][ T9613] 
> [   37.651729][ T9613] Memory state around the buggy address:
> [   37.651737][ T9613]  ffff888048083100: fa fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
> [   37.651751][ T9613]  ffff888048083180: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
> [   37.651764][ T9613] >ffff888048083200: fa fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
> [   37.651774][ T9613]                             ^
> [   37.651783][ T9613]  ffff888048083280: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
> [   37.651796][ T9613]  ffff888048083300: fa fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
> [   37.651806][ T9613] ==================================================================
> 
> Serialize the transmit path with IN request allocation and teardown
> using a per-endpoint lock, and defer it to a work item when it is
> kicked from the IN request completion handler.

Hmm, I think it's an overkill to use a work, and it's not great to
delay the processing.
If it's about the early free in f_midi2_stop_eps(), can't we simply do
sync the completion, instead?


thanks,

Takashi

      reply	other threads:[~2026-12-03 17:30 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-03 13:58 [PATCH v3] usb: gadget: midi2: serialize the IN endpoint transmit path Jaegyun Lee
2026-12-03 17:30 ` Takashi Iwai [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=871p7yuv9b.wl-tiwai@suse.de \
    --to=tiwai@suse.de \
    --cc=dreams200260@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-sound@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=nkapron@google.com \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox