* [PATCH 1/2] usb: gadget: f_ncm: drop datagrams larger than the NTB
@ 2026-11-29 13:36 Jaegyun Lee
2026-11-29 13:36 ` [PATCH 2/2] usb: gadget: f_ncm: check pending NTB tailroom before appending Jaegyun Lee
0 siblings, 1 reply; 2+ messages in thread
From: Jaegyun Lee @ 2026-11-29 13:36 UTC (permalink / raw)
To: Greg Kroah-Hartman, Neill Kapron
Cc: Jaegyun Lee, linux-usb, linux-kernel, stable
When no NTB is pending, ncm_wrap_ntb() starts a new one sized by the
NTB input size the host selected, and copies the datagram into it:
unsigned max_size = ncm->port.fixed_in_len;
...
ncm->skb_tx_data = alloc_skb(max_size, GFP_ATOMIC);
...
skb_put_data(ncm->skb_tx_data, skb->data, skb->len);
fixed_in_len comes from SET_NTB_INPUT_SIZE (ncm_ep0out_complete()
accepts 2048..16384). The size check in front of this only runs when
an NTB is already pending, so nothing checks that a datagram fits in an
empty NTB. With an MTU larger than fits in the selected input size, a
large frame is written past the end of the NTB skb and skb_put()
panics in the transmit path.
Reproduced with dummy_hcd and an ncm gadget with
max_segment_size=8000 and MTU 7986, a host that selects rx_max 2048,
and "ping -s 7900" from the gadget side:
[ 47.833862][ C0] skbuff: skb_over_panic: text:ffffffff87d15a83 len:7954 put:7942 head:ffff888047ad2000 data:ffff888047ad2000 tail:0x1f12 end:0xec0 dev:usb0
[ 47.834376][ C0] ------------[ cut here ]------------
[ 47.834380][ C0] kernel BUG at net/core/skbuff.c:215!
[ 47.834392][ C0] Oops: invalid opcode: 0000 [#1] SMP KASAN NOPTI
[ 47.837564][ C0] CPU: 0 UID: 0 PID: 0 Comm: swapper/0 Not tainted 7.4.0-rc3-g666539eecdf4 #1 PREEMPT(full)
[ 47.838571][ C0] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014
[ 47.839478][ C0] RIP: 0010:skb_panic+0x157/0x1d0
[ 47.839995][ C0] Code: b6 04 01 84 c0 74 04 3c 03 7e 21 41 56 8b 4b 70 45 89 e8 48 c7 c7 e0 0f 43 8d 41 57 56 48 89 ee 52 4c 89 e2 e8 8a e5 07 f8 90 <0f> 0b 4c 89 4c 24 10 48 89 54 24 08 48 89 34 24 e8 74 f5 a1 f8 4c
[ 47.841907][ C0] RSP: 0018:ffffc90000007390 EFLAGS: 00010082
[ 47.842510][ C0] RAX: 000000000000008a RBX: ffff888052e77000 RCX: 0000000000000303
[ 47.843297][ C0] RDX: 0000000000000000 RSI: ffffffff81a10b90 RDI: 0000000000000005
[ 47.844083][ C0] RBP: ffffffff8d433400 R08: ffffffff81a092f1 R09: 0000000000000000
[ 47.844869][ C0] R10: ffffc90000007167 R11: 0000000000000000 R12: ffffffff87d15a83
[ 47.845651][ C0] R13: 0000000000001f06 R14: ffff888027258120 R15: 0000000000000ec0
[ 47.846434][ C0] FS: 0000000000000000(0000) GS:ffff888095a93000(0000) knlGS:0000000000000000
[ 47.847322][ C0] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 47.847977][ C0] CR2: 00007ffd7a70ffb8 CR3: 0000000052c93000 CR4: 0000000000350ef0
[ 47.848770][ C0] Call Trace:
[ 47.849099][ C0] <IRQ>
[ 47.849387][ C0] ? ncm_wrap_ntb+0x5d3/0x11a0
[ 47.849873][ C0] skb_put+0x174/0x1b0
[ 47.850288][ C0] ncm_wrap_ntb+0x5d3/0x11a0
[ 47.850758][ C0] eth_start_xmit+0x3ac/0x1390
[ 47.851245][ C0] dev_hard_start_xmit+0x17f/0x790
[ 47.851763][ C0] sch_direct_xmit+0x1c6/0xe60
[ 47.852245][ C0] ? __pfx_sch_direct_xmit+0x10/0x10
[ 47.852801][ C0] ? lock_acquire+0x1bf/0x370
[ 47.853269][ C0] ? do_raw_spin_lock+0x12b/0x270
[ 47.853785][ C0] ? __pfx_do_raw_spin_lock+0x10/0x10
[ 47.854323][ C0] __dev_queue_xmit+0x3685/0x41d0
[ 47.854833][ C0] ? irqentry_exit+0x407/0xb40
[ 47.855319][ C0] ? __pfx___dev_queue_xmit+0x10/0x10
[ 47.855861][ C0] ? __lock_acquire+0x509/0x2110
[ 47.856364][ C0] ? lock_acquire+0x1bf/0x370
[ 47.856838][ C0] ? find_held_lock+0x2b/0x80
[ 47.857312][ C0] ? __asan_memcpy+0x3d/0x60
[ 47.857791][ C0] ? eth_header+0x11d/0x200
[ 47.858247][ C0] neigh_resolve_output+0x51e/0x8f0
[ 47.858776][ C0] __neigh_update+0x9cd/0x2b50
[ 47.859259][ C0] arp_process+0x96d/0x2540
[ 47.859731][ C0] ? nf_hook.constprop.0+0x453/0x720
[ 47.860265][ C0] ? __pfx_arp_process+0x10/0x10
[ 47.860771][ C0] ? __pfx_nf_hook.constprop.0+0x10/0x10
[ 47.861337][ C0] ? arch_stack_walk+0x86/0xf0
[ 47.861823][ C0] ? __lock_acquire+0x509/0x2110
[ 47.862329][ C0] arp_rcv+0x3c6/0x510
[ 47.862697][ C0] ? __pfx_arp_rcv+0x10/0x10
[ 47.863115][ C0] __netif_receive_skb_one_core+0x1b4/0x1e0
[ 47.863632][ C0] ? __pfx___netif_receive_skb_one_core+0x10/0x10
[ 47.864211][ C0] ? process_backlog+0x330/0x1540
[ 47.864657][ C0] ? process_backlog+0x330/0x1540
[ 47.865112][ C0] __netif_receive_skb+0x1d/0x160
[ 47.865557][ C0] process_backlog+0x382/0x1540
[ 47.865994][ C0] __napi_poll.constprop.0+0xb3/0x540
[ 47.866468][ C0] net_rx_action+0x9b1/0xea0
[ 47.866884][ C0] ? __pfx_net_rx_action+0x10/0x10
[ 47.867336][ C0] ? napi_skb_cache_put+0x31f/0x760
[ 47.867801][ C0] ? __pfx_xfrm_dev_backlog+0x10/0x10
[ 47.868275][ C0] ? napi_skb_cache_put+0x324/0x760
[ 47.868742][ C0] ? net_tx_action+0x83f/0xf30
[ 47.869167][ C0] handle_softirqs+0x20e/0x9e0
[ 47.869592][ C0] __irq_exit_rcu+0x184/0x240
[ 47.870022][ C0] irq_exit_rcu+0x9/0x30
[ 47.870403][ C0] sysvec_apic_timer_interrupt+0xa8/0xc0
[ 47.870907][ C0] </IRQ>
[ 47.871167][ C0] <TASK>
[ 47.871427][ C0] asm_sysvec_apic_timer_interrupt+0x1a/0x20
[ 47.871958][ C0] RIP: 0010:pv_native_safe_halt+0x1e/0x30
[ 47.872465][ C0] Code: 90 90 90 90 90 90 90 90 90 90 90 90 f3 0f 1e fa 0f 1f 44 00 00 eb 0c 0f 1f 44 00 00 0f 00 2d 29 f8 15 00 0f 1f 44 00 00 fb f4 <c3> cc cc cc cc 66 2e 0f 1f 84 00 00 00 00 00 0f 1f 00 90 90 90 90
[ 47.874144][ C0] RSP: 0018:ffffffff8fa07ce8 EFLAGS: 00000206
[ 47.874676][ C0] RAX: 00000000000b725f RBX: ffffffff8fa91740 RCX: ffffffff8bd63249
[ 47.875366][ C0] RDX: 0000000000000000 RSI: 0000000000000001 RDI: 0000000000000000
[ 47.876065][ C0] RBP: fffffbfff1f522e8 R08: 0000000000000001 R09: ffffed1005686825
[ 47.876762][ C0] R10: ffff88802b43412b R11: 0000000000000001 R12: dffffc0000000000
[ 47.877448][ C0] R13: 0000000000000000 R14: 0000000000000000 R15: 0000000000013bd0
[ 47.878142][ C0] ? ct_kernel_exit+0x1d9/0x240
[ 47.878572][ C0] default_idle+0xe/0x20
[ 47.878954][ C0] default_idle_call+0x6c/0xb0
[ 47.879377][ C0] do_idle+0x3c1/0x5c0
[ 47.879747][ C0] ? __pfx___schedule+0x10/0x10
[ 47.880182][ C0] ? __pfx_do_idle+0x10/0x10
[ 47.880592][ C0] cpu_startup_entry+0x4f/0x60
[ 47.881023][ C0] rest_init+0x1ee/0x330
[ 47.881400][ C0] ? __pfx_rest_init+0x10/0x10
[ 47.881837][ C0] ? acpi_ut_value_exit+0xff/0x180
[ 47.882293][ C0] ? __pfx_kernel_init+0x10/0x10
[ 47.882738][ C0] ? acpi_ut_status_exit+0x103/0x1c0
[ 47.883205][ C0] ? acpi_enable_subsystem+0x370/0x5b0
[ 47.883687][ C0] ? regulator_has_full_constraints+0x9/0x20
[ 47.884227][ C0] ? __pfx_x86_late_time_init+0x10/0x10
[ 47.884724][ C0] start_kernel+0x3f4/0x4d0
[ 47.885130][ C0] x86_64_start_reservations+0x18/0x30
[ 47.885614][ C0] x86_64_start_kernel+0x13c/0x1a0
[ 47.886074][ C0] common_startup_64+0x13e/0x158
[ 47.886515][ C0] </TASK>
[ 47.886790][ C0] Modules linked in:
[ 47.887137][ C0] ---[ end trace 0000000000000000 ]---
Drop a datagram that does not fit in an empty NTB and count it in
tx_dropped.
Fixes: 6d3865f9d41f ("usb: gadget: NCM: Add transmit multi-frame.")
Cc: stable@vger.kernel.org
Assisted-by: ZCode:GLM-5.4
Signed-off-by: Jaegyun Lee <dreams200260@gmail.com>
---
drivers/usb/gadget/function/f_ncm.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/drivers/usb/gadget/function/f_ncm.c b/drivers/usb/gadget/function/f_ncm.c
--- a/drivers/usb/gadget/function/f_ncm.c
+++ b/drivers/usb/gadget/function/f_ncm.c
@@ -1034,6 +1034,14 @@ static struct sk_buff *ncm_wrap_ntb(stru
put_unaligned_le32(crc, crc_pos);
}
+ if (ALIGN(ALIGN(opts->nth_size, div) + rem + skb->len,
+ ndp_align) + opts->ndp_size + 2 * dgram_idx_len >
+ max_size) {
+ skb->dev->stats.tx_dropped++;
+ dev_kfree_skb_any(skb);
+ return NULL;
+ }
+
/* If the new skb is too big for the current NCM NTB then
* set the current stored skb to be sent now and clear it
* ready for new data.
^ permalink raw reply [flat|nested] 2+ messages in thread
* [PATCH 2/2] usb: gadget: f_ncm: check pending NTB tailroom before appending
2026-11-29 13:36 [PATCH 1/2] usb: gadget: f_ncm: drop datagrams larger than the NTB Jaegyun Lee
@ 2026-11-29 13:36 ` Jaegyun Lee
0 siblings, 0 replies; 2+ messages in thread
From: Jaegyun Lee @ 2026-11-29 13:36 UTC (permalink / raw)
To: Greg Kroah-Hartman, Neill Kapron
Cc: Jaegyun Lee, linux-usb, linux-kernel, stable
The host can send SET_NTB_INPUT_SIZE at any time; ncm_ep0out_complete()
only updates ncm->port.fixed_in_len. ncm_wrap_ntb() reads that value
for every datagram as max_size and uses it both to allocate a new NTB
(alloc_skb(max_size)) and to decide whether the next datagram still
fits in the pending one:
if (ncm->skb_tx_data
&& (ncm->ndp_dgram_count >= TX_MAX_NUM_DPE
|| (ncm->skb_tx_data->len +
div + rem + skb->len +
ncm->skb_tx_ndp->len + ndp_align + (2 * dgram_idx_len))
> max_size)) {
If the host raises the input size while an NTB is pending, the pending
skb was allocated for the old, smaller size, but datagrams keep being
appended against the new limit until skb_put() runs past the end of
the buffer.
Reproduced (dummy_hcd): the host toggles
SET_NTB_INPUT_SIZE between 2048 and 16384 while two threads on the
gadget send 1514-byte frames on usb0:
[ 33.988468][ T9607] skbuff: skb_over_panic: text:ffffffff87d15a83 len:4558 put:1514 head:ffff888021a38000 data:ffff888021a38000 tail:0x11ce end:0xec0 dev:usb0
[ 33.989351][ T9607] ------------[ cut here ]------------
[ 33.989358][ T9607] kernel BUG at net/core/skbuff.c:215!
[ 33.989378][ T9607] Oops: invalid opcode: 0000 [#1] SMP KASAN NOPTI
[ 33.996242][ T9607] CPU: 1 UID: 0 PID: 9607 Comm: r Not tainted 7.4.0-rc3-g666539eecdf4 #1 PREEMPT(full)
[ 33.998336][ T9607] Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.15.0-1 04/01/2014
[ 34.000313][ T9607] RIP: 0010:skb_panic+0x157/0x1d0
[ 34.001427][ T9607] Code: b6 04 01 84 c0 74 04 3c 03 7e 21 41 56 8b 4b 70 45 89 e8 48 c7 c7 e0 0f 43 8d 41 57 56 48 89 ee 52 4c 89 e2 e8 8a e5 07 f8 90 <0f> 0b 4c 89 4c 24 10 48 89 54 24 08 48 89 34 24 e8 74 f5 a1 f8 4c
[ 34.005618][ T9607] RSP: 0018:ffffc900045f7510 EFLAGS: 00010086
[ 34.006953][ T9607] RAX: 000000000000008a RBX: ffff88802460d680 RCX: 0000000000000202
[ 34.008624][ T9607] RDX: 0000000000000000 RSI: ffffffff81a10b90 RDI: 0000000000000005
[ 34.010153][ T9607] RBP: ffffffff8d433400 R08: ffffffff81a092f1 R09: 0000000000000000
[ 34.011678][ T9607] R10: ffffc900045f72e7 R11: 0000000000000000 R12: ffffffff87d15a83
[ 34.013211][ T9607] R13: 00000000000005ea R14: ffff8880489a4120 R15: 0000000000000ec0
[ 34.014739][ T9607] FS: 00007f4ab88fe640(0000) GS:ffff8880e9493000(0000) knlGS:0000000000000000
[ 34.016429][ T9607] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
[ 34.017699][ T9607] CR2: 00007f4ab88fdb98 CR3: 000000004818d000 CR4: 0000000000350ef0
[ 34.019223][ T9607] Call Trace:
[ 34.019869][ T9607] <TASK>
[ 34.020440][ T9607] ? ncm_wrap_ntb+0x5d3/0x11a0
[ 34.021374][ T9607] skb_put+0x174/0x1b0
[ 34.022189][ T9607] ncm_wrap_ntb+0x5d3/0x11a0
[ 34.023099][ T9607] eth_start_xmit+0x3ac/0x1390
[ 34.024049][ T9607] dev_hard_start_xmit+0x17f/0x790
[ 34.024807][ T9607] sch_direct_xmit+0x1c6/0xe60
[ 34.025503][ T9607] ? __pfx_sch_direct_xmit+0x10/0x10
[ 34.026276][ T9607] ? lock_acquire+0x1bf/0x370
[ 34.026961][ T9607] ? do_raw_spin_lock+0x12b/0x270
[ 34.027703][ T9607] ? __pfx_do_raw_spin_lock+0x10/0x10
[ 34.028483][ T9607] __dev_queue_xmit+0x3685/0x41d0
[ 34.029219][ T9607] ? __pfx___dev_queue_xmit+0x10/0x10
[ 34.029999][ T9607] ? __might_fault+0xe0/0x190
[ 34.030684][ T9607] ? _copy_from_iter+0x14b/0x1740
[ 34.031417][ T9607] ? __pfx__copy_from_iter+0x10/0x10
[ 34.032200][ T9607] ? __sanitizer_cov_trace_switch+0x54/0x90
[ 34.033070][ T9607] ? packet_parse_headers+0x278/0x840
[ 34.033857][ T9607] ? __pfx_packet_parse_headers+0x10/0x10
[ 34.034689][ T9607] packet_xmit+0x24f/0x370
[ 34.035341][ T9607] packet_sendmsg+0x3059/0x4d10
[ 34.036058][ T9607] ? aa_sk_perm+0x367/0xb90
[ 34.036732][ T9607] ? __sanitizer_cov_trace_switch+0x54/0x90
[ 34.037587][ T9607] ? __pfx_aa_sk_perm+0x10/0x10
[ 34.038301][ T9607] ? tomoyo_socket_sendmsg_permission+0x13b/0x3a0
[ 34.039199][ T9607] ? __pfx_packet_sendmsg+0x10/0x10
[ 34.039711][ T9607] ? aa_sock_msg_perm+0x122/0x200
[ 34.040202][ T9607] ? apparmor_socket_sendmsg+0x161/0x280
[ 34.040753][ T9607] ? __pfx_packet_sendmsg+0x10/0x10
[ 34.041258][ T9607] __sys_sendto+0x4cf/0x520
[ 34.041705][ T9607] ? __pfx___sys_sendto+0x10/0x10
[ 34.042197][ T9607] ? __fput+0x67c/0xb50
[ 34.042607][ T9607] ? fput_close_sync+0x114/0x210
[ 34.043096][ T9607] ? __pfx_fput_close_sync+0x10/0x10
[ 34.043610][ T9607] ? dnotify_flush+0x79/0x4c0
[ 34.044074][ T9607] __x64_sys_sendto+0xe0/0x1c0
[ 34.044539][ T9607] ? lockdep_hardirqs_on+0x7d/0x110
[ 34.045051][ T9607] do_syscall_64+0x132/0x7c0
[ 34.045511][ T9607] entry_SYSCALL_64_after_hwframe+0x77/0x7f
[ 34.046087][ T9607] RIP: 0033:0x450bf6
[ 34.046468][ T9607] Code: 9c 02 00 44 8b 4c 24 2c 4c 8b 44 24 20 41 89 c4 44 8b 54 24 28 48 8b 54 24 18 b8 2c 00 00 00 48 8b 74 24 10 8b 7c 24 08 0f 05 <48> 3d 00 f0 ff ff 77 3a 44 89 e7 48 89 44 24 08 e8 75 9c 02 00 48
[ 34.048328][ T9607] RSP: 002b:00007f4ab88fdb60 EFLAGS: 00000293 ORIG_RAX: 000000000000002c
[ 34.049130][ T9607] RAX: ffffffffffffffda RBX: 0000000000000004 RCX: 0000000000450bf6
[ 34.049900][ T9607] RDX: 00000000000005ea RSI: 00007f4ab88fdbc0 RDI: 0000000000000004
[ 34.050656][ T9607] RBP: 00007f4ab88fdba0 R08: 00007f4ab88fdba0 R09: 0000000000000014
[ 34.051420][ T9607] R10: 0000000000000000 R11: 0000000000000293 R12: 0000000000000000
[ 34.052180][ T9607] R13: 0000000000000010 R14: 0000000000416740 R15: 00007f4ab80fe000
[ 34.052942][ T9607] </TASK>
[ 34.053236][ T9607] Modules linked in:
[ 34.053612][ T9607] ---[ end trace 0000000000000000 ]---
Send the pending NTB first when the datagram, its NDP entry and padding
do not fit in the tailroom left in the pending skb.
Fixes: 6d3865f9d41f ("usb: gadget: NCM: Add transmit multi-frame.")
Cc: stable@vger.kernel.org
Assisted-by: ZCode:GLM-5.4
Signed-off-by: Jaegyun Lee <dreams200260@gmail.com>
---
drivers/usb/gadget/function/f_ncm.c | 21 ++++++++++++---------
1 file changed, 12 insertions(+), 9 deletions(-)
diff --git a/drivers/usb/gadget/function/f_ncm.c b/drivers/usb/gadget/function/f_ncm.c
--- a/drivers/usb/gadget/function/f_ncm.c
+++ b/drivers/usb/gadget/function/f_ncm.c
@@ -1039,15 +1039,18 @@ static struct sk_buff *ncm_wrap_ntb(stru
* ready for new data.
* NOTE: Assume maximum align for speed of calculation.
*/
- if (ncm->skb_tx_data
- && (ncm->ndp_dgram_count >= TX_MAX_NUM_DPE
- || (ncm->skb_tx_data->len +
- div + rem + skb->len +
- ncm->skb_tx_ndp->len + ndp_align + (2 * dgram_idx_len))
- > max_size)) {
- skb2 = package_for_tx(ncm);
- if (!skb2)
- goto err;
+ if (ncm->skb_tx_data) {
+ int needed = div + rem + skb->len +
+ ncm->skb_tx_ndp->len + ndp_align +
+ (2 * dgram_idx_len);
+
+ if (ncm->ndp_dgram_count >= TX_MAX_NUM_DPE ||
+ ncm->skb_tx_data->len + needed > max_size ||
+ skb_tailroom(ncm->skb_tx_data) < needed) {
+ skb2 = package_for_tx(ncm);
+ if (!skb2)
+ goto err;
+ }
}
if (!ncm->skb_tx_data) {
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-11-29 13:36 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-29 13:36 [PATCH 1/2] usb: gadget: f_ncm: drop datagrams larger than the NTB Jaegyun Lee
2026-11-29 13:36 ` [PATCH 2/2] usb: gadget: f_ncm: check pending NTB tailroom before appending Jaegyun Lee
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox