Linux-SPI Archive on lore.kernel.org
 help / color / Atom feed
From: Lukas Wunner <lukas@wunner.de>
To: Sergey Shtylyov <s.shtylyov@omprussia.ru>
Cc: Mark Brown <broonie@kernel.org>, linux-spi@vger.kernel.org
Subject: Re: [PATCH for-5.10] spi: rpc-if: Fix use-after-free on unbind
Date: Wed, 2 Dec 2020 12:43:04 +0100
Message-ID: <20201202114304.GA25779@wunner.de> (raw)
In-Reply-To: <31f68249-2499-7ca6-9804-aad39e94b3b6@omprussia.ru>

On Mon, Nov 30, 2020 at 10:18:12PM +0300, Sergey Shtylyov wrote:
> On 11/29/20 2:35 PM, Lukas Wunner wrote:
> > > Not sure why spi_unregister_controller() drops the device reference
> > > while spi_register_controller() itself doesn't allocate the memory... 
> > 
> > Yes, that's exactly what I'm trying to move away from with
> > devm_spi_alloc_master() (introduced in v5.10-rc5 by 5e844cc37a5c).
> > The API as it has been so far has made it really easy to shoot oneself
> > in the foot.
> 
> Maybe it needs to be fixed, rather than using the managed device API?

devm_spi_alloc_master() *is* the fix, or at least a means to get there:

No longer dropping the reference in spi_unregister_controller() requires
that the drivers drop the reference.  So every single SPI driver needs to
be touched.  However, upon closer examination I've found tons of bugs in
the ->probe and ->remove hooks of SPI drivers, some of them related to
reference counting (leaks or use-after-free), others related to not
disabling clocks properly etc.  Ideally, the fixes for those bugs should
be backported to stable.

devm_spi_alloc_master() allows me to do that and at the same time it
allows stretching the migration across multiple releases.  That's because
spi_unregister_controller() auto-senses if devm_spi_alloc_master() was
used, and if so, it no longer drops a reference.

devm_spi_alloc_master() has the additional advantage of simplifying
probe error paths, as is apparent from the diffstat of the $subject patch:

 drivers/spi/spi-rpc-if.c | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)

I think the vast majority of SPI drivers can be converted to
devm_spi_alloc_master() and the few that can't will be amended to
explicitly drop a reference.


> > > Perhaps the order of the calls in the remove() method could be reversed? 
> > 
> > I'm not familiar with power management on these Renesas controllers
> > but rpcif_disable_rpm() calls pm_runtime_put_sync(), which I assume
> > may put the controller to sleep.
> 
> Sigh, that's a stupid typo on my part, being fixed now to
> pm_runtim_disable()...

Okay in that case the order of the two calls in	rpcif_spi_remove()
won't matter, i.e. it would actually be possible to fix the UAF by
calling rpcif_disable_rpm() before spi_unregister_controller().

However, I still recommend fixing the UAF in the way proposed by
the $subject patch because of the simplified probe error path and
reduced LoC.


> > The only thing that looks confusing is that rpcif_enable_rpm() calls
> > pm_runtime_enable(), whereas rpcif_disable_rpm() calls
> > pm_runtime_put_sync().  That looks incongruent.
> 
> Do you need a link to the fix (it a whole patchset of minor fixes)?

I don't *need* it, but am happy to take a look.  Glad that I was able to
point out another bug. :)

Thanks,

Lukas

  reply index

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-16  8:23 [PATCH for-5.10] spi: spi-geni-qcom: " Lukas Wunner
2020-11-16  8:23 ` [PATCH for-5.10] spi: spi-qcom-qspi: " Lukas Wunner
2020-11-16  8:23 ` [PATCH for-5.10] spi: spi-sh: " Lukas Wunner
2020-11-16  8:23 ` [PATCH for-5.10] spi: pxa2xx: " Lukas Wunner
2020-11-16  8:23 ` [PATCH for-5.10] spi: rpc-if: " Lukas Wunner
2020-11-28 20:20   ` Sergey Shtylyov
2020-11-29 11:35     ` Lukas Wunner
2020-11-30 19:18       ` Sergey Shtylyov
2020-12-02 11:43         ` Lukas Wunner [this message]
2020-11-16  8:23 ` [PATCH for-5.10] spi: mxic: Don't leak SPI master in probe error path Lukas Wunner
2020-11-16  8:23 ` [PATCH for-5.10] spi: mt7621: " Lukas Wunner
2020-11-16 11:05   ` Stefan Roese
2020-11-16  8:23 ` [PATCH for-5.10] spi: spi-mtk-nor: " Lukas Wunner
2020-11-17  4:02   ` Ikjoon Jang
2020-11-17 12:32   ` Mark Brown
2020-11-16  8:23 ` [PATCH for-5.10] spi: gpio: " Lukas Wunner
2020-11-16 19:23   ` Andrey Smirnov
2020-11-16 23:03     ` Lukas Wunner
2020-11-16 23:59       ` Andrey Smirnov
2020-11-18  1:08   ` Linus Walleij
2020-11-16  8:23 ` [PATCH for-5.10] spi: npcm-fiu: " Lukas Wunner
2020-11-17 22:38   ` Mark Brown
2020-12-01 13:57   ` Mark Brown
2020-12-01 14:30     ` Lukas Wunner
2020-12-01 17:17       ` Mark Brown
2020-12-01 17:49         ` Lukas Wunner
2020-12-02 15:17           ` Mark Brown
2020-11-16  8:23 ` [PATCH for-5.10] spi: rb4xx: " Lukas Wunner
2020-11-16  8:23 ` [PATCH for-5.10] spi: sc18is602: " Lukas Wunner
2020-11-16  8:23 ` [PATCH for-5.10] media: netup_unidvb: " Lukas Wunner
2020-11-23 14:06   ` Mauro Carvalho Chehab
2020-12-01 13:57   ` Mark Brown

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=20201202114304.GA25779@wunner.de \
    --to=lukas@wunner.de \
    --cc=broonie@kernel.org \
    --cc=linux-spi@vger.kernel.org \
    --cc=s.shtylyov@omprussia.ru \
    /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

Linux-SPI Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-spi/0 linux-spi/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-spi linux-spi/ https://lore.kernel.org/linux-spi \
		linux-spi@vger.kernel.org
	public-inbox-index linux-spi

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-spi


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git