]> www.infradead.org Git - linux.git/commitdiff
ata: libata: Fix memory leak for error path in ata_host_alloc()
authorZheng Qixing <zhengqixing@huawei.com>
Thu, 22 Aug 2024 03:30:50 +0000 (11:30 +0800)
committerDamien Le Moal <dlemoal@kernel.org>
Mon, 26 Aug 2024 21:54:36 +0000 (06:54 +0900)
In ata_host_alloc(), if devres_alloc() fails to allocate the device host
resource data pointer, the already allocated ata_host structure is not
freed before returning from the function. This results in a potential
memory leak.

Call kfree(host) before jumping to the error handling path to ensure
that the ata_host structure is properly freed if devres_alloc() fails.

Fixes: 2623c7a5f279 ("libata: add refcounting to ata_host")
Cc: stable@vger.kernel.org
Signed-off-by: Zheng Qixing <zhengqixing@huawei.com>
Reviewed-by: Yu Kuai <yukuai3@huawei.com>
Signed-off-by: Damien Le Moal <dlemoal@kernel.org>
drivers/ata/libata-core.c

index c7752dc800280ed6102e0a5bce29b74c68ca9a92..30932552437a7eb876e2fe1d8459da245f9ce73e 100644 (file)
@@ -5593,8 +5593,10 @@ struct ata_host *ata_host_alloc(struct device *dev, int n_ports)
        }
 
        dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL);
-       if (!dr)
+       if (!dr) {
+               kfree(host);
                goto err_out;
+       }
 
        devres_add(dev, dr);
        dev_set_drvdata(dev, host);