Thank you for your contribution! Sashiko AI review found 1 potential issue(s) 
to consider:

Pre-existing issues:
- [High] Skipping `synchronize_rcu()` when `p2pdma->pool` is NULL causes a 
Use-After-Free of `pdev->p2pdma` and its XArray.
--

--- Patch [2]: [PATCH v5 2/9] PCI/P2PDMA: Add CONFIG_PCI_P2PDMA_CORE ---
commit 963552636d1deb6dc48ba93e918ef61646e05d1a
Author: Matt Evans <[email protected]>

PCI/P2PDMA: Add CONFIG_PCI_P2PDMA_CORE

This commit introduces CONFIG_PCI_P2PDMA_CORE and refactors the basic
provider functionality into a new p2pdma_core.c file. This allows drivers
to use pcim_p2pdma_provider() without a hard dependency on the full P2P DMA
management features.

> diff --git a/drivers/pci/p2pdma_core.c b/drivers/pci/p2pdma_core.c
> new file mode 100644
> --- /dev/null
> +++ b/drivers/pci/p2pdma_core.c
[ ... ]
> +static void pci_p2pdma_release(void *data)
> +{
> +     struct pci_dev *pdev = data;
> +     struct pci_p2pdma *p2pdma;
> +
> +     p2pdma = rcu_dereference_protected(pdev->p2pdma, 1);
> +     if (!p2pdma)
> +             return;
> +
> +     /* Flush and disable pci_alloc_p2p_mem() */
> +     pdev->p2pdma = NULL;
> +     pci_p2pdma_release_pool(pdev, p2pdma);
> +     xa_destroy(&p2pdma->map_types);
> +}

[Severity: High]
This is a pre-existing issue, but does skipping synchronize_rcu() when the
pool is NULL lead to a use-after-free?

Looking at pci_p2pdma_release_pool() in drivers/pci/p2pdma.c, it skips
synchronize_rcu() if the pool has not been set up:

void pci_p2pdma_release_pool(struct pci_dev *pdev, struct pci_p2pdma *p2pdma)
{
        if (!p2pdma->pool)
                return;

        synchronize_rcu();
        gen_pool_destroy(p2pdma->pool);
        sysfs_remove_group(&pdev->dev.kobj, &p2pmem_group);
}

If a driver initializes P2PDMA without setting up a pool, p2pdma->pool
will be NULL. When the device is unbound, devres executes
pci_p2pdma_release().

Because pci_p2pdma_release_pool() returns early, synchronize_rcu() is
skipped. pci_p2pdma_release() then immediately calls
xa_destroy(&p2pdma->map_types) and the p2pdma structure is freed.

Could concurrent RCU readers, such as pci_p2pdma_map_type() during a
DMA-BUF mapping, access the destroyed xarray or the freed struct and
trigger a use-after-free?

-- 
Sashiko AI review ยท 
https://sashiko.dev/#/patchset/[email protected]?part=2

Reply via email to