From patchwork Fri Jul 1 14:22:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12903375 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 CD839C43334 for ; Fri, 1 Jul 2022 14:24:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 66F176B0074; Fri, 1 Jul 2022 10:24:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 61EA16B0075; Fri, 1 Jul 2022 10:24:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 499616B007E; Fri, 1 Jul 2022 10:24:08 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 3AF8F6B0074 for ; Fri, 1 Jul 2022 10:24:08 -0400 (EDT) Received: from smtpin03.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay08.hostedemail.com (Postfix) with ESMTP id 051B421668 for ; Fri, 1 Jul 2022 14:24:07 +0000 (UTC) X-FDA: 79638750576.03.70909DD Received: from mail-lf1-f74.google.com (mail-lf1-f74.google.com [209.85.167.74]) by imf11.hostedemail.com (Postfix) with ESMTP id 8946C40054 for ; Fri, 1 Jul 2022 14:24:07 +0000 (UTC) Received: by mail-lf1-f74.google.com with SMTP id o7-20020a056512230700b004810a865709so1183339lfu.3 for ; Fri, 01 Jul 2022 07:24:07 -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=qRpA/KHLRYdo76YWwE6d04+ye7UN8yXoHeTH9XEwQK0=; b=nuIy5sPY97WE4Na2+tDp/B2mAWuutSYXusRYqrf6YxSijHhgLbmnsmS0o2fiYh5/Do /09lw8m4SVNKOYmoEK6HMxPLTu1E3kw4LhBcRBfvwY3nS2wLt+OCVSphGAatbhRfsti+ VVp0F1sWJPyWXiGjZXUPSCQZOElXfKweDeG5Yu1Njxbddsc0VwS3a8L8cZHVuO5Klbj8 qdZN0ySrNX0MJDuYE65IztkpOKOSP3D6akR5YHdge9pHQKisJPIuL47h4k0Q33mfj/9L XeJvoIaLRnLrA4bnmskTdmIuMKwd86AQ7L805VR2oh/iRtOJ1JrzzU9ncKoZ9gIHDb8s YLCQ== 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=qRpA/KHLRYdo76YWwE6d04+ye7UN8yXoHeTH9XEwQK0=; b=XyL/rQHQKkMmN3v9FhSCNbTAnKggL+vLkXVnNXPNZo0G6BjBwND94CHcrX6RMTyKUh wFmF7beUDrTKvsRIYXUo56t3yvr6F+SjWZe5HDbQh1TePBSNEdWxb0TM+XG+Qgq7eBYV LLPYm8dQ0tPSRfiNdnkjFvo16zWCHGFqirQMObN6AdtJG5sQw6DDPNg+wDwceva3iRKh r42OE8o9bDTxkMqq/otIoU+oahfHGaBAw8fhrYf9Bu4PmgPCG0qFpv6S4fcD2PjhVjpx tDJX1w1FhMLWbIU3GVjFmXtJPf56l8IUjIXKlNzWa82twpaD4S453g5hsHUbts6sApsO ChLw== X-Gm-Message-State: AJIora+lw/5t/UtYKUReUWEHH/bCAEfxnz7aWx3D5tfHGY0DANFa4R+3 dFyic/oJhH7Kj8+/ybXe4i1tpZixfTU= X-Google-Smtp-Source: AGRyM1sty4IKmaztZhJQX9qrKqisLJMfHz0k4hy5dUHdfFpoWKsT1NmFCA9JTdaWQUOqJX7WaPNwfk8uyOs= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:a6f5:f713:759c:abb6]) (user=glider job=sendgmr) by 2002:a05:6512:b9b:b0:47f:6aae:ecc5 with SMTP id b27-20020a0565120b9b00b0047f6aaeecc5mr9169900lfv.412.1656685445833; Fri, 01 Jul 2022 07:24:05 -0700 (PDT) Date: Fri, 1 Jul 2022 16:22:43 +0200 In-Reply-To: <20220701142310.2188015-1-glider@google.com> Message-Id: <20220701142310.2188015-19-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 18/45] instrumented.h: add KMSAN support 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=1656685447; a=rsa-sha256; cv=none; b=el7PZntIq6dQ3B3/88sCn2EmHWGjf6ZEwmnWj95VoRh2fIG8mtUdMnm6JriB8Ep/VM+EDu naGMJo8OUH4bSKQL01dur5O4wF7qQRwOjF5N/RPZPAExUiKwJjwBkth1smKOwXsVii37/T dkQSXNQW6eP1nLvqro5INSrRTJHi1x0= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=nuIy5sPY; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3hQO_YgYKCKQKPMHIVKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--glider.bounces.google.com designates 209.85.167.74 as permitted sender) smtp.mailfrom=3hQO_YgYKCKQKPMHIVKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1656685447; 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=qRpA/KHLRYdo76YWwE6d04+ye7UN8yXoHeTH9XEwQK0=; b=6hJLMwWSdZZg5n6Da0B1gDMEqOvkBdKaLopDVXZwIB7GWTtVUyJNk7Yu0frPIDYvC14Ml8 JJzl8A0Gc0eGQkTf7TMrF2i8PsTlON8oHhD81BSLIznta1WvUH3pWXgQU/r3niRNKhTbFS cNqGYv7DqeuMqYQxs4w7T8k/yKiiGTU= X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 8946C40054 Authentication-Results: imf11.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=nuIy5sPY; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf11.hostedemail.com: domain of 3hQO_YgYKCKQKPMHIVKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--glider.bounces.google.com designates 209.85.167.74 as permitted sender) smtp.mailfrom=3hQO_YgYKCKQKPMHIVKSSKPI.GSQPMRYb-QQOZEGO.SVK@flex--glider.bounces.google.com X-Rspam-User: X-Stat-Signature: j1rkbhan5ny6xqdnzydo3bcrrsmhxbrg X-HE-Tag: 1656685447-198581 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: To avoid false positives, KMSAN needs to unpoison the data copied from the userspace. To detect infoleaks - check the memory buffer passed to copy_to_user(). Signed-off-by: Alexander Potapenko Reviewed-by: Marco Elver --- v2: -- move implementation of kmsan_copy_to_user() here Link: https://linux-review.googlesource.com/id/I43e93b9c02709e6be8d222342f1b044ac8bdbaaf --- include/linux/instrumented.h | 5 ++++- include/linux/kmsan-checks.h | 19 ++++++++++++++++++ mm/kmsan/hooks.c | 38 ++++++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/include/linux/instrumented.h b/include/linux/instrumented.h index ee8f7d17d34f5..c73c1b19e9227 100644 --- a/include/linux/instrumented.h +++ b/include/linux/instrumented.h @@ -2,7 +2,7 @@ /* * This header provides generic wrappers for memory access instrumentation that - * the compiler cannot emit for: KASAN, KCSAN. + * the compiler cannot emit for: KASAN, KCSAN, KMSAN. */ #ifndef _LINUX_INSTRUMENTED_H #define _LINUX_INSTRUMENTED_H @@ -10,6 +10,7 @@ #include #include #include +#include #include /** @@ -117,6 +118,7 @@ instrument_copy_to_user(void __user *to, const void *from, unsigned long n) { kasan_check_read(from, n); kcsan_check_read(from, n); + kmsan_copy_to_user(to, from, n, 0); } /** @@ -151,6 +153,7 @@ static __always_inline void instrument_copy_from_user_after(const void *to, const void __user *from, unsigned long n, unsigned long left) { + kmsan_unpoison_memory(to, n - left); } #endif /* _LINUX_INSTRUMENTED_H */ diff --git a/include/linux/kmsan-checks.h b/include/linux/kmsan-checks.h index a6522a0c28df9..c4cae333deec5 100644 --- a/include/linux/kmsan-checks.h +++ b/include/linux/kmsan-checks.h @@ -46,6 +46,21 @@ void kmsan_unpoison_memory(const void *address, size_t size); */ void kmsan_check_memory(const void *address, size_t size); +/** + * kmsan_copy_to_user() - Notify KMSAN about a data transfer to userspace. + * @to: destination address in the userspace. + * @from: source address in the kernel. + * @to_copy: number of bytes to copy. + * @left: number of bytes not copied. + * + * If this is a real userspace data transfer, KMSAN checks the bytes that were + * actually copied to ensure there was no information leak. If @to belongs to + * the kernel space (which is possible for compat syscalls), KMSAN just copies + * the metadata. + */ +void kmsan_copy_to_user(void __user *to, const void *from, size_t to_copy, + size_t left); + #else static inline void kmsan_poison_memory(const void *address, size_t size, @@ -58,6 +73,10 @@ static inline void kmsan_unpoison_memory(const void *address, size_t size) static inline void kmsan_check_memory(const void *address, size_t size) { } +static inline void kmsan_copy_to_user(void __user *to, const void *from, + size_t to_copy, size_t left) +{ +} #endif diff --git a/mm/kmsan/hooks.c b/mm/kmsan/hooks.c index 43a529569053d..1cdb4420977f1 100644 --- a/mm/kmsan/hooks.c +++ b/mm/kmsan/hooks.c @@ -212,6 +212,44 @@ void kmsan_iounmap_page_range(unsigned long start, unsigned long end) } EXPORT_SYMBOL(kmsan_iounmap_page_range); +void kmsan_copy_to_user(void __user *to, const void *from, size_t to_copy, + size_t left) +{ + unsigned long ua_flags; + + if (!kmsan_enabled || kmsan_in_runtime()) + return; + /* + * At this point we've copied the memory already. It's hard to check it + * before copying, as the size of actually copied buffer is unknown. + */ + + /* copy_to_user() may copy zero bytes. No need to check. */ + if (!to_copy) + return; + /* Or maybe copy_to_user() failed to copy anything. */ + if (to_copy <= left) + return; + + ua_flags = user_access_save(); + if ((u64)to < TASK_SIZE) { + /* This is a user memory access, check it. */ + kmsan_internal_check_memory((void *)from, to_copy - left, to, + REASON_COPY_TO_USER); + user_access_restore(ua_flags); + return; + } + /* Otherwise this is a kernel memory access. This happens when a compat + * syscall passes an argument allocated on the kernel stack to a real + * syscall. + * Don't check anything, just copy the shadow of the copied bytes. + */ + kmsan_internal_memmove_metadata((void *)to, (void *)from, + to_copy - left); + user_access_restore(ua_flags); +} +EXPORT_SYMBOL(kmsan_copy_to_user); + /* Functions from kmsan-checks.h follow. */ void kmsan_poison_memory(const void *address, size_t size, gfp_t flags) {