From patchwork Mon Feb 26 19:09:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 13572731 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 59AA6C48BF6 for ; Mon, 26 Feb 2024 19:10:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id B7B554401B9; Mon, 26 Feb 2024 14:10:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id B29CC44017F; Mon, 26 Feb 2024 14:10:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 97C4B4401B9; Mon, 26 Feb 2024 14:10:08 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 848C144017F for ; Mon, 26 Feb 2024 14:10:08 -0500 (EST) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id 063F4A0B75 for ; Mon, 26 Feb 2024 19:10:08 +0000 (UTC) X-FDA: 81834895296.14.A7A8028 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.12]) by imf07.hostedemail.com (Postfix) with ESMTP id 2CE6F40008 for ; Mon, 26 Feb 2024 19:10:05 +0000 (UTC) Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=jOT4lAGg; spf=pass (imf07.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.12 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1708974606; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=NFCSdAhrunIzMrgwpPIPHm0AfcuiyWMeHA0mu0m/29Q=; b=a6QZyl+KvTBm8CpllZJf8DzhdTEoL5FVezVDSn+sw5EmLnY8QP/PPKx0U/oud3nO7Ue+lF ZuL6pP9R7i9AdYOj7L4GNxg9UDGP7gAmmQbeq2H0RpXHtT93nyKON2L8pII/1Ypv/P9H+R 6kwr/JRJo1aoyVp+e6r3Jmfvcb8LzkY= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1708974606; a=rsa-sha256; cv=none; b=HXZquSsz+Gn6ihXoA0mkVdeq+GaB7tfhTphya1XpUsGCGXaYpKGvOCSLOCXoCiqJh9yna6 lP8k/4yp63hUc2qoGhERPxJV9xe5Na8yItPHw/VW6fXix2Yj+B6OQ4gBDAF5eydYGMgpJz VlbzfskcTO/yuODLzv/e/Jvi5m97S9o= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=jOT4lAGg; spf=pass (imf07.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.12 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com; dmarc=pass (policy=none) header.from=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1708974606; x=1740510606; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9Kz1B1g9YBWVQVBA7u5n8sBfRhBddgPHEWEy/INdrdY=; b=jOT4lAGgGPJSBIobtFG6DfkeDJDgCH9S1EcXTIpJpCTFNDri0NJf14+l oPC5sVR5E5RLLYfdmLBOiYsmoyATTvnh96egcmJn1rs4CtG+HqLkL9LoI Ew0vQAn5R+LW7AZiyn3e+uS9d4p7Bodw01OY/SLZuZpC8ri/rTx2b/UQw CFhhdD6iMng2Q3j8kVxc3owxbgRLoZAv0834YLBEKOyz6Jbb74JZd5e4X uZRyrmcC/iC5QP/JwpQwR6pArw9vHT0u5acncF0VipYu1mBU4qGSLnMmr YNfkH2UkDydNgcScAwHoZLJRs2P5DKVzMu115buy2CjIZWaBV75v5NJJ7 g==; X-IronPort-AV: E=McAfee;i="6600,9927,10996"; a="14721441" X-IronPort-AV: E=Sophos;i="6.06,186,1705392000"; d="scan'208";a="14721441" Received: from fmviesa008.fm.intel.com ([10.60.135.148]) by orvoesa104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 11:10:06 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.06,186,1705392000"; d="scan'208";a="6911475" Received: from bdmirand-mobl.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.251.3.213]) by fmviesa008-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 26 Feb 2024 11:10:04 -0800 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, debug@rivosinc.com, broonie@kernel.org, kirill.shutemov@linux.intel.com, keescook@chromium.org, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, luto@kernel.org, peterz@infradead.org, hpa@zytor.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: rick.p.edgecombe@intel.com Subject: [PATCH v2 9/9] selftests/x86: Add placement guard gap test for shstk Date: Mon, 26 Feb 2024 11:09:51 -0800 Message-Id: <20240226190951.3240433-10-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240226190951.3240433-1-rick.p.edgecombe@intel.com> References: <20240226190951.3240433-1-rick.p.edgecombe@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: 2CE6F40008 X-Rspam-User: X-Rspamd-Server: rspam11 X-Stat-Signature: demw59pe8na4o8n73iir6say53uxunqz X-HE-Tag: 1708974605-407686 X-HE-Meta: U2FsdGVkX1+R3bDPkRcfYFUda7o/IcWS5gS40RnNU6QdQE3rCTIoiSbG2NWIML+WEFdfUlxUHSGAgbhDbhruDk4uFcMiy95UY0CCXaDIiCLmQnZdaZq5cHF/Ctij77nYppFW7rm/DmbS1N964A78imDOSLdvaWGTnpoPbrI+okQJCxPsoETsGrHCgrkseiHjSe8zZgutEOSj505NG7MUEJqNmgBcatqHk2W4fQk3/su2lOX/fvSFynfWKOvVTLViXR4KfDZYRzMTQBOEsTec6BYrniZPxoxiBFmm+CiyhlS9awxOY2oxE2U0iSjxMwyo/Nvi0JOfVmKAxl6AjCAhPcl8yAsduZOHDuDmm/qndwylTCPgx9f5x1itSdfE5Axg6IPBuEsRQy+voxT6S9gkJICa6bXtck/3LjOLugiDpp67/d3FWrSO6o5vviXjBr8m+zdn6gYE59Ey06W2odKeM4wtBED3t3d73EhNNZxaG/sY0Izc6ZYxWVzptx1ETiaTxErqEnFjPpkPWIyTgywOmE6wmzcdI0B/Gwpkygthetni6tBxIRh3JiWLrdr3UsI8z/hJKvTc55EZA7xzV/pV1k9Q9PIV2726uK9H9Kv1LzNPVkP3JnYourHmG7/6fuU1Bihvm/82dRNGaG01QpBJ09NKV4NCIDmQLYWVRMxtaf85/ejs/lrBiTnRuKCupJ9lD8DQZ0RiOuRH9ovg+dlk8BD+TWZrk8HEnAlnQz8bt1tixWJ3bIGPP+i1O8Zawl58teMeLIX+/wQWL9AwC5BJTo2xy7xCClEM5NoT6WtNAOyYZTpS5+A1B6w/mw6FvD3tay1ZRetD08/43n3ESurB4BJDc8v3jgJ9zTaaO0t1MzSpR/hILKQ24qXEkbCCef9u X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: List-Subscribe: List-Unsubscribe: The existing shadow stack test for guard gaps just checks that new mappings are not placed in an existing mapping's guard gap. Add one that checks that new mappings are not placed such that preexisting mappings are in the new mappings guard gap. Signed-off-by: Rick Edgecombe --- .../testing/selftests/x86/test_shadow_stack.c | 67 +++++++++++++++++-- 1 file changed, 63 insertions(+), 4 deletions(-) diff --git a/tools/testing/selftests/x86/test_shadow_stack.c b/tools/testing/selftests/x86/test_shadow_stack.c index 757e6527f67e..ee909a7927f9 100644 --- a/tools/testing/selftests/x86/test_shadow_stack.c +++ b/tools/testing/selftests/x86/test_shadow_stack.c @@ -556,7 +556,7 @@ struct node { * looked at the shadow stack gaps. * 5. See if it landed in the gap. */ -int test_guard_gap(void) +int test_guard_gap_other_gaps(void) { void *free_area, *shstk, *test_map = (void *)0xFFFFFFFFFFFFFFFF; struct node *head = NULL, *cur; @@ -593,11 +593,64 @@ int test_guard_gap(void) if (shstk - test_map - PAGE_SIZE != PAGE_SIZE) return 1; - printf("[OK]\tGuard gap test\n"); + printf("[OK]\tGuard gap test, other mapping's gaps\n"); return 0; } +/* Tests respecting the guard gap of the mapping getting placed */ +int test_guard_gap_new_mappings_gaps(void) +{ + void *free_area, *shstk_start, *test_map = (void *)0xFFFFFFFFFFFFFFFF; + struct node *head = NULL, *cur; + int ret = 0; + + free_area = mmap(0, PAGE_SIZE * 4, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + munmap(free_area, PAGE_SIZE * 4); + + /* Test letting map_shadow_stack find a free space */ + shstk_start = mmap(free_area, PAGE_SIZE, PROT_READ | PROT_WRITE, + MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); + if (shstk_start == MAP_FAILED || shstk_start != free_area) + return 1; + + while (test_map > shstk_start) { + test_map = (void *)syscall(__NR_map_shadow_stack, 0, PAGE_SIZE, 0); + if (test_map == MAP_FAILED) { + printf("[INFO]\tmap_shadow_stack MAP_FAILED\n"); + ret = 1; + break; + } + + cur = malloc(sizeof(*cur)); + cur->mapping = test_map; + + cur->next = head; + head = cur; + + if (test_map == free_area + PAGE_SIZE) { + printf("[INFO]\tNew mapping has other mapping in guard gap!\n"); + ret = 1; + break; + } + } + + while (head) { + cur = head; + head = cur->next; + munmap(cur->mapping, PAGE_SIZE); + free(cur); + } + + munmap(shstk_start, PAGE_SIZE); + + if (!ret) + printf("[OK]\tGuard gap test, placement mapping's gaps\n"); + + return ret; +} + /* * Too complicated to pull it out of the 32 bit header, but also get the * 64 bit one needed above. Just define a copy here. @@ -850,9 +903,15 @@ int main(int argc, char *argv[]) goto out; } - if (test_guard_gap()) { + if (test_guard_gap_other_gaps()) { ret = 1; - printf("[FAIL]\tGuard gap test\n"); + printf("[FAIL]\tGuard gap test, other mappings' gaps\n"); + goto out; + } + + if (test_guard_gap_new_mappings_gaps()) { + ret = 1; + printf("[FAIL]\tGuard gap test, placement mapping's gaps\n"); goto out; }