Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp2720869ybl; Mon, 20 Jan 2020 08:09:02 -0800 (PST) X-Google-Smtp-Source: APXvYqz3yRsgfh3MzIGD068Kj//OX4j5F431Bxrv0HACq+DDuHGLfMVcYg8JCjMrnyedJiEl21si X-Received: by 2002:aca:2b0a:: with SMTP id i10mr13534241oik.137.1579536542496; Mon, 20 Jan 2020 08:09:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579536542; cv=none; d=google.com; s=arc-20160816; b=mS3d+pzDhrz6qSH+sVCSJ6tspw+01M+J2UGQV1NwvZ3jfmf/s/4PBSgKAgS6BGxr77 AJsaCYOVJuIMxuW/9A7cvqNHNaYCjfOO5y1XHZ7cUUbm/nPRkbBozOwdle4iBP5Cbw+H WBi087E/vt7VMCzSXvrpqoNhLC83qRH7yV1ThUtie5JuFlITCvi1UX2cE2uR8vgR+x2D LRO0NMT48Md049/xJI1DBNfQV4NVss5HJQCv8ezj44NdFHIS+POuOAb0wkTMBcLmdaS0 z9Nn0EeNQ4Tfr/eqYmGVWUNoKFZ4tqG7HOCNvNJ1hO0zIRZ5jxxhZxOhjE6WpNSHs2wm /+8w== 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=rKBXxvKIRJE9r95Oq/vyPWMrKks8UEDi+Qo4dUocXJw=; b=xI5MrrKbGTv/DUgoJ73nyx2xfsdpFKoJvf+rNmLdFFF0urkRI+NcgM2VQWpBxau2Lb eCxZT8mmTZVXZIbRlfSTCFAzP4EOIjXm4BeE5Nvhys9vzzW/uKY6pdnup8K/b4e3QD43 Z3kJspJ0NYhJ/PPFxJZt8TdhK4Bk7zMzADZS8ShMGYdASwwVaWCARwe0nJt6U/rOIgdD tqXu8UZGH4WtralTw7BVEW9g7Tnhe4Nz/e8/ba8GYx978euJtqEaBICKpSXFig4Y3Lgd dEfTswhzEtU4/VKpH+4bq2H6zvA/JJKqe15DrQaeYSjgQhuyvJlnvwkIi7NQita0gQ5C vNwA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Baf6RJnN; 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 c186si17757625oib.103.2020.01.20.08.08.50; Mon, 20 Jan 2020 08:09:02 -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=Baf6RJnN; 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 S1729121AbgATQG3 (ORCPT + 99 others); Mon, 20 Jan 2020 11:06:29 -0500 Received: from mail-qk1-f196.google.com ([209.85.222.196]:42127 "EHLO mail-qk1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729045AbgATQG3 (ORCPT ); Mon, 20 Jan 2020 11:06:29 -0500 Received: by mail-qk1-f196.google.com with SMTP id z14so30548920qkg.9 for ; Mon, 20 Jan 2020 08:06:28 -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=rKBXxvKIRJE9r95Oq/vyPWMrKks8UEDi+Qo4dUocXJw=; b=Baf6RJnN+FgpGw0uph9xwLD0Y+xAcJSaSvv3RdARcDa7LNpveANlU1k6FDswjvy2Xc LObElRWDHMKUj3ZjRSNTBeO/htrBIYiKHweFN5P4tGQq8ToBhQbvCWcvJrSKxEPS3cBd oHUw3dHvbBeOVXV8JvEvvJHoJixZ5NHOGHvztKvlXrnUFBej3ua1JqsCjxG+qkttVE8F iRT1A16DFSmFPRw9dEtVOy/TAHOtJc7+TtiB8sAHFVcUtWX/WqYyo/TcgEJcEDcmRV2R Rz0lHHDuPohMF0E/czlHX+sLN/V8zBqp2azUpf0GHBEpP5dOVwAoA58Zmf9LD/Oro79O wE2Q== 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=rKBXxvKIRJE9r95Oq/vyPWMrKks8UEDi+Qo4dUocXJw=; b=WfjsoDdL5V3I775MGHc8pSyNbjVtjXXBt9X+B4WVPYBPm2DiRckM2Ji7I7NEAaNPDv DVR49rSLx8sufjy+nirP61mQWePkLtwHgdqPkPA38v5mOAEw1wZT5ce72sAS+Zx7QnT2 CoRZ3npAwOe34IYAbzqx2AXE0a27r3q4ymUy6rPuF19quHrXH8F9kMPvIYm/0E+SJFPw MI444kVSkICYpm7u+xoO+hp9FgL9EdyYRLbxbZQqpUvjTiAYjtTAOi6sbON8SN2mFHaU HhZSax1blHXfqte/qehPBw5khRWesTtnnlJH07qy7QgDdbMxjajC5znPS/3TkahCiZb5 9lfg== X-Gm-Message-State: APjAAAXqnWtBNhctR1UARIJrE0Q77/m6iOGSg0IG3zhNpKlQ5varDx30 RnEVxCwwZLOju3/Lwj90Vf71CWz4xC7ErkJgynkeUw== X-Received: by 2002:a05:620a:1136:: with SMTP id p22mr240817qkk.8.1579536388045; Mon, 20 Jan 2020 08:06:28 -0800 (PST) MIME-Version: 1.0 References: <20200120141927.114373-1-elver@google.com> In-Reply-To: From: Dmitry Vyukov Date: Mon, 20 Jan 2020 17:06:16 +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 4:40 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 > > > > > > KMSAN also has instrumentation in > > > copy_to_user_page/copy_from_user_page. Do we need to do anything for > > > KASAN/KCSAN for these functions? > > copy_to_user_page/copy_from_user_page can be instrumented with > instrument_copy_{to,from}_user_. I prefer keeping this series with no > functional change intended for KASAN at least. > > > There is also copy_user_highpage. > > > > And ioread/write8/16/32_rep: do we need any instrumentation there. It > > seems we want both KSAN and KCSAN too. One may argue that KCSAN > > instrumentation there is to super critical at this point, but KASAN > > instrumentation is important, if anything to prevent silent memory > > corruptions. How do we instrument there? I don't see how it maps to > > any of the existing instrumentation functions. > > These should be able to use the regular instrument_{read,write}. I > prefer keeping this series with no functional change intended for > KASAN at least. instrument_{read,write} will not contain any KMSAN instrumentation, which means we will effectively remove KMSAN instrumentation, which is weird because we instrumented these functions because of KMSAN in the first place... > > There is also kmsan_check_skb/kmsan_handle_dma/kmsan_handle_urb that > > does not seem to map to any of the instrumentation functions. > > For now, I would rather that there are some one-off special > instrumentation, like for KMSAN. Coming up with a unified interface > here that, without the use-cases even settled, seems hard to justify. > Once instrumentation for these have settled, unifying the interface > would have better justification. I would assume they may also require an annotation that checks the memory region under all 3 tools and we don't have such annotation (same as the previous case and effectively copy_to_user). I would expect such annotation will be used in more places once we start looking for more opportunities. > This patch series is merely supposed to introduce instrumented.h and > replace the kasan_checks (also implicitly introducing kcsan_checks > there), however, with no further functional change intended. > > I propose that adding entirely new instrumentation for both KASAN and > KCSAN, we should send a separate patch-series. > > Thanks, > -- Marco