All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andy Shevchenko <andy.shevchenko@gmail.com>
To: Krzysztof Kozlowski <krzysztof.kozlowski@canonical.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	Stuart Yoder <stuyoder@gmail.com>,
	"K. Y. Srinivasan" <kys@microsoft.com>,
	Haiyang Zhang <haiyangz@microsoft.com>,
	Stephen Hemminger <sthemmin@microsoft.com>,
	Wei Liu <wei.liu@kernel.org>, Dexuan Cui <decui@microsoft.com>,
	Bjorn Helgaas <bhelgaas@google.com>,
	Bjorn Andersson <bjorn.andersson@linaro.org>,
	Mathieu Poirier <mathieu.poirier@linaro.org>,
	Vineeth Vijayan <vneethv@linux.ibm.com>,
	Peter Oberparleiter <oberpar@linux.ibm.com>,
	Heiko Carstens <hca@linux.ibm.com>,
	Vasily Gorbik <gor@linux.ibm.com>,
	Alexander Gordeev <agordeev@linux.ibm.com>,
	Christian Borntraeger <borntraeger@linux.ibm.com>,
	Sven Schnelle <svens@linux.ibm.com>,
	Andy Gross <agross@kernel.org>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	linux-clk <linux-clk@vger.kernel.org>,
	NXP Linux Team <linux-imx@nxp.com>,
	linux-arm Mailing List <linux-arm-kernel@lists.infradead.org>,
	Linux on Hyper-V List <linux-hyperv@vger.kernel.org>,
	linux-pci <linux-pci@vger.kernel.org>,
	linux-remoteproc@vger.kernel.org, linux-s390@vger.kernel.org,
	linux-arm-msm@vger.kernel.org,
	ALSA Development Mailing List <alsa-devel@alsa-project.org>,
	linux-spi <linux-spi@vger.kernel.org>,
	virtualization@lists.linux-foundation.org,
	Linus Torvalds <torvalds@linux-foundation.org>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>
Subject: Re: [PATCH v5 01/11] driver: platform: Add helper for safer setting of driver_override
Date: Wed, 16 Mar 2022 17:54:04 +0200	[thread overview]
Message-ID: <CAHp75VeaQdzUKJSKzH9FjbmON5asqH799AS8OzHGoDiRnJifNw@mail.gmail.com> (raw)
In-Reply-To: <20220316150533.421349-2-krzysztof.kozlowski@canonical.com>

On Wed, Mar 16, 2022 at 5:06 PM Krzysztof Kozlowski
<krzysztof.kozlowski@canonical.com> wrote:

...

> +int driver_set_override(struct device *dev, const char **override,
> +                       const char *s, size_t len)
> +{
> +       const char *new, *old;
> +       char *cp;

> +       if (!dev || !override || !s)
> +               return -EINVAL;

Sorry, I didn't pay much attention on this. First of all, I would drop
dev checks and simply require that dev should be valid. Do you expect
this can be called when dev is invalid? I would like to hear if it's
anything but theoretical. Second one, is the !s requirement. Do I
understand correctly that the string must be always present? But then
how we NULify the override? Is it possible? Third one is absence of
len check. See below.

> +       /*
> +        * The stored value will be used in sysfs show callback (sysfs_emit()),
> +        * which has a length limit of PAGE_SIZE and adds a trailing newline.
> +        * Thus we can store one character less to avoid truncation during sysfs
> +        * show.
> +        */
> +       if (len >= (PAGE_SIZE - 1))
> +               return -EINVAL;

I would relax this to make sure we can use it if \n is within this limit.

> +       cp = strnchr(s, len, '\n');
> +       if (cp)
> +               len = cp - s;
> +
> +       new = kstrndup(s, len, GFP_KERNEL);

Here is a word about the len check.

> +       if (!new)

If len == 0, this won't trigger and you have something very
interesting as a result.

One way is to use ZERO_PTR_OR_NULL() another is explicitly check for 0
and issue a (different?) error code.

> +               return -ENOMEM;
> +
> +       device_lock(dev);
> +       old = *override;
> +       if (cp != s) {
> +               *override = new;
> +       } else {
> +               kfree(new);
> +               *override = NULL;
> +       }
> +       device_unlock(dev);
> +
> +       kfree(old);
> +
> +       return 0;
> +}

