Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp3059660lqp; Mon, 25 Mar 2024 19:20:14 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUs0QGyIphWt20vHJRdildAY7Msnamp9MM1zsh4peReTjsFCttcGOeqdX8p8ZeWwBzi3S+euFdWPNSy/Et1POTy67RUBVfaqDs1bMJ2cQ== X-Google-Smtp-Source: AGHT+IEzFgnmwQll0sC5TPHoGoa4ittPm3XB59RPX/C3raPKFPpxtSWIOX9dIMXVhs+JpUZkouiw X-Received: by 2002:a50:c055:0:b0:565:9c76:73c3 with SMTP id u21-20020a50c055000000b005659c7673c3mr5431859edd.29.1711419614301; Mon, 25 Mar 2024 19:20:14 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711419614; cv=pass; d=google.com; s=arc-20160816; b=njMwdZebDpXkx3TdK4sYMJGdn4BlCsTKRP00KuLpQE9YIQNGi9NZ9qto+gkl7p3aCu jgeu7xuxYqHuDAS2nAxZkrWLXkAQ6U/z7PEmKCUM51eU1/9KQBx2sFvPBTtRfm6mi6f1 BachHQrNKXO6i0u4lAPxyXqEqEqVvLdQfjAfe18DHRPCU32CmFJzwClyR9KhZK80YySi yqR6jsZmoAGThkmuSc5MbfiQPTCtgxIykn8XV9n2CKcIsIk+x1vZesY6Re3Z7anL9ouQ jgzyf9bRDgqxWfn0y2XFp44u+wIQ/mhGtIsf9cwJp/e4yG/tguGpg1SIkvva4bSY3kVo dZCg== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=NFCSdAhrunIzMrgwpPIPHm0AfcuiyWMeHA0mu0m/29Q=; fh=VdawlRosHP6/TRt7Q+d5dFfk99BjWPbqwi77dk1QfXM=; b=szwwe769zG/f2gqcR+B30545lBhjWV9OPNtV3wzQOfpdZU8sVxEXHkJ97N/3IvOOF0 gfCcVw8lMwnxwXo1bxM82JlWFfAGnPm6nlERQaCG5/ECtTpjknL/BhKNtOnSRMeysIa3 DdwjFOOSF922+G+q4nNlRDEUCoJg9wxLDK/0aQdF0wwPByIOUBQNFlkpuOfhtgXy6vVi Ux9Kglth5ysR6uXm77KjuxR7ppi0kJj63kkK5uqjhl5ZCxDlyQsygk6JpjijU4asrpyl QT/04vaxrYpN0CvBHOuUv9z0h6X3tWQ4r3FxczQxl8IQlE5cqWX8mhdVjgW9Xy1s1PzL u/kw==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=LYYZyUFr; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-118293-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-118293-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id p6-20020a056402500600b0056bdc9bb274si3254360eda.217.2024.03.25.19.20.14 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 25 Mar 2024 19:20:14 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-118293-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=LYYZyUFr; arc=pass (i=1 spf=pass spfdomain=intel.com dkim=pass dkdomain=intel.com dmarc=pass fromdomain=intel.com); spf=pass (google.com: domain of linux-kernel+bounces-118293-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-118293-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 0FDD51F3F804 for ; Tue, 26 Mar 2024 02:20:14 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id E9C847F7CD; Tue, 26 Mar 2024 02:17:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b="LYYZyUFr" Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.16]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 6F6DB12AAEA for ; Tue, 26 Mar 2024 02:17:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=198.175.65.16 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419450; cv=none; b=pgVp4duOHYAZgv+r+v7kDS7rxXReSUT3/lNfDrXCumgQ4V/rS0ZXEVn1zL7t7lYuZfCw7oBSGnNWAlpLNyZjnI/5T3lqgSkJUS7eFIvlO3Py1DVmo5aVlgPVPJtR5W4PMo6F6tVBcMy40BJIlnNOHuZK6h3djkFD5KS8tBH7B0M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711419450; c=relaxed/simple; bh=9Kz1B1g9YBWVQVBA7u5n8sBfRhBddgPHEWEy/INdrdY=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=j08hyYHb7aK3NxWz/o+qGEu3FgcQcWowi4482K5dRoL5ApHnEg8z6nce4TmNpWSNXNIV8Y0AzGUkIMBxiYuS6weeEP7JhBGicKcFDCkFjRGZIVY2E8RWq98tKscOaDajTlf2zphHfuiT0jVlWckTIkRZk1L+j9aos4a9i2lONwc= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.b=LYYZyUFr; arc=none smtp.client-ip=198.175.65.16 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1711419448; x=1742955448; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=9Kz1B1g9YBWVQVBA7u5n8sBfRhBddgPHEWEy/INdrdY=; b=LYYZyUFrtDZkQhAOhqtCiGspppwQiSNYt0J9ZxHDi60absIqwAZ2EZR5 pCBEajXfa3H9NHI68e+NyPUj8PnASFZxA7ZJOwBETuF7CFwpMR/90+46y A6IbGfBr23dXDkyOGz2AqMcy2MQ2Fk8PV4bqx3sVW+AZZLaCWCk+gNB3Z I5LHhzdXjHFuCcQgfUOshc21whlZ31lCYNvcN+Xdq5l/ehIokD/MXOseh NqBdTP1YCJcqqkoTj5+p8WnFCDnRuCU2GGuvewkDPhToZNgPK45DM6tCF FBS7wbeJxDJpwDGHzrua9+HoUysX5gl/gvAKWVAGeuNr3SoOduI90ztvv w==; X-IronPort-AV: E=McAfee;i="6600,9927,11024"; a="6564393" X-IronPort-AV: E=Sophos;i="6.07,154,1708416000"; d="scan'208";a="6564393" 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> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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; } -- 2.34.1