From patchwork Fri Aug 26 15:07:27 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alexander Potapenko X-Patchwork-Id: 12956177 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id E1FC1ECAAA3 for ; Fri, 26 Aug 2022 15:08:26 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 8362294000C; Fri, 26 Aug 2022 11:08:26 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7E666940007; Fri, 26 Aug 2022 11:08:26 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 6397194000C; Fri, 26 Aug 2022 11:08:26 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 4BC8F940007 for ; Fri, 26 Aug 2022 11:08:26 -0400 (EDT) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay03.hostedemail.com (Postfix) with ESMTP id 28A4EA0EC4 for ; Fri, 26 Aug 2022 15:08:26 +0000 (UTC) X-FDA: 79842075012.10.9C12E50 Received: from mail-ej1-f73.google.com (mail-ej1-f73.google.com [209.85.218.73]) by imf07.hostedemail.com (Postfix) with ESMTP id C4AB64001D for ; Fri, 26 Aug 2022 15:08:25 +0000 (UTC) Received: by mail-ej1-f73.google.com with SMTP id js11-20020a17090797cb00b00730d73eac83so716787ejc.19 for ; Fri, 26 Aug 2022 08:08:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc; bh=H9Td7/TMrTAtAVeUrHIrhZSexZsjcdbUinsVZHg+GW4=; b=lGhpRwPTwgy57+sACo10KJOabLS+OzoRYCdrY6/DZ69yKQ32Xcl4893L9mHNBXiHX+ FXTMwAj8o5xEg0UQH1YWvGS2c14JObIHuzS9Q+Cy8Ew9WrtHNB0uH/PCp25eq3/bGv9N RyTCwexA+hu/WiPHftg+7pBz23LUJRMDEYENkvxDYKktsmwZX/r78KtNbrMpmA6VSyhx v3vpLI2jMLbp9/vcMTRt+gZg2hx0sZJLdiHzG1SemMYm6jlEasuaIKQcHRhO6rJsuS08 pazvWhVpuAX2fMk3/sVIk0P6SZQRXKcSnn72mVlZhGjEov6c7m3cAMjmVdPIt4SrB+Xp EeSg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:x-gm-message-state:from:to:cc; bh=H9Td7/TMrTAtAVeUrHIrhZSexZsjcdbUinsVZHg+GW4=; b=Wk/s00OviE3oLvlxHOAudFZooAWnYOoruP8XPieEwOimPkUatqXlYtznVlKfxT5R6/ YugLOR914aowu4bT7fMqq5pow7hcco+CxnEcTVaAT9MbG9XcYGh3OBbMp1QdoyM4KWI0 eoeFg0+USYklnNbYZq/+19FzqSisNC1y3MH1Tvo65fG2dhWFNV3HX5aiL6DN4sFc0RCL 56Mk3rdLU73EyY50Q7eCpNeKa1JFL4uNkatF5RAHA2pjJsuo3/U3Gl/cbWcDNfz6ghL5 3INVd4s6P382KR/Y7JyXZfSFYV90R8gQ9b3LPX8WC5EtwHuaZdv7gI46rp+VhljVn4Qs IZlw== X-Gm-Message-State: ACgBeo1MpqsVtLNfYIR1g6CZecO7epOhZPJ/oECKyk3GWYGdOQmkr2Zv AI2kATCPuzFYATVzoQl9PKyHSjYc2pE= X-Google-Smtp-Source: AA6agR74xpTNsjJW4kV3da/I/rAj+tl6NA1SFrgmEnh0B+2JVTsbM1IMY8LB4LV38RhSRb4nx5S5NYcC2Ws= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:5207:ac36:fdd3:502d]) (user=glider job=sendgmr) by 2002:a17:907:7fa5:b0:730:5d54:4c24 with SMTP id qk37-20020a1709077fa500b007305d544c24mr5759332ejc.641.1661526504373; Fri, 26 Aug 2022 08:08:24 -0700 (PDT) Date: Fri, 26 Aug 2022 17:07:27 +0200 In-Reply-To: <20220826150807.723137-1-glider@google.com> Mime-Version: 1.0 References: <20220826150807.723137-1-glider@google.com> X-Mailer: git-send-email 2.37.2.672.g94769d06f0-goog Message-ID: <20220826150807.723137-5-glider@google.com> Subject: [PATCH v5 04/44] x86: asm: instrument usercopy in get_user() and put_user() From: Alexander Potapenko To: glider@google.com Cc: Alexander Viro , Alexei Starovoitov , Andrew Morton , Andrey Konovalov , Andy Lutomirski , Arnd Bergmann , Borislav Petkov , Christoph Hellwig , Christoph Lameter , David Rientjes , Dmitry Vyukov , Eric Dumazet , Greg Kroah-Hartman , Herbert Xu , Ilya Leoshkevich , Ingo Molnar , Jens Axboe , Joonsoo Kim , Kees Cook , Marco Elver , Mark Rutland , Matthew Wilcox , "Michael S. Tsirkin" , Pekka Enberg , Peter Zijlstra , Petr Mladek , Steven Rostedt , Thomas Gleixner , Vasily Gorbik , Vegard Nossum , Vlastimil Babka , kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kernel@vger.kernel.org ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1661526505; a=rsa-sha256; cv=none; b=4ebheZy7O+rWS3wBxMmmMO6FGs4xAM70Rh5rQeKSh/QmbICvEjmG02onMtx49SFLqve8rC poPHzJ7le422XhWaN36vE6sy3TpBL4jIL2HpyTtidxIjO/xqKQyiJT23f3LItC3Nyuzyyn JvuEZJbJexiCvMX6vfhzOW4iUmKTRLU= ARC-Authentication-Results: i=1; imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=lGhpRwPT; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of 36OEIYwYKCO0VaXSTgVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=36OEIYwYKCO0VaXSTgVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--glider.bounces.google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1661526505; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=H9Td7/TMrTAtAVeUrHIrhZSexZsjcdbUinsVZHg+GW4=; b=fJ5zaJbOxN3e/DN5ysCqmrgsJTzZHVeASAkhnI2MLSxKKoaLNWsaGAM9t9/SjLXpi6Zgkz IxGyQBrba1vnr4vZ0baHWd6SskNeW0ZdBwN9r4FXepvbM4PBPM9ChioaHnXNZdR8u+TK/e hzx74xseNYWHmLZvYpVbLwOZKkZ10vI= Authentication-Results: imf07.hostedemail.com; dkim=pass header.d=google.com header.s=20210112 header.b=lGhpRwPT; dmarc=pass (policy=reject) header.from=google.com; spf=pass (imf07.hostedemail.com: domain of 36OEIYwYKCO0VaXSTgVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--glider.bounces.google.com designates 209.85.218.73 as permitted sender) smtp.mailfrom=36OEIYwYKCO0VaXSTgVddVaT.RdbaXcjm-bbZkPRZ.dgV@flex--glider.bounces.google.com X-Rspam-User: X-Rspamd-Server: rspam04 X-Stat-Signature: d9pcgs3yn63pj8usmoxueixrwb8qf3hz X-Rspamd-Queue-Id: C4AB64001D X-HE-Tag: 1661526505-377202 X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Use hooks from instrumented.h to notify bug detection tools about usercopy events in variations of get_user() and put_user(). Signed-off-by: Alexander Potapenko --- v5: -- handle put_user(), make sure to not evaluate pointer/value twice Link: https://linux-review.googlesource.com/id/Ia9f12bfe5832623250e20f1859fdf5cc485a2fce --- arch/x86/include/asm/uaccess.h | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uaccess.h index 913e593a3b45f..c1b8982899eca 100644 --- a/arch/x86/include/asm/uaccess.h +++ b/arch/x86/include/asm/uaccess.h @@ -5,6 +5,7 @@ * User space memory access functions */ #include +#include #include #include #include @@ -103,6 +104,7 @@ extern int __get_user_bad(void); : "=a" (__ret_gu), "=r" (__val_gu), \ ASM_CALL_CONSTRAINT \ : "0" (ptr), "i" (sizeof(*(ptr)))); \ + instrument_get_user(__val_gu); \ (x) = (__force __typeof__(*(ptr))) __val_gu; \ __builtin_expect(__ret_gu, 0); \ }) @@ -192,9 +194,11 @@ extern void __put_user_nocheck_8(void); int __ret_pu; \ void __user *__ptr_pu; \ register __typeof__(*(ptr)) __val_pu asm("%"_ASM_AX); \ - __chk_user_ptr(ptr); \ - __ptr_pu = (ptr); \ - __val_pu = (x); \ + __typeof__(*(ptr)) __x = (x); /* eval x once */ \ + __typeof__(ptr) __ptr = (ptr); /* eval ptr once */ \ + __chk_user_ptr(__ptr); \ + __ptr_pu = __ptr; \ + __val_pu = __x; \ asm volatile("call __" #fn "_%P[size]" \ : "=c" (__ret_pu), \ ASM_CALL_CONSTRAINT \ @@ -202,6 +206,7 @@ extern void __put_user_nocheck_8(void); "r" (__val_pu), \ [size] "i" (sizeof(*(ptr))) \ :"ebx"); \ + instrument_put_user(__x, __ptr, sizeof(*(ptr))); \ __builtin_expect(__ret_pu, 0); \ }) @@ -248,23 +253,25 @@ extern void __put_user_nocheck_8(void); #define __put_user_size(x, ptr, size, label) \ do { \ + __typeof__(*(ptr)) __x = (x); /* eval x once */ \ __chk_user_ptr(ptr); \ switch (size) { \ case 1: \ - __put_user_goto(x, ptr, "b", "iq", label); \ + __put_user_goto(__x, ptr, "b", "iq", label); \ break; \ case 2: \ - __put_user_goto(x, ptr, "w", "ir", label); \ + __put_user_goto(__x, ptr, "w", "ir", label); \ break; \ case 4: \ - __put_user_goto(x, ptr, "l", "ir", label); \ + __put_user_goto(__x, ptr, "l", "ir", label); \ break; \ case 8: \ - __put_user_goto_u64(x, ptr, label); \ + __put_user_goto_u64(__x, ptr, label); \ break; \ default: \ __put_user_bad(); \ } \ + instrument_put_user(__x, ptr, size); \ } while (0) #ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT @@ -305,6 +312,7 @@ do { \ default: \ (x) = __get_user_bad(); \ } \ + instrument_get_user(x); \ } while (0) #define __get_user_asm(x, addr, itype, ltype, label) \