Skip to contentSkip to conversation

RDMA/rtrs: Fix some signedness bugs in error handling

PATCH Superseded Dan Carpenter 3 participants 6 messages May 19, 2020

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

PATCH 1/1 RDMA/rtrs: Fix some signedness bugs in error handling

The problem is that "req->sg_cnt" is an unsigned int so if "nr" is negative, it gets type promoted to a high positive value and the condition is false. This patch fixes it by handling negatives separately.

Fixes 6a98d71daea1 ("RDMA/rtrs: client: main functionality")
---
 drivers/infiniband/ulp/rtrs/rtrs-clt.c | 7 +++----
 drivers/infiniband/ulp/rtrs/rtrs-srv.c | 2 +-
 2 files changed, 4 insertions(+), 5 deletions(-)
drivers/infiniband/ulp/rtrs/rtrs-clt.c +3 -4
index 468fdd0d8713..17f99f0962d0 100644
@@ -1047,11 +1047,10 @@ static int rtrs_map_sg_fr(struct rtrs_clt_io_req *req, size_t count)
 
 	/* Align the MR to a 4K page size to match the block virt boundary */
 	nr = ib_map_mr_sg(req->mr, req->sglist, count, NULL, SZ_4K);
-	if (unlikely(nr < req->sg_cnt)) {
-		if (nr < 0)
-			return nr;
+	if (nr < 0)
+		return -EINVAL;
+	if (unlikely(nr < req->sg_cnt))
 		return -EINVAL;
-	}
 	ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
 
 	return nr;
drivers/infiniband/ulp/rtrs/rtrs-srv.c +1 -1
index ba8ab33b94a2..eefd149ce7a4 100644
@@ -649,7 +649,7 @@ static int map_cont_bufs(struct rtrs_srv_sess *sess)
 		}
 		nr = ib_map_mr_sg(mr, sgt->sgl, sgt->nents,
 				  NULL, max_chunk_size);
-		if (nr < sgt->nents) {
+		if (nr < 0 || nr < sgt->nents) {
 			err = nr < 0 ? nr : -EINVAL;
 			goto dereg_mr;
 		}
Signature · 1 line
2.26.2
#1 lore raw
Quoted 7 lines · Dan Carpenter

The problem is that "req->sg_cnt" is an unsigned int so if "nr" is negative, it gets type promoted to a high positive value and the condition is false. This patch fixes it by handling negatives separately.

Fixes 6a98d71daea1 ("RDMA/rtrs: client: main functionality")

Thanks Dan, fix looks correct, just one comment inline.

Quoted 10 lines
---
 drivers/infiniband/ulp/rtrs/rtrs-clt.c | 7 +++----
 drivers/infiniband/ulp/rtrs/rtrs-srv.c | 2 +-
 2 files changed, 4 insertions(+), 5 deletions(-)
drivers/infiniband/ulp/rtrs/rtrs-clt.c +0 -0
index 468fdd0d8713..17f99f0962d0 100644

drivers/infiniband/ulp/rtrs/rtrs-clt.c static int rtrs_map_sg_fr(struct rtrs_clt_io_req *req, size_t count) · line 1051

Quoted 8 lines
drivers/infiniband/ulp/rtrs/rtrs-clt.c +2 -3
@@ -1047,11 +1047,10 @@ static int rtrs_map_sg_fr(struct rtrs_clt_io_req *req, size_t count)
 
        /* Align the MR to a 4K page size to match the block virt boundary */
        nr = ib_map_mr_sg(req->mr, req->sglist, count, NULL, SZ_4K);
-       if (unlikely(nr < req->sg_cnt)) {
-               if (nr < 0)
-                       return nr;
+       if (nr < 0)
+               return -EINVAL;

Why not just return nr here?

Quoted 22 lines
drivers/infiniband/ulp/rtrs/rtrs-clt.c +1 -1
+       if (unlikely(nr < req->sg_cnt))
                return -EINVAL;
-       }
        ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
 
        return nr;
drivers/infiniband/ulp/rtrs/rtrs-srv.c +1 -2
index ba8ab33b94a2..eefd149ce7a4 100644
@@ -649,7 +649,7 @@ static int map_cont_bufs(struct rtrs_srv_sess *sess)
                }
                nr = ib_map_mr_sg(mr, sgt->sgl, sgt->nents,
                                  NULL, max_chunk_size);
