Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp3846117ybl; Tue, 21 Jan 2020 08:07:04 -0800 (PST) X-Google-Smtp-Source: APXvYqwmMglvhM0YHp6AJO40Yz1jO6V+Iin81yKDpF6aqqj66ydAMdCWoCpKTPAfq8giRgUP5JEp X-Received: by 2002:aca:1a10:: with SMTP id a16mr3621873oia.9.1579622823984; Tue, 21 Jan 2020 08:07:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579622823; cv=none; d=google.com; s=arc-20160816; b=omqYg7HJSYYsMBo7FerizGrJaw1xC/7ba2eMACs8XqLZK1xWw6SdpBQs9RJXGcMMCU hzfqxzhJNz53/G/gG6EZlXfIJnNvUFUyNoh8q9E3qP7WypmnGuu/42ODPOHudF3CNn7h 9g6ZwliOToDW/xRJd07FGA5DjNUmAEOaeQ+mWHn2GaAmMi3wIhPSLtYi14kbn/L6F30B VnJf8WcWZ7gY+cA4faBaKDmiSG4I8SON88qJMmGB53c1d4lsgRW9ze1IuXLCpC3RBGAF WabA3UkDVwtzmlyXg6LrmDfQQx3V8SF09Ell9dRtPs9QCVafZTjJVILiGTqE/aH4PgXU C3Dg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:mime-version :message-id:date:dkim-signature; bh=3X3r+FgOCgofu61vtdHWB37xIMZxsUR5auYodQTv4sI=; b=RrgesLzn6J3W+cX/bXx8i0QaQg80RiAl9MWd3032Qa0YSBUnxqy13JX9GqPL1cfIhw Sr37VBNAoFbxt1OWNcWkMtf0ZkD9cFWTEvipy9WwIvLII7tREstDOWATaZB5STrwgJXQ EUO+JJLI/ERBoGSstEiimCPOudjG6f5dqYVPSX5NhRISqEtNJskRWmpyxNRmA36TQB9c byhdu8yIhG60zg1H9RatbFIXIvh6ETX1NqUoNjfFPeGlyZVaeGj0HUfvEtBqRpbduZ5Y e6Poi7U20LV5lCcR7z/b566sPEjf8+JJv5x+3Qa02Q1YJ/BvqT5azAFM0DZlWcNcpqdv 4K1w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=FBbFhbGM; 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 i12si22179334otk.215.2020.01.21.08.06.51; Tue, 21 Jan 2020 08:07:03 -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=FBbFhbGM; 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 S1729274AbgAUQF2 (ORCPT + 99 others); Tue, 21 Jan 2020 11:05:28 -0500 Received: from mail-wr1-f73.google.com ([209.85.221.73]:33702 "EHLO mail-wr1-f73.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728901AbgAUQF2 (ORCPT ); Tue, 21 Jan 2020 11:05:28 -0500 Received: by mail-wr1-f73.google.com with SMTP id z15so1527319wrw.0 for ; Tue, 21 Jan 2020 08:05:27 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:message-id:mime-version:subject:from:to:cc; bh=3X3r+FgOCgofu61vtdHWB37xIMZxsUR5auYodQTv4sI=; b=FBbFhbGM/NL6HZQV2amTvdwm6oJ8PBno89Nf7NbyoskQOuVRFIvwKbn6qlGjYf08MG n+mtcfPPBq6GJCcQl3gkRpuUn3RtJ0HqhbzoqQqyeFwZ/NUkiqAWuFVT506mnRlrw0uk irWBGh7lGs3bw+PIVpU622MQdWEygj1ufZd4SEeh9jKvRdsJUyr8vhu+4IMIx/w6RCAE f9Sk1qvWNGLIbLTjhAUyVW5eExCkUPhcfmZ0vFZuRFQrsosOwqv9p+QRu/fgpOUqDlcU skuxWzMgb63FopNhwUDMHhvsqb5najfIU3as9az8w1KMATO2pI4feIzMm3hUEnXVp9AH zg0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:mime-version:subject:from:to:cc; bh=3X3r+FgOCgofu61vtdHWB37xIMZxsUR5auYodQTv4sI=; b=VeGQiGLlqQxAJla+rpGUkkrRtE2HIItorvonORs/i+bJ083c75JvTgf/Wmnomuuiog XcQVZ48eUsBC3hd7X3cVqb499xockBUlKelZFyL1+gamaqhVS842JU0A5+lGH/Ug9ESn CW+F88LUeCnAc+PkPXBru2CYIiuQHIJBMomyCjtBbaIsI9SC6L3EcFKS6e89skww7zIz k8wvYefpfGtoOeCabanJkA4b9a9ZkDDvDg0f+kYJXXH9AqZ5bt14rBnC5hAQWuP5bttl N6N53ibg2Lm4iaCQlwZ0Gb5LADqgGymWe4USlYXg+V+BHZJz8Gp9gcr1/UcXiGSqRite CBVQ== X-Gm-Message-State: APjAAAV86Pxv4qYoDKwGoJLgknWSEp1/A3ZongpJsF2dA7oIiCqhiN9S nshlY8aUvBhSNlqxk8trlQOy/c3ZMQ== X-Received: by 2002:adf:b193:: with SMTP id q19mr5973633wra.78.1579622725966; Tue, 21 Jan 2020 08:05:25 -0800 (PST) Date: Tue, 21 Jan 2020 17:05:08 +0100 Message-Id: <20200121160512.70887-1-elver@google.com> Mime-Version: 1.0 X-Mailer: git-send-email 2.25.0.341.g760bfbb309-goog Subject: [PATCH v2 1/5] include/linux: Add instrumented.h infrastructure From: Marco Elver To: elver@google.com Cc: paulmck@kernel.org, andreyknvl@google.com, glider@google.com, dvyukov@google.com, kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, mark.rutland@arm.com, will@kernel.org, peterz@infradead.org, boqun.feng@gmail.com, arnd@arndb.de, viro@zeniv.linux.org.uk, dja@axtens.net, christophe.leroy@c-s.fr, mpe@ellerman.id.au, mhiramat@kernel.org, rostedt@goodmis.org, mingo@kernel.org, christian.brauner@ubuntu.com, daniel@iogearbox.net, keescook@chromium.org, cyphar@cyphar.com, linux-arch@vger.kernel.org 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 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.] 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