1 - mount does not translate SELIinux context options though libselinux
2 - Need man page entry for -o context= mount option
3 - Can't mount with additional contexts
5 --- util-linux-2.13-pre6/mount/Makefile.am.context 2005-09-12 22:41:11.000000000 +0200
6 +++ util-linux-2.13-pre6/mount/Makefile.am 2006-11-01 11:31:46.000000000 +0100
8 man_MANS += pivot_root.8
12 +mount_LDADD += -lselinux
17 --- util-linux-2.13-pre6/mount/mount.c.context 2006-11-01 11:31:46.000000000 +0100
18 +++ util-linux-2.13-pre6/mount/mount.c 2006-11-01 11:36:17.000000000 +0100
21 #include <sys/mount.h>
23 +#ifdef HAVE_LIBSELINUX
24 +#include <selinux/selinux.h>
25 +#include <selinux/context.h>
28 #include "mount_blkid.h"
29 #include "mount_constants.h"
35 +#ifdef HAVE_LIBSELINUX
36 +/* strip quotes from a "string"
37 + * Warning: This function modify the "str" argument.
40 +strip_quotes(char *str)
47 + end = strrchr(str, '"');
48 + if (end == NULL || end == str)
49 + die (EX_USAGE, _("mount: improperly quoted option string '%s'"), str);
55 +/* translates SELinux context from human to raw format and
56 + * appends it to the mount extra options.
58 + * returns -1 on error and 0 on success
61 +append_context(const char *optname, char *optdata, char *extra_opts, int *len)
63 + security_context_t raw = NULL;
68 + if (!is_selinux_enabled())
69 + /* ignore the option if we running without selinux */
72 + if (optdata==NULL || *optdata=='\0' || optname==NULL)
75 + /* TODO: use strip_quotes() for all mount options? */
76 + data = *optdata =='"' ? strip_quotes(optdata) : optdata;
78 + if (selinux_trans_to_raw_context(
79 + (security_context_t) data, &raw)==-1 ||
84 + printf(_("mount: translated %s '%s' to '%s'\n"),
85 + optname, data, (char *) raw);
87 + bufsz = strlen(optname) + strlen(raw) + 4; /* 4 is \0, '=' and 2x '"' */
88 + buf = xmalloc(bufsz);
90 + snprintf(buf, bufsz, "%s=\"%s\"", optname, (char *) raw);
93 + if ((*len -= bufsz-1) > 0)
94 + strcat(extra_opts, buf);
102 * Look for OPT in opt_map table and return mask value.
103 * If OPT isn't found, tack it onto extra_opts (which is non-NULL).
104 * For the options uid= and gid= replace user or group name by its value.
107 -parse_opt(const char *opt, int *mask, char *extra_opts, int len) {
108 +parse_opt(char *opt, int *mask, char *extra_opts, int len) {
109 const struct opt_map *om;
111 for (om = opt_map; om->opt != NULL; om++)
117 +#ifdef HAVE_LIBSELINUX
118 + if (strncmp(opt, "context=", 8)==0 && *(opt+8)) {
119 + if (append_context("context", opt+8, extra_opts, &len)==0)
122 + if (strncmp(opt, "fscontext=", 10)==0 && *(opt+10)) {
123 + if (append_context("fscontext", opt+10, extra_opts, &len)==0)
126 + if (strncmp(opt, "defcontext=", 11)==0 && *(opt+11)) {
127 + if (append_context("defcontext", opt+11, extra_opts, &len)==0)
131 if ((len -= strlen(opt)) > 0)
132 strcat(extra_opts, opt);
134 @@ -329,16 +413,29 @@
136 if (options != NULL) {
137 char *opts = xstrdup(options);
139 - int len = strlen(opts) + 20;
140 + int len = strlen(opts) + 256;
141 + int open_quote = 0;
144 *extra_opts = xmalloc(len);
147 - for (opt = strtok(opts, ","); opt; opt = strtok(NULL, ","))
148 - if (!parse_string_opt(opt))
149 - parse_opt(opt, flags, *extra_opts, len);
151 + for (p=opts, opt=NULL; p && *p; p++) {
153 + opt = p; /* begin of the option item */
155 + open_quote ^= 1; /* reverse the status */
157 + continue; /* still in quoted block */
159 + *p = '\0'; /* terminate the option item */
160 + /* end of option item or last item */
161 + if (*p == '\0' || *(p+1) == '\0') {
162 + if (!parse_string_opt(opt))
163 + parse_opt(opt, flags, *extra_opts, len);
170 --- util-linux-2.13-pre6/mount/mount.8.context 2006-11-01 11:31:46.000000000 +0100
171 +++ util-linux-2.13-pre6/mount/mount.8 2006-11-01 11:31:46.000000000 +0100
173 .BR noexec ", " nosuid ", and " nodev
174 (unless overridden by subsequent options, as in the option line
175 .BR users,exec,dev,suid ).
177 +\fBcontext=\fP\fIcontext\fP, \fBfscontext=\fP\fIcontext\fP and \fBdefcontext=\fP\fIcontext\fP
180 +option is useful when mounting filesystems that do not support
181 +extended attributes, such as a floppy or hard disk formatted with VFAT, or
182 +systems that are not normally running under SELinux, such as an ext3 formatted
183 +disk from a non-SELinux workstation. You can also use
185 +on filesystems you do not trust, such as a floppy. It also helps in compatibility with
186 +xattr-supporting filesystems on earlier 2.4.<x> kernel versions. Even where
187 +xattrs are supported, you can save time not having to label every file by
188 +assigning the entire disk one security context.
190 +A commonly used option for removable media is
191 +.BR context=system_u:object_r:removable_t .
193 +Two other options are
197 +both of which are mutually exclusive of the context option. This means you
198 +can use fscontext and defcontext with each other, but neither can be used with
203 +option works for all filesystems, regardless of their xattr
204 +support. The fscontext option sets the overarching filesystem label to a
205 +specific security context. This filesystem label is separate from the
206 +individual labels on the files. It represents the entire filesystem for
207 +certain kinds of permission checks, such as during mount or file creation.
208 +Individual file labels are still obtained from the xattrs on the files
209 +themselves. The context option actually sets the aggregate context that
210 +fscontext provides, in addition to supplying the same label for individual
213 +You can set the default security context for unlabeled files using
215 +option. This overrides the value set for unlabeled files in the policy and requires a
216 +file system that supports xattr labeling.
218 +For more details see