Linux ATA/IDE development
 help / color / mirror / Atom feed
From: Niklas Cassel <cassel@kernel.org>
To: Damien Le Moal <dlemoal@kernel.org>
Cc: Baokun Li <libaokun1@huawei.com>,
	Sergei Shtylyov <sergei.shtylyov@gmail.com>,
	Brian Norris <computersforpeace@gmail.com>,
	stable@vger.kernel.org,
	Damien Le Moal <damien.lemoal@opensource.wdc.com>,
	linux-ide@vger.kernel.org
Subject: Re: [PATCH v4 2/5] ata: sata_fsl: Fix use-after-free of host_priv on probe() failure
Date: Tue, 15 Sep 2026 10:40:33 +0200	[thread overview]
Message-ID: <aqkEgcs6NBiZjjgR@ryzen> (raw)
In-Reply-To: <34709b8b-71e9-46d2-8732-a802e544701e@kernel.org>

On Tue, Sep 15, 2026 at 11:06:55AM +0700, Damien Le Moal wrote:
> On 2026/09/11 0:14, Niklas Cassel wrote:
> > sata_fsl_host_stop() releases hcr_base and host_priv:
> > 
> > 	static void sata_fsl_host_stop(struct ata_host *host)
> > 	{
> > 		struct sata_fsl_host_priv *host_priv = host->private_data;
> > 
> > 		iounmap(host_priv->hcr_base);
> > 		kfree(host_priv);
> > 	}
> > 
> > ->host_stop() is called by the driver core through the ata_host_stop()
> > devres action which ata_host_start() registers, so it is called both
> > when the device is unbound and when probe() fails after
> > ata_host_activate() has started the host.
> > 
> > The error_exit_with_cleanup label in sata_fsl_probe() releases hcr_base
> > and host_priv as well, and it is reachable from the two
> > device_create_file() calls done after ata_host_activate(). In that case
> > sata_fsl_host_stop() runs on an already freed host_priv, resulting in a
> > use-after-free and a double iounmap()/kfree().
> > 
> > sata_fsl_probe() also ignores the return value of ata_host_activate(),
> > so probe() returns success even if activating the host failed.
> > 
> > Check the return value of ata_host_activate() and, once the host has
> > been activated, leave hcr_base and host_priv to sata_fsl_host_stop().
> > 
> > Note that if ata_host_activate() fails inside ata_host_start(), e.g. if
> > sata_fsl_port_start() fails, ->host_stop() is not registered and
> > hcr_base and host_priv are leaked. Leaking them is preferable to freeing
> > them twice, and this is addressed by a later patch in this series.
> 
> I would prefer seeing the leak fixed here too so that we have a self contained
> fix in a single commit.
> 
> Or, move the leak fix patch before this one.

I took a third option:

This (temporary) leak was only added by this patch, and was later fixed
by "ata: libata: Do not leave ata_host_stop() registered when activation
fails".

The UAF happened only when the activation succeeded and device_create_file()
failed. Thus, this fix does not rely on ata_host_activate() error handling.

Thus, let this patch only fix the UAF, without adding any error handling to
ata_host_activate(). That way this patch is self contained.

Patch "ata: libata: Do not leave ata_host_stop() registered when activation
fails" will add ata_host_activate() error handling to this driver, and at
this point, ata_host_activate() internals has been fixed. Thus, we there
will never exist any intermediate step where we might leak resources.


Kind regards,
Niklas

  reply	other threads:[~2026-09-15  8:40 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-10 17:14 [PATCH v4 0/5] ata: Do not release the host resources twice on probe() failure Niklas Cassel
2026-09-10 17:14 ` [PATCH v4 1/5] ata: ahci_st: Assert the power down reset in the probe() error path Niklas Cassel
2026-09-10 17:29   ` sashiko-bot
2026-09-10 17:39     ` Niklas Cassel
2026-09-15  4:00   ` Damien Le Moal
2026-09-10 17:14 ` [PATCH v4 2/5] ata: sata_fsl: Fix use-after-free of host_priv on probe() failure Niklas Cassel
2026-09-15  4:06   ` Damien Le Moal
2026-09-15  8:40     ` Niklas Cassel [this message]
2026-09-10 17:14 ` [PATCH v4 3/5] ata: libahci: Free the IRQs when activating a multi-IRQ host fails Niklas Cassel
2026-09-15  4:07   ` Damien Le Moal
2026-09-10 17:14 ` [PATCH v4 4/5] ata: libata: Do not leave ata_host_stop() registered when activation fails Niklas Cassel
2026-09-10 17:14 ` [PATCH v4 5/5] ata: libata-core: Fix the ata_host_register() kdoc Niklas Cassel
2026-09-15  4:08   ` Damien Le Moal

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=aqkEgcs6NBiZjjgR@ryzen \
    --to=cassel@kernel.org \
    --cc=computersforpeace@gmail.com \
    --cc=damien.lemoal@opensource.wdc.com \
    --cc=dlemoal@kernel.org \
    --cc=libaokun1@huawei.com \
    --cc=linux-ide@vger.kernel.org \
    --cc=sergei.shtylyov@gmail.com \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox