Skip to contentSkip to conversation

tools/xenstore: fix quota check in acc_fix_domains()

PATCH In review Juergen Gross 3 participants 4 messages Mar 28, 2023 – Apr 24, 2023

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 tools/xenstore: fix quota check in acc_fix_domains()

Today when finalizing a transaction the number of node quota is checked to not being exceeded after the transaction. This check is always done, even if the transaction is being performed by a privileged connection, or if there were no nodes created in the transaction.

Correct that by checking quota only if:
- the transaction is being performed by an unprivileged guest, and
- at least one node was created in the transaction

Fixes f2bebf72c4d5 ("xenstore: rework of transaction handling")

---
V2:
- add comment (Julien Grall)

---
 tools/xenstore/xenstored_core.c        |  3 +++
 tools/xenstore/xenstored_domain.c      |  4 ++--
 tools/xenstore/xenstored_domain.h      |  7 ++++++-
 tools/xenstore/xenstored_transaction.c | 16 ++++++++++++++--
 tools/xenstore/xenstored_transaction.h |  3 +++
 5 files changed, 28 insertions(+), 5 deletions(-)
tools/xenstore/xenstored_core.c +3 -0
index a61db2db2d..3ca68681e3 100644
@@ -1472,6 +1472,9 @@ static struct node *create_node(struct connection *conn, const void *ctx,
 	if (!node)
 		return NULL;
 
+	if (conn && conn->transaction)
+		ta_node_created(conn->transaction);
+
 	node->data = data;
 	node->datalen = datalen;
 
tools/xenstore/xenstored_domain.c +2 -2
index d7fc2fafc7..f62be2245c 100644
@@ -544,7 +544,7 @@ static struct domain *find_domain_by_domid(unsigned int domid)
 	return (d && d->introduced) ? d : NULL;
 }
 
