Commit 723432cb authored by Paul Moore's avatar Paul Moore Committed by Ben Hutchings

selinux: fix double free in selinux_parse_opts_str()

commit 023f108d upstream.

This patch is based on a discussion generated by an earlier patch
from Tetsuo Handa:

* https://marc.info/?t=149035659300001&r=1&w=2

The double free problem involves the mnt_opts field of the
security_mnt_opts struct, selinux_parse_opts_str() frees the memory
on error, but doesn't set the field to NULL so if the caller later
attempts to call security_free_mnt_opts() we trigger the problem.

In order to play it safe we change selinux_parse_opts_str() to call
security_free_mnt_opts() on error instead of free'ing the memory
directly.  This should ensure that everything is handled correctly,
regardless of what the caller may do.

Fixes: e0007529 ("LSM/SELinux: Interfaces to allow FS to control mount options")
Cc: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Reported-by: default avatarDmitry Vyukov <dvyukov@google.com>
Signed-off-by: default avatarPaul Moore <paul@paul-moore.com>
Signed-off-by: default avatarJames Morris <james.l.morris@oracle.com>
[bwh: Backported to 3.16: adjust context]
Signed-off-by: default avatarBen Hutchings <ben@decadent.org.uk>
parent 9927e72a
...@@ -1000,10 +1000,8 @@ static int selinux_parse_opts_str(char *options, ...@@ -1000,10 +1000,8 @@ static int selinux_parse_opts_str(char *options,
goto out_err; goto out_err;
opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC); opts->mnt_opts_flags = kcalloc(NUM_SEL_MNT_OPTS, sizeof(int), GFP_ATOMIC);
if (!opts->mnt_opts_flags) { if (!opts->mnt_opts_flags)
kfree(opts->mnt_opts);
goto out_err; goto out_err;
}
if (fscontext) { if (fscontext) {
opts->mnt_opts[num_mnt_opts] = fscontext; opts->mnt_opts[num_mnt_opts] = fscontext;
...@@ -1026,6 +1024,7 @@ static int selinux_parse_opts_str(char *options, ...@@ -1026,6 +1024,7 @@ static int selinux_parse_opts_str(char *options,
return 0; return 0;
out_err: out_err:
security_free_mnt_opts(opts);
kfree(context); kfree(context);
kfree(defcontext); kfree(defcontext);
kfree(fscontext); kfree(fscontext);
......
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