Skip to contentSkip to conversation

security: add a "cuppabilities" security module (preliminary version)

PATCH In review David Madore 2 participants 2 messages Sep 16, 2006 – Sep 17, 2006

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 security: add a "cuppabilities" security module (preliminary version)

This patch adds a Linux Security Module called "cuppabilities", which allows for an easy creation of underprivileged processes:

* add a "cuppabilities" field to the task structure, and two prctl() calls (PR_GET_CUPS and PR_ADD_CUPS) which manipulate it,

* add a LSM which forbids certain operations when various bits are set in this field (otherwise it also calls commoncap operations secondarily).

So far this is only a preliminary demonstration of concept, so only very uninteresting cuppabilities are included: CUP_FORK, some variants on CUP_EXEC (including CUP_EXEC_SXID) and CUP_PTRACE.

---

*** IMPORTANT NOTE ***

This patch IS NOT related (nor compatible) with the one posted a few days ago on this list under the name "new capabilities patch". The latter treated underprivileged processes as lacking some capabilities. It has been abandoned due to heavy criticism on LKML. *This* patch adds a completely different field, "cuppabilities", and treats cuppabilities in a simpler fashion than capabilities (only one set per task rather than permitted/effective/inheritable, and just a simple prctl() to add cups - for example, a simple prctl(PR_ADD_CUPS, (1<<CUP_EXEC_SXID)) forbids suid/sgid exec from then on).

This patch should not break (or indeed change!) anything for those who don't activate support for cuppabilities.

Comments are appreciated on whether this is the right way to proceed, especially from those who criticized my earlier (aforementioned) capabilities patch.

