public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: David Herrmann <dh.herrmann@gmail.com>
To: linux-kernel@vger.kernel.org
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Daniel Mack <daniel@zonque.org>,
	Djalal Harouni <tixxdz@opendz.org>,
	David Herrmann <dh.herrmann@gmail.com>
Subject: [PATCH 7/9] kdbus: consolidate common code
Date: Thu,  6 Aug 2015 10:21:26 +0200	[thread overview]
Message-ID: <1438849288-18112-8-git-send-email-dh.herrmann@gmail.com> (raw)
In-Reply-To: <1438849288-18112-1-git-send-email-dh.herrmann@gmail.com>

Move the file-credential checkers into kdbus_ep_*() helper functions to
avoid hard-coding the same behavior in multiple places.

Signed-off-by: David Herrmann <dh.herrmann@gmail.com>
---
 ipc/kdbus/connection.c | 20 ++------------------
 ipc/kdbus/endpoint.c   | 28 ++++++++++++++++++++++++++++
 ipc/kdbus/endpoint.h   |  3 +++
 ipc/kdbus/handle.c     |  8 +-------
 4 files changed, 34 insertions(+), 25 deletions(-)

diff --git a/ipc/kdbus/connection.c b/ipc/kdbus/connection.c
index c81888e..aa3296e 100644
--- a/ipc/kdbus/connection.c
+++ b/ipc/kdbus/connection.c
@@ -84,24 +84,8 @@ static struct kdbus_conn *kdbus_conn_new(struct kdbus_ep *ep,
 		struct kdbus_bloom_parameter bloom;
 	} bloom_item;
 
-	/*
-	 * A connection is considered privileged, if, and only if, it didn't
-	 * connect through a custom endpoint *and* it has CAP_IPC_OWNER on the
-	 * namespace of the current domain.
-	 * Additionally, a connection is considered equivalent to the bus owner
-	 * if it didn't connect through a custom endpoint *and* it either is
-	 * privileged or the same user as the bus owner.
-	 *
-	 * Bus owners and alike can bypass bus policies. Privileged connections
-	 * can additionally change accounting, modify kernel resources and
-	 * perform restricted operations, as long as they're privileged on the
-	 * same level as the resources they touch.
-	 */
-	privileged = !ep->user &&
-		     file_ns_capable(file, ep->bus->domain->user_namespace,
-				     CAP_IPC_OWNER);
-	owner = !ep->user &&
-		(privileged || uid_eq(file->f_cred->euid, ep->bus->node.uid));
+	privileged = kdbus_ep_is_privileged(ep, file);
+	owner = kdbus_ep_is_owner(ep, file);
 
 	is_monitor = hello->flags & KDBUS_HELLO_MONITOR;
 	is_activator = hello->flags & KDBUS_HELLO_ACTIVATOR;
diff --git a/ipc/kdbus/endpoint.c b/ipc/kdbus/endpoint.c
index 977964d..44e7a20 100644
--- a/ipc/kdbus/endpoint.c
+++ b/ipc/kdbus/endpoint.c
@@ -184,6 +184,34 @@ struct kdbus_ep *kdbus_ep_unref(struct kdbus_ep *ep)
 }
 
 /**
+ * kdbus_ep_is_privileged() - check whether a file is privileged
+ * @ep:		endpoint to operate on
+ * @file:	file to test
+ *
+ * Return: True if @file is privileged in the domain of @ep.
+ */
+bool kdbus_ep_is_privileged(struct kdbus_ep *ep, struct file *file)
+{
+	return !ep->user &&
+		file_ns_capable(file, ep->bus->domain->user_namespace,
+				CAP_IPC_OWNER);
+}
+
+/**
+ * kdbus_ep_is_owner() - check whether a file should be treated as bus owner
+ * @ep:		endpoint to operate on
+ * @file:	file to test
+ *
+ * Return: True if @file should be treated as bus owner on @ep
+ */
+bool kdbus_ep_is_owner(struct kdbus_ep *ep, struct file *file)
+{
+	return !ep->user &&
+		(uid_eq(file->f_cred->euid, ep->bus->node.uid) ||
+		 kdbus_ep_is_privileged(ep, file));
+}
+
+/**
  * kdbus_cmd_ep_make() - handle KDBUS_CMD_ENDPOINT_MAKE
  * @bus:		bus to operate on
  * @argp:		command payload
diff --git a/ipc/kdbus/endpoint.h b/ipc/kdbus/endpoint.h
index bc1b94a..e0da59f 100644
--- a/ipc/kdbus/endpoint.h
+++ b/ipc/kdbus/endpoint.h
@@ -61,6 +61,9 @@ struct kdbus_ep *kdbus_ep_new(struct kdbus_bus *bus, const char *name,
 struct kdbus_ep *kdbus_ep_ref(struct kdbus_ep *ep);
 struct kdbus_ep *kdbus_ep_unref(struct kdbus_ep *ep);
 
+bool kdbus_ep_is_privileged(struct kdbus_ep *ep, struct file *file);
+bool kdbus_ep_is_owner(struct kdbus_ep *ep, struct file *file);
+
 struct kdbus_ep *kdbus_cmd_ep_make(struct kdbus_bus *bus, void __user *argp);
 int kdbus_cmd_ep_update(struct kdbus_ep *ep, void __user *argp);
 
diff --git a/ipc/kdbus/handle.c b/ipc/kdbus/handle.c
index 4d41ecf..fc60932 100644
--- a/ipc/kdbus/handle.c
+++ b/ipc/kdbus/handle.c
@@ -395,14 +395,8 @@ static long kdbus_handle_ioctl_ep(struct file *file, unsigned int cmd,
 
 	switch (cmd) {
 	case KDBUS_CMD_ENDPOINT_MAKE: {
-		bool priv;
-
-		priv = uid_eq(file->f_cred->euid, bus->node.uid) ||
-		       file_ns_capable(file, bus->domain->user_namespace,
-				       CAP_IPC_OWNER);
-
 		/* creating custom endpoints is a privileged operation */
-		if (file_ep->user || !priv) {
+		if (!kdbus_ep_is_owner(file_ep, file)) {
 			ret = -EPERM;
 			break;
 		}
-- 
2.5.0


  parent reply	other threads:[~2015-08-06  8:23 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-08-06  8:21 [PATCH 0/9] kdbus: set of random fixes David Herrmann
2015-08-06  8:21 ` [PATCH 1/9] kdbus: return EBADSLT on replies without slot David Herrmann
2015-08-06  8:21 ` [PATCH 2/9] kdbus: reduce stack buffer to 256 bytes David Herrmann
2015-08-06  8:21 ` [PATCH 3/9] kdbus: use separate counter for message IDs David Herrmann
2015-08-06  8:21 ` [PATCH 4/9] kdbus: move privilege checking in kdbus_conn_new() David Herrmann
2015-08-06  8:21 ` [PATCH 5/9] kdbus: perform accounting on proxied uids David Herrmann
2015-08-06  8:21 ` [PATCH 6/9] kdbus: inline privilege checks David Herrmann
2015-08-06  8:21 ` David Herrmann [this message]
2015-08-06  8:21 ` [PATCH 8/9] kdbus/samples: skip if __NR_memfd_create is not defined David Herrmann
2015-08-06  8:21 ` [PATCH 9/9] kdbus/tests: properly parse KDBUS_CMD_LIST objects David Herrmann

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=1438849288-18112-8-git-send-email-dh.herrmann@gmail.com \
    --to=dh.herrmann@gmail.com \
    --cc=daniel@zonque.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=tixxdz@opendz.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox