Commit 597b52f6 authored by Ilya Dryomov's avatar Ilya Dryomov Committed by Sage Weil

libceph: fixup error handling in osdmap_decode()

The existing error handling scheme requires resetting err to -EINVAL
prior to calling any ceph_decode_* macro.  This is ugly and fragile,
and there already are a few places where we would return 0 on error,
due to a missing reset.  Fix this by adding a special e_inval label to
be used by all ceph_decode_* macros.
Signed-off-by: default avatarIlya Dryomov <ilya.dryomov@inktank.com>
Reviewed-by: default avatarAlex Elder <elder@linaro.org>
parent a2505d63
...@@ -688,36 +688,37 @@ static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map) ...@@ -688,36 +688,37 @@ static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map)
{ {
u16 version; u16 version;
u32 len, max, i; u32 len, max, i;
int err = -EINVAL;
u32 epoch = 0; u32 epoch = 0;
void *start = *p; void *start = *p;
int err;
struct ceph_pg_pool_info *pi; struct ceph_pg_pool_info *pi;
dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p)); dout("%s %p to %p len %d\n", __func__, *p, end, (int)(end - *p));
ceph_decode_16_safe(p, end, version, bad); ceph_decode_16_safe(p, end, version, e_inval);
if (version > 6) { if (version > 6) {
pr_warning("got unknown v %d > 6 of osdmap\n", version); pr_warning("got unknown v %d > 6 of osdmap\n", version);
goto bad; goto e_inval;
} }
if (version < 6) { if (version < 6) {
pr_warning("got old v %d < 6 of osdmap\n", version); pr_warning("got old v %d < 6 of osdmap\n", version);
goto bad; goto e_inval;
} }
ceph_decode_need(p, end, 2*sizeof(u64)+6*sizeof(u32), bad); ceph_decode_need(p, end, 2*sizeof(u64)+6*sizeof(u32), e_inval);
ceph_decode_copy(p, &map->fsid, sizeof(map->fsid)); ceph_decode_copy(p, &map->fsid, sizeof(map->fsid));
epoch = map->epoch = ceph_decode_32(p); epoch = map->epoch = ceph_decode_32(p);
ceph_decode_copy(p, &map->created, sizeof(map->created)); ceph_decode_copy(p, &map->created, sizeof(map->created));
ceph_decode_copy(p, &map->modified, sizeof(map->modified)); ceph_decode_copy(p, &map->modified, sizeof(map->modified));
ceph_decode_32_safe(p, end, max, bad); ceph_decode_32_safe(p, end, max, e_inval);
while (max--) { while (max--) {
ceph_decode_need(p, end, 8 + 2, bad); ceph_decode_need(p, end, 8 + 2, e_inval);
err = -ENOMEM;
pi = kzalloc(sizeof(*pi), GFP_NOFS); pi = kzalloc(sizeof(*pi), GFP_NOFS);
if (!pi) if (!pi) {
err = -ENOMEM;
goto bad; goto bad;
}
pi->id = ceph_decode_64(p); pi->id = ceph_decode_64(p);
err = __decode_pool(p, end, pi); err = __decode_pool(p, end, pi);
if (err < 0) { if (err < 0) {
...@@ -728,27 +729,25 @@ static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map) ...@@ -728,27 +729,25 @@ static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map)
} }
err = __decode_pool_names(p, end, map); err = __decode_pool_names(p, end, map);
if (err < 0) { if (err)
dout("fail to decode pool names");
goto bad; goto bad;
}
ceph_decode_32_safe(p, end, map->pool_max, bad); ceph_decode_32_safe(p, end, map->pool_max, e_inval);
ceph_decode_32_safe(p, end, map->flags, bad); ceph_decode_32_safe(p, end, map->flags, e_inval);
max = ceph_decode_32(p); max = ceph_decode_32(p);
/* (re)alloc osd arrays */ /* (re)alloc osd arrays */
err = osdmap_set_max_osd(map, max); err = osdmap_set_max_osd(map, max);
if (err < 0) if (err)
goto bad; goto bad;
/* osds */ /* osds */
err = -EINVAL;
ceph_decode_need(p, end, 3*sizeof(u32) + ceph_decode_need(p, end, 3*sizeof(u32) +
map->max_osd*(1 + sizeof(*map->osd_weight) + map->max_osd*(1 + sizeof(*map->osd_weight) +
sizeof(*map->osd_addr)), bad); sizeof(*map->osd_addr)), e_inval);
*p += 4; /* skip length field (should match max) */ *p += 4; /* skip length field (should match max) */
ceph_decode_copy(p, map->osd_state, map->max_osd); ceph_decode_copy(p, map->osd_state, map->max_osd);
...@@ -762,7 +761,7 @@ static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map) ...@@ -762,7 +761,7 @@ static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map)
ceph_decode_addr(&map->osd_addr[i]); ceph_decode_addr(&map->osd_addr[i]);
/* pg_temp */ /* pg_temp */
ceph_decode_32_safe(p, end, len, bad); ceph_decode_32_safe(p, end, len, e_inval);
for (i = 0; i < len; i++) { for (i = 0; i < len; i++) {
int n, j; int n, j;
struct ceph_pg pgid; struct ceph_pg pgid;
...@@ -771,16 +770,16 @@ static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map) ...@@ -771,16 +770,16 @@ static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map)
err = ceph_decode_pgid(p, end, &pgid); err = ceph_decode_pgid(p, end, &pgid);
if (err) if (err)
goto bad; goto bad;
ceph_decode_need(p, end, sizeof(u32), bad); ceph_decode_need(p, end, sizeof(u32), e_inval);
n = ceph_decode_32(p); n = ceph_decode_32(p);
err = -EINVAL;
if (n > (UINT_MAX - sizeof(*pg)) / sizeof(u32)) if (n > (UINT_MAX - sizeof(*pg)) / sizeof(u32))
goto bad; goto e_inval;
ceph_decode_need(p, end, n * sizeof(u32), bad); ceph_decode_need(p, end, n * sizeof(u32), e_inval);
err = -ENOMEM;
pg = kmalloc(sizeof(*pg) + n*sizeof(u32), GFP_NOFS); pg = kmalloc(sizeof(*pg) + n*sizeof(u32), GFP_NOFS);
if (!pg) if (!pg) {
err = -ENOMEM;
goto bad; goto bad;
}
pg->pgid = pgid; pg->pgid = pgid;
pg->len = n; pg->len = n;
for (j = 0; j < n; j++) for (j = 0; j < n; j++)
...@@ -794,10 +793,10 @@ static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map) ...@@ -794,10 +793,10 @@ static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map)
} }
/* crush */ /* crush */
ceph_decode_32_safe(p, end, len, bad); ceph_decode_32_safe(p, end, len, e_inval);
dout("osdmap_decode crush len %d from off 0x%x\n", len, dout("osdmap_decode crush len %d from off 0x%x\n", len,
(int)(*p - start)); (int)(*p - start));
ceph_decode_need(p, end, len, bad); ceph_decode_need(p, end, len, e_inval);
map->crush = crush_decode(*p, end); map->crush = crush_decode(*p, end);
*p += len; *p += len;
if (IS_ERR(map->crush)) { if (IS_ERR(map->crush)) {
...@@ -812,6 +811,8 @@ static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map) ...@@ -812,6 +811,8 @@ static int osdmap_decode(void **p, void *end, struct ceph_osdmap *map)
dout("full osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd); dout("full osdmap epoch %d max_osd %d\n", map->epoch, map->max_osd);
return 0; return 0;
e_inval:
err = -EINVAL;
bad: bad:
pr_err("corrupt full osdmap (%d) epoch %d off %d (%p of %p-%p)\n", pr_err("corrupt full osdmap (%d) epoch %d off %d (%p of %p-%p)\n",
err, epoch, (int)(*p - start), *p, start, end); err, epoch, (int)(*p - start), *p, start, end);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment