usb: gadget: u_ether: fix out-of-bounds read in get_ether_addr
Subsystems (4)
v7 cover usb: gadget: u_ether: fix out-of-bounds read in get_ether_addr
Rework of
[PATCH v6] usb: gadget: u_ether: fix out-of-bounds read in get_ether_addr
https://lore.kernel.org/all/[email protected]/
following Andy's reply on the v6 2/2 thread: 1/2 prepares a parse_mac_pton() helper in lib/net_utils.c, 2/2 uses it in get_ether_addr().
The two patches belong to different trees: 1/2 touches lib/ and should go via netdev or -mm, 2/2 is for the USB tree. 2/2 depends on 1/2, so it can only be applied once 1/2 has reached Linus' tree.
Changes since v6:
- 2/2: as Andy suggested, consolidate the "rejected address, using random" warning into a single pr_warn inside get_ether_addr(), at the tail of the if (str) block; the two setters go back to their plain pre-v6 form (no warn at their call sites, no per-caller string, and the enclosing if (str) makes the NULL default path silent by construction)
- 2/2: drop the unreferenced Link: trailer from the commit message
- 1/2: unchanged
Changes since v5:
- 2/2: correct the commit message so that the legacy module parameters are described via their real call path (gether_set_dev_addr() and gether_set_host_addr()); add a warning on a rejected caller-supplied address; 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
Changes since v4:
- no driver code change; only documentation and the commit messages
- 1/2: reword the parse_mac_pton() kernel-doc to say at least MAC_ADDR_STR_LEN characters, only the first MAC_ADDR_STR_LEN checked
- 2/2: describe the configfs behaviour, name all five functions, restore a Fixes: tag for 1da177e4c3f4 ("Linux-2.6.12-rc2") keeping the real introducing commit in prose, and say "minimum length"
Changes since v3:
- no code change, only the log of 2/2 changed; name 331a2d82bcaf from v2.6.7 as the commit that introduced the loop; pick up the Reviewed-by
Changes since v2:
- 1/2: pick up Andy's Reviewed-by, no code change
- 2/2: keep the separator-less notation "aabbccddeeff" working by falling back to hex2bin() when parse_mac_pton() does not match
- 2/2: spell out in the log which notations stop being accepted
Changes since v1:
- rework the fix on top of parse_mac_pton() as suggested by Andy instead of open coding the bounds checks in get_ether_addr()
- add the KASAN report and the malformed address example
- select GENERIC_NET_UTILS for USB_U_ETHER
One user visible effect remains: the Cisco notation "aabb.ccdd.eeff" and irregular groupings such as "123456:789abc" are no longer parsed. A configfs store then fails with -EINVAL and keeps the previous value, while the legacy module parameters keep the random address set up earlier and, with this version, log a single warning naming the rejected address.
Liu Chao (2):
lib/net_utils: allow mac_pton() to take a custom delimiter
usb: gadget: u_ether: fix out-of-bounds read in get_ether_addr
.../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 +++++++++---------- include/linux/hex.h | 7 ++++++- lib/net_utils.c | 19 +++++++++++++++--- 9 files changed, 76 insertions(+), 15 deletions(-)
base-commit: 24a19b953e2a3d0ea5a8c4951a0ab8f32720c907
Signature · 1 line
2.50.1
PATCH v7 1/2 lib/net_utils: allow mac_pton() to take a custom delimiter
mac_pton() hardcodes ':' as the only accepted separator, which pushes callers that need a different notation to open-code their own parser.
Split the body out into parse_mac_pton() taking the set of accepted delimiters as a string, and turn mac_pton() into a static inline wrapper passing ":" so that every existing caller keeps its current behaviour. The strnlen() guard still runs before any delimiter is inspected, so strchr() is never handed a NUL character.
No functional change intended.
---
v7: no change.
v6: no change.
v5: reword the parse_mac_pton() kernel-doc to say @s must be at least
MAC_ADDR_STR_LEN characters long and that only the first MAC_ADDR_STR_LEN characters are checked, the rest ignored. No code change.
v4: no change.
v3: pick up Andy's Reviewed-by, no code change.
v2: new patch, split out of the u_ether fix at Andy's suggestion.
include/linux/hex.h | 7 ++++++- lib/net_utils.c | 19 ++++++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-)
include/linux/hex.h +6 -1
@@ -30,6 +30,11 @@ extern int hex_to_bin(unsigned char ch);extern int __must_check hex2bin (u8 *dst,const char *src,size_t count);extern char *bin2hex (char *dst,const void *src,size_t count); -bool mac_pton (const char *s,u8 *mac); +bool parse_mac_pton (const char *s,u8 *mac,const char *delim); + +static inline bool mac_pton (const char *s,u8 *mac) +{ +return parse_mac_pton (s, mac,":" ); +}#endif
lib/net_utils.c +16 -3
@@ -5,7 +5,20 @@#include <linux/export.h> #include <linux/hex.h> -bool mac_pton (const char *s,u8 *mac) +/** +* parse_mac_pton - convert a MAC address string using a custom delimiter +* @s: string to be parsed +* @mac: buffer to store the result, left untouched if @s is malformed +* @delim: characters accepted as a separator between the byte pairs +* +* @s must be at least MAC_ADDR_STR_LEN characters long and use a character +* from @delim at every separator position, e.g. "aa.bb:cc.dd:ee.ff" with +* ".:". Only the first MAC_ADDR_STR_LEN characters are checked; anything +* after them is ignored. +* +* Return: true on success, false if @s is malformed. +*/ +bool parse_mac_pton (const char *s,u8 *mac,const char *delim) {int i;
@@ -16,7 +29,7 @@ bool mac_pton(const char *s, u8 *mac)for (i =0 ; i <ETH_ALEN ; i++) {if (!isxdigit (s[i *3 ]) || !isxdigit (s[i *3 +1 ]))return false; -if (i !=ETH_ALEN -1 && s[i *3 +2 ] !=':' ) +if (i !=ETH_ALEN -1 && !strchr (delim, s[i *3 +2 ]))return false; }for (i =0 ; i <ETH_ALEN ; i++) {
@@ -24,4 +37,4 @@ bool mac_pton(const char *s, u8 *mac) }return true; } -EXPORT_SYMBOL (mac_pton); +EXPORT_SYMBOL (parse_mac_pton);
Signature · 1 line
2.50.1
PATCH v7 2/2 usb: gadget: u_ether: fix out-of-bounds read in get_ether_addr
In reply to Liu Chaoget_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.
---
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.
.../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(-)
Documentation/ABI/testing/configfs-usb-gadget-ecm +8 -1
@@ -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.
Documentation/ABI/testing/configfs-usb-gadget-eem +9 -0
@@ -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.
Documentation/ABI/testing/configfs-usb-gadget-ncm +8 -0
@@ -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.
Documentation/ABI/testing/configfs-usb-gadget-rndis +9 -0
@@ -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.
Documentation/ABI/testing/configfs-usb-gadget-subset +9 -0
@@ -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.
drivers/usb/gadget/Kconfig +1 -0
@@ -158,6 +158,7 @@ config USB_U_SERIAL config USB_U_ETHER tristate + select GENERIC_NET_UTILS config USB_U_AUDIO tristate
drivers/usb/gadget/function/u_ether.c +10 -10
@@ -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 ;
Signature · 1 line
2.50.1
Greg Kroah-Hartman
maintainerQuoted 9 lines · Liu Chao
get_ether_addr()walks the MAC address string with a fixed six iteration loop, callinghex_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. Thedev_addrandhost_addrmodule parameters of the legacy gadget drivers are copied into their own kmalloc'ed buffer byparam_set_charp(), so the overrun crosses a slab boundary and KASAN reports it at insmod time:# insmod g_ether.ko dev_addr=ab
Hey, don't do that and all will be fine :)
One can argue, this isn't a real issue at all, right? You did something foolish as root and boom, things blew up.
Quoted 75 lines
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 intodev_addrunchecked, 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:ffThe configfs
dev_addrandhost_addrattributes of the ECM, EEM, NCM, RNDIS and subset functions reach the same parser throughgether_set_dev_addr()andgether_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 andhex2bin()for the raw one, and keep the existingeth_random_addr()fallback for everything both of them reject.parse_mac_pton()checks the minimum length and every digit before writing anything todev_addr, andhex2bin()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()andgether_set_host_addr(), which return-EINVAL, so the store fails and the attribute keeps its previous value: writing "aabb.ccdd.eeff" todev_addrused 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 thatgether_setup_name_default()picked when it was created, with nothing in the log to say why the requested address was not used. Makeget_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_UTILSso thatparse_mac_pton()is built in.
Quoted 8 lines
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 indrivers/usb/gadget/ether.c, and commit2b3d942c4878("usb ethernet gadget: split out network core") copied it intou_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, commit1da177e4c3f4("Linux-2.6.12-rc2"), and the commit that actually introduced the loop is named here in prose.
This wall of text is insane. Please delete it all and just write it on your own without a LLM.
Quoted 53 lines
Suggested-by Andy Shevchenko <[email protected]>Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")Signed-off-by Liu Chao <[email protected]>---
v7: as Andy suggested, place the warning once insideget_ether_addr(),
at the tail of the if (str) block, and revert bothgether_set_dev_addr()andgether_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
throughgether_setup_name()(it has no callers), they go throughgether_set_dev_addr()andgether_set_host_addr(), which return-EINVALand leave the random address picked at setup in place; make both setters warn whenget_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-EINVALand 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 for1da177e4c3f4("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; name331a2d82bcaffrom 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 tohex2bin()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 ofparse_mac_pton(), add the KASAN report and the
malformed address example, selectGENERIC_NET_UTILS, drop Cc: stable and the Reviewed-by..../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(-)Documentation/ABI/testing/configfs-usb-gadget-ecm +0
-0@@ -17,4 +17,11 @@ Description: - MAC address of device's end of this Ethernet over USB link
Documentation/ABI/testing/configfs-usb-gadget-ecm Description: · line 27
Quoted 10 lines
Documentation/ABI/testing/configfs-usb-gadget-ecm +8
-1- + 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.
This all is assumed, no need to add this, right?
Quoted 17 lines
Documentation/ABI/testing/configfs-usb-gadget-ecm +5
-6@@ -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;
Documentation/ABI/testing/configfs-usb-gadget-ecm static int eth_stop(struct net_device *net) · line 701
Quoted 10 lines
Documentation/ABI/testing/configfs-usb-gadget-ecm +5
-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);
dev_err()?
Why not be just more strict here and if the size is not ok, fail then? This should be a very simple "parser".
thanks,
greg k-h
[email protected]
AI reviewSashiko has reviewed this 2-patch series and found no issues.
Sashiko has reviewed this 2-patch series and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]
Greg Kroah-Hartman
maintainerQuoted 11 lines · Liu Chao
Rework of
[PATCH v6] usb: gadget:
u_ether: fix out-of-bounds read inget_ether_addr
https://lore.kernel.org/all/[email protected]/following Andy's reply on the v6 2/2 thread: 1/2 prepares a
parse_mac_pton()helper inlib/net_utils.c, 2/2 uses it inget_ether_addr().The two patches belong to different trees: 1/2 touches
lib/and should go via netdev or -mm, 2/2 is for the USB tree. 2/2 depends on 1/2, so it can only be applied once 1/2 has reached Linus' tree.
That's not going to work well, are you sure this is the only way to resolve this?
This feels like a crazy complex patch for an issue that is self-inflicted. Why not just resolve it in the gadget driver alone if it wants to handle such crazy inputs?
greg k-h
Quoted 13 lines · Greg Kroah-Hartman
Quoted 11 lines · Liu Chao
Rework of
[PATCH v6] usb: gadget:
u_ether: fix out-of-bounds read inget_ether_addr
https://lore.kernel.org/all/[email protected]/following Andy's reply on the v6 2/2 thread: 1/2 prepares a
parse_mac_pton()helper inlib/net_utils.c, 2/2 uses it inget_ether_addr().The two patches belong to different trees: 1/2 touches
lib/and should go via netdev or -mm, 2/2 is for the USB tree. 2/2 depends on 1/2, so it can only be applied once 1/2 has reached Linus' tree.
Quoted 6 lines
That's not going to work well, are you sure this is the only way to resolve this?
This feels like a crazy complex patch for an issue that is self-inflicted. Why not just resolve it in the gadget driver alone if it wants to handle such crazy inputs?
lib patch may easily go via USB tree. There is a made-up issue.
Signature · 2 lines
With Best Regards, Andy Shevchenko