Received: by 2002:a05:6602:2086:0:0:0:0 with SMTP id a6csp4781283ioa; Wed, 27 Apr 2022 10:59:19 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw9x7PRf+P390k8cOZnFSI6WeGvADkr9RK+/4rO6l6aST9GxWg750hzSXC0l3UNf366I/0x X-Received: by 2002:a17:902:f2ca:b0:15d:180d:704 with SMTP id h10-20020a170902f2ca00b0015d180d0704mr16157922plc.102.1651082359515; Wed, 27 Apr 2022 10:59:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651082359; cv=none; d=google.com; s=arc-20160816; b=U2MuKbxW1GzVWvPeITFjgcMIk9wRyGvaOIsUxdk8HsaU0asVpEJkdtyW5TBqyo9kNW i+FV3WoMD7Q6vKK08sI0UMpE52jl0U05CEbt0GhbEKFbCs+WdtvNuwoUEtteFgYOnFJD eCebCra8zjL8bVoWOVDbknoKKibA9bgi3PMu9bFX6ULdX83B1IXNJ/E3SnGtb5sdmOAn 9CSK5V0E0alohkOLB8/vE0OrvxJd7DVSEKuoEEreWkfgoP8y4HX9Za3lY2Nqv8XAcAlr 9fViT07FPRRqlGQeIt8ujXI7QE8+U2wb0uhwGxwfF3ZApZtXN/AMCBJIkrDX1gvsHa0N TAZg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=JDI3/9fkQE7crCFK4Itu4kxXvv07r3NB8hBq3TDpDlU=; b=aQ+es0P1Xl3NZyOezhcJ2q2YylrN4YiFlZwbv0PjGIyMm9U9a0JEMDEwd9D2d8nJHE MTZ8cgRAiW1L3u5w25LIvlC+bdQz7N023fK7/FoyiuXNB+MSkQRXFiB/FJ1sClnxoL/a rydK7oAkvRSJqZ2EtcOVwc/2u7c1K3Nx3LUigUY1ueZP4E06iZwGhsG1MHAiEr8TnyQ/ 45KgrlXsHIWmcpi7dSHRlJi6EZkSA5/ITuoiLY+0wXikHK3f788NGOlhnSRdoAJTJBva L1z5X6WrtZYI1IEIxuHa+ttRqH9wKzn7B7Za+0dw/Fc6ozVTyDimPvT2qaoqLyTiYFtL SUPw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Return-Path: Received: from lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id n13-20020a170903110d00b00153b2d1640fsi2526525plh.23.2022.04.27.10.59.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Apr 2022 10:59:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=arm.com Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id E9E50245AA; Wed, 27 Apr 2022 10:32:54 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244462AbiD0Rf5 (ORCPT + 99 others); Wed, 27 Apr 2022 13:35:57 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56104 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244363AbiD0RfW (ORCPT ); Wed, 27 Apr 2022 13:35:22 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id D9D312360C5 for ; Wed, 27 Apr 2022 10:31:55 -0700 (PDT) Received: from usa-sjc-imap-foss1.foss.arm.com (unknown [10.121.207.14]) by usa-sjc-mx-foss1.foss.arm.com (Postfix) with ESMTP id C1BA9143D; Wed, 27 Apr 2022 10:31:55 -0700 (PDT) Received: from lakrids.cambridge.arm.com (usa-sjc-imap-foss1.foss.arm.com [10.121.207.14]) by usa-sjc-imap-foss1.foss.arm.com (Postfix) with ESMTPA id 8DA463F73B; Wed, 27 Apr 2022 10:31:54 -0700 (PDT) From: Mark Rutland To: linux-arm-kernel@lists.infradead.org Cc: akpm@linux-foundation.org, alex.popov@linux.com, catalin.marinas@arm.com, keescook@chromium.org, linux-kernel@vger.kernel.org, luto@kernel.org, mark.rutland@arm.com, will@kernel.org Subject: [PATCH v2 11/13] lkdtm/stackleak: check stack boundaries Date: Wed, 27 Apr 2022 18:31:26 +0100 Message-Id: <20220427173128.2603085-12-mark.rutland@arm.com> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20220427173128.2603085-1-mark.rutland@arm.com> References: <20220427173128.2603085-1-mark.rutland@arm.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RDNS_NONE, SPF_HELO_NONE autolearn=no autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The stackleak code relies upon the current SP and lowest recorded SP falling within expected task stack boundaries. Check this at the start of the test. Signed-off-by: Mark Rutland Cc: Alexander Popov Cc: Andrew Morton Cc: Andy Lutomirski Cc: Catalin Marinas Cc: Kees Cook Cc: Will Deacon --- drivers/misc/lkdtm/stackleak.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/drivers/misc/lkdtm/stackleak.c b/drivers/misc/lkdtm/stackleak.c index 46c60761a05ea..52800583fd051 100644 --- a/drivers/misc/lkdtm/stackleak.c +++ b/drivers/misc/lkdtm/stackleak.c @@ -35,6 +35,25 @@ static void noinstr check_stackleak_irqoff(void) unsigned long poison_high, poison_low; bool test_failed = false; + /* + * Check that the current and lowest recorded stack pointer values fall + * within the expected task stack boundaries. These tests should never + * fail unless the boundaries are incorrect or we're clobbering the + * STACK_END_MAGIC, and in either casee something is seriously wrong. + */ + if (current_sp < task_stack_low || current_sp >= task_stack_high) { + pr_err("FAIL: current_stack_pointer (0x%lx) outside of task stack bounds [0x%lx..0x%lx]\n", + current_sp, task_stack_low, task_stack_high - 1); + test_failed = true; + goto out; + } + if (lowest_sp < task_stack_low || lowest_sp >= task_stack_high) { + pr_err("FAIL: current->lowest_stack (0x%lx) outside of task stack bounds [0x%lx..0x%lx]\n", + lowest_sp, task_stack_low, task_stack_high - 1); + test_failed = true; + goto out; + } + /* * Depending on what has run prior to this test, the lowest recorded * stack pointer could be above or below the current stack pointer. @@ -87,6 +106,7 @@ static void noinstr check_stackleak_irqoff(void) poison_high - task_stack_low, task_stack_low - task_stack_base); +out: if (test_failed) { pr_err("FAIL: the thread stack is NOT properly erased!\n"); pr_expected_config(CONFIG_GCC_PLUGIN_STACKLEAK); -- 2.30.2