Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 27829258CF5; Mon, 9 Jun 2025 13:46:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749476798; cv=none; b=AIKS/xC9kOj3cdIjykQrKXe/tkAvOnIzpdpu9C2U/IQcKgRzx5Jz/7WhRMrf+d193rQRNMA6In5Nr/6E7rc3UBFJXSic2g5PAzibinx3BMexzZ3XgPDRv12fxPxVeFyP1s98DzRAcpGXU6JneOATFptqOf0B/ikCwc3f2Z50VVg= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1749476798; c=relaxed/simple; bh=fl3lCfsWTZ/Z+mo1PZ+s6GwkBEy8pZAljzdAy14g7xU=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version:Content-Type; b=AQl5UbF3uZuNHD489KjhLOHSFTNSIeR1R0J0YSARjAz+7JcrZ4waRGUOvsDv860KiHkJR38Ycbzzbw/pL7TO9PgxtK+xlJ7LbLdb6Pq0uJK3E1fnrq9LS4Ixj9vwlz2kuBx3n9OpPm49ELwHlTf5+8Zl1XhkN3iBUFn6NZtvz6A= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=BS7U4BE1; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="BS7U4BE1" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4DB29C4CEEB; Mon, 9 Jun 2025 13:46:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1749476798; bh=fl3lCfsWTZ/Z+mo1PZ+s6GwkBEy8pZAljzdAy14g7xU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BS7U4BE1PEjrFdGGTOd7NBTY7NvvAEqJzXbUWdWp4AYWC0rMq0jNfzB7ktNNA/88q O59kL7Og2c2YQ3PtrxRM2CBT3nNVYM0+WyhHCBS/iiD/0dIN2D8VfzaXwiPCasQKAv Gvi4L4fq1EDY+x+UzwZs3KS2HN7lPycveIFSjnaFEHJuUW/Qs+s7a31aVgPr1l0xbO dVKOL4VX5SNaPwaylyFZVe79Nqahe/ihZ5Nwpy5X0hVh99+kcrtHnz0u2Chl/cpTtS vpdJDFgAY7oPnvDXMnE0xnBf+EEwuzinrD0XxpYjKKTTgqCQ+EAf5ub7EsFsc8QHNe ZISxPnO92LPNg== From: Sasha Levin To: patches@lists.linux.dev, stable@vger.kernel.org Cc: Chen Yufeng , Greg Kroah-Hartman , Sasha Levin , Thinh.Nguyen@synopsys.com Subject: [PATCH AUTOSEL 6.12 16/23] usb: potential integer overflow in usbg_make_tpg() Date: Mon, 9 Jun 2025 09:46:03 -0400 Message-Id: <20250609134610.1343777-16-sashal@kernel.org> X-Mailer: git-send-email 2.39.5 In-Reply-To: <20250609134610.1343777-1-sashal@kernel.org> References: <20250609134610.1343777-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: patches@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 6.12.32 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From: Chen Yufeng [ Upstream commit 153874010354d050f62f8ae25cbb960c17633dc5 ] The variable tpgt in usbg_make_tpg() is defined as unsigned long and is assigned to tpgt->tport_tpgt, which is defined as u16. This may cause an integer overflow when tpgt is greater than USHRT_MAX (65535). I haven't tried to trigger it myself, but it is possible to trigger it by calling usbg_make_tpg() with a large value for tpgt. I modified the type of tpgt to match tpgt->tport_tpgt and adjusted the relevant code accordingly. This patch is similar to commit 59c816c1f24d ("vhost/scsi: potential memory corruption"). Signed-off-by: Chen Yufeng Link: https://lore.kernel.org/r/20250415065857.1619-1-chenyufeng@iie.ac.cn Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- Based on my comprehensive analysis, here's my decision: **YES** This commit should be backported to stable kernel trees for the following reasons: ## Analysis of the Integer Overflow Bug **1. Clear Security/Correctness Issue:** The bug is a classic integer overflow vulnerability where a user- controlled value (`tpgt`) parsed from a string via `kstrtoul()` as an `unsigned long` gets truncated when assigned to a `u16` field (`tpg->tport_tpgt`). In the original code at line 1690: ```c unsigned long tpgt; // Can hold values up to ULONG_MAX // ... kstrtoul(name + 5, 0, &tpgt) ... tpg->tport_tpgt = tpgt; // u16 field - truncation occurs! ``` If a user provides a value like `tpgt_70000`, the parsing succeeds (70000 < UINT_MAX), but when assigned to the 16-bit field, it becomes `70000 & 0xFFFF = 4464`, completely changing the intended value. **2. Matches Stable Backport Criteria:** - **Important Bug Fix**: Yes - prevents integer overflow and data corruption - **Small, Contained Change**: Yes - only changes variable type and validation function - **No Architectural Changes**: Correct - simply fixes data type mismatch - **Minimal Regression Risk**: Very low risk - more restrictive validation - **Confined to Subsystem**: Yes - only affects USB gadget target mode **3. Historical Precedent:** The fix directly references commit `59c816c1f24d` which was a nearly identical fix in `drivers/vhost/scsi.c` that was backported to stable trees. That commit fixed the exact same pattern: - `unsigned long tpgt` → `u16 tpgt` - `kstrtoul() || tpgt > UINT_MAX` → `kstrtou16()` - Assignment to `u16 tport_tpgt` field **4. Real Impact:** While this requires privileged access to trigger (configfs manipulation), the integer overflow could lead to: - Unexpected behavior in target portal group handling - Potential confusion in multi-target setups - Data integrity issues in storage target configuration **5. Code Quality:** The fix properly aligns the variable type with its intended use and makes the validation more precise by using `kstrtou16()` instead of the broader `kstrtoul()` check. This fix exemplifies the type of integer overflow prevention that stable trees actively accept - targeted, low-risk corrections that prevent subtle but potentially serious bugs from affecting users. drivers/usb/gadget/function/f_tcm.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/usb/gadget/function/f_tcm.c b/drivers/usb/gadget/function/f_tcm.c index 7b23631f47449..6ad205046032c 100644 --- a/drivers/usb/gadget/function/f_tcm.c +++ b/drivers/usb/gadget/function/f_tcm.c @@ -1297,14 +1297,14 @@ static struct se_portal_group *usbg_make_tpg(struct se_wwn *wwn, struct usbg_tport *tport = container_of(wwn, struct usbg_tport, tport_wwn); struct usbg_tpg *tpg; - unsigned long tpgt; + u16 tpgt; int ret; struct f_tcm_opts *opts; unsigned i; if (strstr(name, "tpgt_") != name) return ERR_PTR(-EINVAL); - if (kstrtoul(name + 5, 0, &tpgt) || tpgt > UINT_MAX) + if (kstrtou16(name + 5, 0, &tpgt)) return ERR_PTR(-EINVAL); ret = -ENODEV; mutex_lock(&tpg_instances_lock); -- 2.39.5