From nobody Tue Apr 7 16:29:07 2026 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 BC279ECAAA3 for ; Fri, 26 Aug 2022 15:16:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S245673AbiHZPQg (ORCPT ); Fri, 26 Aug 2022 11:16:36 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46086 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344436AbiHZPO7 (ORCPT ); Fri, 26 Aug 2022 11:14:59 -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 C6715A61C8 for ; Fri, 26 Aug 2022 08:10:11 -0700 (PDT) Received: by mail-ed1-x54a.google.com with SMTP id q32-20020a05640224a000b004462f105fa9so1240395eda.4 for ; Fri, 26 Aug 2022 08:10:11 -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; bh=VDn7tLtoTqUri5wCNae2jgpDMB9XRf/m0RZ2a2IuTLU=; b=XuSgIJWo5MH1NG3rWGo/F6MDltFcf2qPl6MCg/lTmi6AVs/W3AFN0L3RCnMqTlX9lr dKd/GckQv5Z6rSA+ayYP2KyxzcAyFxigMT3ptZSUl+gwPWVJSY9aubJhhTBy8zE+VVZx CGTMVKUA67P93/ZY15+zBfj4PaUpI6K4dk8spYaiA6AzXLDnbdicLnYpwutgnjjkOzyS hnFo1lTrpffm0Zz8iscFAwJfrYozG7CPI8rmN9qs3VLS/3azTHfOe6oXTOh5+hkHW5Bu RpNeHbpB3NC1YbvSkhEm3j/KxYQQRtFq+lVJn9jc6zvqweY5eCiiYGU2uk30ec6mXLQI dUWg== 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; bh=VDn7tLtoTqUri5wCNae2jgpDMB9XRf/m0RZ2a2IuTLU=; b=QwgKMMVfuPD9N6FUg+F+aMzENi3B8fYrT/kTlA6TOkNfr8cCv10EYxeuOvS/PgIUbe /SPNklb9ULmhR3V0B6Aa59tf3B+JQby6qs5QoboyketIP1u2B/9yUDYOxCx0zzq3Ppmb ab5ki1L+smttj37WBsAFOYnrKwTUi8hWl1gGUOXU4nKork0l1zXMxB0jwVz1zpEnzuBI +iG3gnxyTbWeqkohYKREG0QJ8OEQIVu/GZzWLOBROu8K+eXQ7N70zjwfOezM7g5Wcger gL9rUgG8VejkVIjRBh9x/6L54KkxXw0OqdE5TRo69127x+Thtzg/M/dZhh8Ek7fM801p YyrA== X-Gm-Message-State: ACgBeo2CCB7HD/B8H6icfvLcjYwoRYx54B4USLl7FjR0GCcXowRpBflI JP6E8ClNfuRxVq1zQ/lWW2splYJQJ2w= X-Google-Smtp-Source: AA6agR7Aswd52JiieVR1uDzHxTIsNC8FPiPrY6f0RXtM2U6YqiEtATQBhicPX6UGtVM3aylaikgOQRe7gwE= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a05:6402:1f87:b0:43b:b88d:1d93 with SMTP id c7-20020a0564021f8700b0043bb88d1d93mr7088766edc.314.1661526607921; Fri, 26 Aug 2022 08:10:07 -0700 (PDT) Date: Fri, 26 Aug 2022 17:08:04 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-42-glider@google.com> Subject: [PATCH v5 41/44] 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 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 , 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 84dddf3aa5f8b..f4015a7546e39 100644 --- a/include/linux/kmsan.h +++ b/include/linux/kmsan.h @@ -238,6 +238,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) @@ -334,6 +345,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 58334fa32ff86..14d6c78a793b8 100644 --- a/mm/kmsan/hooks.c +++ b/mm/kmsan/hooks.c @@ -347,6 +347,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.672.g94769d06f0-goog