]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
uio: uio_pci_generic_sva: fix double free of devm_kzalloc() memory
authorGuangshuo Li <lgs201920130244@gmail.com>
Tue, 5 May 2026 15:02:56 +0000 (23:02 +0800)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Fri, 22 May 2026 10:19:02 +0000 (12:19 +0200)
uio_pci_sva allocates struct uio_pci_sva_dev with devm_kzalloc() in
probe(), but then calls kfree(udev) both on the probe() error path
(label out_free) and again in remove().

Because devm_kzalloc() allocations are devres-managed and are freed
automatically when the device is detached (including after a failing
probe() and during driver unbind), the explicit kfree() can lead to a
double free.

If probe() fails after devm_kzalloc(), the error path frees udev and
devres cleanup will free it again when the core unwinds the partially
bound device. On normal driver removal, remove() frees udev and devres
will free it again when the device is detached.

This issue was identified by a static analysis tool I developed and
confirmed by manual review. Fix by removing the manual kfree() calls
and dropping the now-unused label.

Fixes: 3397c3cd859a2 ("uio: Add SVA support for PCI devices via uio_pci_generic_sva.c")
Cc: stable <stable@kernel.org>
Signed-off-by: Guangshuo Li <lgs201920130244@gmail.com>
Link: https://patch.msgid.link/20260505150256.614071-1-lgs201920130244@gmail.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/uio/uio_pci_generic_sva.c

index 4a46acd994a85d38d9c732e0ae58cd5a0c94342b..d05ef77f7e322f2568bd383ccb2d1546334f61c6 100644 (file)
@@ -129,15 +129,13 @@ static int probe(struct pci_dev *pdev, const struct pci_device_id *id)
        ret = devm_uio_register_device(&pdev->dev, &udev->info);
        if (ret) {
                dev_err(&pdev->dev, "Failed to register uio device\n");
-               goto out_free;
+               goto out_disable;
        }
 
        pci_set_drvdata(pdev, udev);
 
        return 0;
 
-out_free:
-       kfree(udev);
 out_disable:
        pci_disable_device(pdev);
 
@@ -146,11 +144,8 @@ out_disable:
 
 static void remove(struct pci_dev *pdev)
 {
-       struct uio_pci_sva_dev *udev = pci_get_drvdata(pdev);
-
        pci_release_regions(pdev);
        pci_disable_device(pdev);
-       kfree(udev);
 }
 
 static ssize_t pasid_show(struct device *dev,