phy: renesas: rcar-gen3-usb2: Fix sysfs interface of "role"
diff mbox series

Message ID 1564563689-25863-1-git-send-email-yoshihiro.shimoda.uh@renesas.com
State Under Review
Delegated to: Geert Uytterhoeven
Headers show
Series
  • phy: renesas: rcar-gen3-usb2: Fix sysfs interface of "role"
Related show

Commit Message

Yoshihiro Shimoda July 31, 2019, 9:01 a.m. UTC
Since the role_store() uses strncmp(), it's possible to refer
out-of-memory if the sysfs data size is smaller than strlen("host").
This patch fixes it by using sysfs_streq() instead of strncmp().

Reported-by: Pavel Machek <pavel@denx.de>
Fixes: 9bb86777fb71 ("phy: rcar-gen3-usb2: add sysfs for usb role swap")
Cc: <stable@vger.kernel.org> # v4.10+
Signed-off-by: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
---
 Just a record. The role_store() doesn't need to check the count because
 the sysfs_streq() checks the first argument is NULL or not.

 On "if (sysfs_streq(buf, "host"))"
  Example 1: echo ho > role
  --> In this case, the count is 3 and the buf has "ho" + NULL.
      So, the third character differs between NULL and 's'.

  Example 2: echo host-is-not-used > role
  --> In this case, the count is 17 and the buf has "host-is-not-used" + NULL.
      So, the fifth character differs between '-' and NULL.

 drivers/phy/renesas/phy-rcar-gen3-usb2.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Comments

Geert Uytterhoeven July 31, 2019, 9:27 a.m. UTC | #1
Hi Shimoda-san,

On Wed, Jul 31, 2019 at 11:04 AM Yoshihiro Shimoda
<yoshihiro.shimoda.uh@renesas.com> wrote:
> Since the role_store() uses strncmp(), it's possible to refer
> out-of-memory if the sysfs data size is smaller than strlen("host").
> This patch fixes it by using sysfs_streq() instead of strncmp().
>
> Reported-by: Pavel Machek <pavel@denx.de>
> Fixes: 9bb86777fb71 ("phy: rcar-gen3-usb2: add sysfs for usb role swap")
> Cc: <stable@vger.kernel.org> # v4.10+
> Signed-off-by: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>

Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>

> ---
>  Just a record. The role_store() doesn't need to check the count because
>  the sysfs_streq() checks the first argument is NULL or not.

Is that wat you mean? sysfs_streq() doesn't seem to check for NULL pointers.

Isn't the real reason that sysfs (kernfs) guarantees that the passed buffer
is NUL-terminated?

Gr{oetje,eeting}s,

                        Geert
Yoshihiro Shimoda July 31, 2019, 9:58 a.m. UTC | #2
Hi Geert-san,

> From: Geert Uytterhoeven, Sent: Wednesday, July 31, 2019 6:27 PM
> 
> Hi Shimoda-san,
> 
> On Wed, Jul 31, 2019 at 11:04 AM Yoshihiro Shimoda
> <yoshihiro.shimoda.uh@renesas.com> wrote:
> > Since the role_store() uses strncmp(), it's possible to refer
> > out-of-memory if the sysfs data size is smaller than strlen("host").
> > This patch fixes it by using sysfs_streq() instead of strncmp().
> >
> > Reported-by: Pavel Machek <pavel@denx.de>
> > Fixes: 9bb86777fb71 ("phy: rcar-gen3-usb2: add sysfs for usb role swap")
> > Cc: <stable@vger.kernel.org> # v4.10+
> > Signed-off-by: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
> 
> Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>

Thank you for your review!

> > ---
> >  Just a record. The role_store() doesn't need to check the count because
> >  the sysfs_streq() checks the first argument is NULL or not.
> 
> Is that wat you mean? sysfs_streq() doesn't seem to check for NULL pointers.

Oops, sorry for unclear. I meant a NULL-terminated string, not NULL pointer.

> Isn't the real reason that sysfs (kernfs) guarantees that the passed buffer
> is NUL-terminated?

I doesn't check in detail, but I assume so.

Best regards,
Yoshihiro Shimoda

