linux-spi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Linus Walleij <linus.walleij@linaro.org>
To: Lukas Wunner <lukas@wunner.de>
Cc: Mark Brown <broonie@kernel.org>,
	linux-spi <linux-spi@vger.kernel.org>,
	Navid Emamdoost <navid.emamdoost@gmail.com>,
	Andrey Smirnov <andrew.smirnov@gmail.com>
Subject: Re: [PATCH for-5.10] spi: gpio: Don't leak SPI master in probe error path
Date: Wed, 18 Nov 2020 02:08:05 +0100	[thread overview]
Message-ID: <CACRpkdZeLvNz+b5K3iXMOTk=2JciFco5emhYEA_oeG73t=xdhw@mail.gmail.com> (raw)
In-Reply-To: <49102f5bbb3f1592d9cfd7b39ac5e131a031f950.1605512876.git.lukas@wunner.de>

On Mon, Nov 16, 2020 at 9:44 AM Lukas Wunner <lukas@wunner.de> wrote:

> If the call to devm_spi_register_master() fails on probe of the GPIO SPI
> driver, the spi_master struct is erroneously not freed:
>
> After allocating the spi_master, its reference count is 1.  The driver
> unconditionally decrements the reference count on unbind using a devm
> action.  Before calling devm_spi_register_master(), the driver
> unconditionally increments the reference count because on success,
> that function will decrement the reference count on unbind.  However on
> failure, devm_spi_register_master() does *not* decrement the reference
> count, so the spi_master is leaked.
>
> The issue was introduced by commits 8b797490b4db ("spi: gpio: Make sure
> spi_master_put() is called in every error path") and 79567c1a321e ("spi:
> gpio: Use devm_spi_register_master()"), which sought to plug leaks
> introduced by 9b00bc7b901f ("spi: spi-gpio: Rewrite to use GPIO
> descriptors") but missed this remaining leak.
>
> The situation was later aggravated by commit d3b0ffa1d75d ("spi: gpio:
> prevent memory leak in spi_gpio_probe"), which introduced a
> use-after-free because it releases a reference on the spi_master if
> devm_add_action_or_reset() fails even though the function already
> does that.
>
> Fix by switching over to the new devm_spi_alloc_master() helper.
>
> Fixes: 9b00bc7b901f ("spi: spi-gpio: Rewrite to use GPIO descriptors")
> Signed-off-by: Lukas Wunner <lukas@wunner.de>
> Cc: <stable@vger.kernel.org> # v4.17+: 5e844cc37a5c: spi: Introduce device-managed SPI controller allocation
> Cc: <stable@vger.kernel.org> # v5.1-: 8b797490b4db: spi: gpio: Make sure spi_master_put() is called in every error path
> Cc: <stable@vger.kernel.org> # v5.1-: 45beec351998: spi: bitbang: Introduce spi_bitbang_init()
> Cc: <stable@vger.kernel.org> # v5.1-: 79567c1a321e: spi: gpio: Use devm_spi_register_master()
> Cc: <stable@vger.kernel.org> # v5.4-: d3b0ffa1d75d: spi: gpio: prevent memory leak in spi_gpio_probe
> Cc: <stable@vger.kernel.org> # v4.17+
> Cc: Navid Emamdoost <navid.emamdoost@gmail.com>
> Cc: Andrey Smirnov <andrew.smirnov@gmail.com>
> Cc: Linus Walleij <linus.walleij@linaro.org>

That's a really good fix. Thanks to looking into this!
Reviewed-by: Linus Walleij <linus.walleij@linaro.org>

Yours,
Linus Walleij

  parent reply	other threads:[~2020-11-18  1:08 UTC|newest]

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: Fix use-after-free on unbind 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
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 [this message]
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='CACRpkdZeLvNz+b5K3iXMOTk=2JciFco5emhYEA_oeG73t=xdhw@mail.gmail.com' \
    --to=linus.walleij@linaro.org \
    --cc=andrew.smirnov@gmail.com \
    --cc=broonie@kernel.org \
    --cc=linux-spi@vger.kernel.org \
    --cc=lukas@wunner.de \
    --cc=navid.emamdoost@gmail.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;
as well as URLs for NNTP newsgroup(s).