From patchwork Wed Jul 12 23:01:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Haitao Huang X-Patchwork-Id: 119380 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:a6b2:0:b0:3e4:2afc:c1 with SMTP id c18csp1467468vqm; Wed, 12 Jul 2023 16:05:49 -0700 (PDT) X-Google-Smtp-Source: APBJJlGUoPd30WaazooIkha7isk8BCTqUsmkVrKU3d1RGWrVZgw3DeSbsNtHHS9ISx9sHT7HdrpH X-Received: by 2002:a17:906:9b8f:b0:98d:f6eb:3b03 with SMTP id dd15-20020a1709069b8f00b0098df6eb3b03mr22631541ejc.56.1689203149242; Wed, 12 Jul 2023 16:05:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689203149; cv=none; d=google.com; s=arc-20160816; b=q0Dj15BOZfYu0NSsd8fQXfJk0Br36VBwuNUjz9A7ib7cu1HEJ22Huq7oDsgcgc2wXr dlzyW/r1TQmvaRqDFNB2+PAsz7Kc7/aWl9G//kLylLAfClAfDexcnWYdZySBfmZRXqXZ 983yicTWqDNOVa/trpfSW3p/D5Le9qWU1WcU2iuWo0KElJ7WiLgccvnOG/ou+pQT5lQx 4j4aAkWlgf76JeADfyJU4DXvJ54z3gPuZcjNey9Oz/XwsI802oaHXcVtxkHPNYYcd453 IF4C6gmAZD9JvOI1W+VU5OIiAm/XTURcepKWRU6DmDXmWSPZD6WTX8+2pY5UcB9MkC2D iUDw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=JMn5C+SuiMidlQ59XUH64zNjGldBgm+zQ80WuiVSgyY=; fh=qLO4/VO18j+9BHx+eDGN4B9wTedSSqlU+IMbNJ/OHOg=; b=z+1IAPRIzyAw+AfdSMq16CcfFJLn6tkFEab3ziJAmSqu2sNAbDihDsDGA0arc5JfiC 2t7Uos5BOt7lr8UY2dnwfziVg3rb+rX3urIeGHH+r3uaWKpNf3JNhGYfydFzZZL8AZnB vR5U/ZJXL2vd5NchbiErfXFingontn0Rphcv0C6gei+PbssIvxGk4NjkvRPrgw8bY0o5 1FbdbuTBBVHkQBINuavrZi/Dtl519cNH3WrwCTWj8IG5f33JTD10GOLXXO79jgNfJ2RF codFTSiSchJF7E+anhVfW/7qnIUKN+uTOe5WBtP+VkftwpPJBZ+ItOYglAw2lz5ViiAQ uP8w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=GXltZie+; 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=intel.com Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id rp1-20020a170906d96100b009888e0923cfsi6020244ejb.712.2023.07.12.16.05.25; Wed, 12 Jul 2023 16:05:49 -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=@intel.com header.s=Intel header.b=GXltZie+; 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=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233104AbjGLXDM (ORCPT + 99 others); Wed, 12 Jul 2023 19:03:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:50568 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232717AbjGLXCR (ORCPT ); Wed, 12 Jul 2023 19:02:17 -0400 Received: from mga06.intel.com (mga06b.intel.com [134.134.136.31]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 5B47C10D4; Wed, 12 Jul 2023 16:02:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1689202936; x=1720738936; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=29rCY8b1sI/pP+SjE7VInjolfMafoPZmrodrQ56lQbM=; b=GXltZie+I4jPLkN3A6s3ad60A3gpvCwELiAms4Wm9FVXszPi9KL3nkkq mmfhAPa5pOyzyDgffbNlrVprAkdzVL9jDd26Sy2R7uxfimS9ZfEFsnwff m1Nduajsy/9+vJhif1yVqHLJ5NCKn9HPsWfXdyTuruEGcU6gxWoAa/rCy Kzz32WTz77TxwxgMs7q4hTH1jvlr8cSuXNzp5KD9aTDkL1TXf+sTWq8kB zRn8vyIdg4S32/2v6JjvZFxf9vpbBC308aOQG+VG5PK6hPoiQs1yBVVG8 Ykt2D4AJAAP/ZtbJZncskae2RqFrHdgMS0LWIWSjbUZZFVbBBHuCojIfL w==; X-IronPort-AV: E=McAfee;i="6600,9927,10769"; a="428774156" X-IronPort-AV: E=Sophos;i="6.01,200,1684825200"; d="scan'208";a="428774156" Received: from fmsmga002.fm.intel.com ([10.253.24.26]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 12 Jul 2023 16:02:15 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10769"; a="835338646" X-IronPort-AV: E=Sophos;i="6.01,200,1684825200"; d="scan'208";a="835338646" Received: from b4969161e530.jf.intel.com ([10.165.56.46]) by fmsmga002.fm.intel.com with ESMTP; 12 Jul 2023 16:02:14 -0700 From: Haitao Huang To: jarkko@kernel.org, dave.hansen@linux.intel.com, tj@kernel.org, linux-kernel@vger.kernel.org, linux-sgx@vger.kernel.org, cgroups@vger.kernel.org, Thomas Gleixner , Ingo Molnar , Borislav Petkov , x86@kernel.org, "H. Peter Anvin" Cc: kai.huang@intel.com, reinette.chatre@intel.com, zhiquan1.li@intel.com, kristen@linux.intel.com, seanjc@google.com Subject: [PATCH v3 17/28] x86/sgx: fix a NULL pointer Date: Wed, 12 Jul 2023 16:01:51 -0700 Message-Id: <20230712230202.47929-18-haitao.huang@linux.intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20230712230202.47929-1-haitao.huang@linux.intel.com> References: <20230712230202.47929-1-haitao.huang@linux.intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED 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 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1771257881039776032 X-GMAIL-MSGID: 1771257881039776032 Under heavy load, the SGX EPC reclaimers (ksgxd or future EPC cgroup worker) may reclaim SECS EPC page for an enclave and set encl->secs.epc_page to NULL. But the SECS EPC page is required for EAUG in #PF handler and is used without checking for NULL and reloading. Fix this by checking if SECS is loaded before EAUG and load it if it was reclaimed. Signed-off-by: Haitao Huang --- arch/x86/kernel/cpu/sgx/encl.c | 30 +++++++++++++++++++++++------- arch/x86/kernel/cpu/sgx/main.c | 4 ++++ 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/arch/x86/kernel/cpu/sgx/encl.c b/arch/x86/kernel/cpu/sgx/encl.c index c321c848baa9..028d1b9d6572 100644 --- a/arch/x86/kernel/cpu/sgx/encl.c +++ b/arch/x86/kernel/cpu/sgx/encl.c @@ -235,6 +235,19 @@ static struct sgx_epc_page *sgx_encl_eldu(struct sgx_encl_page *encl_page, return epc_page; } +static struct sgx_epc_page *sgx_encl_load_secs(struct sgx_encl *encl) +{ + struct sgx_epc_page *epc_page = encl->secs.epc_page; + + if (!epc_page) { + epc_page = sgx_encl_eldu(&encl->secs, NULL); + if (!IS_ERR(epc_page)) + sgx_record_epc_page(epc_page, SGX_EPC_OWNER_ENCL_PAGE | + SGX_EPC_PAGE_UNRECLAIMABLE); + } + return epc_page; +} + static struct sgx_encl_page *__sgx_encl_load_page(struct sgx_encl *encl, struct sgx_encl_page *entry) { @@ -248,13 +261,9 @@ static struct sgx_encl_page *__sgx_encl_load_page(struct sgx_encl *encl, return entry; } - if (!(encl->secs.epc_page)) { - epc_page = sgx_encl_eldu(&encl->secs, NULL); - if (IS_ERR(epc_page)) - return ERR_CAST(epc_page); - sgx_record_epc_page(epc_page, SGX_EPC_OWNER_ENCL_PAGE | - SGX_EPC_PAGE_UNRECLAIMABLE); - } + epc_page = sgx_encl_load_secs(encl); + if (IS_ERR(epc_page)) + return ERR_CAST(epc_page); epc_page = sgx_encl_eldu(entry, encl->secs.epc_page); if (IS_ERR(epc_page)) @@ -342,6 +351,13 @@ static vm_fault_t sgx_encl_eaug_page(struct vm_area_struct *vma, mutex_lock(&encl->lock); + epc_page = sgx_encl_load_secs(encl); + if (IS_ERR(epc_page)) { + if (PTR_ERR(epc_page) == -EBUSY) + vmret = VM_FAULT_NOPAGE; + goto err_out_unlock; + } + epc_page = sgx_alloc_epc_page(encl_page, false); if (IS_ERR(epc_page)) { if (PTR_ERR(epc_page) == -EBUSY) diff --git a/arch/x86/kernel/cpu/sgx/main.c b/arch/x86/kernel/cpu/sgx/main.c index 9ea487469e4c..68c89d575abc 100644 --- a/arch/x86/kernel/cpu/sgx/main.c +++ b/arch/x86/kernel/cpu/sgx/main.c @@ -265,6 +265,10 @@ static void sgx_reclaimer_write(struct sgx_epc_page *epc_page, mutex_lock(&encl->lock); + /* Should not be possible */ + if (WARN_ON(!(encl->secs.epc_page))) + goto out; + sgx_encl_ewb(epc_page, backing); encl_page->epc_page = NULL; encl->secs_child_cnt--;