Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp2618819ybl; Mon, 20 Jan 2020 06:21:23 -0800 (PST) X-Google-Smtp-Source: APXvYqx36QLXVO9F9Rc/GhSIFC8lGwYVJDNdCYwxHPqHTDTtyl/XOXvf6+AufIiv4cKF+NJq8Syp X-Received: by 2002:a9d:3a66:: with SMTP id j93mr16670335otc.25.1579530083098; Mon, 20 Jan 2020 06:21:23 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1579530083; cv=none; d=google.com; s=arc-20160816; b=rSdkEu+QNsOhweOG7ieDvRjB5/ueBazziPLs224kwPYXvmjLFhAk1TYSbt8QkMOzeg UGnbYwSOmc6ZkJhh64Tc2S8xwD1pm910GSLiUd0XI5B3sExYpzsMjgi0pbZ37r1aSWFP ewQwgo50s7gV6EJK6JeE+a/LNl9nVA+wag4UT5hDg1W+YRRrL2DjsOCIP38mvErk4C0l gviPmKIngoqtfj5MP1ptRnkH7t47qiaVspbsFA8dGHlzm7WEiHWd/AeoPMXuYJUviIFp 2Gvn7gdRmCcvMRFy9xEMaVYJXY+3Jjxi5a3DWnOsiCVSZ9IFbfvUvzNIO3T6vFLhSwVX 6JvQ== 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:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=sfeBMXRfbNdJgAnQzXh8K2r3zlp1l0BIMpCBNTG8NB8=; b=Pw+o0eqzkAJU1GSaz25QG5zKfLifIhjs1SGpBbj0Cn9jk492TbUGE4gfF6wSD2EGrt xra5OliMOBLU9kgNl9QacITBfOS9yLSOH0ZJT8mtN745r2InVU15FVFZPf1BrYHFIF80 YHoRTOqQxbWLN1Ptgh8f5GRFgCr+6mYwboP5IaUC0ZvQrZckLcSCKT8HvK52prykxKS2 y7VnTPvxBjCRZukWedyetuM8DAN0SxvEsI9LXL7T5Pq9uxPDSlYklEwjBXo9jix487xI 8Sppy2vLRdwfS1cmP+mB+inqYrpITomx6TggvG+dFgXfCJ29RnXI+MmG1hulik7XM1BE TzMQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=LSSEn3c2; 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 y140si18274716oia.49.2020.01.20.06.21.11; Mon, 20 Jan 2020 06:21:23 -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=LSSEn3c2; 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 S1728978AbgATOT5 (ORCPT + 99 others); Mon, 20 Jan 2020 09:19:57 -0500 Received: from mail-wr1-f73.google.com ([209.85.221.73]:35284 "EHLO mail-wr1-f73.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728921AbgATOT4 (ORCPT ); Mon, 20 Jan 2020 09:19:56 -0500 Received: by mail-wr1-f73.google.com with SMTP id f15so14303292wrr.2 for ; Mon, 20 Jan 2020 06:19:54 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=sfeBMXRfbNdJgAnQzXh8K2r3zlp1l0BIMpCBNTG8NB8=; b=LSSEn3c2vjZu0hxjSiwTarnKg/7zNzHUTp6W7Ip9UWrzRVuZnDkSlhhXZowqJXjtzg urMYjrs99RlbOKWjpR219PjE4j0BGuSZn8EVeIa93/+grY7qQJq/0WiwXbIrjVxgOm1U RpYw3P/PfAAqTwkQUIp50nOZEQAL84wLy3/uQ6bLtMNh7/DiAIbBrfKA30BsVdqZW4q7 skKvreC085/NbB3Gk4pzNbXRzJTqQB6ZcMvJJVks+ihIp7O9W+jjY/yGnEzov7U2GzL2 ox30Q3SZ6FxqWLqaQMtCN34JN1gnd1e0rOsBy+wlefIlQxRUnasgGBOaecV6fIfXPuda z1fg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=sfeBMXRfbNdJgAnQzXh8K2r3zlp1l0BIMpCBNTG8NB8=; b=tLtGABx/mhpJUfAWHrbjhUMa2YjM4/R4cVW1xR2Qab58rVwblCsO5JV6N18GioHha8 aRrB8tadK0Yd8AW4dxsBZyKCbhyZZIF5lPNKgektVVG/K9NmRPhDUv746k/F+vllyA65 x2U00nA93n70dP3Cd9KkxYLoKfo2SJmZHQ0rlc0VcJZh/NOUJKw+8t4GcdGShoCOQTHB dcpy5iXMgBNVK7+Vf7uAn7At0sczYEeAgC1gtTzTpRoJ2XtZiDIphJYl57yqk3mIhWUG bafXlg3syHbOL3zSdM4SjwumTsOi2vkZPgoz/bzcPfvyiHpRu2V3q7nwsy102WxCzFIl LJ2Q== X-Gm-Message-State: APjAAAXlksBvXbVULQ4huX9jF3MHf1tx7qM6FF2f1cmz04NqnBqtag9s cBSVleXFoYkZvDBmmuZKOTpq+7vO5A== X-Received: by 2002:a5d:5381:: with SMTP id d1mr18199365wrv.259.1579529993355; Mon, 20 Jan 2020 06:19:53 -0800 (PST) Date: Mon, 20 Jan 2020 15:19:27 +0100 In-Reply-To: <20200120141927.114373-1-elver@google.com> Message-Id: <20200120141927.114373-5-elver@google.com> Mime-Version: 1.0 References: <20200120141927.114373-1-elver@google.com> X-Mailer: git-send-email 2.25.0.341.g760bfbb309-goog Subject: [PATCH 5/5] copy_to_user, copy_from_user: Use generic instrumented.h 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, christophe.leroy@c-s.fr, dja@axtens.net, mpe@ellerman.id.au, rostedt@goodmis.org, mhiramat@kernel.org, mingo@kernel.org, christian.brauner@ubuntu.com, daniel@iogearbox.net, cyphar@cyphar.com, keescook@chromium.org, 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 replaces the KASAN instrumentation with generic instrumentation, implicitly adding KCSAN instrumentation support. For KASAN no functional change is intended. Suggested-by: Arnd Bergmann Signed-off-by: Marco Elver --- include/linux/uaccess.h | 46 +++++++++++++++++++++++++++++------------ lib/usercopy.c | 14 ++++++++----- 2 files changed, 42 insertions(+), 18 deletions(-) diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h index 67f016010aad..d3f2d9a8cae3 100644 --- a/include/linux/uaccess.h +++ b/include/linux/uaccess.h @@ -2,9 +2,9 @@ #ifndef __LINUX_UACCESS_H__ #define __LINUX_UACCESS_H__ +#include #include #include -#include #define uaccess_kernel() segment_eq(get_fs(), KERNEL_DS) @@ -58,18 +58,26 @@ static __always_inline __must_check unsigned long __copy_from_user_inatomic(void *to, const void __user *from, unsigned long n) { - kasan_check_write(to, n); + unsigned long res; + check_object_size(to, n, false); - return raw_copy_from_user(to, from, n); + instrument_copy_from_user_pre(to, n); + res = raw_copy_from_user(to, from, n); + instrument_copy_from_user_post(to, n, res); + return res; } static __always_inline __must_check unsigned long __copy_from_user(void *to, const void __user *from, unsigned long n) { + unsigned long res; + might_fault(); - kasan_check_write(to, n); check_object_size(to, n, false); - return raw_copy_from_user(to, from, n); + instrument_copy_from_user_pre(to, n); + res = raw_copy_from_user(to, from, n); + instrument_copy_from_user_post(to, n, res); + return res; } /** @@ -88,18 +96,26 @@ __copy_from_user(void *to, const void __user *from, unsigned long n) static __always_inline __must_check unsigned long __copy_to_user_inatomic(void __user *to, const void *from, unsigned long n) { - kasan_check_read(from, n); + unsigned long res; + check_object_size(from, n, true); - return raw_copy_to_user(to, from, n); + instrument_copy_to_user_pre(from, n); + res = raw_copy_to_user(to, from, n); + instrument_copy_to_user_post(from, n, res); + return res; } static __always_inline __must_check unsigned long __copy_to_user(void __user *to, const void *from, unsigned long n) { + unsigned long res; + might_fault(); - kasan_check_read(from, n); check_object_size(from, n, true); - return raw_copy_to_user(to, from, n); + instrument_copy_to_user_pre(from, n); + res = raw_copy_to_user(to, from, n); + instrument_copy_to_user_post(from, n, res); + return res; } #ifdef INLINE_COPY_FROM_USER @@ -109,8 +125,9 @@ _copy_from_user(void *to, const void __user *from, unsigned long n) unsigned long res = n; might_fault(); if (likely(access_ok(from, n))) { - kasan_check_write(to, n); + instrument_copy_from_user_pre(to, n); res = raw_copy_from_user(to, from, n); + instrument_copy_from_user_post(to, n, res); } if (unlikely(res)) memset(to + (n - res), 0, res); @@ -125,12 +142,15 @@ _copy_from_user(void *, const void __user *, unsigned long); static inline __must_check unsigned long _copy_to_user(void __user *to, const void *from, unsigned long n) { + unsigned long res = n; + might_fault(); if (access_ok(to, n)) { - kasan_check_read(from, n); - n = raw_copy_to_user(to, from, n); + instrument_copy_to_user_pre(from, n); + res = raw_copy_to_user(to, from, n); + instrument_copy_to_user_post(from, n, res); } - return n; + return res; } #else extern __must_check unsigned long diff --git a/lib/usercopy.c b/lib/usercopy.c index cbb4d9ec00f2..1c20d4423b86 100644 --- a/lib/usercopy.c +++ b/lib/usercopy.c @@ -1,6 +1,7 @@ // SPDX-License-Identifier: GPL-2.0 -#include #include +#include +#include /* out-of-line parts */ @@ -10,8 +11,9 @@ unsigned long _copy_from_user(void *to, const void __user *from, unsigned long n unsigned long res = n; might_fault(); if (likely(access_ok(from, n))) { - kasan_check_write(to, n); + instrument_copy_from_user_pre(to, n); res = raw_copy_from_user(to, from, n); + instrument_copy_from_user_post(to, n, res); } if (unlikely(res)) memset(to + (n - res), 0, res); @@ -23,12 +25,14 @@ EXPORT_SYMBOL(_copy_from_user); #ifndef INLINE_COPY_TO_USER unsigned long _copy_to_user(void __user *to, const void *from, unsigned long n) { + unsigned long res = n; might_fault(); if (likely(access_ok(to, n))) { - kasan_check_read(from, n); - n = raw_copy_to_user(to, from, n); + instrument_copy_to_user_pre(from, n); + res = raw_copy_to_user(to, from, n); + instrument_copy_to_user_post(from, n, res); } - return n; + return res; } EXPORT_SYMBOL(_copy_to_user); #endif -- 2.25.0.341.g760bfbb309-goog