Linux-RDMA Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] uverbs: prevent potential underflow
@ 2019-10-05  5:23 Dan Carpenter
  2019-10-08 19:44 ` Jason Gunthorpe
  0 siblings, 1 reply; 4+ messages in thread
From: Dan Carpenter @ 2019-10-05  5:23 UTC (permalink / raw)
  To: Doug Ledford, Potnuri Bharat Teja, Matan Barak
  Cc: Jason Gunthorpe, Leon Romanovsky, Shamir Rabinovitch,
	Matthew Wilcox, Michael Guralnik, linux-rdma, linux-kernel,
	kernel-janitors

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")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
---
 drivers/infiniband/core/uverbs.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/infiniband/core/uverbs.h b/drivers/infiniband/core/uverbs.h
index 1e5aeb39f774..63f7f7db5902 100644
--- a/drivers/infiniband/core/uverbs.h
+++ b/drivers/infiniband/core/uverbs.h
@@ -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 */
-- 
2.20.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] uverbs: prevent potential underflow
  2019-10-05  5:23 [PATCH] uverbs: prevent potential underflow Dan Carpenter
@ 2019-10-08 19:44 ` Jason Gunthorpe
  2019-10-09  7:35   ` Dan Carpenter
  2019-10-11 13:34   ` [PATCH v2] " Dan Carpenter
  0 siblings, 2 replies; 4+ messages in thread
From: Jason Gunthorpe @ 2019-10-08 19:44 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Doug Ledford, Potnuri Bharat Teja, Matan Barak, Leon Romanovsky,
	Shamir Rabinovitch, Matthew Wilcox, Michael Guralnik, linux-rdma,
	linux-kernel, kernel-janitors

On Sat, Oct 05, 2019 at 08:23:37AM +0300, Dan Carpenter wrote:
> 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")
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> ---
>  drivers/infiniband/core/uverbs.h | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)

 
> diff --git a/drivers/infiniband/core/uverbs.h b/drivers/infiniband/core/uverbs.h
> index 1e5aeb39f774..63f7f7db5902 100644
> --- a/drivers/infiniband/core/uverbs.h
> +++ b/drivers/infiniband/core/uverbs.h
> @@ -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

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] uverbs: prevent potential underflow
  2019-10-08 19:44 ` Jason Gunthorpe
@ 2019-10-09  7:35   ` Dan Carpenter
  2019-10-11 13:34   ` [PATCH v2] " Dan Carpenter
  1 sibling, 0 replies; 4+ messages in thread
From: Dan Carpenter @ 2019-10-09  7:35 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Doug Ledford, Potnuri Bharat Teja, Matan Barak, Leon Romanovsky,
	Shamir Rabinovitch, Matthew Wilcox, Michael Guralnik, linux-rdma,
	linux-kernel, kernel-janitors

On Tue, Oct 08, 2019 at 04:44:25PM -0300, Jason Gunthorpe wrote:
> On Sat, Oct 05, 2019 at 08:23:37AM +0300, Dan Carpenter wrote:
> > 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")
> > Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> > ---
> >  drivers/infiniband/core/uverbs.h | 2 +-
> >  1 file changed, 1 insertion(+), 1 deletion(-)
> 
>  
> > diff --git a/drivers/infiniband/core/uverbs.h b/drivers/infiniband/core/uverbs.h
> > index 1e5aeb39f774..63f7f7db5902 100644
> > --- a/drivers/infiniband/core/uverbs.h
> > +++ b/drivers/infiniband/core/uverbs.h
> > @@ -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??

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [PATCH v2] uverbs: prevent potential underflow
  2019-10-08 19:44 ` Jason Gunthorpe
  2019-10-09  7:35   ` Dan Carpenter
@ 2019-10-11 13:34   ` " Dan Carpenter
  1 sibling, 0 replies; 4+ messages in thread
From: Dan Carpenter @ 2019-10-11 13:34 UTC (permalink / raw)
  To: Doug Ledford, Matan Barak
  Cc: Jason Gunthorpe, Yishai Hadas, Leon Romanovsky,
	Shamir Rabinovitch, Michael Guralnik, Matthew Wilcox, linux-rdma,
	kernel-janitors

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")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
---
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(-)

diff --git a/drivers/infiniband/core/uverbs.h b/drivers/infiniband/core/uverbs.h
index 1e5aeb39f774..63f7f7db5902 100644
--- a/drivers/infiniband/core/uverbs.h
+++ b/drivers/infiniband/core/uverbs.h
@@ -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 */
diff --git a/include/rdma/ib_verbs.h b/include/rdma/ib_verbs.h
index 6a47ba85c54c..e7e733add99f 100644
--- a/include/rdma/ib_verbs.h
+++ b/include/rdma/ib_verbs.h
@@ -366,7 +366,7 @@ struct ib_tm_caps {
 
 struct ib_cq_init_attr {
 	unsigned int	cqe;
-	int		comp_vector;
+	u32		comp_vector;
 	u32		flags;
 };
 
-- 
2.20.1


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-10-05  5:23 [PATCH] uverbs: prevent potential underflow Dan Carpenter
2019-10-08 19:44 ` Jason Gunthorpe
2019-10-09  7:35   ` Dan Carpenter
2019-10-11 13:34   ` [PATCH v2] " Dan Carpenter

Linux-RDMA Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-rdma/0 linux-rdma/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-rdma linux-rdma/ https://lore.kernel.org/linux-rdma \
		linux-rdma@vger.kernel.org linux-rdma@archiver.kernel.org
	public-inbox-index linux-rdma

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-rdma


AGPL code for this site: git clone https://public-inbox.org/ public-inbox