From patchwork Tue Mar 29 12:39:33 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12794754 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 7480BC433EF for ; Tue, 29 Mar 2022 12:40:41 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id F2E1D8D000A; Tue, 29 Mar 2022 08:40:40 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id EDE148D0009; Tue, 29 Mar 2022 08:40:40 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D576A8D000A; Tue, 29 Mar 2022 08:40:40 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (relay.hostedemail.com [64.99.140.26]) by kanga.kvack.org (Postfix) with ESMTP id C74798D0009 for ; Tue, 29 Mar 2022 08:40:40 -0400 (EDT) Received: from smtpin14.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay13.hostedemail.com (Postfix) with ESMTP id A167961602 for ; Tue, 29 Mar 2022 12:40:40 +0000 (UTC) X-FDA: 79297382640.14.E2C75F8 Received: from mail-ej1-f73.google.com (mail-ej1-f73.google.com [209.85.218.73]) by imf28.hostedemail.com (Postfix) with ESMTP id 22171C0014 for ; Tue, 29 Mar 2022 12:40:39 +0000 (UTC) Received: by mail-ej1-f73.google.com with SMTP id gv17-20020a1709072bd100b006dfcc7f7962so8125024ejc.5 for ; Tue, 29 Mar 2022 05:40:39 -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=ZbpkAPrcek/iQP5wGrULo2GyXUubszl45zwfN6Jeluw=; b=ItupbKsXf4T5DY5u/yeeKCGIvUjyF/GlXb2acWFwSqEwOouWQJJd75u+lYjEdhwRsk x0pI62vVhg0Y4u8RI7UIPJU/Eqf79K9v97zc3PIqXZQTcfivaabLsxtwYPpNXZLOPN7A SZgOBkXKV3f84Llcqxo97iq6zKz+d2axtqQlOBgHN8rf8y82br0aiI2/9LCooptaQgPP P6ZvYfs4FbSv6hARDwyXMbcMlkWQVBNPbA/3Uoza/8Y05ryJE2Lr1VFxEJTMSrLmTfM4 jNRCJOZhJMXJQQuPXIpHYeQ3+puw+pEYGw6AQKRq8/qV73cd1+HEYhRSrqN+H56zMEzh Uy5A== 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=ZbpkAPrcek/iQP5wGrULo2GyXUubszl45zwfN6Jeluw=; b=Gr7ke32Gjzrs+X4YKriuQeZ64VpColOLDiPJDkrRtJ5zsmksYCVOlrsJ9ib2ONmZIm 72g+3HuP/3HAqt86UWScU6B29VsLFX1mCepXDzHyyUatotvc4JHAi8Qzf6v5QV0fyPWh nHMSC3ScmoBBXafv+5tNWDHNJKmlXhis0YkQay47Pi88nLvQN09u0lS61m7hsy4pGQYq oqK57p2RqSN6om8jEGQFKa5DgnaqsG979GgYNvl6ZCppKDxdwek85UBFAUCB156SKvoZ IKLdz/EiUdhaklWQf2Eftjx50/eSp//MB+ZT0u6/mGiRXElJ2HA4wfsOhF20Gq5kQWVU ckAw== X-Gm-Message-State: AOAM530bE7SNBoulnRkxhsbMU5C0S65w6et6MS6OUaCbYIS/69yyQOm7 tFcocS0+RZcj5p3tVNtaI2E60VZ0dEA= X-Google-Smtp-Source: ABdhPJzbGaMHRSN6Foq0MN7pYOwtpEK1d+tqys1HKVelbpVg4A40evROqReC43MTXMy6pYq67i6tt1aBecs= X-Received: from glider.muc.corp.google.com ([2a00:79e0:15:13:36eb:759:798f:98c3]) (user=glider job=sendgmr) by 2002:a17:906:dc8b:b0:6df:7a71:1321 with SMTP id cs11-20020a170906dc8b00b006df7a711321mr33531777ejc.476.1648557638735; Tue, 29 Mar 2022 05:40:38 -0700 (PDT) Date: Tue, 29 Mar 2022 14:39:33 +0200 In-Reply-To: <20220329124017.737571-1-glider@google.com> Message-Id: <20220329124017.737571-5-glider@google.com> Mime-Version: 1.0 References: <20220329124017.737571-1-glider@google.com> X-Mailer: git-send-email 2.35.1.1021.g381101b075-goog Subject: [PATCH v2 04/48] instrumented.h: allow instrumenting both sides of copy_from_user() From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , 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 , linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org X-Stat-Signature: 1cw5xi3rk793ptzhuc6yios7tnxmzrq7 Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=ItupbKsX; spf=pass (imf28.hostedemail.com: domain of 3Rv5CYgYKCGcLQNIJWLTTLQJ.HTRQNSZc-RRPaFHP.TWL@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=3Rv5CYgYKCGcLQNIJWLTTLQJ.HTRQNSZc-RRPaFHP.TWL@flex--glider.bounces.google.com; dmarc=pass (policy=reject) header.from=google.com X-Rspam-User: X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 22171C0014 X-HE-Tag: 1648557639-836487 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: Introduce instrument_copy_from_user_before() and instrument_copy_from_user_after() hooks to be invoked before and after the call to copy_from_user(). KASAN and KCSAN will be only using instrument_copy_from_user_before(), but for KMSAN we'll need to insert code after copy_from_user(). Signed-off-by: Alexander Potapenko --- Link: https://linux-review.googlesource.com/id/I855034578f0b0f126734cbd734fb4ae1d3a6af99 --- include/linux/instrumented.h | 21 +++++++++++++++++++-- include/linux/uaccess.h | 19 ++++++++++++++----- lib/iov_iter.c | 9 ++++++--- lib/usercopy.c | 3 ++- 4 files changed, 41 insertions(+), 11 deletions(-) diff --git a/include/linux/instrumented.h b/include/linux/instrumented.h index 42faebbaa202a..ee8f7d17d34f5 100644 --- a/include/linux/instrumented.h +++ b/include/linux/instrumented.h @@ -120,7 +120,7 @@ instrument_copy_to_user(void __user *to, const void *from, unsigned long n) } /** - * instrument_copy_from_user - instrument writes of copy_from_user + * instrument_copy_from_user_before - add instrumentation before copy_from_user * * Instrument writes to kernel memory, that are due to copy_from_user (and * variants). The instrumentation should be inserted before the accesses. @@ -130,10 +130,27 @@ instrument_copy_to_user(void __user *to, const void *from, unsigned long n) * @n number of bytes to copy */ static __always_inline void -instrument_copy_from_user(const void *to, const void __user *from, unsigned long n) +instrument_copy_from_user_before(const void *to, const void __user *from, unsigned long n) { kasan_check_write(to, n); kcsan_check_write(to, n); } +/** + * instrument_copy_from_user_after - add instrumentation after copy_from_user + * + * Instrument writes to kernel memory, that are due to copy_from_user (and + * variants). The instrumentation should be inserted after the accesses. + * + * @to destination address + * @from source address + * @n number of bytes to copy + * @left number of bytes not copied (as returned by copy_from_user) + */ +static __always_inline void +instrument_copy_from_user_after(const void *to, const void __user *from, + unsigned long n, unsigned long left) +{ +} + #endif /* _LINUX_INSTRUMENTED_H */ diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h index ac0394087f7d4..8dadd8642afbb 100644 --- a/include/linux/uaccess.h +++ b/include/linux/uaccess.h @@ -98,20 +98,28 @@ static inline void force_uaccess_end(mm_segment_t oldfs) static __always_inline __must_check unsigned long __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n) { - instrument_copy_from_user(to, from, n); + unsigned long res; + + instrument_copy_from_user_before(to, from, n); check_object_size(to, n, false); - return raw_copy_from_user(to, from, n); + res = raw_copy_from_user(to, from, n); + instrument_copy_from_user_after(to, from, n, res); + return res; } static __always_inline __must_check unsigned long __copy_from_user(void *to, const void __user *from, unsigned long n) { + unsigned long res; + might_fault(); + instrument_copy_from_user_before(to, from, n); if (should_fail_usercopy()) return n; - instrument_copy_from_user(to, from, n); check_object_size(to, n, false); - return raw_copy_from_user(to, from, n); + res = raw_copy_from_user(to, from, n); + instrument_copy_from_user_after(to, from, n, res); + return res; } /** @@ -155,8 +163,9 @@ _copy_from_user(void *to, const void __user *from, unsigned long n) unsigned long res = n; might_fault(); if (!should_fail_usercopy() && likely(access_ok(from, n))) { - instrument_copy_from_user(to, from, n); + instrument_copy_from_user_before(to, from, n); res = raw_copy_from_user(to, from, n); + instrument_copy_from_user_after(to, from, n, res); } if (unlikely(res)) memset(to + (n - res), 0, res); diff --git a/lib/iov_iter.c b/lib/iov_iter.c index 6dd5330f7a995..fb19401c29c4f 100644 --- a/lib/iov_iter.c +++ b/lib/iov_iter.c @@ -159,13 +159,16 @@ static int copyout(void __user *to, const void *from, size_t n) static int copyin(void *to, const void __user *from, size_t n) { + size_t res = n; + if (should_fail_usercopy()) return n; if (access_ok(from, n)) { - instrument_copy_from_user(to, from, n); - n = raw_copy_from_user(to, from, n); + instrument_copy_from_user_before(to, from, n); + res = raw_copy_from_user(to, from, n); + instrument_copy_from_user_after(to, from, n, res); } - return n; + return res; } static size_t copy_page_to_iter_iovec(struct page *page, size_t offset, size_t bytes, diff --git a/lib/usercopy.c b/lib/usercopy.c index 7413dd300516e..1505a52f23a01 100644 --- a/lib/usercopy.c +++ b/lib/usercopy.c @@ -12,8 +12,9 @@ unsigned long _copy_from_user(void *to, const void __user *from, unsigned long n unsigned long res = n; might_fault(); if (!should_fail_usercopy() && likely(access_ok(from, n))) { - instrument_copy_from_user(to, from, n); + instrument_copy_from_user_before(to, from, n); res = raw_copy_from_user(to, from, n); + instrument_copy_from_user_after(to, from, n, res); } if (unlikely(res)) memset(to + (n - res), 0, res);