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/2026…@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 <andr…@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