Skip to content

Commit b076173

Browse files
committed
Merge tag 'selinux-pr-20190612' of git://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/selinux
Pull selinux fixes from Paul Moore: "Three patches for v5.2. One fixes a problem where we weren't correctly logging raw SELinux labels, the other two fix problems where we weren't properly checking calls to kmemdup()" * tag 'selinux-pr-20190612' of git://git.kernel.org/pub/scm/linux/kernel/git/pcmoore/selinux: selinux: fix a missing-check bug in selinux_sb_eat_lsm_opts() selinux: fix a missing-check bug in selinux_add_mnt_opt( ) selinux: log raw contexts as untrusted strings
2 parents 35110e3 + fec6375 commit b076173

File tree

2 files changed

+36
-13
lines changed

2 files changed

+36
-13
lines changed

security/selinux/avc.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -739,14 +739,20 @@ static void avc_audit_post_callback(struct audit_buffer *ab, void *a)
739739
rc = security_sid_to_context_inval(sad->state, sad->ssid, &scontext,
740740
&scontext_len);
741741
if (!rc && scontext) {
742-
audit_log_format(ab, " srawcon=%s", scontext);
742+
if (scontext_len && scontext[scontext_len - 1] == '\0')
743+
scontext_len--;
744+
audit_log_format(ab, " srawcon=");
745+
audit_log_n_untrustedstring(ab, scontext, scontext_len);
743746
kfree(scontext);
744747
}
745748

746749
rc = security_sid_to_context_inval(sad->state, sad->tsid, &scontext,
747750
&scontext_len);
748751
if (!rc && scontext) {
749-
audit_log_format(ab, " trawcon=%s", scontext);
752+
if (scontext_len && scontext[scontext_len - 1] == '\0')
753+
scontext_len--;
754+
audit_log_format(ab, " trawcon=");
755+
audit_log_n_untrustedstring(ab, scontext, scontext_len);
750756
kfree(scontext);
751757
}
752758
}

security/selinux/hooks.c

Lines changed: 28 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1052,15 +1052,24 @@ static int selinux_add_mnt_opt(const char *option, const char *val, int len,
10521052
if (token == Opt_error)
10531053
return -EINVAL;
10541054

1055-
if (token != Opt_seclabel)
1055+
if (token != Opt_seclabel) {
10561056
val = kmemdup_nul(val, len, GFP_KERNEL);
1057+
if (!val) {
1058+
rc = -ENOMEM;
1059+
goto free_opt;
1060+
}
1061+
}
10571062
rc = selinux_add_opt(token, val, mnt_opts);
10581063
if (unlikely(rc)) {
10591064
kfree(val);
1060-
if (*mnt_opts) {
1061-
selinux_free_mnt_opts(*mnt_opts);
1062-
*mnt_opts = NULL;
1063-
}
1065+
goto free_opt;
1066+
}
1067+
return rc;
1068+
1069+
free_opt:
1070+
if (*mnt_opts) {
1071+
selinux_free_mnt_opts(*mnt_opts);
1072+
*mnt_opts = NULL;
10641073
}
10651074
return rc;
10661075
}
@@ -2616,10 +2625,11 @@ static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
26162625
char *from = options;
26172626
char *to = options;
26182627
bool first = true;
2628+
int rc;
26192629

26202630
while (1) {
26212631
int len = opt_len(from);
2622-
int token, rc;
2632+
int token;
26232633
char *arg = NULL;
26242634

26252635
token = match_opt_prefix(from, len, &arg);
@@ -2635,15 +2645,15 @@ static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
26352645
*q++ = c;
26362646
}
26372647
arg = kmemdup_nul(arg, q - arg, GFP_KERNEL);
2648+
if (!arg) {
2649+
rc = -ENOMEM;
2650+
goto free_opt;
2651+
}
26382652
}
26392653
rc = selinux_add_opt(token, arg, mnt_opts);
26402654
if (unlikely(rc)) {
26412655
kfree(arg);
2642-
if (*mnt_opts) {
2643-
selinux_free_mnt_opts(*mnt_opts);
2644-
*mnt_opts = NULL;
2645-
}
2646-
return rc;
2656+
goto free_opt;
26472657
}
26482658
} else {
26492659
if (!first) { // copy with preceding comma
@@ -2661,6 +2671,13 @@ static int selinux_sb_eat_lsm_opts(char *options, void **mnt_opts)
26612671
}
26622672
*to = '\0';
26632673
return 0;
2674+
2675+
free_opt:
2676+
if (*mnt_opts) {
2677+
selinux_free_mnt_opts(*mnt_opts);
2678+
*mnt_opts = NULL;
2679+
}
2680+
return rc;
26642681
}
26652682

26662683
static int selinux_sb_remount(struct super_block *sb, void *mnt_opts)

0 commit comments

Comments
 (0)