0c223bc
0c223bc
 This patch adds to the mount man page docs about context, fscontext and
0c223bc
defcontext mount options and translate context options from human to raw
0c223bc
selinux context format.  -- 03/30/2006 Karel Zak <kzak@redhat.com> 
0c223bc
0c223bc
--- util-linux-2.13-pre7/mount/mount.8.cxt	2006-03-30 17:15:06.000000000 +0200
0c223bc
+++ util-linux-2.13-pre7/mount/mount.8	2006-03-30 17:15:06.000000000 +0200
0c223bc
@@ -661,6 +661,50 @@
0c223bc
 .BR noexec ", " nosuid ", and " nodev
0c223bc
 (unless overridden by subsequent options, as in the option line
0c223bc
 .BR users,exec,dev,suid ).
0c223bc
+.TP
0c223bc
+\fBcontext=\fP\fIcontext\fP, \fBfscontext=\fP\fIcontext\fP and \fBdefcontext=\fP\fIcontext\fP
0c223bc
+The 
0c223bc
+.BR context= 
0c223bc
+option is useful when mounting filesystems that do not support
0c223bc
+extended attributes, such as a floppy or hard disk formatted with VFAT, or
0c223bc
+systems that are not normally running under SELinux, such as an ext3 formatted
0c223bc
+disk from a non-SELinux workstation. You can also use
0c223bc
+.BR context= 
0c223bc
+on filesystems you do not trust, such as a floppy. It also helps in compatibility with
0c223bc
+xattr-supporting filesystems on earlier 2.4.<x> kernel versions. Even where
0c223bc
+xattrs are supported, you can save time not having to label every file by
0c223bc
+assigning the entire disk one security context.
0c223bc
+
0c223bc
+A commonly used option for removable media is 
0c223bc
+.BR context=system_u:object_r:removable_t .
0c223bc
+
0c223bc
+Two other options are 
0c223bc
+.BR fscontext= 
0c223bc
+and 
0c223bc
+.BR defcontext= ,
0c223bc
+both of which are mutually exclusive of the context option. This means you
0c223bc
+can use fscontext and defcontext with each other, but neither can be used with
0c223bc
+context.
0c223bc
+
0c223bc
+The 
0c223bc
+.BR fscontext= 
0c223bc
+option works for all filesystems, regardless of their xattr
0c223bc
+support. The fscontext option sets the overarching filesystem label to a
0c223bc
+specific security context. This filesystem label is separate from the
0c223bc
+individual labels on the files. It represents the entire filesystem for
0c223bc
+certain kinds of permission checks, such as during mount or file creation.
0c223bc
+Individual file labels are still obtained from the xattrs on the files
0c223bc
+themselves. The context option actually sets the aggregate context that
0c223bc
+fscontext provides, in addition to supplying the same label for individual
0c223bc
+files.
0c223bc
+
0c223bc
+You can set the default security context for unlabeled files using 
0c223bc
+.BR defcontext=
0c223bc
+option. This overrides the value set for unlabeled files in the policy and requires a
0c223bc
+file system that supports xattr labeling. 
0c223bc
+
0c223bc
+For more details see 
0c223bc
+.BR selinux (8)
0c223bc
 .RE
0c223bc
 .TP
0c223bc
 .B \-\-bind
0c223bc
--- util-linux-2.13-pre7/mount/mount.c.cxt	2006-03-30 17:15:06.000000000 +0200
0c223bc
+++ util-linux-2.13-pre7/mount/mount.c	2006-03-30 20:16:57.000000000 +0200
0c223bc
@@ -21,6 +21,11 @@
0c223bc
 #include <sys/wait.h>
0c223bc
 #include <sys/mount.h>
0c223bc
 
0c223bc
+#ifdef HAVE_LIBSELINUX
0c223bc
+#include <selinux/selinux.h>
0c223bc
+#include <selinux/context.h>
0c223bc
+#endif
0c223bc
+
0c223bc
 #include "mount_blkid.h"
