Overlayfs SELinux Support
Subsystems (4)
v2 cover Overlayfs SELinux Support
Hi,
Please find attached the V2 of patches. I have take care of feedback from round 1.
Following are the changes since V1.
- Broke down patches as mentioned by paul moore.
- Dropped last patch of the series where we skipped permission checks in ovl_getxattr(). Now there should not be any info leak. This means in some uncommon configurations (non-context mount where mounter can't do getattr on underlying file), overlay inode selinux label will be labeled unlabeled_t and there will be warning on console about this every time it happens.
- Modified security_inode_copy_up() and security_inode_copy_up_xattr() as mentioned by Casey.
Original description of patches follows.
Following are RFC patches to support SELinux with overlayfs. I started with David Howells's latest posting on this topic and started modifying patches. These patches apply on top of overlayfs-next branch of miklos vfs git tree.
git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/vfs.git overlayfs-next
These patches can be pulled from my branch too.
https://github.com/rhvgoyal/linux/commits/overlayfs-selinux-mounter-next
Thanks to Dan Walsh, Stephen Smalley and Miklos Szeredi for numerous conversation and ideas in helping figuring out what one reasonable implementation might look like.
Dan Walsh has been writing tests for selinux overlayfs in selinux-testsuite. These patches pass those tests except one. I think that test/policy need to be fixed.
https://github.com/rhatdan/selinux-testsuite/commits/master
Posting these patches for review and comments.
These patches introduce 3 new security hooks.
- security_inode_copy_up(), is called when a file is copied up. This hook prepares a new set of cred which is used for copy up operation. And new set of creds are prepared so that ->create_sid can be set appropriately and newly created file is labeled properly.
When a file is copied up, label of lower file is retained except for the case of context= mount where new file gets the label from context= option.
- security_inode_copy_up_xattr(), is called when xattrs of a file are being copied up. Before this we already called security_inode_copy_up() and created new file and copied up data. That means file already got labeled properly and there is no need to take SELINUX xattr of lower file and overwrite the upper file xattr. So this hook is used to avoid copying up of SELINUX xattr.
- dentry_create_files_as(), is called when a new file is about to be created. This hook determines what the label of the file should be if task had created that file in upper/ and sets create_sid accordingly in the passed in creds.
Normal transition rules don't work for the case of context mounts as underlying file system is not aware of context option which only overlay layer is aware of. For non-context mounts, creation can happen in work/ dir first and then file might be renamed into upper/, and it might get label based on work/ dir. So this hooks helps avoiding all these issues.
When a new file is created in upper/, it gets its label based on transition rules. For the case of context mount, it gets the label from context= option.
Any feedback is welcome.
Thanks
Vivek
Vivek Goyal (7):
security, overlayfs: provide copy up security hook for unioned files
selinux: Implementation for inode_copy_up() hook security,overlayfs: Provide security hook for copy up of xattrs for
overlay file
selinux: Implementation for inode_copy_up_xattr() hook
selinux: Pass security pointer to determine_inode_label() security, overlayfs: Provide hook to correctly label newly created
files
selinux: Implement dentry_create_files_as() hook
fs/overlayfs/copy_up.c | 25 +++++++++++++++ fs/overlayfs/dir.c | 10 ++++++ include/linux/lsm_hooks.h | 36 ++++++++++++++++++++++ include/linux/security.h | 24 +++++++++++++++ security/security.c | 27 ++++++++++++++++ security/selinux/hooks.c | 78 +++++++++++++++++++++++++++++++++++++++++------ 6 files changed, 191 insertions(+), 9 deletions(-)
Signature · 1 line
2.7.4
PATCH v2 1/7 security, overlayfs: provide copy up security hook for unioned files
Provide a security hook to label new file correctly when a file is copied up from lower layer to upper layer of a overlay/union mount.
This hook can prepare a new set of creds which are suitable for new file creation during copy up. Caller will use new creds to create file and then revert back to old creds and release new creds.
--- fs/overlayfs/copy_up.c | 18 ++++++++++++++++++ include/linux/lsm_hooks.h | 11 +++++++++++ include/linux/security.h | 6 ++++++ security/security.c | 8 ++++++++ 4 files changed, 43 insertions(+)
fs/overlayfs/copy_up.c +18 -0
@@ -246,6 +246,8 @@ static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,struct dentry *upper =NULL ;umode_t mode = stat->mode;int err; +const struct cred *old_creds =NULL ; +struct cred *new_creds =NULL ; newdentry =ovl_lookup_temp (workdir, dentry); err =PTR_ERR (newdentry);
@@ -258,10 +260,26 @@ static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,if (IS_ERR (upper)) goto out1; + err =security_inode_copy_up (dentry, &new_creds); +if (err <0 ) { +if (new_creds) +put_cred (new_creds); + goto out2; + } + +if (new_creds) + old_creds =override_creds (new_creds); +/* Can't properly set mode on creation because of the umask */ stat->mode &=S_IFMT ; err =ovl_create_real (wdir, newdentry, stat, link,NULL , true); stat->mode = mode; + +if (new_creds) { +revert_creds (old_creds); +put_cred (new_creds); + } +if (err) goto out2;
include/linux/lsm_hooks.h +11 -0
@@ -401,6 +401,15 @@* @inode contains a pointer to the inode. * @secid contains a pointer to the location where result will be saved. * In case of failure, @secid will be set to zero. +* @inode_copy_up: +* A file is about to be copied up from lower layer to upper layer of +* overlay filesystem. Security module can prepare a set of new creds +* and modify as need be and return new creds. Caller will switch to +* new creds temporarily to create new file and release newly allocated +* creds. +* @src indicates the union dentry of file that is being copied up. +* @new pointer to pointer to return newly allocated creds. +* Returns 0 on success or a negative error code on error. * * Security hooks for file operations *
@@ -1425,6 +1434,7 @@ union security_list_options {int (*inode_listsecurity)(struct inode *inode,char *buffer,size_t buffer_size);void (*inode_getsecid)(struct inode *inode,u32 *secid); +int (*inode_copy_up) (struct dentry *src,struct cred **new);int (*file_permission)(struct file *file,int mask);int (*file_alloc_security)(struct file *file);
@@ -1696,6 +1706,7 @@ struct security_hook_heads {struct list_head inode_setsecurity;struct list_head inode_listsecurity;struct list_head inode_getsecid; +struct list_head inode_copy_up;struct list_head file_permission;struct list_head file_alloc_security;struct list_head file_free_security;
include/linux/security.h +6 -0
@@ -282,6 +282,7 @@ int security_inode_getsecurity(struct inode *inode, const char *name, void **bufint security_inode_setsecurity (struct inode *inode,const char *name,const void *value,size_t size,int flags);int security_inode_listsecurity (struct inode *inode,char *buffer,size_t buffer_size);void security_inode_getsecid (struct inode *inode,u32 *secid); +int security_inode_copy_up (struct dentry *src,struct cred **new);int security_file_permission (struct file *file,int mask);int security_file_alloc (struct file *file);void security_file_free (struct file *file);
@@ -758,6 +759,11 @@ static inline void security_inode_getsecid(struct inode *inode, u32 *secid) *secid =0 ; } +static inline int security_inode_copy_up (struct dentry *src,struct cred **new) +{ +return 0 ; +} +static inline int security_file_permission (struct file *file,int mask) {return 0 ;
security/security.c +8 -0
@@ -727,6 +727,12 @@ void security_inode_getsecid(struct inode *inode, u32 *secid)call_void_hook (inode_getsecid, inode, secid); } +int security_inode_copy_up (struct dentry *src,struct cred **new) +{ +return call_int_hook (inode_copy_up,0 , src, new); +} +EXPORT_SYMBOL (security_inode_copy_up); +int security_file_permission (struct file *file,int mask) {int ret;
@@ -1663,6 +1669,8 @@ struct security_hook_heads security_hook_heads = { LIST_HEAD_INIT(security_hook_heads.inode_listsecurity), .inode_getsecid = LIST_HEAD_INIT(security_hook_heads.inode_getsecid), + .inode_copy_up = + LIST_HEAD_INIT(security_hook_heads.inode_copy_up), .file_permission = LIST_HEAD_INIT(security_hook_heads.file_permission), .file_alloc_security =
Signature · 1 line
2.7.4
Re: [PATCH 1/7] security, overlayfs: provide copy up security hook for unioned files
Quoted 29 lines · Vivek Goyal
Provide a security hook to label new file correctly when a file is copied up from lower layer to upper layer of a overlay/union mount.
This hook can prepare a new set of creds which are suitable for new file creation during copy up. Caller will use new creds to create file and then revert back to old creds and release new creds.
Signed-off-by Vivek Goyal <[email protected]>--- fs/overlayfs/copy_up.c | 18 ++++++++++++++++++ include/linux/lsm_hooks.h | 11 +++++++++++ include/linux/security.h | 6 ++++++ security/security.c | 8 ++++++++ 4 files changed, 43 insertions(+)fs/overlayfs/copy_up.c +2
-0@@ -246,6 +246,8 @@ static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,struct dentry *upper =NULL ;umode_t mode = stat->mode;int err; +const struct cred *old_creds =NULL ; +struct cred *new_creds =NULL ; newdentry =ovl_lookup_temp (workdir, dentry); err =PTR_ERR (newdentry);
fs/overlayfs/copy_up.c static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir, · line 266
Quoted 7 lines
fs/overlayfs/copy_up.c +4
-0@@ -258,10 +260,26 @@ static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,if (IS_ERR (upper)) goto out1; + err =security_inode_copy_up (dentry, &new_creds); +if (err <0 ) { +if (new_creds) +put_cred (new_creds);
Why do we need a put_cred() here?
Quoted 106 lines
fs/overlayfs/copy_up.c +12
-0+ goto out2; + } + +if (new_creds) + old_creds =override_creds (new_creds); +/* Can't properly set mode on creation because of the umask */ stat->mode &=S_IFMT ; err =ovl_create_real (wdir, newdentry, stat, link,NULL , true); stat->mode = mode; + +if (new_creds) { +revert_creds (old_creds); +put_cred (new_creds); + } +if (err) goto out2;include/linux/lsm_hooks.h +11
-0@@ -401,6 +401,15 @@* @inode contains a pointer to the inode. * @secid contains a pointer to the location where result will be saved. * In case of failure, @secid will be set to zero. +* @inode_copy_up: +* A file is about to be copied up from lower layer to upper layer of +* overlay filesystem. Security module can prepare a set of new creds +* and modify as need be and return new creds. Caller will switch to +* new creds temporarily to create new file and release newly allocated +* creds. +* @src indicates the union dentry of file that is being copied up. +* @new pointer to pointer to return newly allocated creds. +* Returns 0 on success or a negative error code on error. * * Security hooks for file operations * @@ -1425,6 +1434,7 @@ union security_list_options {int (*inode_listsecurity)(struct inode *inode,char *buffer,size_t buffer_size);void (*inode_getsecid)(struct inode *inode,u32 *secid); +int (*inode_copy_up) (struct dentry *src,struct cred **new);int (*file_permission)(struct file *file,int mask);int (*file_alloc_security)(struct file *file);@@ -1696,6 +1706,7 @@ struct security_hook_heads {struct list_head inode_setsecurity;struct list_head inode_listsecurity;struct list_head inode_getsecid; +struct list_head inode_copy_up;struct list_head file_permission;struct list_head file_alloc_security;struct list_head file_free_security;include/linux/security.h +6
-0@@ -282,6 +282,7 @@ int security_inode_getsecurity(struct inode *inode, const char *name, void **bufint security_inode_setsecurity (struct inode *inode,const char *name,const void *value,size_t size,int flags);int security_inode_listsecurity (struct inode *inode,char *buffer,size_t buffer_size);void security_inode_getsecid (struct inode *inode,u32 *secid); +int security_inode_copy_up (struct dentry *src,struct cred **new);int security_file_permission (struct file *file,int mask);int security_file_alloc (struct file *file);void security_file_free (struct file *file);@@ -758,6 +759,11 @@ static inline void security_inode_getsecid(struct inode *inode, u32 *secid) *secid =0 ; } +static inline int security_inode_copy_up (struct dentry *src,struct cred **new) +{ +return 0 ; +} +static inline int security_file_permission (struct file *file,int mask) {return 0 ;security/security.c +8
-0@@ -727,6 +727,12 @@ void security_inode_getsecid(struct inode *inode, u32 *secid)call_void_hook (inode_getsecid, inode, secid); } +int security_inode_copy_up (struct dentry *src,struct cred **new) +{ +return call_int_hook (inode_copy_up,0 , src, new); +} +EXPORT_SYMBOL (security_inode_copy_up); +int security_file_permission (struct file *file,int mask) {int ret;@@ -1663,6 +1669,8 @@ struct security_hook_heads security_hook_heads = { LIST_HEAD_INIT(security_hook_heads.inode_listsecurity), .inode_getsecid = LIST_HEAD_INIT(security_hook_heads.inode_getsecid), + .inode_copy_up = + LIST_HEAD_INIT(security_hook_heads.inode_copy_up), .file_permission = LIST_HEAD_INIT(security_hook_heads.file_permission), .file_alloc_security =
Re: [PATCH 1/7] security, overlayfs: provide copy up security hook for unioned files
Quoted 33 lines · Stephen Smalley
Quoted 32 lines · Vivek Goyal
Provide a security hook to label new file correctly when a file is copied up from lower layer to upper layer of a overlay/union mount.
This hook can prepare a new set of creds which are suitable for new file creation during copy up. Caller will use new creds to create file and then revert back to old creds and release new creds.
Signed-off-by Vivek Goyal <[email protected]>--- fs/overlayfs/copy_up.c | 18 ++++++++++++++++++ include/linux/lsm_hooks.h | 11 +++++++++++ include/linux/security.h | 6 ++++++ security/security.c | 8 ++++++++ 4 files changed, 43 insertions(+)fs/overlayfs/copy_up.c +2
-0@@ -246,6 +246,8 @@ static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,struct dentry *upper =NULL ;umode_t mode = stat->mode;int err; +const struct cred *old_creds =NULL ; +struct cred *new_creds =NULL ; newdentry =ovl_lookup_temp (workdir, dentry); err =PTR_ERR (newdentry);@@ -258,10 +260,26 @@ static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir,if (IS_ERR (upper)) goto out1;
Quoted 6 lines
fs/overlayfs/copy_up.c static int ovl_copy_up_locked(struct dentry *workdir, struct dentry *upperdir, · line 266
Quoted 4 lines
fs/overlayfs/copy_up.c +4
-0+ err =security_inode_copy_up (dentry, &new_creds); +if (err <0 ) { +if (new_creds) +put_cred (new_creds);Why do we need a
put_cred()here?
Being paranoid for the case of stacked modules. Say first module allocated creds but second module returned error, in that case creds will have to be freed.
I can get rid of it for now and if in future two LSMs implement this hook, one can change it, if need be.
Thanks
Vivek
PATCH v2 2/7 selinux: Implementation for inode_copy_up() hook
In reply to Vivek GoyalA file is being copied up for overlay file system. Prepare a new set of creds and set create_sid appropriately so that new file is created with appropriate label.
Overlay inode has right label for both context and non-context mount cases. In case of non-context mount, overlay inode will have the label of lower file and in case of context mount, overlay inode will have the label from context= mount option.
--- security/selinux/hooks.c | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+)
security/selinux/hooks.c +21 -0
@@ -3270,6 +3270,26 @@ static void selinux_inode_getsecid(struct inode *inode, u32 *secid) *secid = isec->sid; } +static int selinux_inode_copy_up (struct dentry *src,struct cred **new) +{ +u32 sid; +struct task_security_struct *tsec; +struct cred *new_creds = *new; + +if (new_creds ==NULL ) { + new_creds =prepare_creds (); +if (!new_creds) +return -ENOMEM ; + } + + tsec = new_creds->security; +/* Get label from overlay inode and set it in create_sid */ +selinux_inode_getsecid (d_inode (src), &sid); + tsec->create_sid = sid; + *new = new_creds; +return 0 ; +} +/* file security operations */ static int selinux_revalidate_file_permission (struct file *file,int mask)
@@ -6056,6 +6076,7 @@ static struct security_hook_list selinux_hooks[] = {LSM_HOOK_INIT (inode_setsecurity, selinux_inode_setsecurity),LSM_HOOK_INIT (inode_listsecurity, selinux_inode_listsecurity),LSM_HOOK_INIT (inode_getsecid, selinux_inode_getsecid), +LSM_HOOK_INIT (inode_copy_up, selinux_inode_copy_up),LSM_HOOK_INIT (file_permission, selinux_file_permission), LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
Signature · 1 line
2.7.4
PATCH v2 3/7 security,overlayfs: Provide security hook for copy up of xattrs for overlay file
In reply to Vivek GoyalProvide a security hook which is called when xattrs of a file are being copied up. This hook is called once for each xattr and LSM can return 0 to access the xattr, 1 to reject xattr, -EOPNOTSUPP if none of the lsms claim to know xattr and a negative error code if something went terribly wrong.
If 0 or -EOPNOTSUPP is returned, xattr will be copied up, if 1 is returned, xattr will not be copied up and if negative error code is returned, copy up will be aborted.
--- fs/overlayfs/copy_up.c | 7 +++++++ include/linux/lsm_hooks.h | 10 ++++++++++ include/linux/security.h | 6 ++++++ security/security.c | 8 ++++++++ 4 files changed, 31 insertions(+)
fs/overlayfs/copy_up.c +7 -0
@@ -103,6 +103,13 @@ retry: goto retry; } + error =security_inode_copy_up_xattr (name); +if (error <0 && error != -EOPNOTSUPP ) +break ; +if (error ==1 ) { + error =0 ; +continue ;/* Discard */ + } error =vfs_setxattr (new, name, value, size,0 );if (error)break ;
include/linux/lsm_hooks.h +10 -0
@@ -410,6 +410,14 @@* @src indicates the union dentry of file that is being copied up. * @new pointer to pointer to return newly allocated creds. * Returns 0 on success or a negative error code on error. +* @inode_copy_up_xattr: +* Filter the xattrs being copied up when a unioned file is copied +* up from a lower layer to the union/overlay layer. +* @name indicates the name of the xattr. +* Returns 0 to accept the xattr, 1 to discard the xattr, -EOPNOTSUPP if +* security module does not know about attribute or a negative error code +* to abort the copy up. Note that the caller is responsible for reading +* and writing the xattrs as this hook is merely a filter. * * Security hooks for file operations *
@@ -1435,6 +1443,7 @@ union security_list_options { size_t buffer_size);void (*inode_getsecid)(struct inode *inode,u32 *secid);int (*inode_copy_up) (struct dentry *src,struct cred **new); +int (*inode_copy_up_xattr) (const char *name);int (*file_permission)(struct file *file,int mask);int (*file_alloc_security)(struct file *file);
@@ -1707,6 +1716,7 @@ struct security_hook_heads {struct list_head inode_listsecurity;struct list_head inode_getsecid;struct list_head inode_copy_up; +struct list_head inode_copy_up_xattr;struct list_head file_permission;struct list_head file_alloc_security;struct list_head file_free_security;
include/linux/security.h +6 -0
@@ -283,6 +283,7 @@ int security_inode_setsecurity(struct inode *inode, const char *name, const voidint security_inode_listsecurity (struct inode *inode,char *buffer,size_t buffer_size);void security_inode_getsecid (struct inode *inode,u32 *secid);int security_inode_copy_up (struct dentry *src,struct cred **new); +int security_inode_copy_up_xattr (const char *name);int security_file_permission (struct file *file,int mask);int security_file_alloc (struct file *file);void security_file_free (struct file *file);
@@ -764,6 +765,11 @@ static inline int security_inode_copy_up(struct dentry *src, struct cred **new)return 0 ; } +static inline int security_inode_copy_up_xattr (const char *name) +{ + -EOPNOTSUPP ; +} +static inline int security_file_permission (struct file *file,int mask) {return 0 ;
security/security.c +8 -0
@@ -733,6 +733,12 @@ int security_inode_copy_up(struct dentry *src, struct cred **new) }EXPORT_SYMBOL (security_inode_copy_up); +int security_inode_copy_up_xattr (const char *name) +{ +return call_int_hook (inode_copy_up_xattr, -EOPNOTSUPP , name); +} +EXPORT_SYMBOL (security_inode_copy_up_xattr); +int security_file_permission (struct file *file,int mask) {int ret;
@@ -1671,6 +1677,8 @@ struct security_hook_heads security_hook_heads = { LIST_HEAD_INIT(security_hook_heads.inode_getsecid), .inode_copy_up = LIST_HEAD_INIT(security_hook_heads.inode_copy_up), + .inode_copy_up_xattr = + LIST_HEAD_INIT(security_hook_heads.inode_copy_up_xattr), .file_permission = LIST_HEAD_INIT(security_hook_heads.file_permission), .file_alloc_security =
Signature · 1 line
2.7.4
kbuild test robot
build botRe: [PATCH 3/7] security,overlayfs: Provide security hook for copy up of xattrs for overlay file
url: https://github.com/0day-ci/linux/commits/Vivek-Goyal/Overlayfs-SELinux-Support/20160709-002635 config: mips-gpr_defconfig (attached as .config) compiler: mips-linux-gnu-gcc (Debian 5.3.1-8)…
Hi,
[auto build test ERROR on next-20160708]
[also build test ERROR on v4.7-rc6]
[cannot apply to pcmoore-selinux/next security/next v4.7-rc6 v4.7-rc5 v4.7-rc4] [if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Vivek-Goyal/Overlayfs-SELinux-Support/20160709-002635
config: mips-gpr_defconfig (attached as .config)
compiler: mips-linux-gnu-gcc (Debian 5.3.1-8) 5.3.1 20160205
reproduce:
wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=mips
All errors (new ones prefixed by >>):
In file included from arch/mips/kernel/ptrace.c:27:0:
include/linux/security.h: In function 'security_inode_copy_up_xattr':
>> include/linux/security.h:770:2: error: statement with no effect [-Werror=unused-value]
-EOPNOTSUPP;
^
include/linux/security.h:771:1: error: no return statement in function returning non-void [-Werror=return-type]
}
^cc1: all warnings being treated as errors
vim +770 include/linux/security.h
764 {
765 return 0;
766 }
767
768 static inline int security_inode_copy_up_xattr(const char *name)
769 {
> 770 -EOPNOTSUPP;
771 }
772
773 static inline int security_file_permission(struct file *file, int mask)
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Re: [PATCH 3/7] security,overlayfs: Provide security hook for copy up of xattrs for overlay file
Quoted 6 lines · kbuild test robot
Hi,
[auto build test ERROR on next-20160708]
[also build test ERROR on v4.7-rc6]
[cannot apply to pcmoore-selinux/nextsecurity/nextv4.7-rc6 v4.7-rc5 v4.7-rc4] [if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
These patches should be applied on top of overlayfs-next branch of miklos's vfs tree.
git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/vfs.git overlayfs-next
Quoted 12 lines
url: https://github.com/0day-ci/linux/commits/Vivek-Goyal/Overlayfs-SELinux-Support/20160709-002635
config: mips-gpr_defconfig (attached as .config)
compiler: mips-linux-gnu-gcc (Debian 5.3.1-8) 5.3.1 20160205
reproduce:
wget https://git.kernel.org/cgit/linux/kernel/git/wfg/lkp-tests.git/plain/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
make.cross ARCH=mipsAll errors (new ones prefixed by >>):
Quoted 7 lines
In file included from
arch/mips/kernel/ptrace.c:27:0:
include/linux/security.h: In function 'security_inode_copy_up_xattr':Quoted 1 line
Quoted 1 line
include/linux/security.h:770:2: error: statement with no effect [-Werror=unused-value]-EOPNOTSUPP; ^ include/linux/security.h:771:1: error: no return statement in function returning non-void [-Werror=return-type] }
Typo. No "return" keyword. Fixed in attached patch.
Subject: security,overlayfs: Provide security hook for copy up of xattrs for overlay file
Provide a security hook which is called when xattrs of a file are being copied up. This hook is called once for each xattr and LSM can return 0 to access the xattr, 1 to reject xattr, -EOPNOTSUPP if none of the lsms claim to know xattr and a negative error code if something went terribly wrong.
If 0 or -EOPNOTSUPP is returned, xattr will be copied up, if 1 is returned, xattr will not be copied up and if negative error code is returned, copy up will be aborted.
--- fs/overlayfs/copy_up.c | 7 +++++++ include/linux/lsm_hooks.h | 10 ++++++++++ include/linux/security.h | 6 ++++++ security/security.c | 8 ++++++++ 4 files changed, 31 insertions(+)
include/linux/lsm_hooks.h +10 -0
@@ -410,6 +410,14 @@* @src indicates the union dentry of file that is being copied up. * @new pointer to pointer to return newly allocated creds. * Returns 0 on success or a negative error code on error. +* @inode_copy_up_xattr: +* Filter the xattrs being copied up when a unioned file is copied +* up from a lower layer to the union/overlay layer. +* @name indicates the name of the xattr. +* Returns 0 to accept the xattr, 1 to discard the xattr, -EOPNOTSUPP if +* security module does not know about attribute or a negative error code +* to abort the copy up. Note that the caller is responsible for reading +* and writing the xattrs as this hook is merely a filter. * * Security hooks for file operations *
@@ -1435,6 +1443,7 @@ union security_list_options { size_t buffer_size);void (*inode_getsecid)(struct inode *inode,u32 *secid);int (*inode_copy_up) (struct dentry *src,struct cred **new); +int (*inode_copy_up_xattr) (const char *name);int (*file_permission)(struct file *file,int mask);int (*file_alloc_security)(struct file *file);
@@ -1707,6 +1716,7 @@ struct security_hook_heads {struct list_head inode_listsecurity;struct list_head inode_getsecid;struct list_head inode_copy_up; +struct list_head inode_copy_up_xattr;struct list_head file_permission;struct list_head file_alloc_security;struct list_head file_free_security;
include/linux/security.h +6 -0
@@ -283,6 +283,7 @@ int security_inode_setsecurity(struct inint security_inode_listsecurity (struct inode *inode,char *buffer,size_t buffer_size);void security_inode_getsecid (struct inode *inode,u32 *secid);int security_inode_copy_up (struct dentry *src,struct cred **new); +int security_inode_copy_up_xattr (const char *name);int security_file_permission (struct file *file,int mask);int security_file_alloc (struct file *file);void security_file_free (struct file *file);
@@ -764,6 +765,11 @@ static inline int security_inode_copy_upreturn 0 ; } +static inline int security_inode_copy_up_xattr (const char *name) +{ +return -EOPNOTSUPP ; +} +static inline int security_file_permission (struct file *file,int mask) {return 0 ;
security/security.c +8 -0
@@ -733,6 +733,12 @@ int security_inode_copy_up(struct dentry }EXPORT_SYMBOL (security_inode_copy_up); +int security_inode_copy_up_xattr (const char *name) +{ +return call_int_hook (inode_copy_up_xattr, -EOPNOTSUPP , name); +} +EXPORT_SYMBOL (security_inode_copy_up_xattr); +int security_file_permission (struct file *file,int mask) {int ret;
@@ -1671,6 +1677,8 @@ struct security_hook_heads security_hook LIST_HEAD_INIT(security_hook_heads.inode_getsecid), .inode_copy_up = LIST_HEAD_INIT(security_hook_heads.inode_copy_up), + .inode_copy_up_xattr = + LIST_HEAD_INIT(security_hook_heads.inode_copy_up_xattr), .file_permission = LIST_HEAD_INIT(security_hook_heads.file_permission), .file_alloc_security =
fs/overlayfs/copy_up.c +7 -0
@@ -103,6 +103,13 @@ retry: goto retry; } + error =security_inode_copy_up_xattr (name); +if (error <0 && error != -EOPNOTSUPP ) +break ; +if (error ==1 ) { + error =0 ; +continue ;/* Discard */ + } error =vfs_setxattr (new, name, value, size,0 );if (error)break ;
Fengguang Wu
build botRe: [kbuild-all] [PATCH 3/7] security, overlayfs: Provide security hook for copy up of xattrs for overlay file
Good to know that, thank you for the info!
On Mon, Jul 11, 2016 at 09:40:04AM -0400, Vivek Goyal wrote:
>On Sat, Jul 09, 2016 at 01:41:38AM +0800, kbuild test robot wrote: >> Hi, >> >> [auto build test ERROR on next-20160708] >> [also build test ERROR on v4.7-rc6] >> [cannot apply to pcmoore-selinux/next security/next v4.7-rc6 v4.7-rc5 v4.7-rc4] >> [if your patch is applied to the wrong git tree, please drop us a note to help improve the system] > >These patches should be applied on top of overlayfs-next branch of >miklos's vfs tree. > >git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/vfs.git overlayfs-next
Good to know that, thank you for the info!
Thanks,
Fengguang
Re: [PATCH 3/7] security,overlayfs: Provide security hook for copy up of xattrs for overlay file
In reply to Vivek GoyalHi Vivek,
Quoted 9 lines · On Mon, 11 Jul 2016 09:40:04 -0400 Vivek Goyal <[email protected]> wrote:
Quoted 6 lines · kbuild test robot
Hi,
[auto build test ERROR on next-20160708]
[also build test ERROR on v4.7-rc6]
[cannot apply to pcmoore-selinux/nextsecurity/nextv4.7-rc6 v4.7-rc5 v4.7-rc4] [if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
Quoted 4 lines
These patches should be applied on top of overlayfs-next branch of miklos's vfs tree.
git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/vfs.gitoverlayfs-next
That is included in linux-next ...
Signature · 2 lines
Cheers, Stephen Rothwell
Re: [PATCH 3/7] security,overlayfs: Provide security hook for copy up of xattrs for overlay file
In reply to Vivek GoyalQuoted 5 lines · Vivek Goyal
Provide a security hook which is called when xattrs of a file are being copied up. This hook is called once for each xattr and LSM can return 0 to access the xattr, 1 to reject xattr,
-EOPNOTSUPPif none of the lsms claim to know xattr and a negative error code if something went terribly wrong.
0 if the security module wants the xattr to be copied up, 1 if the security module wants the xattr to be discarded on the copy, -EOPNOTSUPP if the security module does not handle/manage the xattr, or a -errno upon an error.
Quoted 4 lines
If 0 or
-EOPNOTSUPPis returned, xattr will be copied up, if 1 is returned, xattr will not be copied up and if negative error code is returned, copy up will be aborted.
Not sure I understand the benefit of the 0 vs -EOPNOTSUPP distinction.
Quoted 76 lines
Signed-off-by David Howells <[email protected]>Signed-off-by Vivek Goyal <[email protected]>--- fs/overlayfs/copy_up.c | 7 +++++++ include/linux/lsm_hooks.h | 10 ++++++++++ include/linux/security.h | 6 ++++++ security/security.c | 8 ++++++++ 4 files changed, 31 insertions(+)fs/overlayfs/copy_up.c +7
-0@@ -103,6 +103,13 @@ retry: goto retry; } + error =security_inode_copy_up_xattr (name); +if (error <0 && error != -EOPNOTSUPP ) +break ; +if (error ==1 ) { + error =0 ; +continue ;/* Discard */ + } error =vfs_setxattr (new, name, value, size,0 );if (error)break ;include/linux/lsm_hooks.h +10
-0@@ -410,6 +410,14 @@* @src indicates the union dentry of file that is being copied up. * @new pointer to pointer to return newly allocated creds. * Returns 0 on success or a negative error code on error. +* @inode_copy_up_xattr: +* Filter the xattrs being copied up when a unioned file is copied +* up from a lower layer to the union/overlay layer. +* @name indicates the name of the xattr. +* Returns 0 to accept the xattr, 1 to discard the xattr, -EOPNOTSUPP if +* security module does not know about attribute or a negative error code +* to abort the copy up. Note that the caller is responsible for reading +* and writing the xattrs as this hook is merely a filter. * * Security hooks for file operations * @@ -1435,6 +1443,7 @@ union security_list_options { size_t buffer_size);void (*inode_getsecid)(struct inode *inode,u32 *secid);int (*inode_copy_up) (struct dentry *src,struct cred **new); +int (*inode_copy_up_xattr) (const char *name);int (*file_permission)(struct file *file,int mask);int (*file_alloc_security)(struct file *file);@@ -1707,6 +1716,7 @@ struct security_hook_heads {struct list_head inode_listsecurity;struct list_head inode_getsecid;struct list_head inode_copy_up; +struct list_head inode_copy_up_xattr;struct list_head file_permission;struct list_head file_alloc_security;struct list_head file_free_security;include/linux/security.h +1
-0@@ -283,6 +283,7 @@ int security_inode_setsecurity(struct inode *inode, const char *name, const voidint security_inode_listsecurity (struct inode *inode,char *buffer,size_t buffer_size);void security_inode_getsecid (struct inode *inode,u32 *secid);int security_inode_copy_up (struct dentry *src,struct cred **new); +int security_inode_copy_up_xattr (const char *name);int security_file_permission (struct file *file,int mask);int security_file_alloc (struct file *file);void security_file_free (struct file *file);
include/linux/security.h static inline int security_inode_copy_up(struct dentry *src, struct cred **new) · line 770
Quoted 6 lines
include/linux/security.h +3
-0@@ -764,6 +765,11 @@ static inline int security_inode_copy_up(struct dentry *src, struct cred **new)return 0 ; } +static inline int security_inode_copy_up_xattr (const char *name) +{ + -EOPNOTSUPP ;
return?
Quoted 32 lines
include/linux/security.h +2
-0+} +static inline int security_file_permission (struct file *file,int mask) {return 0 ;security/security.c +8
-0@@ -733,6 +733,12 @@ int security_inode_copy_up(struct dentry *src, struct cred **new) }EXPORT_SYMBOL (security_inode_copy_up); +int security_inode_copy_up_xattr (const char *name) +{ +return call_int_hook (inode_copy_up_xattr, -EOPNOTSUPP , name); +} +EXPORT_SYMBOL (security_inode_copy_up_xattr); +int security_file_permission (struct file *file,int mask) {int ret;@@ -1671,6 +1677,8 @@ struct security_hook_heads security_hook_heads = { LIST_HEAD_INIT(security_hook_heads.inode_getsecid), .inode_copy_up = LIST_HEAD_INIT(security_hook_heads.inode_copy_up), + .inode_copy_up_xattr = + LIST_HEAD_INIT(security_hook_heads.inode_copy_up_xattr), .file_permission = LIST_HEAD_INIT(security_hook_heads.file_permission), .file_alloc_security =
Re: [PATCH 3/7] security,overlayfs: Provide security hook for copy up of xattrs for overlay file
Quoted 11 lines · Stephen Smalley
Quoted 5 lines · Vivek Goyal
Provide a security hook which is called when xattrs of a file are being copied up. This hook is called once for each xattr and LSM can return 0 to access the xattr, 1 to reject xattr,
-EOPNOTSUPPif none of the lsms claim to know xattr and a negative error code if something went terribly wrong.0 if the security module wants the xattr to be copied up, 1 if the security module wants the xattr to be discarded on the copy,
-EOPNOTSUPPif the security module does not handle/manage the xattr, or a -errno upon an error.
Ok, will change the description.
Quoted 7 lines
Quoted 5 lines
If 0 or
-EOPNOTSUPPis returned, xattr will be copied up, if 1 is returned, xattr will not be copied up and if negative error code is returned, copy up will be aborted.Not sure I understand the benefit of the 0 vs
-EOPNOTSUPPdistinction.
I am not sure either. Casey wanted to have four states so I introduced it.
Thanks
Vivek
Re: [PATCH 3/7] security,overlayfs: Provide security hook for copy up of xattrs for overlay file
In reply to Stephen SmalleyOn Mon, Jul 11, 2016 at 11:31:47AM -0400, Stephen Smalley wrote:
[..]
Quoted 5 lines
include/linux/security.h static inline int security_inode_copy_up(struct dentry *src, struct cred **new) · line 770
Quoted 3 lines
include/linux/security.h +3
-0+static inline int security_inode_copy_up_xattr (const char *name) +{ + -EOPNOTSUPP ;return?
Yes, this one I fixed it in my patches now. kbuild also flagged this.
Vivek
PATCH v2 4/7 selinux: Implementation for inode_copy_up_xattr() hook
In reply to Vivek GoyalWhen a file is copied up in overlay, we have already created file on upper/ with right label and there is no need to copy up selinux label/xattr from lower file to upper file. In fact in case of context mount, we don't want to copy up label as newly created file got its label from context= option.
--- security/selinux/hooks.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+)
security/selinux/hooks.c +16 -0
@@ -3290,6 +3290,21 @@ static int selinux_inode_copy_up(struct dentry *src, struct cred **new)return 0 ; } +static int selinux_inode_copy_up_xattr (const char *name) +{ +/* The copy_up hook above sets the initial context on an inode, but we +* don't then want to overwrite it by blindly copying all the lower +* xattrs up. Instead, we have to filter out SELinux-related xattrs. +*/ +if (strcmp (name,XATTR_NAME_SELINUX ) ==0 ) +return 1 ;/* Discard */ +/* +* Any other attribute apart from SELINUX is not claimed, supported +* by selinux. +*/ +return -EOPNOTSUPP ; +} +/* file security operations */ static int selinux_revalidate_file_permission (struct file *file,int mask)
@@ -6077,6 +6092,7 @@ static struct security_hook_list selinux_hooks[] = {LSM_HOOK_INIT (inode_listsecurity, selinux_inode_listsecurity),LSM_HOOK_INIT (inode_getsecid, selinux_inode_getsecid),LSM_HOOK_INIT (inode_copy_up, selinux_inode_copy_up), +LSM_HOOK_INIT (inode_copy_up_xattr, selinux_inode_copy_up_xattr),LSM_HOOK_INIT (file_permission, selinux_file_permission), LSM_HOOK_INIT(file_alloc_security, selinux_file_alloc_security),
Signature · 1 line
2.7.4
PATCH v2 5/7 selinux: Pass security pointer to determine_inode_label()
In reply to Vivek GoyalRight now selinux_determine_inode_label() works on security pointer of current task. Soon I need this to work on a security pointer retrieved from a set of creds. So start passing in a pointer and caller can decide where to fetch security pointer from.
--- security/selinux/hooks.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-)
security/selinux/hooks.c +10 -9
@@ -1785,13 +1785,13 @@ out:/* * Determine the label for an inode that might be unioned. */ -static int selinux_determine_inode_label (struct inode *dir, -const struct qstr *name, -u16 tclass, -u32 *_new_isid) +static int +selinux_determine_inode_label (const struct task_security_struct *tsec, +struct inode *dir, +const struct qstr *name,u16 tclass, +u32 *_new_isid) {const struct superblock_security_struct *sbsec = dir->i_sb->s_security; -const struct task_security_struct *tsec =current_security ();if ((sbsec->flags &SE_SBINITIALIZED ) && (sbsec->behavior ==SECURITY_FS_USE_MNTPOINT )) {
@@ -1834,8 +1834,8 @@ static int may_create(struct inode *dir,if (rc)return rc; - rc =selinux_determine_inode_label (dir, &dentry->d_name, tclass, - &newsid); + rc =selinux_determine_inode_label (current_security (), dir, + &dentry->d_name, tclass, &newsid);if (rc)return rc;
@@ -2815,7 +2815,8 @@ static int selinux_dentry_init_security(struct dentry *dentry, int mode,u32 newsid;int rc; - rc =selinux_determine_inode_label (d_inode (dentry->d_parent), name, + rc =selinux_determine_inode_label (current_security (), +d_inode (dentry->d_parent), name,inode_mode_to_security_class (mode), &newsid);if (rc)
@@ -2840,7 +2841,7 @@ static int selinux_inode_init_security(struct inode *inode, struct inode *dir, sid = tsec->sid; newsid = tsec->create_sid; - rc =selinux_determine_inode_label ( + rc =selinux_determine_inode_label (current_security (), dir, qstr,inode_mode_to_security_class (inode->i_mode), &newsid);
Signature · 1 line
2.7.4
PATCH v2 6/7 security, overlayfs: Provide hook to correctly label newly created files
In reply to Vivek GoyalDuring a new file creation we need to make sure new file is created with the right label. New file is created in upper/ so effectively file should get label as if task had created file in upper/.
We switched to mounter's creds for actual file creation. Also if there is a whiteout present, then file will be created in work/ dir first and then renamed in upper. In none of the cases file will be labeled as we want it to be.
This patch introduces a new hook dentry_create_files_as(), which determines the label/context dentry will get if it had been created by task in upper and modify passed set of creds appropriately. Caller makes use of these new creds for file creation.
--- fs/overlayfs/dir.c | 10 ++++++++++ include/linux/lsm_hooks.h | 15 +++++++++++++++ include/linux/security.h | 12 ++++++++++++ security/security.c | 11 +++++++++++ 4 files changed, 48 insertions(+)
fs/overlayfs/dir.c +10 -0
@@ -433,6 +433,15 @@ static int ovl_create_or_link(struct dentry *dentry, int mode, dev_t rdev,if (override_cred) { override_cred->fsuid = inode->i_uid; override_cred->fsgid = inode->i_gid; +if (!hardlink) { + err =security_dentry_create_files_as (dentry, + mode, &dentry->d_name, old_cred, + override_cred); +if (err) { +put_cred (override_cred); + goto out_revert_creds; + } + }put_cred (override_creds (override_cred));put_cred (override_cred);
@@ -443,6 +452,7 @@ static int ovl_create_or_link(struct dentry *dentry, int mode, dev_t rdev, err =ovl_create_over_whiteout (dentry, inode, &stat, link, hardlink); } +out_revert_creds:revert_creds (old_cred);if (!err) {struct inode *realinode =d_inode (ovl_dentry_upper (dentry));
include/linux/lsm_hooks.h +15 -0
@@ -151,6 +151,16 @@* @name name of the last path component used to create file * @ctx pointer to place the pointer to the resulting context in. * @ctxlen point to place the length of the resulting context. +* @dentry_create_files_as: +* Compute a context for a dentry as the inode is not yet available +* and set that context in passed in creds so that new files are +* created using that context. Context is calculated using the +* passed in creds and not the creds of the caller. +* @dentry dentry to use in calculating the context. +* @mode mode used to determine resource type. +* @name name of the last path component used to create file +* @old creds which should be used for context calculation +* @new creds to modify * * * Security hooks for inode operations.
@@ -1375,6 +1385,10 @@ union security_list_options {int (*dentry_init_security)(struct dentry *dentry,int mode,struct qstr *name,void **ctx,u32 *ctxlen); +int (*dentry_create_files_as)(struct dentry *dentry,int mode, +struct qstr *name, +const struct cred *old, +struct cred *new);#ifdef CONFIG_SECURITY_PATH
@@ -1675,6 +1689,7 @@ struct security_hook_heads {struct list_head sb_clone_mnt_opts;struct list_head sb_parse_opts_str;struct list_head dentry_init_security; +struct list_head dentry_create_files_as;#ifdef CONFIG_SECURITY_PATH struct list_head path_unlink;struct list_head path_mkdir;
include/linux/security.h +12 -0
@@ -242,6 +242,10 @@ int security_sb_parse_opts_str(char *options, struct security_mnt_opts *opts);int security_dentry_init_security (struct dentry *dentry,int mode,struct qstr *name,void **ctx,u32 *ctxlen); +int security_dentry_create_files_as (struct dentry *dentry,int mode, +struct qstr *name, +const struct cred *old, +struct cred *new);int security_inode_alloc (struct inode *inode);void security_inode_free (struct inode *inode);
@@ -600,6 +604,14 @@ static inline int security_dentry_init_security(struct dentry *dentry,return -EOPNOTSUPP ; } +static inline int security_dentry_create_files_as (struct dentry *dentry, +int mode,struct qstr *name, +const struct cred *old, +struct cred *new) +{ +return 0 ; +} + static inline int security_inode_init_security(struct inode *inode, struct inode *dir,
security/security.c +11 -0
@@ -364,6 +364,15 @@ int security_dentry_init_security(struct dentry *dentry, int mode, }EXPORT_SYMBOL (security_dentry_init_security); +int security_dentry_create_files_as (struct dentry *dentry,int mode, +struct qstr *name, +const struct cred *old,struct cred *new) +{ +return call_int_hook (dentry_create_files_as,0 , dentry, mode, + name, old, new); +} +EXPORT_SYMBOL (security_dentry_create_files_as); +int security_inode_init_security (struct inode *inode,struct inode *dir,const struct qstr *qstr,const initxattrs initxattrs,void *fs_data)
@@ -1614,6 +1623,8 @@ struct security_hook_heads security_hook_heads = { LIST_HEAD_INIT(security_hook_heads.sb_parse_opts_str), .dentry_init_security = LIST_HEAD_INIT(security_hook_heads.dentry_init_security), + .dentry_create_files_as = +LIST_HEAD_INIT (security_hook_heads.dentry_create_files_as), #ifdef CONFIG_SECURITY_PATH .path_unlink =LIST_HEAD_INIT (security_hook_heads.path_unlink), .path_mkdir = LIST_HEAD_INIT(security_hook_heads.path_mkdir),
Signature · 1 line
2.7.4
PATCH v2 7/7 selinux: Implement dentry_create_files_as() hook
In reply to Vivek GoyalCalculate what would be the label of newly created file and set that secid in the passed creds.
Context of the task which is actually creating file is retrieved from set of creds passed in. (old->security).
--- security/selinux/hooks.c | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+)
security/selinux/hooks.c +22 -0
@@ -2825,6 +2825,27 @@ static int selinux_dentry_init_security(struct dentry *dentry, int mode,return security_sid_to_context (newsid, (char **)ctx, ctxlen); } +static int selinux_dentry_create_files_as (struct dentry *dentry,int mode, +struct qstr *name, +const struct cred *old, +struct cred *new) +{ +u32 newsid; +int rc; +struct task_security_struct *tsec; + + rc =selinux_determine_inode_label (old->security, +d_inode (dentry->d_parent), name, +inode_mode_to_security_class (mode), + &newsid); +if (rc) +return rc; + + tsec = new->security; + tsec->create_sid = newsid; +return 0 ; +} + static int selinux_inode_init_security(struct inode *inode, struct inode *dir, const struct qstr *qstr, const char **name,
@@ -6066,6 +6087,7 @@ static struct security_hook_list selinux_hooks[] = {LSM_HOOK_INIT (sb_parse_opts_str, selinux_parse_opts_str),LSM_HOOK_INIT (dentry_init_security, selinux_dentry_init_security), +LSM_HOOK_INIT (dentry_create_files_as, selinux_dentry_create_files_as),LSM_HOOK_INIT (inode_alloc_security, selinux_inode_alloc_security), LSM_HOOK_INIT(inode_free_security, selinux_inode_free_security),
Signature · 1 line
2.7.4