Skip to contentSkip to conversation

ligceph: fix overflow in __decode_pool_names()

PATCH Superseded Xi Wang 3 participants 12 messages Apr 29, 2012 – Jun 6, 2012 Ceph common code (libceph)

lore
Share & tools
lore

Xi Wang

author
#0 lore raw

PATCH 1/3 ligceph: fix overflow in __decode_pool_names()

len' is read from network and thus needs validation. Otherwise a large len' would cause out-of-bounds access via the memcpy() call. In addition, len = 0xffffffff would overflow the kmalloc() size, leading to out-of-bounds write.

This patch adds a check of `len' via ceph_decode_need(). Also use kstrndup rather than kmalloc/memcpy.

---
 net/ceph/osdmap.c |    9 +++------
 1 files changed, 3 insertions(+), 6 deletions(-)
net/ceph/osdmap.c +3 -6
index 29ad46e..f80afc3 100644
@@ -495,15 +495,12 @@ static int __decode_pool_names(void **p, void *end, struct ceph_osdmap *map)
 		ceph_decode_32_safe(p, end, pool, bad);
 		ceph_decode_32_safe(p, end, len, bad);
 		dout("  pool %d len %d\n", pool, len);
+		ceph_decode_need(p, end, len, bad);
 		pi = __lookup_pg_pool(&map->pg_pools, pool);
 		if (pi) {
 			kfree(pi->name);
-			pi->name = kmalloc(len + 1, GFP_NOFS);
-			if (pi->name) {
-				memcpy(pi->name, *p, len);
-				pi->name[len] = '\0';
-				dout("  name is %s\n", pi->name);
-			}
+			pi->name = kstrndup(*p, len, GFP_NOFS);
+			dout("  name is %s\n", pi->name);
 		}
 		*p += len;
 	}
Signature · 1 line
1.7.5.4
author
#1 lore raw

PATCH 2/3 libceph: fix overflow in osdmap_decode()

On 32-bit systems, a large n' would overflow n * sizeof(u32)' and bypass the check ceph_decode_need(p, end, n * sizeof(u32), bad). It would also overflow the subsequent kmalloc() size, leading to out-of-bounds write.

---
 net/ceph/osdmap.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
net/ceph/osdmap.c +3 -0
index f80afc3..774eac6 100644
@@ -670,6 +670,9 @@ struct ceph_osdmap *osdmap_decode(void **p, void *end)
 		ceph_decode_need(p, end, sizeof(u32) + sizeof(u64), bad);
 		ceph_decode_copy(p, &pgid, sizeof(pgid));
 		n = ceph_decode_32(p);
+		err = -EINVAL;
+		if (n > (UINT_MAX - sizeof(*pg)) / sizeof(u32))
+			goto bad;
 		ceph_decode_need(p, end, n * sizeof(u32), bad);
 		err = -ENOMEM;
 		pg = kmalloc(sizeof(*pg) + n*sizeof(u32), GFP_NOFS);
Signature · 1 line
1.7.5.4
Reviewed-by on v1 2/3
#2 lore raw

Re: [PATCH 2/3] libceph: fix overflow in osdmap_decode()

Quoted 3 lines · Xi Wang

On 32-bit systems, a large n' would overflow n * sizeof(u32)' and bypass the check ceph_decode_need(p, end, n * sizeof(u32), bad). It would also overflow the subsequent kmalloc() size, leading to out-of-bounds write.

This looks good.

Your previous patch made me look at something else though. If you can think of a good solution would you be willing to send a patch to implement it? (See below.) I won't hold up committing this for it, but I'd like your opinion.

net/ceph/osdmap.c struct ceph_osdmap *osdmap_decode(void **p, void *end)

Quoted 10 lines
---
 net/ceph/osdmap.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
net/ceph/osdmap.c +0 -0
index f80afc3..774eac6 100644
@@ -670,6 +670,9 @@ struct ceph_osdmap *osdmap_decode(void **p, void *end)

Just above here we see:

        /* pg_temp */
        ceph_decode_32_safe(p, end, len, bad);
        for (i = 0; i < len; i++) {

We haven't validated "len" here either. Looking at it I'm not sure we can do much, but I think we do know a few things should be true:
- (len & (sizeof (u32) - 1)) == 0
- len <= (UINT_MAX / (sizeof (struct ceph_pg) + sizeof (u32))) and further, if it's invalid to have a value for pg->len of zero, then we can instead assert:
- len <= (UINT_MAX / (sizeof (struct ceph_pg) + 2 * sizeof (u32)))

I don't know if it's that important do do a check like this though.

I appreciate these detail-oriented fixes that you've been sending.

Quoted 9 lines
net/ceph/osdmap.c +3 -0
 		ceph_decode_need(p, end, sizeof(u32) + sizeof(u64), bad);
 		ceph_decode_copy(p, &pgid, sizeof(pgid));
 		n = ceph_decode_32(p);
+		err = -EINVAL;
+		if (n > (UINT_MAX - sizeof(*pg)) / sizeof(u32))
+			goto bad;
 		ceph_decode_need(p, end, n * sizeof(u32), bad);
 		err = -ENOMEM;
 		pg = kmalloc(sizeof(*pg) + n*sizeof(u32), GFP_NOFS);
author
#3 lore raw

Re: [PATCH 2/3] libceph: fix overflow in osdmap_decode()

Quoted 14 lines · Alex Elder

Just above here we see:

       /* pg_temp */
       ceph_decode_32_safe(p, end, len, bad);
       for (i = 0; i < len; i++) {

We haven't validated "len" here either. Looking at it I'm not sure we can do much, but I think we do know a few things should be true:
- (len & (sizeof (u32) - 1)) == 0
- len <= (UINT_MAX / (sizeof (struct ceph_pg) + sizeof (u32))) and further, if it's invalid to have a value for pg->len of zero, then we can instead assert:
- len <= (UINT_MAX / (sizeof (struct ceph_pg) + 2 * sizeof (u32)))

Quoted 1 line

I don't know if it's that important do do a check like this though.

I don't see any overflow issue here. Are you worried about the loop running for a while given a large n? How about this check?

	/* pg_temp */
	ceph_decode_32_safe(p, end, len, bad);

+ if (len > UINT_MAX / (sizeof(u32) + sizeof(u64)))
+ goto bad;
+ ceph_decode_need(p, end, len * (sizeof(u32) + sizeof(u64)), bad);

- xi

#4 lore raw

Re: [PATCH 2/3] libceph: fix overflow in osdmap_decode()

Quoted 15 lines · Xi Wang
Quoted 14 lines · Alex Elder

Just above here we see:

       /* pg_temp */
       ceph_decode_32_safe(p, end, len, bad);
       for (i = 0; i < len; i++) {

We haven't validated "len" here either. Looking at it I'm not sure we can do much, but I think we do know a few things should be true:
- (len & (sizeof (u32) - 1)) == 0
- len <= (UINT_MAX / (sizeof (struct ceph_pg) + sizeof (u32))) and further, if it's invalid to have a value for pg->len of zero, then we can instead assert:
- len <= (UINT_MAX / (sizeof (struct ceph_pg) + 2 * sizeof (u32)))

Quoted 4 lines
Quoted 1 line

I don't know if it's that important do do a check like this though.

I don't see any overflow issue here. Are you worried about the loop running for a while given a large n? How about this check?

Not an overflow check, but a validity check nevertheless.

Review

Quoted 4 lines
	/* pg_temp */
	ceph_decode_32_safe(p, end, len, bad);
(quoted diff) +2 -0
+	if (len > UINT_MAX / (sizeof(u32) + sizeof(u64)))
+		goto bad;

That part is sufficient, though I'd prefer sizeof (ceph_pg) both here and in the line that follows, rather than sizeof (u64).

Review

Quoted 1 line
(quoted diff) +1 -0
+	ceph_decode_need(p, end, len * (sizeof(u32) + sizeof(u64)), bad);

This isn't necessary--I was not looking for overflow, just for some sanity checking on the value that came in from the wire.

It probably won't matter because in time if the value is too large then one of the checks inside the loop might bail out. But catching it as early as possible is always better.

I'm not too concerned about it. I may get around to implement fixes like this myself, but would probably do it comprehensively if I do.

-Alex

author
#5 lore raw

Re: [PATCH 2/3] libceph: fix overflow in osdmap_decode()

Quoted 3 lines · Alex Elder

It probably won't matter because in time if the value is too large then one of the checks inside the loop might bail out. But catching it as early as possible is always better.

Yeah I Agree. :-)

- xi

author
#6 lore raw

PATCH 3/3 libceph: fix overflow in osdmap_apply_incremental()

In reply to Xi Wang

On 32-bit systems, a large pglen' would overflow pglen*sizeof(u32)' and bypass the check ceph_decode_need(p, end, pglen*sizeof(u32), bad). It would also overflow the subsequent kmalloc() size, leading to out-of-bounds write.

---
 net/ceph/osdmap.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
net/ceph/osdmap.c +4 -0
index 774eac6..b1ea6d1 100644
@@ -891,6 +891,10 @@ struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
 
 		if (pglen) {
 			/* insert */
+			if (pglen > (UINT_MAX - sizeof(*pg)) / sizeof(u32)) {
+				err = -EINVAL;
+				goto bad;
+			}
 			ceph_decode_need(p, end, pglen*sizeof(u32), bad);
 			pg = kmalloc(sizeof(*pg) + sizeof(u32)*pglen, GFP_NOFS);
 			if (!pg) {
Signature · 1 line
1.7.5.4
Reviewed-by on v1 3/3
#7 lore raw

Re: [PATCH 3/3] libceph: fix overflow in osdmap_apply_incremental()

Quoted 6 lines · Xi Wang

On 32-bit systems, a large pglen' would overflow pglen*sizeof(u32)' and bypass the check ceph_decode_need(p, end, pglen*sizeof(u32), bad). It would also overflow the subsequent kmalloc() size, leading to out-of-bounds write.

This looks good. I'll wait to hear back on my comments on your earlier patches before committing these.

Quoted 19 lines
---
 net/ceph/osdmap.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
net/ceph/osdmap.c +4 -0
index 774eac6..b1ea6d1 100644
@@ -891,6 +891,10 @@ struct ceph_osdmap *osdmap_apply_incremental(void **p, void *end,
 
 		if (pglen) {
 			/* insert */
+			if (pglen > (UINT_MAX - sizeof(*pg)) / sizeof(u32)) {
+				err = -EINVAL;
+				goto bad;
+			}
 			ceph_decode_need(p, end, pglen*sizeof(u32), bad);
 			pg = kmalloc(sizeof(*pg) + sizeof(u32)*pglen, GFP_NOFS);
 			if (!pg) {
author
#8 lore raw

PATCH v2 1/3 libceph: fix overflow in __decode_pool_names()

In reply to Xi Wang

len' is read from network and thus needs validation. Otherwise a large len' would cause out-of-bounds access via the memcpy() call. In addition, len = 0xffffffff would overflow the kmalloc() size, leading to out-of-bounds write.

This patch adds a check of `len' via ceph_decode_need(). Also use kstrndup rather than kmalloc/memcpy.

