iommu: Honor iommufd uapi for zero entry_num args
PATCH 1/1 iommu: Honor iommufd uapi for zero entry_num args
The IOMMU_HWPT_INVALIDATE ioctl uAPI explicitly allows an empty invalidation request array by setting entry_num == 0. The uAPI documentation in include/uapi/linux/iommufd.h mentions:
" An empty invalidation request array by setting @entry_num==0
is allowed, and @entry_len and @data_uptr would be ignored in this case."
While the core iommufd_hwpt_invalidate() handler honors this by skipping its bounds checks, the generic array copy helper iommu_copy_struct_from_full_user_array() incorrectly rejects it by returning -EINVAL, breaking the uAPI.
Fix this by returning 0 instead of -EINVAL when entry_num is 0.
--- include/linux/iommu.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/iommu.h +1 -1
@@ -569,7 +569,7 @@ iommu_copy_struct_from_full_user_array(void *kdst, size_t kdst_entry_size,if (user_array->type != data_type)return -EINVAL ;if (!user_array->entry_num) -return -EINVAL ; +return 0 ;if (likely (user_array->entry_len == kdst_entry_size)) { if (copy_from_user(kdst, user_array->uptr, user_array->entry_num *
Signature · 1 line
2.55.0.966.g6673acef38-goog
Quoted 8 lines · Pranjal Shrivastava
The
IOMMU_HWPT_INVALIDATEioctl uAPI explicitly allows an empty invalidation request array by settingentry_num== 0. The uAPI documentation ininclude/uapi/linux/iommufd.hmentions:" An empty invalidation request array by setting @entry_num==0
is allowed, and @entry_len and @data_uptr would be ignored in this case."
Quoted 6 lines
While the core
iommufd_hwpt_invalidate()handler honors this by skipping its bounds checks, the generic array copy helperiommu_copy_struct_from_full_user_array()incorrectly rejects it by returning-EINVAL, breaking the uAPI.Fix this by returning 0 instead of
-EINVALwhenentry_numis 0.
I think the caller of this API should be aware of that and check the entry_num. I have submitted an SMMU patch doing so:
https://lore.kernel.org/linux-iommu/5223275dbc8ef00af233f3fee01efa09e45f26b5.1788127877.git.nicolinc@nvidia.com/
Not very sure this API should be changed though..
Nicolin
Quoted 14 lines · Nicolin Chen
Quoted 13 lines · Pranjal Shrivastava
The
IOMMU_HWPT_INVALIDATEioctl uAPI explicitly allows an empty invalidation request array by settingentry_num== 0. The uAPI documentation ininclude/uapi/linux/iommufd.hmentions:" An empty invalidation request array by setting @entry_num==0
is allowed, and @entry_len and @data_uptr would be ignored in this case."While the core
iommufd_hwpt_invalidate()handler honors this by skipping its bounds checks, the generic array copy helperiommu_copy_struct_from_full_user_array()incorrectly rejects it by returning-EINVAL, breaking the uAPI.
Quoted 7 lines
Quoted 1 line
Fix this by returning 0 instead of
-EINVALwhenentry_numis 0.I think the caller of this API should be aware of that and check the
entry_num. I have submitted an SMMU patch doing so:
https://lore.kernel.org/linux-iommu/5223275dbc8ef00af233f3fee01efa09e45f26b5.1788127877.git.nicolinc@nvidia.com/Not very sure this API should be changed though..
Ack. Yes, I remember the smmuv3 fix. However, I think other IOMMUs would trip over this while adding support for invalidation. If not this API, should we add a note above the cache_invalidate op to remind them?
Furthermore, users of the item-by-item helper (like intel/nested.c) get entry_num == 0 support naturally because their loop:
for (index = 0; index < array->entry_num; index++)
simply skips execution.
I'm not sure if we should be returning -EINVAL for 0 because nothing's *really* invalid (and we check index >= src_array->entry_num at places where it is). It's like getting -EINVAL from copy_from_user for len=0.
I'm okay either way but it feels wrong to have specific users to check if (entry_num == 0). Would like to discuss this behaviour?
- Praan
Quoted 6 lines · Pranjal Shrivastava
I'm not sure if we should be returning
-EINVALfor 0 because nothing's *really* invalid (and we check index >=src_array->entry_numat places where it is). It's like getting-EINVALfromcopy_from_userfor len=0.I'm okay either way but it feels wrong to have specific users to check if (
entry_num== 0). Would like to discuss this behaviour?
That makes sense, but this should be respun on top of the accepted one and remove the redundant arm check..
Signature · 1 line
Jason