linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sander Vanheule <sander@svanheule.net>
To: Mark Brown <broonie@kernel.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	"Rafael J . Wysocki" <rafael@kernel.org>,
	linux-kernel@vger.kernel.org
Cc: Andy Shevchenko <andy.shevchenko@gmail.com>,
	Sander Vanheule <sander@svanheule.net>,
	Andrew Lunn <andrew@lunn.ch>
Subject: [PATCH] regmap: mdio: Reject invalid clause-22 addresses
Date: Sat,  5 Jun 2021 10:31:18 +0200	[thread overview]
Message-ID: <20210605083116.12786-1-sander@svanheule.net> (raw)
In-Reply-To: <cover.1622743333.git.sander@svanheule.net>

Currently a regmap configuration for regmap-mdio must have a register
address width of 5 bits (cf. clause-22 register access). This is not
enforced on the provided register addresses, which would enable
clause-45 MDIO bus access, if the right bit packing is used.

Prevent clause-45 access, and other invalid addresses, by verifying the
provided register address.

Cc: Andrew Lunn <andrew@lunn.ch>
Signed-off-by: Sander Vanheule <sander@svanheule.net>
---
 drivers/base/regmap/regmap-mdio.c | 15 ++++++++++++---
 1 file changed, 12 insertions(+), 3 deletions(-)

diff --git a/drivers/base/regmap/regmap-mdio.c b/drivers/base/regmap/regmap-mdio.c
index 5ec208279913..3b842cf1eef9 100644
--- a/drivers/base/regmap/regmap-mdio.c
+++ b/drivers/base/regmap/regmap-mdio.c
@@ -5,16 +5,22 @@
 #include <linux/module.h>
 #include <linux/regmap.h>
 
+#define REGVAL_MASK		GENMASK(15, 0)
+#define REGNUM_C22_MASK		GENMASK(4, 0)
+
 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);
+	if (unlikely(reg & ~REGNUM_C22_MASK))
+		return -ENXIO;
+
+	ret = mdiobus_read(mdio_dev->bus, mdio_dev->addr, reg & REGNUM_C22_MASK);
 	if (ret < 0)
 		return ret;
 
-	*val = ret & 0xffff;
+	*val = ret & REGVAL_MASK;
 	return 0;
 }
 
@@ -22,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, val);
+	if (unlikely(reg & ~REGNUM_C22_MASK))
+		return -ENXIO;
+
+	return mdiobus_write(mdio_dev->bus, mdio_dev->addr, reg & REGNUM_C22_MASK, val);
 }
 
 static const struct regmap_bus regmap_mdio_bus = {
-- 
2.31.1


  parent reply	other threads:[~2021-06-05  8:34 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-06-03 18:25 [RFC PATCH 0/2] Clause-22/Clause-45 MDIO regmap support Sander Vanheule
2021-06-03 18:25 ` [RFC PATCH 1/2] regmap: mdio: Clean up invalid clause-22 addresses Sander Vanheule
2021-06-03 18:25 ` [RFC PATCH 2/2] regmap: mdio: Add clause-45 support Sander Vanheule
2021-06-04 17:25 ` [RFC PATCH 0/2] Clause-22/Clause-45 MDIO regmap support Mark Brown
2021-06-04 18:16   ` Sander Vanheule
2021-06-07 11:54     ` Mark Brown
2021-06-07 12:06       ` Sander Vanheule
2021-06-07 12:14       ` Andy Shevchenko
2021-06-04 20:38   ` Andrew Lunn
2021-06-04 23:55 ` Andrew Lunn
2021-06-05  8:31 ` Sander Vanheule [this message]
2021-06-07 11:03   ` [PATCH] regmap: mdio: Reject invalid clause-22 addresses Mark Brown
2021-06-07 11:12     ` Sander Vanheule
2021-06-09 11:51     ` Sander Vanheule
2021-06-08 16:06 ` [RFC PATCH 0/2] Clause-22/Clause-45 MDIO regmap support Mark Brown

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=20210605083116.12786-1-sander@svanheule.net \
    --to=sander@svanheule.net \
    --cc=andrew@lunn.ch \
    --cc=andy.shevchenko@gmail.com \
    --cc=broonie@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=rafael@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).