-int acc_fix_domains(struct list_head *head, bool update)
+int acc_fix_domains(struct list_head *head, bool chk_quota, bool update)
 {
 	struct changed_domain *cd;
 	int cnt;
@@ -552,7 +552,7 @@ int acc_fix_domains(struct list_head *head, bool update)
 	list_for_each_entry(cd, head, list) {
 		cnt = domain_nbentry_fix(cd->domid, cd->nbentry, update);
 		if (!update) {
-			if (cnt >= quota_nb_entry_per_domain)
+			if (chk_quota && cnt >= quota_nb_entry_per_domain)
 				return ENOSPC;
 			if (cnt < 0)
 				return ENOMEM;
tools/xenstore/xenstored_domain.h +6 -1
index dc4660861e..279cccb3ad 100644
@@ -96,7 +96,12 @@ void domain_outstanding_dec(struct connection *conn);
 void domain_outstanding_domid_dec(unsigned int domid);
 int domain_get_quota(const void *ctx, struct connection *conn,
 		     unsigned int domid);
-int acc_fix_domains(struct list_head *head, bool update);
+
+/*
+ * Update or check number of nodes per domain at the end of a transaction.
+ * If "update" is true, "chk_quota" is ignored.
+ */
+int acc_fix_domains(struct list_head *head, bool chk_quota, bool update);
 
 /* Write rate limiting */
 
tools/xenstore/xenstored_transaction.c +14 -2
index 1aa9d3cb3d..2b15506953 100644
@@ -160,12 +160,20 @@ struct transaction
 	/* List of changed domains - to record the changed domain entry number */
 	struct list_head changed_domains;
 
+	/* There was at least one node created in the transaction. */
+	bool node_created;
+
 	/* Flag for letting transaction fail. */
 	bool fail;
 };
 
 uint64_t generation;
 
+void ta_node_created(struct transaction *trans)
+{
+	trans->node_created = true;
+}
+
 static struct accessed_node *find_accessed_node(struct transaction *trans,
 						const char *name)
 {
@@ -509,6 +517,7 @@ int do_transaction_end(const void *ctx, struct connection *conn,
 	const char *arg = onearg(in);
 	struct transaction *trans;
 	bool is_corrupt = false;
+	bool chk_quota;
 	int ret;
 
 	if (!arg || (!streq(arg, "T") && !streq(arg, "F")))
@@ -523,13 +532,16 @@ int do_transaction_end(const void *ctx, struct connection *conn,
 	if (!conn->transaction_started)
 		conn->ta_start_time = 0;
 
+	chk_quota = trans->node_created && domain_is_unprivileged(conn);
+
 	/* Attach transaction to ctx for auto-cleanup */
 	talloc_steal(ctx, trans);
 
 	if (streq(arg, "T")) {
 		if (trans->fail)
 			return ENOMEM;
-		ret = acc_fix_domains(&trans->changed_domains, false);
+		ret = acc_fix_domains(&trans->changed_domains, chk_quota,
+				      false);
 		if (ret)
 			return ret;
 		ret = finalize_transaction(conn, trans, &is_corrupt);
@@ -539,7 +551,7 @@ int do_transaction_end(const void *ctx, struct connection *conn,
 		wrl_apply_debit_trans_commit(conn);
 
 		/* fix domain entry for each changed domain */
-		acc_fix_domains(&trans->changed_domains, true);
+		acc_fix_domains(&trans->changed_domains, false, true);
 
 		if (is_corrupt)
 			corrupt(conn, "transaction inconsistency");
tools/xenstore/xenstored_transaction.h +3 -0
index b6f8cb7d0a..883145163f 100644
@@ -36,6 +36,9 @@ int do_transaction_end(const void *ctx, struct connection *conn,
 
 struct transaction *transaction_lookup(struct connection *conn, uint32_t id);
 
+/* Set flag for created node. */
+void ta_node_created(struct transaction *trans);
+
 /* This node was accessed. */
 int __must_check access_node(struct connection *conn, struct node *node,
                              enum node_access_type type, TDB_DATA *key);
Signature · 1 line
2.35.3
Reviewed-by on v2 1/1
#1 lore raw

Hi Juergen,

Quoted 5 lines · Juergen Gross

Today when finalizing a transaction the number of node quota is checked to not being exceeded after the transaction. This check is always done, even if the transaction is being performed by a privileged connection, or if there were no nodes created in the transaction.

Quoted 7 lines

Correct that by checking quota only if:
- the transaction is being performed by an unprivileged guest, and
- at least one node was created in the transaction

Fixes f2bebf72c4d5 ("xenstore: rework of transaction handling")

And committed.

Cheers,

Quoted 139 lines

---
V2:
- add comment (Julien Grall)

---
  tools/xenstore/xenstored_core.c        |  3 +++
  tools/xenstore/xenstored_domain.c      |  4 ++--
  tools/xenstore/xenstored_domain.h      |  7 ++++++-
  tools/xenstore/xenstored_transaction.c | 16 ++++++++++++++--
  tools/xenstore/xenstored_transaction.h |  3 +++
  5 files changed, 28 insertions(+), 5 deletions(-)
tools/xenstore/xenstored_core.c +3 -0
index a61db2db2d..3ca68681e3 100644
@@ -1472,6 +1472,9 @@ static struct node *create_node(struct connection *conn, const void *ctx,
  	if (!node)
  		return NULL;
 
+	if (conn && conn->transaction)
+		ta_node_created(conn->transaction);
+
  	node->data = data;
  	node->datalen = datalen;
 
tools/xenstore/xenstored_domain.c +2 -2
index d7fc2fafc7..f62be2245c 100644
@@ -544,7 +544,7 @@ static struct domain *find_domain_by_domid(unsigned int domid)
  	return (d && d->introduced) ? d : NULL;
  }
 
-int acc_fix_domains(struct list_head *head, bool update)
+int acc_fix_domains(struct list_head *head, bool chk_quota, bool update)
  {
  	struct changed_domain *cd;
  	int cnt;
@@ -552,7 +552,7 @@ int acc_fix_domains(struct list_head *head, bool update)
  	list_for_each_entry(cd, head, list) {
  		cnt = domain_nbentry_fix(cd->domid, cd->nbentry, update);
  		if (!update) {
-			if (cnt >= quota_nb_entry_per_domain)
+			if (chk_quota && cnt >= quota_nb_entry_per_domain)
  				return ENOSPC;
  			if (cnt < 0)
  				return ENOMEM;
tools/xenstore/xenstored_domain.h +6 -1
index dc4660861e..279cccb3ad 100644
@@ -96,7 +96,12 @@ void domain_outstanding_dec(struct connection *conn);
  void domain_outstanding_domid_dec(unsigned int domid);
  int domain_get_quota(const void *ctx, struct connection *conn,
  		     unsigned int domid);
-int acc_fix_domains(struct list_head *head, bool update);
+
+/*
+ * Update or check number of nodes per domain at the end of a transaction.
+ * If "update" is true, "chk_quota" is ignored.
+ */
+int acc_fix_domains(struct list_head *head, bool chk_quota, bool update);
 
  /* Write rate limiting */
 
tools/xenstore/xenstored_transaction.c +14 -2
index 1aa9d3cb3d..2b15506953 100644
@@ -160,12 +160,20 @@ struct transaction
  	/* List of changed domains - to record the changed domain entry number */
  	struct list_head changed_domains;
 
+	/* There was at least one node created in the transaction. */
+	bool node_created;
+
  	/* Flag for letting transaction fail. */
  	bool fail;
  };
 
  uint64_t generation;
 
+void ta_node_created(struct transaction *trans)
+{
+	trans->node_created = true;
+}
+
  static struct accessed_node *find_accessed_node(struct transaction *trans,
  						const char *name)
  {
@@ -509,6 +517,7 @@ int do_transaction_end(const void *ctx, struct connection *conn,
  	const char *arg = onearg(in);
  	struct transaction *trans;
  	bool is_corrupt = false;
+	bool chk_quota;
  	int ret;
 
  	if (!arg || (!streq(arg, "T") && !streq(arg, "F")))
@@ -523,13 +532,16 @@ int do_transaction_end(const void *ctx, struct connection *conn,
  	if (!conn->transaction_started)
  		conn->ta_start_time = 0;
 
+	chk_quota = trans->node_created && domain_is_unprivileged(conn);
+
  	/* Attach transaction to ctx for auto-cleanup */
  	talloc_steal(ctx, trans);
 
  	if (streq(arg, "T")) {
  		if (trans->fail)
  			return ENOMEM;
-		ret = acc_fix_domains(&trans->changed_domains, false);
+		ret = acc_fix_domains(&trans->changed_domains, chk_quota,
+				      false);
  		if (ret)
  			return ret;
  		ret = finalize_transaction(conn, trans, &is_corrupt);
@@ -539,7 +551,7 @@ int do_transaction_end(const void *ctx, struct connection *conn,
  		wrl_apply_debit_trans_commit(conn);
 
  		/* fix domain entry for each changed domain */
-		acc_fix_domains(&trans->changed_domains, true);
+		acc_fix_domains(&trans->changed_domains, false, true);
 
  		if (is_corrupt)
  			corrupt(conn, "transaction inconsistency");
tools/xenstore/xenstored_transaction.h +3 -0
index b6f8cb7d0a..883145163f 100644
@@ -36,6 +36,9 @@ int do_transaction_end(const void *ctx, struct connection *conn,
 
  struct transaction *transaction_lookup(struct connection *conn, uint32_t id);
 
+/* Set flag for created node. */
+void ta_node_created(struct transaction *trans);
+
  /* This node was accessed. */
  int __must_check access_node(struct connection *conn, struct node *node,
                               enum node_access_type type, TDB_DATA *key);
Signature · 1 line
Julien Grall
#2 lore raw
In reply to Juergen Gross
Quoted 5 lines · Juergen Gross

Today when finalizing a transaction the number of node quota is checked to not being exceeded after the transaction. This check is always done, even if the transaction is being performed by a privileged connection, or if there were no nodes created in the transaction.

Quoted 7 lines

Correct that by checking quota only if:
- the transaction is being performed by an unprivileged guest, and
- at least one node was created in the transaction

Fixes f2bebf72c4d5 ("xenstore: rework of transaction handling")

Considering the referenced commit is about 6 years old, I thought this would be a backporting candidate. The function mentioned in the title doesn't appear to exist on 4.17, though. Therefore, if there is an intention for this to be backported, may I please ask that a suitable backport be provided?

Thanks, Jan

#3 lore raw
Quoted 14 lines · Jan Beulich
Quoted 5 lines · Juergen Gross

Today when finalizing a transaction the number of node quota is checked to not being exceeded after the transaction. This check is always done, even if the transaction is being performed by a privileged connection, or if there were no nodes created in the transaction.

Quoted 7 lines

Correct that by checking quota only if:
- the transaction is being performed by an unprivileged guest, and
- at least one node was created in the transaction

Fixes f2bebf72c4d5 ("xenstore: rework of transaction handling")
Quoted 5 lines

Considering the referenced commit is about 6 years old, I thought this would be a backporting candidate. The function mentioned in the title doesn't appear to exist on 4.17, though. Therefore, if there is an intention for this to be backported, may I please ask that a suitable backport be provided?

This should do the job.

Juergen

From 1e308552989dd3c371d5ff27d8e615d07ca9847d Mon Sep 17 00:00:00 2001
From: Juergen Gross <[email protected]>
Date: Mon, 24 Apr 2023 11:41:26 +0200
Subject: [PATCH] tools/xenstore: fix quota check in acc_fix_domains()

Today when finalizing a transaction the number of node quota is checked to not being exceeded after the transaction. This check is always done, even if the transaction is being performed by a privileged connection, or if there were no nodes created in the transaction.

Correct that by checking quota only if:
- the transaction is being performed by an unprivileged guest, and
- at least one node was created in the transaction

Commit f6b801c36bd5e4ab22a9f80c8d57121b62b139af upstream.

Fixes f2bebf72c4d5 ("xenstore: rework of transaction handling")
---
 tools/xenstore/xenstored_core.c        |  3 +++
 tools/xenstore/xenstored_transaction.c | 24 ++++++++++++++++++++----
 tools/xenstore/xenstored_transaction.h |  3 +++
 3 files changed, 26 insertions(+), 4 deletions(-)
tools/xenstore/xenstored_core.c +3 -0
index 56dbdc2530..8907c6cb0b 100644
@@ -1474,6 +1474,9 @@ static struct node *create_node(struct connection *conn, const void *ctx,
 	if (!node)
 		return NULL;
 
+	if (conn && conn->transaction)
+		ta_node_created(conn->transaction);
+
 	node->data = data;
 	node->datalen = datalen;
 
tools/xenstore/xenstored_transaction.c +20 -4
index ac854197ca..8ebd16aed2 100644
@@ -172,12 +172,20 @@ struct transaction
 	/* List of changed domains - to record the changed domain entry number */
 	struct list_head changed_domains;
 
+	/* There was at least one node created in the transaction. */
+	bool node_created;
+
 	/* Flag for letting transaction fail. */
 	bool fail;
 };
 
 uint64_t generation;
 
+void ta_node_created(struct transaction *trans)
+{
+	trans->node_created = true;
+}
+
 static struct accessed_node *find_accessed_node(struct transaction *trans,
 						const char *name)
 {
@@ -514,7 +522,12 @@ int do_transaction_start(const void *ctx, struct connection *conn,
 	return 0;
 }
 
-static int transaction_fix_domains(struct transaction *trans, bool update)
+/*
+ * Update or check number of nodes per domain at the end of a transaction.
+ * If "update" is true, "chk_quota" is ignored.
+ */
+static int transaction_fix_domains(struct transaction *trans, bool chk_quota,
+				   bool update)
 {
 	struct changed_domain *d;
 	int cnt;
@@ -522,7 +535,7 @@ static int transaction_fix_domains(struct transaction *trans, bool update)
 	list_for_each_entry(d, &trans->changed_domains, list) {
 		cnt = domain_entry_fix(d->domid, d->nbentry, update);
 		if (!update) {
-			if (cnt >= quota_nb_entry_per_domain)
+			if (chk_quota && cnt >= quota_nb_entry_per_domain)
 				return ENOSPC;
 			if (cnt < 0)
 				return ENOMEM;
@@ -538,6 +551,7 @@ int do_transaction_end(const void *ctx, struct connection *conn,
 	const char *arg = onearg(in);
 	struct transaction *trans;
 	bool is_corrupt = false;
+	bool chk_quota;
 	int ret;
 
 	if (!arg || (!streq(arg, "T") && !streq(arg, "F")))
@@ -552,13 +566,15 @@ int do_transaction_end(const void *ctx, struct connection *conn,
 	if (!conn->transaction_started)
 		conn->ta_start_time = 0;
 
+	chk_quota = trans->node_created && domain_is_unprivileged(conn);
+
 	/* Attach transaction to ctx for auto-cleanup */
 	talloc_steal(ctx, trans);
 
 	if (streq(arg, "T")) {
 		if (trans->fail)
 			return ENOMEM;
-		ret = transaction_fix_domains(trans, false);
+		ret = transaction_fix_domains(trans, chk_quota, false);
 		if (ret)
 			return ret;
 		ret = finalize_transaction(conn, trans, &is_corrupt);
@@ -568,7 +584,7 @@ int do_transaction_end(const void *ctx, struct connection *conn,
 		wrl_apply_debit_trans_commit(conn);
 
 		/* fix domain entry for each changed domain */
-		transaction_fix_domains(trans, true);
+		transaction_fix_domains(trans, false, true);
 
 		if (is_corrupt)
 			corrupt(conn, "transaction inconsistency");
tools/xenstore/xenstored_transaction.h +3 -0
index 3417303f94..f57d204cc0 100644
@@ -36,6 +36,9 @@ int do_transaction_end(const void *ctx, struct connection *conn,
 
 struct transaction *transaction_lookup(struct connection *conn, uint32_t id);
 
+/* Set flag for created node. */
+void ta_node_created(struct transaction *trans);
+
 /* inc/dec entry number local to trans while changing a node */
 void transaction_entry_inc(struct transaction *trans, unsigned int domid);
 void transaction_entry_dec(struct transaction *trans, unsigned int domid);
Signature · 1 line
2.35.3