Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp1822491pxb; Wed, 9 Feb 2022 05:21:15 -0800 (PST) X-Google-Smtp-Source: ABdhPJzZUsLCVoA3aa2ZYS+IU9q/QpNMLMuQHJqzY1ZdRUWrXDgShQcmQVhQygmeCON4Z60hNgOR X-Received: by 2002:a63:f52:: with SMTP id 18mr1881968pgp.187.1644412875181; Wed, 09 Feb 2022 05:21:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1644412875; cv=none; d=google.com; s=arc-20160816; b=aIxB38aS4PM6ZlQvqSjo7W6LPQjI8Oh0FCfcwU6cvqkKcZ5cRnL20oT92BU9IXtPYY c4Ele5EfhgvmvCBCWyk8HPdTmra5bJ3jLggV6uVFc7uGdDEv6ulcNjJ6PqKQ1Eftic0a UjErUBHp8jgFgktpyMfOGu1IcC3vIo69saQdPSZaQ0TG4DXPKLDGWKacRUOiebatArEw bYWpwiIMnakSmH8pvhVpY5w9WmXDBqzTTld3i7SPsenPrRUiNs/Is2Sx3IxGUBKImUTy kwmo16SnHQLurLvq0na8FLAIJLZ13bGkqCZ+iAv3MT3w8US9vWppfmbYbrNmV1MKJwUj LnVA== 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=HVCy1VnFWDSPe8+fYLsqaCv0/mfzFvJ74qd2++DY0dM=; b=rEn+DIz2TNcRO6uFRuHOEEjN4GRK91qK6d6MD14v09Ekmx93VE1590VF0XafZ3LdD/ 6iuQ5ZbSBaKIt7yvEMEElg5Dg0sWRzJlY+IqsxfZIQP+AxFW7lsaeDA3bq6KxkjJwxtE TOO6hdtFBQveXLDOu/dfj5WBkV5qH/9K/k5P0LYRZ/eDmjohDib26F5Aru7tntonrqUE eSYIliVRVgW1dybI813rZFT1pbK0lTbCnbS1ejlpy7698QKr6r23E6sJsUg+QE65zSEH NQg1AAN2QFw9h1/NtrrUYgOQ3EWLOk+J8XNE3Gojmguknm0hmGL7oO3300lY9rb3UHW5 iUbg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=W9Z6dd9X; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id o13si7181073pla.73.2022.02.09.05.21.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Feb 2022 05:21:15 -0800 (PST) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=W9Z6dd9X; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 2D59EE083C5D; Wed, 9 Feb 2022 02:06:53 -0800 (PST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237680AbiBHBKi (ORCPT + 99 others); Mon, 7 Feb 2022 20:10:38 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:59416 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344200AbiBHAqP (ORCPT ); Mon, 7 Feb 2022 19:46:15 -0500 Received: from mga18.intel.com (mga18.intel.com [134.134.136.126]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8A363C043180; Mon, 7 Feb 2022 16:46:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1644281174; x=1675817174; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=1A6UdSkdCK3bqLgWMEXCKbpOWOvvLhq/cAw+l+Ee07I=; b=W9Z6dd9XTItOWGLB23nsu3G8Mahcz4Zs3paJTDokByW9JAy5aEPcr9Q1 Vd+xRDrxCL3uWlEZ69JJNwq4Skgdmoqc9WhlP9JZ+tzDfOhKUjZhwNXrP YG9lUZumy0sllJtu10ixE5isTxDZ0Xufj9AV3v9m1QijuekkWMSjkjOBG TgTcHakk4EKu8Muw1LhrZ2X9dIRN/UJ/U/l05fQebik8XqwBLwhiw6XCV E5CtTdnkBNpGVT0RcH8RHu+oEFpeZt0SKQ467XNsWt6SarxUHfblyVuvG 4BFuoi0XDjJu9zpe+qtdwDKf3g8OwKaUv0Q8+TcIW8sBScgg0hPzrCIzj A==; X-IronPort-AV: E=McAfee;i="6200,9189,10251"; a="232407970" X-IronPort-AV: E=Sophos;i="5.88,351,1635231600"; d="scan'208";a="232407970" 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="499389523" 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 18/32] selftests/sgx: Add test for TCS page permission changes Date: Mon, 7 Feb 2022 16:45:40 -0800 Message-Id: <6666b212b8ab33ad2c0546cd5dc42b8beb7a1605.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.0 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE,T_SCC_BODY_TEXT_LINE autolearn=no 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 Kernel should not allow permission changes on TCS pages. Add test to confirm this behavior. Signed-off-by: Reinette Chatre --- Changes since V1: - Adapt test to the kernel interface changes: the ioctl() name change and providing entire secinfo as parameter. - Rewrite error path to reduce line lengths. tools/testing/selftests/sgx/main.c | 74 ++++++++++++++++++++++++++++++ 1 file changed, 74 insertions(+) diff --git a/tools/testing/selftests/sgx/main.c b/tools/testing/selftests/sgx/main.c index 4f348ed1dc29..1398cd1b0983 100644 --- a/tools/testing/selftests/sgx/main.c +++ b/tools/testing/selftests/sgx/main.c @@ -121,6 +121,24 @@ static Elf64_Sym *vdso_symtab_get(struct vdso_symtab *symtab, const char *name) return NULL; } +/* + * Return the offset in the enclave where the TCS segment can be found. + * The first RW segment loaded is the TCS. + */ +static off_t encl_get_tcs_offset(struct encl *encl) +{ + int i; + + for (i = 0; i < encl->nr_segments; i++) { + struct encl_segment *seg = &encl->segment_tbl[i]; + + if (i == 0 && seg->prot == (PROT_READ | PROT_WRITE)) + return seg->offset; + } + + return -1; +} + /* * Return the offset in the enclave where the data segment can be found. * The first RW segment loaded is the TCS, skip that to get info on the @@ -567,6 +585,62 @@ TEST_F(enclave, pte_permissions) EXPECT_EQ(self->run.exception_addr, 0); } +/* + * Modifying permissions of TCS page should not be possible. + */ +TEST_F(enclave, tcs_permissions) +{ + struct sgx_enclave_restrict_perm ioc; + struct sgx_secinfo secinfo; + int ret, errno_save; + + ASSERT_TRUE(setup_test_encl(ENCL_HEAP_SIZE_DEFAULT, &self->encl, _metadata)); + + memset(&self->run, 0, sizeof(self->run)); + self->run.tcs = self->encl.encl_base; + + memset(&ioc, 0, sizeof(ioc)); + memset(&secinfo, 0, sizeof(secinfo)); + + /* + * Ensure kernel supports needed ioctl() and system supports needed + * commands. + */ + + ret = ioctl(self->encl.fd, SGX_IOC_ENCLAVE_RESTRICT_PERMISSIONS, &ioc); + errno_save = ret == -1 ? errno : 0; + + /* + * Invalid parameters were provided during sanity check, + * expect command to fail. + */ + ASSERT_EQ(ret, -1); + + /* ret == -1 */ + if (errno_save == ENOTTY) + SKIP(return, + "Kernel does not support SGX_IOC_ENCLAVE_RESTRICT_PERMISSIONS ioctl()"); + else if (errno_save == ENODEV) + SKIP(return, "System does not support SGX2"); + + /* + * Attempt to make TCS page read-only. This is not allowed and + * should be prevented by the kernel. + */ + secinfo.flags = PROT_READ; + ioc.offset = encl_get_tcs_offset(&self->encl); + ioc.length = PAGE_SIZE; + ioc.secinfo = (unsigned long)&secinfo; + + ret = ioctl(self->encl.fd, SGX_IOC_ENCLAVE_RESTRICT_PERMISSIONS, &ioc); + errno_save = ret == -1 ? errno : 0; + + EXPECT_EQ(ret, -1); + EXPECT_EQ(errno_save, EINVAL); + EXPECT_EQ(ioc.result, 0); + EXPECT_EQ(ioc.count, 0); +} + /* * Enclave page permission test. * -- 2.25.1