]> xenbits.xensource.com Git - xen.git/commitdiff
tools/xenstore: Fail a transaction if it is not possible to create a node
authorJulien Grall <jgrall@amazon.com>
Tue, 13 Sep 2022 05:35:06 +0000 (07:35 +0200)
committerAndrew Cooper <andrew.cooper3@citrix.com>
Tue, 1 Nov 2022 15:25:15 +0000 (15:25 +0000)
Commit f2bebf72c4d5 "xenstore: rework of transaction handling" moved
out from copying the entire database everytime a new transaction is
opened to track the list of nodes changed.

The content of all the nodes accessed during a transaction will be
temporarily stored in TDB using a different key.

The function create_node() may write/update multiple nodes if the child
doesn't exist. In case of a failure, the function will revert any
changes (this include any update to TDB). Unfortunately, the function
which reverts the changes (i.e. destroy_node()) will not use the correct
key to delete any update or even request the transaction to fail.

This means that if a client decide to go ahead with committing the
transaction, orphan nodes will be created because they were not linked
to an existing node (create_node() will write the nodes backwards).

Once some nodes have been partially updated in a transaction, it is not
easily possible to undo any changes. So rather than continuing and hit
weird issue while committing, it is much saner to fail the transaction.

This will have an impact on any client that decides to commit even if it
can't write a node. Although, it is not clear why a normal client would
want to do that...

Lastly, update destroy_node() to use the correct key for deleting the
node. Rather than recreating it (this will allocate memory and
therefore fail), stash the key in the structure node.

This is XSA-415 / CVE-2022-42310.

Signed-off-by: Julien Grall <jgrall@amazon.com>
Reviewed-by: Juergen Gross <jgross@suse.com>
(cherry picked from commit 5d71766bd1a4a3a8b2fe952ca2be80e02fe48f34)

tools/xenstore/xenstored_core.c
tools/xenstore/xenstored_core.h
tools/xenstore/xenstored_transaction.c
tools/xenstore/xenstored_transaction.h

index c952608468259ec0c076246d0b39b7601fe5a3d2..e0d6d23f3b769f542292bf939c105c3ea7e3e7ce 100644 (file)
@@ -466,15 +466,17 @@ int write_node_raw(struct connection *conn, TDB_DATA *key, struct node *node,
        return 0;
 }
 
+/*
+ * Write the node. If the node is written, caller can find the key used in
+ * node->key. This can later be used if the change needs to be reverted.
+ */
 static int write_node(struct connection *conn, struct node *node,
                      bool no_quota_check)
 {
-       TDB_DATA key;
-
-       if (access_node(conn, node, NODE_ACCESS_WRITE, &key))
+       if (access_node(conn, node, NODE_ACCESS_WRITE, &node->key))
                return errno;
 
-       return write_node_raw(conn, &key, node, no_quota_check);
+       return write_node_raw(conn, &node->key, node, no_quota_check);
 }
 
 enum xs_perm_type perm_for_conn(struct connection *conn,
@@ -977,18 +979,21 @@ nomem:
 
 static int destroy_node(struct connection *conn, struct node *node)
 {
-       TDB_DATA key;
-
        if (streq(node->name, "/"))
                corrupt(NULL, "Destroying root node!");
 
-       key.dptr = (void *)node->name;
-       key.dsize = strlen(node->name);
-
-       tdb_delete(tdb_ctx, key);
+       tdb_delete(tdb_ctx, node->key);
 
        domain_entry_dec(conn, node);
 
+       /*
+        * It is not possible to easily revert the changes in a transaction.
+        * So if the failure happens in a transaction, mark it as fail to
+        * prevent any commit.
+        */
+       if ( conn->transaction )
+               fail_transaction(conn->transaction);
+
        return 0;
 }
 
index 196a6fd2b0bed378866283dd421098bd2fdc01cc..9369c4cbfd2639cf4e2b8fd5b60228bdb383cb6c 100644 (file)
@@ -119,6 +119,8 @@ struct node_perms {
 
 struct node {
        const char *name;
+       /* Key used to update TDB */
+       TDB_DATA key;
 
        /* Parent (optional) */
        struct node *parent;
index 2881f3b2e44d31cf511c9bbc91f0015b867f5ac6..4ffa18311120d5f940ea8ea3d327780156ab8688 100644 (file)
@@ -582,6 +582,11 @@ void transaction_entry_dec(struct transaction *trans, unsigned int domid)
        list_add_tail(&d->list, &trans->changed_domains);
 }
 
+void fail_transaction(struct transaction *trans)
+{
+       trans->fail = true;
+}
+
 void conn_delete_all_transactions(struct connection *conn)
 {
        struct transaction *trans;
index 43a162bea3f36e0e87b530e86d797f7656374690..14062730e3c99021854fe249bd02697fdaf5fec4 100644 (file)
@@ -46,6 +46,9 @@ int access_node(struct connection *conn, struct node *node,
 int transaction_prepend(struct connection *conn, const char *name,
                         TDB_DATA *key);
 
+/* Mark the transaction as failed. This will prevent it to be committed. */
+void fail_transaction(struct transaction *trans);
+
 void conn_delete_all_transactions(struct connection *conn);
 int check_transactions(struct hashtable *hash);