History

vfio/pci: Use the struct file as the handle not the vfio_group

VFIO PCI does a security check as part of hot reset to prove that the user has permission to manipulate all the devices that will be impacted by the reset. Use a new API vfio_file_has_dev() to perform this security check against the struct file directly and remove the vfio_group from VFIO PCI. Since VFIO PCI was the last user of vfio_group_get_external_user() and vfio_group_put_external_user() remove it as well.

Jason Gunthorpe committed 4 years ago

Commit 6a985ae1 parent 3e5449d3 files · +40 −75Browse files

Unified · Split

3 files
  1. drivers/vfio/pci/vfio_pci_core.c +21 −21
  2. drivers/vfio/vfio.c +18 −52
  3. include/linux/vfio.h +1 −2
drivers/vfio/pci/vfio_pci_core.c+21−21
@@ -556,7 +556,7 @@ static int vfio_pci_fill_devs(struct pci_dev *pdev, void *data) · 555 unmodified lines
556 556
557struct vfio_pci_group_info {557struct vfio_pci_group_info {
558 int count;558 int count;
559 struct vfio_group **groups;559 struct file **files;
560};560};
561 561
562static bool vfio_pci_dev_below_slot(struct pci_dev *pdev, struct pci_slot *slot)562static bool vfio_pci_dev_below_slot(struct pci_dev *pdev, struct pci_slot *slot)
@@ -1018,10 +1018,10 @@ reset_info_exit: · 455 unmodified lines
1018 } else if (cmd == VFIO_DEVICE_PCI_HOT_RESET) {1018 } else if (cmd == VFIO_DEVICE_PCI_HOT_RESET) {
1019 struct vfio_pci_hot_reset hdr;1019 struct vfio_pci_hot_reset hdr;
1020 int32_t *group_fds;1020 int32_t *group_fds;
1021 struct vfio_group **groups;1021 struct file **files;
1022 struct vfio_pci_group_info info;1022 struct vfio_pci_group_info info;
1023 bool slot = false;1023 bool slot = false;
1024 int group_idx, count = 0, ret = 0;1024 int file_idx, count = 0, ret = 0;
1025 1025
1026 minsz = offsetofend(struct vfio_pci_hot_reset, count);1026 minsz = offsetofend(struct vfio_pci_hot_reset, count);
1027 1027
@@ -1054,17 +1054,17 @@ reset_info_exit: · 26 unmodified lines
1054 return -EINVAL;1054 return -EINVAL;
1055 1055
1056 group_fds = kcalloc(hdr.count, sizeof(*group_fds), GFP_KERNEL);1056 group_fds = kcalloc(hdr.count, sizeof(*group_fds), GFP_KERNEL);
1057 groups = kcalloc(hdr.count, sizeof(*groups), GFP_KERNEL);1057 files = kcalloc(hdr.count, sizeof(*files), GFP_KERNEL);
1058 if (!group_fds || !groups) {1058 if (!group_fds || !files) {
1059 kfree(group_fds);1059 kfree(group_fds);
1060 kfree(groups);1060 kfree(files);
1061 return -ENOMEM;1061 return -ENOMEM;
1062 }1062 }
1063 1063
1064 if (copy_from_user(group_fds, (void __user *)(arg + minsz),1064 if (copy_from_user(group_fds, (void __user *)(arg + minsz),
1065 hdr.count * sizeof(*group_fds))) {1065 hdr.count * sizeof(*group_fds))) {
1066 kfree(group_fds);1066 kfree(group_fds);
1067 kfree(groups);1067 kfree(files);
1068 return -EFAULT;1068 return -EFAULT;
1069 }1069 }
1070 1070
@@ -1073,22 +1073,22 @@ reset_info_exit: · 2 unmodified lines
1073 * user interface and store the group and iommu ID. This1073 * user interface and store the group and iommu ID. This
1074 * ensures the group is held across the reset.1074 * ensures the group is held across the reset.
1075 */1075 */
1076 for (group_idx = 0; group_idx < hdr.count; group_idx++) {1076 for (file_idx = 0; file_idx < hdr.count; file_idx++) {
1077 struct vfio_group *group;1077 struct file *file = fget(group_fds[file_idx]);
1078 struct fd f = fdget(group_fds[group_idx]);1078
1079 if (!f.file) {1079 if (!file) {
1080 ret = -EBADF;1080 ret = -EBADF;
1081 break;1081 break;
1082 }1082 }
1083 1083
1084 group = vfio_group_get_external_user(f.file);1084 /* Ensure the FD is a vfio group FD.*/
1085 fdput(f);1085 if (!vfio_file_iommu_group(file)) {
1086 if (IS_ERR(group)) {1086 fput(file);
1087 ret = PTR_ERR(group);1087 ret = -EINVAL;
1088 break;1088 break;
1089 }1089 }
1090 1090
1091 groups[group_idx] = group;1091 files[file_idx] = file;
1092 }1092 }
1093 1093
1094 kfree(group_fds);1094 kfree(group_fds);
@@ -1098,15 +1098,15 @@ reset_info_exit: · 3 unmodified lines
1098 goto hot_reset_release;1098 goto hot_reset_release;
1099 1099
1100 info.count = hdr.count;1100 info.count = hdr.count;
1101 info.groups = groups;1101 info.files = files;
1102 1102
1103 ret = vfio_pci_dev_set_hot_reset(vdev->vdev.dev_set, &info);1103 ret = vfio_pci_dev_set_hot_reset(vdev->vdev.dev_set, &info);
1104 1104
1105hot_reset_release:1105hot_reset_release:
1106 for (group_idx--; group_idx >= 0; group_idx--)1106 for (file_idx--; file_idx >= 0; file_idx--)
1107 vfio_group_put_external_user(groups[group_idx]);1107 fput(files[file_idx]);
1108 1108
1109 kfree(groups);1109 kfree(files);
1110 return ret;1110 return ret;
1111 } else if (cmd == VFIO_DEVICE_IOEVENTFD) {1111 } else if (cmd == VFIO_DEVICE_IOEVENTFD) {
1112 struct vfio_device_ioeventfd ioeventfd;1112 struct vfio_device_ioeventfd ioeventfd;
@@ -1972,7 +1972,7 @@ static bool vfio_dev_in_groups(struct vfio_pci_core_device *vdev, · 859 unmodified lines
1972 unsigned int i;1972 unsigned int i;
1973 1973
1974 for (i = 0; i < groups->count; i++)1974 for (i = 0; i < groups->count; i++)
1975 if (groups->groups[i] == vdev->vdev.group)1975 if (vfio_file_has_dev(groups->files[i], &vdev->vdev))
1976 return true;1976 return true;
1977 return false;1977 return false;
1978}1978}
drivers/vfio/vfio.c+18−52
@@ -1633,58 +1633,6 @@ static const struct file_operations vfio_device_fops = { · 1632 unmodified lines
1633 .mmap = vfio_device_fops_mmap,1633 .mmap = vfio_device_fops_mmap,
1634};1634};
1635 1635
1636/*
1637 * External user API, exported by symbols to be linked dynamically.
1638 *
1639 * The protocol includes:
1640 * 1. do normal VFIO init operation:
1641 * - opening a new container;
1642 * - attaching group(s) to it;
1643 * - setting an IOMMU driver for a container.
1644 * When IOMMU is set for a container, all groups in it are
1645 * considered ready to use by an external user.
1646 *
1647 * 2. User space passes a group fd to an external user.
1648 * The external user calls vfio_group_get_external_user()
1649 * to verify that:
1650 * - the group is initialized;
1651 * - IOMMU is set for it.
1652 * If both checks passed, vfio_group_get_external_user()
1653 * increments the container user counter to prevent
1654 * the VFIO group from disposal before KVM exits.
1655 *
1656 * 3. When the external KVM finishes, it calls
1657 * vfio_group_put_external_user() to release the VFIO group.
1658 * This call decrements the container user counter.
1659 */
1660struct vfio_group *vfio_group_get_external_user(struct file *filep)
1661{
1662 struct vfio_group *group = filep->private_data;
1663 int ret;
1664
1665 if (filep->f_op != &vfio_group_fops)
1666 return ERR_PTR(-EINVAL);
1667
1668 ret = vfio_group_add_container_user(group);
1669 if (ret)
1670 return ERR_PTR(ret);
1671
1672 /*
1673 * Since the caller holds the fget on the file group->users must be >= 1
1674 */
1675 vfio_group_get(group);
1676
1677 return group;
1678}
1679EXPORT_SYMBOL_GPL(vfio_group_get_external_user);
1680
1681void vfio_group_put_external_user(struct vfio_group *group)
1682{
1683 vfio_group_try_dissolve_container(group);
1684 vfio_group_put(group);
1685}
1686EXPORT_SYMBOL_GPL(vfio_group_put_external_user);
1687
1688/**1636/**
1689 * vfio_file_iommu_group - Return the struct iommu_group for the vfio group file1637 * vfio_file_iommu_group - Return the struct iommu_group for the vfio group file
1690 * @file: VFIO group file1638 * @file: VFIO group file
@@ -1752,6 +1700,24 @@ void vfio_file_set_kvm(struct file *file, struct kvm *kvm) · 61 unmodified lines
1752}1700}
1753EXPORT_SYMBOL_GPL(vfio_file_set_kvm);1701EXPORT_SYMBOL_GPL(vfio_file_set_kvm);
1754 1702
1703/**
1704 * vfio_file_has_dev - True if the VFIO file is a handle for device
1705 * @file: VFIO file to check
1706 * @device: Device that must be part of the file
1707 *
1708 * Returns true if given file has permission to manipulate the given device.
1709 */
1710bool vfio_file_has_dev(struct file *file, struct vfio_device *device)
1711{
1712 struct vfio_group *group = file->private_data;
1713
1714 if (file->f_op != &vfio_group_fops)
1715 return false;
1716
1717 return group == device->group;
1718}
1719EXPORT_SYMBOL_GPL(vfio_file_has_dev);
1720
1755/*1721/*
1756 * Sub-module support1722 * Sub-module support
1757 */1723 */
include/linux/vfio.h+1−2
@@ -138,11 +138,10 @@ int vfio_mig_get_next_state(struct vfio_device *device, · 137 unmodified lines
138/*138/*
139 * External user API139 * External user API
140 */140 */
141extern struct vfio_group *vfio_group_get_external_user(struct file *filep);
142extern void vfio_group_put_external_user(struct vfio_group *group);
143extern struct iommu_group *vfio_file_iommu_group(struct file *file);141extern struct iommu_group *vfio_file_iommu_group(struct file *file);
144extern bool vfio_file_enforced_coherent(struct file *file);142extern bool vfio_file_enforced_coherent(struct file *file);
145extern void vfio_file_set_kvm(struct file *file, struct kvm *kvm);143extern void vfio_file_set_kvm(struct file *file, struct kvm *kvm);
144extern bool vfio_file_has_dev(struct file *file, struct vfio_device *device);
146 145
147#define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))146#define VFIO_PIN_PAGES_MAX_ENTRIES (PAGE_SIZE/sizeof(unsigned long))
148 147

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t