Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp5965364rwp; Mon, 17 Jul 2023 12:23:35 -0700 (PDT) X-Google-Smtp-Source: APBJJlGe8+AdOleB9VzSnmRV93iassnmd6TL3dyXcBGwYaeDwLVSzSQPDlsDDlU20lrIG8ByZTWX X-Received: by 2002:a17:907:774e:b0:97c:64bd:50a5 with SMTP id kx14-20020a170907774e00b0097c64bd50a5mr11798542ejc.53.1689621814750; Mon, 17 Jul 2023 12:23:34 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689621814; cv=none; d=google.com; s=arc-20160816; b=XyJbBPV5eDtXyUCGDpeY7aaFf0l9OzvJT0KcOIuqtZuAdI/3mEH3gof3c6ug+cmQjF dp84SbVVo1/G/GvZOArVax4Z8/CJSNx4kiFe+oYbe3SZVNRAi9mfbu1I+kTmaoH4QdVu tZjEJbT+QWIihHY/jaxbDo+7/E6gwa3RbEv9N3QZbQGvLCL/eVCyE4Ti82lrtKdpZJiX ihvXt4oJDrohRrWY1RYdIb4gm5OpkE0tHVZMKWCUOeC+s63guBcarFg3OIeft/ezod1r joh3kfd4RkRQffPg4IIRk5ulQO46lHzfaCaoLGGXyMTbx3eS3OSs9V09zU+GvjCLVJLb vmpA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:references:to:from:subject:cc :message-id:date:content-transfer-encoding:mime-version :dkim-signature; bh=CZXNnUzTfY4AalmlAgSPtWSTCNfM6WwjsuwO8q+P+as=; fh=iQMnJG0yIPCkeGgQWrwp8JFsG2G2Nc6ezVI9m6MD+oQ=; b=RhjDl10UmbPOelDaCucFQhHRl/M6FCWsoOUyG5ibbCEhWrNVkpbYiWe/NSX+KjFw8S 7Qx4dI1PyVvw2i2jeA7c9nW/Ln7qDO6rOcuK+nhbDJMZXTQRNe/EEv3ClOxxsMZNyUjm wkcFsYQLacYkpv+ZQ5AoPV0TI4CoM6CWtxYc30BJm0NdwAPWXaS3KMgci5X1xlmG2gBG cFTS4dQVHNcU84M8BaBCNmkzmOpW92mAjryZzY9x3yGLflFh5NulaRs2kgKCDtbyYYOc 1zNh/AAQ76v//uz6hvixbFsGyZDZ9t/Y5xsF2OFu9sdZNHZjiKOE80DJHAwh0RehrM0l 37ZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=mzcQNFn4; 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=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id e12-20020a170906374c00b0099297c99310si52137ejc.442.2023.07.17.12.23.08; Mon, 17 Jul 2023 12:23:34 -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=@kernel.org header.s=k20201202 header.b=mzcQNFn4; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231421AbjGQSxj (ORCPT + 99 others); Mon, 17 Jul 2023 14:53:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46320 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229476AbjGQSxi (ORCPT ); Mon, 17 Jul 2023 14:53:38 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 55F8299; Mon, 17 Jul 2023 11:53:37 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id DF71F611E9; Mon, 17 Jul 2023 18:53:36 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A83D4C433C8; Mon, 17 Jul 2023 18:53:33 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1689620016; bh=MJ+3g/By8w8uqFVuPWUmEVk3gER9FDbhho5+chT2JB4=; h=Date:Cc:Subject:From:To:References:In-Reply-To:From; b=mzcQNFn4zTUv1psOAgofMF0FP4j0vc5sfZec2W5u3AvU1paHyr1IVmCTYo2f1uxh6 R48jm/4L4JsZ5ARR72PKp016hza7C7BBijP3AZnIVuX0/OZgP61Z+qNk4lZgrbqtqi KZultOvIcvsz09OaOyTLDHewYwygvwRnSA+aosHQMe1CB/tW/UE8UATkLXVQlOGrKY E/W9OO20WDlq2Isfu13Ww4LSQ40GHwu4cfrR4cUlPYnSzCLPh2lv4VLh9gWEuVaSxg PLS2BGO9vdZiffi/XoJrjJPZvpRcSA10eo8NX7+DKUmCP+qIC66IboBahm9SIwv2Er /Xpw6OUlTy3pA== Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Mon, 17 Jul 2023 18:53:31 +0000 Message-Id: Cc: , , , , Subject: Re: [PATCH] x86/sgx: fix a NULL pointer From: "Jarkko Sakkinen" To: "Haitao Huang" , , , , "Thomas Gleixner" , "Ingo Molnar" , "Borislav Petkov" , , "H. Peter Anvin" X-Mailer: aerc 0.14.0 References: <20230717181732.84039-1-haitao.huang@linux.intel.com> In-Reply-To: <20230717181732.84039-1-haitao.huang@linux.intel.com> X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, 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 On Mon Jul 17, 2023 at 6:17 PM UTC, Haitao Huang wrote: > Under heavy load, the SGX EPC reclaimers (current ksgxd or future EPC > cgroup worker) may reclaim the SECS EPC page for an enclave and set > encl->secs.epc_page to NULL. But the SECS EPC page is used for EAUG in > the SGX #PF handler without checking for NULL and reloading. > > Fix this by checking if SECS is loaded before EAUG and load it if it was > reclaimed. > > Fixes: 5a90d2c3f5ef8 ("x86/sgx: Support adding of pages to an initialized= enclave") > Cc: stable@vger.kernel.org > Signed-off-by: Haitao Huang > --- > arch/x86/kernel/cpu/sgx/encl.c | 25 ++++++++++++++++++++----- > arch/x86/kernel/cpu/sgx/main.c | 4 ++++ > 2 files changed, 24 insertions(+), 5 deletions(-) > > diff --git a/arch/x86/kernel/cpu/sgx/encl.c b/arch/x86/kernel/cpu/sgx/enc= l.c > index 2a0e90fe2abc..d39e502bb7b0 100644 > --- a/arch/x86/kernel/cpu/sgx/encl.c > +++ b/arch/x86/kernel/cpu/sgx/encl.c > @@ -235,6 +235,16 @@ static struct sgx_epc_page *sgx_encl_eldu(struct sgx= _encl_page *encl_page, > return epc_page; > } > =20 > +static struct sgx_epc_page *sgx_encl_load_secs(struct sgx_encl *encl) > +{ > + struct sgx_epc_page *epc_page =3D encl->secs.epc_page; > + > + if (!epc_page) { > + epc_page =3D sgx_encl_eldu(&encl->secs, NULL); > + } remove curly braces > + return epc_page; > +} > + > static struct sgx_encl_page *__sgx_encl_load_page(struct sgx_encl *encl, > struct sgx_encl_page *entry) > { > @@ -248,11 +258,9 @@ static struct sgx_encl_page *__sgx_encl_load_page(st= ruct sgx_encl *encl, > return entry; > } > =20 > - if (!(encl->secs.epc_page)) { > - epc_page =3D sgx_encl_eldu(&encl->secs, NULL); > - if (IS_ERR(epc_page)) > - return ERR_CAST(epc_page); > - } > + epc_page =3D sgx_encl_load_secs(encl); > + if (IS_ERR(epc_page)) > + return ERR_CAST(epc_page); > =20 > epc_page =3D sgx_encl_eldu(entry, encl->secs.epc_page); > if (IS_ERR(epc_page)) > @@ -339,6 +347,13 @@ static vm_fault_t sgx_encl_eaug_page(struct vm_area_= struct *vma, > =20 > mutex_lock(&encl->lock); > =20 > + epc_page =3D sgx_encl_load_secs(encl); > + if (IS_ERR(epc_page)) { > + if (PTR_ERR(epc_page) =3D=3D -EBUSY) > + vmret =3D VM_FAULT_NOPAGE; > + goto err_out_unlock; > + } > + > epc_page =3D sgx_alloc_epc_page(encl_page, false); > if (IS_ERR(epc_page)) { > if (PTR_ERR(epc_page) =3D=3D -EBUSY) > diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/mai= n.c > index 166692f2d501..4662a364ce62 100644 > --- a/arch/x86/kernel/cpu/sgx/main.c > +++ b/arch/x86/kernel/cpu/sgx/main.c > @@ -257,6 +257,10 @@ static void sgx_reclaimer_write(struct sgx_epc_page = *epc_page, > =20 > mutex_lock(&encl->lock); > =20 > + /* Should not be possible */ > + if (WARN_ON(!(encl->secs.epc_page))) > + goto out; > + > sgx_encl_ewb(epc_page, backing); > encl_page->epc_page =3D NULL; > encl->secs_child_cnt--; > --=20 > 2.25.1 BR, Jarkko