file capabilities: clear fcaps on inode change (v2)
Subsystems (6)
PATCH 1/1 file capabilities: clear fcaps on inode change (v2)
Quoted 1 line
From
1376764cbb54243f088cf00c39000c4f4418f461Mon Sep 17 00:00:00 2001
From: Serge E. Hallyn <[email protected]>
Date: Mon, 6 Aug 2007 14:20:06 -0400
Subject: [PATCH 1/1] file capabilities: clear fcaps on inode change (v2)
When a file with posix capabilities is overwritten, the file capabilities, like a setuid bit, should be removed.
This patch introduces security_inode_killpriv(). This is currently only defined for capability, and is called when an inode is changed to inform the security module that it may want to clear out any privilege attached to that inode. The capability module checks whether any file capabilities are defined for the inode, and, if so, clears them.
--- fs/attr.c | 7 +++++++ fs/nfsd/vfs.c | 4 ++-- fs/open.c | 3 ++- fs/splice.c | 4 ++++ include/linux/fs.h | 1 + include/linux/security.h | 18 ++++++++++++++++++ mm/filemap.c | 5 +++++ security/capability.c | 1 + security/commoncap.c | 27 +++++++++++++++++++++++++++ security/dummy.c | 6 ++++++ security/security.c | 5 +++++ 11 files changed, 78 insertions(+), 3 deletions(-)
fs/attr.c +7 -0
@@ -116,6 +116,13 @@ int notify_change(struct dentry * dentry, struct iattr * attr) attr->ia_atime = now;if (!(ia_valid &ATTR_MTIME_SET )) attr->ia_mtime = now; +if (ia_valid &ATTR_KILL_PRIV ) { + attr->ia_valid &= ~ATTR_KILL_PRIV ; + ia_valid &= ~ATTR_KILL_PRIV ; + error =security_inode_killpriv (dentry,LSM_NO_LOCK ); +if (error) +return error; + }if (ia_valid &ATTR_KILL_SUID ) { attr->ia_valid &= ~ATTR_KILL_SUID ;if (mode &S_ISUID ) {
fs/nfsd/vfs.c +2 -2
@@ -364,7 +364,7 @@ nfsd_setattr(struct svc_rqst *rqstp, struct svc_fh *fhp, struct iattr *iap,/* Revoke setuid/setgid bit on chown/chgrp */ if ((iap->ia_valid &ATTR_UID ) && iap->ia_uid != inode->i_uid) - iap->ia_valid |=ATTR_KILL_SUID ; + iap->ia_valid |=ATTR_KILL_SUID |ATTR_KILL_PRIV ;if ((iap->ia_valid &ATTR_GID ) && iap->ia_gid != inode->i_gid) iap->ia_valid |=ATTR_KILL_SGID ;
@@ -921,7 +921,7 @@ out:static void kill_suid (struct dentry *dentry) {struct iattr ia; - ia.ia_valid =ATTR_KILL_SUID |ATTR_KILL_SGID ; + ia.ia_valid =ATTR_KILL_SUID |ATTR_KILL_SGID |ATTR_KILL_PRIV ;mutex_lock (&dentry->d_inode->i_mutex);notify_change (dentry, &ia);
fs/open.c +2 -1
@@ -658,7 +658,8 @@ static int chown_common(struct dentry * dentry, uid_t user, gid_t group) newattrs.ia_gid = group; }if (!S_ISDIR (inode->i_mode)) - newattrs.ia_valid |=ATTR_KILL_SUID |ATTR_KILL_SGID ; + newattrs.ia_valid |= +ATTR_KILL_SUID |ATTR_KILL_SGID |ATTR_KILL_PRIV ;mutex_lock (&inode->i_mutex); error =notify_change (dentry, &newattrs);mutex_unlock (&inode->i_mutex);
fs/splice.c +4 -0
@@ -827,6 +827,10 @@ generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out,ssize_t ret;int err; + err =security_inode_killpriv (out->f_path.dentry,LSM_NEED_LOCK ); +if (err) +return err; + err =should_remove_suid (out->f_path.dentry);if (unlikely (err)) {mutex_lock (&inode->i_mutex);
include/linux/fs.h +1 -0
@@ -335,6 +335,7 @@ typedef void (dio_iodone_t)(struct kiocb *iocb, loff_t offset,#define ATTR_KILL_SUID 2048#define ATTR_KILL_SGID 4096#define ATTR_FILE 8192 +#define ATTR_KILL_PRIV 16384/* * This is the Inode Attributes structure, used for notify_change(). It
include/linux/security.h +18 -0
@@ -51,6 +51,7 @@ extern void cap_bprm_apply_creds (struct linux_binprm *bprm, int unsafe);extern int cap_bprm_secureexec (struct linux_binprm *bprm);extern int cap_inode_setxattr (struct dentry *dentry,char *name,void *value,size_t size,int flags);extern int cap_inode_removexattr (struct dentry *dentry,char *name); +extern int cap_inode_killpriv (struct dentry *dentry,int needlock);extern int cap_task_post_setuid (uid_t old_ruid,uid_t old_euid,uid_t old_suid,int flags);extern void cap_task_reparent_to_init (struct task_struct *p);extern int cap_task_kill (struct task_struct *p,struct siginfo *info,int sig,u32 secid);
@@ -102,6 +103,9 @@ struct request_sock;#define LSM_UNSAFE_PTRACE 2#define LSM_UNSAFE_PTRACE_CAP 4 +#define LSM_NO_LOCK 0 +#define LSM_NEED_LOCK 1 +#ifdef CONFIG_SECURITY /**
@@ -417,6 +421,13 @@ struct request_sock;* is specified by @buffer_size. @buffer may be NULL to request * the size of the buffer required. * Returns number of bytes used/required on success. +* @inode_killpriv: +* The setuid bit is being removed. Remove similar security labels. +* @dentry is the dentry being changed. +* @needlock is 0 if the inode is locked, 1 if the LSM needs to lock +* it. +* Return 0 on success. If error is returned, then the operation +* causing setuid bit removal is failed. * * Security hooks for file operations *
@@ -1235,6 +1246,7 @@ struct security_operations {int (*inode_getxattr) (struct dentry *dentry,char *name);int (*inode_listxattr) (struct dentry *dentry);int (*inode_removexattr) (struct dentry *dentry,char *name); +int (*inode_killpriv) (struct dentry *dentry,int needlock);const char *(*inode_xattr_getsuffix) (void );int (*inode_getsecurity)(const struct inode *inode,const char *name,void *buffer,size_t size,int err);int (*inode_setsecurity)(struct inode *inode,const char *name,const void *value,size_t size,int flags);
@@ -1490,6 +1502,7 @@ void security_inode_post_setxattr(struct dentry *dentry, char *name,int security_inode_getxattr (struct dentry *dentry,char *name);int security_inode_listxattr (struct dentry *dentry);int security_inode_removexattr (struct dentry *dentry,char *name); +int security_inode_killpriv (struct dentry *dentry,int needlock);const char *security_inode_xattr_getsuffix (void );int security_inode_getsecurity (const struct inode *inode,const char *name,void *buffer,size_t size,int err);int security_inode_setsecurity (struct inode *inode,const char *name,const void *value,size_t size,int flags);
@@ -1879,6 +1892,11 @@ static inline int security_inode_removexattr (struct dentry *dentry, char *name)return cap_inode_removexattr (dentry, name); } +static inline int security_inode_killpriv (struct dentry *dentry,int needlock) +{ +return cap_inode_killpriv (dentry, needlock); +} +static inline const char *security_inode_xattr_getsuffix (void ) {return NULL ;
mm/filemap.c +5 -0
@@ -1648,6 +1648,11 @@ int __remove_suid(struct dentry *dentry, int kill)int remove_suid (struct dentry *dentry) {int kill =should_remove_suid (dentry); +int error; + + error =security_inode_killpriv (dentry,LSM_NO_LOCK ); +if (error) +return error;if (unlikely (kill))return __remove_suid (dentry, kill);
security/capability.c +1 -0
@@ -37,6 +37,7 @@ static struct security_operations capability_ops = { .inode_setxattr = cap_inode_setxattr, .inode_removexattr = cap_inode_removexattr, + .inode_removexattr = cap_inode_killpriv, .task_kill = cap_task_kill, .task_setscheduler = cap_task_setscheduler,
security/commoncap.c +27 -0
@@ -119,6 +119,27 @@ static inline void bprm_clear_caps(struct linux_binprm *bprm)#ifdef CONFIG_SECURITY_FILE_CAPABILITIES +int cap_inode_killpriv (struct dentry *dentry,int needlock) +{ +struct inode *inode = dentry->d_inode; +int error; + +if (!inode->i_op || !inode->i_op->getxattr || !inode->i_op->removexattr) +return 0 ; + + error = inode->i_op->getxattr (dentry,XATTR_NAME_CAPS ,NULL ,0 ); +if (error <=0 ) +return 0 ; + +if (needlock) +mutex_lock (&inode->i_mutex); + error = inode->i_op->removexattr (dentry,XATTR_NAME_CAPS ); +if (needlock) +mutex_unlock (&inode->i_mutex); + +return error; +} +static inline int cap_from_disk (__le32 *caps,struct linux_binprm *bprm,int size) {
@@ -185,6 +206,11 @@ out: }#else +int cap_inode_killpriv (struct dentry *dentry,int needlock) +{ +return 0 ; +} +static inline int get_file_caps (struct linux_binprm *bprm) {bprm_clear_caps (bprm);
@@ -509,6 +535,7 @@ EXPORT_SYMBOL(cap_bprm_apply_creds);EXPORT_SYMBOL (cap_bprm_secureexec);EXPORT_SYMBOL (cap_inode_setxattr);EXPORT_SYMBOL (cap_inode_removexattr); +EXPORT_SYMBOL (cap_inode_killpriv);EXPORT_SYMBOL (cap_task_post_setuid);EXPORT_SYMBOL (cap_task_kill);EXPORT_SYMBOL (cap_task_setscheduler);
security/dummy.c +6 -0
@@ -376,6 +376,11 @@ static int dummy_inode_removexattr (struct dentry *dentry, char *name)return 0 ; } +static int dummy_inode_killpriv (struct dentry *dentry,int needlock) +{ +return 0 ; +} +static int dummy_inode_getsecurity (const struct inode *inode,const char *name,void *buffer,size_t size,int err) {return -EOPNOTSUPP ;
@@ -1017,6 +1022,7 @@ void security_fixup_ops (struct security_operations *ops)set_to_dummy_if_null (ops, inode_getxattr);set_to_dummy_if_null (ops, inode_listxattr);set_to_dummy_if_null (ops, inode_removexattr); +set_to_dummy_if_null (ops, inode_killpriv);set_to_dummy_if_null (ops, inode_xattr_getsuffix);set_to_dummy_if_null (ops, inode_getsecurity);set_to_dummy_if_null (ops, inode_setsecurity);
security/security.c +5 -0
@@ -515,6 +515,11 @@ int security_inode_removexattr(struct dentry *dentry, char *name)return security_ops->inode_removexattr (dentry, name); } +int security_inode_killpriv (struct dentry *dentry,int needlock) +{ +return security_ops->inode_killpriv (dentry, needlock); +} +const char *security_inode_xattr_getsuffix (void ) {return security_ops->inode_xattr_getsuffix ();
Signature · 1 line
1.5.1.1.GIT
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Quoted 4 lines · Serge E. Hallyn
Quoted 1 line
From
1376764cbb54243f088cf00c39000c4f4418f461Mon Sep 17 00:00:00 2001From: Serge E. Hallyn <[email protected]>
Date: Mon, 6 Aug 2007 14:20:06 -0400
Subject: [PATCH 1/1] file capabilities: clear fcaps on inode change (v2)
Cheers
Andrew
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.6 (GNU/Linux)
iD8DBQFGuBZDQheEq9QabfIRAtoVAJ9uOG2q9Za0CJqmH0ueLgUHmRIABACdHW3c SykZm/puZe5JPpiVPAF2rS8=
=Smnk
-----END PGP SIGNATURE-----
James Morris
maintainerfs/splice.c generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out, · line 836
Quoted 7 lines · Serge E. Hallyn
fs/splice.c +4
-0+ err =security_inode_killpriv (out->f_path.dentry,LSM_NEED_LOCK ); +if (err) +return err; + err =should_remove_suid (out->f_path.dentry);if (unlikely (err)) {mutex_lock (&inode->i_mutex);
It seems hackish to pass a needlock arg to an API, and that that we'll end up with some conceptually similar call-outs for both caps and setuid.
How about encapsulating this stuff so that there's something like:
err = should_remove_privs(); if (err) remove_privs();
with
void remove_privs()
{
mutex_lock();
__remove_privs();
mutex_unlock();
}and then __remove_privs() handles the logic for all file privileges, including at this stage suid and the LSM call for file caps ?
- James
Signature · 2 lines
James Morris <[email protected]>
Quoted 23 lines · James Morris
fs/splice.c generic_file_splice_write(struct pipe_inode_info *pipe, struct file *out, · line 836
Quoted 7 lines · Serge E. Hallyn
fs/splice.c +4
-0+ err =security_inode_killpriv (out->f_path.dentry,LSM_NEED_LOCK ); +if (err) +return err; + err =should_remove_suid (out->f_path.dentry);if (unlikely (err)) {mutex_lock (&inode->i_mutex);It seems hackish to pass a needlock arg to an API, and that that we'll end up with some conceptually similar call-outs for both caps and setuid.
How about encapsulating this stuff so that there's something like:
err = should_remove_privs(); if (err) remove_privs();with
void
remove_privs()
Quoted 8 lines
{ mutex_lock(); __remove_privs(); mutex_unlock(); }and then
__remove_privs()handles the logic for all file privileges, including at this stage suid and the LSM call for file caps ?
The problem is that the suid bit is not removed in all cases when the file caps need to be removed. In particular, if capable(CAP_FSETID), then the suid bit is retained.
I suppose we could change those semantics, but then we'd the code still doesn't flow quite right for what you suggest - should_remove_suid() just checks whether the suid bit is set (and the process is !capable(CAP_FSETID), not whether a change has happened requiring suid change. That is already assumed to be the case.
If your main objection is to the LSM_NEED_LOCK argument, we could of course just grab the mutex around security_inode_killpriv(out->f_path.dentry) in fs/splice.c:generic_file_splice_write().
And I suppose we can in fact get rid of ATTR_KILL_PRIV. I had just put it there to split up the code a bit to make it clearer - which I do think it does.
Shall I resend without the LSM_NEED_LOCK, or do you still want a more fundamental change?
thanks,
-serge
James Morris
maintainerQuoted 2 lines · Serge E. Hallyn
Shall I resend without the
LSM_NEED_LOCK, or do you still want a more fundamental change?
Removing the needlock is enough, the rest was just a query/suggestion.
Signature · 2 lines
James Morris <[email protected]>
Quoted 7 lines · James Morris
Quoted 2 lines · Serge E. Hallyn
Shall I resend without the
LSM_NEED_LOCK, or do you still want a more fundamental change?Removing the needlock is enough, the rest was just a query/suggestion.
Ok - I'll explictly lock the i_mutex in fs/splice.c:generic_file_splice_write(). The ugliness there is that the mutex will always be taken - even if there is no xattr to be removed or even if CONFIG_SECURITY=n.
I guess I'll do some perf runs and see what kind of impact that has.
thanks,
-serge
Quoted 36 lines · Serge E. Hallyn
Quoted 1 line
From
1376764cbb54243f088cf00c39000c4f4418f461Mon Sep 17 00:00:00 2001From: Serge E. Hallyn <[email protected]>
Date: Mon, 6 Aug 2007 14:20:06 -0400
Subject: [PATCH 1/1] file capabilities: clear fcaps on inode change (v2)When a file with posix capabilities is overwritten, the file capabilities, like a setuid bit, should be removed.
This patch introduces
security_inode_killpriv(). This is currently only defined for capability, and is called when an inode is changed to inform the security module that it may want to clear out any privilege attached to that inode. The capability module checks whether any file capabilities are defined for the inode, and, if so, clears them.Signed-off-by Serge E. Hallyn <[email protected]>--- fs/attr.c | 7 +++++++ fs/nfsd/vfs.c | 4 ++-- fs/open.c | 3 ++- fs/splice.c | 4 ++++ include/linux/fs.h | 1 + include/linux/security.h | 18 ++++++++++++++++++ mm/filemap.c | 5 +++++ security/capability.c | 1 + security/commoncap.c | 27 +++++++++++++++++++++++++++ security/dummy.c | 6 ++++++ security/security.c | 5 +++++ 11 files changed, 78 insertions(+), 3 deletions(-)security/capability.c +0
-0
security/capability.c static struct security_operations capability_ops = { · line 40
Quoted 4 lines
security/capability.c +1
-0@@ -37,6 +37,7 @@ static struct security_operations capability_ops = { .inode_setxattr = cap_inode_setxattr, .inode_removexattr = cap_inode_removexattr, + .inode_removexattr = cap_inode_killpriv,
s/inode_removexattr/inode_killpriv/
Also, doesn't SELinux then need to define a corresponding hook function to call the secondary module? Otherwise, it will fall back to the dummy implementation and stacking selinux + capabilities with file caps won't yield the right behavior.
Signature · 2 lines
Stephen Smalley National Security Agency
Quoted 38 lines · Stephen Smalley
Quoted 37 lines · Serge E. Hallyn
Quoted 1 line
From
1376764cbb54243f088cf00c39000c4f4418f461Mon Sep 17 00:00:00 2001From: Serge E. Hallyn <[email protected]>
Date: Mon, 6 Aug 2007 14:20:06 -0400
Subject: [PATCH 1/1] file capabilities: clear fcaps on inode change (v2)When a file with posix capabilities is overwritten, the file capabilities, like a setuid bit, should be removed.
This patch introduces
security_inode_killpriv(). This is currently only defined for capability, and is called when an inode is changed to inform the security module that it may want to clear out any privilege attached to that inode. The capability module checks whether any file capabilities are defined for the inode, and, if so, clears them.Signed-off-by Serge E. Hallyn <[email protected]>--- fs/attr.c | 7 +++++++ fs/nfsd/vfs.c | 4 ++-- fs/open.c | 3 ++- fs/splice.c | 4 ++++ include/linux/fs.h | 1 + include/linux/security.h | 18 ++++++++++++++++++ mm/filemap.c | 5 +++++ security/capability.c | 1 + security/commoncap.c | 27 +++++++++++++++++++++++++++ security/dummy.c | 6 ++++++ security/security.c | 5 +++++ 11 files changed, 78 insertions(+), 3 deletions(-)security/capability.c +0
-0@@ -37,6 +37,7 @@ static struct security_operations capability_ops = {
Quoted 5 lines
security/capability.c static struct security_operations capability_ops = { · line 40
Quoted 3 lines
security/capability.c +1
-0.inode_setxattr = cap_inode_setxattr, .inode_removexattr = cap_inode_removexattr, + .inode_removexattr = cap_inode_killpriv,s/inode_removexattr/inode_killpriv/
Well crap - I had fixed that an hour before I sent it. Which makes me wonder which version I sent...
Quoted 4 lines
Also, doesn't SELinux then need to define a corresponding hook function to call the secondary module? Otherwise, it will fall back to the dummy implementation and stacking selinux + capabilities with file caps won't yield the right behavior.
Yes it does. Will fix that on resend.
thanks,
-serge