Skip to content

Commit 3708acb

Browse files
floatiouskwilczynski
authored andcommitted
PCI: dwc: ep: Prevent changing BAR size/flags in pci_epc_set_bar()
In commit 4284c88 ("PCI: designware-ep: Allow pci_epc_set_bar() update inbound map address") set_bar() was modified to support dynamically changing the backing physical address of a BAR that was already configured. This means that set_bar() can be called twice, without ever calling clear_bar() (as calling clear_bar() would clear the BAR's PCI address assigned by the host). This can only be done if the new BAR size/flags does not differ from the existing BAR configuration. Add these missing checks. If we allow set_bar() to set e.g. a new BAR size that differs from the existing BAR size, the new address translation range will be smaller than the BAR size already determined by the host, which would mean that a read past the new BAR size would pass the iATU untranslated, which could allow the host to read memory not belonging to the new struct pci_epf_bar. While at it, add comments which clarifies the support for dynamically changing the physical address of a BAR. (Which was also missing.) Fixes: 4284c88 ("PCI: designware-ep: Allow pci_epc_set_bar() update inbound map address") Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Niklas Cassel <[email protected]> Signed-off-by: Krzysztof Wilczyński <[email protected]> Reviewed-by: Manivannan Sadhasivam <[email protected]> Cc: [email protected]
1 parent 33a6938 commit 3708acb

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

drivers/pci/controller/dwc/pcie-designware-ep.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,8 +222,28 @@ static int dw_pcie_ep_set_bar(struct pci_epc *epc, u8 func_no, u8 vfunc_no,
222222
if ((flags & PCI_BASE_ADDRESS_MEM_TYPE_64) && (bar & 1))
223223
return -EINVAL;
224224

225-
if (ep->epf_bar[bar])
225+
/*
226+
* Certain EPF drivers dynamically change the physical address of a BAR
227+
* (i.e. they call set_bar() twice, without ever calling clear_bar(), as
228+
* calling clear_bar() would clear the BAR's PCI address assigned by the
229+
* host).
230+
*/
231+
if (ep->epf_bar[bar]) {
232+
/*
233+
* We can only dynamically change a BAR if the new BAR size and
234+
* BAR flags do not differ from the existing configuration.
235+
*/
236+
if (ep->epf_bar[bar]->barno != bar ||
237+
ep->epf_bar[bar]->size != size ||
238+
ep->epf_bar[bar]->flags != flags)
239+
return -EINVAL;
240+
241+
/*
242+
* When dynamically changing a BAR, skip writing the BAR reg, as
243+
* that would clear the BAR's PCI address assigned by the host.
244+
*/
226245
goto config_atu;
246+
}
227247

228248
reg = PCI_BASE_ADDRESS_0 + (4 * bar);
229249

0 commit comments

Comments
 (0)