Skip to content

Commit 839e692

Browse files
yiliu1765awilliam
authored andcommitted
vfio: Make vfio_df_open() single open for device cdev path
VFIO group has historically allowed multi-open of the device FD. This was made secure because the "open" was executed via an ioctl to the group FD which is itself only single open. However, no known use of multiple device FDs today. It is kind of a strange thing to do because new device FDs can naturally be created via dup(). When we implement the new device uAPI (only used in cdev path) there is no natural way to allow the device itself from being multi-opened in a secure manner. Without the group FD we cannot prove the security context of the opener. Thus, when moving to the new uAPI we block the ability of opening a device multiple times. Given old group path still allows it we store a vfio_group pointer in struct vfio_device_file to differentiate. Reviewed-by: Kevin Tian <[email protected]> Reviewed-by: Jason Gunthorpe <[email protected]> Reviewed-by: Eric Auger <[email protected]> Tested-by: Terrence Xu <[email protected]> Tested-by: Nicolin Chen <[email protected]> Tested-by: Matthew Rosato <[email protected]> Tested-by: Yanting Jiang <[email protected]> Tested-by: Shameer Kolothum <[email protected]> Tested-by: Zhenzhong Duan <[email protected]> Signed-off-by: Yi Liu <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alex Williamson <[email protected]>
1 parent 270bf4c commit 839e692

File tree

3 files changed

+10
-0
lines changed

3 files changed

+10
-0
lines changed

drivers/vfio/group.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,8 @@ static struct file *vfio_device_open_file(struct vfio_device *device)
245245
goto err_out;
246246
}
247247

248+
df->group = device->group;
249+
248250
ret = vfio_df_group_open(df);
249251
if (ret)
250252
goto err_free;

drivers/vfio/vfio.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ struct vfio_container;
1818

1919
struct vfio_device_file {
2020
struct vfio_device *device;
21+
struct vfio_group *group;
2122

2223
u8 access_granted;
2324
spinlock_t kvm_ref_lock; /* protect kvm field */

drivers/vfio/vfio_main.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,13 @@ int vfio_df_open(struct vfio_device_file *df)
492492

493493
lockdep_assert_held(&device->dev_set->lock);
494494

495+
/*
496+
* Only the group path allows the device to be opened multiple
497+
* times. The device cdev path doesn't have a secure way for it.
498+
*/
499+
if (device->open_count != 0 && !df->group)
500+
return -EINVAL;
501+
495502
device->open_count++;
496503
if (device->open_count == 1) {
497504
ret = vfio_df_device_first_open(df);

0 commit comments

Comments
 (0)