Skip to contentSkip to conversation

RFC: LSM/SELinux handling of mount options

PATCH Eric Paris 2 participants 2 messages Jul 12, 2007 – Jul 13, 2007

Subsystems (5)
lore
Share & tools
lore

Eric Paris

author
#0 lore raw

Do not commit to any tree! This patch is busted and is only being posted for comments on design! I'll explain the problem below but I don't see it in the code right offhand and if the whole design is going to get crapped on I don't feel like digging into it. If the design looks reasonable to people it won't take me long to figure out what I did wrong.

So there are 2 overarching problems this patch set it attempting to solve or make later solutions easier.

1) NFS uses binary mount data (for both normal and nohide/referral mounts) which currently SELinux attempts to understand and use. This was declared a layering issue, no security module should have FS specific data structure knowledge. (Instead we have to put security module specific knowledge into the FS since the FS 'owns' the mount data, see below)

2) File systems own all of their mount options. SELinux currently scans through the text mount data of most filesystems and pulls out the key options it understands, in the case of SELinux those are context=, fscontext=, rootcontext=, defcontext=. On these 2 lists it was stated that the FS owns the options and mount data. I don't try to remove all handling of text mount data from SELinux but I am adding the infrastructure for FS code to pass that data explicitly. Future work may eliminate the fact that security modules directly read and use text mount data for their own purposes.

Someday this will be a 3 part patch.
1) add get/set_sb_sec_ops so an FS can get or set their own security attributes.
2) add set_sec_ops to struct file_system_type to call out to FS specific security module settings
3) make nfs use the get/set_sb_sec_ops code when doing nohide/referral mounts.

LONG explanation below.

How does this patch work? For submitting I'm going to break it up probably into 3 parts. The first part will add new LSM hooks and new functionality where we can get or set the security information relevant to a superblock (see selinux_set_mnt_opts).

static int selinux_set_mnt_opts(const struct super_block *sb,
char **mount_options, int *flags, int num_opts)

mount_options is an array of strings which contain the actual security data. In the case of selinux this will be an array of selinux context strings. Flags is an array which tells which string is trying to set which security relevant attribute. These flags are global flags in security.h of the form

#define SELINUX__FSCONTEXT_MNT_OPT 1

My convention is the first part will specify the security module, and the second part will specify what it's for in that module. Above I said that SELinux had 4 mount options it cared about so there are currently 4 of these flags defined. That example says this is for SELinux and we are setting the FSCONTEXT SELinux mount options.

