Skip to content

Commit f25989c

Browse files
nicolincjgunthorpe
authored andcommitted
iommufd/selftest: Add boundary tests for veventq_depth
Test veventq_depth to cover a memory exhaustion vulnerability. Keep veventq_depth=2 for the existing callers. Link: https://patch.msgid.link/r/acfa370fa4e89e4626f71954bad7ad2bd64cf63b.1779408671.git.nicolinc@nvidia.com Reviewed-by: Jason Gunthorpe <jgg@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 6ebf2eb commit f25989c

3 files changed

Lines changed: 27 additions & 11 deletions

File tree

tools/testing/selftests/iommu/iommufd.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2986,11 +2986,26 @@ TEST_F(iommufd_viommu, vdevice_alloc)
29862986
test_err_mock_domain_replace(ENOENT, self->stdev_id,
29872987
self->nested_hwpt_id);
29882988

2989+
/* Test depth lower and upper bounds (mirrors kernel cap) */
2990+
#define VEVENTQ_MAX_DEPTH (1U << 19)
2991+
test_err_veventq_alloc(EINVAL, viommu_id,
2992+
IOMMU_VEVENTQ_TYPE_SELFTEST, 0, NULL,
2993+
NULL);
2994+
test_err_veventq_alloc(EINVAL, viommu_id,
2995+
IOMMU_VEVENTQ_TYPE_SELFTEST,
2996+
VEVENTQ_MAX_DEPTH + 1, NULL, NULL);
2997+
test_cmd_veventq_alloc(viommu_id, IOMMU_VEVENTQ_TYPE_SELFTEST,
2998+
VEVENTQ_MAX_DEPTH, &veventq_id,
2999+
&veventq_fd);
3000+
close(veventq_fd);
3001+
test_ioctl_destroy(veventq_id);
3002+
29893003
/* Allocate a vEVENTQ with veventq_depth=2 */
29903004
test_cmd_veventq_alloc(viommu_id, IOMMU_VEVENTQ_TYPE_SELFTEST,
2991-
&veventq_id, &veventq_fd);
3005+
2, &veventq_id, &veventq_fd);
29923006
test_err_veventq_alloc(EEXIST, viommu_id,
2993-
IOMMU_VEVENTQ_TYPE_SELFTEST, NULL, NULL);
3007+
IOMMU_VEVENTQ_TYPE_SELFTEST, 2, NULL,
3008+
NULL);
29943009
/* Set vdev_id to 0x99, unset it, and set to 0x88 */
29953010
test_cmd_vdevice_alloc(viommu_id, dev_id, 0x99, &vdev_id);
29963011
test_cmd_mock_domain_replace(self->stdev_id,

tools/testing/selftests/iommu/iommufd_fail_nth.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ TEST_FAIL_NTH(basic_fail_nth, device)
712712
return -1;
713713

714714
if (_test_cmd_veventq_alloc(self->fd, viommu_id,
715-
IOMMU_VEVENTQ_TYPE_SELFTEST, &veventq_id,
715+
IOMMU_VEVENTQ_TYPE_SELFTEST, 2, &veventq_id,
716716
&veventq_fd))
717717
return -1;
718718
close(veventq_fd);

tools/testing/selftests/iommu/iommufd_utils.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,12 +1060,13 @@ static int _test_cmd_hw_queue_alloc(int fd, __u32 viommu_id, __u32 type,
10601060
base_addr, len, out_qid))
10611061

10621062
static int _test_cmd_veventq_alloc(int fd, __u32 viommu_id, __u32 type,
1063-
__u32 *veventq_id, __u32 *veventq_fd)
1063+
__u32 depth, __u32 *veventq_id,
1064+
__u32 *veventq_fd)
10641065
{
10651066
struct iommu_veventq_alloc cmd = {
10661067
.size = sizeof(cmd),
10671068
.type = type,
1068-
.veventq_depth = 2,
1069+
.veventq_depth = depth,
10691070
.viommu_id = viommu_id,
10701071
};
10711072
int ret;
@@ -1080,13 +1081,13 @@ static int _test_cmd_veventq_alloc(int fd, __u32 viommu_id, __u32 type,
10801081
return 0;
10811082
}
10821083

1083-
#define test_cmd_veventq_alloc(viommu_id, type, veventq_id, veventq_fd) \
1084-
ASSERT_EQ(0, _test_cmd_veventq_alloc(self->fd, viommu_id, type, \
1084+
#define test_cmd_veventq_alloc(viommu_id, type, depth, veventq_id, veventq_fd) \
1085+
ASSERT_EQ(0, _test_cmd_veventq_alloc(self->fd, viommu_id, type, depth, \
10851086
veventq_id, veventq_fd))
1086-
#define test_err_veventq_alloc(_errno, viommu_id, type, veventq_id, \
1087-
veventq_fd) \
1088-
EXPECT_ERRNO(_errno, \
1089-
_test_cmd_veventq_alloc(self->fd, viommu_id, type, \
1087+
#define test_err_veventq_alloc(_errno, viommu_id, type, depth, veventq_id, \
1088+
veventq_fd) \
1089+
EXPECT_ERRNO(_errno, \
1090+
_test_cmd_veventq_alloc(self->fd, viommu_id, type, depth, \
10901091
veventq_id, veventq_fd))
10911092

10921093
static int _test_cmd_trigger_vevents(int fd, __u32 dev_id, __u32 nvevents)

0 commit comments

Comments
 (0)