Received: by 2002:a05:6602:2086:0:0:0:0 with SMTP id a6csp4781098ioa; Wed, 27 Apr 2022 10:58:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw4O+hs82qjqewDvo3mSR+7Dt4pP7X+k3e7ctmhhciSWgtMHy0KfLlS62kcgX/0P8a2T07n X-Received: by 2002:a63:5d60:0:b0:3a9:ef9f:eac2 with SMTP id o32-20020a635d60000000b003a9ef9feac2mr24821388pgm.553.1651082339558; Wed, 27 Apr 2022 10:58:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651082339; cv=none; d=google.com; s=arc-20160816; b=1G6Wy1kCygXoxoITUvds2w633TNfnIhlBiYGEwn9s1hqSRw1ktPSvsxBigv5l13fpi VDRjLcKuLJlg2nTLZAorUb8OhYQriQN8jcg24r8X85pYKdUvxnvQ4a9o7HSrcvJ7RL2V tX2ulvQu2X4iegyGWsGPrmwmmq14r2Ucl0vzrGgRslJOXrnyfJv/Tc+wCqbWe0fa71rK CzmjFbvtFb88VxKuaIHe8peyKOTWhYzwXD96tmQnEMyL/Ft7Npca5EYmSQC8UGQabQKO xoFapnPOC39g0u8FJfkrqP3Ns5qUNvVG5DoMLZFCHsS07dW90JujFmyxmz8ojL83N/xC S0Rg== 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=9XiufFvVsCGqf6bBmZ3Isd+GweoMAWu4UsOOe3CigX4=; b=HT8pvh0/Ley2PE4QezOxVz+PoktDPV36yRzCNIZo1xGjfr/333wxADekVJdyraEjxl xvE66w4FzX5lSljHWiAQBAEHe8VruIEx/ros8Hh3WWCqBDkCawreWxvNNpEckAWiNdM4 CY9c44kofP7okHMuhGN7GzAuph6Oiin5/aub0jXHJMMcsOnshrpTYa1CeMRqc4qFimSr GeHadhDILlHpZhtIHkJAUca55/sljLu4fHnOvDXZJdI5ll9rs3jQkFiJV7ZrmGq/qwy4 +5MeGrWIMUcJPk9JQP7TEv5UljWfxymiK8/IrMkh5UjdopcHV53OW7XOhI2pgkmLOpkY s7Gg== 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 c13-20020a170902848d00b0015d29ba7843si2145553plo.262.2022.04.27.10.58.59 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Apr 2022 10:58:59 -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 1FF052989B6; Wed, 27 Apr 2022 10:32:43 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244382AbiD0Rfs (ORCPT + 99 others); Wed, 27 Apr 2022 13:35:48 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56652 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244288AbiD0RfP (ORCPT ); Wed, 27 Apr 2022 13:35:15 -0400 Received: from foss.arm.com (foss.arm.com [217.140.110.172]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 1D3602245B9 for ; Wed, 27 Apr 2022 10:31:54 -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 E06B3ED1; Wed, 27 Apr 2022 10:31:53 -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 AC4123F73B; Wed, 27 Apr 2022 10:31:52 -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 10/13] lkdtm/stackleak: prevent unexpected stack usage Date: Wed, 27 Apr 2022 18:31:25 +0100 Message-Id: <20220427173128.2603085-11-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 lkdtm_STACKLEAK_ERASING() test is instrumentable and runs with IRQs unmasked, so it's possible for unrelated code to clobber the task stack and/or manipulate current->lowest_stack while the test is running, resulting in spurious failures. The regular stackleak erasing code is non-instrumentable and runs with IRQs masked, preventing similar issues. Make the body of the test non-instrumentable, and run it with IRQs masked, avoiding such spurious failures. 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 | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/drivers/misc/lkdtm/stackleak.c b/drivers/misc/lkdtm/stackleak.c index 0aafa46ced7d6..46c60761a05ea 100644 --- a/drivers/misc/lkdtm/stackleak.c +++ b/drivers/misc/lkdtm/stackleak.c @@ -11,7 +11,20 @@ #include "lkdtm.h" #include -void lkdtm_STACKLEAK_ERASING(void) +/* + * Check that stackleak tracks the lowest stack pointer and erases the stack + * below this as expected. + * + * To prevent the lowest stack pointer changing during the test, IRQs are + * masked and instrumentation of this function is disabled. We assume that the + * compiler will create a fixed-size stack frame for this function. + * + * Any non-inlined function may make further use of the stack, altering the + * lowest stack pointer and/or clobbering poison values. To avoid spurious + * failures we must avoid printing until the end of the test or have already + * encountered a failure condition. + */ +static void noinstr check_stackleak_irqoff(void) { const unsigned long task_stack_base = (unsigned long)task_stack_page(current); const unsigned long task_stack_low = stackleak_task_low_bound(current); @@ -81,3 +94,12 @@ void lkdtm_STACKLEAK_ERASING(void) pr_info("OK: the rest of the thread stack is properly erased\n"); } } + +void lkdtm_STACKLEAK_ERASING(void) +{ + unsigned long flags; + + local_irq_save(flags); + check_stackleak_irqoff(); + local_irq_restore(flags); +} -- 2.30.2