Skip to content

Commit 7e71b1e

Browse files
author
Julien Grall
committed
tools/xenstored: Prevent a buffer overflow in dump_state_node_perms()
ASAN reported one issue when Live Updating Xenstored: ================================================================= ==873==ERROR: AddressSanitizer: stack-buffer-overflow on address 0x7ffc194f53e0 at pc 0x555c6b323292 bp 0x7ffc194f5340 sp 0x7ffc194f5338 WRITE of size 1 at 0x7ffc194f53e0 thread T0 #0 0x555c6b323291 in dump_state_node_perms xen/tools/xenstore/xenstored_core.c:2468 #1 0x555c6b32746e in dump_state_special_node xen/tools/xenstore/xenstored_domain.c:1257 #2 0x555c6b32a702 in dump_state_special_nodes xen/tools/xenstore/xenstored_domain.c:1273 xen-project#3 0x555c6b32ddb3 in lu_dump_state xen/tools/xenstore/xenstored_control.c:521 xen-project#4 0x555c6b32e380 in do_lu_start xen/tools/xenstore/xenstored_control.c:660 xen-project#5 0x555c6b31b461 in call_delayed xen/tools/xenstore/xenstored_core.c:278 xen-project#6 0x555c6b32275e in main xen/tools/xenstore/xenstored_core.c:2357 xen-project#7 0x7f95eecf3d09 in __libc_start_main ../csu/libc-start.c:308 xen-project#8 0x555c6b3197e9 in _start (/usr/local/sbin/xenstored+0xc7e9) Address 0x7ffc194f53e0 is located in stack of thread T0 at offset 80 in frame #0 0x555c6b32713e in dump_state_special_node xen/tools/xenstore/xenstored_domain.c:1232 This frame has 2 object(s): [32, 40) 'head' (line 1233) [64, 80) 'sn' (line 1234) <== Memory access at offset 80 overflows this variable This is happening because the callers are passing a pointer to a variable allocated on the stack. However, the field perms is a dynamic array, so Xenstored will end up to read outside of the variable. Rework the code so the permissions are written one by one in the fd. Fixes: ed6eebf ("tools/xenstore: dump the xenstore state for live update") Signed-off-by: Julien Grall <[email protected]> Reviewed-by: Juergen Gross <[email protected]> Reviewed-by: Luca Fancellu <[email protected]>
1 parent 3f56835 commit 7e71b1e

File tree

3 files changed

+16
-15
lines changed

3 files changed

+16
-15
lines changed

tools/xenstore/xenstored_core.c

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2447,34 +2447,36 @@ const char *dump_state_buffered_data(FILE *fp, const struct connection *c,
24472447
return NULL;
24482448
}
24492449

2450-
const char *dump_state_node_perms(FILE *fp, struct xs_state_node *sn,
2451-
const struct xs_permissions *perms,
2450+
const char *dump_state_node_perms(FILE *fp, const struct xs_permissions *perms,
24522451
unsigned int n_perms)
24532452
{
24542453
unsigned int p;
24552454

24562455
for (p = 0; p < n_perms; p++) {
2456+
struct xs_state_node_perm sp;
2457+
24572458
switch ((int)perms[p].perms & ~XS_PERM_IGNORE) {
24582459
case XS_PERM_READ:
2459-
sn->perms[p].access = XS_STATE_NODE_PERM_READ;
2460+
sp.access = XS_STATE_NODE_PERM_READ;
24602461
break;
24612462
case XS_PERM_WRITE:
2462-
sn->perms[p].access = XS_STATE_NODE_PERM_WRITE;
2463+
sp.access = XS_STATE_NODE_PERM_WRITE;
24632464
break;
24642465
case XS_PERM_READ | XS_PERM_WRITE:
2465-
sn->perms[p].access = XS_STATE_NODE_PERM_BOTH;
2466+
sp.access = XS_STATE_NODE_PERM_BOTH;
24662467
break;
24672468
default:
2468-
sn->perms[p].access = XS_STATE_NODE_PERM_NONE;
2469+
sp.access = XS_STATE_NODE_PERM_NONE;
24692470
break;
24702471
}
2471-
sn->perms[p].flags = (perms[p].perms & XS_PERM_IGNORE)
2472+
sp.flags = (perms[p].perms & XS_PERM_IGNORE)
24722473
? XS_STATE_NODE_PERM_IGNORE : 0;
2473-
sn->perms[p].domid = perms[p].id;
2474-
}
2474+
sp.domid = perms[p].id;
24752475

2476-
if (fwrite(sn->perms, sizeof(*sn->perms), n_perms, fp) != n_perms)
2477-
return "Dump node permissions error";
2476+
if (fwrite(&sp, sizeof(sp), 1, fp) != 1)
2477+
return "Dump node permission error";
2478+
2479+
}
24782480

24792481
return NULL;
24802482
}
@@ -2519,7 +2521,7 @@ static const char *dump_state_node_tree(FILE *fp, char *path)
25192521
if (fwrite(&sn, sizeof(sn), 1, fp) != 1)
25202522
return "Dump node state error";
25212523

2522-
ret = dump_state_node_perms(fp, &sn, hdr->perms, hdr->num_perms);
2524+
ret = dump_state_node_perms(fp, hdr->perms, hdr->num_perms);
25232525
if (ret)
25242526
return ret;
25252527

tools/xenstore/xenstored_core.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,8 +271,7 @@ const char *dump_state_buffered_data(FILE *fp, const struct connection *c,
271271
const struct connection *conn,
272272
struct xs_state_connection *sc);
273273
const char *dump_state_nodes(FILE *fp, const void *ctx);
274-
const char *dump_state_node_perms(FILE *fp, struct xs_state_node *sn,
275-
const struct xs_permissions *perms,
274+
const char *dump_state_node_perms(FILE *fp, const struct xs_permissions *perms,
276275
unsigned int n_perms);
277276

278277
void read_state_global(const void *ctx, const void *state);

tools/xenstore/xenstored_domain.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1254,7 +1254,7 @@ static const char *dump_state_special_node(FILE *fp, const char *name,
12541254
if (fwrite(&sn, sizeof(sn), 1, fp) != 1)
12551255
return "Dump special node error";
12561256

1257-
ret = dump_state_node_perms(fp, &sn, perms->p, perms->num);
1257+
ret = dump_state_node_perms(fp, perms->p, perms->num);
12581258
if (ret)
12591259
return ret;
12601260

0 commit comments

Comments
 (0)