The CQ create and resize paths check the requested size against 1 << log_max_cq_sz. The cap is 8 bits wide, so an untrusted NIC can report 31 or more, which makes the shift undefined. A large limit also lets the rounded up CQ size exceed INT_MAX, and it is then truncated when passed on as an int. Such a size also overflows the CQ buffer size, entries * cqe_size, which create_cq_user() and alloc_cq_frag_buf() compute as an int. A user CQ buffer smaller than the CQ the NIC is given then passes the umem size check, and a kernel CQ gets a zero-sized buffer that init_cq_frag_buf() writes to. Take the limit from MLX5_CAP_GEN_LOGSZ_U64(), round up in 64 bits, and reject sizes above INT_MAX. Compute the buffer size with check_mul_overflow(), as resize_user() already does. Signed-off-by: Yishai Hadas --- drivers/infiniband/hw/mlx5/cq.c | 52 ++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 13 deletions(-) diff --git a/drivers/infiniband/hw/mlx5/cq.c b/drivers/infiniband/hw/mlx5/cq.c index 831bdf238319..7180357f9dd7 100644 --- a/drivers/infiniband/hw/mlx5/cq.c +++ b/drivers/infiniband/hw/mlx5/cq.c @@ -897,10 +897,14 @@ static int alloc_cq_frag_buf(struct mlx5_ib_dev *dev, struct mlx5_frag_buf *frag_buf = &buf->frag_buf; u8 log_wq_stride = 6 + (cqe_size == 128 ? 1 : 0); u8 log_wq_sz = ilog2(cqe_size); + int size; int err; + if (check_mul_overflow(nent, cqe_size, &size)) + return -EINVAL; + err = mlx5_frag_buf_alloc_node(dev->mdev, - nent * cqe_size, + size, frag_buf, dev->mdev->priv.numa_node); if (err) @@ -943,6 +947,7 @@ static int create_cq_user(struct mlx5_ib_dev *dev, struct ib_udata *udata, { struct mlx5_ib_create_cq ucmd; unsigned long page_size; + size_t buf_size; unsigned int page_offset_quantized; __be64 *pas; int ncont; @@ -966,9 +971,13 @@ static int create_cq_user(struct mlx5_ib_dev *dev, struct ib_udata *udata, *cqe_size = ucmd.cqe_size; + if (check_mul_overflow((size_t)entries, (size_t)ucmd.cqe_size, + &buf_size)) + return -EINVAL; + cq->buf.umem = ib_umem_get_cq_buf_or_va(&dev->ib_dev, attrs, ucmd.buf_addr, - entries * ucmd.cqe_size, + buf_size, IB_ACCESS_LOCAL_WRITE); if (IS_ERR(cq->buf.umem)) return PTR_ERR(cq->buf.umem); @@ -1167,6 +1176,15 @@ static void notify_soft_wc_handler(struct work_struct *work) cq->ibcq.comp_handler(&cq->ibcq, cq->ibcq.cq_context); } +/* + * Next power of 2 above entries. It can be 2^32, which doesn't fit the + * unsigned long of roundup_pow_of_two() on 32-bit archs. + */ +static u64 cq_roundup_entries(u64 entries) +{ + return 1ULL << fls64(entries); +} + int mlx5_ib_create_user_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr, struct uverbs_attr_bundle *attrs) @@ -1174,7 +1192,7 @@ int mlx5_ib_create_user_cq(struct ib_cq *ibcq, struct ib_udata *udata = &attrs->driver_udata; struct ib_device *ibdev = ibcq->device; struct mlx5_ib_create_cq_resp uresp = {}; - int entries = attr->cqe; + u64 entries = attr->cqe; int vector = attr->comp_vector; struct mlx5_ib_dev *dev = to_mdev(ibdev); struct mlx5_ib_cq *cq = to_mcq(ibcq); @@ -1186,15 +1204,17 @@ int mlx5_ib_create_user_cq(struct ib_cq *ibcq, int cqe_size; int eqn; int err; + u64 max_cq_entries = MLX5_CAP_GEN_LOGSZ_U64(dev->mdev, log_max_cq_sz); - if (attr->cqe > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz))) + if (attr->cqe > max_cq_entries) return -EINVAL; if (check_cq_create_flags(attr->flags)) return -EOPNOTSUPP; - entries = roundup_pow_of_two(entries + 1); - if (entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz))) + entries = cq_roundup_entries(entries); + /* cq->ibcq.cqe and create_cq_user() take an int */ + if (entries > max_cq_entries || entries > INT_MAX) return -EINVAL; cq->ibcq.cqe = entries - 1; @@ -1262,7 +1282,7 @@ int mlx5_ib_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr, struct uverbs_attr_bundle *attrs) { struct ib_device *ibdev = ibcq->device; - int entries = attr->cqe; + u64 entries = attr->cqe; int vector = attr->comp_vector; struct mlx5_ib_dev *dev = to_mdev(ibdev); struct mlx5_ib_cq *cq = to_mcq(ibcq); @@ -1274,12 +1294,14 @@ int mlx5_ib_create_cq(struct ib_cq *ibcq, const struct ib_cq_init_attr *attr, int cqe_size; int eqn; int err; + u64 max_cq_entries = MLX5_CAP_GEN_LOGSZ_U64(dev->mdev, log_max_cq_sz); - if (attr->cqe > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz))) + if (attr->cqe > max_cq_entries) return -EINVAL; - entries = roundup_pow_of_two(entries + 1); - if (entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz))) + entries = cq_roundup_entries(entries); + /* cq->ibcq.cqe and create_cq_kernel() take an int */ + if (entries > max_cq_entries || entries > INT_MAX) return -EINVAL; cq->ibcq.cqe = entries - 1; @@ -1577,18 +1599,22 @@ int mlx5_ib_resize_cq(struct ib_cq *ibcq, unsigned int entries, int inlen; int cqe_size; unsigned long flags; + u64 entries64 = entries; + u64 max_cq_entries = MLX5_CAP_GEN_LOGSZ_U64(dev->mdev, log_max_cq_sz); if (!MLX5_CAP_GEN(dev->mdev, cq_resize)) { pr_info("Firmware does not support resize CQ\n"); return -ENOSYS; } - if (entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz))) + if (entries64 > max_cq_entries) return -EINVAL; - entries = roundup_pow_of_two(entries + 1); - if (entries > (1 << MLX5_CAP_GEN(dev->mdev, log_max_cq_sz)) + 1) + entries64 = cq_roundup_entries(entries64); + /* resize_user() and resize_kernel() take an int */ + if (entries64 > max_cq_entries + 1 || entries64 > INT_MAX) return -EINVAL; + entries = entries64; if (entries == ibcq->cqe + 1) return 0; -- 2.18.1