Received: by 2002:a05:6602:2086:0:0:0:0 with SMTP id a6csp4781171ioa; Wed, 27 Apr 2022 10:59:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwwAE90xTGYlOBEkP6SfB0soB8LUqlhk4XvSuRRnkOdDN18UnLgwiXpxD3h52Oc+iRrSS0D X-Received: by 2002:a17:902:d510:b0:15c:ecd2:e7fc with SMTP id b16-20020a170902d51000b0015cecd2e7fcmr22615901plg.132.1651082347558; Wed, 27 Apr 2022 10:59:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651082347; cv=none; d=google.com; s=arc-20160816; b=ixR0xkPhaDDol0cPv+ETvdQJH9lEuIyEF7mcn9htvSTE3O7Zw705Ovt8OPblDL90Q/ 4TJ4TxzVNwpcYXqGhoT9S0ntShkTQBrzP0ImX5B19RBOkxhKFD0vBWOCBIRZ6uNzetYz UcIBOncEOHIQEjcE904CMwkGXqBHNMpQabjC9/LNbJ49QiDtdWy2Q8hCUOqlPi1KygBl eFvNPTJwJP2JidZSuSpHBzQgsU6C5s0W5OPxVl/4GDCxOU0kStR7nC259A3JhNQ/WvVD GtS+HzBdqtoQJs/PMHZMNIIeRNtSNBRRU+rQrTAT4LFMii2XhjoB4IE+Mdiv+lRvxhjG 7ySQ== 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=x+7k3vqLTs40P0w7WO5PmuJzpjmDLwK+uZKam0/Es/Y=; b=NS0wo17MdX9yXEMzD4nVq6eMqguSr5JsGV54iGIlVbKpHL8S6ycmCNAVhEm94NrqnT DkzXZkH4du6VEjUiOMIIXCcFUySWAv+bUecgCUqmDlDFIunvwZCcHmv21tOuBUnMcyR3 55uJKXVd7E/vNFxgjFPKwN5v+hTCb8XEVq8hHTam6paDi+PbJ0EhYvcUdG5tSPNMM4Ai bHtf9ZdD6rh2N+ZJXGOXjEx7ABjlGkBNWcvkQcRXW6dn5Fei1HgcaGGZYQuGIZf1PPLL nHFGw3RM7y5EtO7E1Ciig2TzqLhxhggjdYxf5VhKEpt1dHKRVpaP2Dhb6m/ndViLvSUy //vw== ARC-Authentication-Results: i=1; mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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. [23.128.96.19]) by mx.google.com with ESMTPS id j9-20020a056a00130900b0050d3e4009easi1999280pfu.125.2022.04.27.10.59.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Apr 2022 10:59:07 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 1D9812A1311; Wed, 27 Apr 2022 10:32:47 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S244434AbiD0Rfw (ORCPT + 99 others); Wed, 27 Apr 2022 13:35:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:54144 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244275AbiD0RfP (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 6433323E832 for ; Wed, 27 Apr 2022 10:31:57 -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 9F8F01474; Wed, 27 Apr 2022 10:31:57 -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 6B58E3F73B; Wed, 27 Apr 2022 10:31:56 -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 12/13] stackleak: add on/off stack variants Date: Wed, 27 Apr 2022 18:31:27 +0100 Message-Id: <20220427173128.2603085-13-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_erase() code dynamically handles being on a task stack or another stack. In most cases, this is a fixed property of the caller, which the caller is aware of, as an architecture might always return using the task stack, or might always return using a trampoline stack. This patch adds stackleak_erase_on_task_stack() and stackleak_erase_off_task_stack() functions which callers can use to avoid on_thread_stack() check and associated redundant work when the calling stack is known. The existing stackleak_erase() is retained as a safe default. There should be no functional change as a result of this patch. Signed-off-by: Mark Rutland Cc: Alexander Popov Cc: Andrew Morton Cc: Andy Lutomirski Cc: Kees Cook --- kernel/stackleak.c | 35 ++++++++++++++++++++++++++++++++--- 1 file changed, 32 insertions(+), 3 deletions(-) diff --git a/kernel/stackleak.c b/kernel/stackleak.c index afd54b8e10b83..c2c33d2202e9a 100644 --- a/kernel/stackleak.c +++ b/kernel/stackleak.c @@ -70,7 +70,7 @@ late_initcall(stackleak_sysctls_init); #define skip_erasing() false #endif /* CONFIG_STACKLEAK_RUNTIME_DISABLE */ -static __always_inline void __stackleak_erase(void) +static __always_inline void __stackleak_erase(bool on_task_stack) { const unsigned long task_stack_low = stackleak_task_low_bound(current); const unsigned long task_stack_high = stackleak_task_high_bound(current); @@ -96,7 +96,7 @@ static __always_inline void __stackleak_erase(void) * function has a fixed-size stack frame, and the current stack pointer * doesn't change while we write poison. */ - if (on_thread_stack()) + if (on_task_stack) erase_high = current_stack_pointer; else erase_high = task_stack_high; @@ -110,12 +110,41 @@ static __always_inline void __stackleak_erase(void) current->lowest_stack = task_stack_high; } +/* + * Erase and poison the portion of the task stack used since the last erase. + * Can be called from the task stack or an entry stack when the task stack is + * no longer in use. + */ asmlinkage void noinstr stackleak_erase(void) { if (skip_erasing()) return; - __stackleak_erase(); + __stackleak_erase(on_thread_stack()); +} + +/* + * Erase and poison the portion of the task stack used since the last erase. + * Can only be called from the task stack. + */ +asmlinkage void noinstr stackleak_erase_on_task_stack(void) +{ + if (skip_erasing()) + return; + + __stackleak_erase(true); +} + +/* + * Erase and poison the portion of the task stack used since the last erase. + * Can only be called from a stack other than the task stack. + */ +asmlinkage void noinstr stackleak_erase_off_task_stack(void) +{ + if (skip_erasing()) + return; + + __stackleak_erase(false); } void __used __no_caller_saved_registers noinstr stackleak_track_stack(void) -- 2.30.2