Skip to content

Commit 920a9fa

Browse files
pskrgagdavem330
authored andcommitted
net: asix: add proper error handling of usb read errors
Syzbot once again hit uninit value in asix driver. The problem still the same -- asix_read_cmd() reads less bytes, than was requested by caller. Since all read requests are performed via asix_read_cmd() let's catch usb related error there and add __must_check notation to be sure all callers actually check return value. So, this patch adds sanity check inside asix_read_cmd(), that simply checks if bytes read are not less, than was requested and adds missing error handling of asix_read_cmd() all across the driver code. Fixes: d9fe64e ("net: asix: Add in_pm parameter") Reported-and-tested-by: [email protected] Signed-off-by: Pavel Skripkin <[email protected]> Tested-by: Oleksij Rempel <[email protected]> Reviewed-by: Greg Kroah-Hartman <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent b845bac commit 920a9fa

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed

drivers/net/usb/asix.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,8 +192,8 @@ extern const struct driver_info ax88172a_info;
192192
/* ASIX specific flags */
193193
#define FLAG_EEPROM_MAC (1UL << 0) /* init device MAC from eeprom */
194194

195-
int asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
196-
u16 size, void *data, int in_pm);
195+
int __must_check asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
196+
u16 size, void *data, int in_pm);
197197

198198
int asix_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
199199
u16 size, void *data, int in_pm);

drivers/net/usb/asix_common.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111

1212
#define AX_HOST_EN_RETRIES 30
1313

14-
int asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
15-
u16 size, void *data, int in_pm)
14+
int __must_check asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
15+
u16 size, void *data, int in_pm)
1616
{
1717
int ret;
1818
int (*fn)(struct usbnet *, u8, u8, u16, u16, void *, u16);
@@ -27,9 +27,12 @@ int asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
2727
ret = fn(dev, cmd, USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
2828
value, index, data, size);
2929

30-
if (unlikely(ret < 0))
30+
if (unlikely(ret < size)) {
31+
ret = ret < 0 ? ret : -ENODATA;
32+
3133
netdev_warn(dev->net, "Failed to read reg index 0x%04x: %d\n",
3234
index, ret);
35+
}
3336

3437
return ret;
3538
}
@@ -79,7 +82,7 @@ static int asix_check_host_enable(struct usbnet *dev, int in_pm)
7982
0, 0, 1, &smsr, in_pm);
8083
if (ret == -ENODEV)
8184
break;
82-
else if (ret < sizeof(smsr))
85+
else if (ret < 0)
8386
continue;
8487
else if (smsr & AX_HOST_EN)
8588
break;
@@ -579,8 +582,12 @@ int asix_mdio_read_nopm(struct net_device *netdev, int phy_id, int loc)
579582
return ret;
580583
}
581584

582-
asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
583-
(__u16)loc, 2, &res, 1);
585+
ret = asix_read_cmd(dev, AX_CMD_READ_MII_REG, phy_id,
586+
(__u16)loc, 2, &res, 1);
587+
if (ret < 0) {
588+
mutex_unlock(&dev->phy_mutex);
589+
return ret;
590+
}
584591
asix_set_hw_mii(dev, 1);
585592
mutex_unlock(&dev->phy_mutex);
586593

drivers/net/usb/asix_devices.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -755,7 +755,12 @@ static int ax88772_bind(struct usbnet *dev, struct usb_interface *intf)
755755
priv->phy_addr = ret;
756756
priv->embd_phy = ((priv->phy_addr & 0x1f) == 0x10);
757757

758-
asix_read_cmd(dev, AX_CMD_STATMNGSTS_REG, 0, 0, 1, &chipcode, 0);
758+
ret = asix_read_cmd(dev, AX_CMD_STATMNGSTS_REG, 0, 0, 1, &chipcode, 0);
759+
if (ret < 0) {
760+
netdev_dbg(dev->net, "Failed to read STATMNGSTS_REG: %d\n", ret);
761+
return ret;
762+
}
763+
759764
chipcode &= AX_CHIPCODE_MASK;
760765

761766
ret = (chipcode == AX_AX88772_CHIPCODE) ? ax88772_hw_reset(dev, 0) :
@@ -919,11 +924,21 @@ static int ax88178_reset(struct usbnet *dev)
919924
int gpio0 = 0;
920925
u32 phyid;
921926

922-
asix_read_cmd(dev, AX_CMD_READ_GPIOS, 0, 0, 1, &status, 0);
927+
ret = asix_read_cmd(dev, AX_CMD_READ_GPIOS, 0, 0, 1, &status, 0);
928+
if (ret < 0) {
929+
netdev_dbg(dev->net, "Failed to read GPIOS: %d\n", ret);
930+
return ret;
931+
}
932+
923933
netdev_dbg(dev->net, "GPIO Status: 0x%04x\n", status);
924934

925935
asix_write_cmd(dev, AX_CMD_WRITE_ENABLE, 0, 0, 0, NULL, 0);
926-
asix_read_cmd(dev, AX_CMD_READ_EEPROM, 0x0017, 0, 2, &eeprom, 0);
936+
ret = asix_read_cmd(dev, AX_CMD_READ_EEPROM, 0x0017, 0, 2, &eeprom, 0);
937+
if (ret < 0) {
938+
netdev_dbg(dev->net, "Failed to read EEPROM: %d\n", ret);
939+
return ret;
940+
}
941+
927942
asix_write_cmd(dev, AX_CMD_WRITE_DISABLE, 0, 0, 0, NULL, 0);
928943

929944
netdev_dbg(dev->net, "EEPROM index 0x17 is 0x%04x\n", eeprom);

0 commit comments

Comments
 (0)