From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=115.124.28.124 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796093071; b=4Lh3KkA8u3nPMw5YMB5nBSseBxj9RpSmYWXb0lINWSPWAivbT9eJJA62ADDsZ7QR0K/C kfB5sBpmeoGh9eeASDW7+0Mg7wIPaKep9jza3FoDF7EzGDtN9BBkkvclELwyruQmqIxJX 2JWMZcYU6ZwtTb7qyKoZ1wSn8AmA34pPpQXy5pjgVVSikCiD8MG7gn7P01t9QT90U0NSk F3ytkn2DdkUUdfVH7eXWMw/0EFCGWjc4ge4HSwpiHwvCn6TvAiwmfZdCtSyoepLsSTpbb EhDpuMcuRPabV+UV36AOay32nRqnx4377UgdqFs1cLdvF2BWUmjc3jCdAJr55MMukhg== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796093071; h=DMARC-Filter:DKIM-Signature:X-Alimail-AntiSpam:Received:From:To:Cc: Subject:Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Transfer-Encoding; bh=1W+RUIdZ/c9Ygs9tILWRSRT7GHK+BWuQWv/i4+UPDUQ=; b=XIPIdqflvpTBfnM6e4nZc/S14YqFhurvWStB91xoZuNdJHuVyukD4/zeEJFVZD5tb6OA 7zfBaF0b46iadrQbWHyc4yrxjj2+kkqOGCcQt4FwUqI+zyaUdZ+etTwH2KjVnhnacYyV8 SpYMMGfnBNML2H35GRe/RKp57DtQaLFxgGj6iT2L9dVa/JCCUhQVasOpT3roTe2lGJz7z IIOrV4BfvCSyaz/MngwjVNmK1cL9jo5SObCjRZlf1HXQVSFZu66zS8KFfuZDJuwCUQBRC 8Y9tNM6hK4PLG8a4SjTv7CMGpl8F6peVQrfXLtZnq/acotQRKtzpNx8uYG3ynCqFPhw== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=none header.from=xiaopeng.com; spf=pass smtp.mailfrom=xiaopeng.com; dkim=pass header.d=xiaopeng.com header.i=@xiaopeng.com header.a=rsa-sha256 header.s=default header.b=o4OXTa+6; arc=none smtp.remote-ip=115.124.28.124 Received: from out28-124.mail.aliyun.com (out28-124.mail.aliyun.com [115.124.28.124]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 91425606A32D; Tue, 1 Dec 2026 02:44:29 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 91425606A32D Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=xiaopeng.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=xiaopeng.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=xiaopeng.com header.i=@xiaopeng.com header.a=rsa-sha256 header.s=default header.b=o4OXTa+6 DKIM-Signature:v=1; a=rsa-sha256; c=relaxed/relaxed; d=xiaopeng.com; s=default; t=1796093061; h=From:To:Subject:Date:Message-ID:MIME-Version; bh=1W+RUIdZ/c9Ygs9tILWRSRT7GHK+BWuQWv/i4+UPDUQ=; b=o4OXTa+6YwLVjRCGJrw+TbOQYy51KY8ceF9GPTPNocyr0eT9vCgnAqMtkFETKsTcVWe11BqOCX4KZISbj7qTEWwAZmQuIcwIQ0DqHLczPBtHn62cX0sEj8oJMBItNDwJCElC1qeyBhJQPflAedfCsNLqwpjf7qigJYIGrUZP4zs= X-Alimail-AntiSpam:AC=CONTINUE;BC=0.07436259|-1;CH=green;DM=|CONTINUE|false|;DS=CONTINUE|ham_system_inform|0.00589463-9.60991e-05-0.994009;FP=16763983865422826678|0|0|0|0|-1|-1|-1;HT=maildocker-contentspam033037034184;MF=liuc63@xiaopeng.com;NM=1;PH=DS;RN=15;RT=15;SR=0;TI=SMTPD_---.kDtpeac_1796093058; Received: from localhost(mailfrom:liuc63@xiaopeng.com fp:SMTPD_---.kDtpeac_1796093058 cluster:ay29) by smtp.aliyun-inc.com; Tue, 01 Dec 2026 10:44:19 +0800 From: Liu Chao To: "Greg Kroah-Hartman" , "Andrew Morton" Cc: "Andy Shevchenko" , "David S. Miller" , "Eric Dumazet" , "Jakub Kicinski" , "Paolo Abeni" , "Simon Horman" , khtsai@google.com, fangxy@xiaopeng.com, "Randy Dunlap" , dbrownell@users.sourceforge.net, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org Subject: [PATCH v7 2/2] usb: gadget: u_ether: fix out-of-bounds read in get_ether_addr Date: Tue, 1 Dec 2026 10:44:15 +0800 Message-ID: <20261201024415.3340593-3-liuc63@xiaopeng.com> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20261201024415.3340593-1-liuc63@xiaopeng.com> References: <20261201024415.3340593-1-liuc63@xiaopeng.com> X-Mailing-List: netdev@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit get_ether_addr() walks the MAC address string with a fixed six iteration loop, calling hex_to_bin() twice per iteration, and never checks for the terminating NUL. A short string therefore makes it read past the end of the buffer. The dev_addr and host_addr module parameters of the legacy gadget drivers are copied into their own kmalloc'ed buffer by param_set_charp(), so the overrun crosses a slab boundary and KASAN reports it at insmod time: # insmod g_ether.ko dev_addr=ab BUG: KASAN: slab-out-of-bounds in get_ether_addr+0x1ec/0x230 Read of size 1 at addr ffff888009f91f53 by task insmod/70 ... get_ether_addr+0x1ec/0x230 gether_set_dev_addr+0x6b/0x180 eth_bind+0xfd/0x3b0 [g_ether] composite_bind+0x1bb/0x7b0 gadget_bind_driver+0x229/0xb00 ... Allocated by task 70: __kmalloc_noprof+0x1d1/0x4a0 param_set_charp+0x4a/0x220 parse_args+0x267/0x5c0 load_module+0x4f6a/0x6600 ... The buggy address is located 0 bytes to the right of allocated 19-byte region [ffff888009f91f40, ffff888009f91f53) Besides reading past the end of the string, the loop ignores what hex_to_bin() returns: it yields -1 for a character that is not a hex digit, and that value is shifted and OR'ed into dev_addr unchecked, so a malformed address can be accepted as a valid one and the interface comes up with an address that nobody asked for: # insmod g_ether.ko dev_addr=aazz:cc:dd:ee:ff # cat /sys/class/net/usb0/address aa:ff:cc:dd:ee:ff The configfs dev_addr and host_addr attributes of the ECM, EEM, NCM, RNDIS and subset functions reach the same parser through gether_set_dev_addr() and gether_set_host_addr(). Their store handler passes the whole configfs buffer on, so an overrun there stays inside the same object and is never reported, but a malformed address is silently accepted all the same. Replace the open coded loop with parse_mac_pton() for the delimited notations and hex2bin() for the raw one, and keep the existing eth_random_addr() fallback for everything both of them reject. parse_mac_pton() checks the minimum length and every digit before writing anything to dev_addr, and hex2bin() stops at the first character that is not a hex digit, which the NUL terminator is, so neither reads past the end of the string. The notations that keep working are the byte pair ones, ':' and '.', in any mixture, and the separator-less "aabbccddeeff". What stops working is the Cisco notation "aabb.ccdd.eeff" and irregular groupings such as "123456:789abc", which the old loop only accepted because it skipped a delimiter wherever it happened to find one. Supporting them would need a parser per grouping, so such input is now rejected. Neither of the two should be missed: they are historical notations that are rarely met these days, and the commit that introduced the loop never said that it accepted them. That commit only explains that it resolves a FIXME by adding module parameters for stable addresses, so skipping a delimiter wherever one happens to turn up reads as a simplification of the parser rather than as support for non-standard formats. How a rejected address is reported depends on the caller. The configfs attributes of the ECM, EEM, NCM, RNDIS and subset functions go through gether_set_dev_addr() and gether_set_host_addr(), which return -EINVAL, so the store fails and the attribute keeps its previous value: writing "aabb.ccdd.eeff" to dev_addr used to succeed and now fails. The legacy module parameters call the same two setters but silently ignore their error, so the interface keeps the random address that gether_setup_name_default() picked when it was created, with nothing in the log to say why the requested address was not used. Make get_ether_addr() warn once, inside its caller-supplied branch, when both parsers reject the string, so the user sees which address was rejected and that a random one is in use; the default case of no address given stays silent. Also document the accepted formats in the configfs ABI files of all five functions. Select GENERIC_NET_UTILS so that parse_mac_pton() is built in. The loop is older than the git history. It was introduced by the commit 331a2d82bcaf ("[PATCH] USB: ethernet/rndis gadget address params") from v2.6.7 in drivers/usb/gadget/ether.c, and commit 2b3d942c4878 ("usb ethernet gadget: split out network core") copied it into u_ether.c. That commit predates the git conversion and only resolves in the historic tree history.git, so the Fixes: tag points at the start of the official git era, commit 1da177e4c3f4 ("Linux-2.6.12-rc2"), and the commit that actually introduced the loop is named here in prose. Suggested-by: Andy Shevchenko Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Liu Chao --- v7: as Andy suggested, place the warning once inside get_ether_addr(), at the tail of the if (str) block, and revert both gether_set_dev_addr() and gether_set_host_addr() to their pre-v6 plain early-return form; drop the unreferenced Link: trailer from the commit message. v6: correct the commit message: the legacy module parameters do not go through gether_setup_name() (it has no callers), they go through gether_set_dev_addr() and gether_set_host_addr(), which return -EINVAL and leave the random address picked at setup in place; make both setters warn when get_ether_addr() rejects an address the caller supplied, leaving the no-address default silent; amend the five configfs ABI notes to say the raw notation matches at the beginning of the input, trailing characters are ignored, and the address must be unicast and not all-zero. v5: no driver code change. Describe the configfs behaviour accurately (a rejected address fails the store with -EINVAL and keeps the old value), name all five functions that use the parser, document the accepted formats in their configfs ABI files, restore a Fixes: tag for 1da177e4c3f4 ("Linux-2.6.12-rc2") while keeping the real introducing commit in prose, and say "minimum length" where v4 said "length". v4: pick up Andy's Reviewed-by; name 331a2d82bcaf from v2.6.7 as the commit that introduced the loop and note that it only resolves in history.git; record that the notations being dropped are historical and were never documented as supported. No code change. v3: fall back to hex2bin() so that the separator-less notation keeps working, as suggested by Andy; drop the Fixes tag, the loop predates the git history. v2: rework on top of parse_mac_pton(), add the KASAN report and the malformed address example, select GENERIC_NET_UTILS, drop Cc: stable and the Reviewed-by. Link: https://lore.kernel.org/all/20261117090110.3263592-3-liuc63@xiaopeng.com/ .../ABI/testing/configfs-usb-gadget-ecm | 9 ++++++++- .../ABI/testing/configfs-usb-gadget-eem | 9 +++++++++ .../ABI/testing/configfs-usb-gadget-ncm | 8 ++++++++ .../ABI/testing/configfs-usb-gadget-rndis | 9 +++++++++ .../ABI/testing/configfs-usb-gadget-subset | 9 +++++++++ drivers/usb/gadget/Kconfig | 1 + drivers/usb/gadget/function/u_ether.c | 20 +++++++++---------- 7 files changed, 54 insertions(+), 11 deletions(-) diff --git a/Documentation/ABI/testing/configfs-usb-gadget-ecm b/Documentation/ABI/testing/configfs-usb-gadget-ecm index 732101ca9d0b..77fec9086ced 100644 --- a/Documentation/ABI/testing/configfs-usb-gadget-ecm +++ b/Documentation/ABI/testing/configfs-usb-gadget-ecm @@ -17,4 +17,11 @@ Description: - MAC address of device's end of this Ethernet over USB link - + host_addr and dev_addr accept a MAC address as six byte + pairs separated by ':' or '.', in any mixture + (aa:bb:cc:dd:ee:ff, aa.bb.cc.dd.ee.ff), or as twelve + hexadecimal digits with no separator (aabbccddeeff) at the + beginning of the input. Trailing characters are ignored. + The address must be unicast and not all-zero. Anything + else is rejected: the write fails with EINVAL and the + attribute keeps its previous value. diff --git a/Documentation/ABI/testing/configfs-usb-gadget-eem b/Documentation/ABI/testing/configfs-usb-gadget-eem index 178c3d5fb647..a690a629554f 100644 --- a/Documentation/ABI/testing/configfs-usb-gadget-eem +++ b/Documentation/ABI/testing/configfs-usb-gadget-eem @@ -14,3 +14,12 @@ Description: dev_addr MAC address of device's end of this Ethernet over USB link ========== ============================================= + + host_addr and dev_addr accept a MAC address as six byte + pairs separated by ':' or '.', in any mixture + (aa:bb:cc:dd:ee:ff, aa.bb.cc.dd.ee.ff), or as twelve + hexadecimal digits with no separator (aabbccddeeff) at the + beginning of the input. Trailing characters are ignored. + The address must be unicast and not all-zero. Anything + else is rejected: the write fails with EINVAL and the + attribute keeps its previous value. diff --git a/Documentation/ABI/testing/configfs-usb-gadget-ncm b/Documentation/ABI/testing/configfs-usb-gadget-ncm index 6fe723effc78..94891eafe81e 100644 --- a/Documentation/ABI/testing/configfs-usb-gadget-ncm +++ b/Documentation/ABI/testing/configfs-usb-gadget-ncm @@ -13,3 +13,11 @@ Description: dev_addr - MAC address of device's end of this Ethernet over USB link + host_addr and dev_addr accept a MAC address as six byte + pairs separated by ':' or '.', in any mixture + (aa:bb:cc:dd:ee:ff, aa.bb.cc.dd.ee.ff), or as twelve + hexadecimal digits with no separator (aabbccddeeff) at the + beginning of the input. Trailing characters are ignored. + The address must be unicast and not all-zero. Anything + else is rejected: the write fails with EINVAL and the + attribute keeps its previous value. diff --git a/Documentation/ABI/testing/configfs-usb-gadget-rndis b/Documentation/ABI/testing/configfs-usb-gadget-rndis index 9416eda7fe93..a6c2bd7916ef 100644 --- a/Documentation/ABI/testing/configfs-usb-gadget-rndis +++ b/Documentation/ABI/testing/configfs-usb-gadget-rndis @@ -17,3 +17,12 @@ Description: subclass USB interface subclass, default is 06 (hex) protocol USB interface protocol, default is 00 (hex) ========= ============================================= + + host_addr and dev_addr accept a MAC address as six byte + pairs separated by ':' or '.', in any mixture + (aa:bb:cc:dd:ee:ff, aa.bb.cc.dd.ee.ff), or as twelve + hexadecimal digits with no separator (aabbccddeeff) at the + beginning of the input. Trailing characters are ignored. + The address must be unicast and not all-zero. Anything + else is rejected: the write fails with EINVAL and the + attribute keeps its previous value. diff --git a/Documentation/ABI/testing/configfs-usb-gadget-subset b/Documentation/ABI/testing/configfs-usb-gadget-subset index 0061b864351f..8a928b9ef531 100644 --- a/Documentation/ABI/testing/configfs-usb-gadget-subset +++ b/Documentation/ABI/testing/configfs-usb-gadget-subset @@ -14,3 +14,12 @@ Description: dev_addr MAC address of device's end of this Ethernet over USB link ========== ============================================= + + host_addr and dev_addr accept a MAC address as six byte + pairs separated by ':' or '.', in any mixture + (aa:bb:cc:dd:ee:ff, aa.bb.cc.dd.ee.ff), or as twelve + hexadecimal digits with no separator (aabbccddeeff) at the + beginning of the input. Trailing characters are ignored. + The address must be unicast and not all-zero. Anything + else is rejected: the write fails with EINVAL and the + attribute keeps its previous value. diff --git a/drivers/usb/gadget/Kconfig b/drivers/usb/gadget/Kconfig index 76521555e3c1..b83c42be686b 100644 --- a/drivers/usb/gadget/Kconfig +++ b/drivers/usb/gadget/Kconfig @@ -158,6 +158,7 @@ config USB_U_SERIAL config USB_U_ETHER tristate + select GENERIC_NET_UTILS config USB_U_AUDIO tristate diff --git a/drivers/usb/gadget/function/u_ether.c b/drivers/usb/gadget/function/u_ether.c index 59d85d6a84a8..dd8e4863d5fd 100644 --- a/drivers/usb/gadget/function/u_ether.c +++ b/drivers/usb/gadget/function/u_ether.c @@ -686,19 +686,19 @@ static int eth_stop(struct net_device *net) static int get_ether_addr(const char *str, u8 *dev_addr) { if (str) { - unsigned i; + bool valid; - for (i = 0; i < 6; i++) { - unsigned char num; + /* Try the delimited formats first */ + valid = parse_mac_pton(str, dev_addr, ".:"); + if (valid && is_valid_ether_addr(dev_addr)) + return 0; - if ((*str == '.') || (*str == ':')) - str++; - num = hex_to_bin(*str++) << 4; - num |= hex_to_bin(*str++); - dev_addr [i] = num; - } - if (is_valid_ether_addr(dev_addr)) + /* Fall back to the raw format */ + valid = hex2bin(dev_addr, str, ETH_ALEN) == 0; + if (valid && is_valid_ether_addr(dev_addr)) return 0; + + pr_warn("rejected ethernet address %s, using random\n", str); } eth_random_addr(dev_addr); return 1; -- 2.50.1