-- 
With Best Regards,
Andy Shevchenko

WARNING: multiple messages have this Message-ID (diff)
From: Andy Shevchenko <andy.shevchenko@gmail.com>
To: Krzysztof Kozlowski <krzysztof.kozlowski@canonical.com>
Cc: Linux on Hyper-V List <linux-hyperv@vger.kernel.org>,
	Stuart Yoder <stuyoder@gmail.com>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	linux-pci <linux-pci@vger.kernel.org>,
	linux-remoteproc@vger.kernel.org,
	ALSA Development Mailing List <alsa-devel@alsa-project.org>,
	Bjorn Andersson <bjorn.andersson@linaro.org>,
	Vineeth Vijayan <vneethv@linux.ibm.com>,
	Alexander Gordeev <agordeev@linux.ibm.com>,
	linux-clk <linux-clk@vger.kernel.org>,
	linux-s390@vger.kernel.org, Wei Liu <wei.liu@kernel.org>,
	Stephen Hemminger <sthemmin@microsoft.com>,
	Dexuan Cui <decui@microsoft.com>, Andy Gross <agross@kernel.org>,
	NXP Linux Team <linux-imx@nxp.com>,
	Heiko Carstens <hca@linux.ibm.com>,
	Vasily Gorbik <gor@linux.ibm.com>,
	linux-arm-msm@vger.kernel.org,
	Haiyang Zhang <haiyangz@microsoft.com>,
	linux-spi <linux-spi@vger.kernel.org>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	Bjorn Helgaas <bhelgaas@google.com>,
	virtualization@lists.linux-foundation.org,
	linux-arm Mailing List <linux-arm-kernel@lists.infradead.org>,
	Mathieu Poirier <mathieu.poirier@linaro.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Peter Oberparleiter <oberpar@linux.ibm.com>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	Sven Schnelle <svens@linux.ibm.com>,
	Linus Torvalds <torvalds@linux-foundation.org>
Subject: Re: [PATCH v5 01/11] driver: platform: Add helper for safer setting of driver_override
Date: Wed, 16 Mar 2022 17:54:04 +0200	[thread overview]
Message-ID: <CAHp75VeaQdzUKJSKzH9FjbmON5asqH799AS8OzHGoDiRnJifNw@mail.gmail.com> (raw)
In-Reply-To: <20220316150533.421349-2-krzysztof.kozlowski@canonical.com>

On Wed, Mar 16, 2022 at 5:06 PM Krzysztof Kozlowski
<krzysztof.kozlowski@canonical.com> wrote:

...

> +int driver_set_override(struct device *dev, const char **override,
> +                       const char *s, size_t len)
> +{
> +       const char *new, *old;
> +       char *cp;

> +       if (!dev || !override || !s)
> +               return -EINVAL;

Sorry, I didn't pay much attention on this. First of all, I would drop
dev checks and simply require that dev should be valid. Do you expect
this can be called when dev is invalid? I would like to hear if it's
anything but theoretical. Second one, is the !s requirement. Do I
understand correctly that the string must be always present? But then
how we NULify the override? Is it possible? Third one is absence of
len check. See below.

> +       /*
> +        * The stored value will be used in sysfs show callback (sysfs_emit()),
> +        * which has a length limit of PAGE_SIZE and adds a trailing newline.
> +        * Thus we can store one character less to avoid truncation during sysfs
> +        * show.
> +        */
> +       if (len >= (PAGE_SIZE - 1))
> +               return -EINVAL;

I would relax this to make sure we can use it if \n is within this limit.

> +       cp = strnchr(s, len, '\n');
> +       if (cp)
> +               len = cp - s;
> +
> +       new = kstrndup(s, len, GFP_KERNEL);

Here is a word about the len check.

> +       if (!new)

If len == 0, this won't trigger and you have something very
interesting as a result.

One way is to use ZERO_PTR_OR_NULL() another is explicitly check for 0
and issue a (different?) error code.

> +               return -ENOMEM;
> +
> +       device_lock(dev);
> +       old = *override;
> +       if (cp != s) {
> +               *override = new;
> +       } else {
> +               kfree(new);
> +               *override = NULL;
> +       }
> +       device_unlock(dev);
> +
> +       kfree(old);
> +
> +       return 0;
> +}

