Skip to contentSkip to conversation

1/2 decouple fscontext/context

RFC PATCH In review Eric Paris 2 participants 4 messages Jun 28, 2006 – Jun 30, 2006 SELinux security module

lore
Share & tools
lore

Eric Paris

author
#0 lore raw

RFC PATCH 1/1 1/2 decouple fscontext/context

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(-)
security/selinux/hooks.c renamed from security/selinux/hooks.c.rootcontext +55 -7
@@ -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) {
security/selinux/include/objsec.h renamed from security/selinux/include/objsec.h.rootcontext +2 -1
@@ -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 [email protected] with the words "unsubscribe selinux" without quotes as the message.

#1 lore raw
Quoted 10 lines · Eric Paris

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(-)

diffstat -p1

security/selinux/hooks.c static int try_context_mount(struct supe · line 449

Quoted 10 lines
security/selinux/hooks.c renamed from security/selinux/hooks.c.rootcontext +4 -0
@@ -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=

Needs a bit of a rewrite. Watch for trailing whitespace. Describe what the code does, not how it does it, as per CodingStyle.

Quoted 9 lines
security/selinux/hooks.c renamed from security/selinux/hooks.c.rootcontext +9 -0
+	 */
+	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;
+		}

security/selinux/hooks.c static int try_context_mount(struct supe · line 468

Quoted 10 lines
security/selinux/hooks.c renamed from security/selinux/hooks.c.rootcontext +10 -0
+
+		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;

I'd split out the permission checks and do them once, or move them into a helper function and call it twice.

security/selinux/hooks.c static int try_context_mount(struct supe · line 480

Quoted 10 lines
security/selinux/hooks.c renamed from security/selinux/hooks.c.rootcontext +10 -0
+	}
+
+	/* 
+	 * 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

Likewise, needs a bit of a rewrite.

security/selinux/hooks.c static int try_context_mount(struct supe · line 492

Quoted 7 lines
security/selinux/hooks.c renamed from security/selinux/hooks.c.rootcontext +1 -1
@@ -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);

This will always end up checking against unlabeled_t, right? I don't think it is what you want, and it changes the existing behavior for people using context= mounts.

security/selinux/hooks.c static int try_context_mount(struct supe · line 503

Quoted 10 lines
security/selinux/hooks.c renamed from security/selinux/hooks.c.rootcontext +3 -1
 		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;

sbsec->sid is initialized by the security_fs_use() call (based on the policy configuration for the fs, either fs_use* or genfs) prior to try_context_mount(), so testing for unlabeled isn't sufficient, right? I think you want to explicitly check whether fscontext was set here.

security/selinux/hooks.c static int try_context_mount(struct supe · line 506

Quoted 2 lines
security/selinux/hooks.c renamed from security/selinux/hooks.c.rootcontext +0 -0
 		if (seen & Opt_context)
 			sbsec->behavior = SECURITY_FS_USE_MNTPOINT;

No longer needs to be conditional here, right? Since this branch is now specific to the context= option?

security/selinux/hooks.c static int inode_doinit_with_dentry(stru · line 926

Quoted 7 lines
security/selinux/hooks.c renamed from security/selinux/hooks.c.rootcontext +7 -0
+	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;

Why wouldn't this always be using mntpoint_sid?

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.
author
#2 lore raw
Quoted 8 lines · Stephen Smalley
Quoted 6 lines · Eric Paris

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.

Quoted 5 lines
Quoted 3 lines
 hooks.c          |   62 ++++++++++++++++++++++++++++++++++++++++++++++++-------
 include/objsec.h |    3 +-
 2 files changed, 57 insertions(+), 8 deletions(-)

diffstat -p1

Will do

Quoted 7 lines
Quoted 7 lines
security/selinux/hooks.c renamed from security/selinux/hooks.c.rootcontext +0 -0
@@ -441,6 +443,42 @@ static int try_context_mount(struct supe
 	if (!seen)
 		goto out;
 
Quoted 7 lines

security/selinux/hooks.c static int try_context_mount(struct supe · line 449

Quoted 4 lines
security/selinux/hooks.c renamed from security/selinux/hooks.c.rootcontext +4 -0
+	/* 
+	 * 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=

Needs a bit of a rewrite. Watch for trailing whitespace. Describe what the code does, not how it does it, as per CodingStyle.

Coding Style re-read and paid particular attention to comments section.

Quoted 15 lines
Quoted 15 lines
security/selinux/hooks.c renamed from security/selinux/hooks.c.rootcontext +14 -0
+	 */
+	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;
Quoted 8 lines

security/selinux/hooks.c static int try_context_mount(struct supe · line 468

Quoted 5 lines
security/selinux/hooks.c renamed from security/selinux/hooks.c.rootcontext +5 -0
+
+		rc = avc_has_perm(tsec->sid, sid, SECCLASS_FILESYSTEM,
+		                  FILESYSTEM__RELABELTO, NULL);
+		if (rc)
+			goto out_free;

I'd split out the permission checks and do them once, or move them into a helper function and call it twice.

suggestion for a meaningful name? context_mount_set_perm()?

[snip]

Quoted 11 lines

security/selinux/hooks.c static int try_context_mount(struct supe · line 492

Quoted 7 lines
security/selinux/hooks.c renamed from security/selinux/hooks.c.rootcontext +1 -1
@@ -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);

This will always end up checking against unlabeled_t, right? I don't think it is what you want, and it changes the existing behavior for people using context= mounts.

So you are saying that both should check against the superblock. The question is if this second check should be the 'new' superblock context (since may have been just set by fscontext=) or should I be checking the 'old' sb sid here?

Quoted 7 lines
Quoted 7 lines
security/selinux/hooks.c renamed from security/selinux/hooks.c.rootcontext +0 -0
 		if (rc)
 			goto out_free;
@@ -460,7 +498,9 @@ static int try_context_mount(struct supe
 		if (rc)
 			goto out_free;
 
Quoted 9 lines

security/selinux/hooks.c static int try_context_mount(struct supe · line 503

Quoted 4 lines
security/selinux/hooks.c renamed from security/selinux/hooks.c.rootcontext +3 -1
-		sbsec->sid = sid;
+		if (sbsec->sid == SECINITSID_UNLABELED)
+			sbsec->sid = sid;
+		sbsec->mntpoint_sid = sid;

sbsec->sid is initialized by the security_fs_use() call (based on the policy configuration for the fs, either fs_use* or genfs) prior to try_context_mount(), so testing for unlabeled isn't sufficient, right? I think you want to explicitly check whether fscontext was set here.

now, if (!fscontext)

Quoted 6 lines

security/selinux/hooks.c static int try_context_mount(struct supe · line 506

Quoted 3 lines
security/selinux/hooks.c renamed from security/selinux/hooks.c.rootcontext +0 -0
 		if (seen & Opt_context)
 			sbsec->behavior = SECURITY_FS_USE_MNTPOINT;

No longer needs to be conditional here, right? Since this branch is now specific to the context= option?

Yup

Quoted 10 lines

security/selinux/hooks.c static int inode_doinit_with_dentry(stru · line 926

Quoted 8 lines
security/selinux/hooks.c renamed from security/selinux/hooks.c.rootcontext +7 -0
+	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;

Why wouldn't this always be using mntpoint_sid?

Originally when I put this in I didn't know if there was any way to get mount point labeling behavior without a context= option. During testing I looked around and realized there was no other way (that I saw) but never pulled this back out.

--
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.

#3 lore raw
Quoted 1 line · Eric Paris

suggestion for a meaningful name? context_mount_set_perm()?

may_context_mount()?

Quoted 4 lines

So you are saying that both should check against the superblock. The question is if this second check should be the 'new' superblock context (since may have been just set by fscontext=) or should I be checking the 'old' sb sid here?

Rechecking the old one would be redundant with the prior check, so checking the updated one seems more useful.

Quoted 4 lines

Originally when I put this in I didn't know if there was any way to get mount point labeling behavior without a context= option. During testing I looked around and realized there was no other way (that I saw) but never pulled this back out.

Right, so you can drop the conditional. Makes it easier to understand as well.

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.