Skip to content

Commit bb8d413

Browse files
KAGA-KOKOjbarnes993
authored andcommitted
x86/PCI: Prevent mmconfig memory corruption
commit ff097dd (x86/PCI: MMCONFIG: manage pci_mmcfg_region as a list, not a table) introduced a nasty memory corruption when pci_mmcfg_list is empty. pci_mmcfg_check_end_bus_number() dereferences pci_mmcfg_list.prev even when the list is empty. The following write hits some variable near to pci_mmcfg_list. Further down a similar problem exists, where cfg->list.next is dereferenced unconditionally and a comparison with some variable near to pci_mmcfg_list happens. Add a check for the last element into the for_each_entry() loop and remove all the other crappy logic which is just a leftover of the old array based code which was replaced by the list conversion. Reported-by: Ingo Molnar <mingo@elte.hu> Signed-off-by: Thomas Gleixner <tglx@linutronix.de> Cc: Bjorn Helgaas <bjorn.helgaas@hp.com> Cc: Yinghai Lu <yinghai@kernel.org> Cc: stable@kernel.org Signed-off-by: Jesse Barnes <jbarnes@virtuousgeek.org>
1 parent cbbc0de commit bb8d413

File tree

1 file changed

+6
-11
lines changed

1 file changed

+6
-11
lines changed

arch/x86/pci/mmconfig-shared.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -303,22 +303,17 @@ static void __init pci_mmcfg_check_end_bus_number(void)
303303
{
304304
struct pci_mmcfg_region *cfg, *cfgx;
305305

306-
/* last one*/
307-
cfg = list_entry(pci_mmcfg_list.prev, typeof(*cfg), list);
308-
if (cfg)
309-
if (cfg->end_bus < cfg->start_bus)
310-
cfg->end_bus = 255;
311-
312-
if (list_is_singular(&pci_mmcfg_list))
313-
return;
314-
315-
/* don't overlap please */
306+
/* Fixup overlaps */
316307
list_for_each_entry(cfg, &pci_mmcfg_list, list) {
317308
if (cfg->end_bus < cfg->start_bus)
318309
cfg->end_bus = 255;
319310

311+
/* Don't access the list head ! */
312+
if (cfg->list.next == &pci_mmcfg_list)
313+
break;
314+
320315
cfgx = list_entry(cfg->list.next, typeof(*cfg), list);
321-
if (cfg != cfgx && cfg->end_bus >= cfgx->start_bus)
316+
if (cfg->end_bus >= cfgx->start_bus)
322317
cfg->end_bus = cfgx->start_bus - 1;
323318
}
324319
}

0 commit comments

Comments
 (0)