diff mbox series

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

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

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

Rob Herring Sept. 8, 2023, 3:18 p.m. UTC | #1
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
>
Dan Carpenter Sept. 8, 2023, 4:14 p.m. UTC | #2
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
Dan Carpenter Sept. 13, 2023, 6:28 a.m. UTC | #3
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 Sept. 13, 2023, 12:44 p.m. UTC | #4
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);