From: Zheng Qixing Date: Thu, 22 Aug 2024 03:30:50 +0000 (+0800) Subject: ata: libata: Fix memory leak for error path in ata_host_alloc() X-Git-Tag: v6.11-rc7~43^2 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=284b75a3d83c7631586d98f6dede1d90f128f0db;p=thirdparty%2Fkernel%2Flinux.git ata: libata: Fix memory leak for error path in ata_host_alloc() 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 Reviewed-by: Yu Kuai Signed-off-by: Damien Le Moal --- diff --git a/drivers/ata/libata-core.c b/drivers/ata/libata-core.c index c7752dc800280..30932552437a7 100644 --- a/drivers/ata/libata-core.c +++ b/drivers/ata/libata-core.c @@ -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);