Skip to content

Commit b537677

Browse files
Serge E. HallynLinus Torvalds
authored andcommitted
Implement file posix capabilities
Implement file posix capabilities. This allows programs to be given a subset of root's powers regardless of who runs them, without having to use setuid and giving the binary all of root's powers. This version works with Kaigai Kohei's userspace tools, found at http://www.kaigai.gr.jp/index.php. For more information on how to use this patch, Chris Friedhoff has posted a nice page at http://www.friedhoff.org/fscaps.html. Changelog: Nov 27: Incorporate fixes from Andrew Morton (security-introduce-file-caps-tweaks and security-introduce-file-caps-warning-fix) Fix Kconfig dependency. Fix change signaling behavior when file caps are not compiled in. Nov 13: Integrate comments from Alexey: Remove CONFIG_ ifdef from capability.h, and use %zd for printing a size_t. Nov 13: Fix endianness warnings by sparse as suggested by Alexey Dobriyan. Nov 09: Address warnings of unused variables at cap_bprm_set_security when file capabilities are disabled, and simultaneously clean up the code a little, by pulling the new code into a helper function. Nov 08: For pointers to required userspace tools and how to use them, see http://www.friedhoff.org/fscaps.html. Nov 07: Fix the calculation of the highest bit checked in check_cap_sanity(). Nov 07: Allow file caps to be enabled without CONFIG_SECURITY, since capabilities are the default. Hook cap_task_setscheduler when !CONFIG_SECURITY. Move capable(TASK_KILL) to end of cap_task_kill to reduce audit messages. Nov 05: Add secondary calls in selinux/hooks.c to task_setioprio and task_setscheduler so that selinux and capabilities with file cap support can be stacked. Sep 05: As Seth Arnold points out, uid checks are out of place for capability code. Sep 01: Define task_setscheduler, task_setioprio, cap_task_kill, and task_setnice to make sure a user cannot affect a process in which they called a program with some fscaps. One remaining question is the note under task_setscheduler: are we ok with CAP_SYS_NICE being sufficient to confine a process to a cpuset? It is a semantic change, as without fsccaps, attach_task doesn't allow CAP_SYS_NICE to override the uid equivalence check. But since it uses security_task_setscheduler, which elsewhere is used where CAP_SYS_NICE can be used to override the uid equivalence check, fixing it might be tough. task_setscheduler note: this also controls cpuset:attach_task. Are we ok with CAP_SYS_NICE being used to confine to a cpuset? task_setioprio task_setnice sys_setpriority uses this (through set_one_prio) for another process. Need same checks as setrlimit Aug 21: Updated secureexec implementation to reflect the fact that euid and uid might be the same and nonzero, but the process might still have elevated caps. Aug 15: Handle endianness of xattrs. Enforce capability version match between kernel and disk. Enforce that no bits beyond the known max capability are set, else return -EPERM. With this extra processing, it may be worth reconsidering doing all the work at bprm_set_security rather than d_instantiate. Aug 10: Always call getxattr at bprm_set_security, rather than caching it at d_instantiate. [morgan@kernel.org: file-caps clean up for linux/capability.h] [bunk@kernel.org: unexport cap_inode_killpriv] Signed-off-by: Serge E. Hallyn <serue@us.ibm.com> Cc: Stephen Smalley <sds@tycho.nsa.gov> Cc: James Morris <jmorris@namei.org> Cc: Chris Wright <chrisw@sous-sol.org> Cc: Andrew Morgan <morgan@kernel.org> Signed-off-by: Andrew Morgan <morgan@kernel.org> Signed-off-by: Adrian Bunk <bunk@kernel.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 57c521c commit b537677

15 files changed

Lines changed: 418 additions & 73 deletions

File tree