0c223bc
 #include "mount_constants.h"
0c223bc
 #include "sundries.h"
0c223bc
@@ -255,6 +260,49 @@
0c223bc
 		free((void *) s);
0c223bc
 }
0c223bc
 
0c223bc
+#ifdef HAVE_LIBSELINUX
0c223bc
+/* translates SELinux context from human to raw format and 
0c223bc
+ * appends it to the mount extra options.
0c223bc
+ *
0c223bc
+ * returns -1 on error and 0 on success 
0c223bc
+ */
0c223bc
+static int
0c223bc
+append_context(const char *optname, const char *optdata, char *extra_opts, int *len)
0c223bc
+{
0c223bc
+	security_context_t raw = NULL;
0c223bc
+	char *buf = NULL;
0c223bc
+	int bufsz;
0c223bc
+	
0c223bc
+	if (!is_selinux_enabled())
0c223bc
+		/* ignore the option if we running without selinux */
0c223bc
+		return 0;
0c223bc
+
0c223bc
+	if (optdata==NULL || *optdata=='\0' || optname==NULL)
0c223bc
+		return -1;
0c223bc
+	
0c223bc
+	if (selinux_trans_to_raw_context(
0c223bc
+			(security_context_t) optdata, &raw)==-1 ||
0c223bc
+			raw==NULL)
0c223bc
+		return -1;
0c223bc
+	
0c223bc
+	if (verbose)
0c223bc
+		printf(_("mount: translated %s '%s' to '%s'\n"), 
0c223bc
+				optname, optdata, (char *) raw);
0c223bc
+
0c223bc
+	bufsz = strlen(optname) + strlen(raw) + 2;	/* 2 is \0 and '=' */ 
0c223bc
+	buf = xmalloc(bufsz);
0c223bc
+
0c223bc
+	snprintf(buf, bufsz, "%s=%s", optname, (char *) raw);
0c223bc
+	freecon(raw);
0c223bc
+	
0c223bc
+	if ((*len -= bufsz-1) > 0)
0c223bc
+		strcat(extra_opts, buf);
0c223bc
+	
0c223bc
+	my_free(buf);
0c223bc
+	return 0;
0c223bc
+}
0c223bc
+#endif
0c223bc
+
0c223bc
 /*
0c223bc
  * Look for OPT in opt_map table and return mask value.
0c223bc
  * If OPT isn't found, tack it onto extra_opts (which is non-NULL).
0c223bc
@@ -313,7 +361,20 @@
0c223bc
 			return;
0c223bc
 		}
0c223bc
 	}
0c223bc
-
0c223bc
+#ifdef HAVE_LIBSELINUX
0c223bc
+	if (strncmp(opt, "context=", 8)==0 && *(opt+8)) {
0c223bc
+		if (append_context("context", opt+8, extra_opts, &len)==0)
0c223bc
+			return;
0c223bc
+	}
0c223bc
+	if (strncmp(opt, "fscontext=", 10)==0 && *(opt+10)) {
0c223bc
+		if (append_context("fscontext", opt+10, extra_opts, &len)==0)
0c223bc
+			return;
0c223bc
+	}
0c223bc
+	if (strncmp(opt, "defcontext=", 11)==0 && *(opt+11)) {
0c223bc
+		if (append_context("defcontext", opt+11, extra_opts, &len)==0)
0c223bc
+			return;
0c223bc
+	}
0c223bc
+#endif
0c223bc
 	if ((len -= strlen(opt)) > 0)
0c223bc
 		strcat(extra_opts, opt);
0c223bc
 }
0c223bc
@@ -330,7 +391,7 @@
0c223bc
 	if (options != NULL) {
0c223bc
 		char *opts = xstrdup(options);
0c223bc
 		char *opt;
0c223bc
-		int len = strlen(opts) + 20;
0c223bc
+		int len = strlen(opts) + 256;
0c223bc
 
0c223bc
 		*extra_opts = xmalloc(len); 
0c223bc
 		**extra_opts = '\0';