diff mbox series

of: dynamic: Fix potential memory leak in of_changeset_action()

Message ID 7dfaf999-30ad-491c-9615-fb1138db121c@moroto.mountain
State Accepted
Headers show
Series of: dynamic: Fix potential memory leak in of_changeset_action() | expand

Checks

Context Check Description
robh/checkpatch warning total: 0 errors, 1 warnings, 16 lines checked
robh/patch-applied fail build log

Commit Message

Dan Carpenter Sept. 8, 2023, 7:03 a.m. UTC
Smatch complains that the error path where "action" is invalid leaks
the "ce" allocation:
    drivers/of/dynamic.c:935 of_changeset_action()
    warn: possible memory leak of 'ce'

Fix this by doing the validation before the allocation.

Fixes: 914d9d831e61 ("of: dynamic: Refactor action prints to not use "%pOF" inside devtree_lock")
Reported-by: kernel test robot <lkp@intel.com>
Closes: https://lore.kernel.org/r/202309011059.EOdr4im9-lkp@intel.com/
Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
---
 drivers/of/dynamic.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

Comments

Geert Uytterhoeven Sept. 8, 2023, 7:16 a.m. UTC | #1
On Fri, Sep 8, 2023 at 9:03 AM Dan Carpenter <dan.carpenter@linaro.org> wrote:
> Smatch complains that the error path where "action" is invalid leaks
> the "ce" allocation:
>     drivers/of/dynamic.c:935 of_changeset_action()
>     warn: possible memory leak of 'ce'
>
> Fix this by doing the validation before the allocation.
>
> Fixes: 914d9d831e61 ("of: dynamic: Refactor action prints to not use "%pOF" inside devtree_lock")
> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/r/202309011059.EOdr4im9-lkp@intel.com/
> Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>

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

Gr{oetje,eeting}s,

                        Geert
Rob Herring (Arm) Sept. 8, 2023, 3:18 p.m. UTC | #2
On Fri, Sep 8, 2023 at 2:03 AM Dan Carpenter <dan.carpenter@linaro.org> wrote:
>
> Smatch complains that the error path where "action" is invalid leaks
> the "ce" allocation:
>     drivers/of/dynamic.c:935 of_changeset_action()
>     warn: possible memory leak of 'ce'
>
> Fix this by doing the validation before the allocation.

I'm going to add a note when applying that "action" can't ever
actually be invalid because all the callers are static inlines with
hardcoded action values. I suppose there could be an out of tree
module calling of_changeset_action() directly, but that's wrong given
the wrappers.

> Fixes: 914d9d831e61 ("of: dynamic: Refactor action prints to not use "%pOF" inside devtree_lock")
> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/r/202309011059.EOdr4im9-lkp@intel.com/

Despite what that says, it was never reported to me. IOW, the added TO
and CC lines don't seem to have any effect.

> Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
> ---
>  drivers/of/dynamic.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
>
> diff --git a/drivers/of/dynamic.c b/drivers/of/dynamic.c
> index 0a3483e247a8..f63250c650ca 100644
> --- a/drivers/of/dynamic.c
> +++ b/drivers/of/dynamic.c
> @@ -890,13 +890,13 @@ int of_changeset_action(struct of_changeset *ocs, unsigned long action,
>  {
>         struct of_changeset_entry *ce;
>
> +       if (WARN_ON(action >= ARRAY_SIZE(action_names)))
> +               return -EINVAL;
> +
>         ce = kzalloc(sizeof(*ce), GFP_KERNEL);
>         if (!ce)
>                 return -ENOMEM;
>
> -       if (WARN_ON(action >= ARRAY_SIZE(action_names)))
> -               return -EINVAL;
> -
>         /* get a reference to the node */
>         ce->action = action;
>         ce->np = of_node_get(np);
> --
> 2.39.2
>
Geert Uytterhoeven Sept. 8, 2023, 3:34 p.m. UTC | #3
Hi Rob,

On Fri, Sep 8, 2023 at 5:18 PM Rob Herring <robh@kernel.org> wrote:
> On Fri, Sep 8, 2023 at 2:03 AM Dan Carpenter <dan.carpenter@linaro.org> wrote:
> > Smatch complains that the error path where "action" is invalid leaks
> > the "ce" allocation:
> >     drivers/of/dynamic.c:935 of_changeset_action()
> >     warn: possible memory leak of 'ce'
> >
> > Fix this by doing the validation before the allocation.
>
> I'm going to add a note when applying that "action" can't ever
> actually be invalid because all the callers are static inlines with
> hardcoded action values. I suppose there could be an out of tree
> module calling of_changeset_action() directly, but that's wrong given
> the wrappers.

FTR, the out-of-tree overlay configfs patches do not call
of_changeset_action() (or any of the wrappers).

> > Fixes: 914d9d831e61 ("of: dynamic: Refactor action prints to not use "%pOF" inside devtree_lock")
> > Reported-by: kernel test robot <lkp@intel.com>
> > Closes: https://lore.kernel.org/r/202309011059.EOdr4im9-lkp@intel.com/
>
> Despite what that says, it was never reported to me. IOW, the added TO
> and CC lines don't seem to have any effect.

The copy I received did list you in the "To"-header, though.
Fall-out of the issues seen with Gmail lately?
I do miss lots of email, too :-(

Gr{oetje,eeting}s,

                        Geert
Dan Carpenter Sept. 8, 2023, 4:14 p.m. UTC | #4
On Fri, Sep 08, 2023 at 05:34:53PM +0200, Geert Uytterhoeven wrote:
> > > Fixes: 914d9d831e61 ("of: dynamic: Refactor action prints to not use "%pOF" inside devtree_lock")
> > > Reported-by: kernel test robot <lkp@intel.com>
> > > Closes: https://lore.kernel.org/r/202309011059.EOdr4im9-lkp@intel.com/
> >
> > Despite what that says, it was never reported to me. IOW, the added TO
> > and CC lines don't seem to have any effect.
> 
> The copy I received did list you in the "To"-header, though.
> Fall-out of the issues seen with Gmail lately?
> I do miss lots of email, too :-(

My gmail account dropped a whole lot of mail too in the last week of
August.  I was out of office that week so I didn't investigate.  I was
assuming it was an issue with vger...

regards,
dan carpenter
Rob Herring (Arm) Sept. 11, 2023, 2:58 p.m. UTC | #5
On Fri, 08 Sep 2023 10:03:50 +0300, Dan Carpenter wrote:
> Smatch complains that the error path where "action" is invalid leaks
> the "ce" allocation:
>     drivers/of/dynamic.c:935 of_changeset_action()
>     warn: possible memory leak of 'ce'
> 
> Fix this by doing the validation before the allocation.
> 
> Fixes: 914d9d831e61 ("of: dynamic: Refactor action prints to not use "%pOF" inside devtree_lock")
> Reported-by: kernel test robot <lkp@intel.com>
> Closes: https://lore.kernel.org/r/202309011059.EOdr4im9-lkp@intel.com/
> Signed-off-by: Dan Carpenter <dan.carpenter@linaro.org>
> ---
>  drivers/of/dynamic.c | 6 +++---
>  1 file changed, 3 insertions(+), 3 deletions(-)
> 

Applied, thanks!
Rob Herring (Arm) Sept. 12, 2023, 3:32 p.m. UTC | #6
On Fri, Sep 8, 2023 at 11:14 AM Dan Carpenter <dan.carpenter@linaro.org> wrote:
>
> On Fri, Sep 08, 2023 at 05:34:53PM +0200, Geert Uytterhoeven wrote:
> > > > Fixes: 914d9d831e61 ("of: dynamic: Refactor action prints to not use "%pOF" inside devtree_lock")
> > > > Reported-by: kernel test robot <lkp@intel.com>
> > > > Closes: https://lore.kernel.org/r/202309011059.EOdr4im9-lkp@intel.com/
> > >
> > > Despite what that says, it was never reported to me. IOW, the added TO
> > > and CC lines don't seem to have any effect.
> >
> > The copy I received did list you in the "To"-header, though.

Are you sure that's the header and not in the body?

> > Fall-out of the issues seen with Gmail lately?
> > I do miss lots of email, too :-(
>
> My gmail account dropped a whole lot of mail too in the last week of
> August.  I was out of office that week so I didn't investigate.  I was
> assuming it was an issue with vger...

I don't think it's related to those issues. If I search lore including
my email[1], then it doesn't find it either. Lore only has it in
oe-kbuild. Not LKML or oe-kbuild-all. It really just looks like the
git-send-email style of extracting emails from tags in the body is not
happening.

Rob


[1] https://lore.kernel.org/all/?q=f%3Alkp%40intel.com+a%3Arobh%40kernel.org+of_changeset_action
Geert Uytterhoeven Sept. 12, 2023, 3:55 p.m. UTC | #7
Hi Rob,

On Tue, Sep 12, 2023 at 5:32 PM Rob Herring <robh@kernel.org> wrote:
> On Fri, Sep 8, 2023 at 11:14 AM Dan Carpenter <dan.carpenter@linaro.org> wrote:
> > On Fri, Sep 08, 2023 at 05:34:53PM +0200, Geert Uytterhoeven wrote:
> > > > > Fixes: 914d9d831e61 ("of: dynamic: Refactor action prints to not use "%pOF" inside devtree_lock")
> > > > > Reported-by: kernel test robot <lkp@intel.com>
> > > > > Closes: https://lore.kernel.org/r/202309011059.EOdr4im9-lkp@intel.com/
> > > >
> > > > Despite what that says, it was never reported to me. IOW, the added TO
> > > > and CC lines don't seem to have any effect.
> > >
> > > The copy I received did list you in the "To"-header, though.
>
> Are you sure that's the header and not in the body?

Yes:

    Date: Thu, 7 Sep 2023 13:52:48 +0300
    From: Dan Carpenter <dan.carpenter@linaro.org>
    To: oe-kbuild@lists.linux.dev, Rob Herring <robh@kernel.org>
    Cc: lkp@intel.com, oe-kbuild-all@lists.linux.dev,
linux-kernel@vger.kernel.org, Geert Uytterhoeven
<geert+renesas@glider.be>
    Subject: drivers/of/dynamic.c:935 of_changeset_action() warn:
possible memory leak of 'ce'
    Message-ID: <eaa86211-436d-445b-80bd-84cea5745b5a@kadam.mountain>

> > > Fall-out of the issues seen with Gmail lately?
> > > I do miss lots of email, too :-(
> >
> > My gmail account dropped a whole lot of mail too in the last week of
> > August.  I was out of office that week so I didn't investigate.  I was
> > assuming it was an issue with vger...
>
> I don't think it's related to those issues. If I search lore including
> my email[1], then it doesn't find it either. Lore only has it in
> oe-kbuild. Not LKML or oe-kbuild-all. It really just looks like the
> git-send-email style of extracting emails from tags in the body is not
> happening.

Oh, looks like there were two emails, one from lkp, and one from Dan:
https://lore.kernel.org/all/eaa86211-436d-445b-80bd-84cea5745b5a@kadam.mountain

I was referring to the one from Dan, which is not the one in Closes:.

Gr{oetje,eeting}s,

                        Geert
Dan Carpenter Sept. 13, 2023, 6:28 a.m. UTC | #8
On Tue, Sep 12, 2023 at 10:32:08AM -0500, Rob Herring wrote:
> On Fri, Sep 8, 2023 at 11:14 AM Dan Carpenter <dan.carpenter@linaro.org> wrote:
> >
> > On Fri, Sep 08, 2023 at 05:34:53PM +0200, Geert Uytterhoeven wrote:
> > > > > Fixes: 914d9d831e61 ("of: dynamic: Refactor action prints to not use "%pOF" inside devtree_lock")
> > > > > Reported-by: kernel test robot <lkp@intel.com>
> > > > > Closes: https://lore.kernel.org/r/202309011059.EOdr4im9-lkp@intel.com/
> > > >
> > > > Despite what that says, it was never reported to me. IOW, the added TO
> > > > and CC lines don't seem to have any effect.
> > >
> > > The copy I received did list you in the "To"-header, though.
> 
> Are you sure that's the header and not in the body?
> 

How these warnings work is that the kbuild bot sends the email to me and
the oe-kbuild@lists.linux.dev list.  I look it over and send it out
publicly if the warning seems right.

You're seeing the first email that I hadn't forwarded yet but the second
forwarded email went out and it reached lkml.

https://lore.kernel.org/lkml/eaa86211-436d-445b-80bd-84cea5745b5a@kadam.mountain/raw

You're on the To: header so it should have reached you as well...

regards,
dan carpenter
Rob Herring (Arm) Sept. 13, 2023, 12:44 p.m. UTC | #9
On Wed, Sep 13, 2023 at 1:29 AM Dan Carpenter <dan.carpenter@linaro.org> wrote:
>
> On Tue, Sep 12, 2023 at 10:32:08AM -0500, Rob Herring wrote:
> > On Fri, Sep 8, 2023 at 11:14 AM Dan Carpenter <dan.carpenter@linaro.org> wrote:
> > >
> > > On Fri, Sep 08, 2023 at 05:34:53PM +0200, Geert Uytterhoeven wrote:
> > > > > > Fixes: 914d9d831e61 ("of: dynamic: Refactor action prints to not use "%pOF" inside devtree_lock")
> > > > > > Reported-by: kernel test robot <lkp@intel.com>
> > > > > > Closes: https://lore.kernel.org/r/202309011059.EOdr4im9-lkp@intel.com/
> > > > >
> > > > > Despite what that says, it was never reported to me. IOW, the added TO
> > > > > and CC lines don't seem to have any effect.
> > > >
> > > > The copy I received did list you in the "To"-header, though.
> >
> > Are you sure that's the header and not in the body?
> >
>
> How these warnings work is that the kbuild bot sends the email to me and
> the oe-kbuild@lists.linux.dev list.  I look it over and send it out
> publicly if the warning seems right.
>
> You're seeing the first email that I hadn't forwarded yet but the second
> forwarded email went out and it reached lkml.
>
> https://lore.kernel.org/lkml/eaa86211-436d-445b-80bd-84cea5745b5a@kadam.mountain/raw
>
> You're on the To: header so it should have reached you as well...

Ah, okay. I have that one. It just got dumped off to my lkml folder
rather than one I have for 0-day which I actually look at.

Thanks for the explanation. Looks like I need to adjust my filters for these.

Rob
diff mbox series

Patch

diff --git a/drivers/of/dynamic.c b/drivers/of/dynamic.c
index 0a3483e247a8..f63250c650ca 100644
--- a/drivers/of/dynamic.c
+++ b/drivers/of/dynamic.c
@@ -890,13 +890,13 @@  int of_changeset_action(struct of_changeset *ocs, unsigned long action,
 {
 	struct of_changeset_entry *ce;
 
+	if (WARN_ON(action >= ARRAY_SIZE(action_names)))
+		return -EINVAL;
+
 	ce = kzalloc(sizeof(*ce), GFP_KERNEL);
 	if (!ce)
 		return -ENOMEM;
 
-	if (WARN_ON(action >= ARRAY_SIZE(action_names)))
-		return -EINVAL;
-
 	/* get a reference to the node */
 	ce->action = action;
 	ce->np = of_node_get(np);