Skip to contentSkip to conversation

RDS: IB: NULL dereference on error in rds_ib_alloc_frmr()

PATCH In review Dan Carpenter 5 participants 7 messages Jun 14, 2017 1 message not in the archive

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

PATCH 1/1 RDS: IB: NULL dereference on error in rds_ib_alloc_frmr()

We accidentally return ERR_PTR(0) if ib_alloc_mr() fails. The caller is expecting error pointers so it results in a NULL dereference.

Fixes 1659185fb4d0 ("RDS: IB: Support Fastreg MR (FRMR) memory registration mode")
net/rds/ib_frmr.c +2 -1
index 48332a6ed738..74a66cc162ed 100644
@@ -38,7 +38,7 @@ static struct rds_ib_mr *rds_ib_alloc_frmr(struct rds_ib_device *rds_ibdev,
 	struct rds_ib_mr_pool *pool;
 	struct rds_ib_mr *ibmr = NULL;
 	struct rds_ib_frmr *frmr;
-	int err = 0;
+	int err;
 
 	if (npages <= RDS_MR_8K_MSG_SIZE)
 		pool = rds_ibdev->mr_8k_pool;
@@ -61,6 +61,7 @@ static struct rds_ib_mr *rds_ib_alloc_frmr(struct rds_ib_device *rds_ibdev,
 			 pool->fmr_attr.max_pages);
 	if (IS_ERR(frmr->mr)) {
 		pr_warn("RDS/IB: %s failed to allocate MR", __func__);
+		err = -ENOMEM;
 		goto out_no_cigar;
 	}
 
#2 lore raw
In reply to a message not in the archive
Quoted 11 lines · Dan Carpenter

We accidentally return ERR_PTR(0) if ib_alloc_mr() fails. The caller is expecting error pointers so it results in a NULL dereference.

Fixes 1659185fb4d0 ("RDS: IB: Support Fastreg MR (FRMR) memory registration mode")
net/rds/ib_frmr.c +0 -0
index 48332a6ed738..74a66cc162ed 100644

net/rds/ib_frmr.c static struct rds_ib_mr *rds_ib_alloc_frmr(struct rds_ib_device *rds_ibdev, · line 41

Quoted 5 lines
net/rds/ib_frmr.c +1 -1
@@ -38,7 +38,7 @@ static struct rds_ib_mr *rds_ib_alloc_frmr(struct rds_ib_device *rds_ibdev,
 	struct rds_ib_mr_pool *pool;
 	struct rds_ib_mr *ibmr = NULL;
 	struct rds_ib_frmr *frmr;
-	int err = 0;
+	int err;

Can we trust it'll be zero?

Quoted 15 lines
net/rds/ib_frmr.c +1 -1
 	if (npages <= RDS_MR_8K_MSG_SIZE)
 		pool = rds_ibdev->mr_8k_pool;
@@ -61,6 +61,7 @@ static struct rds_ib_mr *rds_ib_alloc_frmr(struct rds_ib_device *rds_ibdev,
 			 pool->fmr_attr.max_pages);
 	if (IS_ERR(frmr->mr)) {
 		pr_warn("RDS/IB: %s failed to allocate MR", __func__);
+		err = -ENOMEM;
 		goto out_no_cigar;
 	}
 
--

To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#3 lore raw
Quoted 7 lines · Yuval Shaia
Quoted 6 lines · Dan Carpenter

We accidentally return ERR_PTR(0) if ib_alloc_mr() fails. The caller is expecting error pointers so it results in a NULL dereference.

Fixes 1659185fb4d0 ("RDS: IB: Support Fastreg MR (FRMR) memory registration mode")
Quoted 12 lines

net/rds/ib_frmr.c static struct rds_ib_mr *rds_ib_alloc_frmr(struct rds_ib_device *rds_ibdev, · line 41

Quoted 10 lines
net/rds/ib_frmr.c +1 -1
index 48332a6ed738..74a66cc162ed 100644
@@ -38,7 +38,7 @@ static struct rds_ib_mr *rds_ib_alloc_frmr(struct rds_ib_device *rds_ibdev,
 	struct rds_ib_mr_pool *pool;
 	struct rds_ib_mr *ibmr = NULL;
 	struct rds_ib_frmr *frmr;
-	int err = 0;
+	int err;

Can we trust it'll be zero?

We don't ever want it to be zero. This way, hopefully, GCC will catch it if we introduce any new bugs where we forget to set it to negative.

regards,
dan carpenter

#5 lore raw
In reply to Yuval Shaia
Quoted 7 lines · Yuval Shaia
Quoted 6 lines · Dan Carpenter

We accidentally return ERR_PTR(0) if ib_alloc_mr() fails. The caller is expecting error pointers so it results in a NULL dereference.

Fixes 1659185fb4d0 ("RDS: IB: Support Fastreg MR (FRMR) memory registration mode")
Quoted 12 lines

net/rds/ib_frmr.c static struct rds_ib_mr *rds_ib_alloc_frmr(struct rds_ib_device *rds_ibdev, · line 41

Quoted 10 lines
net/rds/ib_frmr.c +1 -1
index 48332a6ed738..74a66cc162ed 100644
@@ -38,7 +38,7 @@ static struct rds_ib_mr *rds_ib_alloc_frmr(struct rds_ib_device *rds_ibdev,
 	struct rds_ib_mr_pool *pool;
 	struct rds_ib_mr *ibmr = NULL;
 	struct rds_ib_frmr *frmr;
-	int err = 0;
+	int err;

Can we trust it'll be zero?

No need. With the change below there is always an assignment before the only reference at the end of the function.

julia

Quoted 21 lines
Quoted 13 lines
net/rds/ib_frmr.c +1 -1
 	if (npages <= RDS_MR_8K_MSG_SIZE)
 		pool = rds_ibdev->mr_8k_pool;
@@ -61,6 +61,7 @@ static struct rds_ib_mr *rds_ib_alloc_frmr(struct rds_ib_device *rds_ibdev,
 			 pool->fmr_attr.max_pages);
 	if (IS_ERR(frmr->mr)) {
 		pr_warn("RDS/IB: %s failed to allocate MR", __func__);
+		err = -ENOMEM;
 		goto out_no_cigar;
 	}
 
--
Quoted 3 lines

To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
To unsubscribe from this list: send the line "unsubscribe kernel-janitors" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Reviewed-by
#4 lore raw
In reply to Dan Carpenter
Quoted 21 lines · Dan Carpenter
Quoted 7 lines · Yuval Shaia
Quoted 6 lines · Dan Carpenter

We accidentally return ERR_PTR(0) if ib_alloc_mr() fails. The caller is expecting error pointers so it results in a NULL dereference.

Fixes 1659185fb4d0 ("RDS: IB: Support Fastreg MR (FRMR) memory registration mode")
Quoted 12 lines

net/rds/ib_frmr.c static struct rds_ib_mr *rds_ib_alloc_frmr(struct rds_ib_device *rds_ibdev, · line 41

Quoted 10 lines
net/rds/ib_frmr.c +1 -1
index 48332a6ed738..74a66cc162ed 100644
@@ -38,7 +38,7 @@ static struct rds_ib_mr *rds_ib_alloc_frmr(struct rds_ib_device *rds_ibdev,
 	struct rds_ib_mr_pool *pool;
 	struct rds_ib_mr *ibmr = NULL;
 	struct rds_ib_frmr *frmr;
-	int err = 0;
+	int err;

Can we trust it'll be zero?

Quoted 2 lines

We don't ever want it to be zero. This way, hopefully, GCC will catch it if we introduce any new bugs where we forget to set it to negative.

I see your point, in a "good" case we just return the ptr.

Quoted 4 lines

regards,
dan carpenter

Acked-by on v1 1/1
#6 lore raw
In reply to Dan Carpenter
Quoted 6 lines · Dan Carpenter

We accidentally return ERR_PTR(0) if ib_alloc_mr() fails. The caller is expecting error pointers so it results in a NULL dereference.

Fixes 1659185fb4d0 ("RDS: IB: Support Fastreg MR (FRMR) memory registration mode")

Thanks Dan for fix.

You haven't copied netdev. Can you please resend the patch with my ack on netdev so that Dave can pick it up.

Reviewed-by on v1 1/1
#7 lore raw
In reply to Dan Carpenter
Quoted 27 lines · Dan Carpenter

We accidentally return ERR_PTR(0) if ib_alloc_mr() fails. The caller is expecting error pointers so it results in a NULL dereference.

Fixes 1659185fb4d0 ("RDS: IB: Support Fastreg MR (FRMR) memory registration mode")
net/rds/ib_frmr.c +2 -1
index 48332a6ed738..74a66cc162ed 100644
@@ -38,7 +38,7 @@ static struct rds_ib_mr *rds_ib_alloc_frmr(struct rds_ib_device *rds_ibdev,
  	struct rds_ib_mr_pool *pool;
  	struct rds_ib_mr *ibmr = NULL;
  	struct rds_ib_frmr *frmr;
-	int err = 0;
+	int err;
 
  	if (npages <= RDS_MR_8K_MSG_SIZE)
  		pool = rds_ibdev->mr_8k_pool;
@@ -61,6 +61,7 @@ static struct rds_ib_mr *rds_ib_alloc_frmr(struct rds_ib_device *rds_ibdev,
  			 pool->fmr_attr.max_pages);
  	if (IS_ERR(frmr->mr)) {
  		pr_warn("RDS/IB: %s failed to allocate MR", __func__);
+		err = -ENOMEM;
  		goto out_no_cigar;
  	}