Skip to content

Commit 67b4be3

Browse files
ebiggersgregkh
authored andcommitted
Smack: fix use-after-free in smk_write_relabel_self()
commit beb4ee6 upstream. smk_write_relabel_self() frees memory from the task's credentials with no locking, which can easily cause a use-after-free because multiple tasks can share the same credentials structure. Fix this by using prepare_creds() and commit_creds() to correctly modify the task's credentials. Reproducer for "BUG: KASAN: use-after-free in smk_write_relabel_self": #include <fcntl.h> #include <pthread.h> #include <unistd.h> static void *thrproc(void *arg) { int fd = open("/sys/fs/smackfs/relabel-self", O_WRONLY); for (;;) write(fd, "foo", 3); } int main() { pthread_t t; pthread_create(&t, NULL, thrproc, NULL); thrproc(NULL); } Reported-by: syzbot+e6416dabb497a650da40@syzkaller.appspotmail.com Fixes: 38416e5 ("Smack: limited capability for changing process label") Cc: <stable@vger.kernel.org> # v4.4+ Signed-off-by: Eric Biggers <ebiggers@google.com> Signed-off-by: Casey Schaufler <casey@schaufler-ca.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 330aa3b commit 67b4be3

1 file changed

Lines changed: 11 additions & 2 deletions

File tree

security/smack/smackfs.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2746,7 +2746,6 @@ static int smk_open_relabel_self(struct inode *inode, struct file *file)
27462746
static ssize_t smk_write_relabel_self(struct file *file, const char __user *buf,
27472747
size_t count, loff_t *ppos)
27482748
{
2749-
struct task_smack *tsp = current_security();
27502749
char *data;
27512750
int rc;
27522751
LIST_HEAD(list_tmp);
@@ -2771,11 +2770,21 @@ static ssize_t smk_write_relabel_self(struct file *file, const char __user *buf,
27712770
kfree(data);
27722771

27732772
if (!rc || (rc == -EINVAL && list_empty(&list_tmp))) {
2773+
struct cred *new;
2774+
struct task_smack *tsp;
2775+
2776+
new = prepare_creds();
2777+
if (!new) {
2778+
rc = -ENOMEM;
2779+
goto out;
2780+
}
2781+
tsp = new->security;
27742782
smk_destroy_label_list(&tsp->smk_relabel);
27752783
list_splice(&list_tmp, &tsp->smk_relabel);
2784+
commit_creds(new);
27762785
return count;
27772786
}
2778-
2787+
out:
27792788
smk_destroy_label_list(&list_tmp);
27802789
return rc;
27812790
}

0 commit comments

Comments
 (0)