---
Subject corrected. Sorry, my bad.

---
 net/ceph/osdmap.c |    9 +++------
 1 files changed, 3 insertions(+), 6 deletions(-)
net/ceph/osdmap.c +3 -6
index 29ad46e..f80afc3 100644
@@ -495,15 +495,12 @@ static int __decode_pool_names(void **p, void *end, struct ceph_osdmap *map)
 		ceph_decode_32_safe(p, end, pool, bad);
 		ceph_decode_32_safe(p, end, len, bad);
 		dout("  pool %d len %d\n", pool, len);
+		ceph_decode_need(p, end, len, bad);
 		pi = __lookup_pg_pool(&map->pg_pools, pool);
 		if (pi) {
 			kfree(pi->name);
-			pi->name = kmalloc(len + 1, GFP_NOFS);
-			if (pi->name) {
-				memcpy(pi->name, *p, len);
-				pi->name[len] = '\0';
-				dout("  name is %s\n", pi->name);
-			}
+			pi->name = kstrndup(*p, len, GFP_NOFS);
+			dout("  name is %s\n", pi->name);
 		}
 		*p += len;
 	}
Signature · 1 line
1.7.5.4
#9 lore raw

Re: [PATCH v2 1/3] libceph: fix overflow in __decode_pool_names()

