Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp2662026ybl; Mon, 20 Jan 2020 07:07:36 -0800 (PST) X-Google-Smtp-Source: APXvYqzcQHKen17+P+b2+h2eOp99R5Rw7YR43bcYMBFedF5tONEdahfLJJ471yMXUIFRp1LweeKC X-Received: by 2002:a9d:5545:: with SMTP id h5mr16724797oti.296.1579532856802; Mon, 20 Jan 2020 07:07:36 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579532856; cv=none; d=google.com; s=arc-20160816; b=CupJJzrXTDuUk/5a9HCPYImfKRQRJYMMEPTRz9yQpzdAs92PLezCBZP4SQ1VXfYjAC ZssWtzlJhRAhtdt36TEPjXPG6QP98x0P31L6MOp5qQvgRIZYAWF0CALxuqBqrhORzvEX qSjlgdCKIK6Q7Da4T4ftrWPEbGxG98YEruGkPsKvI17e8JmmfnP2Rh6xxcsAOJrSeFmf 2KMp1kPKVhbPynA40IiLglYrMtsP5TExEUF12j3HIcrnKrHDX/7MueetkmPIiwhv9KM7 +WfsQ3JwIkyBVmSlXfZgwG8HshhOoXRLmexXYgjyoaOG1aRwJlFuWNc94m1KGCoTGP3X 1qPA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=smLCZaIYcQF0/52Z19iYa5thG5yG//BBZdb/jdacqzg=; b=Zx9e2XxSZxS8Db2VeEhZ+oJbgnmghsIR9X5hdOXYhJH/QLeG8+d6WoDCplZLoqKCD0 t2WXbrNil/7f85vd4iLKbc++GH96eAMY3l59eTSEDglvb7vyBiDjJOFaB+xdNH/dXCOF +WRDOT/HuNC2l0bU7zChXW/ZKb6VUF+OE3KCeM4S24CSwU0pHjYlIuo+tY5qr8/jIeoT 98OH9zQRdJQOdiu2KcoU93FGy92A6WgQGhrscgp+3Sa8xhegd44RXYrSWeJDYgaUex7W qmU09dwhcFL9rTE8olBj2/FOGyCSEBlutsUaz9c289/0lXgh5wJXLsGRPnbBGU35hjKJ FL/w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=tojSwQu1; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id l186si17591980oib.226.2020.01.20.07.07.24; Mon, 20 Jan 2020 07:07:36 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=tojSwQu1; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 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 S1729094AbgATPF4 (ORCPT + 99 others); Mon, 20 Jan 2020 10:05:56 -0500 Received: from mail-ot1-f67.google.com ([209.85.210.67]:38649 "EHLO mail-ot1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729076AbgATPF4 (ORCPT ); Mon, 20 Jan 2020 10:05:56 -0500 Received: by mail-ot1-f67.google.com with SMTP id z9so10033oth.5 for ; Mon, 20 Jan 2020 07:05:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=smLCZaIYcQF0/52Z19iYa5thG5yG//BBZdb/jdacqzg=; b=tojSwQu1bHYhbIog279hFH4kK/1efQmq3Nw5UqFpyhOHvgdGZIQwaKootMm732MoaR 5uvPwP9TZbXXRqp2YYGWklkJjC1I7wBT5aHBrLDAysGHrTKeZXC4sO+3etTTIOHrkpTI q0hqHd/50k0CXZ9Y0L3+sebhGigshfjrvYhpcrBkYdcnm7/H9JZzGN4YusJQFW4a4vHY WJl0PVj/Dx2oSsTd1mOPgKK6CzhHZle3m0T5dhljPmXpV+blTBDtH7oao5wAKidwgKxI FhHPlmJ7dE2yq2Rc02bDoOhbJGPJcoPpiH6xBAQmFBEvk+AozgxVtu1+/n9vbxVA/CX1 OAkw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=smLCZaIYcQF0/52Z19iYa5thG5yG//BBZdb/jdacqzg=; b=B2gejILCyNZiZAYNknR/d+BkfZ4HUM/oFf3uRhKNHq+CulJ7GiedxjyHoEFRRclaL+ /0A0kMb0HzgUtg//nGXG4a5MZyzjAETqRFJbFDX6DaPvF5xch0Q8EyncXqdqbkHS0wkL J260ze9ExYGvkHKyxsFzEwKUCSIFqUyLUKq2zi2EO3l1+KEYaUf90+3rgmvDr3O7Cbxb ykijAlkWZ6bmJAQVMJqRva4U01bfejbIDPzLDQs8HPoildWxjX/o8n5+9qCZPTxqTCL+ daU6XxwSyreXDunnDo5DSgw1jO9RRWfYoO4c+t24PSfunT9rAO9rk4maGmZVpBpEKBG6 TLaA== X-Gm-Message-State: APjAAAV3JWhro9I8UtKXCiN6z8eAoJslYRZBwqTOTESgchwoKa2KJjze PDBZyRNDJNfxnAN+3mQYGhC/6mcUCYhBExYePOYRHg== X-Received: by 2002:a9d:7410:: with SMTP id n16mr16771057otk.23.1579532754151; Mon, 20 Jan 2020 07:05:54 -0800 (PST) MIME-Version: 1.0 References: <20200120141927.114373-1-elver@google.com> <20200120141927.114373-5-elver@google.com> In-Reply-To: From: Marco Elver Date: Mon, 20 Jan 2020 16:05:42 +0100 Message-ID: Subject: Re: [PATCH 5/5] copy_to_user, copy_from_user: Use generic instrumented.h To: Dmitry Vyukov Cc: "Paul E. McKenney" , Andrey Konovalov , Alexander Potapenko , kasan-dev , LKML , Mark Rutland , Will Deacon , Peter Zijlstra , Boqun Feng , Arnd Bergmann , Al Viro , Christophe Leroy , Daniel Axtens , Michael Ellerman , Steven Rostedt , Masami Hiramatsu , Ingo Molnar , Christian Brauner , Daniel Borkmann , cyphar@cyphar.com, Kees Cook , linux-arch Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 20 Jan 2020 at 15:52, Dmitry Vyukov wrote: > > On Mon, Jan 20, 2020 at 3:19 PM Marco Elver wrote: > > > > This replaces the KASAN instrumentation with generic instrumentation, > > implicitly adding KCSAN instrumentation support. > > > > For KASAN no functional change is intended. > > > > Suggested-by: Arnd Bergmann > > Signed-off-by: Marco Elver > > --- > > include/linux/uaccess.h | 46 +++++++++++++++++++++++++++++------------ > > lib/usercopy.c | 14 ++++++++----- > > 2 files changed, 42 insertions(+), 18 deletions(-) > > > > diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h > > index 67f016010aad..d3f2d9a8cae3 100644 > > --- a/include/linux/uaccess.h > > +++ b/include/linux/uaccess.h > > @@ -2,9 +2,9 @@ > > #ifndef __LINUX_UACCESS_H__ > > #define __LINUX_UACCESS_H__ > > > > +#include > > #include > > #include > > -#include > > > > #define uaccess_kernel() segment_eq(get_fs(), KERNEL_DS) > > > > @@ -58,18 +58,26 @@ > > static __always_inline __must_check unsigned long > > __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n) > > { > > - kasan_check_write(to, n); > > + unsigned long res; > > + > > check_object_size(to, n, false); > > - return raw_copy_from_user(to, from, n); > > + instrument_copy_from_user_pre(to, n); > > + res = raw_copy_from_user(to, from, n); > > + instrument_copy_from_user_post(to, n, res); > > + return res; > > } > > There is also something called strncpy_from_user() that has kasan > instrumentation now: > https://elixir.bootlin.com/linux/v5.5-rc6/source/lib/strncpy_from_user.c#L117 Yes, however, I think it's a special case for KASAN. The implementation is already instrumented by the compiler. In the original commit it says (1771c6e1a567e): "Note: Unlike others strncpy_from_user() is written mostly in C and KASAN sees memory accesses in it. However, it makes sense to add explicit check for all @count bytes that *potentially* could be written to the kernel." I don't think we want unconditional double-instrumentation here. Let me know if you think otherwise. Thanks, -- Marco > > static __always_inline __must_check unsigned long > > __copy_from_user(void *to, const void __user *from, unsigned long n) > > { > > + unsigned long res; > > + > > might_fault(); > > - kasan_check_write(to, n); > > check_object_size(to, n, false); > > - return raw_copy_from_user(to, from, n); > > + instrument_copy_from_user_pre(to, n); > > + res = raw_copy_from_user(to, from, n); > > + instrument_copy_from_user_post(to, n, res); > > + return res; > > } > > > > /** > > @@ -88,18 +96,26 @@ __copy_from_user(void *to, const void __user *from, unsigned long n) > > static __always_inline __must_check unsigned long > > __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n) > > { > > - kasan_check_read(from, n); > > + unsigned long res; > > + > > check_object_size(from, n, true); > > - return raw_copy_to_user(to, from, n); > > + instrument_copy_to_user_pre(from, n); > > + res = raw_copy_to_user(to, from, n); > > + instrument_copy_to_user_post(from, n, res); > > + return res; > > } > > > > static __always_inline __must_check unsigned long > > __copy_to_user(void __user *to, const void *from, unsigned long n) > > { > > + unsigned long res; > > + > > might_fault(); > > - kasan_check_read(from, n); > > check_object_size(from, n, true); > > - return raw_copy_to_user(to, from, n); > > + instrument_copy_to_user_pre(from, n); > > + res = raw_copy_to_user(to, from, n); > > + instrument_copy_to_user_post(from, n, res); > > + return res; > > } > > > > #ifdef INLINE_COPY_FROM_USER > > @@ -109,8 +125,9 @@ _copy_from_user(void *to, const void __user *from, unsigned long n) > > unsigned long res = n; > > might_fault(); > > if (likely(access_ok(from, n))) { > > - kasan_check_write(to, n); > > + instrument_copy_from_user_pre(to, n); > > res = raw_copy_from_user(to, from, n); > > + instrument_copy_from_user_post(to, n, res); > > } > > if (unlikely(res)) > > memset(to + (n - res), 0, res); > > @@ -125,12 +142,15 @@ _copy_from_user(void *, const void __user *, unsigned long); > > static inline __must_check unsigned long > > _copy_to_user(void __user *to, const void *from, unsigned long n) > > { > > + unsigned long res = n; > > + > > might_fault(); > > if (access_ok(to, n)) { > > - kasan_check_read(from, n); > > - n = raw_copy_to_user(to, from, n); > > + instrument_copy_to_user_pre(from, n); > > + res = raw_copy_to_user(to, from, n); > > + instrument_copy_to_user_post(from, n, res); > > } > > - return n; > > + return res; > > } > > #else > > extern __must_check unsigned long > > diff --git a/lib/usercopy.c b/lib/usercopy.c > > index cbb4d9ec00f2..1c20d4423b86 100644 > > --- a/lib/usercopy.c > > +++ b/lib/usercopy.c > > @@ -1,6 +1,7 @@ > > // SPDX-License-Identifier: GPL-2.0 > > -#include > > #include > > +#include > > +#include > > > > /* out-of-line parts */ > > > > @@ -10,8 +11,9 @@ unsigned long _copy_from_user(void *to, const void __user *from, unsigned long n > > unsigned long res = n; > > might_fault(); > > if (likely(access_ok(from, n))) { > > - kasan_check_write(to, n); > > + instrument_copy_from_user_pre(to, n); > > res = raw_copy_from_user(to, from, n); > > + instrument_copy_from_user_post(to, n, res); > > } > > if (unlikely(res)) > > memset(to + (n - res), 0, res); > > @@ -23,12 +25,14 @@ EXPORT_SYMBOL(_copy_from_user); > > #ifndef INLINE_COPY_TO_USER > > unsigned long _copy_to_user(void __user *to, const void *from, unsigned long n) > > { > > + unsigned long res = n; > > might_fault(); > > if (likely(access_ok(to, n))) { > > - kasan_check_read(from, n); > > - n = raw_copy_to_user(to, from, n); > > + instrument_copy_to_user_pre(from, n); > > + res = raw_copy_to_user(to, from, n); > > + instrument_copy_to_user_post(from, n, res); > > } > > - return n; > > + return res; > > } > > EXPORT_SYMBOL(_copy_to_user); > > #endif > > -- > > 2.25.0.341.g760bfbb309-goog > >