-- 
With Best Regards,
Andy Shevchenko
_______________________________________________
Virtualization mailing list
Virtualization@lists.linux-foundation.org
https://lists.linuxfoundation.org/mailman/listinfo/virtualization

WARNING: multiple messages have this Message-ID (diff)
From: Andy Shevchenko <andy.shevchenko@gmail.com>
To: Krzysztof Kozlowski <krzysztof.kozlowski@canonical.com>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	 Stuart Yoder <stuyoder@gmail.com>,
	"K. Y. Srinivasan" <kys@microsoft.com>,
	 Haiyang Zhang <haiyangz@microsoft.com>,
	Stephen Hemminger <sthemmin@microsoft.com>,
	 Wei Liu <wei.liu@kernel.org>, Dexuan Cui <decui@microsoft.com>,
	 Bjorn Helgaas <bhelgaas@google.com>,
	Bjorn Andersson <bjorn.andersson@linaro.org>,
	 Mathieu Poirier <mathieu.poirier@linaro.org>,
	Vineeth Vijayan <vneethv@linux.ibm.com>,
	 Peter Oberparleiter <oberpar@linux.ibm.com>,
	Heiko Carstens <hca@linux.ibm.com>,
	Vasily Gorbik <gor@linux.ibm.com>,
	Alexander Gordeev <agordeev@linux.ibm.com>,
	 Christian Borntraeger <borntraeger@linux.ibm.com>,
	Sven Schnelle <svens@linux.ibm.com>,
	Andy Gross <agross@kernel.org>,
	 Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	linux-clk <linux-clk@vger.kernel.org>,
	 NXP Linux Team <linux-imx@nxp.com>,
	 linux-arm Mailing List <linux-arm-kernel@lists.infradead.org>,
	 Linux on Hyper-V List <linux-hyperv@vger.kernel.org>,
	linux-pci <linux-pci@vger.kernel.org>,
	 linux-remoteproc@vger.kernel.org, linux-s390@vger.kernel.org,
	 linux-arm-msm@vger.kernel.org,
	 ALSA Development Mailing List <alsa-devel@alsa-project.org>,
	linux-spi <linux-spi@vger.kernel.org>,
	 virtualization@lists.linux-foundation.org,
	 Linus Torvalds <torvalds@linux-foundation.org>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>
Subject: Re: [PATCH v5 01/11] driver: platform: Add helper for safer setting of driver_override
Date: Wed, 16 Mar 2022 17:54:04 +0200	[thread overview]
Message-ID: <CAHp75VeaQdzUKJSKzH9FjbmON5asqH799AS8OzHGoDiRnJifNw@mail.gmail.com> (raw)
In-Reply-To: <20220316150533.421349-2-krzysztof.kozlowski@canonical.com>

On Wed, Mar 16, 2022 at 5:06 PM Krzysztof Kozlowski
<krzysztof.kozlowski@canonical.com> wrote:

...