[Note that another feature of the earlier patch was that it included support for inheriting capabilities. This is in no way addressed by this patch. However, I also posted a patch, see <URL: http://lkml.org/lkml/2006/9/15/158 >, which adds a mount option to make capabilities inheritable by default while retaining POSIX.1e semantics.]

Oh, and please don't tell me that CUP_EXEC is useless because a program can mmap() an executable in core, etc. I know that. It's just a demonstration cuppability that's simple to implement, and it's not meant to be useful. CUP_EXEC_SXID, however, is supposed to be (useful).

 fs/exec.c                   |    2 +
 include/linux/binfmts.h     |    1 
 include/linux/cuppability.h |   24 ++++++++
 include/linux/init_task.h   |    1 
 include/linux/prctl.h       |    4 +
 include/linux/sched.h       |    1 
 kernel/sys.c                |    7 ++
 security/Kconfig            |    9 +++
 security/Makefile           |    1 
 security/cuppability.c      |  128 +++++++++++++++++++++++++++++++++++++++++++
 10 files changed, 178 insertions(+), 0 deletions(-)
fs/exec.c +2 -0
index 54135df..6a8eab5 100644
@@ -929,6 +929,7 @@ int prepare_binprm(struct linux_binprm *
 	if(!(bprm->file->f_vfsmnt->mnt_flags & MNT_NOSUID)) {
 		/* Set-uid? */
 		if (mode & S_ISUID) {
+			bprm->is_suid = 1;
 			current->personality &= ~PER_CLEAR_ON_SETID;
 			bprm->e_uid = inode->i_uid;
 		}
@@ -940,6 +941,7 @@ int prepare_binprm(struct linux_binprm *
 		 * executable.
 		 */
 		if ((mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
+			bprm->is_sgid = 1;
 			current->personality &= ~PER_CLEAR_ON_SETID;
 			bprm->e_gid = inode->i_gid;
 		}
include/linux/binfmts.h +1 -0
index c1e82c5..c7fb183 100644
@@ -29,6 +29,7 @@ struct linux_binprm{
 	struct file * file;
 	int e_uid, e_gid;
 	kernel_cap_t cap_inheritable, cap_permitted, cap_effective;
+	char is_suid, is_sgid;
 	void *security;
 	int argc, envc;
 	char * filename;	/* Name of binary as seen by procps */
include/linux/cuppability.h new file +24 -0
new file mode 100644
index 0000000..3ffc376
@@ -0,0 +1,24 @@
+/*
+ * This is <linux/cuppability.h>
+ *
+ * David A. Madore <[email protected]>
+ */ 
+
+#ifndef _LINUX_CUPPABILITY_H
+#define _LINUX_CUPPABILITY_H
+
+#define CUP_NONE 0UL
+#define CUP_ALL 0xffffffUL
+
+#define CUP_TO_MASK(x) (1UL << (x))
+#define cup_raise(c, flag)   ((c) |=  CAP_TO_MASK(flag))
+#define cup_lower(c, flag)   ((c) &= ~CAP_TO_MASK(flag))
+#define cup_raised(c, flag)  ((c) & CAP_TO_MASK(flag))
+
+#define CUP_FORK		0 /* Forbid fork() */
+#define CUP_EXEC		1 /* Forbid exec() */
+#define CUP_EXEC_ONCE		2 /* Forbid exec() except just once */
+#define CUP_EXEC_SXID		3 /* Forbid s[ug]id exec() */
+#define CUP_PTRACE		4 /* Forbid ptrace() */
+
+#endif /* !_LINUX_CUPPABILITY_H */
include/linux/init_task.h +1 -0
index 60aac2c..1878bb8 100644
@@ -110,6 +110,7 @@ #define INIT_TASK(tsk)	\
 	.cap_inheritable = CAP_INIT_INH_SET,				\
 	.cap_permitted	= CAP_FULL_SET,					\
 	.keep_capabilities = 0,						\
+	.cuppabilities  = 0,						\
 	.user		= INIT_USER,					\
 	.comm		= "swapper",					\
 	.thread		= INIT_THREAD,					\
include/linux/prctl.h +4 -0
index 52a9be4..247d262 100644
@@ -59,4 +59,8 @@ # define PR_ENDIAN_BIG		0
 # define PR_ENDIAN_LITTLE	1	/* True little endian mode */
 # define PR_ENDIAN_PPC_LITTLE	2	/* "PowerPC" pseudo little endian */
 
+/* Get/set process cuppabilities */
+#define PR_GET_CUPS    21
+#define PR_ADD_CUPS    22
+
 #endif /* _LINUX_PRCTL_H */
include/linux/sched.h +1 -0
index 34ed0d9..6f5b69f 100644
@@ -859,6 +859,7 @@ #endif
 	struct group_info *group_info;
 	kernel_cap_t   cap_effective, cap_inheritable, cap_permitted;
 	unsigned keep_capabilities:1;
+	u32 cuppabilities;
 	struct user_struct *user;
 #ifdef CONFIG_KEYS
 	struct key *request_key_auth;	/* assumed request_key authority */
kernel/sys.c +7 -0
index e236f98..2bf8564 100644
@@ -18,6 +18,7 @@ #include <linux/kernel.h>
 #include <linux/kexec.h>
 #include <linux/workqueue.h>
 #include <linux/capability.h>
+#include <linux/cuppability.h>
 #include <linux/device.h>
 #include <linux/key.h>
 #include <linux/times.h>
@@ -2055,6 +2056,12 @@ asmlinkage long sys_prctl(int option, un
 		case PR_SET_ENDIAN:
 			error = SET_ENDIAN(current, arg2);
 			break;
+		case PR_GET_CUPS:
+			error = current->cuppabilities;
+			break;
+		case PR_ADD_CUPS:
+			current->cuppabilities |= (arg2 & CUP_ALL);
+			break;
 
 		default:
 			error = -EINVAL;
security/Kconfig +9 -0
index 67785df..b76d239 100644
@@ -93,6 +93,15 @@ config SECURITY_ROOTPLUG
 	  
 	  If you are unsure how to answer this question, answer N.
 
+config SECURITY_CUPPABILITIES
+	tristate "Cuppabilities"
+	depends on SECURITY
+	help
+	  This implements cuppabilities (underprivileged processes) as
+	  an LSM module.
+	  
+	  If you are unsure how to answer this question, answer N.
+
 config SECURITY_SECLVL
 	tristate "BSD Secure Levels"
 	depends on SECURITY
security/Makefile +1 -0
index 8cbbf2f..2ebdca6 100644
@@ -16,4 +16,5 @@ # Must precede capability.o in order to 
 obj-$(CONFIG_SECURITY_SELINUX)		+= selinux/built-in.o
 obj-$(CONFIG_SECURITY_CAPABILITIES)	+= commoncap.o capability.o
 obj-$(CONFIG_SECURITY_ROOTPLUG)		+= commoncap.o root_plug.o
+obj-$(CONFIG_SECURITY_CUPPABILITIES)    += commoncap.o cuppability.o
 obj-$(CONFIG_SECURITY_SECLVL)		+= seclvl.o
security/cuppability.c new file +128 -0

128 lines not on this page · raw message

#1 lore raw

Hi!

Quoted 6 lines

This patch adds a Linux Security Module called "cuppabilities", which allows for an easy creation of underprivileged processes:

* add a "cuppabilities" field to the task structure, and two prctl() calls (PR_GET_CUPS and PR_ADD_CUPS) which manipulate it,

Quoted 7 lines

* add a LSM which forbids certain operations when various bits are set in this field (otherwise it also calls commoncap operations secondarily).

So far this is only a preliminary demonstration of concept, so only very uninteresting cuppabilities are included: CUP_FORK, some variants on CUP_EXEC (including CUP_EXEC_SXID) and CUP_PTRACE.

Basically looks good to me.

Quoted 13 lines

*** IMPORTANT NOTE ***

This patch IS NOT related (nor compatible) with the one posted a few days ago on this list under the name "new capabilities patch". The latter treated underprivileged processes as lacking some capabilities. It has been abandoned due to heavy criticism on LKML. *This* patch adds a completely different field, "cuppabilities", and treats cuppabilities in a simpler fashion than capabilities (only one set per task rather than permitted/effective/inheritable, and just a simple prctl() to add cups - for example, a simple prctl(PR_ADD_CUPS, (1<<CUP_EXEC_SXID)) forbids suid/sgid exec from then on).

Quoted 6 lines

This patch should not break (or indeed change!) anything for those who don't activate support for cuppabilities.

Comments are appreciated on whether this is the right way to proceed, especially from those who criticized my earlier (aforementioned) capabilities patch.

This should go to Documentation/, somewhere.

Why the cuppability name?

Quoted 8 lines
include/linux/cuppability.h new file +8 -0
+/*
+ * This is <linux/cuppability.h>
+ *
+ * David A. Madore <[email protected]>
+ */ 
+
+#ifndef _LINUX_CUPPABILITY_H
+#define _LINUX_CUPPABILITY_H

include/linux/cuppability.h line 18

Quoted 10 lines
include/linux/cuppability.h new file +10 -0
+
+#define CUP_NONE 0UL
+#define CUP_ALL 0xffffffUL
+
+#define CUP_TO_MASK(x) (1UL << (x))
+#define cup_raise(c, flag)   ((c) |=  CAP_TO_MASK(flag))
+#define cup_lower(c, flag)   ((c) &= ~CAP_TO_MASK(flag))
+#define cup_raised(c, flag)  ((c) & CAP_TO_MASK(flag))
+
+#define CUP_FORK		0 /* Forbid fork() */

This is reverted from normal meaning, now?

kernel/sys.c asmlinkage long sys_prctl(int option, un · line 2061

Quoted 7 lines
kernel/sys.c +3 -0
@@ -2055,6 +2056,12 @@ asmlinkage long sys_prctl(int option, un
 		case PR_SET_ENDIAN:
 			error = SET_ENDIAN(current, arg2);
 			break;
+		case PR_GET_CUPS:
+			error = current->cuppabilities;
+			break;

Yep, it seems so.

security/cuppability.c line 26

Quoted 5 lines
security/cuppability.c new file +5 -0
+#if defined(CONFIG_SECURITY_CUPPABILITY_MODULE)
+#define MY_NAME THIS_MODULE->name
+#else
+#define MY_NAME "cuppability"
+#endif

Now this is what I'd call a hack...

security/cuppability.c line 30

Quoted 3 lines
security/cuppability.c new file +3 -0
+static int cup_ptrace (struct task_struct *parent, struct task_struct *child)
+{
+	if (cup_raised (current->cuppabilities, CUP_PTRACE))

~
\- you should avoid this space.

security/cuppability.c line 46

Quoted 5 lines
security/cuppability.c new file +5 -0
+static void cup_bprm_apply_creds (struct linux_binprm *bprm, int unsafe)
+{
+	if (bprm->is_suid || bprm->is_sgid) {
+		current->cuppabilities = CUP_NONE;
+	}

Hmm, meaning is really inverted CUP_NONE means "forbid nothing".. not forbid everything... right?

Quoted 1 line

+MODULE_DESCRIPTION("Root Plug sample LSM module, written for Linux Journal article");

Hmm, probably not...
Pavel

Signature · 2 lines
(english) http://www.livejournal.com/~pavelmachek
(cesky, pictures) http://atrey.karlin.mff.cuni.cz/~pavel/picture/horses/blog.html