Xen Security Advisory 418 v2 (CVE-2022-42321) - Xenstore: Guests can crash xenstored via exhausting the stack
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
Xen Security Advisory CVE-2022-42321 / XSA-418
version 2
Xenstore: Guests can crash xenstored via exhausting the stack
UPDATES IN VERSION 2
====================
Public release.
ISSUE DESCRIPTION
=================
Xenstored is using recursion for some Xenstore operations (e.g. for deleting a sub-tree of Xenstore nodes). With sufficiently deep nesting levels this can result in stack exhaustion on xenstored, leading to a crash of xenstored.
IMPACT
======
A malicious guest creating very deep nesting levels of Xenstore nodes might be able to crash xenstored, resulting in a Denial of Service (DoS) of Xenstore.
This will inhibit creation of new guests or changing the configuration of already running guests.
VULNERABLE SYSTEMS
==================
All versions of Xen are affected.
Only systems running the C variant of Xenstore (xenstored or xenstore- stubdom) are vulnerable.
Systems using the Ocaml variant of Xenstore (oxenstored) are not vulnerable.
MITIGATION
==========
Running oxenstored instead of xenstored will avoid the vulnerability.
CREDITS
=======
This issue was discovered by David Vrabel of Amazon.
RESOLUTION
==========
Applying the appropriate attached patch resolves this issue.
Note that patches for released versions are generally prepared to apply to the stable branches, and may not apply cleanly to the most recent release tarball. Downstreams are encouraged to update to the tip of the stable branch before applying these patches.
xsa418/xsa418-??.patch xen-unstable xsa418/xsa418-4.16-??.patch Xen 4.16.x xsa418/xsa418-4.15-??.patch Xen 4.15.x xsa418/xsa418-4.14-??.patch Xen 4.14.x - 4.13.x
$ sha256sum xsa418* xsa418*/* dba8cf354728d5b9248d9649d042835b2f5f96dd995d0fe23a07a157cba68500 xsa418.meta d13f084bbca78d35b991fe5347297d13f77b4e49ad816344363a61a8335e6632 xsa418/xsa418-01.patch ac9acb8cda844e3873ec0a77fb9bd58581d6f1084f8a38fa494bff548c9232ae xsa418/xsa418-02.patch bc29743d71eed3ba41d1ec732e5c0011107dcc06d945ec554ef04314e0272898 xsa418/xsa418-03.patch bba67ab17c8c132258b0cfbc701e2b79ae6ea5ef507f4c09e103c19a9c729b03 xsa418/xsa418-4.14-01.patch 79eadfee1eeae340256331b5e189f1c8514106dae5ca208b0f4965ba6f6e9e51 xsa418/xsa418-4.14-02.patch 6a96c8636fc3c2a1539b9c21d3af4e0a68124dc4a7219c5eacd685f7d0543dd7 xsa418/xsa418-4.14-03.patch fe4ad75c34ceba6427c6f2ea7ad86af4a25ba3f5f9dc42fdd4ef7bf4fa60d39d xsa418/xsa418-4.14-04.patch 7884b7850d991d098409a3a9a27050f3d34486a3b459e0c2047d1dc43e13515f xsa418/xsa418-4.14-05.patch 27c070655bf27a2ca84506703d76ab5b3c9fd22155a29af5c882013cd5580640 xsa418/xsa418-4.14-06.patch 313707f2b0738680015a38ec50d93f149c386c72c809cd17de8f52e2d883b8e0 xsa418/xsa418-4.15-01.patch 4628506b3f4407034b7c6e0159a6719225f6a4c70fe12b30375f515bb6ce5d93 xsa418/xsa418-4.15-02.patch a59fed27d614de06a8d508da6345dda7260d2ac7ff9762372b34c4e6a5dfa432 xsa418/xsa418-4.15-03.patch 99ea45e5f877afe01af189ebfe3114edc8d3283829424adc53760d385b8a202e xsa418/xsa418-4.15-04.patch dd10d3c3af942fd941604029a5b5262ae6d8f7c7a9071b243904bc34c8d14ab2 xsa418/xsa418-4.15-05.patch 1a50edee9d3a04a982ba22bcf150475f396494c03b4b6eaf18b45561f0d005fc xsa418/xsa418-4.15-06.patch 042cf55472e911b871a8062613b604e7a4641505bae4e6505a176b2976906739 xsa418/xsa418-4.15-07.patch 669e8fc1637b92846ad7b72eb510c05920b267bc54340e83b3f1c8df2092ecbc xsa418/xsa418-4.16-01.patch b382431343ab873d6ab88557b09891dc821a497200c1b61e7b64286bba899ea9 xsa418/xsa418-4.16-02.patch 27737bfa0d3e475ba0e468ab3dcf0274bde40948e5f669f179d2964f6cfab4cf xsa418/xsa418-4.16-03.patch 5677156c12063d0cbad273d45800bb25176308ffd7b660d73aac3a36e4099055 xsa418/xsa418-4.16-04.patch 3c3b0282cbc50da485f6b7a871e0cc318725db2b3debf098b0fc6d0598488a48 xsa418/xsa418-4.16-05.patch d871d0e38f6db4cc86591c63cb37c63aed9ed0ba88429236eb91d142090da529 xsa418/xsa418-4.16-06.patch 145a98f2540b5c17c7d262e1df80103c4478d622a4eeba07d1566679d81a4542 xsa418/xsa418-4.16-07.patch 70874f345806b376fea1b02b0ed4d493d792a43f5c6fc29c13e0658350086f92 xsa418/xsa418-04.patch 8d94a7c6e9e484569c6eb98f274fa7489e68a9f16d12092839bb519cfc32a7b3 xsa418/xsa418-05.patch e5ecc6d3756a485114b57e0d02ff53d6eb3b312fac117a99c05bc392faa45d27 xsa418/xsa418-06.patch 77695fa2f1bfeee051d4a0e0d1e0b654f5177ce104a72635c2f1bafb1d6631cb xsa418/xsa418-07.patch $
DEPLOYMENT DURING EMBARGO
=========================
Deployment of the patches and/or mitigations described above (or others which are substantially similar) is permitted during the embargo, even on public-facing systems with untrusted guest users and administrators.
But: Distribution of updated software is prohibited (except to other members of the predisclosure list).
Predisclosure list members who wish to deploy significantly different patches and/or mitigations, please contact the Xen Project Security Team.
(Note: this during-embargo deployment notice is retained in post-embargo publicly released Xen Project advisories, even though it is then no longer applicable. This is to enable the community to have oversight of the Xen Project Security Team's decisionmaking.)
For more information about permissible uses of embargoed information, consult the Xen Project community's agreed Security Policy:
http://www.xenproject.org/security-policy.html
-----BEGIN PGP SIGNATURE-----
iQFABAEBCAAqFiEEI+MiLBRfRHX6gGCng/4UyVfoK9kFAmNg+6sMHHBncEB4ZW4u b3JnAAoJEIP+FMlX6CvZ0wAH/36wusPv68bogxxnnNwL6eFmZZ1Rd90mAMfw6Qyt OYo3tOWhnZVVH3uC84S7s/zWsZWJaaWxTnGW03Gxnep3GstufnWnV0m/VsmXsI9L /W0C23SgWxao+Bc819TRWF3JTcSb/wdbBbgHOJbu8gzLQc7T8xsgUeOr34fpAtZv qr2fExhKrlxdWYodDJLdZryZRBQ1ZKbO+Rihpv23FKst4HhlQvCvWr99oK6/ubkp 2mzLjeotWxT2G+RnQNJp4JqgXaYr6972/Q5h75lCxQZWxw7baIS62gTaFfK8cD4p j4gVo2zYtMBivUZngmTF36iRN743NAOz3HsvU1pEphbc24o=
=6SQq
-----END PGP SIGNATURE-----
From 9923c06a4f19c5a46927e35f103ec65b65e416c2 Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Tue, 13 Sep 2022 07:35:11 +0200
Subject: tools/xenstore: remove recursion from construct_node()
In order to reduce stack usage due to recursion, switch construct_node() to use a loop instead.
This is part of XSA-418 / CVE-2022-42321.
tools/xenstore/xenstored_core.c +55 -31
@@ -1373,45 +1373,69 @@ static int add_child(const void *ctx, struct node *parent, const char *name)static struct node *construct_node (struct connection *conn,const void *ctx,const char *name) { -struct node *parent, *node; -char *parentname =get_parent (ctx, name); +const char **names =NULL ; +unsigned int levels =0 ; +struct node *node =NULL ; +struct node *parent =NULL ; +const char *parentname =talloc_strdup (ctx, name);if (!parentname)return NULL ; -/* If parent doesn't exist, create it. */ - parent =read_node (conn, parentname, parentname); -if (!parent && errno ==ENOENT ) - parent =construct_node (conn, ctx, parentname); -if (!parent) -return NULL ; +/* Walk the path up until an existing node is found. */ +while (!parent) { + names = talloc_realloc(ctx, names, const char *, levels + 1); +if (!names) + goto nomem; -/* Add child to parent. */ -if (add_child (ctx, parent, name)) - goto nomem; +/* +* names[0] is the name of the node to construct initially, +* names[1] is its parent, and so on. +*/ + names[levels] = parentname; + parentname =get_parent (ctx, parentname); +if (!parentname) +return NULL ; -/* Allocate node */ - node = talloc(ctx, struct node); -if (!node) - goto nomem; - node->name =talloc_strdup (node, name); -if (!node->name) - goto nomem; +/* Try to read parent node until we found an existing one. */ + parent =read_node (conn, ctx, parentname); +if (!parent && (errno !=ENOENT || !strcmp (parentname,"/" ))) +return NULL ; -/* Inherit permissions, except unprivileged domains own what they create */ - node->perms.num = parent->perms.num; - node->perms.p =talloc_memdup (node, parent->perms.p, - node->perms.num *sizeof (*node->perms.p)); -if (!node->perms.p) - goto nomem; -if (domain_is_unprivileged (conn)) - node->perms.p[0 ].id = conn->id; + levels++; + } + +/* Walk the path down again constructing the missing nodes. */ +for (; levels >0 ; levels--) { +/* Add child to parent. */ +if (add_child (ctx, parent, names[levels -1 ])) + goto nomem; + +/* Allocate node */ + node = talloc(ctx, struct node); +if (!node) + goto nomem; + node->name =talloc_steal (node, names[levels -1 ]); + +/* Inherit permissions, unpriv domains own what they create. */ + node->perms.num = parent->perms.num; + node->perms.p =talloc_memdup (node, parent->perms.p, + node->perms.num * +sizeof (*node->perms.p)); +if (!node->perms.p) + goto nomem; +if (domain_is_unprivileged (conn)) + node->perms.p[0 ].id = conn->id; + +/* No children, no data */ + node->children = node->data =NULL ; + node->childlen = node->datalen =0 ; + node->acc.memory =0 ; + node->parent = parent; + + parent = node; + } -/* No children, no data */ - node->children = node->data =NULL ; - node->childlen = node->datalen =0 ; - node->acc.memory =0 ; - node->parent = parent;return node; nomem:
From 86c6ee483547a61355b32117e439d47b4daccbdc Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Tue, 13 Sep 2022 07:35:11 +0200
Subject: tools/xenstore: don't let remove_child_entry() call corrupt()
In case of write_node() returning an error, remove_child_entry() will call corrupt() today. This could result in an endless recursion, as remove_child_entry() is called by corrupt(), too:
corrupt()
check_store()
check_store_()
remove_child_entry()Fix that by letting remove_child_entry() return an error instead and let the caller decide what to do.
This is part of XSA-418 / CVE-2022-42321.
tools/xenstore/xenstored_core.c +20 -16
@@ -1604,15 +1604,15 @@ static void memdel(void *mem, unsigned off, unsigned len, unsigned total)memmove (mem + off, mem + off + len, total - off - len); } -static void remove_child_entry (struct connection *conn,struct node *node, -size_t offset) +static int remove_child_entry (struct connection *conn,struct node *node, +size_t offset) {size_t childlen =strlen (node->children + offset);memdel (node->children, offset, childlen +1 , node->childlen); node->childlen -= childlen +1 ; -if (write_node (conn, node, true)) -corrupt (conn,"Can't update parent node '%s'" , node->name); + +return write_node (conn, node, true); } static void delete_child(struct connection *conn,
@@ -1622,7 +1622,9 @@ static void delete_child(struct connection *conn,for (i =0 ; i < node->childlen; i +=strlen (node->children+i) +1 ) {if (streq (node->children+i, childname)) { -remove_child_entry (conn, node, i); +if (remove_child_entry (conn, node, i)) +corrupt (conn,"Can't update parent node '%s'" , + node->name);return ; } }
@@ -2304,6 +2306,17 @@ int remember_string(struct hashtable *hash, const char *str)return hashtable_insert (hash, k, (void *)1 ); } +static int rm_child_entry (struct node *node,size_t off,size_t len) +{ +if (!recovery) +return off; + +if (remove_child_entry (NULL , node, off)) +log ("check_store: child entry could not be removed from '%s'" , + node->name); + +return off - len -1 ; +}/** * A node has a children field that names the children of the node, separated
@@ -2356,12 +2369,7 @@ static int check_store_(const char *name, struct hashtable *reachable)if (hashtable_search (children, childname)) {log ("check_store: '%s' is duplicated!" , childname); - -if (recovery) { -remove_child_entry (NULL , node, - i); - i -= childlen +1 ; - } + i =rm_child_entry (node, i, childlen); } else { if (!remember_string(children,
@@ -2378,11 +2386,7 @@ static int check_store_(const char *name, struct hashtable *reachable) }else if (errno !=ENOMEM ) {log ("check_store: No child '%s' found!\n" , childname); - -if (recovery) { -remove_child_entry (NULL , node, i); - i -= childlen +1 ; - } + i =rm_child_entry (node, i, childlen); }else {log ("check_store: ENOMEM" ); ret =ENOMEM ;
From 567ac251a657f8c1847abb5a15ffa686b5aed6fb Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Tue, 13 Sep 2022 07:35:11 +0200
Subject: tools/xenstore: add generic treewalk function
Add a generic function to walk the complete node tree. It will start at "/" and descend recursively into each child, calling a function specified by the caller. Depending on the return value of the user specified function the walk will be aborted, continued, or the current child will be skipped by not descending into its children.
This is part of XSA-418 / CVE-2022-42321.
tools/xenstore/xenstored_core.c +130 -13
161 lines not on this page · raw message
tools/xenstore/xenstored_core.h +40 -0
@@ -202,6 +202,7 @@ struct node {/* Children, each nul-terminated. */ unsigned int childlen; +unsigned int childoff;/* Used by walk_node_tree() internally. */ char *children;/* Allocation information for node currently in store. */
@@ -333,6 +334,45 @@ void read_state_buffered_data(const void *ctx, struct connection *conn, const struct xs_state_connection *sc);void read_state_node (const void *ctx,const void *state); +/* +* Walk the node tree below root calling funcs->enter() and funcs->exit() for +* each node. funcs->enter() is being called when entering a node, so before +* any of the children of the node is processed. funcs->exit() is being +* called when leaving the node, so after all children have been processed. +* funcs->enoent() is being called when a node isn't existing. +* funcs->*() return values: +* < 0: tree walk is stopped, walk_node_tree() returns funcs->*() return value +* in case WALK_TREE_ERROR_STOP is returned, errno should be set +* WALK_TREE_OK: tree walk is continuing +* WALK_TREE_SKIP_CHILDREN: tree walk won't descend below current node, but +* walk continues +* WALK_TREE_RM_CHILDENTRY: Remove the child entry from its parent and write +* the modified parent node back to the data base, implies to not descend +* below the current node, but to continue the walk +* funcs->*() is allowed to modify the node it is called for in the data base. +* In case funcs->enter() is deleting the node, it must not return WALK_TREE_OK +* in order to avoid descending into no longer existing children. +*/ +/* Return values for funcs->*() and walk_node_tree(). */ +#define WALK_TREE_SUCCESS_STOP -100/* Stop walk early, no error. */ +#define WALK_TREE_ERROR_STOP -1/* Stop walk due to error. */ +#define WALK_TREE_OK 0/* No error. */ +/* Return value for funcs->*() only. */ +#define WALK_TREE_SKIP_CHILDREN 1/* Don't recurse below current node. */ +#define WALK_TREE_RM_CHILDENTRY 2/* Remove child entry from parent. */ + +struct walk_funcs { +int (*enter)(const void *ctx,struct connection *conn, +struct node *node,void *arg); +int (*exit)(const void *ctx,struct connection *conn, +struct node *node,void *arg); +int (*enoent)(const void *ctx,struct connection *conn, +struct node *parent,char *name,void *arg); +}; + +int walk_node_tree (const void *ctx,struct connection *conn,const char *root, +struct walk_funcs *funcs,void *arg); +#endif /* _XENSTORED_CORE_H */ /*
From 0d4f442b883a27986111fc524d4575a356959bae Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Tue, 13 Sep 2022 07:35:11 +0200
Subject: tools/xenstore: remove recursion from construct_node()
In order to reduce stack usage due to recursion, switch construct_node() to use a loop instead.
This is part of XSA-418 / CVE-2022-42321.
tools/xenstore/xenstored_core.c +72 -38
129 lines not on this page · raw message
From b8075553eab9edd8e40f4c6da6c4439835aa1eea Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Tue, 13 Sep 2022 07:35:11 +0200
Subject: tools/xenstore: don't let remove_child_entry() call corrupt()
In case of write_node() returning an error, remove_child_entry() will call corrupt() today. This could result in an endless recursion, as remove_child_entry() is called by corrupt(), too:
corrupt()
check_store()
check_store_()
remove_child_entry()Fix that by letting remove_child_entry() return an error instead and let the caller decide what to do.
This is part of XSA-418 / CVE-2022-42321.
tools/xenstore/xenstored_core.c +20 -16
@@ -1505,15 +1505,15 @@ static void memdel(void *mem, unsigned off, unsigned len, unsigned total)memmove (mem + off, mem + off + len, total - off - len); } -static void remove_child_entry (struct connection *conn,struct node *node, -size_t offset) +static int remove_child_entry (struct connection *conn,struct node *node, +size_t offset) {size_t childlen =strlen (node->children + offset);memdel (node->children, offset, childlen +1 , node->childlen); node->childlen -= childlen +1 ; -if (write_node (conn, node, true)) -corrupt (conn,"Can't update parent node '%s'" , node->name); + +return write_node (conn, node, true); } static void delete_child(struct connection *conn,
@@ -1523,7 +1523,9 @@ static void delete_child(struct connection *conn,for (i =0 ; i < node->childlen; i +=strlen (node->children+i) +1 ) {if (streq (node->children+i, childname)) { -remove_child_entry (conn, node, i); +if (remove_child_entry (conn, node, i)) +corrupt (conn,"Can't update parent node '%s'" , + node->name);return ; } }
@@ -2125,6 +2127,17 @@ int remember_string(struct hashtable *hash, const char *str)return hashtable_insert (hash, k, (void *)1 ); } +static int rm_child_entry (struct node *node,size_t off,size_t len) +{ +if (!recovery) +return off; + +if (remove_child_entry (NULL , node, off)) +log ("check_store: child entry could not be removed from '%s'" , + node->name); + +return off - len -1 ; +}/** * A node has a children field that names the children of the node, separated
@@ -2173,12 +2186,7 @@ static int check_store_(const char *name, struct hashtable *reachable)if (hashtable_search (children, childname)) {log ("check_store: '%s' is duplicated!" , childname); - -if (recovery) { -remove_child_entry (NULL , node, - i); - i -= childlen +1 ; - } + i =rm_child_entry (node, i, childlen); } else { if (!remember_string(children,
@@ -2195,11 +2203,7 @@ static int check_store_(const char *name, struct hashtable *reachable) }else if (errno !=ENOMEM ) {log ("check_store: No child '%s' found!\n" , childname); - -if (recovery) { -remove_child_entry (NULL , node, i); - i -= childlen +1 ; - } + i =rm_child_entry (node, i, childlen); }else {log ("check_store: ENOMEM" ); ret =ENOMEM ;
From 60e5a1dd436a0d5778208df734044e6c610b8a81 Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Tue, 13 Sep 2022 07:35:11 +0200
Subject: tools/xenstore: add generic treewalk function
Add a generic function to walk the complete node tree. It will start at "/" and descend recursively into each child, calling a function specified by the caller. Depending on the return value of the user specified function the walk will be aborted, continued, or the current child will be skipped by not descending into its children.
This is part of XSA-418 / CVE-2022-42321.
tools/xenstore/xenstored_core.c +130 -13
161 lines not on this page · raw message
tools/xenstore/xenstored_core.h +40 -0
@@ -167,6 +167,7 @@ struct node {/* Children, each nul-terminated. */ unsigned int childlen; +unsigned int childoff;/* Used by walk_node_tree() internally. */ char *children;/* Allocation information for node currently in store. */
@@ -278,6 +279,45 @@ int do_tdb_delete(struct connection *conn, TDB_DATA *key,void conn_free_buffered_data (struct connection *conn); +/* +* Walk the node tree below root calling funcs->enter() and funcs->exit() for +* each node. funcs->enter() is being called when entering a node, so before +* any of the children of the node is processed. funcs->exit() is being +* called when leaving the node, so after all children have been processed. +* funcs->enoent() is being called when a node isn't existing. +* funcs->*() return values: +* < 0: tree walk is stopped, walk_node_tree() returns funcs->*() return value +* in case WALK_TREE_ERROR_STOP is returned, errno should be set +* WALK_TREE_OK: tree walk is continuing +* WALK_TREE_SKIP_CHILDREN: tree walk won't descend below current node, but +* walk continues +* WALK_TREE_RM_CHILDENTRY: Remove the child entry from its parent and write +* the modified parent node back to the data base, implies to not descend +* below the current node, but to continue the walk +* funcs->*() is allowed to modify the node it is called for in the data base. +* In case funcs->enter() is deleting the node, it must not return WALK_TREE_OK +* in order to avoid descending into no longer existing children. +*/ +/* Return values for funcs->*() and walk_node_tree(). */ +#define WALK_TREE_SUCCESS_STOP -100/* Stop walk early, no error. */ +#define WALK_TREE_ERROR_STOP -1/* Stop walk due to error. */ +#define WALK_TREE_OK 0/* No error. */ +/* Return value for funcs->*() only. */ +#define WALK_TREE_SKIP_CHILDREN 1/* Don't recurse below current node. */ +#define WALK_TREE_RM_CHILDENTRY 2/* Remove child entry from parent. */ + +struct walk_funcs { +int (*enter)(const void *ctx,struct connection *conn, +struct node *node,void *arg); +int (*exit)(const void *ctx,struct connection *conn, +struct node *node,void *arg); +int (*enoent)(const void *ctx,struct connection *conn, +struct node *parent,char *name,void *arg); +}; + +int walk_node_tree (const void *ctx,struct connection *conn,const char *root, +struct walk_funcs *funcs,void *arg); +#endif /* _XENSTORED_CORE_H */ /*
From d58d069b0af5e64b667609723ba24ad5242fc920 Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Tue, 13 Sep 2022 07:35:12 +0200
Subject: tools/xenstore: simplify check_store()
check_store() is using a hash table for storing all node names it has found via walking the tree. Additionally it using another hash table for all children of a node to detect duplicate child names.
Simplify that by dropping the second hash table as the first one is already holding all the needed information.
This is part of XSA-418 / CVE-2022-42321.
tools/xenstore/xenstored_core.c +15 -28
80 lines not on this page · raw message
From ac24da3fde16215b4ba5f0cc8d9686881e97bb42 Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Tue, 13 Sep 2022 07:35:12 +0200
Subject: tools/xenstore: use treewalk for check_store()
Instead of doing an open tree walk using call recursion, use walk_node_tree() when checking the store for inconsistencies.
This will reduce code size and avoid many nesting levels of function calls which could potentially exhaust the stack.
This is part of XSA-418 / CVE-2022-42321.
tools/xenstore/xenstored_core.c +28 -77
140 lines not on this page · raw message
From 56d02eea4e7740e4e140cf317c6ec3daa74a3e89 Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Tue, 13 Sep 2022 07:35:12 +0200
Subject: tools/xenstore: use treewalk for deleting nodes
Instead of doing an open tree walk using call recursion, use walk_node_tree() when deleting a sub-tree of nodes.
This will reduce code size and avoid many nesting levels of function calls which could potentially exhaust the stack.
This is part of XSA-418 / CVE-2022-42321.
tools/xenstore/xenstored_core.c +43 -56
149 lines not on this page · raw message
From 92fa48921c17cebe6b61c161f7ff5dfa27975a18 Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Tue, 13 Sep 2022 07:35:11 +0200
Subject: tools/xenstore: remove recursion from construct_node()
In order to reduce stack usage due to recursion, switch construct_node() to use a loop instead.
This is part of XSA-418 / CVE-2022-42321.
tools/xenstore/xenstored_core.c +55 -31
100 lines not on this page · raw message
From df45ea379c68e675ce88bbe25e17cfe49ec108b7 Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Tue, 13 Sep 2022 07:35:11 +0200
Subject: tools/xenstore: don't let remove_child_entry() call corrupt()
In case of write_node() returning an error, remove_child_entry() will call corrupt() today. This could result in an endless recursion, as remove_child_entry() is called by corrupt(), too:
corrupt()
check_store()
check_store_()
remove_child_entry()Fix that by letting remove_child_entry() return an error instead and let the caller decide what to do.
This is part of XSA-418 / CVE-2022-42321.
tools/xenstore/xenstored_core.c +20 -16
@@ -1574,15 +1574,15 @@ static void memdel(void *mem, unsigned off, unsigned len, unsigned total)memmove (mem + off, mem + off + len, total - off - len); } -static void remove_child_entry (struct connection *conn,struct node *node, -size_t offset) +static int remove_child_entry (struct connection *conn,struct node *node, +size_t offset) {size_t childlen =strlen (node->children + offset);memdel (node->children, offset, childlen +1 , node->childlen); node->childlen -= childlen +1 ; -if (write_node (conn, node, true)) -corrupt (conn,"Can't update parent node '%s'" , node->name); + +return write_node (conn, node, true); } static void delete_child(struct connection *conn,
@@ -1592,7 +1592,9 @@ static void delete_child(struct connection *conn,for (i =0 ; i < node->childlen; i +=strlen (node->children+i) +1 ) {if (streq (node->children+i, childname)) { -remove_child_entry (conn, node, i); +if (remove_child_entry (conn, node, i)) +corrupt (conn,"Can't update parent node '%s'" , + node->name);return ; } }
@@ -2226,6 +2228,17 @@ int remember_string(struct hashtable *hash, const char *str)return hashtable_insert (hash, k, (void *)1 ); } +static int rm_child_entry (struct node *node,size_t off,size_t len) +{ +if (!recovery) +return off; + +if (remove_child_entry (NULL , node, off)) +log ("check_store: child entry could not be removed from '%s'" , + node->name); + +return off - len -1 ; +}/** * A node has a children field that names the children of the node, separated
@@ -2278,12 +2291,7 @@ static int check_store_(const char *name, struct hashtable *reachable)if (hashtable_search (children, childname)) {log ("check_store: '%s' is duplicated!" , childname); - -if (recovery) { -remove_child_entry (NULL , node, - i); - i -= childlen +1 ; - } + i =rm_child_entry (node, i, childlen); } else { if (!remember_string(children,
@@ -2300,11 +2308,7 @@ static int check_store_(const char *name, struct hashtable *reachable) }else if (errno !=ENOMEM ) {log ("check_store: No child '%s' found!\n" , childname); - -if (recovery) { -remove_child_entry (NULL , node, i); - i -= childlen +1 ; - } + i =rm_child_entry (node, i, childlen); }else {log ("check_store: ENOMEM" ); ret =ENOMEM ;
From 78d86d76345df44d41fad8659f637a93ccd849b7 Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Tue, 13 Sep 2022 07:35:11 +0200
Subject: tools/xenstore: add generic treewalk function
Add a generic function to walk the complete node tree. It will start at "/" and descend recursively into each child, calling a function specified by the caller. Depending on the return value of the user specified function the walk will be aborted, continued, or the current child will be skipped by not descending into its children.
This is part of XSA-418 / CVE-2022-42321.
tools/xenstore/xenstored_core.c +130 -13
161 lines not on this page · raw message
tools/xenstore/xenstored_core.h +40 -0
@@ -195,6 +195,7 @@ struct node {/* Children, each nul-terminated. */ unsigned int childlen; +unsigned int childoff;/* Used by walk_node_tree() internally. */ char *children;/* Allocation information for node currently in store. */
@@ -334,6 +335,45 @@ void read_state_buffered_data(const void *ctx, struct connection *conn, const struct xs_state_connection *sc);void read_state_node (const void *ctx,const void *state); +/* +* Walk the node tree below root calling funcs->enter() and funcs->exit() for +* each node. funcs->enter() is being called when entering a node, so before +* any of the children of the node is processed. funcs->exit() is being +* called when leaving the node, so after all children have been processed. +* funcs->enoent() is being called when a node isn't existing. +* funcs->*() return values: +* < 0: tree walk is stopped, walk_node_tree() returns funcs->*() return value +* in case WALK_TREE_ERROR_STOP is returned, errno should be set +* WALK_TREE_OK: tree walk is continuing +* WALK_TREE_SKIP_CHILDREN: tree walk won't descend below current node, but +* walk continues +* WALK_TREE_RM_CHILDENTRY: Remove the child entry from its parent and write +* the modified parent node back to the data base, implies to not descend +* below the current node, but to continue the walk +* funcs->*() is allowed to modify the node it is called for in the data base. +* In case funcs->enter() is deleting the node, it must not return WALK_TREE_OK +* in order to avoid descending into no longer existing children. +*/ +/* Return values for funcs->*() and walk_node_tree(). */ +#define WALK_TREE_SUCCESS_STOP -100/* Stop walk early, no error. */ +#define WALK_TREE_ERROR_STOP -1/* Stop walk due to error. */ +#define WALK_TREE_OK 0/* No error. */ +/* Return value for funcs->*() only. */ +#define WALK_TREE_SKIP_CHILDREN 1/* Don't recurse below current node. */ +#define WALK_TREE_RM_CHILDENTRY 2/* Remove child entry from parent. */ + +struct walk_funcs { +int (*enter)(const void *ctx,struct connection *conn, +struct node *node,void *arg); +int (*exit)(const void *ctx,struct connection *conn, +struct node *node,void *arg); +int (*enoent)(const void *ctx,struct connection *conn, +struct node *parent,char *name,void *arg); +}; + +int walk_node_tree (const void *ctx,struct connection *conn,const char *root, +struct walk_funcs *funcs,void *arg); +#endif /* _XENSTORED_CORE_H */ /*
From 1e72aa3c507c9e9bb495d12907189034bc8b1e7a Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Tue, 13 Sep 2022 07:35:12 +0200
Subject: tools/xenstore: simplify check_store()
check_store() is using a hash table for storing all node names it has found via walking the tree. Additionally it using another hash table for all children of a node to detect duplicate child names.
Simplify that by dropping the second hash table as the first one is already holding all the needed information.
This is part of XSA-418 / CVE-2022-42321.
tools/xenstore/xenstored_core.c +15 -32
84 lines not on this page · raw message
From 358c392359a29d28751907303dc181073a42ab33 Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Tue, 13 Sep 2022 07:35:12 +0200
Subject: tools/xenstore: use treewalk for check_store()
Instead of doing an open tree walk using call recursion, use walk_node_tree() when checking the store for inconsistencies.
This will reduce code size and avoid many nesting levels of function calls which could potentially exhaust the stack.
This is part of XSA-418 / CVE-2022-42321.
tools/xenstore/xenstored_core.c +28 -77
140 lines not on this page · raw message
From c47828c7286478a9dc04010ae4191c4c023f2fa8 Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Tue, 13 Sep 2022 07:35:12 +0200
Subject: tools/xenstore: use treewalk for deleting nodes
Instead of doing an open tree walk using call recursion, use walk_node_tree() when deleting a sub-tree of nodes.
This will reduce code size and avoid many nesting levels of function calls which could potentially exhaust the stack.
This is part of XSA-418 / CVE-2022-42321.
tools/xenstore/xenstored_core.c +43 -56
149 lines not on this page · raw message
From a4a0cd435a2d74fc8901cc7979cc37745389f96e Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Tue, 13 Sep 2022 07:35:12 +0200
Subject: tools/xenstore: use treewalk for creating node records
Instead of doing an open tree walk using call recursion, use walk_node_tree() when creating the node records during a live update.
This will reduce code size and avoid many nesting levels of function calls which could potentially exhaust the stack.
This is part of XSA-418 / CVE-2022-42321.
tools/xenstore/xenstored_core.c +52 -75
184 lines not on this page · raw message
tools/xenstore/xenstored_core.h +1 -2
@@ -326,8 +326,7 @@ const char *dump_state_buffered_data(FILE *fp, const struct connection *c,const struct connection *conn, struct xs_state_connection *sc);const char *dump_state_nodes (FILE *fp,const void *ctx); -const char *dump_state_node_perms (FILE *fp,struct xs_state_node *sn, -const struct xs_permissions *perms, +const char *dump_state_node_perms (FILE *fp,const struct xs_permissions *perms,unsigned int n_perms);void read_state_global (const void *ctx,const void *state);
tools/xenstore/xenstored_domain.c +1 -1
@@ -1449,7 +1449,7 @@ static const char *dump_state_special_node(FILE *fp, const char *name,if (fwrite (&sn,sizeof (sn),1 , fp) !=1 )return "Dump special node error" ; - ret =dump_state_node_perms (fp, &sn, perms->p, perms->num); + ret =dump_state_node_perms (fp, perms->p, perms->num);if (ret)return ret;
From d1e6dca486599ab914af7b38b3782b237d3d603b Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Tue, 13 Sep 2022 07:35:11 +0200
Subject: tools/xenstore: remove recursion from construct_node()
In order to reduce stack usage due to recursion, switch construct_node() to use a loop instead.
This is part of XSA-418 / CVE-2022-42321.
tools/xenstore/xenstored_core.c +55 -31
100 lines not on this page · raw message
From c13d85a2fe94bbf3cb8186b89324c5d1b4f9a61f Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Tue, 13 Sep 2022 07:35:11 +0200
Subject: tools/xenstore: don't let remove_child_entry() call corrupt()
In case of write_node() returning an error, remove_child_entry() will call corrupt() today. This could result in an endless recursion, as remove_child_entry() is called by corrupt(), too:
corrupt()
check_store()
check_store_()
remove_child_entry()Fix that by letting remove_child_entry() return an error instead and let the caller decide what to do.
This is part of XSA-418 / CVE-2022-42321.
tools/xenstore/xenstored_core.c +20 -16
@@ -1608,15 +1608,15 @@ static void memdel(void *mem, unsigned off, unsigned len, unsigned total)memmove (mem + off, mem + off + len, total - off - len); } -static void remove_child_entry (struct connection *conn,struct node *node, -size_t offset) +static int remove_child_entry (struct connection *conn,struct node *node, +size_t offset) {size_t childlen =strlen (node->children + offset);memdel (node->children, offset, childlen +1 , node->childlen); node->childlen -= childlen +1 ; -if (write_node (conn, node, true)) -corrupt (conn,"Can't update parent node '%s'" , node->name); + +return write_node (conn, node, true); } static void delete_child(struct connection *conn,
@@ -1626,7 +1626,9 @@ static void delete_child(struct connection *conn,for (i =0 ; i < node->childlen; i +=strlen (node->children+i) +1 ) {if (streq (node->children+i, childname)) { -remove_child_entry (conn, node, i); +if (remove_child_entry (conn, node, i)) +corrupt (conn,"Can't update parent node '%s'" , + node->name);return ; } }
@@ -2325,6 +2327,17 @@ int remember_string(struct hashtable *hash, const char *str)return hashtable_insert (hash, k, (void *)1 ); } +static int rm_child_entry (struct node *node,size_t off,size_t len) +{ +if (!recovery) +return off; + +if (remove_child_entry (NULL , node, off)) +log ("check_store: child entry could not be removed from '%s'" , + node->name); + +return off - len -1 ; +}/** * A node has a children field that names the children of the node, separated
@@ -2377,12 +2390,7 @@ static int check_store_(const char *name, struct hashtable *reachable)if (hashtable_search (children, childname)) {log ("check_store: '%s' is duplicated!" , childname); - -if (recovery) { -remove_child_entry (NULL , node, - i); - i -= childlen +1 ; - } + i =rm_child_entry (node, i, childlen); } else { if (!remember_string(children,
@@ -2399,11 +2407,7 @@ static int check_store_(const char *name, struct hashtable *reachable) }else if (errno !=ENOMEM ) {log ("check_store: No child '%s' found!\n" , childname); - -if (recovery) { -remove_child_entry (NULL , node, i); - i -= childlen +1 ; - } + i =rm_child_entry (node, i, childlen); }else {log ("check_store: ENOMEM" ); ret =ENOMEM ;
From aac9b51b6fbbbd16c910f69365345528c5bec106 Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Tue, 13 Sep 2022 07:35:11 +0200
Subject: tools/xenstore: add generic treewalk function
Add a generic function to walk the complete node tree. It will start at "/" and descend recursively into each child, calling a function specified by the caller. Depending on the return value of the user specified function the walk will be aborted, continued, or the current child will be skipped by not descending into its children.
This is part of XSA-418 / CVE-2022-42321.
tools/xenstore/xenstored_core.c +130 -13
161 lines not on this page · raw message
tools/xenstore/xenstored_core.h +40 -0
@@ -202,6 +202,7 @@ struct node {/* Children, each nul-terminated. */ unsigned int childlen; +unsigned int childoff;/* Used by walk_node_tree() internally. */ char *children;/* Allocation information for node currently in store. */
@@ -338,6 +339,45 @@ void read_state_buffered_data(const void *ctx, struct connection *conn, const struct xs_state_connection *sc);void read_state_node (const void *ctx,const void *state); +/* +* Walk the node tree below root calling funcs->enter() and funcs->exit() for +* each node. funcs->enter() is being called when entering a node, so before +* any of the children of the node is processed. funcs->exit() is being +* called when leaving the node, so after all children have been processed. +* funcs->enoent() is being called when a node isn't existing. +* funcs->*() return values: +* < 0: tree walk is stopped, walk_node_tree() returns funcs->*() return value +* in case WALK_TREE_ERROR_STOP is returned, errno should be set +* WALK_TREE_OK: tree walk is continuing +* WALK_TREE_SKIP_CHILDREN: tree walk won't descend below current node, but +* walk continues +* WALK_TREE_RM_CHILDENTRY: Remove the child entry from its parent and write +* the modified parent node back to the data base, implies to not descend +* below the current node, but to continue the walk +* funcs->*() is allowed to modify the node it is called for in the data base. +* In case funcs->enter() is deleting the node, it must not return WALK_TREE_OK +* in order to avoid descending into no longer existing children. +*/ +/* Return values for funcs->*() and walk_node_tree(). */ +#define WALK_TREE_SUCCESS_STOP -100/* Stop walk early, no error. */ +#define WALK_TREE_ERROR_STOP -1/* Stop walk due to error. */ +#define WALK_TREE_OK 0/* No error. */ +/* Return value for funcs->*() only. */ +#define WALK_TREE_SKIP_CHILDREN 1/* Don't recurse below current node. */ +#define WALK_TREE_RM_CHILDENTRY 2/* Remove child entry from parent. */ + +struct walk_funcs { +int (*enter)(const void *ctx,struct connection *conn, +struct node *node,void *arg); +int (*exit)(const void *ctx,struct connection *conn, +struct node *node,void *arg); +int (*enoent)(const void *ctx,struct connection *conn, +struct node *parent,char *name,void *arg); +}; + +int walk_node_tree (const void *ctx,struct connection *conn,const char *root, +struct walk_funcs *funcs,void *arg); +#endif /* _XENSTORED_CORE_H */ /*
From bdc931fb5dcebbd8d0e44b5d8bd3fb9106ee8596 Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Tue, 13 Sep 2022 07:35:12 +0200
Subject: tools/xenstore: simplify check_store()
check_store() is using a hash table for storing all node names it has found via walking the tree. Additionally it using another hash table for all children of a node to detect duplicate child names.
Simplify that by dropping the second hash table as the first one is already holding all the needed information.
This is part of XSA-418 / CVE-2022-42321.
tools/xenstore/xenstored_core.c +15 -32
84 lines not on this page · raw message
From 27817f0a7d6802be04e8f43a0900b02f881b28b2 Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Tue, 13 Sep 2022 07:35:12 +0200
Subject: tools/xenstore: use treewalk for check_store()
Instead of doing an open tree walk using call recursion, use walk_node_tree() when checking the store for inconsistencies.
This will reduce code size and avoid many nesting levels of function calls which could potentially exhaust the stack.
This is part of XSA-418 / CVE-2022-42321.
tools/xenstore/xenstored_core.c +28 -77
140 lines not on this page · raw message
From 6ea0ffbd88b11f23779d763501ec1370b590bb2a Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Tue, 13 Sep 2022 07:35:12 +0200
Subject: tools/xenstore: use treewalk for deleting nodes
Instead of doing an open tree walk using call recursion, use walk_node_tree() when deleting a sub-tree of nodes.
This will reduce code size and avoid many nesting levels of function calls which could potentially exhaust the stack.
This is part of XSA-418 / CVE-2022-42321.
tools/xenstore/xenstored_core.c +43 -56
149 lines not on this page · raw message
From 1ee281b18b52bec87335ea64ee74cc159e63d036 Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Tue, 13 Sep 2022 07:35:12 +0200
Subject: tools/xenstore: use treewalk for creating node records
Instead of doing an open tree walk using call recursion, use walk_node_tree() when creating the node records during a live update.
This will reduce code size and avoid many nesting levels of function calls which could potentially exhaust the stack.
This is part of XSA-418 / CVE-2022-42321.
tools/xenstore/xenstored_core.c +40 -65
119 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
From ffd1ba6b97f24c3bf29b49557c137d70a3819d07 Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Tue, 13 Sep 2022 07:35:12 +0200
Subject: tools/xenstore: simplify check_store()
check_store() is using a hash table for storing all node names it has found via walking the tree. Additionally it using another hash table for all children of a node to detect duplicate child names.
Simplify that by dropping the second hash table as the first one is already holding all the needed information.
This is part of XSA-418 / CVE-2022-42321.
tools/xenstore/xenstored_core.c +15 -32
47 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
From 47854a52eb8a5c31e6c0c150a87c86787ecde1c6 Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Tue, 13 Sep 2022 07:35:12 +0200
Subject: tools/xenstore: use treewalk for check_store()
Instead of doing an open tree walk using call recursion, use walk_node_tree() when checking the store for inconsistencies.
This will reduce code size and avoid many nesting levels of function calls which could potentially exhaust the stack.
This is part of XSA-418 / CVE-2022-42321.
tools/xenstore/xenstored_core.c +26 -80
106 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
From 06c89dd48c08f1eaa2e5bcc1407554ee3e0b4e27 Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Tue, 13 Sep 2022 07:35:12 +0200
Subject: tools/xenstore: use treewalk for deleting nodes
Instead of doing an open tree walk using call recursion, use walk_node_tree() when deleting a sub-tree of nodes.
This will reduce code size and avoid many nesting levels of function calls which could potentially exhaust the stack.
This is part of XSA-418 / CVE-2022-42321.
tools/xenstore/xenstored_core.c +43 -56
99 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
From 742b8b3e4c2f775e47819754ca914e61aaa078c3 Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Tue, 13 Sep 2022 07:35:12 +0200
Subject: tools/xenstore: use treewalk for creating node records
Instead of doing an open tree walk using call recursion, use walk_node_tree() when creating the node records during a live update.
This will reduce code size and avoid many nesting levels of function calls which could potentially exhaust the stack.
This is part of XSA-418 / CVE-2022-42321.
tools/xenstore/xenstored_core.c +40 -65
105 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
Part of the diff is not shown here. View the raw message.