From mboxrd@z Thu Jan 1 00:00:00 1970 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 3030FC19F2A for ; Sun, 7 Aug 2022 17:34:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233831AbiHGReP (ORCPT ); Sun, 7 Aug 2022 13:34:15 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33438 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229501AbiHGReM (ORCPT ); Sun, 7 Aug 2022 13:34:12 -0400 Received: from mail-yw1-x1129.google.com (mail-yw1-x1129.google.com [IPv6:2607:f8b0:4864:20::1129]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0046F559A for ; Sun, 7 Aug 2022 10:34:10 -0700 (PDT) Received: by mail-yw1-x1129.google.com with SMTP id 00721157ae682-31e7ca45091so61663607b3.3 for ; Sun, 07 Aug 2022 10:34:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:from:to:cc; bh=aS6EBaNIN10+OZPUpR/4ogKAkoJ9rFIL98FCoXlnNSA=; b=XpLWeHmLSgilVTC/YVDJdenSdO9VgEXau1/zbqRnkcJxD/8EfTAnvBvtediAfBDduT yCm30ilSyBucIjn2Mi1MvxCG+jlUqTrB5qyK3AUFwqvaMyAqde4A3I265GlRWFIXINCP TPdJKp96tRb7p/cuTGjalES/FKWxaCpZdLftBKK/a4AHpG/wgikOIFWOcy72yQRxVi8S QoiQizprHh0nnRIovr8RqCM+6QlzZlw40e1oO9C9MRg0lgbttWi0LCqtcsPXTolLCD3j 855X9875RDQl75VEJ5hHCk5GqYr3V2eaLjOcPL21BC0b6vzBiGf4A5U1aW8gCItyNapE YZDQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc; bh=aS6EBaNIN10+OZPUpR/4ogKAkoJ9rFIL98FCoXlnNSA=; b=d6wFfZe9QvgqIxNOqUMdH8+grsJw7GJP1gdq7H+krais9jyWMB8VU3aUZi3cup17nJ 4wKa935Mh+l7c0xsr+N/4J0LdjUSqk0fFXB5k902GWuVivD8FgQOgxqaYuMfzw5ajSSK jE57FACYzudUzIByVhXg8iKTwLFBPDvfMTH/Ahlrt4tKAtL3WCZEPrf2/tj8HzY/b/KM H26v3g/elK8WIJi+yALLdS31uoJ5U2mWFY7Ly6AgH4R7RW6xqynFviXllOq9DoHpyMPn 8hcYuEiZclhoLMawlycwQJFZGLqpgYksmTZaciDnTOB7IFeBDatf1ahxjrU8QlfjEsqa 8Jkw== X-Gm-Message-State: ACgBeo2Q70vHNd4r/6ZHqdHqdgzCUraAdusAWsdJRtrQGw3S1aIMYHqR W3Fzr42Ks/Hnaf3ZDJ2EOLp8/LKqasJBVOHES5yxcQ== X-Google-Smtp-Source: AA6agR6MFeUG2On+7vQSONsbltpXDjqOVg5KhQR92T7QmMYljWwNRhhzj3YFZU8Dlaejm2j4GyGExjq1payzBN1lTaA= X-Received: by 2002:a0d:c7c3:0:b0:31e:9622:c4f6 with SMTP id j186-20020a0dc7c3000000b0031e9622c4f6mr14640035ywd.144.1659893650000; Sun, 07 Aug 2022 10:34:10 -0700 (PDT) MIME-Version: 1.0 References: <20220701142310.2188015-1-glider@google.com> <20220701142310.2188015-5-glider@google.com> In-Reply-To: From: Alexander Potapenko Date: Sun, 7 Aug 2022 19:33:33 +0200 Message-ID: Subject: Re: [PATCH v4 04/45] x86: asm: instrument usercopy in get_user() and __put_user_size() To: Marco Elver 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 , 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 , Linux Memory Management List , Linux-Arch , LKML Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, Jul 7, 2022 at 12:13 PM Marco Elver wrote: > > On Fri, 1 Jul 2022 at 16:23, Alexander Potapenko wrot= e: > > > > Use hooks from instrumented.h to notify bug detection tools about > > usercopy events in get_user() and put_user_size(). > > > > It's still unclear how to instrument put_user(), which assumes that > > instrumentation code doesn't clobber RAX. > > do_put_user_call() has a comment about KASAN clobbering %ax, doesn't > this also apply to KMSAN? If not, could we have a > that provides helpers to push registers on the stack and pop them back > on return? In fact, yes, it is rather simple to not clobber %ax. A more important aspect of instrumenting get_user()/put_user() is to always evaluate `x` and `ptr` only once, because sometimes these macros get called like `put_user(v, sp++)`. I might have confused the effects of evaluating sp++ twice with some register clobbering. > Also it seems the test robot complained about this patch. Will fix in v5. > > > Signed-off-by: Alexander Potapenko > > --- > > Link: https://linux-review.googlesource.com/id/Ia9f12bfe5832623250e20f1= 859fdf5cc485a2fce > > --- > > arch/x86/include/asm/uaccess.h | 7 +++++++ > > 1 file changed, 7 insertions(+) > > > > diff --git a/arch/x86/include/asm/uaccess.h b/arch/x86/include/asm/uacc= ess.h > > index 913e593a3b45f..1a8b5a234474f 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 > > @@ -99,11 +100,13 @@ extern int __get_user_bad(void); > > int __ret_gu; = \ > > register __inttype(*(ptr)) __val_gu asm("%"_ASM_DX); = \ > > __chk_user_ptr(ptr); = \ > > + instrument_copy_from_user_before((void *)&(x), ptr, sizeof(*(pt= r))); \ > > asm volatile("call __" #fn "_%P4" = \ > > : "=3Da" (__ret_gu), "=3Dr" (__val_gu), = \ > > ASM_CALL_CONSTRAINT = \ > > : "0" (ptr), "i" (sizeof(*(ptr)))); = \ > > (x) =3D (__force __typeof__(*(ptr))) __val_gu; = \ > > + instrument_copy_from_user_after((void *)&(x), ptr, sizeof(*(ptr= )), 0); \ > > __builtin_expect(__ret_gu, 0); = \ > > }) > > > > @@ -248,7 +251,9 @@ extern void __put_user_nocheck_8(void); > > > > #define __put_user_size(x, ptr, size, label) = \ > > do { = \ > > + __typeof__(*(ptr)) __pus_val =3D x; = \ > > __chk_user_ptr(ptr); = \ > > + instrument_copy_to_user(ptr, &(__pus_val), size); = \ > > switch (size) { = \ > > case 1: = \ > > __put_user_goto(x, ptr, "b", "iq", label); = \ > > @@ -286,6 +291,7 @@ do { = \ > > #define __get_user_size(x, ptr, size, label) = \ > > do { = \ > > __chk_user_ptr(ptr); = \ > > + instrument_copy_from_user_before((void *)&(x), ptr, size); = \ > > switch (size) { = \ > > case 1: { = \ > > unsigned char x_u8__; = \ > > @@ -305,6 +311,7 @@ do { = \ > > default: = \ > > (x) =3D __get_user_bad(); = \ > > } = \ > > + instrument_copy_from_user_after((void *)&(x), ptr, size, 0); = \ > > } while (0) > > > > #define __get_user_asm(x, addr, itype, ltype, label) = \ > > -- > > 2.37.0.rc0.161.g10f37bed90-goog > > --=20 Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Stra=C3=9Fe, 33 80636 M=C3=BCnchen Gesch=C3=A4ftsf=C3=BChrer: Paul Manicle, Liana Sebastian Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg