Subject: [RFC PATCH] 1/2 decouple fscontext/context From: Eric Paris To: selinux@tycho.nsa.gov Cc: jmorris@namei.org, sds@tycho.nsa.gov Content-Type: text/plain Date: Wed, 28 Jun 2006 19:09:25 -0400 Message-Id: <1151536165.26384.48.camel@localhost.localdomain> Mime-Version: 1.0 Sender: owner-selinux@tycho.nsa.gov List-Id: selinux@tycho.nsa.gov This patch will remove the conflict between fscontext and context mount options. It is now possible to use them both. If context= is specified without fscontext it will operate just as before, if both are specified we will use mount point labeling and all inodes will get the label specified by context=. The superblock will be labeled with the label of fscontext=, thus affecting associate permissions. hooks.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++------- include/objsec.h | 3 +- 2 files changed, 57 insertions(+), 8 deletions(-) --- linux-2.6.17-rootcontext/security/selinux/hooks.c.rootcontext 2006-06-23 16:42:30.000000000 -0400 +++ linux-2.6.17-rootcontext/security/selinux/hooks.c 2006-06-28 17:47:18.000000000 -0400 @@ -246,6 +246,7 @@ static int superblock_alloc_security(str sbsec->sb = sb; sbsec->sid = SECINITSID_UNLABELED; sbsec->def_sid = SECINITSID_FILE; + sbsec->mntpoint_sid = SECINITSID_UNLABELED; sb->s_security = sbsec; return 0; @@ -332,6 +333,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; const char *name; u32 sid; int alloc = 0, rc = 0, seen = 0; @@ -374,7 +376,7 @@ static int try_context_mount(struct supe switch (token) { case Opt_context: - if (seen) { + if (seen & (Opt_context|Opt_defcontext)) { rc = -EINVAL; printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); goto out_free; @@ -390,13 +392,13 @@ static int try_context_mount(struct supe break; case Opt_fscontext: - if (seen & (Opt_context|Opt_fscontext)) { + if (seen & Opt_fscontext) { rc = -EINVAL; printk(KERN_WARNING SEL_MOUNT_FAIL_MSG); goto out_free; } - context = match_strdup(&args[0]); - if (!context) { + fscontext = match_strdup(&args[0]); + if (!fscontext) { rc = -ENOMEM; goto out_free; } @@ -441,6 +443,42 @@ static int try_context_mount(struct supe if (!seen) goto out; + /* + * sets the context of the superblock, sbsec->sid for the fs being mounted + * is done before context= because context= will also set the superblock + * if it is not already set by fscontext= + */ + if (fscontext) { + rc = security_context_to_sid(fscontext, strlen(fscontext), &sid); + if (rc) { + printk(KERN_WARNING "SELinux: security_context_to_sid" + "(%s) failed for (dev %s, type %s) errno=%d\n", + fscontext, sb->s_id, name, rc); + goto out_free; + } + + rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, + FILESYSTEM__RELABELFROM, NULL); + if (rc) + goto out_free; + + rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM, + FILESYSTEM__RELABELTO, NULL); + if (rc) + goto out_free; + + sbsec->sid = sid; + } + + /* + * will set both the superblock sid, sbsec->sid and the sid + * for all file on that fs, sbsec->mntpoint_sid, the superblock + * sid may have already been set by fscontext, in which case + * leave it alone. + * + * later having Opt_context set will switch the fs to using + * SECURITY_FS_USE_MNTPOINT labeling + */ if (context) { rc = security_context_to_sid(context, strlen(context), &sid); if (rc) { @@ -450,7 +488,7 @@ static int try_context_mount(struct supe goto out_free; } - rc = avc_has_perm(tsec->sid, sbsec->sid, SECCLASS_FILESYSTEM, + rc = avc_has_perm(tsec->sid, sbsec->mntpoint_sid, SECCLASS_FILESYSTEM, FILESYSTEM__RELABELFROM, NULL); if (rc) goto out_free; @@ -460,7 +498,9 @@ static int try_context_mount(struct supe if (rc) goto out_free; - sbsec->sid = sid; + if (sbsec->sid == SECINITSID_UNLABELED) + sbsec->sid = sid; + sbsec->mntpoint_sid = sid; if (seen & Opt_context) sbsec->behavior = SECURITY_FS_USE_MNTPOINT; @@ -495,6 +535,7 @@ out_free: if (alloc) { kfree(context); kfree(defcontext); + kfree(fscontext); } out: return rc; @@ -876,8 +917,15 @@ static int inode_doinit_with_dentry(stru goto out; isec->sid = sid; break; + case SECURITY_FS_USE_MNTPOINT: + /* use the mntpoint_sid */ + if (sbsec->mntpoint_sid != SECINITSID_UNLABELED) + isec->sid = sbsec->mntpoint_sid; + else + isec->sid = sbsec->sid; + break; default: - /* Default to the fs SID. */ + /* Default to the fs superblock SID. */ isec->sid = sbsec->sid; if (sbsec->proc) { --- linux-2.6.17-rootcontext/security/selinux/include/objsec.h.rootcontext 2006-06-26 16:21:19.000000000 -0400 +++ linux-2.6.17-rootcontext/security/selinux/include/objsec.h 2006-06-28 17:25:46.000000000 -0400 @@ -56,8 +56,9 @@ struct file_security_struct { struct superblock_security_struct { struct super_block *sb; /* back pointer to sb object */ struct list_head list; /* list of superblock_security_struct */ - u32 sid; /* SID of file system */ + u32 sid; /* SID of file system superblock */ u32 def_sid; /* default SID for labeling */ + u32 mntpoint_sid; /* SECURITY_FS_USE_MNTPOINT context for files */ 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 majordomo@tycho.nsa.gov with the words "unsubscribe selinux" without quotes as the message.