Skip to contentSkip to conversation

SELinux: allow fstype unknown to policy to use xattrs if present

PATCH Superseded Eric Paris 3 participants 5 messages Jun 12, 2008 SELinux security module

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Eric Paris

author
#0 lore raw

PATCH 1/1 SELinux: allow fstype unknown to policy to use xattrs if present

Currently if a fs is mounted for which selinux policy does not define an fs_use_* or a genfscon statement that FS will not support labeling of any kind. This patch allows the kernel to check if the filesystem supports security xattrs and if so will use those if there is no fs_use_* rule in policy. An fstype with a genfs rule will use xattrs if available and will follow the genfs rule if they are not.

This can be particularly interesting for things like ecryptfs which actually overlays a real underlying FS. If we define excryptfs in policy to use xattrs we will likely get this wrong at times, so with this path we just don't need to define it!

Overlay ecryptfs on top of NFS with no xattr support:
SELinux: initialized (dev ecryptfs, type ecryptfs), uses genfs_contexts Overlay ecryptfs on top of ext4 with xattr support:
SELinux: initialized (dev ecryptfs, type ecryptfs), uses xattr

Its also useful as the kernel adds new FS's we don't need to add them in policy if they support xattrs and that's how we want to handle them.

 security/selinux/hooks.c            |   22 +++++++++++++++++-----
 security/selinux/include/security.h |    2 +-
 security/selinux/ss/services.c      |   27 +++++++++++++++++++--------
 3 files changed, 37 insertions(+), 14 deletions(-)
security/selinux/hooks.c +17 -5
index 1c864c0..1844966 100644
@@ -554,13 +554,15 @@ static int selinux_set_mnt_opts(struct super_block *sb,
 	struct task_security_struct *tsec = current->security;
 	struct superblock_security_struct *sbsec = sb->s_security;
 	const char *name = sb->s_type->name;
-	struct inode *inode = sbsec->sb->s_root->d_inode;
-	struct inode_security_struct *root_isec = inode->i_security;
+	struct dentry *root = sb->s_root;
+	struct inode *root_inode = root->d_inode;
+	struct inode_security_struct *root_isec = root_inode->i_security;
 	u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
 	u32 defcontext_sid = 0;
 	char **mount_options = opts->mnt_opts;
 	int *flags = opts->mnt_opts_flags;
 	int num_opts = opts->num_mnt_opts;
+	char can_xattr = 0;
 
 	mutex_lock(&sbsec->lock);
 
@@ -664,14 +666,24 @@ static int selinux_set_mnt_opts(struct super_block *sb,
 		goto out;
 	}
 
-	if (strcmp(sb->s_type->name, "proc") == 0)
+	if (strcmp(name, "proc") == 0)
 		sbsec->proc = 1;
 
+	/*
+	 * test if the fs supports xattrs, fs_use might make use of this if the
+	 * fs has no definition in policy.
+	 */
+	if (root_inode->i_op->getxattr) {
+		rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0);
+		if (rc >= 0 || rc == -ENODATA)
+			can_xattr = 1;
+	}
+
 	/* Determine the labeling behavior to use for this filesystem type. */
-	rc = security_fs_use(sb->s_type->name, &sbsec->behavior, &sbsec->sid);
+	rc = security_fs_use(name, &sbsec->behavior, &sbsec->sid, can_xattr);
 	if (rc) {
 		printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n",
-		       __func__, sb->s_type->name, rc);
+		       __func__, name, rc);
 		goto out;
 	}
 
security/selinux/include/security.h +1 -1
index ad30ac4..eb6da47 100644
@@ -131,7 +131,7 @@ int security_get_allow_unknown(void);
 #define SECURITY_FS_USE_MNTPOINT	6 /* use mountpoint labeling */
 
 int security_fs_use(const char *fstype, unsigned int *behavior,
-	u32 *sid);
+	u32 *sid, char can_xattr);
 
 int security_genfs_sid(const char *fstype, char *name, u16 sclass,
 	u32 *sid);
