From nobody Mon Apr 6 18:41:39 2026 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 vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 400B0C54EE9 for ; Mon, 5 Sep 2022 12:25:42 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229947AbiIEMZj (ORCPT ); Mon, 5 Sep 2022 08:25:39 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57060 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237126AbiIEMZK (ORCPT ); Mon, 5 Sep 2022 08:25:10 -0400 Received: from mail-ed1-x549.google.com (mail-ed1-x549.google.com [IPv6:2a00:1450:4864:20::549]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8335713F12 for ; Mon, 5 Sep 2022 05:25:09 -0700 (PDT) Received: by mail-ed1-x549.google.com with SMTP id y12-20020a056402358c00b00448898f1c33so5690392edc.7 for ; Mon, 05 Sep 2022 05:25:09 -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:subject:date; bh=gSZzwrXvhkj2wpolUx3YVTouxdXHw9PaTEDZ4n2e3I0=; b=tGEGAx8uMhsDCwjOO4YeavI0k5QsjHgjDrM4zDrLN5sS9ToamP5OIkdyf8ZLJoQyOw e2LoiIBveMGNkL1Elhx+wY4FUi2lWQYPS946Ofc2zpeiYXDghDghxvMhyBVvvt9lMxWQ vgiRcYONLBIiMgCzXeNVdaYQRCNi3V8gspn7+0+/HuYQO9Jmovxsizf4gqTMVYmCS/BT 3wSbxDLRcA/TelDo7MMeZ9iCCDJ/9i/v/s7AvSw7tPA5gGWT+7eyJ7eSlRgra5ZKcjZj wqC4BAaZNeAxQzofrZ1I/wGoXPlrz8wTvBufMgNH8R9/2cyykGlVsTniZ4E04Wlp4Kq1 6j4Q== 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:subject:date; bh=gSZzwrXvhkj2wpolUx3YVTouxdXHw9PaTEDZ4n2e3I0=; b=IrE43lE8tRokYHF1xQWgZbLpcnFmVzmwqyE6p6XAX/PWKPh9HqIj2VHYcSRx6NiH/d iCXg3xQ/VJLRc5+ce86JhIaA4Y0fNJ0vnq3ksUJjw8v4heialT+o8AU1M2MIn3j3BuKt 0m/ZnvKbc+YZPbjUy4JiBBoLRY7Be3kpQyz1oz7TJQtuLhfn2YZkTAR+UYtlBVBQu9YE 3tKVnLazagCzHFKtSu/ol4N68nwtLSCjStJYTCyQ7HhxN9mTGqQ95ZIJPYTTuw+F8MMc 6aW/rYWLBixEKPDd8fDWjgaSYewafT/EjXw8qc3YaxBoQObngBz8Jb5E3oDD3XNdqUuG Wdtg== X-Gm-Message-State: ACgBeo37LlpovMfxCfL5kEzFHNzurG07cKE5n8h6L3bmWDrlTlNLweG+ N/2pZZOzCDsZjIHljqYfk2Qw4CYdHkM= X-Google-Smtp-Source: AA6agR7uZSb4I8x12+mloMHQ0B/R9zNrKGyUDj6RqUDl0l3ypI90dtuco15DreoHh/j3D7zLcpHmCF1MiQM= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:b808:8d07:ab4a:554c]) (user=glider job=sendgmr) by 2002:a05:6402:4414:b0:434:f58c:ee2e with SMTP id y20-20020a056402441400b00434f58cee2emr43097203eda.362.1662380707978; Mon, 05 Sep 2022 05:25:07 -0700 (PDT) Date: Mon, 5 Sep 2022 14:24:12 +0200 In-Reply-To: <20220905122452.2258262-1-glider@google.com> Mime-Version: 1.0 References: <20220905122452.2258262-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220905122452.2258262-5-glider@google.com> Subject: [PATCH v6 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 Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 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 v6: -- add missing empty definitions of instrument_get_user() and instrument_put_user() Link: https://linux-review.googlesource.com/id/Ia9f12bfe5832623250e20f1859f= df5cc485a2fce --- arch/x86/include/asm/uaccess.h | 22 +++++++++++++++------- include/linux/instrumented.h | 28 ++++++++++++++++++++++++++++ 2 files changed, 43 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); : "=3Da" (__ret_gu), "=3Dr" (__val_gu), \ ASM_CALL_CONSTRAINT \ : "0" (ptr), "i" (sizeof(*(ptr)))); \ + instrument_get_user(__val_gu); \ (x) =3D (__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 =3D (ptr); \ - __val_pu =3D (x); \ + __typeof__(*(ptr)) __x =3D (x); /* eval x once */ \ + __typeof__(ptr) __ptr =3D (ptr); /* eval ptr once */ \ + __chk_user_ptr(__ptr); \ + __ptr_pu =3D __ptr; \ + __val_pu =3D __x; \ asm volatile("call __" #fn "_%P[size]" \ : "=3Dc" (__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); \ }) =20 @@ -248,23 +253,25 @@ extern void __put_user_nocheck_8(void); =20 #define __put_user_size(x, ptr, size, label) \ do { \ + __typeof__(*(ptr)) __x =3D (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) =20 #ifdef CONFIG_CC_HAS_ASM_GOTO_OUTPUT @@ -305,6 +312,7 @@ do { \ default: \ (x) =3D __get_user_bad(); \ } \ + instrument_get_user(x); \ } while (0) =20 #define __get_user_asm(x, addr, itype, ltype, label) \ diff --git a/include/linux/instrumented.h b/include/linux/instrumented.h index ee8f7d17d34f5..9f1dba8f717b0 100644 --- a/include/linux/instrumented.h +++ b/include/linux/instrumented.h @@ -153,4 +153,32 @@ instrument_copy_from_user_after(const void *to, const = void __user *from, { } =20 +/** + * instrument_get_user() - add instrumentation to get_user()-like macros + * + * get_user() and friends are fragile, so it may depend on the implementat= ion + * whether the instrumentation happens before or after the data is copied = from + * the userspace. + * + * @to destination variable, may not be address-taken + */ +#define instrument_get_user(to) \ +({ \ +}) + +/** + * instrument_put_user() - add instrumentation to put_user()-like macros + * + * put_user() and friends are fragile, so it may depend on the implementat= ion + * whether the instrumentation happens before or after the data is copied = from + * the userspace. + * + * @from source address + * @ptr userspace pointer to copy to + * @size number of bytes to copy + */ +#define instrument_put_user(from, ptr, size) \ +({ \ +}) + #endif /* _LINUX_INSTRUMENTED_H */ --=20 2.37.2.789.g6183377224-goog