From patchwork Fri Aug 26 15:07:41 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956191 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 443DAECAAA3 for ; Fri, 26 Aug 2022 15:09:05 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id D4064940010; Fri, 26 Aug 2022 11:09:04 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id CC7BE940007; Fri, 26 Aug 2022 11:09:04 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A577E940010; Fri, 26 Aug 2022 11:09:04 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 90E31940007 for ; Fri, 26 Aug 2022 11:09:04 -0400 (EDT) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 604E180E24 for ; Fri, 26 Aug 2022 15:09:04 +0000 (UTC) X-FDA: 79842076608.28.D040E26 Received: from mail-yw1-f201.google.com (mail-yw1-f201.google.com [209.85.128.201]) by imf27.hostedemail.com (Postfix) with ESMTP id 0D25740034 for ; Fri, 26 Aug 2022 15:09:03 +0000 (UTC) Received: by mail-yw1-f201.google.com with SMTP id 00721157ae682-33dce8cae71so29654717b3.8 for ; Fri, 26 Aug 2022 08:09:03 -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=pkHi/aIOhGHio3xjvQCqySH1KrxER9e0jmCiCnEZ6lc=; b=JXeupyYJFRZ1PBcUpgnvFCPsxW7O3TyLbJjLRr3bEw8bazd8AjxTgtEgaL4RFoFAcL axwLX7U/Nn4sG6TkxjstDO07G6IATCfIb/BK3SRzs+9AI2e4/1+GdLwmIEJAttOe66rh Ws/2NAnzJijP6+LerNKmbWUU6tsOs6rlS8LAx5U8e8udODCsNmtDKvJ0M8kzi7mHIsnC 1uQR2PNT1cLqbsuMDArU6TuVj+uqAyRd+Csr8b6p3yrQ9uuoqqQUbbD56/JURxVHp/cJ GEPe8uFr3m7dK02AbiE1YpxJbBPoF8luaPkbHEb4VALaEurXZ1rnMfxSeN0afBuyjAX3 L8uQ== 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=pkHi/aIOhGHio3xjvQCqySH1KrxER9e0jmCiCnEZ6lc=; b=nyeTxBpn73q864IQKkLwlD2geZThUgEHrMMf9bvANFhPkFdNcFalQgrilRfzBmREHk /Tkj7VG2eBW7YoxnAeDazzMd+D65Sk+dw++vjTHNlgfD40p9kRTQRIgemW3Pl73nIShB ovwoSAZ4JowZWe0dZ0O6LW45CnOy6a38fpPOtqFCY4ZC/N6xc0Y2c4X8TWc5an/3SpFc fLytoSVAF+J9TcgE1Wyv8aQWbZOztKndeZcX+AYzv8X8YXWMcXosTH1n9DTn/8svl5th 6OB1Cp65QsNtwtAS4UbjsQpIMywvbqaXTO1nb+sk0GpKiV2uFErEqJ6ZWzsp8c/xo2UX ZsFg== X-Gm-Message-State: ACgBeo1RqSYN0lFnFGvScnmERoZ7BsfLeDQAStEzcYsR59hagkzSEwre 38+RPDBL6/noz8gJvUDxekFaOVI7gYo= X-Google-Smtp-Source: AA6agR7L66nO8W0zasRYtvPf81KdTk/iVgrMnKBKmf6fg3J3ezLsPgPrAE30oNB5jA6Nys8ONQqvG/KyGJI= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a81:d0d:0:b0:333:99b1:44f1 with SMTP id 13-20020a810d0d000000b0033399b144f1mr119968ywn.288.1661526543285; Fri, 26 Aug 2022 08:09:03 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:41 +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-19-glider@google.com> Subject: [PATCH v5 18/44] 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-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=JXeupyYJ; spf=pass (imf27.hostedemail.com: domain of 3D-IIYwYKCBY274z0D2AA270.yA8749GJ-886Hwy6.AD2@flex--glider.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3D-IIYwYKCBY274z0D2AA270.yA8749GJ-886Hwy6.AD2@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526544; a=rsa-sha256; cv=none; b=eqLXh+Rku7b9kEtO9g6/onyutMasPxPL4q7zZkAzKJWZcT4CB+yEmf8rclseC1iGXsSBKb 64Fj84dAjyQYWm//VJIDTy9ACr69A+kGhRExfR4+kPQ1OBeGy5jhyHeq+NcJQj5TfOU87u xuaFKPVvrjIcvsSTVHrxCsnL62DD0Nk= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526544; 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=pkHi/aIOhGHio3xjvQCqySH1KrxER9e0jmCiCnEZ6lc=; b=zQOOkZSCS5FTKK4xtXymDU2PZCoWfmHFjYQBtXURsaRUc92QGRjN5IXbpCN/OB/uG8A4wG /AQGnUWz51dGGuMviZ70fPA0fCUjpL30B3OoTwGNuib+SGvK1qv1Jl/QqEmThNizTPAivQ 1bgjpaQG8V61xF7AiauMIZGDBbLumR4= X-Rspam-User: X-Stat-Signature: xgf3tbfzrw4d8aebixeisxcbn8q3ghwd X-Rspamd-Queue-Id: 0D25740034 Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=JXeupyYJ; spf=pass (imf27.hostedemail.com: domain of 3D-IIYwYKCBY274z0D2AA270.yA8749GJ-886Hwy6.AD2@flex--glider.bounces.google.com designates 209.85.128.201 as permitted sender) smtp.mailfrom=3D-IIYwYKCBY274z0D2AA270.yA8749GJ-886Hwy6.AD2@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspamd-Server: rspam09 X-HE-Tag: 1661526543-377580 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 v5: -- simplify kmsan_copy_to_user() -- provide instrument_get_user() and instrument_put_user() Link: https://linux-review.googlesource.com/id/I43e93b9c02709e6be8d222342f1b044ac8bdbaaf --- include/linux/instrumented.h | 17 +++++++++++++++- include/linux/kmsan-checks.h | 19 ++++++++++++++++++ mm/kmsan/hooks.c | 38 ++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 1 deletion(-) diff --git a/include/linux/instrumented.h b/include/linux/instrumented.h index ee8f7d17d34f5..5f0525d95026a 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,19 @@ 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); } +#define instrument_get_user(to) \ +({ \ + u64 __tmp = (u64)(to); \ + kmsan_unpoison_memory(&__tmp, sizeof(__tmp)); \ + to = __tmp; \ +}) + +#define instrument_put_user(from, ptr, size) \ +({ \ + kmsan_copy_to_user(ptr, &from, sizeof(from), 0); \ +}) + #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 4ab8c629acd0c..a8a03f079a8a5 100644 --- a/mm/kmsan/hooks.c +++ b/mm/kmsan/hooks.c @@ -204,6 +204,44 @@ void kmsan_iounmap_page_range(unsigned long start, unsigned long end) kmsan_leave_runtime(); } +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); + } else { + /* 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) {