Received: by 2002:a05:6358:489b:b0:bb:da1:e618 with SMTP id x27csp1000030rwn; Thu, 15 Sep 2022 09:04:08 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7+g9r6HV6jyNs7+9BvgphjYrIFL9eDrnAPXADpsralKaUl7gbGG+++9opfBMGo7BQxiyNT X-Received: by 2002:a05:6a00:1a0e:b0:547:1cf9:40e8 with SMTP id g14-20020a056a001a0e00b005471cf940e8mr497025pfv.82.1663257847787; Thu, 15 Sep 2022 09:04:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663257847; cv=none; d=google.com; s=arc-20160816; b=0ubZTxjW3/G5TdYPD7O22iQ43RDpJcR+zzwbfX6xrk2yWohGYuABDk7jTCh80ecP5k 3NLyG+yTd9mvmrvrb7FY7zYMzia1O1XrKTK+8wTh06gf+AftZ6SZ685UnYVpIv97b3E4 C/ej5rcWMglJIBMZ4b2YzNPMmvHvAGLE2A1Fam/nck/mZGdBTfufMwrsSDb9wVj89v6Y K29uootSCaByEJPDL/oY+DlgQd24qESVU72W5eFQivV46mGZ67+K2SNivgfS12RO3CWH y4fDp1XnODI3SmKS0C3MjBuUBkJQphAUrxPyVBpEcUkudrs5nf1I8heNff9wT4nBoOZd bD8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:message-id:references :mime-version:in-reply-to:date:dkim-signature; bh=gSZzwrXvhkj2wpolUx3YVTouxdXHw9PaTEDZ4n2e3I0=; b=WCHeaRPkmf5win0+u5pcnP3vRoNHTuQkfcqcUrl2Z0oElUWoqnRFsM750ODcI2JTRw U4jz1PQmvvdtGoNQ6x5xlSb/Vo2Qw0oh2AXmZ5sqo1/x0+5ZZXrrZdT2fTgmczUCtc4a TrEFCLyUiYDtAI7MMOTo5u1XdOlfodzv99f/tt3hKFgZhAzchjAC5yZB8F0zoF88BDHS BQWbIJ8z0flo/tpeRBMCC0cMN/yykYKmHI/7B08s2sHqRNnfjLnSfeVzCFVIxBkaRI2t e+QkYZEH7s1YlkS796alGtf3orNgUI0spehBj0fYuvJuXLdgnYt7oD4phAe6y1CmSylV G2eQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=EznsgfSi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id t3-20020a63eb03000000b0042c3a5b6983si610709pgh.104.2022.09.15.09.03.55; Thu, 15 Sep 2022 09:04:07 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20210112 header.b=EznsgfSi; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S229580AbiIOPF0 (ORCPT + 99 others); Thu, 15 Sep 2022 11:05:26 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46274 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230250AbiIOPFJ (ORCPT ); Thu, 15 Sep 2022 11:05:09 -0400 Received: from mail-ej1-x64a.google.com (mail-ej1-x64a.google.com [IPv6:2a00:1450:4864:20::64a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E3D4726118 for ; Thu, 15 Sep 2022 08:04:50 -0700 (PDT) Received: by mail-ej1-x64a.google.com with SMTP id qa33-20020a17090786a100b0077a69976d24so6989649ejc.7 for ; Thu, 15 Sep 2022 08:04:50 -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=EznsgfSiy9WifxPdZjYT1Vwj2wYB4AW4etiNBG8scEb5xiAuby2h1T+k5Dja+N3dWf S2duxflNmAPHuHcgocbmA96vZwFvrsgfdpnNtgj4uq+eTJxlt5j2bMabt1gCsRhCJDg0 AzJpKEbhLm7ml6bETgD4W/9UBTpmd8EsT/Am6VtGzrLBUEUuaNnF2fOKbJNN/Aff33pA czLuVMpyryuAc03jszny9BsfuzYr+hnv5Q0Y0MfzpdFRm1q9kcGSv2bsQlwBLHtxb+Ub wZPRS/RuAFlH1BM4U0m04fB6G24UfKuxoKx9lbER6yfzIS/q5yq4d5MQKvhKRZhxWaXW sOjg== 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=aYIc3HlKNHqMQ0+DiOcO9El6YSa3geTBg3vNG6M2wo20TMHJ2UkhBuzYqN4J2BQBen QDDuLRFhPUuPbvZSnXiySC2zowkIvKok4rTbwJOouM+I3DMenxBC5u5A6YiISzWQjWno Fkdc+HTH94TvdmQXg64J4WmG/3mWr1niSBwwjnM2dGF4aTz164PXtujQEEVsHLm7ZNri XlYRqvqr2H5eHfThR6EJ9AZSvf2+mELQ+WpICUzPk+Td0pnnz3SQUvTIYguN9uHMPFNF eI76h3nDP+U07TCWVLSCvqxwixOfNpySoJkNFO68NzLsOI6NqXwiuj5t4tnI5hcdlN/2 Uo4g== X-Gm-Message-State: ACrzQf0jGQB3tNVv+Vct0w1ynbVjXwms5epsFYyCwDVjUShWYwZFHCo8 hPXVQi9AFEmkraU+GHxJKUsw7LVzLo0= X-Received: from glider.muc.corp.google.com ([2a00:79e0:9c:201:686d:27b5:495:85b7]) (user=glider job=sendgmr) by 2002:a05:6402:c8a:b0:44e:81b3:4b7e with SMTP id cm10-20020a0564020c8a00b0044e81b34b7emr256022edb.181.1663254289168; Thu, 15 Sep 2022 08:04:49 -0700 (PDT) Date: Thu, 15 Sep 2022 17:03:38 +0200 In-Reply-To: <20220915150417.722975-1-glider@google.com> Mime-Version: 1.0 References: <20220915150417.722975-1-glider@google.com> X-Mailer: git-send-email 2.37.2.789.g6183377224-goog Message-ID: <20220915150417.722975-5-glider@google.com> Subject: [PATCH v7 04/43] 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 Biggers , 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 , Stephen Rothwell , 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 Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.6 required=5.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_NONE, SPF_HELO_NONE,SPF_PASS,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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/Ia9f12bfe5832623250e20f1859fdf5cc485a2fce --- 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); : "=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) \ 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, { } +/** + * instrument_get_user() - add instrumentation to get_user()-like macros + * + * get_user() and friends are fragile, so it may depend on the implementation + * 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 implementation + * 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 */ -- 2.37.2.789.g6183377224-goog