Re: [PATCH v5 2/2] usb: gadget: u_ether: fix out-of-bounds read in get_ether_addr

From: Andy Shevchenko

Date: Fri Nov 20 2026 - 04:00:02 EST


On Fri, Nov 20, 2026 at 12:02:43AM +0000, netdev-bot+sashiko@xxxxxxxxxx wrote:
> Thank you for your contribution! Sashiko AI review found 2 potential
> issue(s) to consider.
>
> Critical: 0 · High: 0 · Medium: 1 · Low: 1
>
> If you believe none of the issues are legitimate, please reply to each
> issue of Medium severity or higher and explain why the AI is wrong.
> If any of them are legitimate and you plan to respin, please let patchwork
> know by including "pw-bot: cr" as a separate line at the end of your reply
> (one such reply per series is enough).
>
> - [Medium] The commit message is wrong about how the legacy module
> parameters report a rejected address.
> - [Low] The ABI text this patch adds to configfs-usb-
> gadget-{ecm,eem,ncm,rndis,subset} says host_addr/dev_addr accept only…
>
> --
>
> usb: gadget: u_ether: fix out-of-bounds read in get_ether_addr
>
> This replaces the open coded MAC address loop in get_ether_addr() with
> parse_mac_pton() for the delimited notations and hex2bin() for the raw
> twelve digit notation. This fixes an out-of-bounds read on short strings
> and stops non-hex characters from being silently accepted. It also selects
> GENERIC_NET_UTILS and documents the accepted formats in the configfs ABI
> files for the ECM, EEM, NCM, RNDIS and subset functions.
>
> Link: https://lore.kernel.org/all/20261106030605.576584-3-liuc63@xxxxxxxxxxxx/
>
> > How a rejected address is reported depends on the caller. The legacy
> > module parameters go through gether_setup_name(), which keeps the random
> > address that get_ether_addr() falls back to and only logs a warning.
>
> [Severity: Medium]
> Is this description of the legacy path accurate?
>
> Nothing in the tree seems to call gether_setup_name(). Its only wrapper,
> the inline gether_setup() in u_ether.h, has no callers either.
>
> The legacy gadgets (legacy/ether.c, cdc2.c, ncm.c, multi.c and g_ffs.c)
> pass the dev_addr= and host_addr= parameters through
> gether_set_dev_addr() and gether_set_host_addr(). The KASAN trace earlier
> in this commit message shows the same path
> (eth_bind()->gether_set_dev_addr()->get_ether_addr()):
>
> drivers/usb/gadget/legacy/ether.c:eth_bind() {
> ...
> if (!gether_set_host_addr(net, host_addr))
> pr_info("using host ethernet address: %s", host_addr);
> if (!gether_set_dev_addr(net, dev_addr))
> pr_info("using self ethernet address: %s", dev_addr);
> ...
> }
>
> gether_set_dev_addr() parses into a local new_addr[] and returns -EINVAL
> when get_ether_addr() fails. The random fallback is thrown away:
>
> drivers/usb/gadget/function/u_ether.c:gether_set_dev_addr() {
> ...
> if (get_ether_addr(dev_addr, new_addr))
> return -EINVAL;
> ...
> }
>
> The netdev then keeps the dev_mac/host_mac that
> gether_setup_name_default() picked earlier with eth_random_addr(). Since
> eth_bind() has no else branch, nothing is logged.
>
> With this patch, "insmod g_ether.ko dev_addr=aabb.ccdd.eeff" no longer
> gives a stable aa:bb:cc:dd:ee:ff. parse_mac_pton() fails the length check
> and hex2bin() fails at the first '.'. Would such a user now get a
> different random MAC on every load, with no message explaining why?

It sounds like a good idea to add a warning on rejected addresses and say that
random is in use.

> Could the commit message be corrected to describe the
> gether_set_dev_addr() path that the legacy parameters actually take?

I think this needs to be followed and commit message needs a correction.

...

> > --- a/Documentation/ABI/testing/configfs-usb-gadget-ecm
> > +++ b/Documentation/ABI/testing/configfs-usb-gadget-ecm
> > @@ -17,4 +17,9 @@ 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). Any
> > + other format is rejected: the write fails with EINVAL and
> > + the attribute keeps its previous value.
>
> [Severity: Low]
> Does this text match what the new get_ether_addr() accepts?
>
> parse_mac_pton() only checks a minimum length and the first
> MAC_ADDR_STR_LEN characters:
>
> lib/net_utils.c:parse_mac_pton() {
> ...
> if (strnlen(s, MAC_ADDR_STR_LEN) < MAC_ADDR_STR_LEN)
> return false;
> ...
> }
>
> Its own kernel-doc says "anything after them is ignored". So writing
> "aa:bb:cc:dd:ee:ff:00" or "aa:bb:cc:dd:ee:ffXYZ" to dev_addr succeeds and
> sets aa:bb:cc:dd:ee:ff.

Yeah, the text needs an amendment. Something like
"...with no separator (aabbccddeeff) at the beginning of the input."
(see the part "at the beginning of the input" that's added).

> The hex2bin(dev_addr, str, ETH_ALEN) fallback reads exactly 12 characters
> and never looks at str[12]. "aabbccddeeff.zz" and "aabbccddeeff0011" are
> accepted as well.
>
> The reverse case is also missing from the text. Both paths in
> get_ether_addr() also require is_valid_ether_addr(). A correctly
> formatted multicast or all-zero address such as 01:00:5e:00:00:01 or
> 00:00:00:00:00:00 fails with EINVAL, and the new text doesn't mention
> this.
>
> Trailing characters do have to be tolerated, because a configfs write
> from echo ends in a newline. Could the text say that anything after the
> address is ignored, and that the address must be unicast and non-zero,
> instead of saying that every other format is rejected?
>
> The eem, ncm, rndis and subset ABI files get the same text.

All of them need to follow the same pattern, yes.

--
With Best Regards,
Andy Shevchenko