From patchwork Fri Jan 28 18:23:56 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Reinette Chatre X-Patchwork-Id: 12728921 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 09DE9C43217 for ; Fri, 28 Jan 2022 18:24:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347466AbiA1SYY (ORCPT ); Fri, 28 Jan 2022 13:24:24 -0500 Received: from mga11.intel.com ([192.55.52.93]:26182 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343789AbiA1SYX (ORCPT ); Fri, 28 Jan 2022 13:24:23 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1643394263; x=1674930263; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=F0TN1kp9SPJ+PH0r0h5Xs9kk0gE4PkwwlTkbFb+O0rs=; b=QOnSskvZcAQtdgk9LxCZ3qGYmXN2TBZ8Bs5JyImuseRfEz9oUbJh9uOh 8CoLN+I1nJosvWug4ijNLhPtBztdsx8UCO7y29bOwVlZ6MRKfxFKwnTP1 pfbpdlvVp47xR1Pxl/9g9bgG9FtPsVR0TitsILL1u10i1NtLbNzmWkURi NNqlFS5jUu5f1waTwk4AGn7UoHRsDlNdyUAoRzMjiTM35vmrx+Tiwtrxc xqNa9dcBSg9TXKNjiuN/YbQmgLmXQiaIeFYpWsmuvIRcqagsYt729WlKc 4vaVN/UXe/2sWYe7iF0BbOWmLLdlKGmix39Nx4V7c6c3R750eZbz0OhPX g==; X-IronPort-AV: E=McAfee;i="6200,9189,10240"; a="244773366" X-IronPort-AV: E=Sophos;i="5.88,324,1635231600"; d="scan'208";a="244773366" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Jan 2022 10:24:23 -0800 X-IronPort-AV: E=Sophos;i="5.88,324,1635231600"; d="scan'208";a="581930428" Received: from rchatre-ws.ostc.intel.com ([10.54.69.144]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Jan 2022 10:24:23 -0800 From: Reinette Chatre To: jarkko@kernel.org, dave.hansen@linux.intel.com, linux-sgx@vger.kernel.org, shuah@kernel.org Cc: linux-kselftest@vger.kernel.org Subject: [PATCH 1/4] selftests/sgx: Fix segfault upon early test failure Date: Fri, 28 Jan 2022 10:23:56 -0800 Message-Id: <3c1d84724ecc7c94131ba1d94dc4c5de5aafc58f.1643393473.git.reinette.chatre@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sgx@vger.kernel.org A segfault is encountered if there happens to be an early failure of any of the SGX tests. One way to reproduce this is to remove the enclave binary "test_encl.elf" that will trigger early enclave loading failure followed by a segfault. The segfault occurs within encl_delete() that cleans up after an enclave by umapping its mapped regions and closing the file descriptor to the SGX driver. As integrated with the kselftest harness encl_delete() is called upon exit from every test, irrespective of test success. encl_delete() is also called to clean up if an error is encountered during enclave loading. encl_delete() is thus responsible for cleaning any amount of enclave state - including state that has already been cleaned. encl_delete() starts by accessing encl->segment_tbl that may not have been created yet due to a very early failure or may already be cleaned up because of a failure encountered after encl->segment_tbl was created. Ensure encl->segment_tbl is valid before attempting to access memory offset from it. The offset with which it is accessed, encl->nr_segments, is initialized after encl->segment_tbl and thus considered valid to use after the encl->segment_tbl check succeeds. Fixes: 3200505d4de6 ("selftests/sgx: Create a heap for the test enclave") Signed-off-by: Reinette Chatre Reviewed-by: Jarkko Sakkinen Tested-by: Jarkko Sakkinen --- tools/testing/selftests/sgx/load.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/sgx/load.c b/tools/testing/selftests/sgx/load.c index 9d4322c946e2..006b464c8fc9 100644 --- a/tools/testing/selftests/sgx/load.c +++ b/tools/testing/selftests/sgx/load.c @@ -21,7 +21,7 @@ void encl_delete(struct encl *encl) { - struct encl_segment *heap_seg = &encl->segment_tbl[encl->nr_segments - 1]; + struct encl_segment *heap_seg; if (encl->encl_base) munmap((void *)encl->encl_base, encl->encl_size); @@ -32,10 +32,11 @@ void encl_delete(struct encl *encl) if (encl->fd) close(encl->fd); - munmap(heap_seg->src, heap_seg->size); - - if (encl->segment_tbl) + if (encl->segment_tbl) { + heap_seg = &encl->segment_tbl[encl->nr_segments - 1]; + munmap(heap_seg->src, heap_seg->size); free(encl->segment_tbl); + } memset(encl, 0, sizeof(*encl)); } From patchwork Fri Jan 28 18:23:57 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Reinette Chatre X-Patchwork-Id: 12728922 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 36093C43219 for ; Fri, 28 Jan 2022 18:24:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1343789AbiA1SYY (ORCPT ); Fri, 28 Jan 2022 13:24:24 -0500 Received: from mga11.intel.com ([192.55.52.93]:26182 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S235646AbiA1SYY (ORCPT ); Fri, 28 Jan 2022 13:24:24 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1643394264; x=1674930264; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=/9R+QpM+RrNxzn+MKgSfGWS4ATwl6dwfeXykwHwBvBE=; b=Ki/vZXI2Ry37btoEnYNs0MIiCx6nODjkDNrb32+CQ345aSTl2ObY1b4g XHWisHRmGZPM9X0EGRHo8DpJ7kkYYGbgNZCUXPKAgiCQs4jbi/Hw5Fg4I axUdTrsYi/Hq+aue7JOh1RILHrAVGcvhgaFZGRuFkgUkELma8ZdK9AL3g WTCIk4DEo1A39GIlUWtCtjtxHyPcaTHzcTxSDh8jqm41O+dgq1zS5CGbM lBA+wVJqYZaUMBwuQUTA3jkTnp5qJur+sI5IOEbcCqiMg/+xx/93Lj9y8 j+PQD3OS84BgVmY6bRCTUI57/9bZI9+Ios4w9iKW0WiFHk6jkHqlUeg76 Q==; X-IronPort-AV: E=McAfee;i="6200,9189,10240"; a="244773367" X-IronPort-AV: E=Sophos;i="5.88,324,1635231600"; d="scan'208";a="244773367" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Jan 2022 10:24:23 -0800 X-IronPort-AV: E=Sophos;i="5.88,324,1635231600"; d="scan'208";a="581930430" Received: from rchatre-ws.ostc.intel.com ([10.54.69.144]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Jan 2022 10:24:23 -0800 From: Reinette Chatre To: jarkko@kernel.org, dave.hansen@linux.intel.com, linux-sgx@vger.kernel.org, shuah@kernel.org Cc: linux-kselftest@vger.kernel.org Subject: [PATCH 2/4] selftests/sgx: Do not attempt enclave build without valid enclave Date: Fri, 28 Jan 2022 10:23:57 -0800 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sgx@vger.kernel.org It is not possible to build an enclave if it was not possible to load the binary from which it should be constructed. Do not attempt to make further progress but instead return with failure. Fixes: 1b35eb719549 ("selftests/sgx: Encpsulate the test enclave creation") Signed-off-by: Reinette Chatre Acked-by: Dave Hansen Reviewed-by: Jarkko Sakkinen --- tools/testing/selftests/sgx/main.c | 1 + 1 file changed, 1 insertion(+) diff --git a/tools/testing/selftests/sgx/main.c b/tools/testing/selftests/sgx/main.c index 370c4995f7c4..a7cd2c3e6f7e 100644 --- a/tools/testing/selftests/sgx/main.c +++ b/tools/testing/selftests/sgx/main.c @@ -147,6 +147,7 @@ static bool setup_test_encl(unsigned long heap_size, struct encl *encl, if (!encl_load("test_encl.elf", encl, heap_size)) { encl_delete(encl); TH_LOG("Failed to load the test enclave.\n"); + return false; } if (!encl_measure(encl)) From patchwork Fri Jan 28 18:23:58 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Reinette Chatre X-Patchwork-Id: 12728924 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 9E520C4167D for ; Fri, 28 Jan 2022 18:24:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347999AbiA1SYZ (ORCPT ); Fri, 28 Jan 2022 13:24:25 -0500 Received: from mga11.intel.com ([192.55.52.93]:26182 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346536AbiA1SYY (ORCPT ); Fri, 28 Jan 2022 13:24:24 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1643394264; x=1674930264; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=R/PUqiKdHwxgbFpofEH5q7cvMjoCYoa9qFbdKYcfqqY=; b=D94Ve+JNyrzzzecEVhB4V6qUdyCH/dpE4w/Y4nQp4gt758a9nPtQMKyC 0hnrJ6nxVZJNKaCnhdEuK9gJOMI8ZMHKWd/MHk/uuo+GVsVgkB0v7IQuJ 4YPwL6X+FRuA4pzeW772QIX343qlPXoJq8tyDZSXZ/poqNRVohmkh6bOk mpQpzbB5NNZFvHEnJCHW7r0lUU3BhS+IW7YLSQf+GTftAFCdgTGo2EhwO TH0UiE4wxYegN4cJNUB/UtB3lktz8DSjBG032EYk1voFo7I29ALC+iQMm UEDsg/Br6Qmexnr4h5Np8AAcYpmnBjff6gbEPYLDX0sI83WCAo/fo6x1m Q==; X-IronPort-AV: E=McAfee;i="6200,9189,10240"; a="244773368" X-IronPort-AV: E=Sophos;i="5.88,324,1635231600"; d="scan'208";a="244773368" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Jan 2022 10:24:23 -0800 X-IronPort-AV: E=Sophos;i="5.88,324,1635231600"; d="scan'208";a="581930431" Received: from rchatre-ws.ostc.intel.com ([10.54.69.144]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Jan 2022 10:24:23 -0800 From: Reinette Chatre To: jarkko@kernel.org, dave.hansen@linux.intel.com, linux-sgx@vger.kernel.org, shuah@kernel.org Cc: linux-kselftest@vger.kernel.org Subject: [PATCH 3/4] selftests/sgx: Ensure enclave data available during debug print Date: Fri, 28 Jan 2022 10:23:58 -0800 Message-Id: X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sgx@vger.kernel.org In support of debugging the SGX tests print details from the enclave and its memory mappings if any failure is encountered during enclave loading. When a failure is encountered no data is printed because the printing of the data is preceded by cleanup of the data. Move the data cleanup after the data print. Fixes: 147172148909 ("selftests/sgx: Dump segments and /proc/self/maps only on failure") Signed-off-by: Reinette Chatre Reviewed-by: Jarkko Sakkinen --- tools/testing/selftests/sgx/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/sgx/main.c b/tools/testing/selftests/sgx/main.c index a7cd2c3e6f7e..b0bd95a4730d 100644 --- a/tools/testing/selftests/sgx/main.c +++ b/tools/testing/selftests/sgx/main.c @@ -186,8 +186,6 @@ static bool setup_test_encl(unsigned long heap_size, struct encl *encl, return true; err: - encl_delete(encl); - for (i = 0; i < encl->nr_segments; i++) { seg = &encl->segment_tbl[i]; @@ -208,6 +206,8 @@ static bool setup_test_encl(unsigned long heap_size, struct encl *encl, TH_LOG("Failed to initialize the test enclave.\n"); + encl_delete(encl); + return false; } From patchwork Fri Jan 28 18:23:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Reinette Chatre X-Patchwork-Id: 12728923 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 61E56C4167B for ; Fri, 28 Jan 2022 18:24:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1347971AbiA1SYZ (ORCPT ); Fri, 28 Jan 2022 13:24:25 -0500 Received: from mga11.intel.com ([192.55.52.93]:26185 "EHLO mga11.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347038AbiA1SYY (ORCPT ); Fri, 28 Jan 2022 13:24:24 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1643394264; x=1674930264; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=Ep1DSR67k0vTlYBwdxQUNSBLuOH3FnHrf9E0uu6Gkqg=; b=L+bMcgHarDtqqg7LqLHdh+IcMmoSs7ez0O5+m00fQ9kFiZpm8114exAa 87tZS+OTqL0FBQuN4cG28MwC6rA7ZmjG16F3Fe3Uswzn4C/MDjo4viEZg NKQ+3r0PT1sJAjA74vzR9GgPoEtZ7MJ+OQc7XJ5sglfZmkHfj6Lyy4Bip /i3B+B3JDqIS0+LO1xlNDirqRCNnykQMSZbN9qv8YZ2B9cI8UCLq0Uo/P V5YyQxKVfkCauXRQ9hXJcSqE28JBHQKJygXO+95+2Dvhzo0NcNxdoDZt1 UEFh8RK0cga1n9vtEtGqjfOLsQmSR9voovImiWER5EIKoKMeZ2Wjtkvyj A==; X-IronPort-AV: E=McAfee;i="6200,9189,10240"; a="244773369" X-IronPort-AV: E=Sophos;i="5.88,324,1635231600"; d="scan'208";a="244773369" Received: from fmsmga008.fm.intel.com ([10.253.24.58]) by fmsmga102.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Jan 2022 10:24:23 -0800 X-IronPort-AV: E=Sophos;i="5.88,324,1635231600"; d="scan'208";a="581930432" Received: from rchatre-ws.ostc.intel.com ([10.54.69.144]) by fmsmga008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 28 Jan 2022 10:24:23 -0800 From: Reinette Chatre To: jarkko@kernel.org, dave.hansen@linux.intel.com, linux-sgx@vger.kernel.org, shuah@kernel.org Cc: linux-kselftest@vger.kernel.org Subject: [PATCH 4/4] selftests/sgx: Remove extra newlines in test output Date: Fri, 28 Jan 2022 10:23:59 -0800 Message-Id: <699e4e1382e005386722bc296602d1b270a28b1e.1643393473.git.reinette.chatre@intel.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: References: MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-sgx@vger.kernel.org The TH_LOG() macro is an optional debug logging function made available by kselftest itself. When TH_LOG_ENABLED is set it prints the provided message with additional information and formatting that already includes a newline. Providing a newline to the message printed by TH_LOG() results in a double newline that produces irregular test output. Remove the unnecessary newlines from the text provided to TH_LOG(). Fixes: 1b35eb719549 ("selftests/sgx: Encpsulate the test enclave creation") Signed-off-by: Reinette Chatre Acked-by: Dave Hansen Reviewed-by: Jarkko Sakkinen --- tools/testing/selftests/sgx/main.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/testing/selftests/sgx/main.c b/tools/testing/selftests/sgx/main.c index b0bd95a4730d..dd74fa42302e 100644 --- a/tools/testing/selftests/sgx/main.c +++ b/tools/testing/selftests/sgx/main.c @@ -146,7 +146,7 @@ static bool setup_test_encl(unsigned long heap_size, struct encl *encl, if (!encl_load("test_encl.elf", encl, heap_size)) { encl_delete(encl); - TH_LOG("Failed to load the test enclave.\n"); + TH_LOG("Failed to load the test enclave."); return false; } @@ -204,7 +204,7 @@ static bool setup_test_encl(unsigned long heap_size, struct encl *encl, fclose(maps_file); } - TH_LOG("Failed to initialize the test enclave.\n"); + TH_LOG("Failed to initialize the test enclave."); encl_delete(encl);