From patchwork Tue Mar 26 02:16:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Rick Edgecombe X-Patchwork-Id: 13603268 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 08752C54E58 for ; Tue, 26 Mar 2024 02:17:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 0AED36B009C; Mon, 25 Mar 2024 22:17:31 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 033A56B009D; Mon, 25 Mar 2024 22:17:30 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id CDD0E6B009E; Mon, 25 Mar 2024 22:17:30 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id B995A6B009C for ; Mon, 25 Mar 2024 22:17:30 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 948141C06C3 for ; Tue, 26 Mar 2024 02:17:30 +0000 (UTC) X-FDA: 81937578660.08.FDF48E2 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) by imf11.hostedemail.com (Postfix) with ESMTP id AA10740006 for ; Tue, 26 Mar 2024 02:17:28 +0000 (UTC) Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Q4sw9epN; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf11.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.16 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711419449; 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=Y8zHFHSdr5mITiWZhyITZUXmnOSOZPcH6iDi7+iBB95eRArETOgy2AJJRyvECCK/F/kTqa jU7gMhMB5p5FL6i9RAJPPZkv3I9xEBP9ly/ScZboA2vaGs8c0MR06jrRa0IxMyczi0vBCn ow/5a6KA32vE/S275uVl77oRzVRkMBw= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=Q4sw9epN; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf11.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 198.175.65.16 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711419449; a=rsa-sha256; cv=none; b=kaOGTL01d8C7rvZInQrrXYL4J1c0w6a8OjzpV8gPl8D5bHY1YP3+MpODHojApW7WSagKM5 ZIF3B60runapd2xBfReC9era6hw3oWbRfPbflVbUJVwNOi5oWucwSM4kDOOTj2lBOcNoqV yhjXeM8KB3WsBdc8Dy2FGoCe58AySWk= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711419449; x=1742955449; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9Kz1B1g9YBWVQVBA7u5n8sBfRhBddgPHEWEy/INdrdY=; b=Q4sw9epNbeSaibFvId3kVf/gSkl2yhf6NALaEgYfSW9heEFdtYAfuHvu zsji9LSgaAa3T2U93L5GdAgYGzIwQ7SEvlu9nLMqpmwyv3eC+wCz3jwf3 rmfc3VynuzFqkXrHsZf7hypGCb0r85PEiMc1/BquIDjGLwycoQgs+6in4 flkurXIZ4ZLczJ/P7/qRmpH0r0JGwgI/aZB7gr1Wc97ISZyqtoO7LjKG/ N3aJfr8WHOW9cMw264B/m/uSopLN9TUc5ZKc4QyKICzfQOlAOrsa/NfAl +MUHfUGPab95gqwCOpZ5iPvxMy5maiemyvZTJikOXrZpX+Z58HeRieRXY Q==; X-IronPort-AV: E=McAfee;i="6600,9927,11024"; a="6564396" X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="6564396" Received: from orviesa004.jf.intel.com ([10.64.159.144]) by orvoesa108.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:18 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="20489921" Received: from rpwilson-mobl.amr.corp.intel.com (HELO rpedgeco-desk4.intel.com) ([10.251.11.187]) by orviesa004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2024 19:17:18 -0700 From: Rick Edgecombe To: Liam.Howlett@oracle.com, akpm@linux-foundation.org, bp@alien8.de, broonie@kernel.org, christophe.leroy@csgroup.eu, dave.hansen@linux.intel.com, debug@rivosinc.com, hpa@zytor.com, keescook@chromium.org, kirill.shutemov@linux.intel.com, luto@kernel.org, mingo@redhat.com, peterz@infradead.org, tglx@linutronix.de, x86@kernel.org Cc: rick.p.edgecombe@intel.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH v4 14/14] selftests/x86: Add placement guard gap test for shstk Date: Mon, 25 Mar 2024 19:16:56 -0700 Message-Id: <20240326021656.202649-15-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240326021656.202649-1-rick.p.edgecombe@intel.com> References: <20240326021656.202649-1-rick.p.edgecombe@intel.com> MIME-Version: 1.0 X-Rspamd-Queue-Id: AA10740006 X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: xk43azfu7yax8o7jbrj4yuqabn5zetj9 X-HE-Tag: 1711419448-806599 X-HE-Meta: U2FsdGVkX1/oe1OjtowMAF4my3vl7X+jmR/eyBpNZhdBj7tSkEGzYoabLXNOT1Xzwg0S6/odOfZ/DpzAgOeTqCqAYyhqjfopLrcp+AnOYjCPCyIsrMmUBpqM0vpdsD2WSLwiA6YzWAlFggqhHHYC3bFQnL4XO/2wbLIo2CmodWcSC4ZeK9JMID+HMxueazOz40AM+bzOYJlQXuDZ6hVYnFLDOH7mZUkArOZv9wXwEc7HKkfQyXOgikRHhKwugxzZLQ4qDbSDosqstqGL+ddL2cNiSJMFfB1Kaiw2zSytC6ddBfPCGcrGYImREOugLwfrTXadXXRkCVJ+CDNrvHTPf05m8G6BPFDeWzpwXRB6V2nKZdi4NopmVZynpOERs2qam1ryFJK11r4z8VRbaRAUXWk1iqm9+K+6riAawTqkEEO9/KWHwHSPSKwLyVAQN1/+EkuNC+YThytmBP5Vm3qw98o9Y6yzOoGjl3cldyGomxGKuGNTtebgsR+j11Bii3CJIV76TKXlLDNNVFbw/hroLXbOYa77RU6O1QfwV6V4jC9vu5Cr5kDbS+V5Ydu7QAZQNdW6MrDzxvPU+jHX9QWtxmZvc/XtqcEQ6RHSngO066CzHxx2YVzcbnLeCchZ9tuRFomQ9Yv+OKUDLz1jepWFtoUOtvDdxXo2e12Qlg0lG9SQngG+bRZVUBL5zquEJqCgGu9UPlncTxjeoLi3+8LY72g8DlkXl0BUMv2wGQSKbvXAbsuU4UL5+z/SkdCzVlrgwUou/DP66f7i2lyNb3jJzagMciTVvCW/55FAGuUm2SE3YDOLNXEw07eKtga/eZ6qH3reTjpgE4vl6GXI4XmLHl4HOr2Rv31GwZsimThZxPeENUrxKfV0BOWrzZQMe4iK 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; }