From patchwork Mon Jul 18 07:31:28 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?b?SsO8cmdlbiBHcm/Dnw==?= X-Patchwork-Id: 9234143 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id DF1D66075D for ; Mon, 18 Jul 2016 07:34:35 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D50DC212CF for ; Mon, 18 Jul 2016 07:34:35 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id C9E4926A4D; Mon, 18 Jul 2016 07:34:35 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 4FC07212CF for ; Mon, 18 Jul 2016 07:34:35 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bP31d-0005TB-BB; Mon, 18 Jul 2016 07:31:37 +0000 Received: from mail6.bemta14.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1bP31b-0005Re-MX for xen-devel@lists.xen.org; Mon, 18 Jul 2016 07:31:35 +0000 Received: from [193.109.254.147] by server-16.bemta-14.messagelabs.com id 4B/64-24830-6D58C875; Mon, 18 Jul 2016 07:31:34 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFrrKLMWRWlGSWpSXmKPExsVyuP0Ov+611p5 wg+ddXBZLPi5mcWD0OLr7N1MAYxRrZl5SfkUCa8aBt7PYC74bVKzYu5mlgXGKRhcjJ4eEgJHE 24n/mLoYuTiEBBYySiz9c48ZJMEmoCqx4fopVhBbREBa4trny4wgNrNAqMTJdxA1wkD2vveP2 EFsFqD6p0c/gtXwCthJrNvzmgVigZzE9ZnTmUBsTgF7iVl9p8FsIaCaLa1HWCcwci9gZFjFqF 6cWlSWWqRroZdUlJmeUZKbmJmja2hoopebWlycmJ6ak5hUrJecn7uJEehdBiDYwbhxkvMhRkk OJiVR3iaFnnAhvqT8lMqMxOKM+KLSnNTiQ4wyHBxKErxHW4BygkWp6akVaZk5wDCDSUtw8CiJ 8J4GSfMWFyTmFmemQ6ROMSpKifP2gCQEQBIZpXlwbbDQvsQoKyXMywh0iBBPQWpRbmYJqvwrR nEORiVh3isgU3gy80rgpr8CWswEtNhAtRtkcUkiQkqqgVE3+sW9pLU7bHsOtuycFdHtkbDeba Y4/7enUuvlriuHOt2WPvY+7Ij29O3Xwu67zJ8wtbTn4rO+7w3Fyrfdr086G8jAP+P2lykLeSa /qr591Tj5st6jQj3eA8vDq5QPVemJxjLdmTC763r37w1VaYsXXs12FZ9+UvF8q7DQLzf9RGWf 8wlhf58psRRnJBpqMRcVJwIAkwPTF2gCAAA= X-Env-Sender: jgross@suse.com X-Msg-Ref: server-4.tower-27.messagelabs.com!1468827093!53575066!1 X-Originating-IP: [195.135.220.15] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 8.77; banners=-,-,- X-VirusChecked: Checked Received: (qmail 15137 invoked from network); 18 Jul 2016 07:31:34 -0000 Received: from mx2.suse.de (HELO mx2.suse.de) (195.135.220.15) by server-4.tower-27.messagelabs.com with DHE-RSA-CAMELLIA256-SHA encrypted SMTP; 18 Jul 2016 07:31:34 -0000 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 9BD15AD26; Mon, 18 Jul 2016 07:31:33 +0000 (UTC) From: Juergen Gross To: xen-devel@lists.xen.org Date: Mon, 18 Jul 2016 09:31:28 +0200 Message-Id: <1468827089-9054-5-git-send-email-jgross@suse.com> X-Mailer: git-send-email 2.6.6 In-Reply-To: <1468827089-9054-1-git-send-email-jgross@suse.com> References: <1468827089-9054-1-git-send-email-jgross@suse.com> Cc: Juergen Gross , wei.liu2@citrix.com, ian.jackson@eu.citrix.com Subject: [Xen-devel] [PATCH v2 4/5] xenstore: add explicit memory context parameter to get_node() X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP Add a parameter to xenstored get_node() function to explicitly specify the memory context to be used for allocations. This will make it easier to avoid memory leaks by using a context which is freed soon. This requires adding the temporary context to errno_from_parents() and ask_parents(), too. When calling get_node() select a sensible memory context for the new parameter by preferring a temporary one. Signed-off-by: Juergen Gross Reviewed-by: Wei Liu Acked-by: Ian Jackson --- tools/xenstore/xenstored_core.c | 33 ++++++++++++++++++--------------- tools/xenstore/xenstored_core.h | 1 + tools/xenstore/xenstored_watch.c | 2 +- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/tools/xenstore/xenstored_core.c b/tools/xenstore/xenstored_core.c index e5c74f4..095ba00 100644 --- a/tools/xenstore/xenstored_core.c +++ b/tools/xenstore/xenstored_core.c @@ -517,13 +517,14 @@ static char *get_parent(const void *mem, const char *node) } /* What do parents say? */ -static enum xs_perm_type ask_parents(struct connection *conn, const char *name) +static enum xs_perm_type ask_parents(struct connection *conn, const void *mem, + const char *name) { struct node *node; do { - name = get_parent(name, name); - node = read_node(conn, name, name); + name = get_parent(mem, name); + node = read_node(conn, mem, name); if (node) break; } while (!streq(name, "/")); @@ -541,20 +542,22 @@ static enum xs_perm_type ask_parents(struct connection *conn, const char *name) * specific node without allowing it in the parents. If it's going to * fail, however, we don't want the errno to indicate any information * about the node. */ -static int errno_from_parents(struct connection *conn, const char *node, - int errnum, enum xs_perm_type perm) +static int errno_from_parents(struct connection *conn, const void *mem, + const char *node, int errnum, + enum xs_perm_type perm) { /* We always tell them about memory failures. */ if (errnum == ENOMEM) return errnum; - if (ask_parents(conn, node) & perm) + if (ask_parents(conn, mem, node) & perm) return errnum; return EACCES; } /* If it fails, returns NULL and sets errno. */ struct node *get_node(struct connection *conn, + const void *mem, const char *name, enum xs_perm_type perm) { @@ -564,7 +567,7 @@ struct node *get_node(struct connection *conn, errno = EINVAL; return NULL; } - node = read_node(conn, name, name); + node = read_node(conn, mem, name); /* If we don't have permission, we don't have node. */ if (node) { if ((perm_for_conn(conn, node->perms, node->num_perms) & perm) @@ -575,7 +578,7 @@ struct node *get_node(struct connection *conn, } /* Clean up errno if they weren't supposed to know. */ if (!node) - errno = errno_from_parents(conn, name, errno, perm); + errno = errno_from_parents(conn, mem, name, errno, perm); return node; } @@ -768,7 +771,7 @@ static void send_directory(struct connection *conn, struct buffered_data *in) const char *name = onearg(in); name = canonicalize(conn, name); - node = get_node(conn, name, XS_PERM_READ); + node = get_node(conn, in, name, XS_PERM_READ); if (!node) { send_error(conn, errno); return; @@ -783,7 +786,7 @@ static void do_read(struct connection *conn, struct buffered_data *in) const char *name = onearg(in); name = canonicalize(conn, name); - node = get_node(conn, name, XS_PERM_READ); + node = get_node(conn, in, name, XS_PERM_READ); if (!node) { send_error(conn, errno); return; @@ -920,7 +923,7 @@ static void do_write(struct connection *conn, struct buffered_data *in) datalen = in->used - offset; name = canonicalize(conn, vec[0]); - node = get_node(conn, name, XS_PERM_WRITE); + node = get_node(conn, in, name, XS_PERM_WRITE); if (!node) { /* No permissions, invalid input? */ if (errno != ENOENT) { @@ -952,7 +955,7 @@ static void do_mkdir(struct connection *conn, struct buffered_data *in) const char *name = onearg(in); name = canonicalize(conn, name); - node = get_node(conn, name, XS_PERM_WRITE); + node = get_node(conn, in, name, XS_PERM_WRITE); /* If it already exists, fine. */ if (!node) { @@ -1070,7 +1073,7 @@ static void do_rm(struct connection *conn, struct buffered_data *in) const char *name = onearg(in); name = canonicalize(conn, name); - node = get_node(conn, name, XS_PERM_WRITE); + node = get_node(conn, in, name, XS_PERM_WRITE); if (!node) { /* Didn't exist already? Fine, if parent exists. */ if (errno == ENOENT) { @@ -1107,7 +1110,7 @@ static void do_get_perms(struct connection *conn, struct buffered_data *in) unsigned int len; name = canonicalize(conn, name); - node = get_node(conn, name, XS_PERM_READ); + node = get_node(conn, in, name, XS_PERM_READ); if (!node) { send_error(conn, errno); return; @@ -1139,7 +1142,7 @@ static void do_set_perms(struct connection *conn, struct buffered_data *in) num--; /* We must own node to do this (tools can do this too). */ - node = get_node(conn, name, XS_PERM_WRITE|XS_PERM_OWNER); + node = get_node(conn, in, name, XS_PERM_WRITE|XS_PERM_OWNER); if (!node) { send_error(conn, errno); return; diff --git a/tools/xenstore/xenstored_core.h b/tools/xenstore/xenstored_core.h index 5dbf9c8..f763e47 100644 --- a/tools/xenstore/xenstored_core.h +++ b/tools/xenstore/xenstored_core.h @@ -149,6 +149,7 @@ bool check_event_node(const char *node); /* Get this node, checking we have permissions. */ struct node *get_node(struct connection *conn, + const void *mem, const char *name, enum xs_perm_type perm); diff --git a/tools/xenstore/xenstored_watch.c b/tools/xenstore/xenstored_watch.c index 8543999..beefd6c 100644 --- a/tools/xenstore/xenstored_watch.c +++ b/tools/xenstore/xenstored_watch.c @@ -57,7 +57,7 @@ static void add_event(struct connection *conn, if (!check_event_node(name)) { /* Can this conn load node, or see that it doesn't exist? */ - struct node *node = get_node(conn, name, XS_PERM_READ); + struct node *node = get_node(conn, name, name, XS_PERM_READ); /* * XXX We allow EACCES here because otherwise a non-dom0 * backend driver cannot watch for disappearance of a frontend