All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andy Shevchenko <andriy.shevchenko@intel.com>
To: liuc63 <liuc63@xiaopeng.com>
Cc: gregkh@linuxfoundation.org, khtsai@google.com,
	fangxy@xiaopeng.com, rdunlap@infradead.org,
	dbrownell@users.sourceforge.net, linux-usb@vger.kernel.org,
	linux-kernel@vger.kernel.org, stable@vger.kernel.org
Subject: Re: [PATCH] usb: gadget: u_ether: fix out-of-bounds read in get_ether_addr
Date: Tue, 27 Oct 2026 19:08:18 +0200	[thread overview]
Message-ID: <auDagk-2QU88ofVC@ashevche-desk.local> (raw)
In-Reply-To: <20261023110113.2969708-1-liuc63@xiaopeng.com>

On Fri, Oct 23, 2026 at 07:00:59PM +0800, liuc63 wrote:
> get_ether_addr() parses a MAC address string with a fixed six-iteration
> loop, unconditionally calling hex_to_bin() twice per iteration.  It never
> checks for the terminating NUL, so a short string supplied through the
> configfs dev_addr attribute (e.g. "ab") makes the parser read past the
> end of the buffer, triggering a slab-out-of-bounds read reported by
> KASAN.  hex_to_bin() also returns -1 for non-hex characters, which is
> shifted and OR'ed into dev_addr.
> 
> Bail out as soon as the string ends or a non-hex digit is found, routing
> all failure paths through the existing eth_random_addr() fallback so that
> dev_addr is always fully initialized before the function returns.

Instead, in lib/net_utils.c:

bool parse_mac_pton(const char *s, u8 *mac, const char *delim)
{
	...
		if (i != ETH_ALEN - 1 && !strchr(delim, s[i * 3 + 2]))
	...
}

In include/linux/hex.h:

static bool mac_pton(const char *s, u8 *mac)
{
	return parse_mac_pton(s, mac, ":");
}

and here

static int get_ether_addr(const char *str, u8 *dev_addr)
{
        if (str) {
		bool valid;

		valid = parse_mac_pton(str, dev_addr, ".:");
		if (valid && is_valid_ether_addr(dev_addr))
			return 0;
        }
        eth_random_addr(dev_addr);
        return 1;
}

Note, this will require select NET_UTILS in Kconfig to be added.

Make it two patches (first prepares parse_mac_pton() for others to use).

-- 
With Best Regards,
Andy Shevchenko



      parent reply	other threads:[~2026-10-27 17:08 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-23 11:00 [PATCH] usb: gadget: u_ether: fix out-of-bounds read in get_ether_addr liuc63
2026-10-23 11:09 ` sashiko-bot
2026-10-27 17:08 ` Andy Shevchenko [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=auDagk-2QU88ofVC@ashevche-desk.local \
    --to=andriy.shevchenko@intel.com \
    --cc=dbrownell@users.sourceforge.net \
    --cc=fangxy@xiaopeng.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=khtsai@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    --cc=liuc63@xiaopeng.com \
    --cc=rdunlap@infradead.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.