kfan / rpms / util-linux

Forked from rpms/util-linux 3 years ago
Clone
0c223bc
0c223bc
 This patch adds to the mount man page docs about context, fscontext and
bc3ff95
 defcontext mount options and translate context options from human to raw
bc3ff95
 selinux context format.  -- 03/30/2006 Karel Zak <kzak@redhat.com>
0c223bc
bc3ff95
--- util-linux-2.13-pre6/mount/mount.c.kzak	2006-08-21 11:51:50.000000000 +0200
bc3ff95
+++ util-linux-2.13-pre6/mount/mount.c	2006-08-21 11:51:50.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';
bc3ff95
--- util-linux-2.13-pre6/mount/mount.8.kzak	2006-08-21 11:51:50.000000000 +0200
bc3ff95
+++ util-linux-2.13-pre6/mount/mount.8	2006-08-21 11:51:50.000000000 +0200
bc3ff95
@@ -660,6 +660,50 @@
bc3ff95
 .BR noexec ", " nosuid ", and " nodev
bc3ff95
 (unless overridden by subsequent options, as in the option line
bc3ff95
 .BR users,exec,dev,suid ).
bc3ff95
+.TP
bc3ff95
+\fBcontext=\fP\fIcontext\fP, \fBfscontext=\fP\fIcontext\fP and \fBdefcontext=\fP\fIcontext\fP
bc3ff95
+The 
bc3ff95
+.BR context= 
bc3ff95
+option is useful when mounting filesystems that do not support
bc3ff95
+extended attributes, such as a floppy or hard disk formatted with VFAT, or
bc3ff95
+systems that are not normally running under SELinux, such as an ext3 formatted
bc3ff95
+disk from a non-SELinux workstation. You can also use
bc3ff95
+.BR context= 
bc3ff95
+on filesystems you do not trust, such as a floppy. It also helps in compatibility with
bc3ff95
+xattr-supporting filesystems on earlier 2.4.<x> kernel versions. Even where
bc3ff95
+xattrs are supported, you can save time not having to label every file by
bc3ff95
+assigning the entire disk one security context.
bc3ff95
+
bc3ff95
+A commonly used option for removable media is 
bc3ff95
+.BR context=system_u:object_r:removable_t .
bc3ff95
+
bc3ff95
+Two other options are 
bc3ff95
+.BR fscontext= 
bc3ff95
+and 
bc3ff95
+.BR defcontext= ,
bc3ff95
+both of which are mutually exclusive of the context option. This means you
bc3ff95
+can use fscontext and defcontext with each other, but neither can be used with
bc3ff95
+context.
bc3ff95
+
bc3ff95
+The 
bc3ff95
+.BR fscontext= 
bc3ff95
+option works for all filesystems, regardless of their xattr
bc3ff95
+support. The fscontext option sets the overarching filesystem label to a
bc3ff95
+specific security context. This filesystem label is separate from the
bc3ff95
+individual labels on the files. It represents the entire filesystem for
bc3ff95
+certain kinds of permission checks, such as during mount or file creation.
bc3ff95
+Individual file labels are still obtained from the xattrs on the files
bc3ff95
+themselves. The context option actually sets the aggregate context that
bc3ff95
+fscontext provides, in addition to supplying the same label for individual
bc3ff95
+files.
bc3ff95
+
bc3ff95
+You can set the default security context for unlabeled files using 
bc3ff95
+.BR defcontext=
bc3ff95
+option. This overrides the value set for unlabeled files in the policy and requires a
bc3ff95
+file system that supports xattr labeling. 
bc3ff95
+
bc3ff95
+For more details see 
bc3ff95
+.BR selinux (8)
bc3ff95
 .RE
bc3ff95
 .TP
bc3ff95
 .B \-\-bind
bc3ff95
--- util-linux-2.13-pre6/mount/Makefile.am.kzak	2006-08-21 12:13:10.000000000 +0200
bc3ff95
+++ util-linux-2.13-pre6/mount/Makefile.am	2006-08-21 12:13:03.000000000 +0200
bc3ff95
@@ -37,6 +37,9 @@
bc3ff95
 man_MANS += pivot_root.8
bc3ff95
 endif
bc3ff95
 
bc3ff95
+if HAVE_SELINUX
bc3ff95
+mount_LDADD += -lselinux
bc3ff95
+endif
bc3ff95
 
bc3ff95
 swapon.c: swapargs.h
bc3ff95