Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp994324rwn; Thu, 15 Sep 2022 09:00:48 -0700 (PDT) X-Google-Smtp-Source: AMsMyM5HCcy3Xw7IYPiSyhkzPgBPoh0dWyf5hmGwVeCHd+Q0N/XN7nzT25qalPkPVZwhTSEnaxxd X-Received: by 2002:a17:90b:2242:b0:200:1c81:c108 with SMTP id hk2-20020a17090b224200b002001c81c108mr581552pjb.89.1663257647882; Thu, 15 Sep 2022 09:00:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663257647; cv=none; d=google.com; s=arc-20160816; b=kxJuw+PYackVJ44EIj7+27MUTA0bn36lJEWVRfoNQpR7kqbCbRrr6hxvyfEtDccBBe Nl+i79oxLieJPJeG1NzseFGvC/3VxxrVDqtPnIjv4LmLzgqRfsNdkqzeh6OabHsQj2S5 Dk0UCYKNG+aaVdghcP3cyl23asDmFcCSSHB8DyAa01suK55cqPxKzWChzk53Mq7BYm6d 1Gdz69k0G/9BfXT6t6YRNZwZllXQ3/AN2hzTgVSFQsDwXCFNLRCMB6P8oORz67dd9l0P JcqEwe9tdQ/t/+x3qQFImiPuvVgKI1Mc/YiJzaWkoPbI9fIRMW2ZKfQIeXN52Z91JGyL XcSw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=S+kwzH5LfDyg6tc+T47KWRR4zTWxpHjqyuN2TRvGEwc=; b=DC+eYo5Ut6PowBZu+01w5poZ9yeyKbVj1hQvezkwxneiIP7UzcfsBWP/sUrM+tdm7U XWXikhnvW/Z7mWKAlfjPr9j32zt160fgrZNw7sT9W3yyL7K2WTZwvaeT3NyKubgehEID l9Cg+RH6/VkcK0eHhbmmq8k44hGzVZeue5Pbioaym65e3FsFbgv4Mb665c7yavhq7fge NUsKd2HV0dwybklhwTtSOLKK5Qia7govhxtYW0gkYxqLSQu7HJ0dJFrrbyYVqf8YGRft rSR1730cZg63PRZE1gbOVVyKdxHenW2KE1AbvKciQEjzfP6iOnfiCkRFnzHRY6ngfyd1 bmtg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=ikiVkwTW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id p8-20020a1709027ec800b00172a117cbcfsi17888502plb.324.2022.09.15.09.00.34; Thu, 15 Sep 2022 09:00:47 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=ikiVkwTW; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231231AbiIOPMY (ORCPT + 99 others); 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-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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham 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 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/Ibfd7018ac847fd8e5491681f508ba5d14e4669cf --- 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 nents, */ void kmsan_handle_urb(const struct urb *urb, bool is_out); +/** + * 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 static inline void kmsan_init_shadow(void) @@ -310,6 +321,10 @@ static inline void kmsan_handle_urb(const struct urb *urb, bool is_out) { } +static inline void kmsan_unpoison_entry_regs(const struct pt_regs *regs) +{ +} + #endif #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(); 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(); @@ -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_regs *regs) ct_nmi_enter(); 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); +/* + * Version of kmsan_unpoison_memory() that can be called from within the KMSAN + * runtime. + * + * Non-instrumented IRQ entry functions receive struct pt_regs from assembly + * code. Those regs need to be unpoisoned, otherwise using them will result in + * false positives. + * Using kmsan_unpoison_memory() is not an option in entry code, because the + * return value of in_task() is inconsistent - as a result, certain calls to + * kmsan_unpoison_memory() are ignored. kmsan_unpoison_entry_regs() ensures 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 = 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) -- 2.37.2.789.g6183377224-goog