-               if (nr < sgt->nents) {
+               if (nr < 0 || nr < sgt->nents) {
                        err = nr < 0 ? nr : -EINVAL;
                        goto dereg_mr;
                }
--

2.26.2

#2 lore raw
Quoted 20 lines · Jinpu Wang
Quoted 7 lines · Dan Carpenter

The problem is that "req->sg_cnt" is an unsigned int so if "nr" is negative, it gets type promoted to a high positive value and the condition is false. This patch fixes it by handling negatives separately.

Fixes 6a98d71daea1 ("RDMA/rtrs: client: main functionality")

Thanks Dan, fix looks correct, just one comment inline.

Quoted 11 lines
---
 drivers/infiniband/ulp/rtrs/rtrs-clt.c | 7 +++----
 drivers/infiniband/ulp/rtrs/rtrs-srv.c | 2 +-
 2 files changed, 4 insertions(+), 5 deletions(-)
drivers/infiniband/ulp/rtrs/rtrs-clt.c +0 -0
index 468fdd0d8713..17f99f0962d0 100644
@@ -1047,11 +1047,10 @@ static int rtrs_map_sg_fr(struct rtrs_clt_io_req *req, size_t count)
 
Quoted 8 lines

drivers/infiniband/ulp/rtrs/rtrs-clt.c static int rtrs_map_sg_fr(struct rtrs_clt_io_req *req, size_t count) · line 1051

Quoted 7 lines
drivers/infiniband/ulp/rtrs/rtrs-clt.c +2 -3
        /* Align the MR to a 4K page size to match the block virt boundary */
        nr = ib_map_mr_sg(req->mr, req->sglist, count, NULL, SZ_4K);
-       if (unlikely(nr < req->sg_cnt)) {
-               if (nr < 0)
-                       return nr;
+       if (nr < 0)
+               return -EINVAL;

Why not just return nr here?

Sorry. I thought I did but I made a typo. Let me resend.

regards,
dan carpenter

#3 lore raw

PATCH v2 1/1 RDMA/rtrs: Fix some signedness bugs in error handling

In reply to Jinpu Wang

The problem is that "req->sg_cnt" is an unsigned int so if "nr" is negative, it gets type promoted to a high positive value and the condition is false. This patch fixes it by handling negatives separately.

Fixes 6a98d71daea1 ("RDMA/rtrs: client: main functionality")

---
v2: propagate the correct error code instead of returning -EINVAL

 drivers/infiniband/ulp/rtrs/rtrs-clt.c | 7 +++----
 drivers/infiniband/ulp/rtrs/rtrs-srv.c | 2 +-
 2 files changed, 4 insertions(+), 5 deletions(-)
drivers/infiniband/ulp/rtrs/rtrs-clt.c +3 -4
index 468fdd0d8713..17f99f0962d0 100644
@@ -1047,11 +1047,10 @@ static int rtrs_map_sg_fr(struct rtrs_clt_io_req *req, size_t count)
 
 	/* Align the MR to a 4K page size to match the block virt boundary */
 	nr = ib_map_mr_sg(req->mr, req->sglist, count, NULL, SZ_4K);
-	if (unlikely(nr < req->sg_cnt)) {
-		if (nr < 0)
-			return nr;
+	if (nr < 0)
+		return nr;
+	if (unlikely(nr < req->sg_cnt))
 		return -EINVAL;
-	}
 	ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
 
 	return nr;
drivers/infiniband/ulp/rtrs/rtrs-srv.c +1 -1
index ba8ab33b94a2..eefd149ce7a4 100644
@@ -649,7 +649,7 @@ static int map_cont_bufs(struct rtrs_srv_sess *sess)
 		}
 		nr = ib_map_mr_sg(mr, sgt->sgl, sgt->nents,
 				  NULL, max_chunk_size);
-		if (nr < sgt->nents) {
+		if (nr < 0 || nr < sgt->nents) {
 			err = nr < 0 ? nr : -EINVAL;
 			goto dereg_mr;
 		}
Signature · 1 line
2.26.2
Reviewed-by on v2 1/1
#4 lore raw
Quoted 7 lines · Dan Carpenter

The problem is that "req->sg_cnt" is an unsigned int so if "nr" is negative, it gets type promoted to a high positive value and the condition is false. This patch fixes it by handling negatives separately.

Fixes 6a98d71daea1 ("RDMA/rtrs: client: main functionality")

Thanks Dan,

Quoted 41 lines

---
v2: propagate the correct error code instead of returning -EINVAL

 drivers/infiniband/ulp/rtrs/rtrs-clt.c | 7 +++----
 drivers/infiniband/ulp/rtrs/rtrs-srv.c | 2 +-
 2 files changed, 4 insertions(+), 5 deletions(-)
drivers/infiniband/ulp/rtrs/rtrs-clt.c +3 -4
index 468fdd0d8713..17f99f0962d0 100644
@@ -1047,11 +1047,10 @@ static int rtrs_map_sg_fr(struct rtrs_clt_io_req *req, size_t count)
 
        /* Align the MR to a 4K page size to match the block virt boundary */
        nr = ib_map_mr_sg(req->mr, req->sglist, count, NULL, SZ_4K);
-       if (unlikely(nr < req->sg_cnt)) {
-               if (nr < 0)
-                       return nr;
+       if (nr < 0)
+               return nr;
+       if (unlikely(nr < req->sg_cnt))
                return -EINVAL;
-       }
        ib_update_fast_reg_key(req->mr, ib_inc_rkey(req->mr->rkey));
 
        return nr;
drivers/infiniband/ulp/rtrs/rtrs-srv.c +1 -2
index ba8ab33b94a2..eefd149ce7a4 100644
@@ -649,7 +649,7 @@ static int map_cont_bufs(struct rtrs_srv_sess *sess)
                }
                nr = ib_map_mr_sg(mr, sgt->sgl, sgt->nents,
                                  NULL, max_chunk_size);
-               if (nr < sgt->nents) {
+               if (nr < 0 || nr < sgt->nents) {
                        err = nr < 0 ? nr : -EINVAL;
                        goto dereg_mr;
                }
--

2.26.2

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

The problem is that "req->sg_cnt" is an unsigned int so if "nr" is negative, it gets type promoted to a high positive value and the condition is false. This patch fixes it by handling negatives separately.

Fixes 6a98d71daea1 ("RDMA/rtrs: client: main functionality")
Quoted 6 lines

---
v2: propagate the correct error code instead of returning -EINVAL

 drivers/infiniband/ulp/rtrs/rtrs-clt.c | 7 +++----
 drivers/infiniband/ulp/rtrs/rtrs-srv.c | 2 +-
 2 files changed, 4 insertions(+), 5 deletions(-)

Applied to for-next, thanks

Jason