Commit 99dbbb59 authored by Al Viro's avatar Al Viro

selinux: rewrite selinux_sb_eat_lsm_opts()

make it use selinux_add_opt() and avoid separate copies - gather
non-LSM options by memmove() in place
Reviewed-by: default avatarDavid Howells <dhowells@redhat.com>
Signed-off-by: default avatarAl Viro <viro@zeniv.linux.org.uk>
parent da3d76ab
...@@ -2606,109 +2606,71 @@ static void selinux_sb_free_security(struct super_block *sb) ...@@ -2606,109 +2606,71 @@ static void selinux_sb_free_security(struct super_block *sb)
superblock_free_security(sb); superblock_free_security(sb);
} }
static inline int match_prefix(char *prefix, int plen, char *option, int olen) static inline int opt_len(const char *s)
{ {
if (plen > olen) bool open_quote = false;
return 0; int len;
char c;
return !memcmp(prefix, option, plen); for (len = 0; (c = s[len]) != '\0'; len++) {
if (c == '"')
open_quote = !open_quote;
if (c == ',' && !open_quote)
break;
}
return len;
} }
static inline int selinux_option(char *option, int len) static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
{ {
return (match_prefix(CONTEXT_STR, sizeof(CONTEXT_STR)-1, option, len) || char *from = options;
match_prefix(FSCONTEXT_STR, sizeof(FSCONTEXT_STR)-1, option, len) || char *to = options;
match_prefix(DEFCONTEXT_STR, sizeof(DEFCONTEXT_STR)-1, option, len) || bool first = true;
match_prefix(ROOTCONTEXT_STR, sizeof(ROOTCONTEXT_STR)-1, option, len) ||
match_prefix(LABELSUPP_STR, sizeof(LABELSUPP_STR)-1, option, len));
}
static inline void take_option(char **to, char *from, int *first, int len) while (1) {
{ int len = opt_len(from);
if (!*first) { int token, rc;
**to = ','; char *arg = NULL;
*to += 1;
} else
*first = 0;
memcpy(*to, from, len);
*to += len;
}
static inline void take_selinux_option(char **to, char *from, int *first, token = match_opt_prefix(from, len, &arg);
int len)
{
int current_size = 0;
if (!*first) { if (token != Opt_error) {
**to = '|'; char *p, *q;
*to += 1;
} else
*first = 0;
while (current_size < len) { /* strip quotes */
if (*from != '"') { if (arg) {
**to = *from; for (p = q = arg; p < from + len; p++) {
*to += 1; char c = *p;
if (c != '"')
*q++ = c;
} }
from += 1; arg = kmemdup_nul(arg, q - arg, GFP_KERNEL);
current_size += 1;
} }
} rc = selinux_add_opt(token, arg, mnt_opts);
if (unlikely(rc)) {
static int selinux_sb_copy_data(char *orig, char *copy) kfree(arg);
{ if (*mnt_opts) {
int fnosec, fsec, rc = 0; selinux_free_mnt_opts(*mnt_opts);
char *in_save, *in_curr, *in_end; *mnt_opts = NULL;
char *sec_curr, *nosec_save, *nosec;
int open_quote = 0;
in_curr = orig;
sec_curr = copy;
nosec = (char *)get_zeroed_page(GFP_KERNEL);
if (!nosec) {
rc = -ENOMEM;
goto out;
}
nosec_save = nosec;
fnosec = fsec = 1;
in_save = in_end = orig;
do {
if (*in_end == '"')
open_quote = !open_quote;
if ((*in_end == ',' && open_quote == 0) ||
*in_end == '\0') {
int len = in_end - in_curr;
if (selinux_option(in_curr, len))
take_selinux_option(&sec_curr, in_curr, &fsec, len);
else
take_option(&nosec, in_curr, &fnosec, len);
in_curr = in_end + 1;
} }
} while (*in_end++);
strcpy(in_save, nosec_save);
free_page((unsigned long)nosec_save);
out:
return rc; return rc;
} }
} else {
static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts) if (!first) { // copy with preceding comma
{ from--;
char *s = (char *)get_zeroed_page(GFP_KERNEL); len++;
int err; }
if (to != from)
if (!s) memmove(to, from, len);
return -ENOMEM; to += len;
err = selinux_sb_copy_data(options, s); first = false;
if (!err) }
err = selinux_parse_opts_str(s, mnt_opts); if (!from[len])
free_page((unsigned long)s); break;
return err; from += len + 1;
}
*to = '\0';
return 0;
} }
static int selinux_sb_remount(struct super_block *sb, void *mnt_opts) static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment