From nobody Fri Dec 19 20:35:56 2025 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 03E7EECAAA1 for ; Thu, 15 Sep 2022 15:12:27 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231234AbiIOPMY (ORCPT ); Thu, 15 Sep 2022 11:12:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48208 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231163AbiIOPKZ (ORCPT ); Thu, 15 Sep 2022 11:10:25 -0400 Received: from mail-ed1-x54a.google.com (mail-ed1-x54a.google.com [IPv6:2a00:1450:4864:20::54a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 74F6F60517 for ; Thu, 15 Sep 2022 08:06:28 -0700 (PDT) Received: by mail-ed1-x54a.google.com with SMTP id i17-20020a05640242d100b0044f18a5379aso13464034edc.21 for ; Thu, 15 Sep 2022 08:06:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date; bh=S+kwzH5LfDyg6tc+T47KWRR4zTWxpHjqyuN2TRvGEwc=; b=ikiVkwTWTLFg9q22Qo4MSxlAf8PXtNYS/YYj3pii7FEhNeb9uHoJOl8elk4rPV4ZJZ 0IGJinTNDSDUCJXFLmtqtctlrBSMtSZx+Q27jQ5t/D4KUgh+E+9+KwPOr2K16xHCw6ZT stactk3FWOzjDq3XEnZtyX+gYnhHfZkZCmjyOGFqc6N3weiBgsHifRSIeQhCUvV5vZJX CG1yQ4Azvcw+Azu27dzh37E/xbi/PjeAK04sk085Qwwj7CDPFh4bJbybx/ZYZq8xM1DR 9LGd3d+74k/4QZbbzCwxMuDFLSZDOavjAjoH2kSQnr509kCCEUIdA8blYvLSclZ5hJ2P x2mA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc:subject:date; bh=S+kwzH5LfDyg6tc+T47KWRR4zTWxpHjqyuN2TRvGEwc=; b=7Moz1wqXm8NmMQca9LGbFXo7edEaHsqCVxgTslsmDkdU4XeAEa2vQjB3Dz6elegc5h G/+I5722HQ72rtFeSFJe5DwAZnj8TBuD6/lYpxFQKEZ31BPjXA4dR5v9n8GKMAMBt8mL NAEkmLqfCdTA5uGmcmM0dAm3+pNVb0o79p7gLPyerF8LY1H5M97CTXjf/Q7ELaruv2HG C4dq1WzMHBBvniIiq50iFRlDb16WUrgMRE2zTkApZhRueNwLDI5m6hv7sr8f1XHuEPRh PLJniTnfSz/1+1PkhIxMQxz6t3DwTCvYqnHKNc97S4H4c+Xils31ipPTHXWIqkUY3nIA 4RSg== X-Gm-Message-State: ACrzQf3GGYtJPJJkcF97c7RP0W3SKfoUwJ0YTaoeAzMIHTUH2du7oH5r kRi7TNkjcyXfQVYCbrjCgMy+Nsk7HNM= X-Google-Smtp-Source: AMsMyM7sbunNpO+CpqIWn/hftkFrFIDp3ih3TnneGFODdNEkk+fy3fgyPU94i649AG9zEEjpdPfVfIIHuAw= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a17:906:5a5a:b0:770:86d0:fd8 with SMTP id my26-20020a1709065a5a00b0077086d00fd8mr329690ejc.164.1663254387679; Thu, 15 Sep 2022 08:06:27 -0700 (PDT) Date: Thu, 15 Sep 2022 17:04:14 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-41-glider@google.com> Subject: [PATCH v7 40/43] entry: kmsan: introduce kmsan_unpoison_entry_regs() From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , Andrew Morton , Andrey Konovalov , Andy Lutomirski , Arnd Bergmann , Borislav Petkov , Christoph Hellwig , Christoph Lameter , David Rientjes , Dmitry Vyukov , Eric Biggers , Eric Dumazet , Greg Kroah-Hartman , Herbert Xu , Ilya Leoshkevich , Ingo Molnar , Jens Axboe , Joonsoo Kim , Kees Cook , Marco Elver , Mark Rutland , Matthew Wilcox , "Michael S. Tsirkin" , Pekka Enberg , Peter Zijlstra , Petr Mladek , Stephen Rothwell , Steven Rostedt , Thomas Gleixner , Vasily Gorbik , Vegard Nossum , Vlastimil Babka , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" struct pt_regs passed into IRQ entry code is set up by uninstrumented asm functions, therefore KMSAN may not notice the registers are initialized. kmsan_unpoison_entry_regs() unpoisons the contents of struct pt_regs, preventing potential false positives. Unlike kmsan_unpoison_memory(), it can be called under kmsan_in_runtime(), which is often the case in IRQ entry code. Signed-off-by: Alexander Potapenko --- Link: https://linux-review.googlesource.com/id/Ibfd7018ac847fd8e5491681f508= ba5d14e4669cf --- include/linux/kmsan.h | 15 +++++++++++++++ kernel/entry/common.c | 5 +++++ mm/kmsan/hooks.c | 26 ++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/include/linux/kmsan.h b/include/linux/kmsan.h index c473e0e21683c..e38ae3c346184 100644 --- a/include/linux/kmsan.h +++ b/include/linux/kmsan.h @@ -214,6 +214,17 @@ void kmsan_handle_dma_sg(struct scatterlist *sg, int n= ents, */ void kmsan_handle_urb(const struct urb *urb, bool is_out); =20 +/** + * kmsan_unpoison_entry_regs() - Handle pt_regs in low-level entry code. + * @regs: struct pt_regs pointer received from assembly code. + * + * KMSAN unpoisons the contents of the passed pt_regs, preventing potential + * false positive reports. Unlike kmsan_unpoison_memory(), + * kmsan_unpoison_entry_regs() can be called from the regions where + * kmsan_in_runtime() returns true, which is the case in early entry code. + */ +void kmsan_unpoison_entry_regs(const struct pt_regs *regs); + #else =20 static inline void kmsan_init_shadow(void) @@ -310,6 +321,10 @@ static inline void kmsan_handle_urb(const struct urb *= urb, bool is_out) { } =20 +static inline void kmsan_unpoison_entry_regs(const struct pt_regs *regs) +{ +} + #endif =20 #endif /* _LINUX_KMSAN_H */ diff --git a/kernel/entry/common.c b/kernel/entry/common.c index 063068a9ea9b3..846add8394c41 100644 --- a/kernel/entry/common.c +++ b/kernel/entry/common.c @@ -5,6 +5,7 @@ #include #include #include +#include #include #include #include @@ -24,6 +25,7 @@ static __always_inline void __enter_from_user_mode(struct= pt_regs *regs) user_exit_irqoff(); =20 instrumentation_begin(); + kmsan_unpoison_entry_regs(regs); trace_hardirqs_off_finish(); instrumentation_end(); } @@ -352,6 +354,7 @@ noinstr irqentry_state_t irqentry_enter(struct pt_regs = *regs) lockdep_hardirqs_off(CALLER_ADDR0); ct_irq_enter(); instrumentation_begin(); + kmsan_unpoison_entry_regs(regs); trace_hardirqs_off_finish(); instrumentation_end(); =20 @@ -367,6 +370,7 @@ noinstr irqentry_state_t irqentry_enter(struct pt_regs = *regs) */ lockdep_hardirqs_off(CALLER_ADDR0); instrumentation_begin(); + kmsan_unpoison_entry_regs(regs); rcu_irq_enter_check_tick(); trace_hardirqs_off_finish(); instrumentation_end(); @@ -452,6 +456,7 @@ irqentry_state_t noinstr irqentry_nmi_enter(struct pt_r= egs *regs) ct_nmi_enter(); =20 instrumentation_begin(); + kmsan_unpoison_entry_regs(regs); trace_hardirqs_off_finish(); ftrace_nmi_enter(); instrumentation_end(); diff --git a/mm/kmsan/hooks.c b/mm/kmsan/hooks.c index 79d7e73e2cfd8..35f6b6e6a908c 100644 --- a/mm/kmsan/hooks.c +++ b/mm/kmsan/hooks.c @@ -348,6 +348,32 @@ void kmsan_unpoison_memory(const void *address, size_t= size) } EXPORT_SYMBOL(kmsan_unpoison_memory); =20 +/* + * Version of kmsan_unpoison_memory() that can be called from within the K= MSAN + * runtime. + * + * Non-instrumented IRQ entry functions receive struct pt_regs from assemb= ly + * code. Those regs need to be unpoisoned, otherwise using them will resul= t in + * false positives. + * Using kmsan_unpoison_memory() is not an option in entry code, because t= he + * return value of in_task() is inconsistent - as a result, certain calls = to + * kmsan_unpoison_memory() are ignored. kmsan_unpoison_entry_regs() ensure= s that + * the registers are unpoisoned even if kmsan_in_runtime() is true in the = early + * entry code. + */ +void kmsan_unpoison_entry_regs(const struct pt_regs *regs) +{ + unsigned long ua_flags; + + if (!kmsan_enabled) + return; + + ua_flags =3D user_access_save(); + kmsan_internal_unpoison_memory((void *)regs, sizeof(*regs), + KMSAN_POISON_NOCHECK); + user_access_restore(ua_flags); +} + void kmsan_check_memory(const void *addr, size_t size) { if (!kmsan_enabled) --=20 2.37.2.789.g6183377224-goog