From patchwork Thu Jan 19 16:03:44 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Joey Gouly X-Patchwork-Id: 13108326 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 bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D2EC1C004D4 for ; Thu, 19 Jan 2023 16:06:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20210309; h=Sender: Content-Transfer-Encoding:Content-Type:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Message-ID:Date:Subject:CC:To:From:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=zKVbm9C0WKI9c7Pbi4lMAIlf6GyJbfE649g8X5AFf/I=; b=vdWZNwYN7r1YOh sUsh+CSVu6xQI2jtPUwX3MnBhmHlsWOYt9PmY9wqj9BWVM/S1euq1bku4AbrDAiwYtt1SrBowWlp2 uPT+GAMyYlJQY8aQFRCGW8PSXRyfYDGX6hHcYplZ21l0S071OsH8keCVI3PTqFHkcKofcsDJbMfxF NtFhUqgqneNPKBq7uAglfznOXRvbXAsaSKUfvJKIMlQ1i6wCXoL/InrxCWLGQKAUss7keAnOXEKQR xzUS/3CKnc4E5pZqzE8WYWMnsXp9ZuoaoKvx0SwPJXxw6glZ7zE3XNf8BiYHdXMQk1L0QvI39s3tO nmJH+5wA7dwEV08PuLlw==; Received: from localhost ([::1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.94.2 #2 (Red Hat Linux)) id 1pIXPo-005q6K-0A; Thu, 19 Jan 2023 16:05:24 +0000 Received: from mail-vi1eur05on2067.outbound.protection.outlook.com ([40.107.21.67] helo=EUR05-VI1-obe.outbound.protection.outlook.com) by bombadil.infradead.org with esmtps (Exim 4.94.2 #2 (Red Hat Linux)) id 1pIXPW-005pw7-Dc for linux-arm-kernel@lists.infradead.org; Thu, 19 Jan 2023 16:05:08 +0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; s=selector2-armh-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=TVqUovDaP0Eis0yf04LEyuGnbnC1xSyDtOGIyaojtyA=; b=V73VpP7Z56d4ftzhBcoPMGZ9SLcgx2v+yQRa3sVGW8+g+RNr3WXtcxzKfKEopOaf6/fA+oVFIJNkeDcVD8WlzEhCGDQTPC3x2omvjJtzgb1TPbzDurgNIOmdIMzQOHPIJc71uAF/aBhPajxfTksFhirqFMOVcl19qLOdkP2qfWo= Received: from DU2PR04CA0302.eurprd04.prod.outlook.com (2603:10a6:10:2b5::7) by AS8PR08MB7306.eurprd08.prod.outlook.com (2603:10a6:20b:441::16) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.12; Thu, 19 Jan 2023 16:04:49 +0000 Received: from DBAEUR03FT033.eop-EUR03.prod.protection.outlook.com (2603:10a6:10:2b5:cafe::1d) by DU2PR04CA0302.outlook.office365.com (2603:10a6:10:2b5::7) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.24 via Frontend Transport; Thu, 19 Jan 2023 16:04:49 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 63.35.35.123) smtp.mailfrom=arm.com; dkim=pass (signature was verified) header.d=armh.onmicrosoft.com;dmarc=pass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 63.35.35.123 as permitted sender) receiver=protection.outlook.com; client-ip=63.35.35.123; helo=64aa7808-outbound-1.mta.getcheckrecipient.com; pr=C Received: from 64aa7808-outbound-1.mta.getcheckrecipient.com (63.35.35.123) by DBAEUR03FT033.mail.protection.outlook.com (100.127.142.251) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.13 via Frontend Transport; Thu, 19 Jan 2023 16:04:49 +0000 Received: ("Tessian outbound 3ad958cd7492:v132"); Thu, 19 Jan 2023 16:04:49 +0000 X-CheckRecipientChecked: true X-CR-MTA-CID: ae1d195896073eb1 X-CR-MTA-TID: 64aa7808 Received: from cb9ffc682106.1 by 64aa7808-outbound-1.mta.getcheckrecipient.com id A654F2B7-F24F-4434-A2CE-8B00202A7FD2.1; Thu, 19 Jan 2023 16:04:06 +0000 Received: from EUR03-DBA-obe.outbound.protection.outlook.com by 64aa7808-outbound-1.mta.getcheckrecipient.com with ESMTPS id cb9ffc682106.1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384); Thu, 19 Jan 2023 16:04:06 +0000 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=CBt93A7pn/yhEWmvxLmH/8P+JHctCpPg7sYTkvGXHdiiSbGx1LXFtb/8WcPM7WPpbQzxXfWKB3Pix6TkYws1PmC0WQI2HJmtJ5ecffAPSWnH2cX4Bt0EX2DV1LbiQ6hf0/w0XC6FW/dExrMlB03pYBEs+qFe0ABLCKScmmaBgWYIVIxI2Ip1TUxu3WMDMl1lduIp+5Zil1rPHUNryZWl0kPJHBzSy9SkgaKgXXQGzOHeriFBQOCt7igUd944YUuGd0SzdRmcXuybF+QbybekDdZW43Nqg9qkmFwtj5UPiCyvYOn9f2T3Ctu+D5OalJsOcCp7IUKlZTOKjcQIbGwEUg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-AntiSpam-MessageData-ChunkCount:X-MS-Exchange-AntiSpam-MessageData-0:X-MS-Exchange-AntiSpam-MessageData-1; bh=TVqUovDaP0Eis0yf04LEyuGnbnC1xSyDtOGIyaojtyA=; b=lWIpeJ8qhIemmks1v+GMIPZlJ79+SYK0tidfOmXXx8EVUbVkiR7iF5XAvBB3wds/s/CnVUKnO4WVPZdV8rEqSCScdBYDbljIdGsezkPWBwMKyszzzt1cjsRhA8PCxeqwWvCPQvYXQ+sSJDUdp2Vb1sBzfAXkwb6czpElYSkHBXDNtkGBmyO/3tFVTJC44Z3CiOKJVenTJhg7tLGjEUBkYQAxE8djMWyY2EN92LgLR97E8AmNsbL0/KU1XWPTmpirj/aPgHt+rDv9JRYMdz0TJxCS7j2+6bTxniYUMsJ7BY+ivtGLcUw4BhFw0DhTMpaavlT0NEbDLz7CEgPFemnGnA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass (sender ip is 40.67.248.234) smtp.rcpttodomain=linux-foundation.org smtp.mailfrom=arm.com; dmarc=pass (p=none sp=none pct=100) action=none header.from=arm.com; dkim=none (message not signed); arc=none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=armh.onmicrosoft.com; s=selector2-armh-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=TVqUovDaP0Eis0yf04LEyuGnbnC1xSyDtOGIyaojtyA=; b=V73VpP7Z56d4ftzhBcoPMGZ9SLcgx2v+yQRa3sVGW8+g+RNr3WXtcxzKfKEopOaf6/fA+oVFIJNkeDcVD8WlzEhCGDQTPC3x2omvjJtzgb1TPbzDurgNIOmdIMzQOHPIJc71uAF/aBhPajxfTksFhirqFMOVcl19qLOdkP2qfWo= Received: from DU2PR04CA0342.eurprd04.prod.outlook.com (2603:10a6:10:2b4::21) by VI1PR08MB10121.eurprd08.prod.outlook.com (2603:10a6:800:1c4::13) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.25; Thu, 19 Jan 2023 16:04:02 +0000 Received: from DBAEUR03FT022.eop-EUR03.prod.protection.outlook.com (2603:10a6:10:2b4:cafe::90) by DU2PR04CA0342.outlook.office365.com (2603:10a6:10:2b4::21) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.6002.25 via Frontend Transport; Thu, 19 Jan 2023 16:04:02 +0000 X-MS-Exchange-Authentication-Results: spf=pass (sender IP is 40.67.248.234) smtp.mailfrom=arm.com; dkim=none (message not signed) header.d=none;dmarc=pass action=none header.from=arm.com; Received-SPF: Pass (protection.outlook.com: domain of arm.com designates 40.67.248.234 as permitted sender) receiver=protection.outlook.com; client-ip=40.67.248.234; helo=nebula.arm.com; pr=C Received: from nebula.arm.com (40.67.248.234) by DBAEUR03FT022.mail.protection.outlook.com (100.127.142.217) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.20.6002.13 via Frontend Transport; Thu, 19 Jan 2023 16:04:02 +0000 Received: from AZ-NEU-EX02.Emea.Arm.com (10.251.26.5) by AZ-NEU-EX03.Arm.com (10.251.24.31) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.16; Thu, 19 Jan 2023 16:04:01 +0000 Received: from AZ-NEU-EX03.Arm.com (10.251.24.31) by AZ-NEU-EX02.Emea.Arm.com (10.251.26.5) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.16; Thu, 19 Jan 2023 16:04:00 +0000 Received: from e124191.cambridge.arm.com (10.1.197.45) by mail.arm.com (10.251.24.31) with Microsoft SMTP Server id 15.1.2507.16 via Frontend Transport; Thu, 19 Jan 2023 16:04:00 +0000 From: Joey Gouly To: Catalin Marinas , Andrew Morton , Lennart Poettering , =?utf-8?q?Zbigniew_J=C4=99drze?= =?utf-8?q?jewski-Szmek?= CC: Alexander Viro , Kees Cook , Szabolcs Nagy , Mark Brown , Jeremy Linton , Topi Miettinen , , , , , , , Subject: [PATCH v2 2/2] kselftest: vm: add tests for memory-deny-write-execute Date: Thu, 19 Jan 2023 16:03:44 +0000 Message-ID: <20230119160344.54358-3-joey.gouly@arm.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20230119160344.54358-1-joey.gouly@arm.com> References: <20230119160344.54358-1-joey.gouly@arm.com> MIME-Version: 1.0 X-EOPAttributedMessage: 1 X-MS-TrafficTypeDiagnostic: DBAEUR03FT022:EE_|VI1PR08MB10121:EE_|DBAEUR03FT033:EE_|AS8PR08MB7306:EE_ X-MS-Office365-Filtering-Correlation-Id: cb2c007e-db89-4653-e048-08dafa36e479 x-checkrecipientrouted: true NoDisclaimer: true X-MS-Exchange-SenderADCheck: 1 X-MS-Exchange-AntiSpam-Relay: 0 X-Microsoft-Antispam-Untrusted: BCL:0; X-Microsoft-Antispam-Message-Info-Original: hQajnFbyiBtM6evBKQi+8/X+dow65HqZhKYZGVA+iJZDjLKrxsvrAZOiCBjxCysA7VHjnJSezMa8HBYQzGPp9ChJWfC7X76NMtNlZAi5qi57C8xbovpsWefWfEiobXj2bkUqN+2RbuV/OnovppztDdO+wjZw/kvCJ9cnKEhR/Vb3u9UAPLkI7ECcmcl+U1aWlUd2OgjtUdz1p9k6qO+5NQsIyeMnsjIcmSyCg6zCuBaZLoxTMYaIN/O7mgbQd2JSVajB/UIgzyU7CZgnm8XS6szjKwHkqg2AFiIDow5W1+DBgDG2GJteOqvpVcRVJZqT9S3ehcBKDoEmlblgpuynnZjlGIvuuDD9HI4YPRKSQG3MJWYLbPeUjjYoHlUgDMCBkJ1Ks+9RdHiAXCNUh/FJl4Nz7yBp7wrEKMPNk8siu0+QMQDSGTc5HMPeJznCLoHBu/8g2YNv9XaNdZ5FWSCMgSR78skWVK9l3w9E7l0zhTmNDHiUjFqNdWv9S3t2howiQ/1Ru/ZyPq2GdNczXUxvQW5i55ukKvkXeidGAoTXUaBFsAlbw8qK/qUYChv6tLmG9ll5OZ91yzARwwZlKgtdGedzAXwpE1DoPefc/pBWlpvPF7RWFKzi6+NFPOQ1DhzZPnU9zVneZ/aE7IUFbvyNGQcuLS1ClpumxMUcJ5AD6GStf6kbAYzVIcwSqG6DhmQmDl1AjHtnkXJG3AnfOkvTvY9Dpp64RdavF6SgY9HFA0o= X-Forefront-Antispam-Report-Untrusted: CIP:40.67.248.234;CTRY:IE;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:nebula.arm.com;PTR:InfoDomainNonexistent;CAT:NONE;SFS:(13230022)(4636009)(396003)(39860400002)(376002)(136003)(346002)(451199015)(40470700004)(46966006)(36840700001)(2906002)(36860700001)(81166007)(82740400003)(40460700003)(426003)(47076005)(40480700001)(82310400005)(36756003)(2616005)(8676002)(316002)(110136005)(7696005)(86362001)(356005)(54906003)(70206006)(7416002)(44832011)(41300700001)(5660300002)(4326008)(70586007)(336012)(1076003)(6666004)(8936002)(26005)(186003)(478600001)(36900700001);DIR:OUT;SFP:1101; X-MS-Exchange-Transport-CrossTenantHeadersStamped: VI1PR08MB10121 X-MS-Exchange-Transport-CrossTenantHeadersStripped: DBAEUR03FT033.eop-EUR03.prod.protection.outlook.com X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-Correlation-Id-Prvs: 282693c1-6223-41a5-f24a-08dafa36c854 X-Microsoft-Antispam: BCL:0; X-Microsoft-Antispam-Message-Info: anjFeQQVY2+1XgTlFRuXqxlohYbvU6dWySgOq130xhAnjYBfSs+Gruo1RkvQpSwXxbai2Gi+9PT4MH+eQ9YEe+zwVL+yaNgfrPXzW364bcgSFA0nVeQ3htVe+PSXghEK1jtUJYCI6BTvld9CXsLNAzH335uOYx6uXNLT52/XsoeZipIaP2JQls8JTuYeqYGKaReUj3nkRlarjLEXceE895tRXvjEJDJVe+H2jX81xItnYPx51HWRMEWzD1FL9kmndMTd40bUwPHpuFrcROMSyloBV8MOIX1HckLWySlcbS7hdiBRKNi0CMzARJ46KDwkRmDNtCbf2Mm23zoCMcywlowORwsV0FpX4r33QA59hFo5EibLfoVnDQFNhNq+PZreOS5tpqhM3pfFL+QysOJZM+2hdg7t/FfzBduKD8z9z18yjnhq69NTI1m6l0INrxWXIAdq+WO8vZ6F4Xax+pfEbiKJUJpWkjXmQwW4KBYOjR6Y3zjdS9M/P5elzQbK3sMqRKPeSf2Mg9YeQJKg18WtDY0VsH1av1lVTzs/TPOupXDXeNrlTwQysdxYB4Deme3iVfO3+lqiXApnIMueCtCejYz84uzxOcB0+Q84KmNJ/hvL6GunaB++ktlYJSrlLOd15Q7EgZ5owdkt3YN2L7WqIBAcrCz6akt9YAtN63ypcxGaew1UTHrVAYBuMCStiNduW545w1G25XhpiWzDOjZByg== X-Forefront-Antispam-Report: CIP:63.35.35.123;CTRY:IE;LANG:en;SCL:1;SRV:;IPV:CAL;SFV:NSPM;H:64aa7808-outbound-1.mta.getcheckrecipient.com;PTR:ec2-63-35-35-123.eu-west-1.compute.amazonaws.com;CAT:NONE;SFS:(13230022)(4636009)(346002)(376002)(39860400002)(136003)(396003)(451199015)(40470700004)(46966006)(36840700001)(5660300002)(70206006)(70586007)(2906002)(8676002)(4326008)(44832011)(41300700001)(8936002)(7696005)(36860700001)(110136005)(54906003)(478600001)(6666004)(107886003)(36756003)(316002)(26005)(186003)(2616005)(426003)(47076005)(86362001)(82740400003)(81166007)(40480700001)(82310400005)(336012)(1076003)(40460700003);DIR:OUT;SFP:1101; X-OriginatorOrg: arm.com X-MS-Exchange-CrossTenant-OriginalArrivalTime: 19 Jan 2023 16:04:49.4560 (UTC) X-MS-Exchange-CrossTenant-Network-Message-Id: cb2c007e-db89-4653-e048-08dafa36e479 X-MS-Exchange-CrossTenant-Id: f34e5979-57d9-4aaa-ad4d-b122a662184d X-MS-Exchange-CrossTenant-OriginalAttributedTenantConnectingIp: TenantId=f34e5979-57d9-4aaa-ad4d-b122a662184d;Ip=[63.35.35.123];Helo=[64aa7808-outbound-1.mta.getcheckrecipient.com] X-MS-Exchange-CrossTenant-AuthSource: DBAEUR03FT033.eop-EUR03.prod.protection.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Anonymous X-MS-Exchange-CrossTenant-FromEntityHeader: HybridOnPrem X-MS-Exchange-Transport-CrossTenantHeadersStamped: AS8PR08MB7306 X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20230119_080506_835749_5E307C92 X-CRM114-Status: GOOD ( 16.46 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org From: Kees Cook Add some tests to cover the new PR_SET_MDWE prctl. Co-developed-by: Joey Gouly Signed-off-by: Joey Gouly Signed-off-by: Kees Cook Cc: Shuah Khan Acked-by: Joey Gouly --- tools/testing/selftests/vm/Makefile | 1 + tools/testing/selftests/vm/mdwe_test.c | 197 +++++++++++++++++++++++++ 2 files changed, 198 insertions(+) create mode 100644 tools/testing/selftests/vm/mdwe_test.c diff --git a/tools/testing/selftests/vm/Makefile b/tools/testing/selftests/vm/Makefile index 89c14e41bd43..4e1a7b1d4c52 100644 --- a/tools/testing/selftests/vm/Makefile +++ b/tools/testing/selftests/vm/Makefile @@ -56,6 +56,7 @@ TEST_GEN_PROGS += soft-dirty TEST_GEN_PROGS += split_huge_page_test TEST_GEN_FILES += ksm_tests TEST_GEN_PROGS += ksm_functional_tests +TEST_GEN_PROGS += mdwe_test ifeq ($(MACHINE),x86_64) CAN_BUILD_I386 := $(shell ./../x86/check_cc.sh "$(CC)" ../x86/trivial_32bit_program.c -m32) diff --git a/tools/testing/selftests/vm/mdwe_test.c b/tools/testing/selftests/vm/mdwe_test.c new file mode 100644 index 000000000000..f466a099f1bf --- /dev/null +++ b/tools/testing/selftests/vm/mdwe_test.c @@ -0,0 +1,197 @@ +// SPDX-License-Identifier: GPL-2.0 + +#ifdef __aarch64__ +#include +#endif + +#include +#include + +#include +#include +#include +#include +#include +#include + +#include "../kselftest_harness.h" + +#ifndef __aarch64__ +# define PROT_BTI 0 +#endif + +TEST(prctl_flags) +{ + EXPECT_LT(prctl(PR_SET_MDWE, 7L, 0L, 0L, 0L), 0); + EXPECT_LT(prctl(PR_SET_MDWE, 0L, 7L, 0L, 0L), 0); + EXPECT_LT(prctl(PR_SET_MDWE, 0L, 0L, 7L, 0L), 0); + EXPECT_LT(prctl(PR_SET_MDWE, 0L, 0L, 0L, 7L), 0); + + EXPECT_LT(prctl(PR_GET_MDWE, 7L, 0L, 0L, 0L), 0); + EXPECT_LT(prctl(PR_GET_MDWE, 0L, 7L, 0L, 0L), 0); + EXPECT_LT(prctl(PR_GET_MDWE, 0L, 0L, 7L, 0L), 0); + EXPECT_LT(prctl(PR_GET_MDWE, 0L, 0L, 0L, 7L), 0); +} + +FIXTURE(mdwe) +{ + void *p; + int flags; + size_t size; + pid_t pid; +}; + +FIXTURE_VARIANT(mdwe) +{ + bool enabled; + bool forked; +}; + +FIXTURE_VARIANT_ADD(mdwe, stock) +{ + .enabled = false, + .forked = false, +}; + +FIXTURE_VARIANT_ADD(mdwe, enabled) +{ + .enabled = true, + .forked = false, +}; + +FIXTURE_VARIANT_ADD(mdwe, forked) +{ + .enabled = true, + .forked = true, +}; + +FIXTURE_SETUP(mdwe) +{ + int ret, status; + + self->p = NULL; + self->flags = MAP_SHARED | MAP_ANONYMOUS; + self->size = getpagesize(); + + if (!variant->enabled) + return; + + ret = prctl(PR_SET_MDWE, PR_MDWE_REFUSE_EXEC_GAIN, 0L, 0L, 0L); + ASSERT_EQ(ret, 0) { + TH_LOG("PR_SET_MDWE failed or unsupported"); + } + + ret = prctl(PR_GET_MDWE, 0L, 0L, 0L, 0L); + ASSERT_EQ(ret, 1); + + if (variant->forked) { + self->pid = fork(); + ASSERT_GE(self->pid, 0) { + TH_LOG("fork failed\n"); + } + + if (self->pid > 0) { + ret = waitpid(self->pid, &status, 0); + ASSERT_TRUE(WIFEXITED(status)); + exit(WEXITSTATUS(status)); + } + } +} + +FIXTURE_TEARDOWN(mdwe) +{ + if (self->p && self->p != MAP_FAILED) + munmap(self->p, self->size); +} + +TEST_F(mdwe, mmap_READ_EXEC) +{ + self->p = mmap(NULL, self->size, PROT_READ | PROT_EXEC, self->flags, 0, 0); + EXPECT_NE(self->p, MAP_FAILED); +} + +TEST_F(mdwe, mmap_WRITE_EXEC) +{ + self->p = mmap(NULL, self->size, PROT_WRITE | PROT_EXEC, self->flags, 0, 0); + if (variant->enabled) { + EXPECT_EQ(self->p, MAP_FAILED); + } else { + EXPECT_NE(self->p, MAP_FAILED); + } +} + +TEST_F(mdwe, mprotect_stay_EXEC) +{ + int ret; + + self->p = mmap(NULL, self->size, PROT_READ | PROT_EXEC, self->flags, 0, 0); + ASSERT_NE(self->p, MAP_FAILED); + + ret = mprotect(self->p, self->size, PROT_READ | PROT_EXEC); + EXPECT_EQ(ret, 0); +} + +TEST_F(mdwe, mprotect_add_EXEC) +{ + int ret; + + self->p = mmap(NULL, self->size, PROT_READ, self->flags, 0, 0); + ASSERT_NE(self->p, MAP_FAILED); + + ret = mprotect(self->p, self->size, PROT_READ | PROT_EXEC); + if (variant->enabled) { + EXPECT_LT(ret, 0); + } else { + EXPECT_EQ(ret, 0); + } +} + +TEST_F(mdwe, mprotect_WRITE_EXEC) +{ + int ret; + + self->p = mmap(NULL, self->size, PROT_WRITE, self->flags, 0, 0); + ASSERT_NE(self->p, MAP_FAILED); + + ret = mprotect(self->p, self->size, PROT_WRITE | PROT_EXEC); + if (variant->enabled) { + EXPECT_LT(ret, 0); + } else { + EXPECT_EQ(ret, 0); + } +} + +TEST_F(mdwe, mmap_FIXED) +{ + void *p, *p2; + + p2 = mmap(NULL, self->size, PROT_READ | PROT_EXEC, self->flags, 0, 0); + self->p = mmap(NULL, self->size, PROT_READ, self->flags, 0, 0); + ASSERT_NE(self->p, MAP_FAILED); + + p = mmap(self->p + self->size, self->size, PROT_READ | PROT_EXEC, + self->flags | MAP_FIXED, 0, 0); + if (variant->enabled) { + EXPECT_EQ(p, MAP_FAILED); + } else { + EXPECT_EQ(p, self->p); + } +} + +TEST_F(mdwe, arm64_BTI) +{ + int ret; + +#ifdef __aarch64__ + if (!(getauxval(AT_HWCAP2) & HWCAP2_BTI)) +#endif + SKIP(return, "HWCAP2_BTI not supported"); + + self->p = mmap(NULL, self->size, PROT_EXEC, self->flags, 0, 0); + ASSERT_NE(self->p, MAP_FAILED); + + ret = mprotect(self->p, self->size, PROT_EXEC | PROT_BTI); + EXPECT_EQ(ret, 0); +} + +TEST_HARNESS_MAIN