]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
PCI: endpoint: pci-epf-test: Fix double free that causes kernel to oops
authorChristian Bruel <christian.bruel@foss.st.com>
Fri, 24 Jan 2025 12:30:43 +0000 (13:30 +0100)
committerKrzysztof Wilczyński <kwilczynski@kernel.org>
Sat, 8 Mar 2025 14:47:35 +0000 (14:47 +0000)
Fix a kernel oops found while testing the stm32_pcie Endpoint driver
with handling of PERST# deassertion:

During EP initialization, pci_epf_test_alloc_space() allocates all BARs,
which are further freed if epc_set_bar() fails (for instance, due to no
free inbound window).

However, when pci_epc_set_bar() fails, the error path:

  pci_epc_set_bar() ->
    pci_epf_free_space()

does not clear the previous assignment to epf_test->reg[bar].

Then, if the host reboots, the PERST# deassertion restarts the BAR
allocation sequence with the same allocation failure (no free inbound
window), creating a double free situation since epf_test->reg[bar] was
deallocated and is still non-NULL.

Thus, make sure that pci_epf_alloc_space() and pci_epf_free_space()
invocations are symmetric, and as such, set epf_test->reg[bar] to NULL
when memory is freed.

Reviewed-by: Niklas Cassel <cassel@kernel.org>
Reviewed-by: Manivannan Sadhasivam <manivannan.sadhasivam@linaro.org>
Signed-off-by: Christian Bruel <christian.bruel@foss.st.com>
Link: https://lore.kernel.org/r/20250124123043.96112-1-christian.bruel@foss.st.com
[kwilczynski: commit log]
Signed-off-by: Krzysztof Wilczyński <kwilczynski@kernel.org>
drivers/pci/endpoint/functions/pci-epf-test.c

index 2409787cf56d99316889052fcd9f9da6a061b8b2..bce3ae2c0f652dab529b1c335406833ee6f7be11 100644 (file)
@@ -738,6 +738,7 @@ static int pci_epf_test_set_bar(struct pci_epf *epf)
                if (ret) {
                        pci_epf_free_space(epf, epf_test->reg[bar], bar,
                                           PRIMARY_INTERFACE);
+                       epf_test->reg[bar] = NULL;
                        dev_err(dev, "Failed to set BAR%d\n", bar);
                        if (bar == test_reg_bar)
                                return ret;
@@ -929,6 +930,7 @@ static void pci_epf_test_free_space(struct pci_epf *epf)
 
                pci_epf_free_space(epf, epf_test->reg[bar], bar,
                                   PRIMARY_INTERFACE);
+               epf_test->reg[bar] = NULL;
        }
 }