From patchwork Fri Jul 1 14:23:06 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12903398 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 kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 58CD5C43334 for ; Fri, 1 Jul 2022 14:25:12 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E38796B0073; Fri, 1 Jul 2022 10:25:11 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E0E756B0074; Fri, 1 Jul 2022 10:25:11 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id C3C776B007E; Fri, 1 Jul 2022 10:25:11 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0016.hostedemail.com [216.40.44.16]) by kanga.kvack.org (Postfix) with ESMTP id B3A4F6B0073 for ; Fri, 1 Jul 2022 10:25:11 -0400 (EDT) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay11.hostedemail.com (Postfix) with ESMTP id 926A8804AF for ; Fri, 1 Jul 2022 14:25:11 +0000 (UTC) X-FDA: 79638753222.13.D413762 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf24.hostedemail.com (Postfix) with ESMTP id 30FCA180007 for ; Fri, 1 Jul 2022 14:25:11 +0000 (UTC) Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-31ba792c793so20271477b3.20 for ; Fri, 01 Jul 2022 07:25:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=kErHaO8c2VDyOovKMTTPjLnffjCar7lX0B4oxY8ZYJM=; b=QikpwS8HSBicv3aiX5l18DFFItV5cAfTWljYQPRMj2W7svuU9foj5CYwNhPW5g1+1u bB4glo+uMZCRum5mXol+jHpQv/lgXI3n7dJV/CM+AV/P8K/r9Gy2E3HtcxQY9ID9PHSC wVw80I52aTXRRhG6Ci/za3zw0SD1eIdJQitn35q5f/DsJHwTzK0rEhzr2LJtogI15qw/ PNVnInhl+jBMMMMjNB1XrOY7EpC2Lh+bCIf6lfRBYGnQduk/EEoxldI491nxYD8nls9J Xv49bFK0wQhLo1KkR4mkWE/FmlIpB/UN96BNNMS/ZsSyzjXx8nx8GEKrJFs5/vreJQ9A EF9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=kErHaO8c2VDyOovKMTTPjLnffjCar7lX0B4oxY8ZYJM=; b=1dROglq/WqsvojUEIFnKJYmR2H8orDAWRq8ZMh+/asJ4CPxMGQR0WIJOZMttaDqy4U clSAKhm/AfT2XpRAUOs2RppDzsctbviuEUO5dIYogV+8tRcESl57vtB+YdLVodghYd2E 5LYI+emUZUQ6B98oARVrc9SJ6ObI50K2nAWDtSnJF3+bZdjKevZpd2Em8JOtnsKskgEs HQzyIv1XpeM/oL+R2X9HzxFmvBYKuJhCt2XrtWuES2UAsPfFbQGXGT9kRUw32MLUS0iI JfI1scyUP6gbNMhp5y/oxL5oYSNV5N8zEa06KUkxRgkF8uIVhiATWqo4Hdcnk5F8HCRS dPdg== X-Gm-Message-State: AJIora+vq9JkgOf/24B+Og5wOFj5lBn5lXYKyd+U4ojVu0WebRyX4yM9 svYqMyIwPEvdXvo+oCb7K2lt8pMRpHA= X-Google-Smtp-Source: AGRyM1sNDBrS8JsvaLtaQ5Nuve5a/xxYqM8vAs8mLC9hMfdeY1tED78m34uqRhiFnJFi3MqtOhAJlCD8K7A= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:a6f5:f713:759c:abb6]) (user=glider job=sendgmr) by 2002:a81:d82:0:b0:31b:fd6f:9005 with SMTP id 124-20020a810d82000000b0031bfd6f9005mr16667693ywn.389.1656685510371; Fri, 01 Jul 2022 07:25:10 -0700 (PDT) Date: Fri, 1 Jul 2022 16:23:06 +0200 In-Reply-To: <20220701142310.2188015-1-glider@google.com> Message-Id: <20220701142310.2188015-42-glider@google.com> Mime-Version: 1.0 References: <20220701142310.2188015-1-glider@google.com> X-Mailer: git-send-email 2.37.0.rc0.161.g10f37bed90-goog Subject: [PATCH v4 41/45] 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 ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1656685511; a=rsa-sha256; cv=none; b=vO4KrHPfT7vl/32+AQvfergNmSO8yd2TUEG7OtPKLwxsEmkkwLvvFI3gaZBOScjE7BiO8B jkh+FKB7Qmh3qSsocvGxttCpEv6lbd8DOJ56SieCCWnVV4zJDFVmNY2BKbWBPPBF2BTI2h CNWXSaB0M1xdIN7CFcZkP+Xqu0V/VZw= ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=QikpwS8H; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf24.hostedemail.com: domain of 3xgO_YgYKCOUNSPKLYNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--glider.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3xgO_YgYKCOUNSPKLYNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1656685511; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=kErHaO8c2VDyOovKMTTPjLnffjCar7lX0B4oxY8ZYJM=; b=cD/pG8taKelEBMQnTCW4/Y/aUQin4gJYA+YOVlv69ZiS7u6nEblpU9ITKsUzUSxpKlAgls /pxjTpUtE7In59DUfpQwYF4xJYkicEZYBFj8QmGKjcNVfyeZM/27vHnLmE2+s5bvs6n5ad dsRNZ7KQ2pAbavnaz3rJD14Oz7SNwD0= X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 30FCA180007 Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=QikpwS8H; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf24.hostedemail.com: domain of 3xgO_YgYKCOUNSPKLYNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--glider.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3xgO_YgYKCOUNSPKLYNVVNSL.JVTSPUbe-TTRcHJR.VYN@flex--glider.bounces.google.com X-Rspam-User: X-Stat-Signature: 361duz6rhho7y99ho5k16eg6w7qbc14s X-HE-Tag: 1656685511-155763 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: 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 | 27 +++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) diff --git a/include/linux/kmsan.h b/include/linux/kmsan.h index e8b5c306c4aa1..c4412622b9a78 100644 --- a/include/linux/kmsan.h +++ b/include/linux/kmsan.h @@ -246,6 +246,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) @@ -342,6 +353,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 032f164abe7ce..055d3bdb0442c 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); rcu_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) rcu_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 9aecbf2825837..c7528bcbb2f91 100644 --- a/mm/kmsan/hooks.c +++ b/mm/kmsan/hooks.c @@ -358,6 +358,33 @@ 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); +} +EXPORT_SYMBOL(kmsan_unpoison_entry_regs); + void kmsan_check_memory(const void *addr, size_t size) { if (!kmsan_enabled)