From patchwork Fri May 7 08:35:51 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 12244107 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2A4FBC433B4 for ; Fri, 7 May 2021 08:36:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E8B146145E for ; Fri, 7 May 2021 08:36:05 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236369AbhEGIhD (ORCPT ); Fri, 7 May 2021 04:37:03 -0400 Received: from mail.kernel.org ([198.145.29.99]:35180 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236356AbhEGIhC (ORCPT ); Fri, 7 May 2021 04:37:02 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 5206860FDC; Fri, 7 May 2021 08:36:02 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1620376562; bh=R0O9/+3NWKcgkdhHuXxWuRHSC7X1XKa4b11RA5ahgDo=; h=From:To:Cc:Subject:Date:From; b=WOYBK97iEwRiLGAnba0vqCoVQDIFHQgSo1ZyTWwl7CLTGoc4j9IfJHhWBr8uuw/qq YjzC6hKRhvNWgiUbSZPFP16M8HlZpm3oGQot3DG/x28p8SMO8KwQtdB4apHLZu91NV KXVSDBrT0YpgD+ysRzLbOP6GftyMHfuv60IlP2Tlv5GYRDWthjKtd90dEYOiqwag99 PzOSKQA69aucIcKtmLVpII+838qaQHmd9/r1BfnwOKrdeG4NmgjaqZkUBVTGKbo1sk /w1vr6LQP7Miy17ZxbX3oJ/PF49C6ceMIE4Rq+gTXK27zQn+EOKpzGaQCGE5RVj+yg We3DEl27rrYoQ== From: Jarkko Sakkinen To: Shuah Khan Cc: linux-sgx@vger.kernel.org, dave.hansen@intel.com, Jarkko Sakkinen , Dave Hansen , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 1/2] selftests/sgx: Rename 'eenter' and 'sgx_call_vdso' Date: Fri, 7 May 2021 11:35:51 +0300 Message-Id: <20210507083552.6832-1-jarkko@kernel.org> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Rename symbols for better clarity: * 'eenter' -> 'vdso_sgx_enter_enclave' * 'sgx_call_vdso' -> 'sgx_enter_enclave' Signed-off-by: Jarkko Sakkinen --- v2: Refined thh renames just a bit. tools/testing/selftests/sgx/call.S | 6 +++--- tools/testing/selftests/sgx/main.c | 25 +++++++++++++------------ tools/testing/selftests/sgx/main.h | 4 ++-- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/tools/testing/selftests/sgx/call.S b/tools/testing/selftests/sgx/call.S index 4ecadc7490f4..b09a25890f3b 100644 --- a/tools/testing/selftests/sgx/call.S +++ b/tools/testing/selftests/sgx/call.S @@ -5,8 +5,8 @@ .text - .global sgx_call_vdso -sgx_call_vdso: + .global sgx_enter_enclave +sgx_enter_enclave: .cfi_startproc push %r15 .cfi_adjust_cfa_offset 8 @@ -27,7 +27,7 @@ sgx_call_vdso: .cfi_adjust_cfa_offset 8 push 0x38(%rsp) .cfi_adjust_cfa_offset 8 - call *eenter(%rip) + call *vdso_sgx_enter_enclave(%rip) add $0x10, %rsp .cfi_adjust_cfa_offset -0x10 pop %rbx diff --git a/tools/testing/selftests/sgx/main.c b/tools/testing/selftests/sgx/main.c index d304a4044eb9..43da68388e25 100644 --- a/tools/testing/selftests/sgx/main.c +++ b/tools/testing/selftests/sgx/main.c @@ -21,7 +21,7 @@ #include "../kselftest.h" static const uint64_t MAGIC = 0x1122334455667788ULL; -vdso_sgx_enter_enclave_t eenter; +vdso_sgx_enter_enclave_t vdso_sgx_enter_enclave; struct vdso_symtab { Elf64_Sym *elf_symtab; @@ -149,7 +149,7 @@ int main(int argc, char *argv[]) { struct sgx_enclave_run run; struct vdso_symtab symtab; - Elf64_Sym *eenter_sym; + Elf64_Sym *sgx_enter_enclave_sym; uint64_t result = 0; struct encl encl; unsigned int i; @@ -194,29 +194,30 @@ int main(int argc, char *argv[]) if (!vdso_get_symtab(addr, &symtab)) goto err; - eenter_sym = vdso_symtab_get(&symtab, "__vdso_sgx_enter_enclave"); - if (!eenter_sym) + sgx_enter_enclave_sym = vdso_symtab_get(&symtab, "__vdso_sgx_enter_enclave"); + if (!sgx_enter_enclave_sym) goto err; - eenter = addr + eenter_sym->st_value; + vdso_sgx_enter_enclave = addr + sgx_enter_enclave_sym->st_value; - ret = sgx_call_vdso((void *)&MAGIC, &result, 0, EENTER, NULL, NULL, &run); - if (!report_results(&run, ret, result, "sgx_call_vdso")) + ret = sgx_enter_enclave((void *)&MAGIC, &result, 0, EENTER, + NULL, NULL, &run); + if (!report_results(&run, ret, result, "sgx_enter_enclave_unclobbered")) goto err; /* Invoke the vDSO directly. */ result = 0; - ret = eenter((unsigned long)&MAGIC, (unsigned long)&result, 0, EENTER, - 0, 0, &run); - if (!report_results(&run, ret, result, "eenter")) + ret = vdso_sgx_enter_enclave((unsigned long)&MAGIC, (unsigned long)&result, + 0, EENTER, 0, 0, &run); + if (!report_results(&run, ret, result, "sgx_enter_enclave")) goto err; /* And with an exit handler. */ run.user_handler = (__u64)user_handler; run.user_data = 0xdeadbeef; - ret = eenter((unsigned long)&MAGIC, (unsigned long)&result, 0, EENTER, - 0, 0, &run); + ret = vdso_sgx_enter_enclave((unsigned long)&MAGIC, (unsigned long)&result, + 0, EENTER, 0, 0, &run); if (!report_results(&run, ret, result, "user_handler")) goto err; diff --git a/tools/testing/selftests/sgx/main.h b/tools/testing/selftests/sgx/main.h index 67211a708f04..68672fd86cf9 100644 --- a/tools/testing/selftests/sgx/main.h +++ b/tools/testing/selftests/sgx/main.h @@ -35,7 +35,7 @@ bool encl_load(const char *path, struct encl *encl); bool encl_measure(struct encl *encl); bool encl_build(struct encl *encl); -int sgx_call_vdso(void *rdi, void *rsi, long rdx, u32 function, void *r8, void *r9, - struct sgx_enclave_run *run); +int sgx_enter_enclave(void *rdi, void *rsi, long rdx, u32 function, void *r8, void *r9, + struct sgx_enclave_run *run); #endif /* MAIN_H */ From patchwork Fri May 7 08:35:52 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jarkko Sakkinen X-Patchwork-Id: 12244109 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-19.7 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,USER_AGENT_GIT autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E64B9C433ED for ; Fri, 7 May 2021 08:36:10 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id C0EE26113E for ; Fri, 7 May 2021 08:36:10 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S236392AbhEGIhJ (ORCPT ); Fri, 7 May 2021 04:37:09 -0400 Received: from mail.kernel.org ([198.145.29.99]:35292 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236347AbhEGIhF (ORCPT ); Fri, 7 May 2021 04:37:05 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 023D760FDC; Fri, 7 May 2021 08:36:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1620376565; bh=I+qN+0aqIyLTx4KBomCn8E0G7kBDY8z+bAk62yK5pIY=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=cmH+prl+5ntKjNuny4tzsesY56MjuDqD0wKQ1rjtaes53SQhgF800YP0AVyUiT7Yt Jp2T92BQC2mOp5TgfXyZ/0gaqDB33GejHBJ2QN4k7B+AKYG+9xuKyQ3KtY3Pblaxvo Naz79bv9DOeJAnvyHZy04hMTMGKFBFNUiSJ2ZSksmqDuPwhnYxwJf1lw05hyD2YEk/ VdMH5adblafLzNAuDyUYEOHN1jLKRxLCh4M8lARvYktOAr6S0y9czGCSveyeczB9VF 6uGoVGHTeamNxM0BeAU+YhngPkAuFKlsdn2v8DMkl8+kedighCLbg1qazytO2ikQgz GRqGHiEhbhpxA== From: Jarkko Sakkinen To: Shuah Khan Cc: linux-sgx@vger.kernel.org, dave.hansen@intel.com, Jarkko Sakkinen , Dave Hansen , linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3 2/2] selftests/sgx: Migrate to kselftest harness Date: Fri, 7 May 2021 11:35:52 +0300 Message-Id: <20210507083552.6832-2-jarkko@kernel.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210507083552.6832-1-jarkko@kernel.org> References: <20210507083552.6832-1-jarkko@kernel.org> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-kselftest@vger.kernel.org Migrate to kselftest harness so that the kselftest framework can probably count and report passed and failed tests. Signed-off-by: Jarkko Sakkinen --- v3: * Use helper macros. v2: * Add the missing string argument to ksft_test_result_pass() and ksft_test_result_fail() calls. tools/testing/selftests/sgx/main.c | 120 +++++++++++++++++------------ 1 file changed, 71 insertions(+), 49 deletions(-) diff --git a/tools/testing/selftests/sgx/main.c b/tools/testing/selftests/sgx/main.c index 43da68388e25..4622dedcd290 100644 --- a/tools/testing/selftests/sgx/main.c +++ b/tools/testing/selftests/sgx/main.c @@ -18,7 +18,7 @@ #include #include "defines.h" #include "main.h" -#include "../kselftest.h" +#include "../kselftest_harness.h" static const uint64_t MAGIC = 0x1122334455667788ULL; vdso_sgx_enter_enclave_t vdso_sgx_enter_enclave; @@ -107,31 +107,27 @@ static Elf64_Sym *vdso_symtab_get(struct vdso_symtab *symtab, const char *name) return NULL; } -bool report_results(struct sgx_enclave_run *run, int ret, uint64_t result, - const char *test) +bool is_test_passed(struct sgx_enclave_run *run, int ret, uint64_t result) { bool valid = true; if (ret) { - printf("FAIL: %s() returned: %d\n", test, ret); + ksft_print_msg("ret = %d\n", ret); valid = false; } if (run->function != EEXIT) { - printf("FAIL: %s() function, expected: %u, got: %u\n", test, EEXIT, - run->function); + ksft_print_msg("run->function: expected: %u, got: %u\n", EEXIT, run->function); valid = false; } if (result != MAGIC) { - printf("FAIL: %s(), expected: 0x%lx, got: 0x%lx\n", test, MAGIC, - result); + ksft_print_msg("result: expected: 0x%lx, got: 0x%lx\n", MAGIC, result); valid = false; } if (run->user_data) { - printf("FAIL: %s() user data, expected: 0x0, got: 0x%llx\n", - test, run->user_data); + ksft_print_msg("run->user_data:expected: 0x0, got: 0x%llx\n", run->user_data); valid = false; } @@ -145,47 +141,42 @@ static int user_handler(long rdi, long rsi, long rdx, long ursp, long r8, long r return 0; } -int main(int argc, char *argv[]) +FIXTURE(enclave) { + struct encl encl; +}; + +FIXTURE_SETUP(enclave) { - struct sgx_enclave_run run; + Elf64_Sym *sgx_enter_enclave_sym = NULL; struct vdso_symtab symtab; - Elf64_Sym *sgx_enter_enclave_sym; - uint64_t result = 0; - struct encl encl; unsigned int i; void *addr; - int ret; - memset(&run, 0, sizeof(run)); - - if (!encl_load("test_encl.elf", &encl)) { - encl_delete(&encl); + if (!encl_load("test_encl.elf", &self->encl)) { + encl_delete(&self->encl); ksft_exit_skip("cannot load enclaves\n"); } - if (!encl_measure(&encl)) + if (!encl_measure(&self->encl)) goto err; - if (!encl_build(&encl)) + if (!encl_build(&self->encl)) goto err; /* * An enclave consumer only must do this. */ - for (i = 0; i < encl.nr_segments; i++) { - struct encl_segment *seg = &encl.segment_tbl[i]; + for (i = 0; i < self->encl.nr_segments; i++) { + struct encl_segment *seg = &self->encl.segment_tbl[i]; - addr = mmap((void *)encl.encl_base + seg->offset, seg->size, - seg->prot, MAP_SHARED | MAP_FIXED, encl.fd, 0); + addr = mmap((void *)self->encl.encl_base + seg->offset, seg->size, + seg->prot, MAP_SHARED | MAP_FIXED, self->encl.fd, 0); if (addr == MAP_FAILED) { - perror("mmap() segment failed"); - exit(KSFT_FAIL); + ksft_print_msg("mmap() segment: %s", strerror(errno)); + goto err; } } - memset(&run, 0, sizeof(run)); - run.tcs = encl.encl_base; - /* Get vDSO base address */ addr = (void *)getauxval(AT_SYSINFO_EHDR); if (!addr) @@ -200,32 +191,63 @@ int main(int argc, char *argv[]) vdso_sgx_enter_enclave = addr + sgx_enter_enclave_sym->st_value; - ret = sgx_enter_enclave((void *)&MAGIC, &result, 0, EENTER, - NULL, NULL, &run); - if (!report_results(&run, ret, result, "sgx_enter_enclave_unclobbered")) - goto err; +err: + if (!sgx_enter_enclave_sym) + encl_delete(&self->encl); + + ASSERT_NE(sgx_enter_enclave_sym, NULL); +} + +FIXTURE_TEARDOWN(enclave) +{ + encl_delete(&self->encl); + vdso_sgx_enter_enclave = NULL; +} + + +TEST_F(enclave, unclobbered_vdso) +{ + struct sgx_enclave_run run; + uint64_t result = 0; + int ret; + + memset(&run, 0, sizeof(run)); + + ret = sgx_enter_enclave((void *)&MAGIC, &result, 0, EENTER, NULL, NULL, &run); + ASSERT_EQ(true, is_test_passed(&run, ret, result)); +} + +TEST_F(enclave, clobbered_vdso) +{ + struct sgx_enclave_run run; + uint64_t result = 0; + int ret; + + memset(&run, 0, sizeof(run)); - /* Invoke the vDSO directly. */ - result = 0; ret = vdso_sgx_enter_enclave((unsigned long)&MAGIC, (unsigned long)&result, 0, EENTER, 0, 0, &run); - if (!report_results(&run, ret, result, "sgx_enter_enclave")) - goto err; - /* And with an exit handler. */ + + ASSERT_EQ(true, is_test_passed(&run, ret, result)); +} + +TEST_F(enclave, clobbered_vdso_and_user_function) +{ + struct sgx_enclave_run run; + uint64_t result = 0; + int ret; + + memset(&run, 0, sizeof(run)); + run.user_handler = (__u64)user_handler; run.user_data = 0xdeadbeef; + ret = vdso_sgx_enter_enclave((unsigned long)&MAGIC, (unsigned long)&result, 0, EENTER, 0, 0, &run); - if (!report_results(&run, ret, result, "user_handler")) - goto err; - printf("SUCCESS\n"); - encl_delete(&encl); - exit(KSFT_PASS); - -err: - encl_delete(&encl); - exit(KSFT_FAIL); + ASSERT_EQ(true, is_test_passed(&run, ret, result)); } + +TEST_HARNESS_MAIN