Re: [PATCH] PCI: endpoint: Fix use after free in pci_epf_remove_cfs()

From: Lorenzo Pieralisi
Date: Mon Dec 06 2021 - 06:45:38 EST


[dropped stable, erroneously added to the CC list]

On Mon, Jun 21, 2021 at 04:00:58PM +0900, Shunsuke Mie wrote:
> All of entries are freed in a loop, however, the freed entry is accessed
> by list_del() after the loop.
>
> When epf driver that includes pci-epf-test unload, the pci_epf_remove_cfs()
> is called, and occurred the use after free. Therefore, kernel panics
> randomly after or while the module unloading.
>
> I tested this patch with r8a77951-Salvator-xs boards.
>
> Fixes: ef1433f ("PCI: endpoint: Create configfs entry for each pci_epf_device_id table entry")
> Signed-off-by: Shunsuke Mie <mie@xxxxxxxxxx>
> ---
> drivers/pci/endpoint/pci-epf-core.c | 4 +++-
> 1 file changed, 3 insertions(+), 1 deletion(-)

Kishon, please review this patch, thanks.

Lorenzo

>
> diff --git a/drivers/pci/endpoint/pci-epf-core.c b/drivers/pci/endpoint/pci-epf-core.c
> index e9289d10f822..538e902b0ba6 100644
> --- a/drivers/pci/endpoint/pci-epf-core.c
> +++ b/drivers/pci/endpoint/pci-epf-core.c
> @@ -202,8 +202,10 @@ static void pci_epf_remove_cfs(struct pci_epf_driver *driver)
> return;
>
> mutex_lock(&pci_epf_mutex);
> - list_for_each_entry_safe(group, tmp, &driver->epf_group, group_entry)
> + list_for_each_entry_safe(group, tmp, &driver->epf_group, group_entry) {
> + list_del(&group->group_entry);
> pci_ep_cfs_remove_epf_group(group);
> + }
> list_del(&driver->epf_group);
> mutex_unlock(&pci_epf_mutex);
> }
> --
> 2.17.1
>