From: Lukas Wunner <lukas@wunner.de>
To: Florian Fainelli <f.fainelli@gmail.com>
Cc: Mark Brown <broonie@kernel.org>,
Vladimir Oltean <olteanv@gmail.com>,
"linux-kernel@vger.kernel.org" <linux-kernel@vger.kernel.org>,
linux-spi <linux-spi@vger.kernel.org>
Subject: Re: Use after free in bcm2835_spi_remove()
Date: Thu, 22 Oct 2020 14:12:54 +0200 [thread overview]
Message-ID: <20201022121254.GA3847@wunner.de> (raw)
In-Reply-To: <b094c266-99ce-4462-9041-7d1659b13300@gmail.com>
On Wed, Oct 14, 2020 at 02:20:16PM -0700, Florian Fainelli wrote:
> In bcm2835_spi_remove(), spi_controller_unregister() will free the ctlr
> reference which will lead to an use after free in bcm2835_release_dma().
>
> To avoid this use after free, allocate the bcm2835_spi structure with a
> different lifecycle than the spi_controller structure such that we
> unregister the SPI controller, free up all the resources and finally let
> device managed allocations free the bcm2835_spi structure.
[...]
> - if (ctlr->dma_tx) {
> - dmaengine_terminate_sync(ctlr->dma_tx);
> + if (dma_tx) {
> + dmaengine_terminate_sync(dma_tx);
>
> if (bs->fill_tx_desc)
> dmaengine_desc_free(bs->fill_tx_desc);
>
> if (bs->fill_tx_addr)
> - dma_unmap_page_attrs(ctlr->dma_tx->device->dev,
> + dma_unmap_page_attrs(dma_tx->device->dev,
> bs->fill_tx_addr, sizeof(u32),
> DMA_TO_DEVICE,
> DMA_ATTR_SKIP_CPU_SYNC);
>
> - dma_release_channel(ctlr->dma_tx);
> - ctlr->dma_tx = NULL;
> + dma_release_channel(dma_tx);
> }
You must set ctlr->dma_tx and ctlr->dma_rx to NULL because the driver
checks their value in a couple of places.
E.g. bcm2835_spi_setup() checks ctlr->dma_rx.
Likewise, the error paths of bcm2835_dma_init() and bcm2835_spi_probe()
call bcm2835_dma_release() and the latter checks ctlr->dma_tx and
ctlr->dma_rx to determine whether DMA was set up, hence needs to be
torn down.
> + bs = devm_kzalloc(&pdev->dev, sizeof(*bs), GFP_KERNEL);
> + if (!bs)
> + return -ENOMEM;
> +
> ctlr = spi_alloc_master(&pdev->dev, ALIGN(sizeof(*bs),
> dma_get_cache_alignment()));
You can set the second argument to spi_alloc_master() to 0
to conserve memory.
Thanks,
Lukas
next prev parent reply other threads:[~2020-10-22 12:13 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-10-13 23:48 Use after free in bcm2835_spi_remove() Florian Fainelli
2020-10-14 14:09 ` Lukas Wunner
2020-10-14 19:40 ` Vladimir Oltean
2020-10-14 20:25 ` Mark Brown
2020-10-14 21:20 ` Florian Fainelli
2020-10-22 12:12 ` Lukas Wunner [this message]
2020-10-15 5:38 ` Lukas Wunner
2020-10-15 12:53 ` Mark Brown
2020-10-28 9:59 ` Lukas Wunner
2020-10-29 22:24 ` Mark Brown
2020-11-11 19:07 ` [PATCH 0/4] Use-after-free be gone Lukas Wunner
2020-11-11 19:07 ` [PATCH 1/4] spi: Introduce device-managed SPI controller allocation Lukas Wunner
2020-11-11 19:07 ` [PATCH 2/4] spi: bcm2835: Fix use-after-free on unbind Lukas Wunner
2020-11-11 20:18 ` Florian Fainelli
2020-11-11 19:07 ` [PATCH 3/4] spi: bcm2835aux: " Lukas Wunner
2020-11-11 19:07 ` [PATCH 4/4] spi: bcm-qspi: " Lukas Wunner
2020-11-11 21:12 ` Florian Fainelli
2020-11-12 13:50 ` [PATCH 0/4] Use-after-free be gone Mark Brown
2020-11-12 19:39 ` 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=20201022121254.GA3847@wunner.de \
--to=lukas@wunner.de \
--cc=broonie@kernel.org \
--cc=f.fainelli@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-spi@vger.kernel.org \
--cc=olteanv@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 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.