The last argument is num_opts which is simply how many security parameters are being passed to this function. Assume num_opts = 1. We look at flags[0] (say it equals 1) to find out what we are supposed to set (fscontext), and then set it to the string at mount_options[0] (= some context like system_u:object_r:type_t). The get functions do the opposite. They return all of these arrays based on the information in the given sb. Now we have the infrastructure for a FS to set their own security settings assuming they know what the security module below them is. (Big if and I'm not really solving that problem right now... ifdefs?)

The second part of the patch will be the change to add

struct file_system_type {
	[....]
        int (*set_sec_ops) (struct vfsmount *mnt, void *mnt_ops);

Since now the FS owns the mount data the security layer needs a way to tell the FS, "hey FS, now is the time for you to set the security information on this vfsmount, and here is the mount data you sent our way but we don't understand." The only user I see for this at the moment is NFS. But others may use this if they don't continue to just let SELinux grep the security information from the text mount data. NFS will set the NFS specific

	.set_sec_ops = nfs_set_sec_ops;

Inside vfs_kern_mount we can call back out and get NFS to set its security settings (using the new LSM hooks above). Obviously this is the point where the FS now has to have security module specific information. If NFS is going to own the mount options and the mount options are security module specific some level of security module knowledge must exist. This is easy on NFS, it only understands context= and only has a ->context field in the nfs_mount_data so this can be hard coded. Right now I think this function will bust without SELinux below it, but that's an easy fix to just handle the return from a call to security_sb_set_mnt_opts if it is a no-op.

This set_sec_ops pointer would be null on all FS's which didn't need this call back and wouldn't do anything.

At this point we should have the same functionality we have today, but should have gone a lot of the way towards solving problem 2. Other FS's which want to own their mount data completely could switch to start using this infrastructure.

part 3 and now the real reason I started to look at this, NFS referral and nohide mounts which use nfs_clone_mount instead of nfs_mount_data. The fix for this is to actually change nfs_do_clone_mount() to get the security relevant information on the parent superblock, mount the new 'child,' and then set that security information onto the child. All of this security getting/setting data is actually opaque to NFS and it doesn't matter what the underlying security module is (or if there even is one once i handle return codes better)

I said there was a problem right? The root inode is not getting the label specified in the mount options. I'm not sure why but in the case of NFS I always get nfs_t on the root inode instead of the label I explicitly set for the rest of the mount point. I thought that would be fixed by setting rootcontext in my set_mnt_opts() function but it isn't working and I don't see the issue offhand. Obviously this will be fixed before I considered posting for people to look at again. You can actually run and test with this code, just note that your root inode is going to be labeled wrong....

Anyway, here is the single patch. I'd like to hear comments on my design (and my code but don't get picky, its got a long way to go, see all those printk's and dump_stacks? those are bad, don't tell me)

-Eric

 fs/nfs/client.c          |   18 +++
 fs/nfs/namespace.c       |   27 +++-
 fs/nfs/super.c           |   44 ++++++
 fs/super.c               |    5 +
 include/linux/fs.h       |    1 +
 include/linux/security.h |   54 +++++++-
 security/dummy.c         |   19 +++
 security/selinux/hooks.c |  379 +++++++++++++++++++++++++++++++---------------
 8 files changed, 422 insertions(+), 125 deletions(-)
fs/nfs/client.c +18 -0
index 881fa49..1d124ad 100644
@@ -532,6 +532,13 @@ static int nfs_init_client(struct nfs_client *clp, const struct nfs_mount_data *
 	int proto = (data->flags & NFS_MOUNT_TCP) ? IPPROTO_TCP : IPPROTO_UDP;
 	int error;
 
+	printk(KERN_CRIT "%s: called.  mount data is: ", __FUNCTION__);
+	if (data && data->context)
+		printk("%s\n", data->context);
+	else
+		printk("NULL\n");
+	dump_stack();
+
 	if (clp->cl_cons_state == NFS_CS_READY) {
 		/* the client is already initialised */
 		dprintk("<-- nfs_init_client() = 0 [already %p]\n", clp);
@@ -569,6 +576,11 @@ static int nfs_init_server(struct nfs_server *server, const struct nfs_mount_dat
 	struct nfs_client *clp;
 	int error, nfsvers = 2;
 
+        printk(KERN_CRIT "%s: called.  mount data is: ", __FUNCTION__);
+        if (data && data->context)
+                printk("%s\n", data->context);
+        else
+                printk("NULL\n");
 	dprintk("--> nfs_init_server()\n");
 
 #ifdef CONFIG_NFS_V3
@@ -803,6 +815,12 @@ struct nfs_server *nfs_create_server(const struct nfs_mount_data *data,
 	struct nfs_fattr fattr;
 	int error;
 
+        printk(KERN_CRIT "%s: called.  mount data is: ", __FUNCTION__);
+        if (data && data->context)
+                printk(KERN_CRIT "%s\n", data->context);
+        else
+                printk(KERN_CRIT "NULL\n");
+
 	server = nfs_alloc_server();
 	if (!server)
 		return ERR_PTR(-ENOMEM);
fs/nfs/namespace.c +24 -3
index 7f86e65..6436be0 100644
@@ -11,6 +11,7 @@
 #include <linux/mount.h>
 #include <linux/namei.h>
 #include <linux/nfs_fs.h>
+#include <linux/security.h>
 #include <linux/string.h>
 #include <linux/sunrpc/clnt.h>
 #include <linux/vfs.h>
@@ -188,8 +189,18 @@ static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
 					   const char *devname,
 					   struct nfs_clone_mount *mountdata)
 {
-#ifdef CONFIG_NFS_V4
+	int rc = 0, num_sec_mnt_opts = 0, i;
+	char **sec_mnt_opts;
+	int *sec_mnt_flags;
 	struct vfsmount *mnt = NULL;
+
+	sec_mnt_opts = NULL;
+	sec_mnt_flags = NULL;
+	rc = security_sb_get_mnt_opts(mountdata->sb, &sec_mnt_opts,
+				      &sec_mnt_flags, &num_sec_mnt_opts);
+	if (rc)
+		return ERR_PTR(rc);
+#ifdef CONFIG_NFS_V4
 	switch (server->nfs_client->cl_nfsversion) {
 		case 2:
 		case 3:
@@ -198,10 +209,20 @@ static struct vfsmount *nfs_do_clone_mount(struct nfs_server *server,
 		case 4:
 			mnt = vfs_kern_mount(&nfs4_xdev_fs_type, 0, devname, mountdata);
 	}
-	return mnt;
 #else
-	return vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
+	mnt = vfs_kern_mount(&nfs_xdev_fs_type, 0, devname, mountdata);
 #endif
+	if (mnt) {
+		security_sb_set_mnt_opts(mnt->mnt_sb, sec_mnt_opts,
+					 sec_mnt_flags, num_sec_mnt_opts);
+	}
+	if (sec_mnt_opts) {
+		for (i = 0; i < num_sec_mnt_opts; i++)
+			kfree(sec_mnt_opts[i]);
+		kfree(sec_mnt_opts);
+		kfree(sec_mnt_flags);
+	}
+	return mnt;
 }
 
 /**
fs/nfs/super.c +44 -0

86 lines not on this page · raw message

fs/super.c +5 -0
index 5260d62..b555d61 100644
@@ -889,6 +889,11 @@ vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void
  	if (error)
  		goto out_sb;
 
+	if (type->set_sec_ops && data) {
+		error = type->set_sec_ops(mnt, data);
+		if (error)
+			goto out_sb;
+	}
 	mnt->mnt_mountpoint = mnt->mnt_root;
 	mnt->mnt_parent = mnt;
 	up_write(&mnt->mnt_sb->s_umount);
include/linux/fs.h +1 -0
index b3ae77c..ed6e0f2 100644
@@ -1371,6 +1371,7 @@ struct file_system_type {
 	struct list_head fs_supers;
 	struct lock_class_key s_lock_key;
 	struct lock_class_key s_umount_key;
+	int (*set_sec_ops) (struct vfsmount *mnt, void *mnt_ops);
 };
 
 extern int get_sb_bdev(struct file_system_type *fs_type,
include/linux/security.h +53 -1

84 lines not on this page · raw message

security/dummy.c +19 -0
index 8ffd764..f4644ff 100644
@@ -248,6 +248,23 @@ static void dummy_sb_post_pivotroot (struct nameidata *old_nd, struct nameidata
 	return;
 }
 
+static int dummy_sb_get_mnt_opts(const struct super_block *sb, char ***mount_options,
+				 int **flags, int *num_opts)
+{
+	*mount_options = NULL;
+	*flags = NULL;
+	*num_opts = 0;
+	return 0;
+}
+
+static int dummy_sb_set_mnt_opts(struct super_block *sb, char **mount_options,
+				 int *flags, int num_opts)
+{
+	if (unlikely(num_opts))
+		return -EOPNOTSUPP;
+	return 0;
+}
+
 static int dummy_inode_alloc_security (struct inode *inode)
 {
 	return 0;
@@ -992,6 +1009,8 @@ void security_fixup_ops (struct security_operations *ops)
 	set_to_dummy_if_null(ops, sb_post_addmount);
 	set_to_dummy_if_null(ops, sb_pivotroot);
 	set_to_dummy_if_null(ops, sb_post_pivotroot);
+	set_to_dummy_if_null(ops, sb_get_mnt_opts);
+	set_to_dummy_if_null(ops, sb_set_mnt_opts);
 	set_to_dummy_if_null(ops, inode_alloc_security);
 	set_to_dummy_if_null(ops, inode_free_security);
 	set_to_dummy_if_null(ops, inode_init_security);
security/selinux/hooks.c +258 -121

472 lines not on this page · raw message

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

Hi Eric -

Quoted 9 lines · Eric Paris

So there are 2 overarching problems this patch set it attempting to solve or make later solutions easier.

1) NFS uses binary mount data (for both normal and nohide/referral mounts) which currently SELinux attempts to understand and use. This was declared a layering issue, no security module should have FS specific data structure knowledge. (Instead we have to put security module specific knowledge into the FS since the FS 'owns' the mount data, see below)

Please take a look at the string-ified NFS mount patches that are going into 2.6.23. In the future we want to pass mount options for NFS mounts via a C string instead of a binary blob. If nothing else, it will affect your changes to fs/nfs/super.c.

begin:vcard
fn:Chuck Lever
n:Lever;Chuck
org:Oracle Corporation;Corporate Architecture: Linux Projects Group adr:;;1015 Granger Avenue;Ann Arbor;MI;48104;USA email;internet:chuck dot lever at nospam oracle dot com title:Principal Member of Staff
tel;work:+1 248 614 5091
x-mozilla-html:FALSE
version:2.1
end:vcard

-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/

_______________________________________________
NFS maillist - [email protected]
https://lists.sourceforge.net/lists/listinfo/nfs