> +int driver_set_override(struct device *dev, const char **override,
> +                       const char *s, size_t len)
> +{
> +       const char *new, *old;
> +       char *cp;

> +       if (!dev || !override || !s)
> +               return -EINVAL;

Sorry, I didn't pay much attention on this. First of all, I would drop
dev checks and simply require that dev should be valid. Do you expect
this can be called when dev is invalid? I would like to hear if it's
anything but theoretical. Second one, is the !s requirement. Do I
understand correctly that the string must be always present? But then
how we NULify the override? Is it possible? Third one is absence of
len check. See below.

> +       /*
> +        * The stored value will be used in sysfs show callback (sysfs_emit()),
> +        * which has a length limit of PAGE_SIZE and adds a trailing newline.
> +        * Thus we can store one character less to avoid truncation during sysfs
> +        * show.
> +        */
> +       if (len >= (PAGE_SIZE - 1))
> +               return -EINVAL;

I would relax this to make sure we can use it if \n is within this limit.

> +       cp = strnchr(s, len, '\n');
> +       if (cp)
> +               len = cp - s;
> +
> +       new = kstrndup(s, len, GFP_KERNEL);

Here is a word about the len check.

> +       if (!new)

If len == 0, this won't trigger and you have something very
interesting as a result.

One way is to use ZERO_PTR_OR_NULL() another is explicitly check for 0
and issue a (different?) error code.

> +               return -ENOMEM;
> +
> +       device_lock(dev);
> +       old = *override;
> +       if (cp != s) {
> +               *override = new;
> +       } else {
> +               kfree(new);
> +               *override = NULL;
> +       }
> +       device_unlock(dev);
> +
> +       kfree(old);
> +
> +       return 0;
> +}

-- 
With Best Regards,
Andy Shevchenko

_______________________________________________
linux-arm-kernel mailing list
linux-arm-kernel@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel

WARNING: multiple messages have this Message-ID (diff)
From: Andy Shevchenko <andy.shevchenko@gmail.com>
To: Krzysztof Kozlowski <krzysztof.kozlowski@canonical.com>
Cc: Linux on Hyper-V List <linux-hyperv@vger.kernel.org>,
	Stuart Yoder <stuyoder@gmail.com>,
	"Rafael J. Wysocki" <rafael@kernel.org>,
	linux-pci <linux-pci@vger.kernel.org>,
	linux-remoteproc@vger.kernel.org,
	ALSA Development Mailing List <alsa-devel@alsa-project.org>,
	Bjorn Andersson <bjorn.andersson@linaro.org>,
	Vineeth Vijayan <vneethv@linux.ibm.com>,
	Alexander Gordeev <agordeev@linux.ibm.com>,
	"K. Y. Srinivasan" <kys@microsoft.com>,
	linux-clk <linux-clk@vger.kernel.org>,
	linux-s390@vger.kernel.org, Wei Liu <wei.liu@kernel.org>,
	Stephen Hemminger <sthemmin@microsoft.com>,
	Dexuan Cui <decui@microsoft.com>, Andy Gross <agross@kernel.org>,
	NXP Linux Team <linux-imx@nxp.com>,
	Christian Borntraeger <borntraeger@linux.ibm.com>,
	Heiko Carstens <hca@linux.ibm.com>,
	Vasily Gorbik <gor@linux.ibm.com>,
	linux-arm-msm@vger.kernel.org,
	Haiyang Zhang <haiyangz@microsoft.com>,
	linux-spi <linux-spi@vger.kernel.org>,
	Rasmus Villemoes <linux@rasmusvillemoes.dk>,
	Bjorn Helgaas <bhelgaas@google.com>,
	virtualization@lists.linux-foundation.org,
	linux-arm Mailing List <linux-arm-kernel@lists.infradead.org>,
	Mathieu Poirier <mathieu.poirier@linaro.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Peter Oberparleiter <oberpar@linux.ibm.com>,
	Linux Kernel Mailing List <linux-kernel@vger.kernel.org>,
	Sven Schnelle <svens@linux.ibm.com>,
	Linus Torvalds <torvalds@linux-foundation.org>
