Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp986694pxb; Tue, 8 Feb 2022 06:59:04 -0800 (PST) X-Google-Smtp-Source: ABdhPJxLbgAfx7YFHaef3Xc/M7T/z3REhymIcUftTOqAOT9llYMqDvQGTAaBMUwbmauvpQyDmMk8 X-Received: by 2002:a17:906:edae:: with SMTP id sa14mr4263253ejb.722.1644332344497; Tue, 08 Feb 2022 06:59:04 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644332344; cv=none; d=google.com; s=arc-20160816; b=xFXsYxZmpsyWNZRw1YHXEPm+BmVA9n4Jd1vEgjZ3wSXp4rwOQdX+Gud9BLKLbXiZjx rcBPrRTfZ+cH06k9Xmm+7gw83dqsX56mdeM2wuFZwnRkAGSCN7iO66HjuLMC0irTGCPx 7PZv6zBoHXovzNxmtpoAwiAk/imV5FHYguNNFl8YEOCg3w00X9518Osp0MlKZmucLCii IL7Y+WxHae4WwatwUW4CNhRl9vWm7WGK7Hd8ECMicKzHPA8dMUoQFFBAuGDMAPtLSEXx 5J3I7z1a7CzB27wrEKgpF4pvxRIhGrWVsWK5At6SXUHRfJ3cxH3TtjLaSNxCw6wgYmTy YGyQ== 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=IPBSmttH8+gfGOTYC3e4HyP+nuwl76PvBp1tSa7AA+Y=; b=LdhHMg6SEPA6PDdE/Q/aY6QAW7TtIvznYsQbY5rIcDIDnKqgwPP+wDxZT6KWTBsrdB IN9IFCuppucRbqXWh/iyLcDAEVNHECYGdtoVOANCx/+IH6LOCRtNwgSadoIXtyAdGV9d hv3xZ+C2XwUGfhZyX/vYmctYkTZLIOSQ5ZVp5IMG5wWWLCk55o3VcjK1XB8jD8bYPE0m SquU3OGXwHupJUZX6b+MpiyzowDXkSV6GH+tfUZqxNoLQ7s8cZ4qiKfTJrIhKNBfiQ4C 27BcnMlMBQr0gaxqD8c1O0EBVW6kEARY+6vij9JRVEoVRMKfthAwvCNn+6CnpXB8Fyth ie5w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=P54keFmT; 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 Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id oz14si5723365ejc.867.2022.02.08.06.58.38; Tue, 08 Feb 2022 06:59:04 -0800 (PST) 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=P54keFmT; 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 S1345014AbiBHBLE (ORCPT + 99 others); Mon, 7 Feb 2022 20:11:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59434 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344208AbiBHAqQ (ORCPT ); Mon, 7 Feb 2022 19:46:16 -0500 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 72F1DC06109E; Mon, 7 Feb 2022 16:46:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1644281175; x=1675817175; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=nPz4+vatRmyqZEYH2Bd8uVMCoWcWywhPkxm9WqPHwrI=; b=P54keFmTR633x0M/2ytgyWPMkLgWbx4cmVNQ3npGjPw92mlYZaoT3OBK ZanBlteUdQLXL97WfRpqcawUv3Cc73jmM/rypQZVUu6k7a4Eld17gB0Xx fhp/Gt1Zy9UBXCeaghSOSFZPQd8X9HCohRxrNgCTpx7DNibW/LjFy859p uLssjV+c5Q/E+JmWvZr+lBgUA0Dy9IzaIML4BNEmSrw/GhviU6xDB0fE/ +oWUAafDLTB+mipA9JPXiKhiusI+p498rUhErXxECmeuL4oOsDVZq5tO7 +2cD8wiMDzB4/mu8P6D4D5shkUodGwqmuqQxGaPkV04eoDAMV0RteRMpT g==; X-IronPort-AV: E=McAfee;i="6200,9189,10251"; a="232407974" X-IronPort-AV: E=Sophos;i="5.88,351,1635231600"; d="scan'208";a="232407974" Received: from orsmga002.jf.intel.com ([10.7.209.21]) by orsmga106.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Feb 2022 16:46:09 -0800 X-IronPort-AV: E=Sophos;i="5.88,351,1635231600"; d="scan'208";a="499389528" Received: from rchatre-ws.ostc.intel.com ([10.54.69.144]) by orsmga002-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 07 Feb 2022 16:46:09 -0800 From: Reinette Chatre To: dave.hansen@linux.intel.com, jarkko@kernel.org, tglx@linutronix.de, bp@alien8.de, luto@kernel.org, mingo@redhat.com, linux-sgx@vger.kernel.org, x86@kernel.org Cc: seanjc@google.com, kai.huang@intel.com, cathy.zhang@intel.com, cedric.xing@intel.com, haitao.huang@intel.com, mark.shanahan@intel.com, hpa@zytor.com, linux-kernel@vger.kernel.org Subject: [PATCH V2 20/32] x86/sgx: Tighten accessible memory range after enclave initialization Date: Mon, 7 Feb 2022 16:45:42 -0800 Message-Id: <1b833dbce6c937f71523f4aaf4b2181b9673519f.1644274683.git.reinette.chatre@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,SPF_HELO_NONE, SPF_NONE,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 Before an enclave is initialized the enclave's memory range is unknown. The enclave's memory range is learned at the time it is created via the SGX_IOC_ENCLAVE_CREATE ioctl() where the provided memory range is obtained from an earlier mmap() of /dev/sgx_enclave. After an enclave is initialized its memory can be mapped into user space (mmap()) from where it can be entered at its defined entry points. With the enclave's memory range known after it is initialized there is no reason why it should be possible to map memory outside this range. Lock down access to the initialized enclave's memory range by denying any attempt to map memory outside its memory range. Locking down the memory range also makes adding pages to an initialized enclave more efficient. Pages are added to an initialized enclave by accessing memory that belongs to the enclave's memory range but not yet backed by an enclave page. If it is possible for user space to map memory that does not form part of the enclave then an access to this memory would eventually fail. Failures range from a prompt general protection fault if the access was an ENCLU[EACCEPT] from within the enclave, or a page fault via the vDSO if it was another access from within the enclave, or a SIGBUS (also resulting from a page fault) if the access was from outside the enclave. Disallowing invalid memory to be mapped in the first place avoids preventable failures. Signed-off-by: Reinette Chatre --- Changes since V1: - Add comment (Jarkko). arch/x86/kernel/cpu/sgx/encl.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/arch/x86/kernel/cpu/sgx/encl.c b/arch/x86/kernel/cpu/sgx/encl.c index d1e3ea86b902..c20100245411 100644 --- a/arch/x86/kernel/cpu/sgx/encl.c +++ b/arch/x86/kernel/cpu/sgx/encl.c @@ -403,6 +403,11 @@ int sgx_encl_may_map(struct sgx_encl *encl, unsigned long start, XA_STATE(xas, &encl->page_array, PFN_DOWN(start)); + /* Disallow mapping outside enclave's address range. */ + if (test_bit(SGX_ENCL_INITIALIZED, &encl->flags) && + (start < encl->base || end > encl->base + encl->size)) + return -EACCES; + /* * Disallow READ_IMPLIES_EXEC tasks as their VMA permissions might * conflict with the enclave page permissions. -- 2.25.1