tools/xenstored: Fix off-by-one in dump_state_nodes()
authorJulien Grall <jgrall@amazon.com>
Thu, 29 Jul 2021 09:34:20 +0000 (10:34 +0100)
committerIan Jackson <iwj@xenproject.org>
Fri, 30 Jul 2021 10:02:35 +0000 (11:02 +0100)
The maximum path length supported by Xenstored protocol is
XENSTORE_ABS_PATH_MAX (i.e 3072). This doesn't take into account the
NUL at the end of the path.

However, the code to dump the nodes will allocate a buffer
of XENSTORE_ABS_PATH. As a result it may not be possible to live-update
if there is a node name of XENSTORE_ABS_PATH.

Fix it by allocating a buffer of XENSTORE_ABS_PATH_MAX + 1 characters.

Take the opportunity to pass the max length of the buffer as a
parameter of dump_state_node_tree(). This will be clearer that the
check in the function is linked to the allocation in dump_state_nodes().

Signed-off-by: Julien Grall <jgrall@amazon.com>
Reviewed-by: Juergen Gross <jgross@suse.com>
tools/xenstore/xenstored_core.c

index 16c856730c5541aa290cb1d7ea48222d1ed81d09..0d4c73d6e20c31183bd83e10cacf80851e652d0a 100644 (file)
@@ -2574,7 +2574,8 @@ const char *dump_state_node_perms(FILE *fp, const struct xs_permissions *perms,
        return NULL;
 }
 
-static const char *dump_state_node_tree(FILE *fp, char *path)
+static const char *dump_state_node_tree(FILE *fp, char *path,
+                                       unsigned int path_max_len)
 {
        unsigned int pathlen, childlen, p = 0;
        struct xs_state_record_header head;
@@ -2642,10 +2643,10 @@ static const char *dump_state_node_tree(FILE *fp, char *path)
        }
        while (p < hdr->childlen) {
                childlen = strlen(child) + 1;
-               if (pathlen + childlen > XENSTORE_ABS_PATH_MAX)
+               if (pathlen + childlen > path_max_len)
                        return "Dump node path length error";
                strcpy(path + pathlen, child);
-               ret = dump_state_node_tree(fp, path);
+               ret = dump_state_node_tree(fp, path, path_max_len);
                if (ret)
                        return ret;
                p += childlen;
@@ -2661,13 +2662,13 @@ const char *dump_state_nodes(FILE *fp, const void *ctx)
 {
        char *path;
 
-       path = talloc_size(ctx, XENSTORE_ABS_PATH_MAX);
+       path = talloc_size(ctx, XENSTORE_ABS_PATH_MAX + 1);
        if (!path)
                return "Path buffer allocation error";
 
        strcpy(path, "/");
 
-       return dump_state_node_tree(fp, path);
+       return dump_state_node_tree(fp, path, XENSTORE_ABS_PATH_MAX + 1);
 }
 
 void read_state_global(const void *ctx, const void *state)