Subject: Re: [PATCH v5 01/11] driver: platform: Add helper for safer setting of driver_override
Date: Wed, 16 Mar 2022 17:54:04 +0200	[thread overview]
Message-ID: <CAHp75VeaQdzUKJSKzH9FjbmON5asqH799AS8OzHGoDiRnJifNw@mail.gmail.com> (raw)
In-Reply-To: <20220316150533.421349-2-krzysztof.kozlowski@canonical.com>

On Wed, Mar 16, 2022 at 5:06 PM Krzysztof Kozlowski
<krzysztof.kozlowski@canonical.com> wrote:

...

> +int driver_set_override(struct device *dev, const char **override,
> +                       const char *s, size_t len)
> +{
> +       const char *new, *old;
> +       char *cp;

> +       if (!dev || !override || !s)
> +               return -EINVAL;

Sorry, I didn't pay much attention on this. First of all, I would drop
dev checks and simply require that dev should be valid. Do you expect
this can be called when dev is invalid? I would like to hear if it's
anything but theoretical. Second one, is the !s requirement. Do I
understand correctly that the string must be always present? But then
how we NULify the override? Is it possible? Third one is absence of
len check. See below.

> +       /*
> +        * The stored value will be used in sysfs show callback (sysfs_emit()),
> +        * which has a length limit of PAGE_SIZE and adds a trailing newline.
> +        * Thus we can store one character less to avoid truncation during sysfs
> +        * show.
> +        */
> +       if (len >= (PAGE_SIZE - 1))
> +               return -EINVAL;

I would relax this to make sure we can use it if \n is within this limit.

> +       cp = strnchr(s, len, '\n');
> +       if (cp)
> +               len = cp - s;
> +
> +       new = kstrndup(s, len, GFP_KERNEL);

Here is a word about the len check.

> +       if (!new)

If len == 0, this won't trigger and you have something very
interesting as a result.

One way is to use ZERO_PTR_OR_NULL() another is explicitly check for 0
and issue a (different?) error code.

> +               return -ENOMEM;
> +
> +       device_lock(dev);
> +       old = *override;
> +       if (cp != s) {
> +               *override = new;
> +       } else {
> +               kfree(new);
> +               *override = NULL;
> +       }
> +       device_unlock(dev);
> +
> +       kfree(old);
> +
> +       return 0;
> +}

-- 
With Best Regards,
Andy Shevchenko

  reply	other threads:[~2022-03-16 15:55 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-16 15:05 [PATCH v5 00/11] Fix broken usage of driver_override (and kfree of static memory) Krzysztof Kozlowski
2022-03-16 15:05 ` Krzysztof Kozlowski
2022-03-16 15:05 ` Krzysztof Kozlowski
2022-03-16 15:05 ` [PATCH v5 01/11] driver: platform: Add helper for safer setting of driver_override Krzysztof Kozlowski
2022-03-16 15:05   ` Krzysztof Kozlowski
2022-03-16 15:05   ` Krzysztof Kozlowski
2022-03-16 15:54   ` Andy Shevchenko [this message]
2022-03-16 15:54     ` Andy Shevchenko
2022-03-16 15:54     ` Andy Shevchenko
2022-03-16 15:54     ` Andy Shevchenko
2022-04-03 18:26     ` Krzysztof Kozlowski
2022-04-03 18:26       ` Krzysztof Kozlowski
2022-03-16 15:05 ` [PATCH v5 02/11] amba: Use driver_set_override() instead of open-coding Krzysztof Kozlowski
2022-03-16 15:05   ` Krzysztof Kozlowski
2022-03-16 15:05   ` Krzysztof Kozlowski
2022-03-16 15:05 ` [PATCH v5 03/11] fsl-mc: " Krzysztof Kozlowski
2022-03-16 15:05   ` Krzysztof Kozlowski
2022-03-16 15:05   ` Krzysztof Kozlowski
2022-03-16 15:05 ` [PATCH v5 04/11] hv: " Krzysztof Kozlowski
2022-03-16 15:05   ` Krzysztof Kozlowski
2022-03-16 15:05   ` Krzysztof Kozlowski
2022-03-16 15:05 ` [PATCH v5 05/11] PCI: " Krzysztof Kozlowski
2022-03-16 15:05   ` Krzysztof Kozlowski
2022-03-16 15:05   ` Krzysztof Kozlowski
2022-03-16 15:05 ` [PATCH v5 06/11] s390/cio: " Krzysztof Kozlowski
2022-03-16 15:05   ` Krzysztof Kozlowski
2022-03-16 15:05   ` Krzysztof Kozlowski
2022-03-16 15:07 ` [PATCH v5 07/11] spi: Use helper for safer setting of driver_override Krzysztof Kozlowski
2022-03-16 15:07   ` Krzysztof Kozlowski
2022-03-16 15:07   ` Krzysztof Kozlowski
2022-03-16 15:08 ` [PATCH v5 08/11] vdpa: " Krzysztof Kozlowski
2022-03-16 15:08   ` Krzysztof Kozlowski
2022-03-16 15:08   ` Krzysztof Kozlowski
2022-03-16 15:08 ` [PATCH v5 09/11] clk: imx: scu: Fix kfree() of static memory on setting driver_override Krzysztof Kozlowski
2022-03-16 15:08   ` Krzysztof Kozlowski
2022-03-16 15:08   ` Krzysztof Kozlowski
2022-03-16 15:08 ` [PATCH v5 10/11] slimbus: qcom-ngd: " Krzysztof Kozlowski
2022-03-16 15:08   ` Krzysztof Kozlowski
2022-03-16 15:08   ` Krzysztof Kozlowski
2022-03-16 15:08 ` [PATCH v5 11/11] rpmsg: " Krzysztof Kozlowski
2022-03-16 15:08   ` Krzysztof Kozlowski
2022-03-16 15:08   ` Krzysztof Kozlowski
2022-03-16 19:38   ` Krzysztof Kozlowski
2022-03-16 19:38     ` Krzysztof Kozlowski
2022-03-16 19:38     ` Krzysztof Kozlowski
2022-03-16 23:35   ` kernel test robot

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=CAHp75VeaQdzUKJSKzH9FjbmON5asqH799AS8OzHGoDiRnJifNw@mail.gmail.com \
    --to=andy.shevchenko@gmail.com \
    --cc=agordeev@linux.ibm.com \
    --cc=agross@kernel.org \
    --cc=alsa-devel@alsa-project.org \
    --cc=bhelgaas@google.com \
    --cc=bjorn.andersson@linaro.org \
    --cc=borntraeger@linux.ibm.com \
    --cc=decui@microsoft.com \
    --cc=gor@linux.ibm.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=haiyangz@microsoft.com \
    --cc=hca@linux.ibm.com \
    --cc=krzysztof.kozlowski@canonical.com \
    --cc=kys@microsoft.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-arm-msm@vger.kernel.org \
    --cc=linux-clk@vger.kernel.org \
    --cc=linux-hyperv@vger.kernel.org \
    --cc=linux-imx@nxp.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pci@vger.kernel.org \
    --cc=linux-remoteproc@vger.kernel.org \
    --cc=linux-s390@vger.kernel.org \
    --cc=linux-spi@vger.kernel.org \
    --cc=linux@rasmusvillemoes.dk \
    --cc=mathieu.poirier@linaro.org \
    --cc=oberpar@linux.ibm.com \
    --cc=rafael@kernel.org \
    --cc=sthemmin@microsoft.com \
    --cc=stuyoder@gmail.com \
    --cc=svens@linux.ibm.com \
    --cc=torvalds@linux-foundation.org \
    --cc=virtualization@lists.linux-foundation.org \
    --cc=vneethv@linux.ibm.com \
    --cc=wei.liu@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 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.