Skip to content

Commit 3f63a1d

Browse files
Keir Frasermstsirkin
authored andcommitted
virtio: pci: check bar values read from virtio config space
virtio pci config structures may in future have non-standard bar values in the bar field. We should anticipate this by skipping any structures containing such a reserved value. The bar value should never change: check for harmful modified values we re-read it from the config space in vp_modern_map_capability(). Also clean up an existing check to consistently use PCI_STD_NUM_BARS. Signed-off-by: Keir Fraser <keirf@google.com> Link: https://lore.kernel.org/r/20220323140727.3499235-1-keirf@google.com Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
1 parent eb4cecb commit 3f63a1d

2 files changed

Lines changed: 17 additions & 4 deletions

File tree

drivers/virtio/virtio_pci_modern.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ static int virtio_pci_find_shm_cap(struct pci_dev *dev, u8 required_id,
293293

294294
for (pos = pci_find_capability(dev, PCI_CAP_ID_VNDR); pos > 0;
295295
pos = pci_find_next_capability(dev, pos, PCI_CAP_ID_VNDR)) {
296-
u8 type, cap_len, id;
296+
u8 type, cap_len, id, res_bar;
297297
u32 tmp32;
298298
u64 res_offset, res_length;
299299

@@ -315,9 +315,14 @@ static int virtio_pci_find_shm_cap(struct pci_dev *dev, u8 required_id,
315315
if (id != required_id)
316316
continue;
317317

318-
/* Type, and ID match, looks good */
319318
pci_read_config_byte(dev, pos + offsetof(struct virtio_pci_cap,
320-
bar), bar);
319+
bar), &res_bar);
320+
if (res_bar >= PCI_STD_NUM_BARS)
321+
continue;
322+
323+
/* Type and ID match, and the BAR value isn't reserved.
324+
* Looks good.
325+
*/
321326

322327
/* Read the lower 32bit of length and offset */
323328
pci_read_config_dword(dev, pos + offsetof(struct virtio_pci_cap,
@@ -337,6 +342,7 @@ static int virtio_pci_find_shm_cap(struct pci_dev *dev, u8 required_id,
337342
length_hi), &tmp32);
338343
res_length |= ((u64)tmp32) << 32;
339344

345+
*bar = res_bar;
340346
*offset = res_offset;
341347
*len = res_length;
342348

drivers/virtio/virtio_pci_modern_dev.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,13 @@ vp_modern_map_capability(struct virtio_pci_modern_device *mdev, int off,
3535
pci_read_config_dword(dev, off + offsetof(struct virtio_pci_cap, length),
3636
&length);
3737

38+
/* Check if the BAR may have changed since we requested the region. */
39+
if (bar >= PCI_STD_NUM_BARS || !(mdev->modern_bars & (1 << bar))) {
40+
dev_err(&dev->dev,
41+
"virtio_pci: bar unexpectedly changed to %u\n", bar);
42+
return NULL;
43+
}
44+
3845
if (length <= start) {
3946
dev_err(&dev->dev,
4047
"virtio_pci: bad capability len %u (>%u expected)\n",
@@ -120,7 +127,7 @@ static inline int virtio_pci_find_capability(struct pci_dev *dev, u8 cfg_type,
120127
&bar);
121128

122129
/* Ignore structures with reserved BAR values */
123-
if (bar > 0x5)
130+
if (bar >= PCI_STD_NUM_BARS)
124131
continue;
125132

126133
if (type == cfg_type) {

0 commit comments

Comments
 (0)