[PATCH 2/2] regmap: mdio: Reject invalid clause-22 addresses

From: Sander Vanheule
Date: Wed Jun 09 2021 - 07:46:28 EST


When an invalid register offset is provided, the upper bits are silently
discarded. Change this to return -ENXIO instead, to help catch potential
bugs.

Signed-off-by: Sander Vanheule <sander@xxxxxxxxxxxxx>
---
drivers/base/regmap/regmap-mdio.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/drivers/base/regmap/regmap-mdio.c b/drivers/base/regmap/regmap-mdio.c
index aee34bf2400e..9adfb82be8f1 100644
--- a/drivers/base/regmap/regmap-mdio.c
+++ b/drivers/base/regmap/regmap-mdio.c
@@ -13,7 +13,10 @@ static int regmap_mdio_read(void *context, unsigned int reg, unsigned int *val)
struct mdio_device *mdio_dev = context;
int ret;

- ret = mdiobus_read(mdio_dev->bus, mdio_dev->addr, reg & REGNUM_C22_MASK);
+ if (unlikely(reg & ~REGNUM_C22_MASK))
+ return -ENXIO;
+
+ ret = mdiobus_read(mdio_dev->bus, mdio_dev->addr, reg);
if (ret < 0)
return ret;

@@ -25,7 +28,10 @@ static int regmap_mdio_write(void *context, unsigned int reg, unsigned int val)
{
struct mdio_device *mdio_dev = context;

- return mdiobus_write(mdio_dev->bus, mdio_dev->addr, reg & REGNUM_C22_MASK, val);
+ if (unlikely(reg & ~REGNUM_C22_MASK))
+ return -ENXIO;
+
+ return mdiobus_write(mdio_dev->bus, mdio_dev->addr, reg, val);
}

static const struct regmap_bus regmap_mdio_bus = {
--
2.31.1