Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp4406462pxa; Mon, 10 Aug 2020 08:22:30 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw84+SRDONG41N4mb0jW+//wXM38ykrStvVsA6WvJvFQ6RbyQ8JGuou3p5z87RF6cn4gvjs X-Received: by 2002:aa7:dc4f:: with SMTP id g15mr21876834edu.335.1597072949917; Mon, 10 Aug 2020 08:22:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597072949; cv=none; d=google.com; s=arc-20160816; b=p+JWeLNi1tCv0GVeES755I4oIqYRzIGBgKZO4pB4rUZ6zTtFRWWEANxnq3JLnDvI2v WmyovXEEIxoMpqR4XIWCHo7QYGxrZ0RcF3Cqbe1JEYUgmqRuRiyIy5HTyUJO4L7YA1Ht wSa773xrrIvt99egJvvl6HDjg4SPK2kd0TgZ6lLIT+3LG097dAXyO5Vdmm8l5Ib7iPq+ A3Ao0/rqXBdRXjqHKnLTmLTSkZt2ooRe1uX9Q6wi6l2Cha2Yger8bu/+YH08yn3MRqYB BlU80+9IQ3GAE9ppcYN0T6C8jpe0jTL7NAACC7YvlOJe/4IJekG4pZ6f1VF9gKBS4cgf G67g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=qp2jRFbGmtGB88cMob2zJmzz1sz3bDBKouzRnAik+Ko=; b=yN3qTGVbOMXcGmZr6pMVMtC5QRy2AwkMRogSWOMaBnK4yd1TLJrEp41O/h/1juzvdb o1TA+vfkloL9HF7jwj0eJ7rOv3DGCc6HoabrW7+Mu8lZsZOGY3vRHdjyEiGNQgu6jPQL NR7hyJPUv7Jl31BeUuQzMrE7OqB8ZqctxGORtXrrt8Y14+Ff6OnO1DKmdWY3tNldvmpU QV4ZRRD/uU6AsuW7O/QhCBhMNW+FmnvYLbbNxLGPihZ6S23ngPvG47emBswmMq2FxfGI GQ2nPqL6gar1BoW7giGKvvjEwB2lw1fBhPMHsDsvBBxGbeV6beLVaRg8iLbhriP1nhWy X2jA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=iy2YDdm4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id md1si10818006ejb.344.2020.08.10.08.22.06; Mon, 10 Aug 2020 08:22:29 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=iy2YDdm4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728080AbgHJPUy (ORCPT + 99 others); Mon, 10 Aug 2020 11:20:54 -0400 Received: from mail.kernel.org ([198.145.29.99]:51496 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728071AbgHJPUv (ORCPT ); Mon, 10 Aug 2020 11:20:51 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id ED0E120772; Mon, 10 Aug 2020 15:20:49 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1597072850; bh=3XyBT8qm/Hn65XwcVEVyAbRbtgSY0hvyPErs0wZummM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iy2YDdm4I6BIXcEQSJTV7LydIF9Em961dJNaTSQYSVC/cTevVNPvB7XCY5cbcLtxS JLvaOD6mwlMcmizPuPoVW8Clboj96NhRQ4lLrNHGhsoJTyLR/OvJQiiqRmucviU2yX KYHfQm9yDauVv6o1rX6vXxdwNQs7CSCZc+qbtRAY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+e6416dabb497a650da40@syzkaller.appspotmail.com, Eric Biggers , Casey Schaufler Subject: [PATCH 5.8 20/38] Smack: fix use-after-free in smk_write_relabel_self() Date: Mon, 10 Aug 2020 17:19:10 +0200 Message-Id: <20200810151804.884669703@linuxfoundation.org> X-Mailer: git-send-email 2.28.0 In-Reply-To: <20200810151803.920113428@linuxfoundation.org> References: <20200810151803.920113428@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Eric Biggers commit beb4ee6770a89646659e6a2178538d2b13e2654e 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 #include #include 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: 38416e53936e ("Smack: limited capability for changing process label") Cc: # v4.4+ Signed-off-by: Eric Biggers Signed-off-by: Casey Schaufler Signed-off-by: Greg Kroah-Hartman --- security/smack/smackfs.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) --- a/security/smack/smackfs.c +++ b/security/smack/smackfs.c @@ -2720,7 +2720,6 @@ static int smk_open_relabel_self(struct static ssize_t smk_write_relabel_self(struct file *file, const char __user *buf, size_t count, loff_t *ppos) { - struct task_smack *tsp = smack_cred(current_cred()); char *data; int rc; LIST_HEAD(list_tmp); @@ -2745,11 +2744,21 @@ static ssize_t smk_write_relabel_self(st kfree(data); if (!rc || (rc == -EINVAL && list_empty(&list_tmp))) { + struct cred *new; + struct task_smack *tsp; + + new = prepare_creds(); + if (!new) { + rc = -ENOMEM; + goto out; + } + tsp = smack_cred(new); smk_destroy_label_list(&tsp->smk_relabel); list_splice(&list_tmp, &tsp->smk_relabel); + commit_creds(new); return count; } - +out: smk_destroy_label_list(&list_tmp); return rc; }