Skip to contentSkip to conversation

uverbs: prevent potential underflow

PATCH Superseded Dan Carpenter 2 participants 5 messages Oct 5, 2019 – Oct 22, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 uverbs: prevent potential underflow

The issue is in drivers/infiniband/core/uverbs_std_types_cq.c in the UVERBS_HANDLER(UVERBS_METHOD_CQ_CREATE) function. We check that:

	if (attr.comp_vector >= attrs->ufile->device->num_comp_vectors) {

But we don't check that "attr.comp_vector" whether negative. It could potentially lead to an array underflow. My concern would be where cq->vector is used in the create_cq() function from the cxgb4 driver.

Fixes 9ee79fce3642 ("IB/core: Add completion queue (cq) object actions")
---
 drivers/infiniband/core/uverbs.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/infiniband/core/uverbs.h +1 -1
index 1e5aeb39f774..63f7f7db5902 100644
@@ -98,7 +98,7 @@ ib_uverbs_init_udata_buf_or_null(struct ib_udata *udata,
 
 struct ib_uverbs_device {
 	atomic_t				refcount;
-	int					num_comp_vectors;
+	u32					num_comp_vectors;
 	struct completion			comp;
 	struct device				dev;
 	/* First group for device attributes, NULL terminated array */
Signature · 1 line
2.20.1
#1 lore raw
Quoted 21 lines · Dan Carpenter

The issue is in drivers/infiniband/core/uverbs_std_types_cq.c in the UVERBS_HANDLER(UVERBS_METHOD_CQ_CREATE) function. We check that:

	if (attr.comp_vector >= attrs->ufile->device->num_comp_vectors) {

But we don't check that "attr.comp_vector" whether negative. It could potentially lead to an array underflow. My concern would be where cq->vector is used in the create_cq() function from the cxgb4 driver.

Fixes 9ee79fce3642 ("IB/core: Add completion queue (cq) object actions")
---
 drivers/infiniband/core/uverbs.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/infiniband/core/uverbs.h +0 -0
index 1e5aeb39f774..63f7f7db5902 100644

drivers/infiniband/core/uverbs.h ib_uverbs_init_udata_buf_or_null(struct ib_udata *udata, · line 104

Quoted 8 lines
drivers/infiniband/core/uverbs.h +1 -1
@@ -98,7 +98,7 @@ ib_uverbs_init_udata_buf_or_null(struct ib_udata *udata,
 
 struct ib_uverbs_device {
 	atomic_t				refcount;
-	int					num_comp_vectors;
+	u32					num_comp_vectors;
 	struct completion			comp;
 	struct device				dev;
 	/* First group for device attributes, NULL terminated array */

I would have expected you to change struct ib_cq_init_attr ? Or at least both..

This is actually a bug as the type of UVERBS_ATTR_CREATE_CQ_COMP_VECTOR for userspace is u32:

        UVERBS_ATTR_PTR_IN(UVERBS_ATTR_CREATE_CQ_COMP_VECTOR,
                           UVERBS_ATTR_TYPE(u32),
                           UA_MANDATORY),

But we are stuffing it into a int:

ret = uverbs_copy_from(&attr.comp_vector, attrs,
UVERBS_ATTR_CREATE_CQ_COMP_VECTOR);

So very large values will become negative and switching num_comp_vectors to u32 won't help??

Jason

#2 lore raw
Quoted 41 lines · Jason Gunthorpe
Quoted 21 lines · Dan Carpenter

The issue is in drivers/infiniband/core/uverbs_std_types_cq.c in the UVERBS_HANDLER(UVERBS_METHOD_CQ_CREATE) function. We check that:

	if (attr.comp_vector >= attrs->ufile->device->num_comp_vectors) {

But we don't check that "attr.comp_vector" whether negative. It could potentially lead to an array underflow. My concern would be where cq->vector is used in the create_cq() function from the cxgb4 driver.

Fixes 9ee79fce3642 ("IB/core: Add completion queue (cq) object actions")
---
 drivers/infiniband/core/uverbs.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/infiniband/core/uverbs.h +0 -0
index 1e5aeb39f774..63f7f7db5902 100644

drivers/infiniband/core/uverbs.h ib_uverbs_init_udata_buf_or_null(struct ib_udata *udata, · line 104

Quoted 8 lines
drivers/infiniband/core/uverbs.h +1 -1
@@ -98,7 +98,7 @@ ib_uverbs_init_udata_buf_or_null(struct ib_udata *udata,
 
 struct ib_uverbs_device {
 	atomic_t				refcount;
-	int					num_comp_vectors;
+	u32					num_comp_vectors;
 	struct completion			comp;
 	struct device				dev;
 	/* First group for device attributes, NULL terminated array */

I would have expected you to change struct ib_cq_init_attr ? Or at least both..

This is actually a bug as the type of UVERBS_ATTR_CREATE_CQ_COMP_VECTOR for userspace is u32:

        UVERBS_ATTR_PTR_IN(UVERBS_ATTR_CREATE_CQ_COMP_VECTOR,
                           UVERBS_ATTR_TYPE(u32),
                           UA_MANDATORY),
Quoted 7 lines

But we are stuffing it into a int:

ret = uverbs_copy_from(&attr.comp_vector, attrs,
UVERBS_ATTR_CREATE_CQ_COMP_VECTOR);

So very large values will become negative and switching num_comp_vectors to u32 won't help??

Yeah. You're right. I should have changed both. I'm not sure what I was thinking.

My patch does fix the bug because of type promotion, but we should change both to u32.

regards,
dan carpenter

#3 lore raw

PATCH v2 1/1 uverbs: prevent potential underflow

In reply to Jason Gunthorpe

The issue is in drivers/infiniband/core/uverbs_std_types_cq.c in the UVERBS_HANDLER(UVERBS_METHOD_CQ_CREATE) function. We check that:

        if (attr.comp_vector >= attrs->ufile->device->num_comp_vectors) {

But we don't check that "attr.comp_vector" whether negative. It could potentially lead to an array underflow. My concern would be where cq->vector is used in the create_cq() function from the cxgb4 driver.

And really "attr.comp_vector" is appears as a u32 to user space so that's the right type to use.

Fixes 9ee79fce3642 ("IB/core: Add completion queue (cq) object actions")

---
v2: Change both types to u32

 drivers/infiniband/core/uverbs.h | 2 +-
 include/rdma/ib_verbs.h          | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
drivers/infiniband/core/uverbs.h +1 -1
index 1e5aeb39f774..63f7f7db5902 100644
@@ -98,7 +98,7 @@ ib_uverbs_init_udata_buf_or_null(struct ib_udata *udata,
 
 struct ib_uverbs_device {
 	atomic_t				refcount;
-	int					num_comp_vectors;
+	u32					num_comp_vectors;
 	struct completion			comp;
 	struct device				dev;
 	/* First group for device attributes, NULL terminated array */
include/rdma/ib_verbs.h +1 -1
index 6a47ba85c54c..e7e733add99f 100644
@@ -366,7 +366,7 @@ struct ib_tm_caps {
 
 struct ib_cq_init_attr {
 	unsigned int	cqe;
-	int		comp_vector;
+	u32		comp_vector;
 	u32		flags;
 };
 
Signature · 1 line
2.20.1
#4 lore raw
Quoted 12 lines · Dan Carpenter

The issue is in drivers/infiniband/core/uverbs_std_types_cq.c in the UVERBS_HANDLER(UVERBS_METHOD_CQ_CREATE) function. We check that:

        if (attr.comp_vector >= attrs->ufile->device->num_comp_vectors) {

But we don't check that "attr.comp_vector" whether negative. It could potentially lead to an array underflow. My concern would be where cq->vector is used in the create_cq() function from the cxgb4 driver.

And really "attr.comp_vector" is appears as a u32 to user space so that's the right type to use.

Quoted 8 lines
Fixes 9ee79fce3642 ("IB/core: Add completion queue (cq) object actions")

---
v2: Change both types to u32

 drivers/infiniband/core/uverbs.h | 2 +-
 include/rdma/ib_verbs.h          | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)

Applied to for-rc, thanks

Jason