Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp448454ybl; Fri, 24 Jan 2020 03:25:49 -0800 (PST) X-Google-Smtp-Source: APXvYqyl8W4w/Zu/NPAf++A5cbF3j7VZOpV+LW96UdsFB3xBlsVKGFBH0ETzi79Je/skCbQOG4OC X-Received: by 2002:a9d:7:: with SMTP id 7mr2232946ota.26.1579865149101; Fri, 24 Jan 2020 03:25:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579865149; cv=none; d=google.com; s=arc-20160816; b=ts8FIzUDHWczIPiLm/xeIZl7rGsfb8A6l2Oo9J94ZxnU+A+qPFAeNbe/Mqc1EqQhcI ZZBTF+kvzJRTxUtfjleF2i7eeUOga9bANdC7yfzPerrmVsTSMyi/sDexAvI1oWLhZrz9 ToZw7UoM4QrX5n8H1Pe2kc7cTbDhLGTdX3IWligB70o3QU9CG8LVmYb7htzJrR6rftXE 40w6JNJZu+9kW33k84bi4fHyrmX59TABGNPIm7vInJbKU4dutXEujebj2Nu4029GYvNe Z8eVSTUUPy9ZuTWoWhYDdsVHsgNHh5YH4pvw22rkXtABQL4zDOs3IPkH+uHJXyyPk3By OcTg== 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=hwzQ3VCUqoiQqyG2DwDT1s000avPDz+MSypapI5GJkU=; b=xiBfWMXFI6Zm6ak1oVcyHLRnmpRSeyXYLSlAneY1U9npL08NGPfGe1/X4TNvPOW2cG 1rg+RWxVlvITle3BcW0kA0p2b68PkleylbLYZj9DZNSbBnpnr+JQSwOP6+JqNoqN1vdY 94ZO1HgEl6ufLI3IBR99n029piDBHkQr6fTGTLZWldQQx5l/XOJR5fdjaGzorVFTsNfr c/vYHhjfmxxTAL3q05ED7dDYlj6K06XPW2RzswHJ2I1dg6Pfa/zBG6YzElLVmUii2q48 Wt0/gcTG0l+fqwVAULgMgE8dbwNl1jpDjCyh5Z7wA0GgCUfPdgsrbJSftWbTlpVFlgro GCIA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=tBdd9AOg; 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 x7si2004794oia.165.2020.01.24.03.25.37; Fri, 24 Jan 2020 03:25:49 -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=tBdd9AOg; 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 S2391306AbgAXLXT (ORCPT + 99 others); Fri, 24 Jan 2020 06:23:19 -0500 Received: from mail-qv1-f66.google.com ([209.85.219.66]:35462 "EHLO mail-qv1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S2391283AbgAXLXJ (ORCPT ); Fri, 24 Jan 2020 06:23:09 -0500 Received: by mail-qv1-f66.google.com with SMTP id u10so716882qvi.2 for ; Fri, 24 Jan 2020 03:23:08 -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=hwzQ3VCUqoiQqyG2DwDT1s000avPDz+MSypapI5GJkU=; b=tBdd9AOgbGoefVh+ESSREOiklsPn1d77K/Z5wFAES3zsRlmecr69hozwJsD6NXOiwr RS9KD3JteWX1fOpwBTGhtclav8Pe0gYHDDdj/dJtvD+vsycy9s7cOC27/EDv+oMXr9RU N0Mvcnkok5z5ftwgqX1r0IoOqXFcd5skObUigB52pxuTzPIxARasiFCgtQq7IIhG6Twp 1e1r4j7OaEBbRPtsVcNZY7L07dR00eBKM0Y3O79/JgZYSKL5kqsKkFitVMMCu6wHkJt4 fezr3Y07wnwe/dKxSkktfScgB9kVXndNX3zywa5Sh/XZL2QwZKzROJpBtv1kp4Dh2zDh Ydjg== 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=hwzQ3VCUqoiQqyG2DwDT1s000avPDz+MSypapI5GJkU=; b=i6JQRlu0qZhoc4VaalJsyVEsHes/KN/d8xqfVG/orv1bsYiRYSpqI1qZ+K9goWJMVO R7rKj1QVQuyur9/U3/v0ZZQdKAbmq0MN3MyFJc5h06wTTQPMDBWpVM8kikh6jeVmIcG5 TEmMV8DZVHopT0QZkojE91Heo/8GUrZ+4RM/oSzHnUnXUhZfM3fwGwNu/GYo6nVwQ+4j RRxE/ARNIlh5xAmIEfP9uDDNUA5ZjJHbkJg0K7xoiYdRYvrkk9K87bSsVB4ZvyCMs/B+ +eHkF61MSNei94EEShkNas7Ul4D54BIYRbKIajH2eiiW3WNrcj4jsv2sbVOf7p5+3dp5 mG5A== X-Gm-Message-State: APjAAAXSmFzVjizZCGLNnR95t0txYDZ8KZQzEFAeEXRtWNKNNtx+MRWK rLw3LUkXt430ANDMmeVDr3HQRGTlvBqQ1xB3M/vUCQ== X-Received: by 2002:a05:6214:1103:: with SMTP id e3mr2207642qvs.159.1579864987764; Fri, 24 Jan 2020 03:23:07 -0800 (PST) MIME-Version: 1.0 References: <20200121160512.70887-1-elver@google.com> In-Reply-To: <20200121160512.70887-1-elver@google.com> From: Dmitry Vyukov Date: Fri, 24 Jan 2020 12:22:56 +0100 Message-ID: Subject: Re: [PATCH v2 1/5] include/linux: Add instrumented.h infrastructure To: Marco Elver Cc: "Paul E. McKenney" , Andrey Konovalov , Alexander Potapenko , kasan-dev , LKML , Mark Rutland , Will Deacon , Peter Zijlstra , Boqun Feng , Arnd Bergmann , Al Viro , Daniel Axtens , Christophe Leroy , Michael Ellerman , Masami Hiramatsu , Steven Rostedt , Ingo Molnar , Christian Brauner , Daniel Borkmann , Kees Cook , cyphar@cyphar.com, 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 Tue, Jan 21, 2020 at 5:05 PM 'Marco Elver' via kasan-dev wrote: > > This adds instrumented.h, which provides generic wrappers for memory > access instrumentation that the compiler cannot emit for various > sanitizers. Currently this unifies KASAN and KCSAN instrumentation. In > future this will also include KMSAN instrumentation. > > Note that, copy_{to,from}_user should use special instrumentation, since > we should be able to instrument both source and destination memory > accesses if both are kernel memory. > > The current patch only instruments the memory access where the address > is always in kernel space, however, both may in fact be kernel addresses > when a compat syscall passes an argument allocated in the kernel to a > real syscall. In a future change, both KASAN and KCSAN should check both > addresses in such cases, as well as KMSAN will make use of both > addresses. [It made more sense to provide the completed function > signature, rather than updating it and changing all locations again at a > later time.] Reviewed-by: Dmitry Vyukov > Suggested-by: Arnd Bergmann > Signed-off-by: Marco Elver > Acked-by: Alexander Potapenko > --- > v2: > * Simplify header, since we currently do not need pre/post user-copy > distinction. > * Make instrument_copy_{to,from}_user function arguments match > copy_{to,from}_user and update rationale in commit message. > --- > include/linux/instrumented.h | 109 +++++++++++++++++++++++++++++++++++ > 1 file changed, 109 insertions(+) > create mode 100644 include/linux/instrumented.h > > diff --git a/include/linux/instrumented.h b/include/linux/instrumented.h > new file mode 100644 > index 000000000000..43e6ea591975 > --- /dev/null > +++ b/include/linux/instrumented.h > @@ -0,0 +1,109 @@ > +/* SPDX-License-Identifier: GPL-2.0 */ > + > +/* > + * This header provides generic wrappers for memory access instrumentation that > + * the compiler cannot emit for: KASAN, KCSAN. > + */ > +#ifndef _LINUX_INSTRUMENTED_H > +#define _LINUX_INSTRUMENTED_H > + > +#include > +#include > +#include > +#include > + > +/** > + * instrument_read - instrument regular read access > + * > + * Instrument a regular read access. The instrumentation should be inserted > + * before the actual read happens. > + * > + * @ptr address of access > + * @size size of access > + */ > +static __always_inline void instrument_read(const volatile void *v, size_t size) > +{ > + kasan_check_read(v, size); > + kcsan_check_read(v, size); > +} > + > +/** > + * instrument_write - instrument regular write access > + * > + * Instrument a regular write access. The instrumentation should be inserted > + * before the actual write happens. > + * > + * @ptr address of access > + * @size size of access > + */ > +static __always_inline void instrument_write(const volatile void *v, size_t size) > +{ > + kasan_check_write(v, size); > + kcsan_check_write(v, size); > +} > + > +/** > + * instrument_atomic_read - instrument atomic read access > + * > + * Instrument an atomic read access. The instrumentation should be inserted > + * before the actual read happens. > + * > + * @ptr address of access > + * @size size of access > + */ > +static __always_inline void instrument_atomic_read(const volatile void *v, size_t size) > +{ > + kasan_check_read(v, size); > + kcsan_check_atomic_read(v, size); > +} > + > +/** > + * instrument_atomic_write - instrument atomic write access > + * > + * Instrument an atomic write access. The instrumentation should be inserted > + * before the actual write happens. > + * > + * @ptr address of access > + * @size size of access > + */ > +static __always_inline void instrument_atomic_write(const volatile void *v, size_t size) > +{ > + kasan_check_write(v, size); > + kcsan_check_atomic_write(v, size); > +} > + > +/** > + * instrument_copy_to_user - instrument reads of copy_to_user > + * > + * Instrument reads from kernel memory, that are due to copy_to_user (and > + * variants). The instrumentation must be inserted before the accesses. > + * > + * @to destination address > + * @from source address > + * @n number of bytes to copy > + */ > +static __always_inline void > +instrument_copy_to_user(void __user *to, const void *from, unsigned long n) > +{ > + kasan_check_read(from, n); > + kcsan_check_read(from, n); > +} > + > +/** > + * instrument_copy_from_user - instrument writes of 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. > + * > + * @to destination address > + * @from source address > + * @n number of bytes to copy > + */ > +static __always_inline void > +instrument_copy_from_user(const void *to, const void __user *from, unsigned long n) > +{ > + kasan_check_write(to, n); > + kcsan_check_write(to, n); > +} > + > +#endif /* _LINUX_INSTRUMENTED_H */ > -- > 2.25.0.341.g760bfbb309-goog > > -- > You received this message because you are subscribed to the Google Groups "kasan-dev" group. > To unsubscribe from this group and stop receiving emails from it, send an email to kasan-dev+unsubscribe@googlegroups.com. > To view this discussion on the web visit https://groups.google.com/d/msgid/kasan-dev/20200121160512.70887-1-elver%40google.com.