From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-14.4 required=3.0 tests=DKIMWL_WL_MED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id C54E9C2D0CE for ; Tue, 21 Jan 2020 13:01:28 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 8D7EF217F4 for ; Tue, 21 Jan 2020 13:01:28 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="FCqcb4yg" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728733AbgAUNB1 (ORCPT ); Tue, 21 Jan 2020 08:01:27 -0500 Received: from mail-qv1-f68.google.com ([209.85.219.68]:35156 "EHLO mail-qv1-f68.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727059AbgAUNB1 (ORCPT ); Tue, 21 Jan 2020 08:01:27 -0500 Received: by mail-qv1-f68.google.com with SMTP id u10so1379343qvi.2 for ; Tue, 21 Jan 2020 05:01:26 -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=Z9x75RFBujs35vp0kR9dshO1qIssZRWX2b+4uOymJ80=; b=FCqcb4ygTmSBXXYx4QWOeURFbw2FrZIkV2jK+GI0RDrqM0YVXN0Uovq1h2on15KGzE 3cKrBBsI+Kb089CmH9jgl7IzkYUNIH/bYmArxTDum6UnYykFFVJiAtvAfGsqaNpaREI0 K5BJrYvIgf897fvawh7hR8+yNaPTiqWJJvpAcYeWAi64Mjmq0hg+GdYl6o9xUp7/JRCN DRG6+FleFXdEKjYuWJDIKV8lnhL77igKISKBXzpJ8k5nFopWlnDSEc8RpjJld0Cfdmt1 5MIKMSLhqrrOf7ZVxjw1CoeTn4SUIJ84jhadU742gu2A2hBSuQMe1xX8Z3rTfJJhOnoK wwkA== 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=Z9x75RFBujs35vp0kR9dshO1qIssZRWX2b+4uOymJ80=; b=Dr5KTGhn6aQ+xa2cZp1+qnG5+qacKb8SwLD1R1g4Oe57E/6RBh2tDF21rPhx6QJywx x/2tblPRqRDPl01/3O2DtB4mGGzZF2qjJsPSvZ8h+zdjWEFkmVK5ZPcSKsCtCW54oLLr go6d4jwMM7xgcqDbIEnU8etTDqXwLyBuxrfU1RZovtIkVW0mPjHy4Dx2Ji0Bk+EUbdUR 0z/x3PKvVzOjawDFG8Mm7QbvqOpbH0q0A86BLc1/zQuFA9i9e73J7Zi/Y8D4vE3SW883 L4LeF2Kqo3Reh8cmSVbTRrxgNvarpM84L1X/sPqeQApowH8CJDuxWv2FFazqVMSnrVMn ehpA== X-Gm-Message-State: APjAAAUfH8bJLoifr/uEtfJZ1wqyAvDYqAiNAC+HKYEG1BdWgzbly+bX CgWaS3BXYcz0ezLZ9uFo3HxX2cIwW8HlTNF1mgg+mw== X-Google-Smtp-Source: APXvYqzTs36y76+Ax1v7ZHC0Ws6VwBO4HXtUDbFUhZveQnJnQSlEBxHmvNMaydYMvKVFdtV7j0xnLpmxsRnTAiuAAKA= X-Received: by 2002:ad4:5a53:: with SMTP id ej19mr4652000qvb.34.1579611685771; Tue, 21 Jan 2020 05:01:25 -0800 (PST) MIME-Version: 1.0 References: <20200120141927.114373-1-elver@google.com> In-Reply-To: From: Dmitry Vyukov Date: Tue, 21 Jan 2020 14:01:13 +0100 Message-ID: Subject: Re: [PATCH 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 , 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, 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); > > +} > > + > > +/** > > + * instrument_copy_from_user_pre - instrument writes of copy_from_user > > + * > > + * Instrument writes to kernel memory, that are due to copy_from_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_from_user_pre(const volatile void *dst, size_t size) > > +{ > > + /* Check before, to warn before potential memory corruption. */ > > + kasan_check_write(dst, size); > > KMSAN: nothing > > > +} > > + > > +/** > > + * instrument_copy_from_user_post - instrument writes of copy_from_user > > + * > > + * Instrument writes to kernel memory, that are due to copy_from_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_from_user_post(const volatile void *dst, size_t size, size_t left) > > +{ > > + /* Check after, to avoid false positive if memory was not accessed. */ > > + kcsan_check_write(dst, size - left); > > KMSAN: mark (dst, size-left) as initialized > > > +} > > + > > +#endif /* _LINUX_INSTRUMENTED_H */ > > -- > > 2.25.0.341.g760bfbb309-goog > > From mboxrd@z Thu Jan 1 00:00:00 1970 From: Dmitry Vyukov Subject: Re: [PATCH 1/5] include/linux: Add instrumented.h infrastructure Date: Tue, 21 Jan 2020 14:01:13 +0100 Message-ID: References: <20200120141927.114373-1-elver@google.com> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Return-path: In-Reply-To: Sender: linux-kernel-owner@vger.kernel.org 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 , Christophe Leroy , Daniel Axtens , Michael Ellerman , Steven Rostedt , Masami Hiramatsu , Ingo Molnar , Christian Brauner , Daniel Borkmann , cyphar@cyphar.comK List-Id: linux-arch.vger.kernel.org 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); > > +} > > + > > +/** > > + * instrument_copy_from_user_pre - instrument writes of copy_from_user > > + * > > + * Instrument writes to kernel memory, that are due to copy_from_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_from_user_pre(const volatile void *dst, size_t size) > > +{ > > + /* Check before, to warn before potential memory corruption. */ > > + kasan_check_write(dst, size); > > KMSAN: nothing > > > +} > > + > > +/** > > + * instrument_copy_from_user_post - instrument writes of copy_from_user > > + * > > + * Instrument writes to kernel memory, that are due to copy_from_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_from_user_post(const volatile void *dst, size_t size, size_t left) > > +{ > > + /* Check after, to avoid false positive if memory was not accessed. */ > > + kcsan_check_write(dst, size - left); > > KMSAN: mark (dst, size-left) as initialized > > > +} > > + > > +#endif /* _LINUX_INSTRUMENTED_H */ > > -- > > 2.25.0.341.g760bfbb309-goog > >