Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > linux.kernel > #1201555

[PATCH 7/9] kdbus: consolidate common code

From David Herrmann <dh.herrmann@gmail.com>
Newsgroups linux.kernel
Subject [PATCH 7/9] kdbus: consolidate common code
Date 2015-08-06 10:30 +0200
Message-ID <pUoIO-3l2-5@gated-at.bofh.it> (permalink)
References <pUoIN-3l2-3@gated-at.bofh.it>
Organization linux.* mail to news gateway

Show all headers | View raw


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

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
Please read the FAQ at  http://www.tux.org/lkml/

Back to linux.kernel | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

[PATCH 0/9] kdbus: set of random fixes David Herrmann <dh.herrmann@gmail.com> - 2015-08-06 10:30 +0200
  [PATCH 7/9] kdbus: consolidate common code David Herrmann <dh.herrmann@gmail.com> - 2015-08-06 10:30 +0200
  [PATCH 4/9] kdbus: move privilege checking in kdbus_conn_new() David Herrmann <dh.herrmann@gmail.com> - 2015-08-06 10:30 +0200
  [PATCH 9/9] kdbus/tests: properly parse KDBUS_CMD_LIST objects David Herrmann <dh.herrmann@gmail.com> - 2015-08-06 10:30 +0200
  [PATCH 1/9] kdbus: return EBADSLT on replies without slot David Herrmann <dh.herrmann@gmail.com> - 2015-08-06 10:30 +0200
  [PATCH 2/9] kdbus: reduce stack buffer to 256 bytes David Herrmann <dh.herrmann@gmail.com> - 2015-08-06 10:30 +0200
  [PATCH 3/9] kdbus: use separate counter for message IDs David Herrmann <dh.herrmann@gmail.com> - 2015-08-06 10:30 +0200
  [PATCH 5/9] kdbus: perform accounting on proxied uids David Herrmann <dh.herrmann@gmail.com> - 2015-08-06 10:30 +0200
  [PATCH 6/9] kdbus: inline privilege checks David Herrmann <dh.herrmann@gmail.com> - 2015-08-06 10:30 +0200

csiph-web