* [PATCH] iommufd/selftest: Rework TEST_LENGTH to test min_size explicitly
@ 2023-10-15 7:46 Nicolin Chen
2023-10-16 8:48 ` Tian, Kevin
2023-10-16 14:09 ` Jason Gunthorpe
0 siblings, 2 replies; 3+ messages in thread
From: Nicolin Chen @ 2023-10-15 7:46 UTC (permalink / raw)
To: jgg, kevin.tian; +Cc: yi.l.liu, shuah, iommu, linux-kselftest
TEST_LENGTH passing ".size = sizeof(struct _struct) - 1" expects -EINVAL
from "if (ucmd.user_size < op->min_size)" check in iommufd_fops_ioctl().
This has been working when min_size is exactly the size of the structure.
However, if the size of the structure becomes larger than min_size, i.e.
the passing size above is larger than min_size, that min_size sanity no
longer works.
Since the first test in TEST_LENGTH() was to test that min_size sanity
routine, rework it to support a min_size calculation, rather than using
the full size of the structure.
Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
---
Hi Jason/Kevin,
This was a part of the nesting series. Its link in v4:
https://lore.kernel.org/linux-iommu/20230921075138.124099-13-yi.l.liu@intel.com/
I just realized that this should go in prior to the nesting series.
One of the nesting patches changes the IOMMU_HWPT_ALLOC structure,
which would break the cmd_length test without this patch.
Thanks!
Nicolin
tools/testing/selftests/iommu/iommufd.c | 29 ++++++++++++++-----------
1 file changed, 16 insertions(+), 13 deletions(-)
diff --git a/tools/testing/selftests/iommu/iommufd.c b/tools/testing/selftests/iommu/iommufd.c
index c5eca2fee42c..6323153d277b 100644
--- a/tools/testing/selftests/iommu/iommufd.c
+++ b/tools/testing/selftests/iommu/iommufd.c
@@ -86,12 +86,13 @@ TEST_F(iommufd, cmd_fail)
TEST_F(iommufd, cmd_length)
{
-#define TEST_LENGTH(_struct, _ioctl) \
+#define TEST_LENGTH(_struct, _ioctl, _last) \
{ \
+ size_t min_size = offsetofend(struct _struct, _last); \
struct { \
struct _struct cmd; \
uint8_t extra; \
- } cmd = { .cmd = { .size = sizeof(struct _struct) - 1 }, \
+ } cmd = { .cmd = { .size = min_size - 1 }, \
.extra = UINT8_MAX }; \
int old_errno; \
int rc; \
@@ -112,17 +113,19 @@ TEST_F(iommufd, cmd_length)
} \
}
- TEST_LENGTH(iommu_destroy, IOMMU_DESTROY);
- TEST_LENGTH(iommu_hw_info, IOMMU_GET_HW_INFO);
- TEST_LENGTH(iommu_hwpt_alloc, IOMMU_HWPT_ALLOC);
- TEST_LENGTH(iommu_ioas_alloc, IOMMU_IOAS_ALLOC);
- TEST_LENGTH(iommu_ioas_iova_ranges, IOMMU_IOAS_IOVA_RANGES);
- TEST_LENGTH(iommu_ioas_allow_iovas, IOMMU_IOAS_ALLOW_IOVAS);
- TEST_LENGTH(iommu_ioas_map, IOMMU_IOAS_MAP);
- TEST_LENGTH(iommu_ioas_copy, IOMMU_IOAS_COPY);
- TEST_LENGTH(iommu_ioas_unmap, IOMMU_IOAS_UNMAP);
- TEST_LENGTH(iommu_option, IOMMU_OPTION);
- TEST_LENGTH(iommu_vfio_ioas, IOMMU_VFIO_IOAS);
+ TEST_LENGTH(iommu_destroy, IOMMU_DESTROY, id);
+ TEST_LENGTH(iommu_hw_info, IOMMU_GET_HW_INFO, __reserved);
+ TEST_LENGTH(iommu_hwpt_alloc, IOMMU_HWPT_ALLOC, __reserved);
+ TEST_LENGTH(iommu_ioas_alloc, IOMMU_IOAS_ALLOC, out_ioas_id);
+ TEST_LENGTH(iommu_ioas_iova_ranges, IOMMU_IOAS_IOVA_RANGES,
+ out_iova_alignment);
+ TEST_LENGTH(iommu_ioas_allow_iovas, IOMMU_IOAS_ALLOW_IOVAS,
+ allowed_iovas);
+ TEST_LENGTH(iommu_ioas_map, IOMMU_IOAS_MAP, iova);
+ TEST_LENGTH(iommu_ioas_copy, IOMMU_IOAS_COPY, src_iova);
+ TEST_LENGTH(iommu_ioas_unmap, IOMMU_IOAS_UNMAP, length);
+ TEST_LENGTH(iommu_option, IOMMU_OPTION, val64);
+ TEST_LENGTH(iommu_vfio_ioas, IOMMU_VFIO_IOAS, __reserved);
#undef TEST_LENGTH
}
--
2.42.0
^ permalink raw reply related [flat|nested] 3+ messages in thread* RE: [PATCH] iommufd/selftest: Rework TEST_LENGTH to test min_size explicitly
2023-10-15 7:46 [PATCH] iommufd/selftest: Rework TEST_LENGTH to test min_size explicitly Nicolin Chen
@ 2023-10-16 8:48 ` Tian, Kevin
2023-10-16 14:09 ` Jason Gunthorpe
1 sibling, 0 replies; 3+ messages in thread
From: Tian, Kevin @ 2023-10-16 8:48 UTC (permalink / raw)
To: Nicolin Chen, jgg@nvidia.com
Cc: Liu, Yi L, shuah@kernel.org, iommu@lists.linux.dev,
linux-kselftest@vger.kernel.org
> From: Nicolin Chen <nicolinc@nvidia.com>
> Sent: Sunday, October 15, 2023 3:47 PM
>
> TEST_LENGTH passing ".size = sizeof(struct _struct) - 1" expects -EINVAL
> from "if (ucmd.user_size < op->min_size)" check in iommufd_fops_ioctl().
> This has been working when min_size is exactly the size of the structure.
>
> However, if the size of the structure becomes larger than min_size, i.e.
> the passing size above is larger than min_size, that min_size sanity no
> longer works.
>
> Since the first test in TEST_LENGTH() was to test that min_size sanity
> routine, rework it to support a min_size calculation, rather than using
> the full size of the structure.
>
> Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
Reviewed-by: Kevin Tian <kevin.tian@intel.com>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] iommufd/selftest: Rework TEST_LENGTH to test min_size explicitly
2023-10-15 7:46 [PATCH] iommufd/selftest: Rework TEST_LENGTH to test min_size explicitly Nicolin Chen
2023-10-16 8:48 ` Tian, Kevin
@ 2023-10-16 14:09 ` Jason Gunthorpe
1 sibling, 0 replies; 3+ messages in thread
From: Jason Gunthorpe @ 2023-10-16 14:09 UTC (permalink / raw)
To: Nicolin Chen; +Cc: kevin.tian, yi.l.liu, shuah, iommu, linux-kselftest
On Sun, Oct 15, 2023 at 12:46:48AM -0700, Nicolin Chen wrote:
> TEST_LENGTH passing ".size = sizeof(struct _struct) - 1" expects -EINVAL
> from "if (ucmd.user_size < op->min_size)" check in iommufd_fops_ioctl().
> This has been working when min_size is exactly the size of the structure.
>
> However, if the size of the structure becomes larger than min_size, i.e.
> the passing size above is larger than min_size, that min_size sanity no
> longer works.
>
> Since the first test in TEST_LENGTH() was to test that min_size sanity
> routine, rework it to support a min_size calculation, rather than using
> the full size of the structure.
>
> Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
> ---
> Hi Jason/Kevin,
>
> This was a part of the nesting series. Its link in v4:
> https://lore.kernel.org/linux-iommu/20230921075138.124099-13-yi.l.liu@intel.com/
>
> I just realized that this should go in prior to the nesting series.
> One of the nesting patches changes the IOMMU_HWPT_ALLOC structure,
> which would break the cmd_length test without this patch.
>
> Thanks!
> Nicolin
>
> tools/testing/selftests/iommu/iommufd.c | 29 ++++++++++++++-----------
> 1 file changed, 16 insertions(+), 13 deletions(-)
Applied to iommufd for-next
Thanks,
Jason
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-10-16 14:09 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-10-15 7:46 [PATCH] iommufd/selftest: Rework TEST_LENGTH to test min_size explicitly Nicolin Chen
2023-10-16 8:48 ` Tian, Kevin
2023-10-16 14:09 ` Jason Gunthorpe
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.