public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Marc Zyngier <maz@kernel.org>
To: Kefeng Wang <wangkefeng.wang@huawei.com>
Cc: Mark Brown <broonie@kernel.org>, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] regmap: Properly free allocated name for regmap_config of syscon
Date: Tue, 10 Nov 2020 08:47:08 +0000	[thread overview]
Message-ID: <9b291d6ebdebe8a3a9f07d2bf4033fe9@kernel.org> (raw)
In-Reply-To: <18a3857d-3250-e136-7d80-abdab902367c@huawei.com>

On 2020-11-10 01:35, Kefeng Wang wrote:
> On 2020/11/10 1:23, Mark Brown wrote:
>> On Mon, Nov 09, 2020 at 07:58:16PM +0800, Kefeng Wang wrote:
>> 
>>> syscon_config.name in of_syscon_register is allocated using 
>>> kasprintf,
>>> which should be freed when it is not used after regmap_set_name, fix
>>> the following memory leak.
>>> unreferenced object 0xffffffe07fe8c150 (size 16):
>>>    comm "swapper/0", pid 1, jiffies 4294892540 (age 68.168s)
>>>    hex dump (first 16 bytes):
>>>      74 65 73 74 40 31 30 30 30 30 30 00 e0 ff ff ff  
>>> test@100000.....
>>>    backtrace:
>>>      [<0000000023d86736>] create_object+0xe8/0x348
>>>      [<00000000fe9d1b17>] kmemleak_alloc+0x20/0x2a
>> Please think hard before including complete backtraces in upstream
>> reports, they are very large and contain almost no useful information
>> relative to their size so often obscure the relevant content in your
>> message. If part of the backtrace is usefully illustrative (it often 
>> is
>> for search engines if nothing else) then it's usually better to pull 
>> out
>> the relevant sections.
> 
> 2899872b627e   "regmap: debugfs: Fix memory leak in
> regmap_debugfs_init" add a similar
> 
> backtrack, but the address of the trace is useless, will be careful 
> next time.
> 
>>> @@ -601,6 +601,7 @@ static int regmap_set_name(struct regmap *map, 
>>> const struct regmap_config *confi
>>>   		if (!name)
>>>   			return -ENOMEM;
>>>   +		kfree_const(config->name);
>>>   		kfree_const(map->name);
>>>   		map->name = name;
>>>   	}
>> Why would we free the passed in name here?  The name wes passed in 
>> from
>> outside regmap in a const configuration struct, we've no idea within
>> regmap if it was dynamically allocted or not and it seems very
>> surprising that we'd go off and free it.  The whole reason we're
>> duplicating it in regmap_set_name() is that we don't know how long 
>> it's
>> going to be around so we don't want to reference it after having
>> returned to the caller.  If the caller has dynamically allocated it 
>> then
>> the caller should deal with freeing it.
> 
> Yes, after check it again, this patch is wrong.
> 
> Hi Marc,  the regmap debugfs will duplicate a name in 
> regmap_set_name(), and
> 
> syscon_config.name won't be used in syscon,  so your following patch
> doesn't seem
> 
> to be necessary,  right ? Please correct me if I'm wrong, thanks.

It was certainly necessary at the time when I wrote the patch, as it
was fixing some obvious memory corruption (use after free).

It is very possible that the flow has been reorganised since, as the
following commit hints at:

commit e15d7f2b81d2e7d93115d46fa931b366c1cdebc2
Author: Suman Anna <s-anna@ti.com>
Date:   Mon Jul 27 16:10:08 2020 -0500

     mfd: syscon: Use a unique name with regmap_config

     The DT node full name is currently being used in regmap_config
     which in turn is used to create the regmap debugfs directories.
     This name however is not guaranteed to be unique and the regmap
     debugfs registration can fail in the cases where the syscon nodes
     have the same unit-address but are present in different DT node
     hierarchies. Replace this logic using the syscon reg resource
     address instead (inspired from logic used while creating platform
     devices) to ensure a unique name is given for each syscon.

     Signed-off-by: Suman Anna <s-anna@ti.com>
     Reviewed-by: Arnd Bergmann <arnd@arndb.de>
     Signed-off-by: Lee Jones <lee.jones@linaro.org>

I suggest you come up with a more complete analysis of the problem
and how it came to be.

         M.
-- 
Jazz is not dead. It just smells funny...

  reply	other threads:[~2020-11-10  8:47 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-09 11:58 [PATCH] regmap: Properly free allocated name for regmap_config of syscon Kefeng Wang
2020-11-09 17:23 ` Mark Brown
2020-11-10  1:35   ` Kefeng Wang
2020-11-10  8:47     ` Marc Zyngier [this message]
2020-11-11 10:53       ` Kefeng Wang

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=9b291d6ebdebe8a3a9f07d2bf4033fe9@kernel.org \
    --to=maz@kernel.org \
    --cc=broonie@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=wangkefeng.wang@huawei.com \
    /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