* [PATCH] net: usb: pegasus: Proper error handing when setting pegasus' MAC address
@ 2020-09-29 11:40 Petko Manolov
2020-10-02 1:42 ` David Miller
0 siblings, 1 reply; 4+ messages in thread
From: Petko Manolov @ 2020-09-29 11:40 UTC (permalink / raw)
To: netdev; +Cc: davem, Petko Manolov
Fix a bug in set_ethernet_addr() which does not take into account possible
errors (or partial reads) returned by its helpers. This can potentially lead to
writing random data into device's MAC address registers.
Signed-off-by: Petko Manolov <petko.manolov@konsulko.com>
---
drivers/net/usb/pegasus.c | 35 ++++++++++++++++++++++++++---------
1 file changed, 26 insertions(+), 9 deletions(-)
diff --git a/drivers/net/usb/pegasus.c b/drivers/net/usb/pegasus.c
index e92cb51a2c77..25855f976c1b 100644
--- a/drivers/net/usb/pegasus.c
+++ b/drivers/net/usb/pegasus.c
@@ -360,28 +360,45 @@ static int write_eprom_word(pegasus_t *pegasus, __u8 index, __u16 data)
}
#endif /* PEGASUS_WRITE_EEPROM */
-static inline void get_node_id(pegasus_t *pegasus, __u8 *id)
+static inline int get_node_id(pegasus_t *pegasus, u8 *id)
{
- int i;
- __u16 w16;
+ int i, ret;
+ u16 w16;
for (i = 0; i < 3; i++) {
- read_eprom_word(pegasus, i, &w16);
+ ret = read_eprom_word(pegasus, i, &w16);
+ if (ret < 0)
+ return ret;
((__le16 *) id)[i] = cpu_to_le16(w16);
}
+
+ return 0;
}
-static void set_ethernet_addr(pegasus_t *pegasus)
+static int set_ethernet_addr(pegasus_t *pegasus)
{
- __u8 node_id[6];
+ int ret;
+ u8 node_id[6];
if (pegasus->features & PEGASUS_II) {
- get_registers(pegasus, 0x10, sizeof(node_id), node_id);
+ ret = get_registers(pegasus, 0x10, sizeof(node_id), node_id);
+ if (ret < 0)
+ goto err;
} else {
- get_node_id(pegasus, node_id);
- set_registers(pegasus, EthID, sizeof(node_id), node_id);
+ ret = get_node_id(pegasus, node_id);
+ if (ret < 0)
+ goto err;
+ ret = set_registers(pegasus, EthID, sizeof(node_id), node_id);
+ if (ret < 0)
+ goto err;
}
+
memcpy(pegasus->net->dev_addr, node_id, sizeof(node_id));
+
+ return 0;
+err:
+ dev_err(&pegasus->intf->dev, "device's MAC address not set.\n");
+ return ret;
}
static inline int reset_mac(pegasus_t *pegasus)
--
2.28.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] net: usb: pegasus: Proper error handing when setting pegasus' MAC address
2020-09-29 11:40 [PATCH] net: usb: pegasus: Proper error handing when setting pegasus' MAC address Petko Manolov
@ 2020-10-02 1:42 ` David Miller
2020-10-02 7:56 ` [PATCH v2] " Petko Manolov
2020-10-02 7:57 ` [PATCH] " Petko Manolov
0 siblings, 2 replies; 4+ messages in thread
From: David Miller @ 2020-10-02 1:42 UTC (permalink / raw)
To: petko.manolov; +Cc: netdev
From: Petko Manolov <petko.manolov@konsulko.com>
Date: Tue, 29 Sep 2020 14:40:39 +0300
> -static void set_ethernet_addr(pegasus_t *pegasus)
> +static int set_ethernet_addr(pegasus_t *pegasus)
> {
You change this to return an 'int' but no callers were updated to check it.
Furthermore, failure to probe a MAC address can be resolved by
choosing a random MAC address. This handling is preferrable because
it allows the interface to still come up successfully.
^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH v2] net: usb: pegasus: Proper error handing when setting pegasus' MAC address
2020-10-02 1:42 ` David Miller
@ 2020-10-02 7:56 ` Petko Manolov
2020-10-02 7:57 ` [PATCH] " Petko Manolov
1 sibling, 0 replies; 4+ messages in thread
From: Petko Manolov @ 2020-10-02 7:56 UTC (permalink / raw)
To: netdev; +Cc: davem, Petko Manolov
v2:
If reading the MAC address from eeprom fail don't throw an error, use randomly
generated MAC instead. Either way the adapter will soldier on and the return
type of set_ethernet_addr() can be reverted to void.
v1:
Fix a bug in set_ethernet_addr() which does not take into account possible
errors (or partial reads) returned by its helpers. This can potentially lead to
writing random data into device's MAC address registers.
Signed-off-by: Petko Manolov <petko.manolov@konsulko.com>
---
drivers/net/usb/pegasus.c | 34 ++++++++++++++++++++++++++--------
1 file changed, 26 insertions(+), 8 deletions(-)
diff --git a/drivers/net/usb/pegasus.c b/drivers/net/usb/pegasus.c
index e92cb51a2c77..39b78d8fcc79 100644
--- a/drivers/net/usb/pegasus.c
+++ b/drivers/net/usb/pegasus.c
@@ -360,28 +360,47 @@ static int write_eprom_word(pegasus_t *pegasus, __u8 index, __u16 data)
}
#endif /* PEGASUS_WRITE_EEPROM */
-static inline void get_node_id(pegasus_t *pegasus, __u8 *id)
+static inline int get_node_id(pegasus_t *pegasus, u8 *id)
{
- int i;
- __u16 w16;
+ int i, ret;
+ u16 w16;
for (i = 0; i < 3; i++) {
- read_eprom_word(pegasus, i, &w16);
+ ret = read_eprom_word(pegasus, i, &w16);
+ if (ret < 0)
+ return ret;
((__le16 *) id)[i] = cpu_to_le16(w16);
}
+
+ return 0;
}
static void set_ethernet_addr(pegasus_t *pegasus)
{
- __u8 node_id[6];
+ int ret;
+ u8 node_id[6];
if (pegasus->features & PEGASUS_II) {
- get_registers(pegasus, 0x10, sizeof(node_id), node_id);
+ ret = get_registers(pegasus, 0x10, sizeof(node_id), node_id);
+ if (ret < 0)
+ goto err;
} else {
- get_node_id(pegasus, node_id);
- set_registers(pegasus, EthID, sizeof(node_id), node_id);
+ ret = get_node_id(pegasus, node_id);
+ if (ret < 0)
+ goto err;
+ ret = set_registers(pegasus, EthID, sizeof(node_id), node_id);
+ if (ret < 0)
+ goto err;
}
+
memcpy(pegasus->net->dev_addr, node_id, sizeof(node_id));
+
+ return;
+err:
+ eth_hw_addr_random(pegasus->net);
+ dev_info(&pegasus->intf->dev, "software assigned MAC address.\n");
+
+ return;
}
static inline int reset_mac(pegasus_t *pegasus)
--
2.28.0
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH] net: usb: pegasus: Proper error handing when setting pegasus' MAC address
2020-10-02 1:42 ` David Miller
2020-10-02 7:56 ` [PATCH v2] " Petko Manolov
@ 2020-10-02 7:57 ` Petko Manolov
1 sibling, 0 replies; 4+ messages in thread
From: Petko Manolov @ 2020-10-02 7:57 UTC (permalink / raw)
To: David Miller; +Cc: netdev
On 20-10-01 18:42:18, David Miller wrote:
> From: Petko Manolov <petko.manolov@konsulko.com>
> Date: Tue, 29 Sep 2020 14:40:39 +0300
>
> > -static void set_ethernet_addr(pegasus_t *pegasus)
> > +static int set_ethernet_addr(pegasus_t *pegasus)
> > {
>
> You change this to return an 'int' but no callers were updated to check it.
>
> Furthermore, failure to probe a MAC address can be resolved by choosing a
> random MAC address. This handling is preferrable because it allows the
> interface to still come up successfully.
Thanks for looking into this. V2 already sent.
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2020-10-02 7:57 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-09-29 11:40 [PATCH] net: usb: pegasus: Proper error handing when setting pegasus' MAC address Petko Manolov
2020-10-02 1:42 ` David Miller
2020-10-02 7:56 ` [PATCH v2] " Petko Manolov
2020-10-02 7:57 ` [PATCH] " Petko Manolov
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.