Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp6755685rwn; Tue, 13 Sep 2022 08:36:11 -0700 (PDT) X-Google-Smtp-Source: AA6agR7mFR4M/uEEKCGCusxs5mInZ0ToJXa/5SzInhAc6Vgr39ab0g4Ivogo+8zlXYcIVhBN6WGp X-Received: by 2002:a17:907:7290:b0:77a:27ef:18d3 with SMTP id dt16-20020a170907729000b0077a27ef18d3mr13879124ejc.42.1663083370903; Tue, 13 Sep 2022 08:36:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663083370; cv=none; d=google.com; s=arc-20160816; b=f6ynts/Ns3tvzt214GicKUtcSo4PUp43Aza88IyGb5oTamwUHbt8CM7zENtUad7Vg9 dfU6eHAF8/74gQWZImHsErhGMNGUPnDCxKmO+N04Z1ydQFzC8K5M0ZT7NNajOwB4G0cm 9mqh8K16Yvhg3benSQS711r2w7XMGicnye9dL8dDMP4M9asgjRzyT+UAl5iQEEl3iFqB 3i9By6fGaeNm16vro0gLLGL5/V9/TCbohaDb8Ej5ttRLPUaStuKFYmYGt4PtwZPmb5py Ee53+7ktNKv52+ARITqcPG3M5dRn6oV232SXM0uf5F667E8CdbSR4Wn+NVbf183kGd+Z 1wWg== 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=G7c/zp5ywuJJRbT8c3ezB+qhYE6dLhnsST9zDQzsUR4=; b=Q0K+JREAdo+llenMYHDoJBM/QLP/Lxpef4GUniVSvhDKydYycR9I801pML2lCakAoW ywQ/pxuas5WEcEpxqRWiNaW8oIS8kqsSnclG1c/RVrZSkrUGHmBKhmK87cppbKe32mNE QdUZK2ZU/NqXli4oMYMPaWgdLZoVFR9OKh6uM7ckuaG1TPTRqo8UrQI65kAdbWvjWz9l vexbvB/GM31NxysnLVVex1I78n/EH8rE2DKRnINTS4C+swiaGJLaUPH39EOovg0li8q3 +sfdGvwCkhG6+CJ9SjRI1uswJg8ywBaeOMyLUUDlbUOij5148dRabsyuJuT41ijvBQli ZABw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=UwXnLhDR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id qw10-20020a1709066a0a00b0073d6b849d4fsi11533389ejc.731.2022.09.13.08.35.45; Tue, 13 Sep 2022 08:36:10 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=UwXnLhDR; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S234666AbiIMOve (ORCPT + 99 others); Tue, 13 Sep 2022 10:51:34 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32954 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234503AbiIMOst (ORCPT ); Tue, 13 Sep 2022 10:48:49 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id AC2B93FA09; Tue, 13 Sep 2022 07:25:19 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 57DC6B80F6F; Tue, 13 Sep 2022 14:25:15 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CA8AFC433D6; Tue, 13 Sep 2022 14:25:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1663079114; bh=3+djE0cYjt4Mjthlxcf5Dgf2G12NKjka1wyut8v8rMQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=UwXnLhDRQF7jCP/0gXUxRKjlIpBIrCCT6dYgWq2B9347PshsFDPyvcCQZDFs67KOB m9izSldRij9HicWVPwH1ctpb5krY67FkcpXLzSxuTqUPsELfIfEZO/uTbzvKo6syEs hXYeNngiO0V7z52M6RsZD6DXKURBztVrQZvfoh28= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Hyunwoo Kim , Ard Biesheuvel Subject: [PATCH 5.4 001/108] efi: capsule-loader: Fix use-after-free in efi_capsule_write Date: Tue, 13 Sep 2022 16:05:32 +0200 Message-Id: <20220913140353.614615587@linuxfoundation.org> X-Mailer: git-send-email 2.37.3 In-Reply-To: <20220913140353.549108748@linuxfoundation.org> References: <20220913140353.549108748@linuxfoundation.org> User-Agent: quilt/0.67 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Hyunwoo Kim commit 9cb636b5f6a8cc6d1b50809ec8f8d33ae0c84c95 upstream. A race condition may occur if the user calls close() on another thread during a write() operation on the device node of the efi capsule. This is a race condition that occurs between the efi_capsule_write() and efi_capsule_flush() functions of efi_capsule_fops, which ultimately results in UAF. So, the page freeing process is modified to be done in efi_capsule_release() instead of efi_capsule_flush(). Cc: # v4.9+ Signed-off-by: Hyunwoo Kim Link: https://lore.kernel.org/all/20220907102920.GA88602@ubuntu/ Signed-off-by: Ard Biesheuvel Signed-off-by: Greg Kroah-Hartman --- drivers/firmware/efi/capsule-loader.c | 31 +++++++------------------------ 1 file changed, 7 insertions(+), 24 deletions(-) --- a/drivers/firmware/efi/capsule-loader.c +++ b/drivers/firmware/efi/capsule-loader.c @@ -242,29 +242,6 @@ failed: } /** - * efi_capsule_flush - called by file close or file flush - * @file: file pointer - * @id: not used - * - * If a capsule is being partially uploaded then calling this function - * will be treated as upload termination and will free those completed - * buffer pages and -ECANCELED will be returned. - **/ -static int efi_capsule_flush(struct file *file, fl_owner_t id) -{ - int ret = 0; - struct capsule_info *cap_info = file->private_data; - - if (cap_info->index > 0) { - pr_err("capsule upload not complete\n"); - efi_free_all_buff_pages(cap_info); - ret = -ECANCELED; - } - - return ret; -} - -/** * efi_capsule_release - called by file close * @inode: not used * @file: file pointer @@ -276,6 +253,13 @@ static int efi_capsule_release(struct in { struct capsule_info *cap_info = file->private_data; + if (cap_info->index > 0 && + (cap_info->header.headersize == 0 || + cap_info->count < cap_info->total_size)) { + pr_err("capsule upload not complete\n"); + efi_free_all_buff_pages(cap_info); + } + kfree(cap_info->pages); kfree(cap_info->phys); kfree(file->private_data); @@ -323,7 +307,6 @@ static const struct file_operations efi_ .owner = THIS_MODULE, .open = efi_capsule_open, .write = efi_capsule_write, - .flush = efi_capsule_flush, .release = efi_capsule_release, .llseek = no_llseek, };