Received: by 2002:ac0:c50a:0:0:0:0:0 with SMTP id y10csp1325012imi; Fri, 1 Jul 2022 07:31:44 -0700 (PDT) X-Google-Smtp-Source: AGRyM1thVv81mZDBPUiu+XnskzCYiVyppZ/wZSXkhkvMAt1SWT9fdm/D/VK5KUWaB1PSUIhTTO+H X-Received: by 2002:a63:9d88:0:b0:40c:9c4d:29ba with SMTP id i130-20020a639d88000000b0040c9c4d29bamr12908209pgd.590.1656685904461; Fri, 01 Jul 2022 07:31:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1656685904; cv=none; d=google.com; s=arc-20160816; b=H1S3/qsd78SAK4gEDjTUsDARYFGAcdUcvHHlIjs22n2PYlNcDWZ2rFrQ3rokMxC0Ec /tBzCnl8pQq5s0SEniBP/zDqNRivGVToOsHdtk8Uvf38dE92RXxegEGBo3Q0Yo9ZTjW8 03q2io+xRgcHC+YD5Ta2q68cQZq+WBwD/StO5kor4SkLgX02UCNXze9YGCGToJwwY81z ChFls7mFdRUc185vHLLypq5HLA8UoKZwG27QcAq5UiKEUgrBClp18hq1RnYDD3h5zFy3 zliKhpT4Y2+gsTVIvFwGyHXDZBFJ2EKFgsSMytoOF8vdf2dG0Tl7HLrrvVgkrYNFZpPn a4mQ== 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:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=qRpA/KHLRYdo76YWwE6d04+ye7UN8yXoHeTH9XEwQK0=; b=JKjEHsSv+XmLhgRD3gZYYgC89gFwlc4orhq86WnuA09PVTrbrXl4tvic0a1DkNN/lr QVj2QhCkaE37G6u+5vzeKPPt/m/W/9+oijvLnd4SMpA22l2dXhOMjuLYKNHR7kGqAxjs xyKkUNJBbrmon1Snc0X0As9Gh90doU4Ser9iTlDOjHQuTo92eF2xC9+oIYyZMwBR4B9V A0gXUvUkNIm+ITchD/iaDOFwPKsZO5IFWEft7M196+dk0eiUl4YLLRwStOYkmTO5nrJp 9mJ/Hsfjt/9tXU+TIwWpNcmIQvRFUgulPEBzLbFwsPZxDJATp/R1CgwItJXOQMIGAhUC YG4Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=nuIy5sPY; 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 v186-20020a6389c3000000b00411eb01e769si1644064pgd.657.2022.07.01.07.31.31; Fri, 01 Jul 2022 07:31:44 -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=nuIy5sPY; 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 S231880AbiGAO0V (ORCPT + 99 others); Fri, 1 Jul 2022 10:26:21 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40626 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232398AbiGAOZO (ORCPT ); Fri, 1 Jul 2022 10:25:14 -0400 Received: from mail-lj1-x24a.google.com (mail-lj1-x24a.google.com [IPv6:2a00:1450:4864:20::24a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D528A42A09 for ; Fri, 1 Jul 2022 07:24:07 -0700 (PDT) Received: by mail-lj1-x24a.google.com with SMTP id c13-20020a05651c014d00b0025bb794a55eso500244ljd.10 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=HRBomeoivjJ7Wv8isYbq/+/g9VrNNfr1f0bVpOHbpq7Lrs57TWaXRiqPLqC90zP7CK XqlhCc22dz4dDqkSdNCT2I+Ae0PG+x2pPQALGcD2zxyzjxO0t3GYkOUrFZea0XRKDRfg XRI3HnwZHXz+dHGtrPfUGOYrOzkvtb+0jhwql8HGyMG1Fhje//KRZ1c7sP3omPiIJmAQ 3ZJUYEaLa4C4GPuep4EJB/um+zQfhVql14iayG9fSUTb99ELS8P0hi5Km78kd3WqQgrw Rg0vp/ukEXqpbDU/qZxLyeo0g2k1akBoDIBQZKoxKuL9kS2KVCXv2qHckDzB30QnjUKR VJAA== X-Gm-Message-State: AJIora88thNKWLpRr/90/I7+FzURGgq+JbzGIvLGOwjqC6PnfMtoB4QX 7qRfXGdbczqEE82GOwyIdC2GPUT/Hlw= 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 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,T_SCC_BODY_TEXT_LINE,USER_IN_DEF_DKIM_WL autolearn=unavailable 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 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 --- 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) { -- 2.37.0.rc0.161.g10f37bed90-goog