Article View: linux.kernel.netdev
Article #24652Re: [PATCH v6 2/2] usb: gadget: u_ether: fix out-of-bounds read in get_ether_addr
From: Andy Shevchenko
Date: Mon, 30 Nov 2026 14:53
Date: Mon, 30 Nov 2026 14:53
140 lines
15608 bytes
15608 bytes
On Sat, Nov 28, 2026 at 04:07:15PM +0800, Liu Chao wrote:
> 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 both
> setters warn when get_ether_addr() rejects an address the caller
> actually supplied, so that 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.
> Link: https://lore.kernel.org/all/20261117090110.3263592-3-liuc63@xiaopeng.com/
Usually we don't put unreferred Link tags. I don't see something like [1] and
the respective reference in the commit message above.
> Suggested-by: Andy Shevchenko <andriy.shevchenko@intel.com>
> Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
...
> 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;
> }
If it's a pr_*(), why not place it here once for all?
> eth_random_addr(dev_addr);
pr_warn("rejected device ethernet address %s, use random\n", str);
--
With Best Regards,
Andy Shevchenko
Technical Details
Message-ID:
<aw2N_CPGuC0V4nWX@ashevche-desk.local>Path:
mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!2.eu.feeder.erje.net!feeder.erje.net!feeder3.feed.ams11.usenet.farm!feed.usenet.farm!feeder.usenetexpress.com!tr3.eu1.usenetexpress.com!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-374258-linux-kernel-netdev=dewy.ruReferences:
<20261128080722.1469508-1-liuc63@xiaopeng.com> <20261128080722.1469508-3-liuc63@xiaopeng.com>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!2.eu.feeder.erje.net!feeder.erje.net!feeder3.feed.ams11.usenet.farm!feed.usenet.farm!feeder.usenetexpress.com!tr3.eu1.usenetexpress.com!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-374258-linux-kernel-netdev=dewy.ru From: andriy.shevchenko@intel.com (Andy Shevchenko) Newsgroups: linux.kernel.netdev Subject: Re: [PATCH v6 2/2] usb: gadget: u_ether: fix out-of-bounds read in get_ether_addr Date: Mon, 30 Nov 2026 16:53:48 +0200 Organization: Usenet Network Message-ID: <aw2N_CPGuC0V4nWX@ashevche-desk.local> References: <20261128080722.1469508-1-liuc63@xiaopeng.com> <20261128080722.1469508-3-liuc63@xiaopeng.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Injection-Info: usenet.network; logging-data="1449883"; mail-complaints-to="abuse@usenet.network" Cancel-Lock: sha1:GW9qqo13hJ2PTYeyHfjkPUcKSgs= sha256:CeauyjvPa5jIUM2mClS5x6qen9FYrqMacENMhrA9lMI= sha1:FtDwAUFcpSCcBkPgaBCCUVBkI94= sha256:n8Cm17oaFe/bqxGrhWvkdXq5Z05ASwV4qNgxdJ7K230= Content-Disposition: inline List-ID: <netdev.vger.kernel.org> X-Mailpost-Empty-Hdrs: X-Spam-Level X-Received: from localhost (localhost [127.0.0.1]) by mail.dewy.ru (Postfix) with ESMTP id 55CD7125FF1; Mon, 30 Nov 2026 17:54:21 +0300 (MSK) X-Virus-Scanned: Debian amavisd-new at usenet.network X-Spam-Flag: NO X-Spam-Score: -1.952 X-Spam-Status: No, score=-1.952 tagged_above=-9999 required=6.31 tests=[DCC_CHECK=1.1, DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, HEADER_FROM_DIFFERENT_DOMAINS=0.249, MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001] autolearn=ham autolearn_force=no X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key) header.d=intel.com X-Received: from mail.dewy.ru ([127.0.0.1]) by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id k9XrbDpdGRY1; Mon, 30 Nov 2026 17:54:17 +0300 (MSK) X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-374258-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=intel.com X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10]) by mail.dewy.ru (Postfix) with ESMTPS id 71BD4125FED; Mon, 30 Nov 2026 17:54:05 +0300 (MSK) X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92]) by sea.lore.kernel.org (Postfix) with ESMTP id B3CF32E797; Mon, 30 Nov 2026 14:54:03 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass; t=1796050443; b=KcqZNQS/cZZ53gxd5Y1AO7qGgmrbXMHlVEtCAnYGzFHgx50yZFX5itimCSIwhpahmqaB jLPfeJ+peApZnHwJflAfcWiXH1bVXflh2FwkYJNCDTHYcfO+XneQygwD/624jtoG4MOa3 uGtqzFQsM95BrvemfNe+tvjJsmcHI5AqJCPerCLw6v4Zzllf7Vjv/QIMUrA4PTBNLgNXK AwhvzjctGcjgmIKlRot9uuOrsg3A/fqepFouF81VwFHGiE2TztSbOcQMyM952hdZUt4le c098R5E6pjS42BU87lbE82QHzPZCVbtc6DYT5CI3Gep0WbmgGoTBKugBOyJ/M/V5nDA== X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796050443; h=X-Original-To:Received:DMARC-Filter:DKIM-Signature: X-CSE-ConnectionGUID:X-CSE-MsgGUID:X-IronPort-AV:X-IronPort-AV: Received:X-CSE-ConnectionGUID:X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV: Received:Date:From:To:Cc:Subject:Message-ID:References:X-Mailing-List: List-Id:List-Post:List-Help:List-Subscribe:List-Unsubscribe: List-Owner:Precedence:MIME-Version:Content-Type:Content-Disposition: In-Reply-To:Organization; bh=Nwn06DX8SYBsbHSvI9RU53w2xBTuI7XdGFC8YRFIrdM=; b=lgRwbStKcGGqGlhI+RkidzSFu4kfgg1/+caSgBwlVQlilr5TL+gmPtbjeSTaXZn8DnJ9 ktlLfpIIP2ZpdTCUnVKf7enBubG9ncUki0ovvk14zeBLEvdQ2RLwDXBIPTyWl+eB8UzM4 sfUuAm1l/7KYQF7f+wCyPy6Stib4QjADSSrfgDtt3UM6wVzIukl+1OvbQMaaY6+qRPJKU 2TcTXyJWqq2uUx36UwNvXHdXG3aalr1GocYVsiUVvk1ujRhbmG4v39jMfGaSbF46O9FO5 D+CtuiQfcnDU9RhHLVqBbWkPkbzjE+PCYj08reNoIt/naQGFOLFntuP0TBT3/hOlchA== X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b="Lp5/3T6A"; dmarc=pass header.from=intel.com; spf=pass smtp.mailfrom=intel.com; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1 X-Received: from localhost.localdomain (localhost [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 635FB606A325; Mon, 30 Nov 2026 14:54:03 +0000 (UTC) X-Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=Lp5/3T6A X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=192.198.163.11 X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796050437; b=AqLwTPqbu6eCjPuB9o77ygMFQPIfmWwEdtm/iLcLe7jLd1p/GaKxkVCCU/KeKpunT/me zfZDhU6QkLlECVt1bahK0g2dnMuY2Wj/VxueJE7wGHlUUcrebWAp1LvqcTmceUqaUyKYm XC5xazpX7txCI6yV34SUADvW3kjz2tJwnFBRzBT17N+oh/pNjuCDpDgpFRwk/1Sg50oC8 Nga4bGkPZU608yhfT1LTrnldM9cfH76iy1N4ySlgiNHG5roDIcOnAgFqmYeVLtL3NOOhr zsYG0U+yhZHMJQY4IZE0TBBZwzAULmG5BjB9AN6eSvYvSTGJxJAUuNAYU6dP2wwiNTQ== X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796050437; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:Date:From:To:Cc: Subject:Message-ID:References:MIME-Version:Content-Type: Content-Disposition:In-Reply-To:Organization; bh=Nwn06DX8SYBsbHSvI9RU53w2xBTuI7XdGFC8YRFIrdM=; b=ZuLke18oleq2LKlFHZjjirhXbPyMGAPOR9UiNF8TfhGwd2kkRZ76d0Ddz+E80S67UDKU KrTNS3INV/t7upv/KR3TInXfKk3J+Tx5e9H6je5cJ3CRwpCy0psK12dZA5hUYJ5ZAxWlC f45SCPeGktmIHZDtkfqb6eStJVyjK3g3iQsM1wVCq6FBAOzVVr56SiGBNCiNuktLO6tG5 datdOiYLzenLzbVzXUesjmJlAkyH088EUp731dn/W1zaSIyTmGkihxssYmJbylSmKQU0O 5wMm6mT+lJ9qo9ixrLrR3lN5Zt4CzfnvaphthGxkWjHk1YLpXXHL5vmVnkRVBkePnzQ== X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b="Lp5/3T6A"; arc=none smtp.remote-ip=192.198.163.11 X-Received: from mgamail.intel.com (mgamail.intel.com [192.198.163.11]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id EF2BB606A31C; Mon, 30 Nov 2026 14:53:56 +0000 (UTC) X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org EF2BB606A31C X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796050437; x=1827586437; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=Nwn06DX8SYBsbHSvI9RU53w2xBTuI7XdGFC8YRFIrdM=; b=Lp5/3T6Ajzyr10SNn3Q3XMdOHk05o5cceokdG2mRPK+7zyA4w+bmUZZP 1FNsvHwwiWu9irkjsZ8Lg/rdvlFvAurTadh1d/fa9m8nCGDIlBHY5z6KB aJD/4JjD8HrsMrMfujPgiHz9ATp7/glolZDvOBqNRZmQnoW5v/3nxOJv3 hbRJSy0JdACPoorQ6ZEWMUYPkm2vtss/2mkvWO06DxGu8/cyDeNbinpFm OhG1lUEoL8nRMAiU3irGudePTai2jQB0FyJ/+pl3plHAjHyblQ4a+AerQ IQig8qG5wjr4NLp3kUX6Eyc5w83u09SUorv8yv2SFa5CPhRmgTbGXWb68 Q==; X-CSE-ConnectionGUID: DR8vgavzSXqXNJw3w3PxXg== X-CSE-MsgGUID: JYilNVdISeGNjm/+9E/+pg== X-IronPort-AV: E=McAfee;i="6800,10657,11982"; a="4798542" X-IronPort-AV: E=Sophos;i="6.27,242,1787036400"; d="scan'208";a="4798542" X-Received: from fmviesa007.fm.intel.com ([10.60.135.147]) by fmvoesa105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Nov 2026 06:53:56 -0800 X-CSE-ConnectionGUID: rN1Mk2uxQCWHkPrHv/3OMA== X-CSE-MsgGUID: UMWfyHG5RYKQqjjkwN7agg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,242,1787036400"; d="scan'208";a="13275870" X-Received: from abityuts-desk1.ger.corp.intel.com (HELO localhost) ([10.245.244.117]) by smtpauth.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Nov 2026 06:53:51 -0800 X-Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>, Andrew Morton <akpm@linux-foundation.org>, "David S . Miller" <davem@davemloft.net>, Eric Dumazet <edumazet@kernel.org>, Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>, Simon Horman <horms@kernel.org>, khtsai@google.com, fangxy@xiaopeng.com, Randy Dunlap <rdunlap@infradead.org>, dbrownell@users.sourceforge.net, linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org, netdev@vger.kernel.org X-Mailing-List: netdev@vger.kernel.org X-List-Post: <mailto:netdev@vger.kernel.org> X-List-Help: <mailto:netdev+help@vger.kernel.org> X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org> X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org> X-List-Owner: <mailto:netdev+owner@vger.kernel.org> X-Precedence: list X-In-Reply-To: <20261128080722.1469508-3-liuc63@xiaopeng.com> X-Organization: Intel Finland Oy - BIC 0357606-4 - c/o Alberga Business Park, 6 krs, Bertel Jungin Aukio 5, 02600 Espoo