svcrdma: underflow issue in decode_write_list()
Subsystems (5)
PATCH 1/1 svcrdma: underflow issue in decode_write_list()
My static checker marks everything from ntohl() as untrusted and it complains we could have an underflow problem doing:
return (u32 *)&ary->wc_array[nchunks];
Also on 32 bit systems the upper bound check could overflow.
net/sunrpc/xprtrdma/svc_rdma_marshal.c +1 -0
@@ -98,6 +98,7 @@ void svc_rdma_rcl_chunk_counts(struct rpcrdma_read_chunk *ch,*/ static u32 *decode_write_list (u32 *va,u32 *vaend) { +unsigned long start, end;int nchunks; struct rpcrdma_write_array *ary @@ -113,9 +114,12 @@ static u32 *decode_write_list(u32 *va, u32 *vaend)
return NULL;
}
nchunks = ntohl(ary->wc_nchunks);
- if (((unsigned long)&ary->wc_array[0] +
- (sizeof(struct rpcrdma_write_chunk) * nchunks)) >
- (unsigned long)vaend) {
+
+ start = (unsigned long)&ary->wc_array[0];
+ end = (unsigned long)vaend;
+ if (nchunks < 0 ||
+ nchunks > (SIZE_MAX - start) / sizeof(struct rpcrdma_write_chunk) ||
+ (start + (sizeof(struct rpcrdma_write_chunk) * nchunks)) > end) {
dprintk("svcrdma: ary=%p, wc_nchunks=%d, vaend=%p\n",
ary, nchunks, vaend);
return NULL;
@@ -129,6 +133,7 @@ static u32 *decode_write_list(u32 *va, u32 *vaend)
static u32 *decode_reply_array(u32 *va, u32 *vaend)
{
+ unsigned long start, end;
int nchunks;
struct rpcrdma_write_array *ary (struct rpcrdma_write_array *)va;
@@ -143,9 +148,12 @@ static u32 *decode_reply_array(u32 *va, u32 *vaend)
return NULL;
}
nchunks = ntohl(ary->wc_nchunks);
- if (((unsigned long)&ary->wc_array[0] +
- (sizeof(struct rpcrdma_write_chunk) * nchunks)) >
- (unsigned long)vaend) {
+
+ start = (unsigned long)&ary->wc_array[0];
+ end = (unsigned long)vaend;
+ if (nchunks < 0 ||
+ nchunks > (SIZE_MAX - start) / sizeof(struct rpcrdma_write_chunk) ||
+ (start + (sizeof(struct rpcrdma_write_chunk) * nchunks)) > end) {
dprintk("svcrdma: ary=%p, wc_nchunks=%d, vaend=%p\n",
ary, nchunks, vaend);
return NULL;
Am 12.07.2013 08:39, schrieb Dan Carpenter:
Quoted 25 lines
My static checker marks everything from
ntohl()as untrusted and it complains we could have an underflow problem doing:return (u32 *)&ary->wc_array[nchunks];Also on 32 bit systems the upper bound check could overflow.
Signed-off-by Dan Carpenter <[email protected]>net/sunrpc/xprtrdma/svc_rdma_marshal.c +1
-0@@ -98,6 +98,7 @@ void svc_rdma_rcl_chunk_counts(struct rpcrdma_read_chunk *ch,*/ static u32 *decode_write_list (u32 *va,u32 *vaend) { +unsigned long start, end;int nchunks; struct rpcrdma_write_array *ary > @@ -113,9 +114,12 @@ static u32 *decode_write_list(u32 *va, u32 *vaend)net/sunrpc/xprtrdma/svc_rdma_marshal.c +0
-1return NULL ; } nchunks =ntohl (ary->wc_nchunks); - if (((unsigned long)&ary->wc_array[0] +
net/sunrpc/xprtrdma/svc_rdma_marshal.c
Quoted 10 lines
net/sunrpc/xprtrdma/svc_rdma_marshal.c +6
-2- (sizeof(struct rpcrdma_write_chunk) * nchunks)) > - (unsigned long)vaend) { + + start = (unsigned long )&ary->wc_array[0 ]; + end = (unsigned long )vaend; +if (nchunks <0 || + nchunks > (SIZE_MAX - start) /sizeof (struct rpcrdma_write_chunk ) || + (start + (sizeof (struct rpcrdma_write_chunk ) * nchunks)) > end) {dprintk ("svcrdma: ary=%p, wc_nchunks=%d, vaend=%p\n" , ary, nchunks, vaend);
i am struggling to understand what is actually checked here. Perhaps this improves the readability a bit
if ( nchunks < 0 ||
sizeof(struct rpcrdma_write_chunk) * nchunks > (SIZE_MAX - start) || sizeof(struct rpcrdma_write_chunk) * nchunks > (end - start) )
with that rewrite i would say that (SIZE_MAX - start) is strange.
just my 2 cents,
wh
Quoted 29 lines
net/sunrpc/xprtrdma/svc_rdma_marshal.c +7
-4return NULL ;@@ -129,6 +133,7 @@ static u32 *decode_write_list(u32 *va, u32 *vaend)static u32 *decode_reply_array (u32 *va,u32 *vaend) { +unsigned long start, end;int nchunks; struct rpcrdma_write_array *ary > (struct rpcrdma_write_array *)va;@@ -143,9 +148,12 @@ static u32 *decode_reply_array(u32 *va, u32 *vaend)return NULL ; } nchunks =ntohl (ary->wc_nchunks); -if (((unsigned long )&ary->wc_array[0 ] + - (sizeof (struct rpcrdma_write_chunk ) * nchunks)) > - (unsigned long )vaend) { + + start = (unsigned long )&ary->wc_array[0 ]; + end = (unsigned long )vaend; +if (nchunks <0 || + nchunks > (SIZE_MAX - start) /sizeof (struct rpcrdma_write_chunk ) || + (start + (sizeof (struct rpcrdma_write_chunk ) * nchunks)) > end) {dprintk ("svcrdma: ary=%p, wc_nchunks=%d, vaend=%p\n" , ary, nchunks, vaend);return NULL ; --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
Quoted 37 lines · walter harms
Am 12.07.2013 08:39, schrieb Dan Carpenter:
Quoted 34 lines
My static checker marks everything from
ntohl()as untrusted and it complains we could have an underflow problem doing:return (u32 *)&ary->wc_array[nchunks];Also on 32 bit systems the upper bound check could overflow.
Signed-off-by Dan Carpenter <[email protected]>net/sunrpc/xprtrdma/svc_rdma_marshal.c +1
-0@@ -98,6 +98,7 @@ void svc_rdma_rcl_chunk_counts(struct rpcrdma_read_chunk *ch,*/ static u32 *decode_write_list (u32 *va,u32 *vaend) { +unsigned long start, end;int nchunks; struct rpcrdma_write_array *ary > > @@ -113,9 +114,12 @@ static u32 *decode_write_list(u32 *va, u32 *vaend)net/sunrpc/xprtrdma/svc_rdma_marshal.c +6
-3return NULL ; } nchunks =ntohl (ary->wc_nchunks); -if (((unsigned long )&ary->wc_array[0 ] + - (sizeof (struct rpcrdma_write_chunk ) * nchunks)) > - (unsigned long )vaend) { + + start = (unsigned long )&ary->wc_array[0 ]; + end = (unsigned long )vaend; +if (nchunks <0 || + nchunks > (SIZE_MAX - start) /sizeof (struct rpcrdma_write_chunk ) || + (start + (sizeof (struct rpcrdma_write_chunk ) * nchunks)) > end) { dprintk("svcrdma: ary=%p, wc_nchunks=%d, vaend=%p\n",
Quoted 8 lines
Quoted 1 line
ary, nchunks, vaend);
i am struggling to understand what is actually checked here. Perhaps this improves the readability a bit
if ( nchunks < 0 ||
sizeof(struct rpcrdma_write_chunk)* nchunks > (SIZE_MAX- start) ||sizeof(struct rpcrdma_write_chunk)* nchunks > (end - start) )
If the product on the left-hand size overflows, the product could pass all these tests while nchunks is still too large. That's the same problem the original code had.
Committing Dan's version unless someone has something better.
--b.
Quoted 35 lines
with that rewrite i would say that (
SIZE_MAX- start) is strange.just my 2 cents,
whQuoted 29 lines
net/sunrpc/xprtrdma/svc_rdma_marshal.c +7
-4return NULL ;@@ -129,6 +133,7 @@ static u32 *decode_write_list(u32 *va, u32 *vaend)static u32 *decode_reply_array (u32 *va,u32 *vaend) { +unsigned long start, end;int nchunks; struct rpcrdma_write_array *ary > > (struct rpcrdma_write_array *)va;@@ -143,9 +148,12 @@ static u32 *decode_reply_array(u32 *va, u32 *vaend)return NULL ; } nchunks =ntohl (ary->wc_nchunks); -if (((unsigned long )&ary->wc_array[0 ] + - (sizeof (struct rpcrdma_write_chunk ) * nchunks)) > - (unsigned long )vaend) { + + start = (unsigned long )&ary->wc_array[0 ]; + end = (unsigned long )vaend; +if (nchunks <0 || + nchunks > (SIZE_MAX - start) /sizeof (struct rpcrdma_write_chunk ) || + (start + (sizeof (struct rpcrdma_write_chunk ) * nchunks)) > end) {dprintk ("svcrdma: ary=%p, wc_nchunks=%d, vaend=%p\n" , ary, nchunks, vaend);return NULL ; --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
A message that is not in the archive (sent off-list or not yet received): [email protected]
Quoted 4 lines · walter harms
i am struggling to understand what is actually checked here. Perhaps this improves the readability a bit
if ( nchunks < 0 ||sizeof(struct rpcrdma_write_chunk) * nchunks > (SIZE_MAX - start) ||
That doesn't work.
This is one of the few times I get to use algebra in real life. Yay! The problem is you have to
have the nchunks by itself and the trusted bits on the other side. So the multiply becomes a
divide.
There are lots of these checks in the kernel. It's idiomatic.
regards,
dan carpenter