Allow initializing the kernfs node's secctx based on its parent
Subsystems (4)
v7 cover Allow initializing the kernfs node's secctx based on its parent
TL;DR:
This series adds a new security hook that allows to initialize the security context of kernfs properly, taking into account the parent context (and possibly other attributes). Kernfs nodes require special handling here, since they are not bound to specific inodes/superblocks, but instead represent the backing tree structure that is used to build the VFS tree when the kernfs tree is mounted.
Changes in v7:
- simplify the new security hook's interface
- rather than trying to extract kernfs data into common structures, just pass the kernfs nodes themselves and add helper functions to <linux/kernfs.h> for accessing their security xattrs
- in case other LSMs need more kernfs node attributes than the file mode (uid/gid/...), they can simply add new helpers to <linux/kernfs.h> as needed
- refactor "kernfs: use simple_xattrs for security attributes" to keep using a single common simple_xattrs structure
- turns out having two separate simple_xattrs wouldn't work right (see the definition of simple_xattr_list() in fs/xattr.c)
- drop unnecessary initializations from inode_doinit_use_xattr()
- move the IOP_XATTR check out of inode_doinit_use_xattr()
- add two kernfs cleanup patches
- these could be applied independently, but the rest of the patches depend on them, so I'd rather they stay bundled with the rest to avoid cross-tree conflicts
v6: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v6:
- remove copy-pasted duplicate macro definition
v5: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v5:
- fix misplaced semicolon detected by 0day robot
v4: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v4:
- reorder and rename hook arguments
- avoid allocating kernfs_iattrs unless needed
v3: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v3:
- rename the hook to "kernfs_init_security"
- change the hook interface to simply pass pointers to struct iattr and struct simple_xattrs of both the new node and its parent
- add full security xattr support to kernfs (and fixup SELinux behavior to handle it properly)
v2: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v2:
- add docstring for the new hook in union security_list_options
- initialize *ctx to NULL and *ctxlen to 0 in case the hook is not implemented
v1: https://lore.kernel.org/selinux/[email protected]/T/
The kernfs nodes initially do not store any security context and rely on the LSM to assign some default context to inodes created over them. Kernfs inodes, however, allow setting an explicit context via the *setxattr(2) syscalls, in which case the context is stored inside the kernfs node's internal structure.
SELinux (and possibly other LSMs) initialize the context of newly created FS objects based on the parent object's context (usually the child inherits the parent's context, unless the policy dictates otherwise). This is done by hooking the creation of the new inode corresponding to the newly created file/directory via security_inode_init_security() (most filesystems always create a fresh inode when a new FS object is created). However, kernfs nodes can be created "behind the scenes" while the filesystem is not mounted anywhere and thus no inodes can exist for them yet.
Therefore, to allow maintaining similar behavior for kernfs nodes, a new LSM hook is needed, which will allow initializing the kernfs node's security context based on its own attributes and those of the parent's node.
The main motivation for this change is that the userspace users of cgroupfs (which is built on kernfs) expect the usual security context inheritance to work under SELinux (see [1] and [2]). This functionality is required for better confinement of containers under SELinux.
Patch 1/7 simplifies the kernfs_iattrs structure and patch 2/7 optimizes kernfs to not allocate kernfs_iattrs when getting the value of an xattr.
Patch 3/7 changes SELinux to fetch security context from extended attributes on kernfs filesystems, falling back to genfs-defined context if that fails. Without this patch the 4/7 would be a regression for SELinux (due to the removal of ...notifysecctx() call.
Patch 4/7 implements full security xattr support in kernfs using simple_xattrs; patch 5/7 adds the new LSM hook; patch 6/7 implements the new hook in SELinux; and patch 7/7 modifies kernfs to call the new hook on new node creation.
Testing:
- passed the reproducer from the commit message of the last patch
- passed SELinux testsuite on Fedora Rawhide (x86_64) when applied on top of current Rawhide kernel (5.0.0-0.rc7.git2.1) [3]
- including the new proposed selinux-testsuite subtest [4] (adapted from the reproducer)
[1] https://github.com/SELinuxProject/selinux-kernel/issues/39
[2] https://bugzilla.redhat.com/show_bug.cgi?id=1553803
[3] https://koji.fedoraproject.org/koji/taskinfo?taskID=32963825
[4] https://github.com/SELinuxProject/selinux-testsuite/pull/48
Ondrej Mosnacek (7):
kernfs: clean up struct kernfs_iattrs
kernfs: do not alloc iattrs in kernfs_xattr_get
selinux: try security xattr after genfs for kernfs filesystems
kernfs: use simple_xattrs for security attributes
LSM: add new hook for kernfs node initialization
selinux: implement the kernfs_init_security hook
kernfs: initialize security of newly created nodes
fs/kernfs/dir.c | 28 ++-- fs/kernfs/inode.c | 166 +++++++++------------ fs/kernfs/kernfs-internal.h | 8 +- fs/kernfs/symlink.c | 4 +- include/linux/kernfs.h | 15 ++ include/linux/lsm_hooks.h | 13 ++ include/linux/security.h | 9 ++ security/security.c | 6 + security/selinux/hooks.c | 223 +++++++++++++++++++--------- security/selinux/include/security.h | 1 + 10 files changed, 290 insertions(+), 183 deletions(-)
Signature · 1 line
2.20.1
PATCH v7 1/7 kernfs: clean up struct kernfs_iattrs
Right now, kernfs_iattrs embeds the whole struct iattr, even though it doesn't really use half of its fields... This both leads to wasting space and makes the code look awkward. Let's just list the few fields we need directly in struct kernfs_iattrs.
--- fs/kernfs/dir.c | 10 ++++---- fs/kernfs/inode.c | 47 ++++++++++++++++--------------------- fs/kernfs/kernfs-internal.h | 6 ++++- fs/kernfs/symlink.c | 4 ++-- 4 files changed, 31 insertions(+), 36 deletions(-)
fs/kernfs/dir.c +4 -6
@@ -795,9 +795,8 @@ int kernfs_add_one(struct kernfs_node *kn)/* Update timestamps on the parent */ ps_iattr = parent->iattr;if (ps_iattr) { -struct iattr *ps_iattrs = &ps_iattr->ia_iattr; -ktime_get_real_ts64 (&ps_iattrs->ia_ctime); - ps_iattrs->ia_mtime = ps_iattrs->ia_ctime; +ktime_get_real_ts64 (&ps_iattr->ia_ctime); + ps_iattr->ia_mtime = ps_iattr->ia_ctime; }mutex_unlock (&kernfs_mutex);
@@ -1329,9 +1328,8 @@ static void __kernfs_remove(struct kernfs_node *kn)/* update timestamps on the parent */ if (ps_iattr) { -ktime_get_real_ts64 (&ps_iattr->ia_iattr.ia_ctime); - ps_iattr->ia_iattr.ia_mtime = - ps_iattr->ia_iattr.ia_ctime; +ktime_get_real_ts64 (&ps_iattr->ia_ctime); + ps_iattr->ia_mtime = ps_iattr->ia_ctime; }kernfs_put (pos);
fs/kernfs/inode.c +20 -27
93 lines not on this page · raw message
fs/kernfs/kernfs-internal.h +5 -1
@@ -19,7 +19,11 @@#include <linux/kernfs.h> struct kernfs_iattrs { -struct iattr ia_iattr; +kuid_t ia_uid; +kgid_t ia_gid; +struct timespec64 ia_atime; +struct timespec64 ia_mtime; +struct timespec64 ia_ctime;void *ia_secdata;u32 ia_secdata_len;
fs/kernfs/symlink.c +2 -2
@@ -33,8 +33,8 @@ struct kernfs_node *kernfs_create_link(struct kernfs_node *parent,kgid_t gid =GLOBAL_ROOT_GID ;if (target->iattr) { - uid = target->iattr->ia_iattr.ia_uid; - gid = target->iattr->ia_iattr.ia_gid; + uid = target->iattr->ia_uid; + gid = target->iattr->ia_gid; } kn = kernfs_new_node(parent, name, S_IFLNK|S_IRWXUGO, uid, gid,
Signature · 1 line
2.20.1
PATCH v7 2/7 kernfs: do not alloc iattrs in kernfs_xattr_get
In reply to Ondrej MosnacekThis is a read-only operation, so we can simply return -ENODATA if kn->iattr is NULL.
--- fs/kernfs/inode.c | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-)
fs/kernfs/inode.c +14 -4
@@ -31,14 +31,14 @@ static const struct inode_operations kernfs_iops = { .listxattr = kernfs_iop_listxattr, }; -static struct kernfs_iattrs *kernfs_iattrs (struct kernfs_node *kn) +static struct kernfs_iattrs *__kernfs_iattrs (struct kernfs_node *kn,int alloc) {static DEFINE_MUTEX (iattr_mutex);struct kernfs_iattrs *ret;mutex_lock (&iattr_mutex); -if (kn->iattr) +if (kn->iattr || !alloc) goto out_unlock; kn->iattr =kzalloc (sizeof (struct kernfs_iattrs ),GFP_KERNEL );
@@ -60,6 +60,16 @@ out_unlock:return ret; } +static struct kernfs_iattrs *kernfs_iattrs (struct kernfs_node *kn) +{ +return __kernfs_iattrs (kn,1 ); +} + +static struct kernfs_iattrs *kernfs_iattrs_noalloc (struct kernfs_node *kn) +{ +return __kernfs_iattrs (kn,0 ); +} +int __kernfs_setattr (struct kernfs_node *kn,const struct iattr *iattr) {struct kernfs_iattrs *attrs;
@@ -306,9 +316,9 @@ static int kernfs_xattr_get(const struct xattr_handler *handler,struct kernfs_node *kn = inode->i_private;struct kernfs_iattrs *attrs; - attrs =kernfs_iattrs (kn); + attrs =kernfs_iattrs_noalloc (kn);if (!attrs) -return -ENOMEM ; +return -ENODATA ;return simple_xattr_get (&attrs->xattrs, name, value, size); }
Signature · 1 line
2.20.1
PATCH v7 3/7 selinux: try security xattr after genfs for kernfs filesystems
In reply to Ondrej MosnacekSince kernfs supports the security xattr handlers, we can simply use these to determine the inode's context, dropping the need to update it from kernfs explicitly using a security_inode_notifysecctx() call.
We achieve this by setting a new sbsec flag SE_SBGENFS_XATTR to all mounts that are known to use kernfs under the hood and then fetching the xattrs after determining the fallback genfs sid in inode_doinit_with_dentry() when this flag is set.
This will allow implementing full security xattr support in kernfs and removing the ...notifysecctx() call in a subsequent patch.
--- security/selinux/hooks.c | 157 +++++++++++++++------------- security/selinux/include/security.h | 1 + 2 files changed, 85 insertions(+), 73 deletions(-)
security/selinux/hooks.c +84 -73
@@ -793,11 +793,13 @@ static int selinux_set_mnt_opts(struct super_block *sb,if (!strcmp (sb->s_type->name,"debugfs" ) || !strcmp (sb->s_type->name,"tracefs" ) || - !strcmp (sb->s_type->name,"sysfs" ) || - !strcmp (sb->s_type->name,"pstore" ) || + !strcmp (sb->s_type->name,"pstore" )) + sbsec->flags |=SE_SBGENFS ; + +if (!strcmp (sb->s_type->name,"sysfs" ) || !strcmp (sb->s_type->name,"cgroup" ) || !strcmp (sb->s_type->name,"cgroup2" )) - sbsec->flags |=SE_SBGENFS ; + sbsec->flags |=SE_SBGENFS |SE_SBGENFS_XATTR ;if (!sbsec->behavior) {/*
@@ -1392,6 +1394,67 @@ static int selinux_genfs_get_sid(struct dentry *dentry,return rc; } +static int inode_doinit_use_xattr (struct inode *inode,struct dentry *dentry, +u32 def_sid,u32 *sid) +{ +#define INITCONTEXTLEN 255 +char *context; +unsigned int len; +int rc; + + len =INITCONTEXTLEN ; + context =kmalloc (len +1 ,GFP_NOFS ); +if (!context) +return -ENOMEM ; + + context[len] ='\0' ; + rc =__vfs_getxattr (dentry, inode,XATTR_NAME_SELINUX , context, len); +if (rc == -ERANGE ) { +kfree (context); + +/* Need a larger buffer. Query for the right size. */ + rc =__vfs_getxattr (dentry, inode,XATTR_NAME_SELINUX ,NULL ,0 ); +if (rc <0 ) +return rc; + + len = rc; + context =kmalloc (len +1 ,GFP_NOFS ); +if (!context) +return -ENOMEM ; + + context[len] ='\0' ; + rc =__vfs_getxattr (dentry, inode,XATTR_NAME_SELINUX , + context, len); + } +if (rc <0 ) { +kfree (context); +if (rc != -ENODATA ) { +pr_warn ("SELinux: %s: getxattr returned %d for dev=%s ino=%ld\n" , + __func__, -rc, inode->i_sb->s_id, inode->i_ino); +return rc; + } + *sid = def_sid; +return 0 ; + } + + rc =security_context_to_sid_default (&selinux_state, context, rc, sid, + def_sid,GFP_NOFS ); +if (rc) { +char *dev = inode->i_sb->s_id; +unsigned long ino = inode->i_ino; + +if (rc == -EINVAL ) { +pr_notice_ratelimited ("SELinux: inode=%lu on dev=%s was found to have an invalid context=%s. This indicates you may need to relabel the inode or the filesystem in question.\n" , + ino, dev, context); + }else { +pr_warn ("SELinux: %s: context_to_sid(%s) returned %d for dev=%s ino=%ld\n" , + __func__, context, -rc, dev, ino); + } + } +kfree (context); +return 0 ; +} +/* The inode's security attributes must be initialized before first use. */ static int inode_doinit_with_dentry (struct inode *inode,struct dentry *opt_dentry) {
@@ -1400,9 +1463,6 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentu32 task_sid, sid =0 ;u16 sclass;struct dentry *dentry; -#define INITCONTEXTLEN 255 -char *context =NULL ; -unsigned len =0 ;int rc =0 ;if (isec->initialized ==LABEL_INITIALIZED )
@@ -1470,72 +1530,11 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent goto out; } - len =INITCONTEXTLEN ; - context =kmalloc (len+1 ,GFP_NOFS ); -if (!context) { - rc = -ENOMEM ; -dput (dentry); - goto out; - } - context[len] ='\0' ; - rc =__vfs_getxattr (dentry, inode,XATTR_NAME_SELINUX , context, len); -if (rc == -ERANGE ) { -kfree (context); - -/* Need a larger buffer. Query for the right size. */ - rc =__vfs_getxattr (dentry, inode,XATTR_NAME_SELINUX ,NULL ,0 ); -if (rc <0 ) { -dput (dentry); - goto out; - } - len = rc; - context =kmalloc (len+1 ,GFP_NOFS ); -if (!context) { - rc = -ENOMEM ; -dput (dentry); - goto out; - } - context[len] ='\0' ; - rc =__vfs_getxattr (dentry, inode,XATTR_NAME_SELINUX , context, len); - } + rc =inode_doinit_use_xattr (inode, dentry, sbsec->def_sid, + &sid);dput (dentry); -if (rc <0 ) { -if (rc != -ENODATA ) { -pr_warn ("SELinux: %s: getxattr returned " -"%d for dev=%s ino=%ld\n" , __func__, - -rc, inode->i_sb->s_id, inode->i_ino); -kfree (context); - goto out; - } -/* Map ENODATA to the default file SID */ - sid = sbsec->def_sid; - rc =0 ; - }else { - rc =security_context_to_sid_default (&selinux_state, - context, rc, &sid, - sbsec->def_sid, -GFP_NOFS ); -if (rc) { -char *dev = inode->i_sb->s_id; -unsigned long ino = inode->i_ino; - -if (rc == -EINVAL ) { -if (printk_ratelimit ()) -pr_notice ("SELinux: inode=%lu on dev=%s was found to have an invalid " -"context=%s. This indicates you may need to relabel the inode or the " -"filesystem in question.\n" , ino, dev, context); - }else { -pr_warn ("SELinux: %s: context_to_sid(%s) " -"returned %d for dev=%s ino=%ld\n" , - __func__, context, -rc, dev, ino); - } -kfree (context); -/* Leave with the unlabeled SID */ - rc =0 ; -break ; - } - } -kfree (context); +if (rc) + goto out;break ;case SECURITY_FS_USE_TASK : sid = task_sid;
@@ -1586,9 +1585,21 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent goto out; rc =selinux_genfs_get_sid (dentry, sclass, sbsec->flags, &sid); -dput (dentry); -if (rc) +if (rc) { +dput (dentry); goto out; + } + +if ((sbsec->flags &SE_SBGENFS_XATTR ) && + (inode->i_opflags &IOP_XATTR )) { + rc =inode_doinit_use_xattr (inode, dentry, + sid, &sid); +if (rc) { +dput (dentry); + goto out; + } + } +dput (dentry); }break ; }
security/selinux/include/security.h +1 -0
@@ -58,6 +58,7 @@#define SE_SBINITIALIZED 0x0100#define SE_SBPROC 0x0200#define SE_SBGENFS 0x0400 +#define SE_SBGENFS_XATTR 0x0800#define CONTEXT_STR "context="#define FSCONTEXT_STR "fscontext="
Signature · 1 line
2.20.1
Re: [PATCH v7 3/7] selinux: try security xattr after genfs for kernfs filesystems
Quoted 9 lines · Ondrej Mosnacek
Since kernfs supports the security xattr handlers, we can simply use these to determine the inode's context, dropping the need to update it from kernfs explicitly using a
security_inode_notifysecctx()call.We achieve this by setting a new sbsec flag
SE_SBGENFS_XATTRto all mounts that are known to use kernfs under the hood and then fetching the xattrs after determining the fallback genfs sid ininode_doinit_with_dentry()when this flag is set.
Quoted 4 lines
This will allow implementing full security xattr support in kernfs and removing the ...notifysecctx() call in a subsequent patch.
Signed-off-by Ondrej Mosnacek <[email protected]>
Quoted 218 lines
--- security/selinux/hooks.c | 157 +++++++++++++++------------- security/selinux/include/security.h | 1 + 2 files changed, 85 insertions(+), 73 deletions(-)security/selinux/hooks.c +84
-73@@ -793,11 +793,13 @@ static int selinux_set_mnt_opts(struct super_block *sb,if (!strcmp (sb->s_type->name,"debugfs" ) || !strcmp (sb->s_type->name,"tracefs" ) || - !strcmp (sb->s_type->name,"sysfs" ) || - !strcmp (sb->s_type->name,"pstore" ) || + !strcmp (sb->s_type->name,"pstore" )) + sbsec->flags |=SE_SBGENFS ; + +if (!strcmp (sb->s_type->name,"sysfs" ) || !strcmp (sb->s_type->name,"cgroup" ) || !strcmp (sb->s_type->name,"cgroup2" )) - sbsec->flags |=SE_SBGENFS ; + sbsec->flags |=SE_SBGENFS |SE_SBGENFS_XATTR ;if (!sbsec->behavior) {/* @@ -1392,6 +1394,67 @@ static int selinux_genfs_get_sid(struct dentry *dentry,return rc; } +static int inode_doinit_use_xattr (struct inode *inode,struct dentry *dentry, +u32 def_sid,u32 *sid) +{ +#define INITCONTEXTLEN 255 +char *context; +unsigned int len; +int rc; + + len =INITCONTEXTLEN ; + context =kmalloc (len +1 ,GFP_NOFS ); +if (!context) +return -ENOMEM ; + + context[len] ='\0' ; + rc =__vfs_getxattr (dentry, inode,XATTR_NAME_SELINUX , context, len); +if (rc == -ERANGE ) { +kfree (context); + +/* Need a larger buffer. Query for the right size. */ + rc =__vfs_getxattr (dentry, inode,XATTR_NAME_SELINUX ,NULL ,0 ); +if (rc <0 ) +return rc; + + len = rc; + context =kmalloc (len +1 ,GFP_NOFS ); +if (!context) +return -ENOMEM ; + + context[len] ='\0' ; + rc =__vfs_getxattr (dentry, inode,XATTR_NAME_SELINUX , + context, len); + } +if (rc <0 ) { +kfree (context); +if (rc != -ENODATA ) { +pr_warn ("SELinux: %s: getxattr returned %d for dev=%s ino=%ld\n" , + __func__, -rc, inode->i_sb->s_id, inode->i_ino); +return rc; + } + *sid = def_sid; +return 0 ; + } + + rc =security_context_to_sid_default (&selinux_state, context, rc, sid, + def_sid,GFP_NOFS ); +if (rc) { +char *dev = inode->i_sb->s_id; +unsigned long ino = inode->i_ino; + +if (rc == -EINVAL ) { +pr_notice_ratelimited ("SELinux: inode=%lu on dev=%s was found to have an invalid context=%s. This indicates you may need to relabel the inode or the filesystem in question.\n" , + ino, dev, context); + }else { +pr_warn ("SELinux: %s: context_to_sid(%s) returned %d for dev=%s ino=%ld\n" , + __func__, context, -rc, dev, ino); + } + } +kfree (context); +return 0 ; +} +/* The inode's security attributes must be initialized before first use. */ static int inode_doinit_with_dentry (struct inode *inode,struct dentry *opt_dentry) {@@ -1400,9 +1463,6 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dentu32 task_sid, sid =0 ;u16 sclass;struct dentry *dentry; -#define INITCONTEXTLEN 255 -char *context =NULL ; -unsigned len =0 ;int rc =0 ;if (isec->initialized ==LABEL_INITIALIZED )@@ -1470,72 +1530,11 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent goto out; } - len =INITCONTEXTLEN ; - context =kmalloc (len+1 ,GFP_NOFS ); -if (!context) { - rc = -ENOMEM ; -dput (dentry); - goto out; - } - context[len] ='\0' ; - rc =__vfs_getxattr (dentry, inode,XATTR_NAME_SELINUX , context, len); -if (rc == -ERANGE ) { -kfree (context); - -/* Need a larger buffer. Query for the right size. */ - rc =__vfs_getxattr (dentry, inode,XATTR_NAME_SELINUX ,NULL ,0 ); -if (rc <0 ) { -dput (dentry); - goto out; - } - len = rc; - context =kmalloc (len+1 ,GFP_NOFS ); -if (!context) { - rc = -ENOMEM ; -dput (dentry); - goto out; - } - context[len] ='\0' ; - rc =__vfs_getxattr (dentry, inode,XATTR_NAME_SELINUX , context, len); - } + rc =inode_doinit_use_xattr (inode, dentry, sbsec->def_sid, + &sid);dput (dentry); -if (rc <0 ) { -if (rc != -ENODATA ) { -pr_warn ("SELinux: %s: getxattr returned " -"%d for dev=%s ino=%ld\n" , __func__, - -rc, inode->i_sb->s_id, inode->i_ino); -kfree (context); - goto out; - } -/* Map ENODATA to the default file SID */ - sid = sbsec->def_sid; - rc =0 ; - }else { - rc =security_context_to_sid_default (&selinux_state, - context, rc, &sid, - sbsec->def_sid, -GFP_NOFS ); -if (rc) { -char *dev = inode->i_sb->s_id; -unsigned long ino = inode->i_ino; - -if (rc == -EINVAL ) { -if (printk_ratelimit ()) -pr_notice ("SELinux: inode=%lu on dev=%s was found to have an invalid " -"context=%s. This indicates you may need to relabel the inode or the " -"filesystem in question.\n" , ino, dev, context); - }else { -pr_warn ("SELinux: %s: context_to_sid(%s) " -"returned %d for dev=%s ino=%ld\n" , - __func__, context, -rc, dev, ino); - } -kfree (context); -/* Leave with the unlabeled SID */ - rc =0 ; -break ; - } - } -kfree (context); +if (rc) + goto out;break ;case SECURITY_FS_USE_TASK : sid = task_sid;@@ -1586,9 +1585,21 @@ static int inode_doinit_with_dentry(struct inode *inode, struct dentry *opt_dent goto out; rc =selinux_genfs_get_sid (dentry, sclass, sbsec->flags, &sid); -dput (dentry); -if (rc) +if (rc) { +dput (dentry); goto out; + } + +if ((sbsec->flags &SE_SBGENFS_XATTR ) && + (inode->i_opflags &IOP_XATTR )) { + rc =inode_doinit_use_xattr (inode, dentry, + sid, &sid); +if (rc) { +dput (dentry); + goto out; + } + } +dput (dentry); }break ; }security/selinux/include/security.h +1
-0@@ -58,6 +58,7 @@#define SE_SBINITIALIZED 0x0100#define SE_SBPROC 0x0200#define SE_SBGENFS 0x0400 +#define SE_SBGENFS_XATTR 0x0800#define CONTEXT_STR "context="#define FSCONTEXT_STR "fscontext="
PATCH v7 4/7 kernfs: use simple_xattrs for security attributes
In reply to Ondrej MosnacekReplace the special handling of security xattrs with simple_xattrs, as is already done for the trusted xattrs. This simplifies the code and allows LSMs to use more than just a single xattr to do their business.
--- fs/kernfs/dir.c | 7 ++--- fs/kernfs/inode.c | 55 ++----------------------------------- fs/kernfs/kernfs-internal.h | 2 -- 3 files changed, 4 insertions(+), 60 deletions(-)
fs/kernfs/dir.c +2 -5
@@ -531,12 +531,9 @@ void kernfs_put(struct kernfs_node *kn)kfree_const (kn->name); -if (kn->iattr) { -if (kn->iattr->ia_secdata) -security_release_secctx (kn->iattr->ia_secdata, - kn->iattr->ia_secdata_len); +if (kn->iattr)simple_xattrs_free (&kn->iattr->xattrs); - } +kfree (kn->iattr);spin_lock (&kernfs_idr_lock);idr_remove (&root->ino_idr, kn->id.ino);
fs/kernfs/inode.c +2 -53
81 lines not on this page · raw message
fs/kernfs/kernfs-internal.h +0 -2
@@ -24,8 +24,6 @@ struct kernfs_iattrs {struct timespec64 ia_atime;struct timespec64 ia_mtime;struct timespec64 ia_ctime; -void *ia_secdata; -u32 ia_secdata_len;struct simple_xattrs xattrs; };
Signature · 1 line
2.20.1
PATCH v7 5/7 LSM: add new hook for kernfs node initialization
In reply to Ondrej MosnacekThis patch introduces a new security hook that is intended for initializing the security data for newly created kernfs nodes, which provide a way of storing a non-default security context, but need to operate independently from mounts (and therefore may not have an associated inode at the moment of creation).
The main motivation is to allow kernfs nodes to inherit the context of the parent under SELinux, similar to the behavior of security_inode_init_security(). Other LSMs may implement their own logic for handling the creation of new nodes.
This patch also adds helper functions to <linux/kernfs.h> for getting/setting security xattrs of a kernfs node so that LSMs hooks are able to do their job. Other important attributes should be accessible direcly in the kernfs_node fields (in case there is need for more, then new helpers should be added to kernfs.h along with the patch that needs them).
--- fs/kernfs/inode.c | 48 +++++++++++++++++++++++++++++++-------- include/linux/kernfs.h | 15 ++++++++++++ include/linux/lsm_hooks.h | 13 +++++++++++ include/linux/security.h | 9 ++++++++ security/security.c | 6 +++++ 5 files changed, 82 insertions(+), 9 deletions(-)
fs/kernfs/inode.c +39 -9
@@ -288,12 +288,11 @@ int kernfs_iop_permission(struct inode *inode, int mask)return generic_permission (inode, mask); } -static int kernfs_xattr_get (const struct xattr_handler *handler, -struct dentry *unused,struct inode *inode, -const char *suffix,void *value,size_t size) +static int kernfs_node_xattr_get (const struct xattr_handler *handler, +struct kernfs_node *kn,const char *suffix, +void *value,size_t size) {const char *name =xattr_full_name (handler, suffix); -struct kernfs_node *kn = inode->i_private;struct kernfs_iattrs *attrs; attrs =kernfs_iattrs_noalloc (kn);
@@ -303,13 +302,11 @@ static int kernfs_xattr_get(const struct xattr_handler *handler,return simple_xattr_get (&attrs->xattrs, name, value, size); } -static int kernfs_xattr_set (const struct xattr_handler *handler, -struct dentry *unused,struct inode *inode, -const char *suffix,const void *value, -size_t size,int flags) +static int kernfs_node_xattr_set (const struct xattr_handler *handler, +struct kernfs_node *kn,const char *suffix, +const void *value,size_t size,int flags) {const char *name =xattr_full_name (handler, suffix); -struct kernfs_node *kn = inode->i_private;struct kernfs_iattrs *attrs; attrs =kernfs_iattrs (kn);
@@ -319,6 +316,25 @@ static int kernfs_xattr_set(const struct xattr_handler *handler,return simple_xattr_set (&attrs->xattrs, name, value, size, flags); } +static int kernfs_xattr_get (const struct xattr_handler *handler, +struct dentry *unused,struct inode *inode, +const char *suffix,void *value,size_t size) +{ +struct kernfs_node *kn = inode->i_private; + +return kernfs_node_xattr_get (handler, kn, suffix, value, size); +} + +static int kernfs_xattr_set (const struct xattr_handler *handler, +struct dentry *unused,struct inode *inode, +const char *suffix,const void *value, +size_t size,int flags) +{ +struct kernfs_node *kn = inode->i_private; + +return kernfs_node_xattr_set (handler, kn, suffix, value, size, flags); +} +static const struct xattr_handler kernfs_trusted_xattr_handler = { .prefix =XATTR_TRUSTED_PREFIX , .get = kernfs_xattr_get,
@@ -336,3 +352,17 @@ const struct xattr_handler *kernfs_xattr_handlers[] = { &kernfs_security_xattr_handler, NULL }; + +int kernfs_security_xattr_get (struct kernfs_node *kn,const char *suffix, +void *value,size_t size) +{ +return kernfs_node_xattr_get (&kernfs_security_xattr_handler, + kn, suffix, value, size); +} + +int kernfs_security_xattr_set (struct kernfs_node *kn,const char *suffix, +void *value,size_t size,int flags) +{ +return kernfs_node_xattr_set (&kernfs_security_xattr_handler, + kn, suffix, value, size, flags); +}
include/linux/kernfs.h +15 -0
@@ -352,6 +352,11 @@ int kernfs_rename_ns(struct kernfs_node *kn, struct kernfs_node *new_parent,int kernfs_setattr (struct kernfs_node *kn,const struct iattr *iattr);void kernfs_notify (struct kernfs_node *kn); +int kernfs_security_xattr_get (struct kernfs_node *kn,const char *suffix, +void *value,size_t size); +int kernfs_security_xattr_set (struct kernfs_node *kn,const char *suffix, +void *value,size_t size,int flags); +const void *kernfs_super_ns (struct super_block *sb); struct dentry *kernfs_mount_ns(struct file_system_type *fs_type, int flags, struct kernfs_root *root, unsigned long magic,
@@ -456,6 +461,16 @@ static inline int kernfs_setattr(struct kernfs_node *kn,static inline void kernfs_notify (struct kernfs_node *kn) { } +static inline int kernfs_security_xattr_get (struct kernfs_node *kn, +const char *suffix,void *value, +size_t size) +{return -ENOSYS ; } + +static inline int kernfs_security_xattr_set (struct kernfs_node *kn, +const char *suffix,void *value, +size_t size,int flags) +{return -ENOSYS ; } +static inline const void *kernfs_super_ns (struct super_block *sb) {return NULL ; }
include/linux/lsm_hooks.h +13 -0
@@ -429,6 +429,15 @@* 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 kernfs node operations +* +* @kernfs_init_security +* Initialize the security context of a newly created kernfs node based +* on its own and its parent's attributes. +* +* @kn_dir the parent kernfs node +* @kn the new child kernfs node +* * Security hooks for file operations * * @file_permission:
@@ -1558,6 +1567,9 @@ union security_list_options {int (*inode_copy_up)(struct dentry *src,struct cred **new);int (*inode_copy_up_xattr)(const char *name); +int (*kernfs_init_security)(struct kernfs_node *kn_dir, +struct kernfs_node *kn); +int (*file_permission)(struct file *file,int mask);int (*file_alloc_security)(struct file *file);void (*file_free_security)(struct file *file);
@@ -1858,6 +1870,7 @@ struct security_hook_heads {struct hlist_head inode_getsecid;struct hlist_head inode_copy_up;struct hlist_head inode_copy_up_xattr; +struct hlist_head kernfs_init_security;struct hlist_head file_permission;struct hlist_head file_alloc_security;struct hlist_head file_free_security;
include/linux/security.h +9 -0
@@ -51,6 +51,7 @@ struct fown_struct;struct file_operations ;struct msg_msg ;struct xattr ; +struct kernfs_node ;struct xfrm_sec_ctx ;struct mm_struct ;
@@ -291,6 +292,8 @@ int security_inode_listsecurity(struct inode *inode, char *buffer, size_t buffervoid 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_kernfs_init_security (struct kernfs_node *kn_dir, +struct kernfs_node *kn);int security_file_permission (struct file *file,int mask);int security_file_alloc (struct file *file);void security_file_free (struct file *file);
@@ -783,6 +786,12 @@ static inline int security_inode_copy_up(struct dentry *src, struct cred **new)return 0 ; } +static inline int security_kernfs_init_security (struct kernfs_node *kn_dir, +struct kernfs_node *kn) +{ +return 0 ; +} +static inline int security_inode_copy_up_xattr (const char *name) {return -EOPNOTSUPP ;
security/security.c +6 -0
@@ -892,6 +892,12 @@ int security_inode_copy_up_xattr(const char *name) }EXPORT_SYMBOL (security_inode_copy_up_xattr); +int security_kernfs_init_security (struct kernfs_node *kn_dir, +struct kernfs_node *kn) +{ +return call_int_hook (kernfs_init_security,0 , kn_dir, kn); +} +int security_file_permission (struct file *file,int mask) {int ret;
Signature · 1 line
2.20.1
PATCH v7 6/7 selinux: implement the kernfs_init_security hook
In reply to Ondrej MosnacekThe hook applies the same logic as selinux_determine_inode_label(), with the exception of the super_block handling, which will be enforced on the actual inodes later by other hooks.
--- security/selinux/hooks.c | 66 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+)
security/selinux/hooks.c +66 -0
@@ -88,6 +88,8 @@#include <linux/msg.h> #include <linux/shm.h> #include <linux/bpf.h> +#include <linux/kernfs.h> +#include <linux/stringhash.h> /* for hashlen_string() */ #include <uapi/linux/mount.h> #include "avc.h"
@@ -3350,6 +3352,68 @@ static int selinux_inode_copy_up_xattr(const char *name)return -EOPNOTSUPP ; } +/* kernfs node operations */ + +int selinux_kernfs_init_security (struct kernfs_node *kn_dir, +struct kernfs_node *kn) +{ +const struct task_security_struct *tsec =current_security (); +u32 parent_sid, newsid, clen; +int rc; +char *context; + + rc =kernfs_security_xattr_get (kn_dir,XATTR_SELINUX_SUFFIX ,NULL ,0 ); +if (rc == -ENODATA ) +return 0 ; +else if (rc <0 ) +return rc; + + clen = (u32 )rc; + context =kmalloc (clen,GFP_KERNEL ); +if (!context) +return -ENOMEM ; + + rc =kernfs_security_xattr_get (kn_dir,XATTR_SELINUX_SUFFIX , context, + clen); +if (rc <0 ) { +kfree (context); +return rc; + } + + rc =security_context_to_sid (&selinux_state, context, clen, &parent_sid, +GFP_KERNEL ); +kfree (context); +if (rc) +return rc; + +if (tsec->create_sid) { + newsid = tsec->create_sid; + }else { +u16 secclass =inode_mode_to_security_class (kn->mode); +struct qstr q; + + q.name = kn->name; + q.hash_len =hashlen_string (kn_dir, kn->name); + + rc =security_transition_sid (&selinux_state, tsec->sid, + parent_sid, secclass, &q, + &newsid); +if (rc) +return rc; + } + + rc =security_sid_to_context_force (&selinux_state, newsid, + &context, &clen); +if (rc) +return rc; + + rc =kernfs_security_xattr_set (kn,XATTR_SELINUX_SUFFIX , context, clen, +XATTR_CREATE ); +kfree (context); +return rc; +} + +/* file security operations */ static int selinux_revalidate_file_permission (struct file *file,int mask)
@@ -6796,6 +6860,8 @@ static struct security_hook_list selinux_hooks[] __lsm_ro_after_init = {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 (kernfs_init_security, selinux_kernfs_init_security), +LSM_HOOK_INIT (file_permission, selinux_file_permission),LSM_HOOK_INIT (file_alloc_security, selinux_file_alloc_security), LSM_HOOK_INIT(file_free_security, selinux_file_free_security),
Signature · 1 line
2.20.1
PATCH v7 7/7 kernfs: initialize security of newly created nodes
In reply to Ondrej MosnacekUse the new security_kernfs_init_security() hook to allow LSMs to possibly assign a non-default security context to a newly created kernfs node based on the attributes of the new node and also its parent node.
This fixes an issue with cgroupfs under SELinux, where newly created cgroup subdirectories/files would not inherit its parent's context if it had been set explicitly to a non-default value (other than the genfs context specified by the policy). This can be reproduced as follows (on Fedora/RHEL):
# mkdir /sys/fs/cgroup/unified/test
# # Need permissive to change the label under Fedora policy:
# setenforce 0
# chcon -t container_file_t /sys/fs/cgroup/unified/test
# ls -lZ /sys/fs/cgroup/unified
total 0
-r--r--r--. 1 root root system_u:object_r:cgroup_t:s0 0 Jan 29 03:06 cgroup.controllers
-rw-r--r--. 1 root root system_u:object_r:cgroup_t:s0 0 Jan 29 03:06 cgroup.max.depth
-rw-r--r--. 1 root root system_u:object_r:cgroup_t:s0 0 Jan 29 03:06 cgroup.max.descendants
-rw-r--r--. 1 root root system_u:object_r:cgroup_t:s0 0 Jan 29 03:06 cgroup.procs
-r--r--r--. 1 root root system_u:object_r:cgroup_t:s0 0 Jan 29 03:06 cgroup.stat
-rw-r--r--. 1 root root system_u:object_r:cgroup_t:s0 0 Jan 29 03:06 cgroup.subtree_control
-rw-r--r--. 1 root root system_u:object_r:cgroup_t:s0 0 Jan 29 03:06 cgroup.threads
drwxr-xr-x. 2 root root system_u:object_r:cgroup_t:s0 0 Jan 29 03:06 init.scope
drwxr-xr-x. 26 root root system_u:object_r:cgroup_t:s0 0 Jan 29 03:21 system.slice
drwxr-xr-x. 3 root root system_u:object_r:container_file_t:s0 0 Jan 29 03:15 test
drwxr-xr-x. 3 root root system_u:object_r:cgroup_t:s0 0 Jan 29 03:06 user.slice
# mkdir /sys/fs/cgroup/unified/test/subdirActual result:
# ls -ldZ /sys/fs/cgroup/unified/test/subdir
drwxr-xr-x. 2 root root system_u:object_r:cgroup_t:s0 0 Jan 29 03:15 /sys/fs/cgroup/unified/test/subdirExpected result:
# ls -ldZ /sys/fs/cgroup/unified/test/subdir
drwxr-xr-x. 2 root root unconfined_u:object_r:container_file_t:s0 0 Jan 29 03:15 /sys/fs/cgroup/unified/test/subdir--- fs/kernfs/dir.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-)
fs/kernfs/dir.c +9 -2
@@ -615,6 +615,7 @@ struct kernfs_node *kernfs_node_from_dentry(struct dentry *dentry) }static struct kernfs_node *__kernfs_new_node (struct kernfs_root *root, +struct kernfs_node *parent,const char *name,umode_t mode,kuid_t uid,kgid_t gid,unsigned flags)
@@ -671,6 +672,12 @@ static struct kernfs_node *__kernfs_new_node(struct kernfs_root *root, goto err_out3; } +if (parent) { + ret =security_kernfs_init_security (parent, kn); +if (ret) + goto err_out3; + } +return kn; err_out3:
@@ -689,7 +696,7 @@ struct kernfs_node *kernfs_new_node(struct kernfs_node *parent, {struct kernfs_node *kn; - kn =__kernfs_new_node (kernfs_root (parent), + kn =__kernfs_new_node (kernfs_root (parent), parent, name, mode, uid, gid, flags);if (kn) {kernfs_get (parent);
@@ -958,7 +965,7 @@ struct kernfs_root *kernfs_create_root(struct kernfs_syscall_ops *scops,INIT_LIST_HEAD (&root->supers); root->next_generation =1 ; - kn =__kernfs_new_node (root,"" ,S_IFDIR |S_IRUGO |S_IXUGO , + kn =__kernfs_new_node (root,NULL ,"" ,S_IFDIR |S_IRUGO |S_IXUGO ,GLOBAL_ROOT_UID ,GLOBAL_ROOT_GID ,KERNFS_DIR );if (!kn) {
Signature · 1 line
2.20.1
Quoted 127 lines · Ondrej Mosnacek
TL;DR:
This series adds a new security hook that allows to initialize the security context of kernfs properly, taking into account the parent context (and possibly other attributes). Kernfs nodes require special handling here, since they are not bound to specific inodes/superblocks, but instead represent the backing tree structure that is used to build the VFS tree when the kernfs tree is mounted.Changes in v7:
- simplify the new security hook's interface
- rather than trying to extract kernfs data into common structures, just pass the kernfs nodes themselves and add helper functions to <linux/kernfs.h> for accessing their security xattrs
- in case other LSMs need more kernfs node attributes than the file mode (uid/gid/...), they can simply add new helpers to <linux/kernfs.h> as needed
- refactor "kernfs: usesimple_xattrsfor security attributes" to keep using a single commonsimple_xattrsstructure
- turns out having two separatesimple_xattrswouldn't work right (see the definition ofsimple_xattr_list()infs/xattr.c)
- drop unnecessary initializations frominode_doinit_use_xattr()
- move theIOP_XATTRcheck out ofinode_doinit_use_xattr()
- add two kernfs cleanup patches
- these could be applied independently, but the rest of the patches depend on them, so I'd rather they stay bundled with the rest to avoid cross-tree conflictsv6: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v6:
- remove copy-pasted duplicate macro definitionv5: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v5:
- fix misplaced semicolon detected by 0day robotv4: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v4:
- reorder and rename hook arguments
- avoid allocatingkernfs_iattrsunless neededv3: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v3:
- rename the hook to "kernfs_init_security"
- change the hook interface to simply pass pointers to struct iattr and structsimple_xattrsof both the new node and its parent
- add full security xattr support to kernfs (and fixup SELinux behavior to handle it properly)v2: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v2:
- add docstring for the new hook in unionsecurity_list_options
- initialize *ctx to NULL and *ctxlen to 0 in case the hook is not implementedv1: https://lore.kernel.org/selinux/[email protected]/T/
The kernfs nodes initially do not store any security context and rely on the LSM to assign some default context to inodes created over them. Kernfs inodes, however, allow setting an explicit context via the *setxattr(2) syscalls, in which case the context is stored inside the kernfs node's internal structure.
SELinux (and possibly other LSMs) initialize the context of newly created FS objects based on the parent object's context (usually the child inherits the parent's context, unless the policy dictates otherwise). This is done by hooking the creation of the new inode corresponding to the newly created file/directory via
security_inode_init_security()(most filesystems always create a fresh inode when a new FS object is created). However, kernfs nodes can be created "behind the scenes" while the filesystem is not mounted anywhere and thus no inodes can exist for them yet.Therefore, to allow maintaining similar behavior for kernfs nodes, a new LSM hook is needed, which will allow initializing the kernfs node's security context based on its own attributes and those of the parent's node.
The main motivation for this change is that the userspace users of cgroupfs (which is built on kernfs) expect the usual security context inheritance to work under SELinux (see [1] and [2]). This functionality is required for better confinement of containers under SELinux.
Patch 1/7 simplifies the
kernfs_iattrsstructure and patch 2/7 optimizes kernfs to not allocatekernfs_iattrswhen getting the value of an xattr.Patch 3/7 changes SELinux to fetch security context from extended attributes on kernfs filesystems, falling back to genfs-defined context if that fails. Without this patch the 4/7 would be a regression for SELinux (due to the removal of ...notifysecctx() call.
Patch 4/7 implements full security xattr support in kernfs using
simple_xattrs; patch 5/7 adds the new LSM hook; patch 6/7 implements the new hook in SELinux; and patch 7/7 modifies kernfs to call the new hook on new node creation.Testing:
- passed the reproducer from the commit message of the last patch
- passed SELinux testsuite on Fedora Rawhide (x86_64) when applied on top of current Rawhide kernel (5.0.0-0.rc7.git2.1) [3]
- including the new proposed selinux-testsuite subtest [4] (adapted from the reproducer)[1] https://github.com/SELinuxProject/selinux-kernel/issues/39
[2] https://bugzilla.redhat.com/show_bug.cgi?id=1553803
[3] https://koji.fedoraproject.org/koji/taskinfo?taskID=32963825
[4] https://github.com/SELinuxProject/selinux-testsuite/pull/48Ondrej Mosnacek (7):
kernfs: clean up structkernfs_iattrs
kernfs: do not alloc iattrs inkernfs_xattr_get
selinux: try security xattr after genfs for kernfs filesystems
kernfs: usesimple_xattrsfor security attributes
LSM: add new hook for kernfs node initialization
selinux: implement thekernfs_init_securityhook
kernfs: initialize security of newly created nodesfs/kernfs/dir.c | 28 ++-- fs/kernfs/inode.c | 166 +++++++++------------ fs/kernfs/kernfs-internal.h | 8 +- fs/kernfs/symlink.c | 4 +- include/linux/kernfs.h | 15 ++ include/linux/lsm_hooks.h | 13 ++ include/linux/security.h | 9 ++ security/security.c | 6 + security/selinux/hooks.c | 223 +++++++++++++++++++--------- security/selinux/include/security.h | 1 + 10 files changed, 290 insertions(+), 183 deletions(-)
Quoted 2 lines
--
2.20.1
Ping about this series... Casey, are you OK with this new version?
Signature · 3 lines
Ondrej Mosnacek <omosnace at redhat dot com> Associate Software Engineer, Security Technologies Red Hat, Inc.
Paul Moore
maintainerQuoted 128 lines · Ondrej Mosnacek
Quoted 127 lines · Ondrej Mosnacek
TL;DR:
This series adds a new security hook that allows to initialize the security context of kernfs properly, taking into account the parent context (and possibly other attributes). Kernfs nodes require special handling here, since they are not bound to specific inodes/superblocks, but instead represent the backing tree structure that is used to build the VFS tree when the kernfs tree is mounted.Changes in v7:
- simplify the new security hook's interface
- rather than trying to extract kernfs data into common structures, just pass the kernfs nodes themselves and add helper functions to <linux/kernfs.h> for accessing their security xattrs
- in case other LSMs need more kernfs node attributes than the file mode (uid/gid/...), they can simply add new helpers to <linux/kernfs.h> as needed
- refactor "kernfs: usesimple_xattrsfor security attributes" to keep using a single commonsimple_xattrsstructure
- turns out having two separatesimple_xattrswouldn't work right (see the definition ofsimple_xattr_list()infs/xattr.c)
- drop unnecessary initializations frominode_doinit_use_xattr()
- move theIOP_XATTRcheck out ofinode_doinit_use_xattr()
- add two kernfs cleanup patches
- these could be applied independently, but the rest of the patches depend on them, so I'd rather they stay bundled with the rest to avoid cross-tree conflictsv6: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v6:
- remove copy-pasted duplicate macro definitionv5: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v5:
- fix misplaced semicolon detected by 0day robotv4: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v4:
- reorder and rename hook arguments
- avoid allocatingkernfs_iattrsunless neededv3: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v3:
- rename the hook to "kernfs_init_security"
- change the hook interface to simply pass pointers to struct iattr and structsimple_xattrsof both the new node and its parent
- add full security xattr support to kernfs (and fixup SELinux behavior to handle it properly)v2: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v2:
- add docstring for the new hook in unionsecurity_list_options
- initialize *ctx to NULL and *ctxlen to 0 in case the hook is not implementedv1: https://lore.kernel.org/selinux/[email protected]/T/
The kernfs nodes initially do not store any security context and rely on the LSM to assign some default context to inodes created over them. Kernfs inodes, however, allow setting an explicit context via the *setxattr(2) syscalls, in which case the context is stored inside the kernfs node's internal structure.
SELinux (and possibly other LSMs) initialize the context of newly created FS objects based on the parent object's context (usually the child inherits the parent's context, unless the policy dictates otherwise). This is done by hooking the creation of the new inode corresponding to the newly created file/directory via
security_inode_init_security()(most filesystems always create a fresh inode when a new FS object is created). However, kernfs nodes can be created "behind the scenes" while the filesystem is not mounted anywhere and thus no inodes can exist for them yet.Therefore, to allow maintaining similar behavior for kernfs nodes, a new LSM hook is needed, which will allow initializing the kernfs node's security context based on its own attributes and those of the parent's node.
The main motivation for this change is that the userspace users of cgroupfs (which is built on kernfs) expect the usual security context inheritance to work under SELinux (see [1] and [2]). This functionality is required for better confinement of containers under SELinux.
Patch 1/7 simplifies the
kernfs_iattrsstructure and patch 2/7 optimizes kernfs to not allocatekernfs_iattrswhen getting the value of an xattr.Patch 3/7 changes SELinux to fetch security context from extended attributes on kernfs filesystems, falling back to genfs-defined context if that fails. Without this patch the 4/7 would be a regression for SELinux (due to the removal of ...notifysecctx() call.
Patch 4/7 implements full security xattr support in kernfs using
simple_xattrs; patch 5/7 adds the new LSM hook; patch 6/7 implements the new hook in SELinux; and patch 7/7 modifies kernfs to call the new hook on new node creation.Testing:
- passed the reproducer from the commit message of the last patch
- passed SELinux testsuite on Fedora Rawhide (x86_64) when applied on top of current Rawhide kernel (5.0.0-0.rc7.git2.1) [3]
- including the new proposed selinux-testsuite subtest [4] (adapted from the reproducer)[1] https://github.com/SELinuxProject/selinux-kernel/issues/39
[2] https://bugzilla.redhat.com/show_bug.cgi?id=1553803
[3] https://koji.fedoraproject.org/koji/taskinfo?taskID=32963825
[4] https://github.com/SELinuxProject/selinux-testsuite/pull/48Ondrej Mosnacek (7):
kernfs: clean up structkernfs_iattrs
kernfs: do not alloc iattrs inkernfs_xattr_get
selinux: try security xattr after genfs for kernfs filesystems
kernfs: usesimple_xattrsfor security attributes
LSM: add new hook for kernfs node initialization
selinux: implement thekernfs_init_securityhook
kernfs: initialize security of newly created nodesfs/kernfs/dir.c | 28 ++-- fs/kernfs/inode.c | 166 +++++++++------------ fs/kernfs/kernfs-internal.h | 8 +- fs/kernfs/symlink.c | 4 +- include/linux/kernfs.h | 15 ++ include/linux/lsm_hooks.h | 13 ++ include/linux/security.h | 9 ++ security/security.c | 6 + security/selinux/hooks.c | 223 +++++++++++++++++++--------- security/selinux/include/security.h | 1 + 10 files changed, 290 insertions(+), 183 deletions(-)
Quoted 4 lines
Quoted 2 lines
--
2.20.1Ping about this series... Casey, are you OK with this new version?
We've got two weeks, well one and a half weeks, to decide as the earliest I would merge this would be after the merge window closes :)
Signature · 2 lines
paul moore www.paul-moore.com
Quoted 8 lines · Paul Moore
Quoted 4 lines · Ondrej Mosnacek
Quoted 1 line · Ondrej Mosnacek
[...]
Ping about this series... Casey, are you OK with this new version?
We've got two weeks, well one and a half weeks, to decide as the earliest I would merge this would be after the merge window closes :)
Sure, I just wanted to know early if there would be any more changes needed.
--
Ondrej Mosnacek <omosnace at redhat dot com> Associate Software Engineer, Security Technologies Red Hat, Inc.
Casey Schaufler
Acked-byQuoted 128 lines · Ondrej Mosnacek
Quoted 127 lines · Ondrej Mosnacek
TL;DR:
This series adds a new security hook that allows to initialize the security context of kernfs properly, taking into account the parent context (and possibly other attributes). Kernfs nodes require special handling here, since they are not bound to specific inodes/superblocks, but instead represent the backing tree structure that is used to build the VFS tree when the kernfs tree is mounted.Changes in v7:
- simplify the new security hook's interface
- rather than trying to extract kernfs data into common structures, just pass the kernfs nodes themselves and add helper functions to <linux/kernfs.h> for accessing their security xattrs
- in case other LSMs need more kernfs node attributes than the file mode (uid/gid/...), they can simply add new helpers to <linux/kernfs.h> as needed
- refactor "kernfs: usesimple_xattrsfor security attributes" to keep using a single commonsimple_xattrsstructure
- turns out having two separatesimple_xattrswouldn't work right (see the definition ofsimple_xattr_list()infs/xattr.c)
- drop unnecessary initializations frominode_doinit_use_xattr()
- move theIOP_XATTRcheck out ofinode_doinit_use_xattr()
- add two kernfs cleanup patches
- these could be applied independently, but the rest of the patches depend on them, so I'd rather they stay bundled with the rest to avoid cross-tree conflictsv6: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v6:
- remove copy-pasted duplicate macro definitionv5: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v5:
- fix misplaced semicolon detected by 0day robotv4: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v4:
- reorder and rename hook arguments
- avoid allocatingkernfs_iattrsunless neededv3: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v3:
- rename the hook to "kernfs_init_security"
- change the hook interface to simply pass pointers to struct iattr and structsimple_xattrsof both the new node and its parent
- add full security xattr support to kernfs (and fixup SELinux behavior to handle it properly)v2: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v2:
- add docstring for the new hook in unionsecurity_list_options
- initialize *ctx to NULL and *ctxlen to 0 in case the hook is not implementedv1: https://lore.kernel.org/selinux/[email protected]/T/
The kernfs nodes initially do not store any security context and rely on the LSM to assign some default context to inodes created over them. Kernfs inodes, however, allow setting an explicit context via the *setxattr(2) syscalls, in which case the context is stored inside the kernfs node's internal structure.
SELinux (and possibly other LSMs) initialize the context of newly created FS objects based on the parent object's context (usually the child inherits the parent's context, unless the policy dictates otherwise). This is done by hooking the creation of the new inode corresponding to the newly created file/directory via
security_inode_init_security()(most filesystems always create a fresh inode when a new FS object is created). However, kernfs nodes can be created "behind the scenes" while the filesystem is not mounted anywhere and thus no inodes can exist for them yet.Therefore, to allow maintaining similar behavior for kernfs nodes, a new LSM hook is needed, which will allow initializing the kernfs node's security context based on its own attributes and those of the parent's node.
The main motivation for this change is that the userspace users of cgroupfs (which is built on kernfs) expect the usual security context inheritance to work under SELinux (see [1] and [2]). This functionality is required for better confinement of containers under SELinux.
Patch 1/7 simplifies the
kernfs_iattrsstructure and patch 2/7 optimizes kernfs to not allocatekernfs_iattrswhen getting the value of an xattr.Patch 3/7 changes SELinux to fetch security context from extended attributes on kernfs filesystems, falling back to genfs-defined context if that fails. Without this patch the 4/7 would be a regression for SELinux (due to the removal of ...notifysecctx() call.
Patch 4/7 implements full security xattr support in kernfs using
simple_xattrs; patch 5/7 adds the new LSM hook; patch 6/7 implements the new hook in SELinux; and patch 7/7 modifies kernfs to call the new hook on new node creation.Testing:
- passed the reproducer from the commit message of the last patch
- passed SELinux testsuite on Fedora Rawhide (x86_64) when applied on top of current Rawhide kernel (5.0.0-0.rc7.git2.1) [3]
- including the new proposed selinux-testsuite subtest [4] (adapted from the reproducer)[1] https://github.com/SELinuxProject/selinux-kernel/issues/39
[2] https://bugzilla.redhat.com/show_bug.cgi?id=1553803
[3] https://koji.fedoraproject.org/koji/taskinfo?taskID=32963825
[4] https://github.com/SELinuxProject/selinux-testsuite/pull/48Ondrej Mosnacek (7):
kernfs: clean up structkernfs_iattrs
kernfs: do not alloc iattrs inkernfs_xattr_get
selinux: try security xattr after genfs for kernfs filesystems
kernfs: usesimple_xattrsfor security attributes
LSM: add new hook for kernfs node initialization
selinux: implement thekernfs_init_securityhook
kernfs: initialize security of newly created nodesfs/kernfs/dir.c | 28 ++-- fs/kernfs/inode.c | 166 +++++++++------------ fs/kernfs/kernfs-internal.h | 8 +- fs/kernfs/symlink.c | 4 +- include/linux/kernfs.h | 15 ++ include/linux/lsm_hooks.h | 13 ++ include/linux/security.h | 9 ++ security/security.c | 6 + security/selinux/hooks.c | 223 +++++++++++++++++++--------- security/selinux/include/security.h | 1 + 10 files changed, 290 insertions(+), 183 deletions(-)
Quoted 3 lines
Quoted 2 lines
--
2.20.1Ping about this series... Casey, are you OK with this new version?
I'm still not wildly enthusiastic about it, but I can't offer a better solution right now. You can add my
Quoted 129 lines · Casey Schaufler
Quoted 128 lines · Ondrej Mosnacek
Quoted 127 lines · Ondrej Mosnacek
TL;DR:
This series adds a new security hook that allows to initialize the security context of kernfs properly, taking into account the parent context (and possibly other attributes). Kernfs nodes require special handling here, since they are not bound to specific inodes/superblocks, but instead represent the backing tree structure that is used to build the VFS tree when the kernfs tree is mounted.Changes in v7:
- simplify the new security hook's interface
- rather than trying to extract kernfs data into common structures, just pass the kernfs nodes themselves and add helper functions to <linux/kernfs.h> for accessing their security xattrs
- in case other LSMs need more kernfs node attributes than the file mode (uid/gid/...), they can simply add new helpers to <linux/kernfs.h> as needed
- refactor "kernfs: usesimple_xattrsfor security attributes" to keep using a single commonsimple_xattrsstructure
- turns out having two separatesimple_xattrswouldn't work right (see the definition ofsimple_xattr_list()infs/xattr.c)
- drop unnecessary initializations frominode_doinit_use_xattr()
- move theIOP_XATTRcheck out ofinode_doinit_use_xattr()
- add two kernfs cleanup patches
- these could be applied independently, but the rest of the patches depend on them, so I'd rather they stay bundled with the rest to avoid cross-tree conflictsv6: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v6:
- remove copy-pasted duplicate macro definitionv5: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v5:
- fix misplaced semicolon detected by 0day robotv4: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v4:
- reorder and rename hook arguments
- avoid allocatingkernfs_iattrsunless neededv3: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v3:
- rename the hook to "kernfs_init_security"
- change the hook interface to simply pass pointers to struct iattr and structsimple_xattrsof both the new node and its parent
- add full security xattr support to kernfs (and fixup SELinux behavior to handle it properly)v2: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v2:
- add docstring for the new hook in unionsecurity_list_options
- initialize *ctx to NULL and *ctxlen to 0 in case the hook is not implementedv1: https://lore.kernel.org/selinux/[email protected]/T/
The kernfs nodes initially do not store any security context and rely on the LSM to assign some default context to inodes created over them. Kernfs inodes, however, allow setting an explicit context via the *setxattr(2) syscalls, in which case the context is stored inside the kernfs node's internal structure.
SELinux (and possibly other LSMs) initialize the context of newly created FS objects based on the parent object's context (usually the child inherits the parent's context, unless the policy dictates otherwise). This is done by hooking the creation of the new inode corresponding to the newly created file/directory via
security_inode_init_security()(most filesystems always create a fresh inode when a new FS object is created). However, kernfs nodes can be created "behind the scenes" while the filesystem is not mounted anywhere and thus no inodes can exist for them yet.Therefore, to allow maintaining similar behavior for kernfs nodes, a new LSM hook is needed, which will allow initializing the kernfs node's security context based on its own attributes and those of the parent's node.
The main motivation for this change is that the userspace users of cgroupfs (which is built on kernfs) expect the usual security context inheritance to work under SELinux (see [1] and [2]). This functionality is required for better confinement of containers under SELinux.
Patch 1/7 simplifies the
kernfs_iattrsstructure and patch 2/7 optimizes kernfs to not allocatekernfs_iattrswhen getting the value of an xattr.Patch 3/7 changes SELinux to fetch security context from extended attributes on kernfs filesystems, falling back to genfs-defined context if that fails. Without this patch the 4/7 would be a regression for SELinux (due to the removal of ...notifysecctx() call.
Patch 4/7 implements full security xattr support in kernfs using
simple_xattrs; patch 5/7 adds the new LSM hook; patch 6/7 implements the new hook in SELinux; and patch 7/7 modifies kernfs to call the new hook on new node creation.Testing:
- passed the reproducer from the commit message of the last patch
- passed SELinux testsuite on Fedora Rawhide (x86_64) when applied on top of current Rawhide kernel (5.0.0-0.rc7.git2.1) [3]
- including the new proposed selinux-testsuite subtest [4] (adapted from the reproducer)[1] https://github.com/SELinuxProject/selinux-kernel/issues/39
[2] https://bugzilla.redhat.com/show_bug.cgi?id=1553803
[3] https://koji.fedoraproject.org/koji/taskinfo?taskID=32963825
[4] https://github.com/SELinuxProject/selinux-testsuite/pull/48Ondrej Mosnacek (7):
kernfs: clean up structkernfs_iattrs
kernfs: do not alloc iattrs inkernfs_xattr_get
selinux: try security xattr after genfs for kernfs filesystems
kernfs: usesimple_xattrsfor security attributes
LSM: add new hook for kernfs node initialization
selinux: implement thekernfs_init_securityhook
kernfs: initialize security of newly created nodesfs/kernfs/dir.c | 28 ++-- fs/kernfs/inode.c | 166 +++++++++------------ fs/kernfs/kernfs-internal.h | 8 +- fs/kernfs/symlink.c | 4 +- include/linux/kernfs.h | 15 ++ include/linux/lsm_hooks.h | 13 ++ include/linux/security.h | 9 ++ security/security.c | 6 + security/selinux/hooks.c | 223 +++++++++++++++++++--------- security/selinux/include/security.h | 1 + 10 files changed, 290 insertions(+), 183 deletions(-)
Quoted 8 lines
Quoted 3 lines
Quoted 2 lines
--
2.20.1Ping about this series... Casey, are you OK with this new version?
I'm still not wildly enthusiastic about it, but I can't offer a better solution right now. You can add my
Acked-by Casey Schaufler <[email protected]>
Thanks!
Signature · 3 lines
Ondrej Mosnacek <omosnace at redhat dot com> Associate Software Engineer, Security Technologies Red Hat, Inc.
Paul Moore
maintainerQuoted 115 lines · Ondrej Mosnacek
TL;DR:
This series adds a new security hook that allows to initialize the security context of kernfs properly, taking into account the parent context (and possibly other attributes). Kernfs nodes require special handling here, since they are not bound to specific inodes/superblocks, but instead represent the backing tree structure that is used to build the VFS tree when the kernfs tree is mounted.Changes in v7:
- simplify the new security hook's interface
- rather than trying to extract kernfs data into common structures, just pass the kernfs nodes themselves and add helper functions to <linux/kernfs.h> for accessing their security xattrs
- in case other LSMs need more kernfs node attributes than the file mode (uid/gid/...), they can simply add new helpers to <linux/kernfs.h> as needed
- refactor "kernfs: usesimple_xattrsfor security attributes" to keep using a single commonsimple_xattrsstructure
- turns out having two separatesimple_xattrswouldn't work right (see the definition ofsimple_xattr_list()infs/xattr.c)
- drop unnecessary initializations frominode_doinit_use_xattr()
- move theIOP_XATTRcheck out ofinode_doinit_use_xattr()
- add two kernfs cleanup patches
- these could be applied independently, but the rest of the patches depend on them, so I'd rather they stay bundled with the rest to avoid cross-tree conflictsv6: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v6:
- remove copy-pasted duplicate macro definitionv5: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v5:
- fix misplaced semicolon detected by 0day robotv4: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v4:
- reorder and rename hook arguments
- avoid allocatingkernfs_iattrsunless neededv3: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v3:
- rename the hook to "kernfs_init_security"
- change the hook interface to simply pass pointers to struct iattr and structsimple_xattrsof both the new node and its parent
- add full security xattr support to kernfs (and fixup SELinux behavior to handle it properly)v2: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v2:
- add docstring for the new hook in unionsecurity_list_options
- initialize *ctx to NULL and *ctxlen to 0 in case the hook is not implementedv1: https://lore.kernel.org/selinux/[email protected]/T/
The kernfs nodes initially do not store any security context and rely on the LSM to assign some default context to inodes created over them. Kernfs inodes, however, allow setting an explicit context via the *setxattr(2) syscalls, in which case the context is stored inside the kernfs node's internal structure.
SELinux (and possibly other LSMs) initialize the context of newly created FS objects based on the parent object's context (usually the child inherits the parent's context, unless the policy dictates otherwise). This is done by hooking the creation of the new inode corresponding to the newly created file/directory via
security_inode_init_security()(most filesystems always create a fresh inode when a new FS object is created). However, kernfs nodes can be created "behind the scenes" while the filesystem is not mounted anywhere and thus no inodes can exist for them yet.Therefore, to allow maintaining similar behavior for kernfs nodes, a new LSM hook is needed, which will allow initializing the kernfs node's security context based on its own attributes and those of the parent's node.
The main motivation for this change is that the userspace users of cgroupfs (which is built on kernfs) expect the usual security context inheritance to work under SELinux (see [1] and [2]). This functionality is required for better confinement of containers under SELinux.
Patch 1/7 simplifies the
kernfs_iattrsstructure and patch 2/7 optimizes kernfs to not allocatekernfs_iattrswhen getting the value of an xattr.Patch 3/7 changes SELinux to fetch security context from extended attributes on kernfs filesystems, falling back to genfs-defined context if that fails. Without this patch the 4/7 would be a regression for SELinux (due to the removal of ...notifysecctx() call.
Patch 4/7 implements full security xattr support in kernfs using
simple_xattrs; patch 5/7 adds the new LSM hook; patch 6/7 implements the new hook in SELinux; and patch 7/7 modifies kernfs to call the new hook on new node creation.Testing:
- passed the reproducer from the commit message of the last patch
- passed SELinux testsuite on Fedora Rawhide (x86_64) when applied on top of current Rawhide kernel (5.0.0-0.rc7.git2.1) [3]
- including the new proposed selinux-testsuite subtest [4] (adapted from the reproducer)[1] https://github.com/SELinuxProject/selinux-kernel/issues/39
[2] https://bugzilla.redhat.com/show_bug.cgi?id=1553803
[3] https://koji.fedoraproject.org/koji/taskinfo?taskID=32963825
[4] https://github.com/SELinuxProject/selinux-testsuite/pull/48Ondrej Mosnacek (7):
kernfs: clean up structkernfs_iattrs
kernfs: do not alloc iattrs inkernfs_xattr_get
selinux: try security xattr after genfs for kernfs filesystems
kernfs: usesimple_xattrsfor security attributes
LSM: add new hook for kernfs node initialization
selinux: implement thekernfs_init_securityhook
kernfs: initialize security of newly created nodes
Quoted 11 lines
fs/kernfs/dir.c | 28 ++-- fs/kernfs/inode.c | 166 +++++++++------------ fs/kernfs/kernfs-internal.h | 8 +- fs/kernfs/symlink.c | 4 +- include/linux/kernfs.h | 15 ++ include/linux/lsm_hooks.h | 13 ++ include/linux/security.h | 9 ++ security/security.c | 6 + security/selinux/hooks.c | 223 +++++++++++++++++++--------- security/selinux/include/security.h | 1 + 10 files changed, 290 insertions(+), 183 deletions(-)
I just merged this patchset into selinux/next, thanks everyone.
Ondrej, every patch in the patchset except for one required some amount of merge fixups, please take a look and make sure everything in the selinux/next branch still looks right to you.
Signature · 2 lines
paul moore www.paul-moore.com
Quoted 128 lines · Paul Moore
Quoted 115 lines · Ondrej Mosnacek
TL;DR:
This series adds a new security hook that allows to initialize the security context of kernfs properly, taking into account the parent context (and possibly other attributes). Kernfs nodes require special handling here, since they are not bound to specific inodes/superblocks, but instead represent the backing tree structure that is used to build the VFS tree when the kernfs tree is mounted.Changes in v7:
- simplify the new security hook's interface
- rather than trying to extract kernfs data into common structures, just pass the kernfs nodes themselves and add helper functions to <linux/kernfs.h> for accessing their security xattrs
- in case other LSMs need more kernfs node attributes than the file mode (uid/gid/...), they can simply add new helpers to <linux/kernfs.h> as needed
- refactor "kernfs: usesimple_xattrsfor security attributes" to keep using a single commonsimple_xattrsstructure
- turns out having two separatesimple_xattrswouldn't work right (see the definition ofsimple_xattr_list()infs/xattr.c)
- drop unnecessary initializations frominode_doinit_use_xattr()
- move theIOP_XATTRcheck out ofinode_doinit_use_xattr()
- add two kernfs cleanup patches
- these could be applied independently, but the rest of the patches depend on them, so I'd rather they stay bundled with the rest to avoid cross-tree conflictsv6: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v6:
- remove copy-pasted duplicate macro definitionv5: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v5:
- fix misplaced semicolon detected by 0day robotv4: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v4:
- reorder and rename hook arguments
- avoid allocatingkernfs_iattrsunless neededv3: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v3:
- rename the hook to "kernfs_init_security"
- change the hook interface to simply pass pointers to struct iattr and structsimple_xattrsof both the new node and its parent
- add full security xattr support to kernfs (and fixup SELinux behavior to handle it properly)v2: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v2:
- add docstring for the new hook in unionsecurity_list_options
- initialize *ctx to NULL and *ctxlen to 0 in case the hook is not implementedv1: https://lore.kernel.org/selinux/[email protected]/T/
The kernfs nodes initially do not store any security context and rely on the LSM to assign some default context to inodes created over them. Kernfs inodes, however, allow setting an explicit context via the *setxattr(2) syscalls, in which case the context is stored inside the kernfs node's internal structure.
SELinux (and possibly other LSMs) initialize the context of newly created FS objects based on the parent object's context (usually the child inherits the parent's context, unless the policy dictates otherwise). This is done by hooking the creation of the new inode corresponding to the newly created file/directory via
security_inode_init_security()(most filesystems always create a fresh inode when a new FS object is created). However, kernfs nodes can be created "behind the scenes" while the filesystem is not mounted anywhere and thus no inodes can exist for them yet.Therefore, to allow maintaining similar behavior for kernfs nodes, a new LSM hook is needed, which will allow initializing the kernfs node's security context based on its own attributes and those of the parent's node.
The main motivation for this change is that the userspace users of cgroupfs (which is built on kernfs) expect the usual security context inheritance to work under SELinux (see [1] and [2]). This functionality is required for better confinement of containers under SELinux.
Patch 1/7 simplifies the
kernfs_iattrsstructure and patch 2/7 optimizes kernfs to not allocatekernfs_iattrswhen getting the value of an xattr.Patch 3/7 changes SELinux to fetch security context from extended attributes on kernfs filesystems, falling back to genfs-defined context if that fails. Without this patch the 4/7 would be a regression for SELinux (due to the removal of ...notifysecctx() call.
Patch 4/7 implements full security xattr support in kernfs using
simple_xattrs; patch 5/7 adds the new LSM hook; patch 6/7 implements the new hook in SELinux; and patch 7/7 modifies kernfs to call the new hook on new node creation.Testing:
- passed the reproducer from the commit message of the last patch
- passed SELinux testsuite on Fedora Rawhide (x86_64) when applied on top of current Rawhide kernel (5.0.0-0.rc7.git2.1) [3]
- including the new proposed selinux-testsuite subtest [4] (adapted from the reproducer)[1] https://github.com/SELinuxProject/selinux-kernel/issues/39
[2] https://bugzilla.redhat.com/show_bug.cgi?id=1553803
[3] https://koji.fedoraproject.org/koji/taskinfo?taskID=32963825
[4] https://github.com/SELinuxProject/selinux-testsuite/pull/48Ondrej Mosnacek (7):
kernfs: clean up structkernfs_iattrs
kernfs: do not alloc iattrs inkernfs_xattr_get
selinux: try security xattr after genfs for kernfs filesystems
kernfs: usesimple_xattrsfor security attributes
LSM: add new hook for kernfs node initialization
selinux: implement thekernfs_init_securityhook
kernfs: initialize security of newly created nodesQuoted 11 lines
fs/kernfs/dir.c | 28 ++-- fs/kernfs/inode.c | 166 +++++++++------------ fs/kernfs/kernfs-internal.h | 8 +- fs/kernfs/symlink.c | 4 +- include/linux/kernfs.h | 15 ++ include/linux/lsm_hooks.h | 13 ++ include/linux/security.h | 9 ++ security/security.c | 6 + security/selinux/hooks.c | 223 +++++++++++++++++++--------- security/selinux/include/security.h | 1 + 10 files changed, 290 insertions(+), 183 deletions(-)
Quoted 5 lines
I just merged this patchset into selinux/next, thanks everyone.
Ondrej, every patch in the patchset except for one required some amount of merge fixups, please take a look and make sure everything in the selinux/next branch still looks right to you.
Looks good to me, thanks! I checked by rebasing the original branch on v5.1-rc1 myself and then comparing my result with selinux/next. The only difference was one extra empty line on my side, but that doesn't bother me :)
Signature · 3 lines
Ondrej Mosnacek <omosnace at redhat dot com> Software Engineer, Security Technologies Red Hat, Inc.
Paul Moore
maintainerQuoted 132 lines · Ondrej Mosnacek
Quoted 130 lines · Paul Moore
Quoted 115 lines · Ondrej Mosnacek
TL;DR:
This series adds a new security hook that allows to initialize the security context of kernfs properly, taking into account the parent context (and possibly other attributes). Kernfs nodes require special handling here, since they are not bound to specific inodes/superblocks, but instead represent the backing tree structure that is used to build the VFS tree when the kernfs tree is mounted.Changes in v7:
- simplify the new security hook's interface
- rather than trying to extract kernfs data into common structures, just pass the kernfs nodes themselves and add helper functions to <linux/kernfs.h> for accessing their security xattrs
- in case other LSMs need more kernfs node attributes than the file mode (uid/gid/...), they can simply add new helpers to <linux/kernfs.h> as needed
- refactor "kernfs: usesimple_xattrsfor security attributes" to keep using a single commonsimple_xattrsstructure
- turns out having two separatesimple_xattrswouldn't work right (see the definition ofsimple_xattr_list()infs/xattr.c)
- drop unnecessary initializations frominode_doinit_use_xattr()
- move theIOP_XATTRcheck out ofinode_doinit_use_xattr()
- add two kernfs cleanup patches
- these could be applied independently, but the rest of the patches depend on them, so I'd rather they stay bundled with the rest to avoid cross-tree conflictsv6: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v6:
- remove copy-pasted duplicate macro definitionv5: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v5:
- fix misplaced semicolon detected by 0day robotv4: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v4:
- reorder and rename hook arguments
- avoid allocatingkernfs_iattrsunless neededv3: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v3:
- rename the hook to "kernfs_init_security"
- change the hook interface to simply pass pointers to struct iattr and structsimple_xattrsof both the new node and its parent
- add full security xattr support to kernfs (and fixup SELinux behavior to handle it properly)v2: https://lore.kernel.org/selinux/[email protected]/T/
Changes in v2:
- add docstring for the new hook in unionsecurity_list_options
- initialize *ctx to NULL and *ctxlen to 0 in case the hook is not implementedv1: https://lore.kernel.org/selinux/[email protected]/T/
The kernfs nodes initially do not store any security context and rely on the LSM to assign some default context to inodes created over them. Kernfs inodes, however, allow setting an explicit context via the *setxattr(2) syscalls, in which case the context is stored inside the kernfs node's internal structure.
SELinux (and possibly other LSMs) initialize the context of newly created FS objects based on the parent object's context (usually the child inherits the parent's context, unless the policy dictates otherwise). This is done by hooking the creation of the new inode corresponding to the newly created file/directory via
security_inode_init_security()(most filesystems always create a fresh inode when a new FS object is created). However, kernfs nodes can be created "behind the scenes" while the filesystem is not mounted anywhere and thus no inodes can exist for them yet.Therefore, to allow maintaining similar behavior for kernfs nodes, a new LSM hook is needed, which will allow initializing the kernfs node's security context based on its own attributes and those of the parent's node.
The main motivation for this change is that the userspace users of cgroupfs (which is built on kernfs) expect the usual security context inheritance to work under SELinux (see [1] and [2]). This functionality is required for better confinement of containers under SELinux.
Patch 1/7 simplifies the
kernfs_iattrsstructure and patch 2/7 optimizes kernfs to not allocatekernfs_iattrswhen getting the value of an xattr.Patch 3/7 changes SELinux to fetch security context from extended attributes on kernfs filesystems, falling back to genfs-defined context if that fails. Without this patch the 4/7 would be a regression for SELinux (due to the removal of ...notifysecctx() call.
Patch 4/7 implements full security xattr support in kernfs using
simple_xattrs; patch 5/7 adds the new LSM hook; patch 6/7 implements the new hook in SELinux; and patch 7/7 modifies kernfs to call the new hook on new node creation.Testing:
- passed the reproducer from the commit message of the last patch
- passed SELinux testsuite on Fedora Rawhide (x86_64) when applied on top of current Rawhide kernel (5.0.0-0.rc7.git2.1) [3]
- including the new proposed selinux-testsuite subtest [4] (adapted from the reproducer)[1] https://github.com/SELinuxProject/selinux-kernel/issues/39
[2] https://bugzilla.redhat.com/show_bug.cgi?id=1553803
[3] https://koji.fedoraproject.org/koji/taskinfo?taskID=32963825
[4] https://github.com/SELinuxProject/selinux-testsuite/pull/48Ondrej Mosnacek (7):
kernfs: clean up structkernfs_iattrs
kernfs: do not alloc iattrs inkernfs_xattr_get
selinux: try security xattr after genfs for kernfs filesystems
kernfs: usesimple_xattrsfor security attributes
LSM: add new hook for kernfs node initialization
selinux: implement thekernfs_init_securityhook
kernfs: initialize security of newly created nodesQuoted 11 lines
fs/kernfs/dir.c | 28 ++-- fs/kernfs/inode.c | 166 +++++++++------------ fs/kernfs/kernfs-internal.h | 8 +- fs/kernfs/symlink.c | 4 +- include/linux/kernfs.h | 15 ++ include/linux/lsm_hooks.h | 13 ++ include/linux/security.h | 9 ++ security/security.c | 6 + security/selinux/hooks.c | 223 +++++++++++++++++++--------- security/selinux/include/security.h | 1 + 10 files changed, 290 insertions(+), 183 deletions(-)I just merged this patchset into selinux/next, thanks everyone.
Quoted 8 lines
Quoted 3 lines
Ondrej, every patch in the patchset except for one required some amount of merge fixups, please take a look and make sure everything in the selinux/next branch still looks right to you.
Looks good to me, thanks! I checked by rebasing the original branch on v5.1-rc1 myself and then comparing my result with selinux/next. The only difference was one extra empty line on my side, but that doesn't bother me :)
Thanks for double-checking.
Signature · 2 lines
paul moore www.paul-moore.com