Quoted 7 lines · Xi Wang

len' is read from network and thus needs validation. Otherwise a large len' would cause out-of-bounds access via the memcpy() call. In addition, len = 0xffffffff would overflow the kmalloc() size, leading to out-of-bounds write.

This patch adds a check of `len' via ceph_decode_need(). Also use kstrndup rather than kmalloc/memcpy.

This looks good, however I'd like to correct one thing, and fix another (both noted below) before committing. Please confirm/ack my suggested change; I'll still credit you with the original patch. Thanks.

-Alex

Quoted 17 lines

---
Subject corrected. Sorry, my bad.

---
 net/ceph/osdmap.c |    9 +++------
 1 files changed, 3 insertions(+), 6 deletions(-)
net/ceph/osdmap.c +1 -0
index 29ad46e..f80afc3 100644
@@ -495,15 +495,12 @@ static int __decode_pool_names(void **p, void *end, struct ceph_osdmap *map)
 		ceph_decode_32_safe(p, end, pool, bad);
 		ceph_decode_32_safe(p, end, len, bad);
 		dout("  pool %d len %d\n", pool, len);
+		ceph_decode_need(p, end, len, bad);
 		pi = __lookup_pg_pool(&map->pg_pools, pool);

net/ceph/osdmap.c static int __decode_pool_names(void **p, void *end, struct ceph_osdmap *map) · line 503

Quoted 10 lines
net/ceph/osdmap.c +2 -6
 		if (pi) {
 			kfree(pi->name);
-			pi->name = kmalloc(len + 1, GFP_NOFS);
-			if (pi->name) {
-				memcpy(pi->name, *p, len);
-				pi->name[len] = '\0';
-				dout("  name is %s\n", pi->name);
-			}
+			pi->name = kstrndup(*p, len, GFP_NOFS);
+			dout("  name is %s\n", pi->name);

Instead:

		if (pi) {
			char *name = kstrndup(*p, len, GFP_NOFS);

			if (!name)
				return -ENOMEM;
			kfree(pi->name);
			pi->name = name;
			dout("  name is %s\n", pi->name);
		}
Quoted 3 lines
net/ceph/osdmap.c +0 -0
 		}
 		*p += len;
 	}
author
#10 lore raw

Re: [PATCH v2 1/3] libceph: fix overflow in __decode_pool_names()

Quoted 22 lines · Alex Elder
Quoted 10 lines
net/ceph/osdmap.c +1 -0
index 29ad46e..f80afc3 100644
@@ -495,15 +495,12 @@ static int __decode_pool_names(void **p, void *end, struct ceph_osdmap *map)
 		ceph_decode_32_safe(p, end, pool, bad);
 		ceph_decode_32_safe(p, end, len, bad);
 		dout("  pool %d len %d\n", pool, len);
+		ceph_decode_need(p, end, len, bad);
 		pi = __lookup_pg_pool(&map->pg_pools, pool);

net/ceph/osdmap.c static int __decode_pool_names(void **p, void *end, struct ceph_osdmap *map) · line 503

Quoted 10 lines
net/ceph/osdmap.c +2 -6
 		if (pi) {
 			kfree(pi->name);
-			pi->name = kmalloc(len + 1, GFP_NOFS);
-			if (pi->name) {
-				memcpy(pi->name, *p, len);
-				pi->name[len] = '\0';
-				dout("  name is %s\n", pi->name);
-			}
+			pi->name = kstrndup(*p, len, GFP_NOFS);
+			dout("  name is %s\n", pi->name);

Instead:

Quoted 9 lines
		if (pi) {
			char *name = kstrndup(*p, len, GFP_NOFS);

			if (!name)
				return -ENOMEM;
			kfree(pi->name);
			pi->name = name;
			dout("  name is %s\n", pi->name);
		}

Looks good to me. Thanks!

Do you want me to send another patch?

- xi

#11 lore raw

Re: [PATCH v2 1/3] libceph: fix overflow in __decode_pool_names()

Quoted 33 lines · Xi Wang
Quoted 22 lines · Alex Elder
Quoted 10 lines
net/ceph/osdmap.c +1 -0
index 29ad46e..f80afc3 100644
@@ -495,15 +495,12 @@ static int __decode_pool_names(void **p, void *end, struct ceph_osdmap *map)
 		ceph_decode_32_safe(p, end, pool, bad);
 		ceph_decode_32_safe(p, end, len, bad);
 		dout("  pool %d len %d\n", pool, len);
+		ceph_decode_need(p, end, len, bad);
 		pi = __lookup_pg_pool(&map->pg_pools, pool);

net/ceph/osdmap.c static int __decode_pool_names(void **p, void *end, struct ceph_osdmap *map) · line 503

Quoted 10 lines
net/ceph/osdmap.c +2 -6
 		if (pi) {
 			kfree(pi->name);
-			pi->name = kmalloc(len + 1, GFP_NOFS);
-			if (pi->name) {
-				memcpy(pi->name, *p, len);
-				pi->name[len] = '\0';
-				dout("  name is %s\n", pi->name);
-			}
+			pi->name = kstrndup(*p, len, GFP_NOFS);
+			dout("  name is %s\n", pi->name);

Instead:

Quoted 9 lines
		if (pi) {
			char *name = kstrndup(*p, len, GFP_NOFS);

			if (!name)
				return -ENOMEM;
			kfree(pi->name);
			pi->name = name;
			dout("  name is %s\n", pi->name);
		}
Quoted 3 lines

Looks good to me. Thanks!

Do you want me to send another patch?

Nope. It's already done, I just wasn't going to commit it without hearing back from you.

Thanks.

-Alex