All of lore.kernel.org
 help / color / mirror / Atom feed
From: Dmitry Torokhov <dmitry.torokhov@gmail.com>
To: Krzysztof Kozlowski <k.kozlowski@samsung.com>
Cc: Jiri Kosina <jkosina@suse.com>,
	linux-input@vger.kernel.org, linux-kernel@vger.kernel.org,
	sre@kernel.org, linux-pm@vger.kernel.org,
	"H.J. Lu" <hjl.tools@gmail.com>,
	stable@vger.kernel.org
Subject: Re: [PATCH] HID: hid-input: Fix accessing freed memory during driver unbind
Date: Wed, 29 Jul 2015 17:10:26 -0700	[thread overview]
Message-ID: <20150730001026.GH23178@dtor-ws> (raw)
In-Reply-To: <CAJKOXPcsHAj7_pggVw9eeU-C1_zKuZJA7oUodtXp=ZJH4AWg9w@mail.gmail.com>

On Thu, Jul 30, 2015 at 08:42:12AM +0900, Krzysztof Kozlowski wrote:
> 2015-07-30 2:46 GMT+09:00 Dmitry Torokhov <dmitry.torokhov@gmail.com>:
> > On Wed, Jul 29, 2015 at 03:07:04PM +0200, Jiri Kosina wrote:
> >> On Tue, 28 Jul 2015, Krzysztof Kozlowski wrote:
> >>
> >> > During unbinding the driver was dereferencing a pointer to memory
> >> > already freed by power_supply_unregister().
> >> >
> >> > Driver was freeing its internal description of battery through pointers
> >> > stored in power_supply structure. However, because the core owns the
> >> > power supply instance, after calling power_supply_unregister() the
> >> > driver cannot access these members.
> >> >
> >> > Fix this by using resource-managed allocations so internal data will be
> >> > freed by pointers stored in resource-managed core.
> >> >
> >> > Signed-off-by: Krzysztof Kozlowski <k.kozlowski@samsung.com>
> >> > Reported-by: H.J. Lu <hjl.tools@gmail.com>
> >> > Fixes: 297d716f6260 ("power_supply: Change ownership from driver to core")
> >> > Cc: <stable@vger.kernel.org>
> >>
> >> Applied to for-4.2/upstream-fixes, thanks.
> >
> > Wait, what guarantees do we have that this is only called in probe()
> > paths? Don't we allow hid_hw_start() be deferred to open() calls?
> 
> Indeed, this may be called in other contexts. But this should not
> introduce errors except not reclaimable memory (till remove()
> happens).
> 
> > In general we need to be careful with devm* conversions in core code.
> >
> 
> Another and less intrusive fix would be:
> 
>     char *name = dev->battery->desc->name;
>     struct power_supply_desc *psy_desc = dev->battery->desc;
>     power_supply_unregister(dev->battery);
>     kfree(name);
>     kfree(psy_desc);

I would much rather prefer this to the other version as it does not
leave memory hanging around, potentially indefinitely, but ultimately it
is up to Jiri. I only hope that power supply code does not reference
power_supply_desc pointer past unregister (since the device structure
itself may live past the point where power_supply_unregister() returns).

By the way, you do not need name temp, you can do

	kfree(psy_desc->name);
	kfree(psy_desc);

Thanks.

-- 
Dmitry

  reply	other threads:[~2015-07-30  0:10 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-28  0:16 [PATCH] HID: hid-input: Fix accessing freed memory during driver unbind Krzysztof Kozlowski
2015-07-29 13:07 ` Jiri Kosina
2015-07-29 17:46   ` Dmitry Torokhov
2015-07-29 23:42     ` Krzysztof Kozlowski
2015-07-30  0:10       ` Dmitry Torokhov [this message]
2015-08-01 12:11         ` Jiri Kosina
2015-08-02  5:09           ` Krzysztof Kozlowski

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=20150730001026.GH23178@dtor-ws \
    --to=dmitry.torokhov@gmail.com \
    --cc=hjl.tools@gmail.com \
    --cc=jkosina@suse.com \
    --cc=k.kozlowski@samsung.com \
    --cc=linux-input@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-pm@vger.kernel.org \
    --cc=sre@kernel.org \
    --cc=stable@vger.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.