All of lore.kernel.org
 help / color / mirror / Atom feed
From: Juergen Gross <jgross@suse.com>
To: xen-devel@lists.xenproject.org
Cc: Juergen Gross <jgross@suse.com>, Wei Liu <wl@xen.org>,
	Julien Grall <julien@xen.org>,
	Anthony PERARD <anthony.perard@citrix.com>
Subject: [PATCH v3 24/25] tools/xenstore: rework get_node()
Date: Mon, 24 Jul 2023 13:02:46 +0200	[thread overview]
Message-ID: <20230724110247.10520-25-jgross@suse.com> (raw)
In-Reply-To: <20230724110247.10520-1-jgross@suse.com>

Today get_node_canonicalized() is the only caller of get_node().

In order to prepare introducing a get_node() variant returning a
pointer to const struct node, do the following restructuring:

- move the call of read_node() from get_node() into
  get_node_canonicalized()

- rename get_node() to get_node_chk_perm()

- rename get_node_canonicalized() to get_node()

Signed-off-by: Juergen Gross <jgross@suse.com>
---
V3:
- new patch
---
 tools/xenstore/xenstored_core.c | 57 +++++++++++++++------------------
 1 file changed, 25 insertions(+), 32 deletions(-)

diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_core.c
index ec20bc042d..fa07bc0c31 100644
--- a/tools/xenstore/xenstored_core.c
+++ b/tools/xenstore/xenstored_core.c
@@ -996,27 +996,26 @@ static int errno_from_parents(struct connection *conn, const void *ctx,
  * If it fails, returns NULL and sets errno.
  * Temporary memory allocations are done with ctx.
  */
-static struct node *get_node(struct connection *conn,
-			     const void *ctx,
-			     const char *name,
-			     unsigned int perm)
+static bool get_node_chk_perm(struct connection *conn, const void *ctx,
+			      const struct node *node, const char *name,
+			      unsigned int perm)
 {
-	struct node *node;
 	struct node_perms perms;
+	bool err = false;
 
-	node = read_node(conn, ctx, name);
 	/* If we don't have permission, we don't have node. */
 	if (node) {
 		node_to_node_perms(node, &perms);
 		if ((perm_for_conn(conn, &perms) & perm) != perm) {
 			errno = EACCES;
-			node = NULL;
+			err = true;
 		}
 	}
 	/* Clean up errno if they weren't supposed to know. */
-	if (!node && !read_node_can_propagate_errno())
+	if (err && !read_node_can_propagate_errno())
 		errno = errno_from_parents(conn, ctx, name, errno, perm);
-	return node;
+
+	return err;
 }
 
 static struct buffered_data *new_buffer(void *ctx)
@@ -1285,14 +1284,12 @@ const char *canonicalize(struct connection *conn, const void *ctx,
 	return name;
 }
 
-static struct node *get_node_canonicalized(struct connection *conn,
-					   const void *ctx,
-					   const char *name,
-					   const char **canonical_name,
-					   unsigned int perm,
-					   bool allow_special)
+static struct node *get_node(struct connection *conn, const void *ctx,
+			     const char *name, const char **canonical_name,
+			     unsigned int perm, bool allow_special)
 {
 	const char *tmp_name;
+	struct node *node;
 
 	if (!canonical_name)
 		canonical_name = &tmp_name;
@@ -1300,7 +1297,10 @@ static struct node *get_node_canonicalized(struct connection *conn,
 	if (!*canonical_name)
 		return NULL;
 
-	return get_node(conn, ctx, *canonical_name, perm);
+	node = read_node(conn, ctx, *canonical_name);
+
+	return get_node_chk_perm(conn, ctx, node, *canonical_name, perm)
+	       ? NULL : node;
 }
 
 static int send_directory(const void *ctx, struct connection *conn,
@@ -1308,8 +1308,7 @@ static int send_directory(const void *ctx, struct connection *conn,
 {
 	struct node *node;
 
-	node = get_node_canonicalized(conn, ctx, onearg(in), NULL,
-				      XS_PERM_READ, false);
+	node = get_node(conn, ctx, onearg(in), NULL, XS_PERM_READ, false);
 	if (!node)
 		return errno;
 
@@ -1330,8 +1329,7 @@ static int send_directory_part(const void *ctx, struct connection *conn,
 		return EINVAL;
 
 	/* First arg is node name. */
-	node = get_node_canonicalized(conn, ctx, in->buffer, NULL,
-				      XS_PERM_READ, false);
+	node = get_node(conn, ctx, in->buffer, NULL, XS_PERM_READ, false);
 	if (!node)
 		return errno;
 
@@ -1380,8 +1378,7 @@ static int do_read(const void *ctx, struct connection *conn,
 {
 	struct node *node;
 
-	node = get_node_canonicalized(conn, ctx, onearg(in), NULL,
-				      XS_PERM_READ, false);
+	node = get_node(conn, ctx, onearg(in), NULL, XS_PERM_READ, false);
 	if (!node)
 		return errno;
 
@@ -1595,8 +1592,7 @@ static int do_write(const void *ctx, struct connection *conn,
 	offset = strlen(vec[0]) + 1;
 	datalen = in->used - offset;
 
-	node = get_node_canonicalized(conn, ctx, vec[0], &name, XS_PERM_WRITE,
-				      false);
+	node = get_node(conn, ctx, vec[0], &name, XS_PERM_WRITE, false);
 	if (!node) {
 		/* No permissions, invalid input? */
 		if (errno != ENOENT)
@@ -1624,8 +1620,7 @@ static int do_mkdir(const void *ctx, struct connection *conn,
 	struct node *node;
 	const char *name;
 
-	node = get_node_canonicalized(conn, ctx, onearg(in), &name,
-				      XS_PERM_WRITE, false);
+	node = get_node(conn, ctx, onearg(in), &name, XS_PERM_WRITE, false);
 
 	/* If it already exists, fine. */
 	if (!node) {
@@ -1754,8 +1749,7 @@ static int do_rm(const void *ctx, struct connection *conn,
 	const char *name;
 	char *parentname;
 
-	node = get_node_canonicalized(conn, ctx, onearg(in), &name,
-				      XS_PERM_WRITE, false);
+	node = get_node(conn, ctx, onearg(in), &name, XS_PERM_WRITE, false);
 	if (!node) {
 		/* Didn't exist already?  Fine, if parent exists. */
 		if (errno == ENOENT) {
@@ -1797,8 +1791,7 @@ static int do_get_perms(const void *ctx, struct connection *conn,
 	unsigned int len;
 	struct node_perms perms;
 
-	node = get_node_canonicalized(conn, ctx, onearg(in), NULL, XS_PERM_READ,
-				      true);
+	node = get_node(conn, ctx, onearg(in), NULL, XS_PERM_READ, true);
 	if (!node)
 		return errno;
 
@@ -1842,8 +1835,8 @@ static int do_set_perms(const void *ctx, struct connection *conn,
 		return ENOENT;
 
 	/* We must own node to do this (tools can do this too). */
-	node = get_node_canonicalized(conn, ctx, in->buffer, &name,
-				      XS_PERM_WRITE | XS_PERM_OWNER, true);
+	node = get_node(conn, ctx, in->buffer, &name,
+			XS_PERM_WRITE | XS_PERM_OWNER, true);
 	if (!node)
 		return errno;
 
-- 
2.35.3



  parent reply	other threads:[~2023-07-24 11:08 UTC|newest]

Thread overview: 84+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-24 11:02 [PATCH v3 00/25] tools/xenstore: drop TDB Juergen Gross
2023-07-24 11:02 ` [PATCH v3 01/25] tools/xenstore: explicitly specify create or modify for tdb_store() Juergen Gross
2023-07-25 16:01   ` Julien Grall
2023-07-24 11:02 ` [PATCH v3 02/25] tools/xenstore: replace key in struct node with data base name Juergen Gross
2023-07-24 11:02 ` [PATCH v3 03/25] tools/xenstore: let transaction_prepend() return the name for access Juergen Gross
2023-07-24 11:02 ` [PATCH v3 04/25] tools/xenstore: rename do_tdb_delete() and change parameter type Juergen Gross
2023-07-24 11:02 ` [PATCH v3 05/25] tools/xenstore: rename do_tdb_write() " Juergen Gross
2023-07-24 11:02 ` [PATCH v3 06/25] tools/xenstore: switch get_acc_data() to use name instead of key Juergen Gross
2023-07-24 11:02 ` [PATCH v3 07/25] tools/xenstore: add wrapper for tdb_fetch() Juergen Gross
2023-07-24 11:02 ` [PATCH v3 08/25] tools/xenstore: make hashtable key and value parameters const Juergen Gross
2023-07-25 16:08   ` Julien Grall
2023-07-26  6:19     ` Juergen Gross
2023-07-26  8:20       ` Julien Grall
2023-07-26  8:44         ` Juergen Gross
2023-07-26  9:29           ` Julien Grall
2023-07-26 11:07             ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 09/25] tools/xenstore: let hashtable_add() fail in case of existing entry Juergen Gross
2023-07-24 11:02 ` [PATCH v3 10/25] tools/xenstore: add hashtable_replace() function Juergen Gross
2023-07-27 21:00   ` Julien Grall
2023-07-24 11:02 ` [PATCH v3 11/25] tools/xenstore: drop use of tdb Juergen Gross
2023-07-27 21:07   ` Julien Grall
2023-07-24 11:02 ` [PATCH v3 12/25] tools/xenstore: remove tdb code Juergen Gross
2023-07-24 11:02 ` [PATCH v3 13/25] tools/xenstore: let db_delete() return void Juergen Gross
2023-07-24 11:02 ` [PATCH v3 14/25] tools/xenstore: change talloc_free() to take a const pointer Juergen Gross
2023-07-27 21:21   ` Julien Grall
2023-07-28  6:15     ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 15/25] tools/xenstore: make data parameter of db_write() const Juergen Gross
2023-07-24 11:02 ` [PATCH v3 16/25] tools/xenstore: move copying of node data out of db_fetch() Juergen Gross
2023-07-27 21:33   ` Julien Grall
2023-07-28  6:18     ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 17/25] tools/xenstore: rework struct xs_tdb_record_hdr Juergen Gross
2023-07-27 21:53   ` Julien Grall
2023-07-28  6:23     ` Juergen Gross
2023-07-28  8:59       ` Julien Grall
2023-07-28  9:14         ` Juergen Gross
2023-07-28  9:38           ` Julien Grall
2023-07-28  9:45             ` Juergen Gross
2023-07-28 10:34               ` Julien Grall
2023-07-28 10:47                 ` Juergen Gross
2023-07-28 11:19                   ` Julien Grall
2023-07-28 12:06                     ` Juergen Gross
2023-07-28 12:48                       ` Julien Grall
2023-07-28 13:24                         ` Juergen Gross
2023-07-28 14:08                           ` Julien Grall
2023-07-28 14:32                             ` Juergen Gross
2023-07-28 14:59                               ` Julien Grall
2023-07-28 15:08                                 ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 18/25] tools/xenstore: don't use struct node_perms in struct node Juergen Gross
2023-08-01 21:29   ` Julien Grall
2023-08-02  4:47     ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 19/25] tools/xenstore: use struct node_hdr " Juergen Gross
2023-08-01 21:34   ` Julien Grall
2023-08-02  4:50     ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 20/25] tools/xenstore: alloc new memory in domain_adjust_node_perms() Juergen Gross
2023-08-01 21:46   ` Julien Grall
2023-08-02  4:51     ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 21/25] tools/xenstore: introduce read_node_nocopy() Juergen Gross
2023-08-01 22:00   ` Julien Grall
2023-08-02  4:52     ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 22/25] tools/xenstore: merge get_spec_node() into get_node_canonicalized() Juergen Gross
2023-08-03 21:36   ` Julien Grall
2023-08-04  9:17     ` Juergen Gross
2023-08-04  9:21       ` Julien Grall
2023-08-04  9:34         ` Juergen Gross
2023-08-04  9:44           ` Julien Grall
2023-08-04  9:56             ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 23/25] tools/xenstore: merge is_valid_nodename() into canonicalize() Juergen Gross
2023-08-03 21:46   ` Julien Grall
2023-08-04  9:35     ` Juergen Gross
2023-08-04 10:00       ` Julien Grall
2023-08-04 10:17         ` Juergen Gross
2023-08-04 10:33           ` Julien Grall
2023-08-04 12:05             ` Juergen Gross
2023-08-04 12:27               ` Julien Grall
2023-08-04 12:43                 ` Juergen Gross
2023-07-24 11:02 ` Juergen Gross [this message]
2023-08-12 11:56   ` [PATCH v3 24/25] tools/xenstore: rework get_node() Julien Grall
2023-08-14  5:42     ` Juergen Gross
2023-08-12 12:03   ` Julien Grall
2023-08-14  5:48     ` Juergen Gross
2023-07-24 11:02 ` [PATCH v3 25/25] tools/xenstore: introduce get_node_const() Juergen Gross
2023-08-12 12:05   ` Julien Grall
2023-08-14  5:54     ` Juergen Gross
2023-07-27 21:02 ` [PATCH v3 00/25] tools/xenstore: drop TDB Julien Grall

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230724110247.10520-25-jgross@suse.com \
    --to=jgross@suse.com \
    --cc=anthony.perard@citrix.com \
    --cc=julien@xen.org \
    --cc=wl@xen.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.