2/2 rootcontext=
RFC PATCH 1/1 2/2 rootcontext=
The second patch will introduce a new rootcontext= option to mounting. This option will allow you to explicitly label the root inode of a FS being mounted. This was found to be useful for things like stateless linux, see https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=190001
I'd like some close eyes on this patch, specifically because I had to move the avc_has_perm() checks farther up the stack. I'm also not sure even if we want perms on this operation. Right now you need the same perms to use rootcontext= that you need to use chcon /mnt/point after you mount. I'm also not sure if this might be a good mechanism to usefully mount a fs (with XATTR support) from a different machine that had contexts that may not even exist on the mounting machine.
hooks.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- include/objsec.h | 1 2 files changed, 56 insertions(+), 1 deletion(-)
security/selinux/hooks.c renamed from security/selinux/hooks.c.decouple +55 -1
@@ -247,6 +247,7 @@ static int superblock_alloc_security(str sbsec->sid =SECINITSID_UNLABELED ; sbsec->def_sid =SECINITSID_FILE ; sbsec->mntpoint_sid =SECINITSID_UNLABELED ; + sbsec->root_sid =SECINITSID_UNLABELED ; sb->s_security = sbsec;return 0 ;
@@ -320,12 +321,14 @@ enum { Opt_context =1 , Opt_fscontext =2 , Opt_defcontext =4 , + Opt_rootcontext = 8, };static match_table_t tokens = { {Opt_context,"context=%s" }, {Opt_fscontext,"fscontext=%s" }, {Opt_defcontext,"defcontext=%s" }, + {Opt_rootcontext,"rootcontext=%s" }, };#define SEL_MOUNT_FAIL_MSG "SELinux: duplicate or incompatible mount options\n"
@@ -333,7 +336,7 @@ static match_table_t tokens = {static int try_context_mount (struct super_block *sb,void *data) {char *context =NULL , *defcontext =NULL ; -char *fscontext =NULL ; +char *fscontext =NULL , *rootcontext =NULL ;const char *name;u32 sid;int alloc =0 , rc =0 , seen =0 ;
@@ -407,6 +410,22 @@ static int try_context_mount(struct supe seen |= Opt_fscontext;break ; +case Opt_rootcontext: +if (seen & Opt_rootcontext) { + rc = -EINVAL ; + printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); + goto out_free; + } + rootcontext =match_strdup (&args[0 ]); +if (!rootcontext) { + rc = -ENOMEM ; + goto out_free; + } +if (!alloc) + alloc =1 ; + seen |= Opt_fscontext; +break ; +case Opt_defcontext:if (sbsec->behavior !=SECURITY_FS_USE_XATTR ) { rc = -EINVAL ;
@@ -506,6 +525,23 @@ static int try_context_mount(struct supe sbsec->behavior =SECURITY_FS_USE_MNTPOINT ; } +if (rootcontext) { + rc =security_context_to_sid (rootcontext,strlen (rootcontext), &sid); +if (rc) { +printk (KERN_WARNING "SELinux: security_context_to_sid" +"(%s) failed for (dev %s, type %s) errno=%d\n" , + rootcontext, sb->s_id, name, rc); + goto out_free; + } + +/* +* Security checking on this transition is done later in superblock_doinit() +* since we don't actually know the root inode context we are relabeling from +* until after inode_doinit_with_dentry() has been called on the root inode. +*/ + sbsec->root_sid = sid; + } +if (defcontext) { rc =security_context_to_sid (defcontext,strlen (defcontext), &sid);if (rc) {
@@ -536,11 +572,14 @@ out_free:kfree (context);kfree (defcontext);kfree (fscontext); +kfree (rootcontext); } out:return rc; } +static int inode_security_set_sid (struct inode *inode,u32 sid); +static int superblock_doinit (struct super_block *sb,void *data) {struct superblock_security_struct *sbsec = sb->s_security;
@@ -619,6 +658,20 @@ static int superblock_doinit(struct supe/* Initialize the root inode. */ rc =inode_doinit_with_dentry (sb->s_root->d_inode, sb->s_root); +/* Set the context on the root inode if mounted with rootcontext= */ +if ((sbsec->root_sid !=SECINITSID_UNLABELED ) && !rc) { +struct inode_security_struct *isec = sb->s_root->d_inode->i_security; +struct task_security_struct *tsec = current->security; + + rc =avc_has_perm (tsec->sid, isec->sid,SECCLASS_FILESYSTEM , +FILESYSTEM__RELABELFROM ,NULL ); +if (!rc) + rc =avc_has_perm (tsec->sid, sbsec->root_sid,SECCLASS_FILESYSTEM , +FILESYSTEM__RELABELTO ,NULL ); +if (!rc) +inode_security_set_sid (sb->s_root->d_inode, sbsec->root_sid); + } +/* Initialize any other inodes associated with the superblock, e.g. inodes created prior to initial policy load or inodes created during get_sb by a pseudo filesystem that directly
@@ -1889,6 +1942,7 @@ static inline int selinux_option(char *o {return (match_prefix ("context=" ,sizeof ("context=" )-1 , option, len) ||match_prefix ("fscontext=" ,sizeof ("fscontext=" )-1 , option, len) || +match_prefix ("rootcontext=" ,sizeof ("rootcontext=" )-1 , option, len) ||match_prefix ("defcontext=" ,sizeof ("defcontext=" )-1 , option, len)); }
security/selinux/include/objsec.h renamed from security/selinux/include/objsec.h.decouple +1 -0
@@ -59,6 +59,7 @@ struct superblock_security_struct {u32 sid;/* SID of file system superblock */ u32 def_sid;/* default SID for labeling */ u32 mntpoint_sid;/* SECURITY_FS_USE_MNTPOINT context for files */ +u32 root_sid;/* context of the root inode */ unsigned int behavior;/* labeling behavior */ unsigned char initialized;/* initialization flag */ unsigned char proc;/* proc fs */
--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 11 lines · Eric Paris
The second patch will introduce a new rootcontext= option to mounting. This option will allow you to explicitly label the root inode of a FS being mounted. This was found to be useful for things like stateless linux, see https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=190001
I'd like some close eyes on this patch, specifically because I had to move theavc_has_perm()checks farther up the stack. I'm also not sure even if we want perms on this operation. Right now you need the same perms to use rootcontext= that you need to use chcon/mnt/pointafter you mount. I'm also not sure if this might be a good mechanism to usefully mount a fs (with XATTR support) from a different machine that had contexts that may not even exist on the mounting machine.
Usually one would use context= aka mountpoint labeling for that purpose, not just rootcontext=.
Quoted 4 lines
hooks.c | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- include/objsec.h | 1 2 files changed, 56 insertions(+), 1 deletion(-)
diffstat -p1
Quoted 10 lines
security/selinux/hooks.c renamed from security/selinux/hooks.c.decouple +4
-0@@ -407,6 +410,22 @@ static int try_context_mount(struct supe seen |= Opt_fscontext;break ; +case Opt_rootcontext: +if (seen & Opt_rootcontext) { + rc = -EINVAL ; + printk(KERN_WARNING SEL_MOUNT_FAIL_MSG);
security/selinux/hooks.c static int try_context_mount(struct supe · line 426
Quoted 10 lines
security/selinux/hooks.c renamed from security/selinux/hooks.c.decouple +10
-0+ goto out_free; + } + rootcontext =match_strdup (&args[0 ]); +if (!rootcontext) { + rc = -ENOMEM ; + goto out_free; + } +if (!alloc) + alloc =1 ; + seen |= Opt_fscontext;
Wrong flag set above. Which also happens to be the value of token.
Quoted 9 lines
security/selinux/hooks.c renamed from security/selinux/hooks.c.decouple +5
-0@@ -506,6 +525,23 @@ static int try_context_mount(struct supe sbsec->behavior =SECURITY_FS_USE_MNTPOINT ; } +if (rootcontext) { + rc =security_context_to_sid (rootcontext,strlen (rootcontext), &sid); +if (rc) { + printk(KERN_WARNING "SELinux: security_context_to_sid" + "(%s) failed for (dev %s, type %s) errno=%d\n",
security/selinux/hooks.c static int try_context_mount(struct supe · line 542
Quoted 10 lines
security/selinux/hooks.c renamed from security/selinux/hooks.c.decouple +10
-0+ rootcontext, sb->s_id, name, rc); + goto out_free; + } + +/* +* Security checking on this transition is done later in superblock_doinit() +* since we don't actually know the root inode context we are relabeling from +* until after inode_doinit_with_dentry() has been called on the root inode. +*/ + sbsec->root_sid = sid;
I'd favor doing checking here, and paralleling the defcontext checks, i.e. you still check relabelfrom to the superblock (which abstractly represents setting any of these context options at mount time), and then check associate between the root SID and the superblock to ensure that they form a legal pair. I doubt we'd care to distinguish the ability to set rootcontext= from the ability to set defcontext= in the policy, whereas I think we might care to distinguish between setting the root inode security context to a particular value at mount time (before it is user-accessible) versus relabeling it later.
Quoted 22 lines
security/selinux/hooks.c renamed from security/selinux/hooks.c.decouple +6
-0@@ -536,11 +572,14 @@ out_free:kfree (context);kfree (defcontext);kfree (fscontext); +kfree (rootcontext); } out:return rc; } +static int inode_security_set_sid (struct inode *inode,u32 sid); +static int superblock_doinit (struct super_block *sb,void *data) {struct superblock_security_struct *sbsec = sb->s_security;@@ -619,6 +658,20 @@ static int superblock_doinit(struct supe/* Initialize the root inode. */ rc =inode_doinit_with_dentry (sb->s_root->d_inode, sb->s_root); +/* Set the context on the root inode if mounted with rootcontext= */ +if ((sbsec->root_sid !=SECINITSID_UNLABELED ) && !rc) { +struct inode_security_struct *isec = sb->s_root->d_inode->i_security;
security/selinux/hooks.c static int superblock_doinit(struct supe · line 673
Quoted 10 lines
security/selinux/hooks.c renamed from security/selinux/hooks.c.decouple +10
-0+struct task_security_struct *tsec = current->security; + + rc =avc_has_perm (tsec->sid, isec->sid,SECCLASS_FILESYSTEM , +FILESYSTEM__RELABELFROM ,NULL ); +if (!rc) + rc =avc_has_perm (tsec->sid, sbsec->root_sid,SECCLASS_FILESYSTEM , +FILESYSTEM__RELABELTO ,NULL ); +if (!rc) +inode_security_set_sid (sb->s_root->d_inode, sbsec->root_sid); + }
I think that the mixing of inode SIDs with filesystem classes here will cause confusion in the policy, so I wouldn't do this. Note that the actual file relabeling checks by setxattr are different - those are against the isec->sclass. If you set the root inode SID and its initialized flag in try_context_mount, then inode_doinit should do nothing here.
Signature · 8 lines
Stephen Smalley National Security Agency -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.