Skip to content

Commit beb4ee6

Browse files
ebiggerscschaufler
authored andcommitted
Smack: fix use-after-free in smk_write_relabel_self()
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: [email protected] Fixes: 38416e5 ("Smack: limited capability for changing process label") Cc: <[email protected]> # v4.4+ Signed-off-by: Eric Biggers <[email protected]> Signed-off-by: Casey Schaufler <[email protected]>
1 parent b3a9e3b commit beb4ee6

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

security/smack/smackfs.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2720,7 +2720,6 @@ static int smk_open_relabel_self(struct inode *inode, struct file *file)
27202720
static ssize_t smk_write_relabel_self(struct file *file, const char __user *buf,
27212721
size_t count, loff_t *ppos)
27222722
{
2723-
struct task_smack *tsp = smack_cred(current_cred());
27242723
char *data;
27252724
int rc;
27262725
LIST_HEAD(list_tmp);
@@ -2745,11 +2744,21 @@ static ssize_t smk_write_relabel_self(struct file *file, const char __user *buf,
27452744
kfree(data);
27462745

27472746
if (!rc || (rc == -EINVAL && list_empty(&list_tmp))) {
2747+
struct cred *new;
2748+
struct task_smack *tsp;
2749+
2750+
new = prepare_creds();
2751+
if (!new) {
2752+
rc = -ENOMEM;
2753+
goto out;
2754+
}
2755+
tsp = smack_cred(new);
27482756
smk_destroy_label_list(&tsp->smk_relabel);
27492757
list_splice(&list_tmp, &tsp->smk_relabel);
2758+
commit_creds(new);
27502759
return count;
27512760
}
2752-
2761+
out:
27532762
smk_destroy_label_list(&list_tmp);
27542763
return rc;
27552764
}

0 commit comments

Comments
 (0)