mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Jaegyun Lee <dreams200260@gmail.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Neill Kapron <nkapron@google.com>
Cc: Jaegyun Lee <dreams200260@gmail.com>,
	linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org,
	stable@vger.kernel.org
Subject: [PATCH 2/2] usb: gadget: f_ncm: check pending NTB tailroom before appending
Date: Sun, 29 Nov 2026 22:36:09 +0900	[thread overview]
Message-ID: <20261129133609.403044-2-dreams200260@gmail.com> (raw)
In-Reply-To: <20261129133609.403044-1-dreams200260@gmail.com>

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

      reply	other threads:[~2026-11-29 13:36 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [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=20261129133609.403044-2-dreams200260@gmail.com \
    --to=dreams200260@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@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

all inboxes | Powered by JetHome®