capabilities patch (v 0.1)
Subsystems (6)
Well, I wasn't sleepy tonight, so I produced the following patch for Linux capabilities, which attempts to make them useful. It is supposed to do the following (which may or may not conform with the POSIX semantics, I don't think it matters much):
* First, and most importantly, capabilities are carried across execve(). More precisely, on execve(), the inheritable set of the process (which is always a subset of the permitted set) is copied to the permitted set, and ANDed on the effective set; except when a suid root binary is executed, in which case the permitted, inheritable and effective sets are fully set.
* Second, a much more extensive change, the patch introduces a third set of capabilities for every process, the "bounding" set. Normally the bounding set has every capability in it. If a capability is removed from it, that means the process is never allowed to gain it on exec. In the current state of affairs, since the only way of gaining capabilities is through suid root programs, the bounding set is essentially an all-or-nothing affair: if you do not have every capability in your bounding set, you may not run a suid root program (execve() will fail with EPERM). This can still be very useful on untrusted programs.
This patch hasn't been tested very much; in fact, it has been hardly tested at all (I just ran the kernel in a qemu and made a few basic checks). Since it adds a whole new set of capabilities to every process, it also requires a specially modified version of libcap (the one I have right now is pretty buggy, so I'm not posting the patch here).
Consider this more a "proof of concept" than a serious patch, but I'm interested in any comments.
### cut after ###
fs/proc/array.c +4 -2
@@ -281,10 +281,12 @@ static inline char *task_cap(struct task {return buffer +sprintf (buffer,"CapInh:\t%016x\n" "CapPrm:\t%016x\n" -"CapEff:\t%016x\n" , +"CapEff:\t%016x\n" +"CapBnd:\t%016x\n" ,cap_t (p->cap_inheritable),cap_t (p->cap_permitted), -cap_t (p->cap_effective)); +cap_t (p->cap_effective), +cap_t (p->cap_bounding)); }int proc_pid_status (struct task_struct *task,char * buffer)
include/linux/binfmts.h +1 -1
@@ -28,7 +28,7 @@ struct linux_binprm{int sh_bang;struct file * file;int e_uid, e_gid; -kernel_cap_t cap_inheritable, cap_permitted, cap_effective; +kernel_cap_t cap_inheritable, cap_permitted, cap_effective, cap_bounding;void *security;int argc, envc;char * filename;/* Name of binary as seen by procps */
include/linux/capability.h +3 -2
@@ -27,7 +27,7 @@library since the draft standard requires the use of malloc/free etc.. */ -#define _LINUX_CAPABILITY_VERSION 0x19980330 +#define _LINUX_CAPABILITY_VERSION 0x20050809 typedef struct __user_cap_header_struct { __u32 version;
@@ -38,6 +38,7 @@ typedef struct __user_cap_data_struct {__u32 effective;__u32 permitted;__u32 inheritable; +__u32 bounding; }__user *cap_user_data_t;#ifdef __KERNEL__
@@ -311,7 +312,7 @@ extern kernel_cap_t cap_bset;#define CAP_EMPTY_SET to_cap_t(0)#define CAP_FULL_SET to_cap_t(~0)#define CAP_INIT_EFF_SET to_cap_t(~0 & ~CAP_TO_MASK(CAP_SETPCAP)) -#define CAP_INIT_INH_SET to_cap_t(0) +#define CAP_INIT_INH_SET to_cap_t(~0 & ~CAP_TO_MASK(CAP_SETPCAP))#define CAP_TO_MASK (x) (1 << (x))#define cap_raise (c, flag) (cap_t(c) |= CAP_TO_MASK(flag))
include/linux/init_task.h +1 -0
@@ -94,6 +94,7 @@ extern struct group_info init_groups; .cap_effective = CAP_INIT_EFF_SET, \ .cap_inheritable = CAP_INIT_INH_SET, \ .cap_permitted = CAP_FULL_SET, \ + .cap_bounding = CAP_FULL_SET, \ .keep_capabilities = 0, \ .user = INIT_USER, \ .comm = "swapper", \
include/linux/sched.h +1 -1
@@ -654,7 +654,7 @@ struct task_struct {uid_t uid,euid,suid,fsuid;gid_t gid,egid,sgid,fsgid;struct group_info *group_info; -kernel_cap_t cap_effective, cap_inheritable, cap_permitted; +kernel_cap_t cap_effective, cap_inheritable, cap_permitted, cap_bounding; unsigned keep_capabilities:1;struct user_struct *user;#ifdef CONFIG_KEYS
include/linux/security.h +34 -22
140 lines not on this page · raw message
kernel/capability.c +22 -14
@@ -14,7 +14,7 @@#include <asm/uaccess.h> unsigned securebits =SECUREBITS_DEFAULT ;/* systemwide security settings */ -kernel_cap_t cap_bset =CAP_INIT_EFF_SET ; +kernel_cap_t cap_bset =CAP_INIT_INH_SET ;EXPORT_SYMBOL (securebits);EXPORT_SYMBOL (cap_bset);
@@ -69,7 +69,7 @@ asmlinkage long sys_capget(cap_user_head }else target = current; - ret =security_capget (target, &data.effective, &data.inheritable, &data.permitted); + ret =security_capget (target, &data.effective, &data.inheritable, &data.permitted, &data.bounding); out:read_unlock (&tasklist_lock);
@@ -87,7 +87,8 @@ out:*/ static inline int cap_set_pg (int pgrp,kernel_cap_t *effective,kernel_cap_t *inheritable, -kernel_cap_t *permitted) +kernel_cap_t *permitted, +kernel_cap_t *bounding) {task_t *g, *target;int ret = -EPERM ;
@@ -98,10 +99,12 @@ static inline int cap_set_pg(int pgrp, k while_each_thread(g, target) {if (!security_capset_check (target, effective, inheritable, - permitted)) { + permitted, + bounding)) {security_capset_set (target, effective, inheritable, - permitted); + permitted, + bounding); ret =0 ; } found =1 ;
@@ -119,7 +122,8 @@ static inline int cap_set_pg(int pgrp, k*/ static inline int cap_set_all (kernel_cap_t *effective,kernel_cap_t *inheritable, -kernel_cap_t *permitted) +kernel_cap_t *permitted, +kernel_cap_t *bounding) {task_t *g, *target;int ret = -EPERM ;
@@ -130,10 +134,10 @@ static inline int cap_set_all(kernel_capcontinue ; found =1 ;if (security_capset_check (target, effective, inheritable, - permitted)) + permitted, bounding))continue ; ret =0 ; -security_capset_set (target, effective, inheritable, permitted); +security_capset_set (target, effective, inheritable, permitted, bounding); }while_each_thread (g, target);if (!found)
@@ -155,7 +159,7 @@ static inline int cap_set_all(kernel_cap*/ asmlinkage long sys_capset (cap_user_header_t header,const cap_user_data_t data) { -kernel_cap_t inheritable, permitted, effective; +kernel_cap_t inheritable, permitted, effective, bounding;__u32 version;task_t *target;int ret;
@@ -178,7 +182,8 @@ asmlinkage long sys_capset(cap_user_headif (copy_from_user (&effective, &data->effective,sizeof (effective)) ||copy_from_user (&inheritable, &data->inheritable,sizeof (inheritable)) || -copy_from_user (&permitted, &data->permitted,sizeof (permitted))) +copy_from_user (&permitted, &data->permitted,sizeof (permitted)) || +copy_from_user (&bounding, &data->bounding,sizeof (bounding)))return -EFAULT ;spin_lock (&task_capability_lock);
@@ -199,17 +204,20 @@ asmlinkage long sys_capset(cap_user_headwe now put them into effect. */ if (pid <0 ) {if (pid ==-1 )/* all procs other than current and init */ - ret =cap_set_all (&effective, &inheritable, &permitted); + ret =cap_set_all (&effective, &inheritable, &permitted, &bounding);else /* all procs in process group */ ret =cap_set_pg (-pid, &effective, &inheritable, - &permitted); + &permitted, + &bounding); }else { ret =security_capset_check (target, &effective, &inheritable, - &permitted); + &permitted, + &bounding);if (!ret)security_capset_set (target, &effective, &inheritable, - &permitted); + &permitted, + &bounding); } out:
security/commoncap.c +47 -28
151 lines not on this page · raw message
security/dummy.c +13 -7
@@ -35,17 +35,21 @@ static int dummy_ptrace (struct task_str }static int dummy_capget (struct task_struct *target,kernel_cap_t * effective, -kernel_cap_t * inheritable,kernel_cap_t * permitted) +kernel_cap_t * inheritable,kernel_cap_t * permitted, +kernel_cap_t * bounding) { *effective = *inheritable = *permitted =0 ; + *bounding =CAP_FULL_SET ;if (!issecure (SECURE_NOROOT )) {if (target->euid ==0 ) { - *permitted |= (~0 & ~CAP_FS_MASK ); - *effective |= (~0 & ~CAP_TO_MASK (CAP_SETPCAP ) & ~CAP_FS_MASK ); + *permitted |= (CAP_FULL_SET & ~CAP_FS_MASK ); + *effective |= (CAP_FULL_SET & ~CAP_TO_MASK (CAP_SETPCAP ) & ~CAP_FS_MASK ); + *inheritable |= (CAP_FULL_SET & ~CAP_TO_MASK (CAP_SETPCAP ) & ~CAP_FS_MASK ); }if (target->fsuid ==0 ) { *permitted |=CAP_FS_MASK ; *effective |=CAP_FS_MASK ; + *inheritable |=CAP_FS_MASK ; } }return 0 ;
@@ -54,7 +58,8 @@ static int dummy_capget (struct task_strstatic int dummy_capset_check (struct task_struct *target,kernel_cap_t * effective,kernel_cap_t * inheritable, -kernel_cap_t * permitted) +kernel_cap_t * permitted, +kernel_cap_t * bounding) {return -EPERM ; }
@@ -62,7 +67,8 @@ static int dummy_capset_check (struct tastatic void dummy_capset_set (struct task_struct *target,kernel_cap_t * effective,kernel_cap_t * inheritable, -kernel_cap_t * permitted) +kernel_cap_t * permitted, +kernel_cap_t * bounding) {return ; }
@@ -141,7 +147,7 @@ static void dummy_bprm_apply_creds (stru current->suid = current->euid = current->fsuid = bprm->e_uid; current->sgid = current->egid = current->fsgid = bprm->e_gid; -dummy_capget (current, ¤t->cap_effective, ¤t->cap_inheritable, ¤t->cap_permitted); +dummy_capget (current, ¤t->cap_effective, ¤t->cap_inheritable, ¤t->cap_permitted, ¤t->cap_bounding); }static void dummy_bprm_post_apply_creds (struct linux_binprm *bprm)
@@ -509,7 +515,7 @@ static int dummy_task_setuid (uid_t id0,static int dummy_task_post_setuid (uid_t id0,uid_t id1,uid_t id2,int flags) { -dummy_capget (current, ¤t->cap_effective, ¤t->cap_inheritable, ¤t->cap_permitted); +dummy_capget (current, ¤t->cap_effective, ¤t->cap_inheritable, ¤t->cap_permitted, ¤t->cap_bounding);return 0 ; }
security/selinux/hooks.c +9 -6
@@ -1362,7 +1362,8 @@ static int selinux_ptrace(struct task_st }static int selinux_capget (struct task_struct *target,kernel_cap_t *effective, -kernel_cap_t *inheritable,kernel_cap_t *permitted) +kernel_cap_t *inheritable,kernel_cap_t *permitted, +kernel_cap_t *bounding) {int error;
@@ -1370,15 +1371,16 @@ static int selinux_capget(struct task_stif (error)return error; -return secondary_ops->capget (target, effective, inheritable, permitted); +return secondary_ops->capget (target, effective, inheritable, permitted, bounding); }static int selinux_capset_check (struct task_struct *target,kernel_cap_t *effective, -kernel_cap_t *inheritable,kernel_cap_t *permitted) +kernel_cap_t *inheritable,kernel_cap_t *permitted, +kernel_cap_t *bounding) {int error; - error = secondary_ops->capset_check (target, effective, inheritable, permitted); + error = secondary_ops->capset_check (target, effective, inheritable, permitted, bounding);if (error)return error;
@@ -1386,9 +1388,10 @@ static int selinux_capset_check(struct t }static void selinux_capset_set (struct task_struct *target,kernel_cap_t *effective, -kernel_cap_t *inheritable,kernel_cap_t *permitted) +kernel_cap_t *inheritable,kernel_cap_t *permitted, +kernel_cap_t *bounding) { - secondary_ops->capset_set (target, effective, inheritable, permitted); + secondary_ops->capset_set (target, effective, inheritable, permitted, bounding); }static int selinux_capable (struct task_struct *tsk,int cap)
### cut before ###
Signature · 3 lines
David A. Madore
([email protected],
http://www.madore.org/~david/ )Quoted 2 lines · * David Madore ([email protected]) wrote:
* Second, a much more extensive change, the patch introduces a third set of capabilities for every process, the "bounding" set. Normally
this is not a good idea. don't add more sets. if you really want to work on this i'll give you all the patches that have been done thus far, plus a set of tests that look at all the execve, ptrace, setuid type of corner cases.
thanks,
-chris
Quoted 5 lines · Chris Wright
Quoted 2 lines · * David Madore ([email protected]) wrote:
* Second, a much more extensive change, the patch introduces a third set of capabilities for every process, the "bounding" set. Normally
this is not a good idea. don't add more sets.
Could you elaborate? Why is adding sets bad? From what I read of the June 2000 discussions on the linux-privs-discuss mailing-list (<URL:
http://sourceforge.net/mailarchive/forum.php?forum_id=25120&max_rows=25&style=flat&viewmonth=200006
Quoted 1 line
), a rather large consensus had formed around the idea that some
kind of bounding set was a useful idea (as a matter of fact, the sendmail problem came essentially from the fact that some people wanted an inheritable set and other people wanted a bounding set, and the code was some mixture of the two); and it had been argued convincincly that it could be made POSIX compliant if that is the issue. Plus, Solaris privileges also come in four sets.
If it's compatibility you're worried about, it seems to me that the user interface can be made so that it will still work with the old libcap and merely ignore the bounding set. So full binary compatibility will be achieved, at least on the user level.
Finally, if it's a matter of kernel policy, I seem to understand that my patch has a snowball's chance in hell of ever being accepted in the mainstream kernel (I mean, it's not as though this were new: patches to make capabilities work have been available ever since the sendmail exploit, and in five years they haven't ever been accepted, so I suppose there's a reason to this), so adding a fourth set of capabilities of my own initiative isn't going to change a thing there.
So what's the problem?
Quoted 4 lines
if you really want to
work on this i'll give you all the patches that have been done thus far, plus a set of tests that look at all the execve, ptrace, setuid type of corner cases.
Yes, I'm very interested in the test suite.
Signature · 3 lines
David A. Madore
([email protected],
http://www.madore.org/~david/ )Quoted 3 lines · David Madore
* Second, a much more extensive change, the patch introduces a third set of capabilities for every process, the "bounding" set. Normally the bounding set has every capability in it
How is this different in semantics from the existing 'permitted' capset?
include/linux/capabilities.h:
typedef struct __user_cap_data_struct {
__u32 effective;
__u32 permitted;
__u32 inheritable;
} __user *cap_user_data_t;Quoted 6 lines · On Tue, Aug 09, 2005 at 04:28:31PM -0400, [email protected] wrote:
Quoted 3 lines · David Madore
* Second, a much more extensive change, the patch introduces a third set of capabilities for every process, the "bounding" set. Normally the bounding set has every capability in it
How is this different in semantics from the existing 'permitted' capset?
The permitted sets is a set of capabilities really available to the process (though they may be temporarily dropped by removing them from the effective set, they are still available to take back). In contrast, the bounding set capabilities are not readily available to the process; it just means that the capabilities in question *might* be acquired by running a suid program (or setcap program if filesystem support for capabilities ever comes to Linux).
Currently this is more or less an all-or-nothing process: since capabilities can only be acquired by running a suid program, removing any capability from the bounding set means the program will never be permitted to execute a suid program any more (execve() will fail with EPERM). But maybe I'll reinstate the CAP_SETPCAP thing in some future version of the patch (I'm still waiting for someone to tell me what was wrong with CAP_SETPCAP and why it was removed), and then the bounding set should also prohibit capabilities being given through that interface.
The bottom line is: if you have some untrusted process, it might be wise to remove empty its bounding set, making it incapable of executing a suid root program and thus acquiring new capabilities. (I also plan to add some normally-available-to-all capabilities such as "permission to fork()", "permission to exec()" and so on, and then it will also be useful to remove these from a process's permitted set.)
Quoted 8 lines
include/linux/capabilities.h:typedef struct __user_cap_data_struct { __u32 effective; __u32 permitted; __u32 inheritable; } __user *cap_user_data_t;
And my patch adds a __u32 bounding to that structure.
Signature · 3 lines
David A. Madore
([email protected],
http://www.madore.org/~david/ )