From nobody Sun Apr 19 02:22:21 2026 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 00F1CC43334 for ; Thu, 7 Jul 2022 18:28:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236261AbiGGS2K (ORCPT ); Thu, 7 Jul 2022 14:28:10 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53780 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236398AbiGGS1n (ORCPT ); Thu, 7 Jul 2022 14:27:43 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3CE785C944; Thu, 7 Jul 2022 11:27:39 -0700 (PDT) Date: Thu, 07 Jul 2022 18:27:36 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1657218457; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zcWcQxJpH0CAkTdtJIPGFmesdUp3saKALgRAD6oHVZY=; b=kdIl3OCLPiIDneyRdZypcB16XWKPBd7vdBU+AFjU1t9pmociKv7ZN0D8sUQW++TmlMiR4S QFU2p6uQgMpr0cfQS3phLgpmeP5gZXKMtqqo6rL6ErGicOMS05zzLzzNIhWsc5D23LmwbL XLsnxf47y/84Bp/zDlJ5Vrq6a7SVOyCAU4GE+9dc9MJp7qHUldQIDCTs4aYtBKr8Zo0qrB YuHIBp8q/h/mDXYPnKMF0AnUN1Ub5ua7m8wNHwU8SgqUq8+CYep/CdWHK4Yx4P+vcvXYJN XqB0MNf8tGSSfRhMnYacKny7c/oKirp9018iOr4FFQ5hc57JeiVvpiae4udOhQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1657218457; h=from:from:sender:sender:reply-to:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=zcWcQxJpH0CAkTdtJIPGFmesdUp3saKALgRAD6oHVZY=; b=uuZ9mp/MmeC+N6p40V7qFaegPR5HVk8hixDBE+Y+rxXh/osEixPJxU5H+QLL0tCCllTOU3 GytXtXFfgByo5vCQ== From: "tip-bot2 for Reinette Chatre" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/sgx] selftests/sgx: Add test for TCS page permission changes Cc: Reinette Chatre , Dave Hansen , Jarkko Sakkinen , x86@kernel.org, linux-kernel@vger.kernel.org In-Reply-To: =?utf-8?q?=3C0121ad1b21befb94519072e2c18b89aa5dca00d4=2E16521?= =?utf-8?q?37848=2Egit=2Ereinette=2Echatre=40intel=2Ecom=3E?= References: =?utf-8?q?=3C0121ad1b21befb94519072e2c18b89aa5dca00d4=2E165213?= =?utf-8?q?7848=2Egit=2Ereinette=2Echatre=40intel=2Ecom=3E?= MIME-Version: 1.0 Message-ID: <165721845674.15455.2677093217380111436.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The following commit has been merged into the x86/sgx branch of tip: Commit-ID: 7088c81f94733fd5d103f8975d5e1d1fad12f665 Gitweb: https://git.kernel.org/tip/7088c81f94733fd5d103f8975d5e1d1fa= d12f665 Author: Reinette Chatre AuthorDate: Tue, 10 May 2022 11:08:59 -07:00 Committer: Dave Hansen CommitterDate: Thu, 07 Jul 2022 10:13:04 -07:00 selftests/sgx: Add test for TCS page permission changes Kernel should not allow permission changes on TCS pages. Add test to confirm this behavior. Signed-off-by: Reinette Chatre Signed-off-by: Dave Hansen Acked-by: Jarkko Sakkinen Link: https://lkml.kernel.org/r/0121ad1b21befb94519072e2c18b89aa5dca00d4.16= 52137848.git.reinette.chatre@intel.com --- tools/testing/selftests/sgx/main.c | 71 +++++++++++++++++++++++++++++- 1 file changed, 71 insertions(+) diff --git a/tools/testing/selftests/sgx/main.c b/tools/testing/selftests/s= gx/main.c index 46eac09..016ae3e 100644 --- a/tools/testing/selftests/sgx/main.c +++ b/tools/testing/selftests/sgx/main.c @@ -122,6 +122,24 @@ static Elf64_Sym *vdso_symtab_get(struct vdso_symtab *= symtab, const char *name) } =20 /* + * 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 =3D 0; i < encl->nr_segments; i++) { + struct encl_segment *seg =3D &encl->segment_tbl[i]; + + if (i =3D=3D 0 && seg->prot =3D=3D (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 * data segment. @@ -568,6 +586,59 @@ TEST_F(enclave, pte_permissions) } =20 /* + * Modifying permissions of TCS page should not be possible. + */ +TEST_F(enclave, tcs_permissions) +{ + struct sgx_enclave_restrict_permissions ioc; + int ret, errno_save; + + ASSERT_TRUE(setup_test_encl(ENCL_HEAP_SIZE_DEFAULT, &self->encl, _metadat= a)); + + memset(&self->run, 0, sizeof(self->run)); + self->run.tcs =3D self->encl.encl_base; + + memset(&ioc, 0, sizeof(ioc)); + + /* + * Ensure kernel supports needed ioctl() and system supports needed + * commands. + */ + + ret =3D ioctl(self->encl.fd, SGX_IOC_ENCLAVE_RESTRICT_PERMISSIONS, &ioc); + errno_save =3D ret =3D=3D -1 ? errno : 0; + + /* + * Invalid parameters were provided during sanity check, + * expect command to fail. + */ + ASSERT_EQ(ret, -1); + + /* ret =3D=3D -1 */ + if (errno_save =3D=3D ENOTTY) + SKIP(return, + "Kernel does not support SGX_IOC_ENCLAVE_RESTRICT_PERMISSIONS ioctl= ()"); + else if (errno_save =3D=3D 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. + */ + ioc.offset =3D encl_get_tcs_offset(&self->encl); + ioc.length =3D PAGE_SIZE; + ioc.permissions =3D SGX_SECINFO_R; + + ret =3D ioctl(self->encl.fd, SGX_IOC_ENCLAVE_RESTRICT_PERMISSIONS, &ioc); + errno_save =3D ret =3D=3D -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. * * Modify and restore enclave page's EPCM (enclave) permissions from