> Gr{oetje,eeting}s,
> 
>                         Geert
> 
> --
> Geert Uytterhoeven -- There's lots of Linux beyond ia32 -- geert@linux-m68k.org
> 
> In personal conversations with technical people, I call myself a hacker. But
> when I'm talking to journalists I just say "programmer" or something like that.
>                                 -- Linus Torvalds
Geert Uytterhoeven July 31, 2019, 10:09 a.m. UTC | #3
Hi Shimoda-san,

On Wed, Jul 31, 2019 at 11:58 AM Yoshihiro Shimoda
<yoshihiro.shimoda.uh@renesas.com> wrote:
> > From: Geert Uytterhoeven, Sent: Wednesday, July 31, 2019 6:27 PM
> > On Wed, Jul 31, 2019 at 11:04 AM Yoshihiro Shimoda
> > <yoshihiro.shimoda.uh@renesas.com> wrote:
> > > Since the role_store() uses strncmp(), it's possible to refer
> > > out-of-memory if the sysfs data size is smaller than strlen("host").
> > > This patch fixes it by using sysfs_streq() instead of strncmp().
> > >
> > > Reported-by: Pavel Machek <pavel@denx.de>
> > > Fixes: 9bb86777fb71 ("phy: rcar-gen3-usb2: add sysfs for usb role swap")
> > > Cc: <stable@vger.kernel.org> # v4.10+
> > > Signed-off-by: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>
> >
> > Reviewed-by: Geert Uytterhoeven <geert+renesas@glider.be>
>
> Thank you for your review!
>
> > > ---
> > >  Just a record. The role_store() doesn't need to check the count because
> > >  the sysfs_streq() checks the first argument is NULL or not.
> >
> > Is that wat you mean? sysfs_streq() doesn't seem to check for NULL pointers.
>
> Oops, sorry for unclear. I meant a NULL-terminated string, not NULL pointer.

OK.

> > Isn't the real reason that sysfs (kernfs) guarantees that the passed buffer
> > is NUL-terminated?
>
> I doesn't check in detail, but I assume so.

I have checked that recently, so it is OK.

Gr{oetje,eeting}s,

                        Geert
Pavel Machek Aug. 14, 2019, 8:21 a.m. UTC | #4
On Wed 2019-07-31 18:01:29, Yoshihiro Shimoda wrote:
> Since the role_store() uses strncmp(), it's possible to refer
> out-of-memory if the sysfs data size is smaller than strlen("host").
> This patch fixes it by using sysfs_streq() instead of strncmp().
> 
> Reported-by: Pavel Machek <pavel@denx.de>
> Fixes: 9bb86777fb71 ("phy: rcar-gen3-usb2: add sysfs for usb role swap")
> Cc: <stable@vger.kernel.org> # v4.10+
> Signed-off-by: Yoshihiro Shimoda <yoshihiro.shimoda.uh@renesas.com>

Acked-by: Pavel Machek <pavel@denx.de>

Patch
diff mbox series

diff --git a/drivers/phy/renesas/phy-rcar-gen3-usb2.c b/drivers/phy/renesas/phy-rcar-gen3-usb2.c
index 1322185..cc18970 100644
--- a/drivers/phy/renesas/phy-rcar-gen3-usb2.c
+++ b/drivers/phy/renesas/phy-rcar-gen3-usb2.c
@@ -20,6 +20,7 @@ 
 #include <linux/platform_device.h>
 #include <linux/pm_runtime.h>
 #include <linux/regulator/consumer.h>
+#include <linux/string.h>
 #include <linux/usb/of.h>
 #include <linux/workqueue.h>
 
@@ -317,9 +318,9 @@  static ssize_t role_store(struct device *dev, struct device_attribute *attr,
 	if (!ch->is_otg_channel || !rcar_gen3_is_any_rphy_initialized(ch))
 		return -EIO;
 
-	if (!strncmp(buf, "host", strlen("host")))
+	if (sysfs_streq(buf, "host"))
 		new_mode = PHY_MODE_USB_HOST;
-	else if (!strncmp(buf, "peripheral", strlen("peripheral")))
+	else if (sysfs_streq(buf, "peripheral"))
 		new_mode = PHY_MODE_USB_DEVICE;
 	else
 		return -EINVAL;