‎fs/attr.c‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ int notify_change(struct dentry * dentry, struct iattr * attr)
116116
attr->ia_atime = now;
117117
if (!(ia_valid & ATTR_MTIME_SET))
118118
attr->ia_mtime = now;
119+
if (ia_valid & ATTR_KILL_PRIV) {
120+
attr->ia_valid &= ~ATTR_KILL_PRIV;
121+
ia_valid &= ~ATTR_KILL_PRIV;
122+
error = security_inode_need_killpriv(dentry);
123+
if (error > 0)
124+
error = security_inode_killpriv(dentry);
125+
if (error)
126+
return error;
127+
}
119128
if (ia_valid & ATTR_KILL_SUID) {
120129
attr->ia_valid &= ~ATTR_KILL_SUID;
121130
if (mode & S_ISUID) {

‎fs/nfsd/vfs.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,
368368

369369
/* Revoke setuid/setgid bit on chown/chgrp */
370370
if ((iap->ia_valid & ATTR_UID) && iap->ia_uid != inode->i_uid)
371-
iap->ia_valid |= ATTR_KILL_SUID;
371+
iap->ia_valid |= ATTR_KILL_SUID | ATTR_KILL_PRIV;
372372
if ((iap->ia_valid & ATTR_GID) && iap->ia_gid != inode->i_gid)
373373
iap->ia_valid |= ATTR_KILL_SGID;
374374

@@ -937,7 +937,7 @@ nfsd_vfs_read(struct svc_rqst *rqstp, struct svc_fh *fhp, struct file *file,
937937
static void kill_suid(struct dentry *dentry)
938938
{
939939
struct iattr ia;
940-
ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID;
940+
ia.ia_valid = ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
941941

942942
mutex_lock(&dentry->d_inode->i_mutex);
943943
notify_change(dentry, &ia);

‎fs/open.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,7 +658,8 @@ static int chown_common(struct dentry * dentry, uid_t user, gid_t group)
658658
newattrs.ia_gid = group;
659659
}
660660
if (!S_ISDIR(inode->i_mode))
661-
newattrs.ia_valid |= ATTR_KILL_SUID|ATTR_KILL_SGID;
661+
newattrs.ia_valid |=
662+
ATTR_KILL_SUID | ATTR_KILL_SGID | ATTR_KILL_PRIV;
662663
mutex_lock(&inode->i_mutex);
663664
error = notify_change(dentry, &newattrs);
664665
mutex_unlock(&inode->i_mutex);

‎fs/splice.c‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -824,13 +824,18 @@ generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,
824824
{
825825
struct address_space *mapping = out->f_mapping;
826826
struct inode *inode = mapping->host;
827+
int killsuid, killpriv;
827828
ssize_t ret;
828-
int err;
829+
int err = 0;
829830

830-
err = should_remove_suid(out->f_path.dentry);
831-
if (unlikely(err)) {
831+
killpriv = security_inode_need_killpriv(out->f_path.dentry);
832+
killsuid = should_remove_suid(out->f_path.dentry);
833+
if (unlikely(killsuid || killpriv)) {
832834
mutex_lock(&inode->i_mutex);
833-
err = __remove_suid(out->f_path.dentry, err);
835+
if (killpriv)
836+
err = security_inode_killpriv(out->f_path.dentry);
837+
if (!err && killsuid)
838+
err = __remove_suid(out->f_path.dentry, killsuid);
834839
mutex_unlock(&inode->i_mutex);
835840
if (err)
836841
return err;

‎include/linux/binfmts.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ struct linux_binprm{
3737
int sh_bang;
3838
struct file * file;
3939
int e_uid, e_gid;
40-
kernel_cap_t cap_inheritable, cap_permitted, cap_effective;
40+
kernel_cap_t cap_inheritable, cap_permitted;
41+
bool cap_effective;
4142
void *security;
4243
int argc, envc;
4344
char * filename; /* Name of binary as seen by procps */

‎include/linux/capability.h‎

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
/*
22
* This is <linux/capability.h>
33
*
4-
* Andrew G. Morgan <morgan@transmeta.com>
4+
* Andrew G. Morgan <morgan@kernel.org>
55
* Alexander Kjeldaas <astor@guardian.no>
66
* with help from Aleph1, Roland Buresund and Andrew Main.
77
*
88
* See here for the libcap library ("POSIX draft" compliance):
99
*
10-
* ftp://linux.kernel.org/pub/linux/libs/security/linux-privs/kernel-2.2/
11-
*/
10+
* ftp://linux.kernel.org/pub/linux/libs/security/linux-privs/kernel-2.6/
11+
*/
1212

1313
#ifndef _LINUX_CAPABILITY_H
1414
#define _LINUX_CAPABILITY_H
@@ -28,23 +28,41 @@ struct task_struct;
2828
following structure to such a composite is better handled in a user
2929
library since the draft standard requires the use of malloc/free
3030
etc.. */
31-
31+
3232
#define _LINUX_CAPABILITY_VERSION 0x19980330
3333

3434
typedef struct __user_cap_header_struct {
3535
__u32 version;
3636
int pid;
3737
} __user *cap_user_header_t;
38-
38+
3939
typedef struct __user_cap_data_struct {
4040
__u32 effective;
4141
__u32 permitted;
4242
__u32 inheritable;
4343
} __user *cap_user_data_t;
44-
45-
#ifdef __KERNEL__
4644

47-
#include <asm/current.h>
45+
#define XATTR_CAPS_SUFFIX "capability"
46+
#define XATTR_NAME_CAPS XATTR_SECURITY_PREFIX XATTR_CAPS_SUFFIX
47+
48+
#define XATTR_CAPS_SZ (3*sizeof(__le32))
49+
#define VFS_CAP_REVISION_MASK 0xFF000000
50+
#define VFS_CAP_REVISION_1 0x01000000
51+
52+
#define VFS_CAP_REVISION VFS_CAP_REVISION_1
53+
54+
#define VFS_CAP_FLAGS_MASK ~VFS_CAP_REVISION_MASK
55+
#define VFS_CAP_FLAGS_EFFECTIVE 0x000001
56+
57+
struct vfs_cap_data {
58+
__u32 magic_etc; /* Little endian */
59+
struct {
60+
__u32 permitted; /* Little endian */
61+
__u32 inheritable; /* Little endian */
62+
} data[1];
63+
};
64+
65+
#ifdef __KERNEL__
4866

4967
/* #define STRICT_CAP_T_TYPECHECKS */
5068

@@ -59,15 +77,15 @@ typedef struct kernel_cap_struct {
5977
typedef __u32 kernel_cap_t;
6078

6179
#endif
62-
80+
6381
#define _USER_CAP_HEADER_SIZE (2*sizeof(__u32))
6482
#define _KERNEL_CAP_T_SIZE (sizeof(kernel_cap_t))
6583

6684
#endif
6785

6886

6987
/**
70-
** POSIX-draft defined capabilities.
88+
** POSIX-draft defined capabilities.
7189
**/
7290

7391
/* In a system with the [_POSIX_CHOWN_RESTRICTED] option defined, this
@@ -87,7 +105,7 @@ typedef __u32 kernel_cap_t;
87105
defined. Excluding DAC access covered by CAP_LINUX_IMMUTABLE. */
88106

89107
#define CAP_DAC_READ_SEARCH 2
90-
108+
91109
/* Overrides all restrictions about allowed operations on files, where
92110
file owner ID must be equal to the user ID, except where CAP_FSETID
93111
is applicable. It doesn't override MAC and DAC restrictions. */
@@ -257,7 +275,7 @@ typedef __u32 kernel_cap_t;
257275
/* Override reserved space on ext2 filesystem */
258276
/* Modify data journaling mode on ext3 filesystem (uses journaling
259277
resources) */
260-
/* NOTE: ext2 honors fsuid when checking for resource overrides, so
278+
/* NOTE: ext2 honors fsuid when checking for resource overrides, so
261279
you can override using fsuid too */
262280
/* Override size restrictions on IPC message queues */
263281
/* Allow more than 64hz interrupts from the real-time clock */
@@ -289,16 +307,18 @@ typedef __u32 kernel_cap_t;
289307

290308
#define CAP_AUDIT_CONTROL 30
291309

310+
#define CAP_SETFCAP 31
311+
292312
#ifdef __KERNEL__
293-
/*
313+
/*
294314
* Bounding set
295315
*/
296316
extern kernel_cap_t cap_bset;
297317

298318
/*
299319
* Internal kernel functions only
300320
*/
301-
321+
302322
#ifdef STRICT_CAP_T_TYPECHECKS
303323

304324
#define to_cap_t(x) { x }

‎include/linux/fs.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,7 @@ typedef void (dio_iodone_t)(struct kiocb *iocb, loff_t offset,
329329
#define ATTR_KILL_SUID 2048
330330
#define ATTR_KILL_SGID 4096
331331
#define ATTR_FILE 8192
332+
#define ATTR_KILL_PRIV 16384
332333

333334
/*
334335
* This is the Inode Attributes structure, used for notify_change(). It

‎include/linux/security.h‎

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,14 @@ extern void cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe);
5151
extern int cap_bprm_secureexec(struct linux_binprm *bprm);
5252
extern int cap_inode_setxattr(struct dentry *dentry, char *name, void *value, size_t size, int flags);
5353
extern int cap_inode_removexattr(struct dentry *dentry, char *name);
54+
extern int cap_inode_need_killpriv(struct dentry *dentry);
55+
extern int cap_inode_killpriv(struct dentry *dentry);
5456
extern int cap_task_post_setuid (uid_t old_ruid, uid_t old_euid, uid_t old_suid, int flags);
5557
extern void cap_task_reparent_to_init (struct task_struct *p);
58+
extern int cap_task_kill(struct task_struct *p, struct siginfo *info, int sig, u32 secid);
59+
extern int cap_task_setscheduler (struct task_struct *p, int policy, struct sched_param *lp);
60+
extern int cap_task_setioprio (struct task_struct *p, int ioprio);
61+
extern int cap_task_setnice (struct task_struct *p, int nice);
5662
extern int cap_syslog (int type);
5763
extern int cap_vm_enough_memory(struct mm_struct *mm, long pages);
5864

@@ -413,6 +419,18 @@ struct request_sock;
413419
* is specified by @buffer_size. @buffer may be NULL to request
414420
* the size of the buffer required.
415421
* Returns number of bytes used/required on success.
422+
* @inode_need_killpriv:
423+
* Called when an inode has been changed.
424+
* @dentry is the dentry being changed.
425+
* Return <0 on error to abort the inode change operation.
426+
* Return 0 if inode_killpriv does not need to be called.
427+
* Return >0 if inode_killpriv does need to be called.
428+
* @inode_killpriv:
429+
* The setuid bit is being removed. Remove similar security labels.
430+
* Called with the dentry->d_inode->i_mutex held.
431+
* @dentry is the dentry being changed.
432+
* Return 0 on success. If error is returned, then the operation
433+
* causing setuid bit removal is failed.
416434
*
417435
* Security hooks for file operations
418436
*
@@ -1239,6 +1257,8 @@ struct security_operations {
12391257
int (*inode_getxattr) (struct dentry *dentry, char *name);
12401258
int (*inode_listxattr) (struct dentry *dentry);
12411259
int (*inode_removexattr) (struct dentry *dentry, char *name);
1260+
int (*inode_need_killpriv) (struct dentry *dentry);
1261+
int (*inode_killpriv) (struct dentry *dentry);
12421262
const char *(*inode_xattr_getsuffix) (void);
12431263
int (*inode_getsecurity)(const struct inode *inode, const char *name, void *buffer, size_t size, int err);
12441264
int (*inode_setsecurity)(struct inode *inode, const char *name, const void *value, size_t size, int flags);
@@ -1496,6 +1516,8 @@ void security_inode_post_setxattr(struct dentry *dentry, char *name,
14961516
int security_inode_getxattr(struct dentry *dentry, char *name);
14971517
int security_inode_listxattr(struct dentry *dentry);
14981518
int security_inode_removexattr(struct dentry *dentry, char *name);
1519+
int security_inode_need_killpriv(struct dentry *dentry);
1520+
int security_inode_killpriv(struct dentry *dentry);
14991521
const char *security_inode_xattr_getsuffix(void);
15001522
int security_inode_getsecurity(const struct inode *inode, const char *name, void *buffer, size_t size, int err);
15011523
int security_inode_setsecurity(struct inode *inode, const char *name, const void *value, size_t size, int flags);
@@ -1891,6 +1913,16 @@ static inline int security_inode_removexattr (struct dentry *dentry, char *name)
18911913
return cap_inode_removexattr(dentry, name);
18921914
}
18931915

1916+
static inline int security_inode_need_killpriv(struct dentry *dentry)
1917+
{
1918+
return cap_inode_need_killpriv(dentry);
1919+
}
1920+
1921+
static inline int security_inode_killpriv(struct dentry *dentry)
1922+
{
1923+
return cap_inode_killpriv(dentry);
1924+
}
1925+
18941926
static inline const char *security_inode_xattr_getsuffix (void)
18951927
{
18961928
return NULL ;
@@ -2035,12 +2067,12 @@ static inline int security_task_setgroups (struct group_info *group_info)
20352067

20362068
static inline int security_task_setnice (struct task_struct *p, int nice)
20372069
{
2038-
return 0;
2070+
return cap_task_setnice(p, nice);
20392071
}
20402072

20412073
static inline int security_task_setioprio (struct task_struct *p, int ioprio)
20422074
{
2043-
return 0;
2075+
return cap_task_setioprio(p, ioprio);
20442076
}
20452077

20462078
static inline int security_task_getioprio (struct task_struct *p)
@@ -2058,7 +2090,7 @@ static inline int security_task_setscheduler (struct task_struct *p,
20582090
int policy,
20592091
struct sched_param *lp)
20602092
{
2061-
return 0;
2093+
return cap_task_setscheduler(p, policy, lp);
20622094
}
20632095

20642096
static inline int security_task_getscheduler (struct task_struct *p)
@@ -2075,7 +2107,7 @@ static inline int security_task_kill (struct task_struct *p,
20752107
struct siginfo *info, int sig,
20762108
u32 secid)
20772109
{
2078-
return 0;
2110+
return cap_task_kill(p, info, sig, secid);
20792111
}
20802112

20812113
static inline int security_task_wait (struct task_struct *p)

‎mm/filemap.c‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,12 +1627,18 @@ int __remove_suid(struct dentry *dentry, int kill)
16271627

16281628
int remove_suid(struct dentry *dentry)
16291629
{
1630-
int kill = should_remove_suid(dentry);
1630+
int killsuid = should_remove_suid(dentry);
1631+
int killpriv = security_inode_need_killpriv(dentry);
1632+
int error = 0;
16311633

1632-
if (unlikely(kill))
1633-
return __remove_suid(dentry, kill);
1634+
if (killpriv < 0)
1635+
return killpriv;
1636+
if (killpriv)
1637+
error = security_inode_killpriv(dentry);
1638+
if (!error && killsuid)
1639+
error = __remove_suid(dentry, killsuid);
16341640

1635-
return 0;
1641+
return error;
16361642
}
16371643
EXPORT_SYMBOL(remove_suid);
16381644

‎security/Kconfig‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,16 @@ config SECURITY_CAPABILITIES
8080
This enables the "default" Linux capabilities functionality.
8181
If you are unsure how to answer this question, answer Y.
8282

83+
config SECURITY_FILE_CAPABILITIES
84+
bool "File POSIX Capabilities (EXPERIMENTAL)"
85+
depends on (SECURITY=n || SECURITY_CAPABILITIES!=n) && EXPERIMENTAL
86+
default n
87+
help
88+
This enables filesystem capabilities, allowing you to give
89+
binaries a subset of root's powers without using setuid 0.
90+
91+
If in doubt, answer N.
92+
8393
config SECURITY_ROOTPLUG
8494
bool "Root Plug Support"
8595
depends on USB=y && SECURITY

0 commit comments

Comments
 (0)