public inbox for netdev@vger.kernel.org
 help / color / mirror / Atom feed
From: Omer Zalman <omerzalman42@gmail.com>
To: oliver@neukum.org
Cc: andrew+netdev@lunn.ch, davem@davemloft.net, edumazet@google.com,
	kuba@kernel.org, pabeni@redhat.com, linux-usb@vger.kernel.org,
	netdev@vger.kernel.org, Omer Zalman <omerzalman42@gmail.com>
Subject: [PATCH 2/2] net: usb: cdc_ncm: prevent silent u16 truncation in min_tx_pkt_store()
Date: Tue, 17 Mar 2026 23:06:32 -0500	[thread overview]
Message-ID: <20260318040632.69531-2-omerzalman42@gmail.com> (raw)
In-Reply-To: <20260318040632.69531-1-omerzalman42@gmail.com>

min_tx_pkt is a u16 field but the sysfs store handler accepted any
unsigned long and assigned it directly. Values that do not fit in 16
bits silently truncate: writing 65537 wraps to 1, turning a
"never pad" intent into "always pad".

ctx->tx_max can itself reach 65537 because cdc_ncm_update_rxtx_max()
increments the negotiated NTB size by one pad byte when it is an exact
multiple of the USB maxpacket size.

Clamp the stored value to min(tx_max, U16_MAX). Any value above tx_max
is already semantically equivalent to tx_max since frame lengths never
exceed it; the U16_MAX cap ensures the result is always representable
in the u16 field without wrapping.

Fixes: 39eb7e0e8c88 ("net: cdc_ncm: allow tuning min_tx_pkt")
Signed-off-by: Omer Zalman <omerzalman42@gmail.com>
---
 drivers/net/usb/cdc_ncm.c | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/net/usb/cdc_ncm.c b/drivers/net/usb/cdc_ncm.c
index 6fcb4d711a64..edd69f4e1596 100644
--- a/drivers/net/usb/cdc_ncm.c
+++ b/drivers/net/usb/cdc_ncm.c
@@ -242,11 +242,17 @@ static ssize_t min_tx_pkt_store(struct device *d,
 	struct cdc_ncm_ctx *ctx = (struct cdc_ncm_ctx *)dev->data[0];
 	unsigned long val;
 
-	/* no need to restrict values - anything from 0 to infinity is OK */
 	if (kstrtoul(buf, 0, &val))
 		return -EINVAL;
 
-	ctx->min_tx_pkt = val;
+	/* Clamp to tx_max: the frame length can never exceed tx_max,
+	 * so any threshold above it has the same effect (padding is
+	 * never applied).  Also cap at U16_MAX since min_tx_pkt is
+	 * a u16 - without this, values like 65537 silently wrap to 1
+	 * and invert the intended behavior.
+	 */
+	ctx->min_tx_pkt = min_t(unsigned long, val,
+				min_t(u32, ctx->tx_max, U16_MAX));
 	return len;
 }
 
-- 
2.39.5 (Apple Git-154)


  reply	other threads:[~2026-03-18  4:06 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-18  4:06 [PATCH 1/2] net: usb: cdc_ncm: convert sysfs show() callbacks to sysfs_emit() Omer Zalman
2026-03-18  4:06 ` Omer Zalman [this message]
2026-03-18  6:37   ` [PATCH 2/2] net: usb: cdc_ncm: prevent silent u16 truncation in min_tx_pkt_store() Greg KH
2026-03-18  6:36 ` [PATCH 1/2] net: usb: cdc_ncm: convert sysfs show() callbacks to sysfs_emit() Greg KH

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=20260318040632.69531-2-omerzalman42@gmail.com \
    --to=omerzalman42@gmail.com \
    --cc=andrew+netdev@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=kuba@kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=oliver@neukum.org \
    --cc=pabeni@redhat.com \
    /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