From patchwork Fri Aug 26 15:08:04 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956214 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 34293ECAAD4 for ; Fri, 26 Aug 2022 15:10:10 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id C0789940015; Fri, 26 Aug 2022 11:10:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id B8EC7940007; Fri, 26 Aug 2022 11:10:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 9E1E5940015; Fri, 26 Aug 2022 11:10:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0012.hostedemail.com [216.40.44.12]) by kanga.kvack.org (Postfix) with ESMTP id 8C666940007 for ; Fri, 26 Aug 2022 11:10:09 -0400 (EDT) Received: from smtpin23.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 641D980E3C for ; Fri, 26 Aug 2022 15:10:09 +0000 (UTC) X-FDA: 79842079338.23.6CD63DA Received: from mail-ed1-f73.google.com (mail-ed1-f73.google.com [209.85.208.73]) by imf24.hostedemail.com (Postfix) with ESMTP id 2609F18001F for ; Fri, 26 Aug 2022 15:10:08 +0000 (UTC) Received: by mail-ed1-f73.google.com with SMTP id p4-20020a056402500400b00447e8b6f62bso1237627eda.17 for ; Fri, 26 Aug 2022 08:10:08 -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=72fMcX1pxMik1t4ZJNqkb8aoIMa6Rlt6HY2bGlacGWSYH68fHZLrj+tuNoHjBzaUKs VPTa6jitfrJ0fzMs72fJ4TyDnOMDdY2ht/2U/LojFwL/G26VGIX6HzIMhj5lS3ON8VgL LodPLxu9WQ8nI7nSkOcqGG7vhRInvIxtaJDGCoofkcWJ8JBO+lghh0TgBFVXGkNbXriP 1kj77xH9hJXe/pcOalMfasDLjFUWnZe5Sb6Sl374GgJUsr8/f+743zmnwwNmeiIPUG46 8XcZlRLSGHdvaC3wFAufdQ4FVpZ+PIJxS8F6cm5/510WQvGWtd7t3QmT2g918sgSQ4kz dRtQ== X-Gm-Message-State: ACgBeo0RZ2Y+06kDBb9Fe0oyZ+pwCBoNTtNcRlnm9PmjT2863HtjyblE gdNng6SxHKjXNDJoDQqbUhu6mUVfL2I= 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 ARC-Authentication-Results: i=1; imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=XuSgIJWo; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf24.hostedemail.com: domain of 3T-IIYwYKCFY49612F4CC492.0CA96BIL-AA8Jy08.CF4@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3T-IIYwYKCFY49612F4CC492.0CA96BIL-AA8Jy08.CF4@flex--glider.bounces.google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526609; a=rsa-sha256; cv=none; b=ESEFopQFg+vSO1yOOy8NkXi8JUYaO9L1X2ZG+93YolrFIYYXUOKZSE3vJgwSrS/Xni+s7B MgOfiJur3sQKPbCJAkkmPhq0S6Ji95ibbmyqnqYU/74aLdjDUPkBvbzwIZ2AVDtBYLDz/y Dks2BtSZ0n7MuPaYByp2ApIcTuRnoD4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526609; 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=VDn7tLtoTqUri5wCNae2jgpDMB9XRf/m0RZ2a2IuTLU=; b=hQN7RCbzTU7nL6YsY9vCpTeOHGTheiDrMYN+DzllDLR1WTKUelQwAf9439FI0dnJdi/CNx oFj1yZbbHpCMeu1Hv3HIMfRccYn+yyspiL4lv99oAWh7iBGD1JYi2AnN1xsStpr8PObOWC Qx1AQMNc0NnHRTVoUKKyjqH500pLipQ= X-Rspam-User: X-Rspamd-Queue-Id: 2609F18001F Authentication-Results: imf24.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=XuSgIJWo; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf24.hostedemail.com: domain of 3T-IIYwYKCFY49612F4CC492.0CA96BIL-AA8Jy08.CF4@flex--glider.bounces.google.com designates 209.85.208.73 as permitted sender) smtp.mailfrom=3T-IIYwYKCFY49612F4CC492.0CA96BIL-AA8Jy08.CF4@flex--glider.bounces.google.com X-Stat-Signature: tjkqt4w53cwgj6zd65tpghf5uyz7t49o X-Rspamd-Server: rspam03 X-HE-Tag: 1661526608-780238 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 | 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 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) @@ -334,6 +345,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 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); +/* + * 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)