Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp3855378ybl; Tue, 21 Jan 2020 08:15:58 -0800 (PST) X-Google-Smtp-Source: APXvYqwR6npVhAO5mi9PDqYw9sqbOa9n0+wdrKsz1xxqrZBn9PxMqhJ7/IdkslW4qeujhP4QGJV/ X-Received: by 2002:a9d:7b51:: with SMTP id f17mr4165857oto.302.1579623358520; Tue, 21 Jan 2020 08:15:58 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579623358; cv=none; d=google.com; s=arc-20160816; b=0qY8FavB5/eta6lGP7bXTF5PokNCGqgvJ2Hpk9/w974DJPzaI4tL+tikFG4FGY9rKy iJ4l6r95DhFu1esOWyb41sF3iachbsbXsoADIqL8kKcDseGxwbEoKO3YTzwINA6baYSR hK0A4gbWkb7boSFj7BRG65ZcytnDGp5Fy34DVY2i54VnLRdJ7WeaJVNnyD33NHeA1Aiu 4aFx8m6N+lP/gVuPXLrTkYryWWW3PnOMG4v/CSqtU6H1KSPrkgTRF/Ecaxs1sMearbH5 QsqleYHT9iNG6uOiW2gpfonrI0a9tDWjszNvOAu+YfUJsTqQ+y3Hq5E0yZ1Cttt3NqcA qCHQ== 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=IXf7v5u4QW5b0oUhqlEbSYsuPaK7kUXwuaQl8tSfrCE=; b=mjm8nB/mnwlCH57G7X88z547KaFPnMebn4rV+5ujLysoIYLk5snq8N9Sb3Ej67gxNh XECASWWx/VyDCKZFYycOrZzyihQWzhQqUpBtqG/BC3ELvWJ4RY3F41HyjzIhqI23XRRm Jr6jw606zzqDY401g/Arslo/3DGnc5CxY2hnQx35g2O1wsCFiR11xTWfV7ygtpIC92tV FCrFlxJBiobjrl05Zn/sytKU+/AQKtv+G2KxshvyYvYZCPoUXgIf/Q11B24yKWE9dqVu 5DzgHpaRs4UlUdF5FN9r9UusPJxq/H5OUnH2NmjEromF3MqE8x0CbPjCPJOPMZGvv2Oe UMJg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="D/Pop3pi"; 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 a14si20155604oid.58.2020.01.21.08.15.44; Tue, 21 Jan 2020 08:15:58 -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="D/Pop3pi"; 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 S1729366AbgAUQOU (ORCPT + 99 others); Tue, 21 Jan 2020 11:14:20 -0500 Received: from mail-oi1-f195.google.com ([209.85.167.195]:32835 "EHLO mail-oi1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729045AbgAUQOS (ORCPT ); Tue, 21 Jan 2020 11:14:18 -0500 Received: by mail-oi1-f195.google.com with SMTP id q81so3094110oig.0 for ; Tue, 21 Jan 2020 08:14:17 -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=IXf7v5u4QW5b0oUhqlEbSYsuPaK7kUXwuaQl8tSfrCE=; b=D/Pop3pi/9T/J/oSC1bENHywcgIYAFOPdwbx0MZrz7SJzbwnbXbwEHEtiexEN5OR6w K3pzmf5ThEt1CKco6axerkBYKjN2f7T/IPcVceQcETFetnu+tgnhRan+8LBYwMN2+EsI 0oSmjMRQ5JpH1+QZPW8Ql8uOjW34wd5/1uV7QnvWT6NvF2wgAd6Jl1ofygKTujFGE83o 5xKnDfG6RZr5T9CnI0LoQL5LVm8aKF+bY+qFV2cYSrLiSV6BJI1WiwddD/3/dUhyidQz 9gjFnKPglx7sMKHDEFzBOI0A72G84TjJYZ1t0vzif9x10I0ne+T9eg3XfpEfuYRsm2jr W30A== 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=IXf7v5u4QW5b0oUhqlEbSYsuPaK7kUXwuaQl8tSfrCE=; b=bl4V11K04NbdcZRuu7MgXhC9wNR9pv3EYdUhKs4hcaF3fWXK1yVlz8NqvEXPGAu17O oVZJ/TQ4DJNRkkW2KSlvFj5xEw7hRxKN9IcHNYwFjKczC6rST/yHGRdsT3mA85+Uo2U1 ADBll/xtlzKygy/ENy21WCM0iU+VUd6RJnDagTtLeC4qy0sW++um3F3/Ckxj+8LVI44R 1tYDmoV3SL/2zmjXRAqzVuPQZ+csUips+Z1Bvxo4Gp77RsNk/aZeuhi/cpsgRWFzUc50 ptrw15N2yNRCQ2dVRbd1I2V3i4pRieOP8NQ9R65Ef/LGWxwTqtn1JEkoEUY46yTLSPj+ Txvw== X-Gm-Message-State: APjAAAXyAF5+HqWFApM/xJ9swOk1lMnBBz0TNTYyHdqJsXueb7ttCpdJ CiQsW92vwXe3fD67RlZ0EzwpgrlyTRALaNy8jg+ZwzatuTeTiw== X-Received: by 2002:aca:2112:: with SMTP id 18mr3379090oiz.155.1579623256839; Tue, 21 Jan 2020 08:14:16 -0800 (PST) MIME-Version: 1.0 References: <20200120141927.114373-1-elver@google.com> In-Reply-To: From: Marco Elver Date: Tue, 21 Jan 2020 17:14:05 +0100 Message-ID: Subject: Re: [PATCH 1/5] include/linux: Add instrumented.h infrastructure 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 Tue, 21 Jan 2020 at 14:01, Dmitry Vyukov wrote: > > On Mon, Jan 20, 2020 at 3:45 PM Dmitry Vyukov wrote: > > > > On Mon, Jan 20, 2020 at 3:19 PM Marco Elver 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 require special instrumentation, > > > providing hooks before and after the access, since we may need to know > > > the actual bytes accessed (currently this is relevant for KCSAN, and is > > > also relevant in future for KMSAN). > > > > > > Suggested-by: Arnd Bergmann > > > Signed-off-by: Marco Elver > > > --- > > > include/linux/instrumented.h | 153 +++++++++++++++++++++++++++++++++++ > > > 1 file changed, 153 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..9f83c8520223 > > > --- /dev/null > > > +++ b/include/linux/instrumented.h > > > @@ -0,0 +1,153 @@ > > > +/* 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 > > > + */ > > > > Based on offline discussion, that's what we add for KMSAN: > > > > > +static __always_inline void instrument_read(const volatile void *v, size_t size) > > > +{ > > > + kasan_check_read(v, size); > > > + kcsan_check_read(v, size); > > > > KMSAN: nothing > > > > > +} > > > + > > > +/** > > > + * 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); > > > > KMSAN: nothing > > > > > +} > > > + > > > +/** > > > + * 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); > > > > KMSAN: nothing > > > > > +} > > > + > > > +/** > > > + * 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); > > > > KMSAN: nothing > > > > > +} > > > + > > > +/** > > > + * instrument_copy_to_user_pre - 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. At this point the > > > + * actual number of bytes accessed is not yet known. > > > + * > > > + * @dst destination address > > > + * @size maximum access size > > > + */ > > > +static __always_inline void > > > +instrument_copy_to_user_pre(const volatile void *src, size_t size) > > > +{ > > > + /* Check before, to warn before potential memory corruption. */ > > > + kasan_check_read(src, size); > > > > KMSAN: check that (src,size) is initialized > > > > > +} > > > + > > > +/** > > > + * instrument_copy_to_user_post - 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 after the accesses. At this point the > > > + * actual number of bytes accessed should be known. > > > + * > > > + * @dst destination address > > > + * @size maximum access size > > > + * @left number of bytes left that were not copied > > > + */ > > > +static __always_inline void > > > +instrument_copy_to_user_post(const volatile void *src, size_t size, size_t left) > > > +{ > > > + /* Check after, to avoid false positive if memory was not accessed. */ > > > + kcsan_check_read(src, size - left); > > > > KMSAN: nothing > > One detail I noticed for KMSAN is that kmsan_copy_to_user has a > special case when @to address is in kernel-space (compat syscalls > doing tricky things), in that case it only copies metadata. We can't > handle this with existing annotations. > > > * 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(const void *to, const void *from, size_t > to_copy, size_t left); Sent v2: http://lkml.kernel.org/r/20200121160512.70887-1-elver@google.com I hope it'll satisfy our various constraints for now. Thanks, -- Marco