Skip to content

Commit 266dcae

Browse files
nicolincjgunthorpe
authored andcommitted
iommufd/selftest: Rework TEST_LENGTH to test min_size explicitly
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. Link: https://lore.kernel.org/r/20231015074648.24185-1-nicolinc@nvidia.com Signed-off-by: Nicolin Chen <nicolinc@nvidia.com> Reviewed-by: Kevin Tian <kevin.tian@intel.com> Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
1 parent c97d1b2 commit 266dcae

1 file changed

Lines changed: 16 additions & 13 deletions

File tree

tools/testing/selftests/iommu/iommufd.c

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,13 @@ TEST_F(iommufd, cmd_fail)
8686

8787
TEST_F(iommufd, cmd_length)
8888
{
89-
#define TEST_LENGTH(_struct, _ioctl) \
89+
#define TEST_LENGTH(_struct, _ioctl, _last) \
9090
{ \
91+
size_t min_size = offsetofend(struct _struct, _last); \
9192
struct { \
9293
struct _struct cmd; \
9394
uint8_t extra; \
94-
} cmd = { .cmd = { .size = sizeof(struct _struct) - 1 }, \
95+
} cmd = { .cmd = { .size = min_size - 1 }, \
9596
.extra = UINT8_MAX }; \
9697
int old_errno; \
9798
int rc; \
@@ -112,17 +113,19 @@ TEST_F(iommufd, cmd_length)
112113
} \
113114
}
114115

115-
TEST_LENGTH(iommu_destroy, IOMMU_DESTROY);
116-
TEST_LENGTH(iommu_hw_info, IOMMU_GET_HW_INFO);
117-
TEST_LENGTH(iommu_hwpt_alloc, IOMMU_HWPT_ALLOC);
118-
TEST_LENGTH(iommu_ioas_alloc, IOMMU_IOAS_ALLOC);
119-
TEST_LENGTH(iommu_ioas_iova_ranges, IOMMU_IOAS_IOVA_RANGES);
120-
TEST_LENGTH(iommu_ioas_allow_iovas, IOMMU_IOAS_ALLOW_IOVAS);
121-
TEST_LENGTH(iommu_ioas_map, IOMMU_IOAS_MAP);
122-
TEST_LENGTH(iommu_ioas_copy, IOMMU_IOAS_COPY);
123-
TEST_LENGTH(iommu_ioas_unmap, IOMMU_IOAS_UNMAP);
124-
TEST_LENGTH(iommu_option, IOMMU_OPTION);
125-
TEST_LENGTH(iommu_vfio_ioas, IOMMU_VFIO_IOAS);
116+
TEST_LENGTH(iommu_destroy, IOMMU_DESTROY, id);
117+
TEST_LENGTH(iommu_hw_info, IOMMU_GET_HW_INFO, __reserved);
118+
TEST_LENGTH(iommu_hwpt_alloc, IOMMU_HWPT_ALLOC, __reserved);
119+
TEST_LENGTH(iommu_ioas_alloc, IOMMU_IOAS_ALLOC, out_ioas_id);
120+
TEST_LENGTH(iommu_ioas_iova_ranges, IOMMU_IOAS_IOVA_RANGES,
121+
out_iova_alignment);
122+
TEST_LENGTH(iommu_ioas_allow_iovas, IOMMU_IOAS_ALLOW_IOVAS,
123+
allowed_iovas);
124+
TEST_LENGTH(iommu_ioas_map, IOMMU_IOAS_MAP, iova);
125+
TEST_LENGTH(iommu_ioas_copy, IOMMU_IOAS_COPY, src_iova);
126+
TEST_LENGTH(iommu_ioas_unmap, IOMMU_IOAS_UNMAP, length);
127+
TEST_LENGTH(iommu_option, IOMMU_OPTION, val64);
128+
TEST_LENGTH(iommu_vfio_ioas, IOMMU_VFIO_IOAS, __reserved);
126129
#undef TEST_LENGTH
127130
}
128131

0 commit comments

Comments
 (0)