vfio: Fix ksize arg while copying user struct in vfio_df_ioctl_bind_iommufd()
PATCH 1/1 vfio: Fix ksize arg while copying user struct in vfio_df_ioctl_bind_iommufd()
For the cases where user includes a non-zero value in 'token_uuid_ptr' field of 'struct vfio_device_bind_iommufd', the copy_struct_from_user() in vfio_df_ioctl_bind_iommufd() fails with -E2BIG. For the 'minsz' passed, copy_struct_from_user() expects the newly introduced field to be zero-ed, which would be incorrect in this case.
Fix this by passing the actual size of the kernel struct. If working with a newer userspace, copy_struct_from_user() would copy the 'token_uuid_ptr' field, and if working with an old userspace, it would zero out this field, thus still retaining backward compatibility.
--- drivers/vfio/device_cdev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/vfio/device_cdev.c +1 -1
@@ -99,7 +99,7 @@ long vfio_df_ioctl_bind_iommufd(struct vfio_device_file *df,return ret;if (user_size < minsz)return -EINVAL ; - ret =copy_struct_from_user (&bind, minsz, arg, user_size); + ret =copy_struct_from_user (&bind,sizeof (bind), arg, user_size);if (ret)return ret;
base-commit: 3a8660878839faadb4f1a6dd72c3179c1df56787
Signature · 1 line
2.51.1.930.gacf6e81ea2-goog
Quoted 11 lines · Raghavendra Rao Ananta
For the cases where user includes a non-zero value in '
token_uuid_ptr' field of 'structvfio_device_bind_iommufd', thecopy_struct_from_user()invfio_df_ioctl_bind_iommufd()fails with-E2BIG. For the 'minsz' passed,copy_struct_from_user()expects the newly introduced field to be zero-ed, which would be incorrect in this case.Fix this by passing the actual size of the kernel struct. If working with a newer userspace,
copy_struct_from_user()would copy the 'token_uuid_ptr' field, and if working with an old userspace, it would zero out this field, thus still retaining backward compatibility.
Quoted 5 lines
Fixes 86624ba3b522 ("vfio/pci: Do vf_token checks for VFIO_DEVICE_BIND_IOMMUFD")Signed-off-by Raghavendra Rao Ananta <[email protected]>--- drivers/vfio/device_cdev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Though I feel this was copied from some other spot in vfio so I wonder if we have a larger set of things that are a little off..
Jason
Quoted 19 lines · Jason Gunthorpe
Quoted 11 lines · Raghavendra Rao Ananta
For the cases where user includes a non-zero value in '
token_uuid_ptr' field of 'structvfio_device_bind_iommufd', thecopy_struct_from_user()invfio_df_ioctl_bind_iommufd()fails with-E2BIG. For the 'minsz' passed,copy_struct_from_user()expects the newly introduced field to be zero-ed, which would be incorrect in this case.Fix this by passing the actual size of the kernel struct. If working with a newer userspace,
copy_struct_from_user()would copy the 'token_uuid_ptr' field, and if working with an old userspace, it would zero out this field, thus still retaining backward compatibility.Quoted 5 lines
Fixes 86624ba3b522 ("vfio/pci: Do vf_token checks for VFIO_DEVICE_BIND_IOMMUFD")Signed-off-by Raghavendra Rao Ananta <[email protected]>--- drivers/vfio/device_cdev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
Quoted 6 lines
Reviewed-by Jason Gunthorpe <[email protected]>Though I feel this was copied from some other spot in vfio so I wonder if we have a larger set of things that are a little off..
I could only find vfio_df_ioctl_bind_iommufd() in vfio referencing copy_struct_from_user(). The other closest would be in drivers/iommu/iommufd/main.c::iommufd_fops_ioctl(), which seems to be doing the right thing.
Thank you.
Raghavendra
liulongfang
maintainerQuoted 6 lines · Raghavendra Rao Ananta
For the cases where user includes a non-zero value in '
token_uuid_ptr' field of 'structvfio_device_bind_iommufd', thecopy_struct_from_user()invfio_df_ioctl_bind_iommufd()fails with-E2BIG. For the 'minsz' passed,copy_struct_from_user()expects the newly introduced field to be zero-ed, which would be incorrect in this case.
Quoted 6 lines
Fix this by passing the actual size of the kernel struct. If working with a newer userspace,
copy_struct_from_user()would copy the 'token_uuid_ptr' field, and if working with an old userspace, it would zero out this field, thus still retaining backward compatibility.Fixes 86624ba3b522 ("vfio/pci: Do vf_token checks for VFIO_DEVICE_BIND_IOMMUFD")
Hi Ananta,
This patch also has another bug: in the hisi_acc_vfio_pci.c driver, It have two "struct vfio_device_ops" Only one of them, "hisi_acc_vfio_pci_ops" has match_token_uuid added,
while the other one, "hisi_acc_vfio_pci_migrn_ops", is missing it.
This will cause a QEMU crash (call trace) when QEMU tries to start the device.
Could you please help include this fix in your patchset as well?
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +1 -0
@@ -1637,6 +1637,7 @@ static const struct vfio_device_ops hisi_acc_vfio_pci_migrn_ops = { .mmap = hisi_acc_vfio_pci_mmap, .request = vfio_pci_core_request, .match = vfio_pci_core_match, + .match_token_uuid = vfio_pci_core_match_token_uuid, .bind_iommufd = vfio_iommufd_physical_bind, .unbind_iommufd = vfio_iommufd_physical_unbind, .attach_ioas = vfio_iommufd_physical_attach_ioas,
Thanks.
Longfang.
Quoted 21 lines
Signed-off-by Raghavendra Rao Ananta <[email protected]>--- drivers/vfio/device_cdev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)drivers/vfio/device_cdev.c +1
-1@@ -99,7 +99,7 @@ long vfio_df_ioctl_bind_iommufd(struct vfio_device_file *df,return ret;if (user_size < minsz)return -EINVAL ; - ret =copy_struct_from_user (&bind, minsz, arg, user_size); + ret =copy_struct_from_user (&bind,sizeof (bind), arg, user_size);if (ret)return ret;base-commit:
3a8660878839faadb4f1a6dd72c3179c1df56787
Quoted 28 lines · liulongfang
Quoted 6 lines · Raghavendra Rao Ananta
For the cases where user includes a non-zero value in '
token_uuid_ptr' field of 'structvfio_device_bind_iommufd', thecopy_struct_from_user()invfio_df_ioctl_bind_iommufd()fails with-E2BIG. For the 'minsz' passed,copy_struct_from_user()expects the newly introduced field to be zero-ed, which would be incorrect in this case.Quoted 6 lines
Fix this by passing the actual size of the kernel struct. If working with a newer userspace,
copy_struct_from_user()would copy the 'token_uuid_ptr' field, and if working with an old userspace, it would zero out this field, thus still retaining backward compatibility.Fixes 86624ba3b522 ("vfio/pci: Do vf_token checks for VFIO_DEVICE_BIND_IOMMUFD")Hi Ananta,
This patch also has another bug: in the
hisi_acc_vfio_pci.cdriver, It have two "structvfio_device_ops" Only one of them, "hisi_acc_vfio_pci_ops" hasmatch_token_uuidadded,
while the other one, "hisi_acc_vfio_pci_migrn_ops", is missing it.
This will cause a QEMU crash (call trace) when QEMU tries to start the device.Could you please help include this fix in your patchset as well?
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c static const struct vfio_device_ops hisi_acc_vfio_pci_migrn_ops = { · line 1643
Quoted 7 lines
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +1
-0@@ -1637,6 +1637,7 @@ static const struct vfio_device_ops hisi_acc_vfio_pci_migrn_ops = { .mmap = hisi_acc_vfio_pci_mmap, .request = vfio_pci_core_request, .match = vfio_pci_core_match, + .match_token_uuid = vfio_pci_core_match_token_uuid, .bind_iommufd = vfio_iommufd_physical_bind, .unbind_iommufd = vfio_iommufd_physical_unbind, .attach_ioas = vfio_iommufd_physical_attach_ioas,
Sent as a separate patch in v2:
https://lore.kernel.org/all/[email protected]/
(untested).
Thank you.
Raghavendra
liulongfang
maintainerQuoted 25 lines · Raghavendra Rao Ananta
Quoted 24 lines · liulongfang
Quoted 6 lines · Raghavendra Rao Ananta
For the cases where user includes a non-zero value in '
token_uuid_ptr' field of 'structvfio_device_bind_iommufd', thecopy_struct_from_user()invfio_df_ioctl_bind_iommufd()fails with-E2BIG. For the 'minsz' passed,copy_struct_from_user()expects the newly introduced field to be zero-ed, which would be incorrect in this case.Quoted 6 lines
Fix this by passing the actual size of the kernel struct. If working with a newer userspace,
copy_struct_from_user()would copy the 'token_uuid_ptr' field, and if working with an old userspace, it would zero out this field, thus still retaining backward compatibility.Fixes 86624ba3b522 ("vfio/pci: Do vf_token checks for VFIO_DEVICE_BIND_IOMMUFD")Hi Ananta,
This patch also has another bug: in the
hisi_acc_vfio_pci.cdriver, It have two "structvfio_device_ops" Only one of them, "hisi_acc_vfio_pci_ops" hasmatch_token_uuidadded,
while the other one, "hisi_acc_vfio_pci_migrn_ops", is missing it.
This will cause a QEMU crash (call trace) when QEMU tries to start the device.Could you please help include this fix in your patchset as well?
Quoted 15 lines
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c static const struct vfio_device_ops hisi_acc_vfio_pci_migrn_ops = { · line 1643
Quoted 11 lines
drivers/vfio/pci/hisilicon/hisi_acc_vfio_pci.c +1
-0@@ -1637,6 +1637,7 @@ static const struct vfio_device_ops hisi_acc_vfio_pci_migrn_ops = { .mmap = hisi_acc_vfio_pci_mmap, .request = vfio_pci_core_request, .match = vfio_pci_core_match, + .match_token_uuid = vfio_pci_core_match_token_uuid, .bind_iommufd = vfio_iommufd_physical_bind, .unbind_iommufd = vfio_iommufd_physical_unbind, .attach_ioas = vfio_iommufd_physical_attach_ioas,Sent as a separate patch in v2:
https://lore.kernel.org/all/[email protected]/
(untested).
I've tested this patch locally, and after applying it, QEMU no longer fails to start and the functionality works as expected.
Thanks.
Longfang.
Quoted 4 lines
Thank you.
Raghavendra
.