Skip to content

Commit 3d27e23

Browse files
Alex Williamsonavikivity
authored andcommitted
KVM: Device assignment permission checks
Only allow KVM device assignment to attach to devices which: - Are not bridges - Have BAR resources (assume others are special devices) - The user has permissions to use Assigning a bridge is a configuration error, it's not supported, and typically doesn't result in the behavior the user is expecting anyway. Devices without BAR resources are typically chipset components that also don't have host drivers. We don't want users to hold such devices captive or cause system problems by fencing them off into an iommu domain. We determine "permission to use" by testing whether the user has access to the PCI sysfs resource files. By default a normal user will not have access to these files, so it provides a good indication that an administration agent has granted the user access to the device. [Yang Bai: add missing #include] [avi: fix comment style] Signed-off-by: Alex Williamson <alex.williamson@redhat.com> Signed-off-by: Yang Bai <hamo.by@gmail.com> Signed-off-by: Marcelo Tosatti <mtosatti@redhat.com>
1 parent 4238737 commit 3d27e23

2 files changed

Lines changed: 79 additions & 0 deletions

File tree

‎Documentation/virtual/kvm/api.txt‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,10 @@ following flags are specified:
11541154
The KVM_DEV_ASSIGN_ENABLE_IOMMU flag is a mandatory option to ensure
11551155
isolation of the device. Usages not specifying this flag are deprecated.
11561156

1157+
Only PCI header type 0 devices with PCI BAR resources are supported by
1158+
device assignment. The user requesting this ioctl must have read/write
1159+
access to the PCI sysfs resource files associated with the device.
1160+
11571161
4.49 KVM_DEASSIGN_PCI_DEVICE
11581162

11591163
Capability: KVM_CAP_DEVICE_DEASSIGNMENT

‎virt/kvm/assigned-dev.c‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
#include <linux/pci.h>
1818
#include <linux/interrupt.h>
1919
#include <linux/slab.h>
20+
#include <linux/namei.h>
21+
#include <linux/fs.h>
2022
#include "irq.h"
2123

2224
static struct kvm_assigned_dev_kernel *kvm_find_assigned_dev(struct list_head *head,
@@ -480,12 +482,73 @@ static int kvm_vm_ioctl_deassign_dev_irq(struct kvm *kvm,
480482
return r;
481483
}
482484

485+
/*
486+
* We want to test whether the caller has been granted permissions to
487+
* use this device. To be able to configure and control the device,
488+
* the user needs access to PCI configuration space and BAR resources.
489+
* These are accessed through PCI sysfs. PCI config space is often
490+
* passed to the process calling this ioctl via file descriptor, so we
491+
* can't rely on access to that file. We can check for permissions
492+
* on each of the BAR resource files, which is a pretty clear
493+
* indicator that the user has been granted access to the device.
494+
*/
495+
static int probe_sysfs_permissions(struct pci_dev *dev)
496+
{
497+
#ifdef CONFIG_SYSFS
498+
int i;
499+
bool bar_found = false;
500+
501+
for (i = PCI_STD_RESOURCES; i <= PCI_STD_RESOURCE_END; i++) {
502+
char *kpath, *syspath;
503+
struct path path;
504+
struct inode *inode;
505+
int r;
506+
507+
if (!pci_resource_len(dev, i))
508+
continue;
509+
510+
kpath = kobject_get_path(&dev->dev.kobj, GFP_KERNEL);
511+
if (!kpath)
512+
return -ENOMEM;
513+
514+
/* Per sysfs-rules, sysfs is always at /sys */
515+
syspath = kasprintf(GFP_KERNEL, "/sys%s/resource%d", kpath, i);
516+
kfree(kpath);
517+
if (!syspath)
518+
return -ENOMEM;
519+
520+
r = kern_path(syspath, LOOKUP_FOLLOW, &path);
521+
kfree(syspath);
522+
if (r)
523+
return r;
524+
525+
inode = path.dentry->d_inode;
526+
527+
r = inode_permission(inode, MAY_READ | MAY_WRITE | MAY_ACCESS);
528+
path_put(&path);
529+
if (r)
530+
return r;
531+
532+
bar_found = true;
533+
}
534+
535+
/* If no resources, probably something special */
536+
if (!bar_found)
537+
return -EPERM;
538+
539+
return 0;
540+
#else
541+
return -EINVAL; /* No way to control the device without sysfs */
542+
#endif
543+
}
544+
483545
static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
484546
struct kvm_assigned_pci_dev *assigned_dev)
485547
{
486548
int r = 0, idx;
487549
struct kvm_assigned_dev_kernel *match;
488550
struct pci_dev *dev;
551+
u8 header_type;
489552

490553
if (!(assigned_dev->flags & KVM_DEV_ASSIGN_ENABLE_IOMMU))
491554
return -EINVAL;
@@ -516,6 +579,18 @@ static int kvm_vm_ioctl_assign_device(struct kvm *kvm,
516579
r = -EINVAL;
517580
goto out_free;
518581
}
582+
583+
/* Don't allow bridges to be assigned */
584+
pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type);
585+
if ((header_type & PCI_HEADER_TYPE) != PCI_HEADER_TYPE_NORMAL) {
586+
r = -EPERM;
587+
goto out_put;
588+
}
589+
590+
r = probe_sysfs_permissions(dev);
591+
if (r)
592+
goto out_put;
593+
519594
if (pci_enable_device(dev)) {
520595
printk(KERN_INFO "%s: Could not enable PCI device\n", __func__);
521596
r = -EBUSY;

0 commit comments

Comments
 (0)