security/selinux/ss/services.c +19 -8
index dcc2e1c..5af3aab 100644
@@ -1825,7 +1825,8 @@ out:
 int security_fs_use(
 	const char *fstype,
 	unsigned int *behavior,
-	u32 *sid)
+	u32 *sid,
+	char can_xattr)
 {
 	int rc = 0;
 	struct ocontext *c;
@@ -1839,6 +1840,7 @@ int security_fs_use(
 		c = c->next;
 	}
 
+	/* look for labeling behavior defined in policy */
 	if (c) {
 		*behavior = c->v.behavior;
 		if (!c->sid[0]) {
@@ -1849,14 +1851,23 @@ int security_fs_use(
 				goto out;
 		}
 		*sid = c->sid[0];
+		goto out;
+	}
+
+	/* labeling behavior not in policy, use xattrs if possible */
+	if (can_xattr) {
+		*behavior = SECURITY_FS_USE_XATTR;
+		*sid = SECINITSID_FS;
+		goto out;
+	}
+
+	/* no behavior in policy and can't use xattrs, try GENFS */
+	rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
+	if (rc) {
+		*behavior = SECURITY_FS_USE_NONE;
+		rc = 0;
 	} else {
-		rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
-		if (rc) {
-			*behavior = SECURITY_FS_USE_NONE;
-			rc = 0;
-		} else {
-			*behavior = SECURITY_FS_USE_GENFS;
-		}
+		*behavior = SECURITY_FS_USE_GENFS;
 	}
 
 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.

#1 lore raw
Quoted 12 lines · Eric Paris

Currently if a fs is mounted for which selinux policy does not define an fs_use_* or a genfscon statement that FS will not support labeling of any kind. This patch allows the kernel to check if the filesystem supports security xattrs and if so will use those if there is no fs_use_* rule in policy. An fstype with a genfs rule will use xattrs if available and will follow the genfs rule if they are not.

This can be particularly interesting for things like ecryptfs which actually overlays a real underlying FS. If we define excryptfs in policy to use xattrs we will likely get this wrong at times, so with this path we just don't need to define it!

Quoted 7 lines

Overlay ecryptfs on top of NFS with no xattr support:
SELinux: initialized (dev ecryptfs, type ecryptfs), uses genfs_contexts Overlay ecryptfs on top of ext4 with xattr support:
SELinux: initialized (dev ecryptfs, type ecryptfs), uses xattr

Its also useful as the kernel adds new FS's we don't need to add them in policy if they support xattrs and that's how we want to handle them.

<pedantic>

"Its" -> "It is", "that's" => "That is"

Signed-off-by?

Need a "---" here to prevent diffstat from leaking into the changelog ;)

</pedantic>

Quoted 15 lines
 security/selinux/hooks.c            |   22 +++++++++++++++++-----
 security/selinux/include/security.h |    2 +-
 security/selinux/ss/services.c      |   27 +++++++++++++++++++--------
 3 files changed, 37 insertions(+), 14 deletions(-)
security/selinux/hooks.c +0 -1
index 1c864c0..1844966 100644
@@ -554,13 +554,15 @@ static int selinux_set_mnt_opts(struct super_block *sb,
 	struct task_security_struct *tsec = current->security;
 	struct superblock_security_struct *sbsec = sb->s_security;
 	const char *name = sb->s_type->name;
-	struct inode *inode = sbsec->sb->s_root->d_inode;

security/selinux/hooks.c static int selinux_set_mnt_opts(struct super_block *sb, · line 565

Quoted 10 lines
security/selinux/hooks.c +4 -1
-	struct inode_security_struct *root_isec = inode->i_security;
+	struct dentry *root = sb->s_root;
+	struct inode *root_inode = root->d_inode;
+	struct inode_security_struct *root_isec = root_inode->i_security;
 	u32 fscontext_sid = 0, context_sid = 0, rootcontext_sid = 0;
 	u32 defcontext_sid = 0;
 	char **mount_options = opts->mnt_opts;
 	int *flags = opts->mnt_opts_flags;
 	int num_opts = opts->num_mnt_opts;
+	char can_xattr = 0;

bool instead of char since it exists these days.

Quoted 98 lines
security/selinux/hooks.c +13 -3
 	mutex_lock(&sbsec->lock);
 
@@ -664,14 +666,24 @@ static int selinux_set_mnt_opts(struct super_block *sb,
 		goto out;
 	}
 
-	if (strcmp(sb->s_type->name, "proc") == 0)
+	if (strcmp(name, "proc") == 0)
 		sbsec->proc = 1;
 
+	/*
+	 * test if the fs supports xattrs, fs_use might make use of this if the
+	 * fs has no definition in policy.
+	 */
+	if (root_inode->i_op->getxattr) {
+		rc = root_inode->i_op->getxattr(root, XATTR_NAME_SELINUX, NULL, 0);
+		if (rc >= 0 || rc == -ENODATA)
+			can_xattr = 1;
+	}
+
 	/* Determine the labeling behavior to use for this filesystem type. */
-	rc = security_fs_use(sb->s_type->name, &sbsec->behavior, &sbsec->sid);
+	rc = security_fs_use(name, &sbsec->behavior, &sbsec->sid, can_xattr);
 	if (rc) {
 		printk(KERN_WARNING "%s: security_fs_use(%s) returned %d\n",
-		       __func__, sb->s_type->name, rc);
+		       __func__, name, rc);
 		goto out;
 	}
 
security/selinux/include/security.h +1 -1
index ad30ac4..eb6da47 100644
@@ -131,7 +131,7 @@ int security_get_allow_unknown(void);
 #define SECURITY_FS_USE_MNTPOINT	6 /* use mountpoint labeling */
 
 int security_fs_use(const char *fstype, unsigned int *behavior,
-	u32 *sid);
+	u32 *sid, char can_xattr);
 
 int security_genfs_sid(const char *fstype, char *name, u16 sclass,
 	u32 *sid);
security/selinux/ss/services.c +19 -8
index dcc2e1c..5af3aab 100644
@@ -1825,7 +1825,8 @@ out:
 int security_fs_use(
 	const char *fstype,
 	unsigned int *behavior,
-	u32 *sid)
+	u32 *sid,
+	char can_xattr)
 {
 	int rc = 0;
 	struct ocontext *c;
@@ -1839,6 +1840,7 @@ int security_fs_use(
 		c = c->next;
 	}
 
+	/* look for labeling behavior defined in policy */
 	if (c) {
 		*behavior = c->v.behavior;
 		if (!c->sid[0]) {
@@ -1849,14 +1851,23 @@ int security_fs_use(
 				goto out;
 		}
 		*sid = c->sid[0];
+		goto out;
+	}
+
+	/* labeling behavior not in policy, use xattrs if possible */
+	if (can_xattr) {
+		*behavior = SECURITY_FS_USE_XATTR;
+		*sid = SECINITSID_FS;
+		goto out;
+	}
+
+	/* no behavior in policy and can't use xattrs, try GENFS */
+	rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
+	if (rc) {
+		*behavior = SECURITY_FS_USE_NONE;
+		rc = 0;
 	} else {
-		rc = security_genfs_sid(fstype, "/", SECCLASS_DIR, sid);
-		if (rc) {
-			*behavior = SECURITY_FS_USE_NONE;
-			rc = 0;
-		} else {
-			*behavior = SECURITY_FS_USE_GENFS;
-		}
+		*behavior = SECURITY_FS_USE_GENFS;
 	}
 
 out:
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.
#2 lore raw
In reply to Eric Paris
Quoted 6 lines · Eric Paris

Currently if a fs is mounted for which selinux policy does not define an fs_use_* or a genfscon statement that FS will not support labeling of any kind. This patch allows the kernel to check if the filesystem supports security xattrs and if so will use those if there is no fs_use_* rule in policy. An fstype with a genfs rule will use xattrs if available and will follow the genfs rule if they are not.

Let met verify what I think I read in the patch. The filesystem labeling behavior (filesystem class) is unchanged, right? i.e.

1. no fs_use + genfs + security xattr on fs = genfs type for fs
2. no fs_use + no genfs + security xattr on fs = unlabeled isid for fs

Signature · 10 lines
Chris PeBenito
Tresys Technology, LLC
(410) 290-1411 x150



--
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 8 lines · Christopher J. PeBenito
Quoted 6 lines · Eric Paris

Currently if a fs is mounted for which selinux policy does not define an fs_use_* or a genfscon statement that FS will not support labeling of any kind. This patch allows the kernel to check if the filesystem supports security xattrs and if so will use those if there is no fs_use_* rule in policy. An fstype with a genfs rule will use xattrs if available and will follow the genfs rule if they are not.

Quoted 5 lines

Let met verify what I think I read in the patch. The filesystem labeling behavior (filesystem class) is unchanged, right? i.e.

1. no fs_use + genfs + security xattr on fs = genfs type for fs
2. no fs_use + no genfs + security xattr on fs = unlabeled isid for fs

No. If there is a fs_use statement for the fs type, then that gets used regardless. If there is no fs_use type and the fs supports selinux xattrs, then use them. If there is no fs_use type and the fs does not support selinux xattrs, use genfs or fall back to none/unlabeled if none.

Thus, with ecryptfs, we can put a genfs statement in the policy as the fallback behavior, yet use xattrs when the lower filesystem supports them.

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
#4 lore raw
In reply to Christopher J. PeBenito
Quoted 8 lines · Christopher J. PeBenito
Quoted 6 lines · Eric Paris

Currently if a fs is mounted for which selinux policy does not define an fs_use_* or a genfscon statement that FS will not support labeling of any kind. This patch allows the kernel to check if the filesystem supports security xattrs and if so will use those if there is no fs_use_* rule in policy. An fstype with a genfs rule will use xattrs if available and will follow the genfs rule if they are not.

Quoted 4 lines

Let met verify what I think I read in the patch. The filesystem labeling behavior (filesystem class) is unchanged, right? i.e.

1. no fs_use + genfs + security xattr on fs = genfs type for fs

no. we set fs_sid = SECINITSID_FS and we use the labels in the xattr

Quoted 1 line

2. no fs_use + no genfs + security xattr on fs = unlabeled isid for fs

fs_sid = SECINITSID_FS and we use the labeles in the xattrs

1st priority: fs_use_{xattr,transition,etc} wins 2nd priority: fs has xattr support wins (and we use xattrs) 3rd priority: genfscon statement wins

If there is a case where this is wrong I suggest we add a
fs_use_genfscon rule... But I don't think such a case exists today.
genfscon has always been a fallback, and now it just falls further back.

-Eric

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