Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp3756442pxt; Tue, 10 Aug 2021 10:35:02 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz2bxbvQfnf21btsHGpChgf2p+S3EU4fHmoMHqpvO0EAgzkaUiiKh+Uin8oxJMWNlfj/Wsp X-Received: by 2002:a17:906:40d1:: with SMTP id a17mr29158335ejk.503.1628616902785; Tue, 10 Aug 2021 10:35:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628616902; cv=none; d=google.com; s=arc-20160816; b=ZB+dA3+LNuxqFPTSX+3FiuH+JeIvA4DQOJURcWX4wWpwNLcriBAZt6B3gvgGt8Z3/z AdlqaHv5HObWL3/aH7r5Z6V2fCz8woC/oVHwaH+B8sXuDV7AnrJShQOYBZNiHZwGOFw+ nX8wGFOIu84leikr9Se1haUezCkGzuoOEkDkKjY2NJ2ltOArA5QPC4KNJFSCSyrEhNYX k1FTcbbdIYoWbopO2wEuV0dK08XnpGMv84TET7q6yrwopM+jAP2CzqNQPNavmw9vq56b iVBdfs1qRtg7lw4UXdvhE1d3peWAy5gZpnVSWQDWV2zCXb+Fmipx1t300ElIgYIOBhLz 8UqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=Tmr233W2aednYkpcss98Zt7ZTwU8NXUkTjr+qSO/RCg=; b=QVsBzrZSI5V8AbNh2MFgwc+8xgSOASU1N2XgtPSqsYlzPjWw8ZQ1lLukZls1HGbRJh Fd+GAB2b5JpGMz+7PjyrTTNkFUzPjlwOopUO06Q6UOLC5ykCT4pJ6bYi+rC0NYZw889e ot4NiRyM3FJyCWN9qx39KUokDGQmc6KQJeiu5OQV3AqKQndnolvzDnES2tqAz6OSCKHz 6Nanv27bPGKUjVHvxLK4wn4F7uV6ahAhcEg961K77tCjvCUsT9YmD6SqOnizCxMVBvqO 9fBh0PfqEqlmvi3qNDzXcPVA0dwxCZi5681bW9buk0NCaxhFvUlkoqzz3B2e2sEzXNV6 8ybw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=XGQn4T8d; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id h8si1165266ejj.750.2021.08.10.10.34.38; Tue, 10 Aug 2021 10:35:02 -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=@linuxfoundation.org header.s=korg header.b=XGQn4T8d; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231626AbhHJRc7 (ORCPT + 99 others); Tue, 10 Aug 2021 13:32:59 -0400 Received: from mail.kernel.org ([198.145.29.99]:33340 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231731AbhHJRco (ORCPT ); Tue, 10 Aug 2021 13:32:44 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id A0B8F61019; Tue, 10 Aug 2021 17:32:21 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1628616742; bh=RWzDhTlap1EPnTUPUBiqBIam1x9KEmwZkwx8besEvGY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=XGQn4T8dtW0BpitDLV0eg4/KwlMAohhD4MxNHsca54ncPwUGy/NnnxBQ7u186z2By wozBdkjnPF7irLhm9rsJujVUrOUS6KDS5X44gMBCiZwwq8C7dTKU6KhC374Qy0Bhi9 FdiXnzSQErIyHAhHGFrxtSDksbtdJI0JKbXSQ3xs= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, syzbot+de271708674e2093097b@syzkaller.appspotmail.com, Shuah Khan , Luis Chamberlain , Anirudh Rayabharam Subject: [PATCH 4.19 26/54] firmware_loader: fix use-after-free in firmware_fallback_sysfs Date: Tue, 10 Aug 2021 19:30:20 +0200 Message-Id: <20210810172945.037293184@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210810172944.179901509@linuxfoundation.org> References: <20210810172944.179901509@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Anirudh Rayabharam commit 75d95e2e39b27f733f21e6668af1c9893a97de5e upstream. This use-after-free happens when a fw_priv object has been freed but hasn't been removed from the pending list (pending_fw_head). The next time fw_load_sysfs_fallback tries to insert into the list, it ends up accessing the pending_list member of the previously freed fw_priv. The root cause here is that all code paths that abort the fw load don't delete it from the pending list. For example: _request_firmware() -> fw_abort_batch_reqs() -> fw_state_aborted() To fix this, delete the fw_priv from the list in __fw_set_state() if the new state is DONE or ABORTED. This way, all aborts will remove the fw_priv from the list. Accordingly, remove calls to list_del_init that were being made before calling fw_state_(aborted|done). Also, in fw_load_sysfs_fallback, don't add the fw_priv to the pending list if it is already aborted. Instead, just jump out and return early. Fixes: bcfbd3523f3c ("firmware: fix a double abort case with fw_load_sysfs_fallback") Cc: stable Reported-by: syzbot+de271708674e2093097b@syzkaller.appspotmail.com Tested-by: syzbot+de271708674e2093097b@syzkaller.appspotmail.com Reviewed-by: Shuah Khan Acked-by: Luis Chamberlain Signed-off-by: Anirudh Rayabharam Link: https://lore.kernel.org/r/20210728085107.4141-3-mail@anirudhrb.com Signed-off-by: Greg Kroah-Hartman --- drivers/base/firmware_loader/fallback.c | 12 ++++++++---- drivers/base/firmware_loader/firmware.h | 10 +++++++++- drivers/base/firmware_loader/main.c | 2 ++ 3 files changed, 19 insertions(+), 5 deletions(-) --- a/drivers/base/firmware_loader/fallback.c +++ b/drivers/base/firmware_loader/fallback.c @@ -86,12 +86,11 @@ static void __fw_load_abort(struct fw_pr { /* * There is a small window in which user can write to 'loading' - * between loading done and disappearance of 'loading' + * between loading done/aborted and disappearance of 'loading' */ - if (fw_sysfs_done(fw_priv)) + if (fw_state_is_aborted(fw_priv) || fw_sysfs_done(fw_priv)) return; - list_del_init(&fw_priv->pending_list); fw_state_aborted(fw_priv); } @@ -297,7 +296,6 @@ static ssize_t firmware_loading_store(st * Same logic as fw_load_abort, only the DONE bit * is ignored and we set ABORT only on failure. */ - list_del_init(&fw_priv->pending_list); if (rc) { fw_state_aborted(fw_priv); written = rc; @@ -559,6 +557,11 @@ static int fw_load_sysfs_fallback(struct } mutex_lock(&fw_lock); + if (fw_state_is_aborted(fw_priv)) { + mutex_unlock(&fw_lock); + retval = -EINTR; + goto out; + } list_add(&fw_priv->pending_list, &pending_fw_head); mutex_unlock(&fw_lock); @@ -584,6 +587,7 @@ static int fw_load_sysfs_fallback(struct } else if (fw_priv->is_paged_buf && !fw_priv->data) retval = -ENOMEM; +out: device_del(f_dev); err_put_dev: put_device(f_dev); --- a/drivers/base/firmware_loader/firmware.h +++ b/drivers/base/firmware_loader/firmware.h @@ -106,8 +106,16 @@ static inline void __fw_state_set(struct WRITE_ONCE(fw_st->status, status); - if (status == FW_STATUS_DONE || status == FW_STATUS_ABORTED) + if (status == FW_STATUS_DONE || status == FW_STATUS_ABORTED) { +#ifdef CONFIG_FW_LOADER_USER_HELPER + /* + * Doing this here ensures that the fw_priv is deleted from + * the pending list in all abort/done paths. + */ + list_del_init(&fw_priv->pending_list); +#endif complete_all(&fw_st->completion); + } } static inline void fw_state_aborted(struct fw_priv *fw_priv) --- a/drivers/base/firmware_loader/main.c +++ b/drivers/base/firmware_loader/main.c @@ -558,8 +558,10 @@ static void fw_abort_batch_reqs(struct f return; fw_priv = fw->priv; + mutex_lock(&fw_lock); if (!fw_state_is_aborted(fw_priv)) fw_state_aborted(fw_priv); + mutex_unlock(&fw_lock); } /* called from request_firmware() and request_firmware_work_func() */