Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp5882325pxv; Wed, 7 Jul 2021 14:05:33 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxUvUtbDvvAJbaVjOo5zghrvOCj1oODD/Tg57vqULOoJRorkvFiqlogr3c37eeW9JUrnS+g X-Received: by 2002:aa7:dd57:: with SMTP id o23mr32808885edw.6.1625691933566; Wed, 07 Jul 2021 14:05:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625691933; cv=none; d=google.com; s=arc-20160816; b=Jh4PvjnwB8s+E4th2qrVzaOw4zm3Pymlc2BFNtnI6aL4xbC2bODQ/csKRRhCMK75ol euiaHZ3CXlwpTmhYtPalEhhJBCSYY7hz7y74tKMTIQ1dTOk8mZa0jjIodEdoGDTbf9cV YHdaELTLmxwEGrnAI+5BNp0gdHpHFYPyW+wXd6S2BmlvfhdB8QlYkHJc+R7XSQ3SmMJH LYay34B4cYm/ViVkXS1LVEdLwclzR1Px4bJ5MoPrvEP+5/+q64ujR0VstsGOZdg/WiVh EAEo02hALAIN/l93ublnCdpdYAkXijBgeBnaHpv0y9gH81TqllobFjtUHM1wReWjjwLa jFxA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=M6yCm28WnoyNx3nRePoJett8yHbUw3QYAhUOWEwm4lQ=; b=xIHY4BsgtNe2xNfSNaZfLcfT2FvjwVnJ75k2OZdC2pKTaaA7Kpz5TlhhJ3Ga/03Mi2 GrQvwZ4rVtcs0+2wUDXBDZe114/LioJ+BZz0qCEslVHBteRZrda/TYv5+Un3sKYYActT dmBEaC1nbsNiZmqKMuuj36/7eMwSz2C+OSvPRxWX4stQW8x40/PwCNYIyB7kzAt/oHbk d0aIBGWaCwOITfddQkRBdUDv10NcM76YfAE4CC28jYnPLWMXsfmNnkPcSjuAM8dI7Jr9 1DSs8o0538F9AVSJzQnGcluqp1iWmSR+DEiKbRVDyJtNp8sTGZkNJ2eEmUI8B5Mmj5nR EagA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=hH6hFm63; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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. [23.128.96.18]) by mx.google.com with ESMTP id g18si236146edy.150.2021.07.07.14.05.10; Wed, 07 Jul 2021 14:05:33 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=hH6hFm63; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S230333AbhGGTCw (ORCPT + 99 others); Wed, 7 Jul 2021 15:02:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:45498 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229956AbhGGTCt (ORCPT ); Wed, 7 Jul 2021 15:02:49 -0400 Received: from mail-yb1-xb2e.google.com (mail-yb1-xb2e.google.com [IPv6:2607:f8b0:4864:20::b2e]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 10FC4C061574 for ; Wed, 7 Jul 2021 12:00:08 -0700 (PDT) Received: by mail-yb1-xb2e.google.com with SMTP id i4so4828474ybe.2 for ; Wed, 07 Jul 2021 12:00:08 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=M6yCm28WnoyNx3nRePoJett8yHbUw3QYAhUOWEwm4lQ=; b=hH6hFm63vuMyFoK8+hGoqoFO8Fh65M3Klc2NWtTh1joNH7eb4tJE3iBw/mwSt1cEYc PZ2HlWDO6gyMhGh6seGHoHog/c+KYZ2N4LvUOT4UQF2eljgD2EOE96jbjJvJplh3sC4q 7x2+GYNtMCJADpdoVqcCSA45UjfeZrq1c/0qXNOrjVrY9XnxwlmpB1YZGAAgzAEeGhsS 7FuiSIFIFwKAIhLh1E+YuRHwdzSpU/PXISM6wuPnItxKC//+Qdmhs4CyjjRZWvUCWfQk k7GDLcQdhNq6USeQR3dxL8S8XqKNPu/I+oAt8D1ZTpd/+qfbZHBfYvR5eh5QD0WuyTvW c8mA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=M6yCm28WnoyNx3nRePoJett8yHbUw3QYAhUOWEwm4lQ=; b=R42Jxz4/5f4PZbvgLSlkyJ6uSCPpfjJEHYBY6YdySAPjb434mw0f4ePZV8rkIqeN5c 5suXM6x+aDPXO4FCB/P9bK3mAL6lPMQYozqtpKUcEq0ZO7rvFVMraVPV5Yjj4lO6wugJ +GmjW/F+nVg41EyKRRuLVpp0Skg9a4EZcLPHUwqS9z/4+cQjpziF+Zhc6WyAnAoOHZFY kkelBZ0VXLcaY+p3XFPMrAOmfuEgzfrg5gRhQxVAuTgaaJgmZCerfXX79KQv8ab9h514 NS483AVkX5TgbHP8dMusJKtQ4wTKillsMXCTR2RZHXbOUyN+qfp8HIlRWM6FjG5tpfR/ 4zXw== X-Gm-Message-State: AOAM530yzPXv9E3u0MF6pnsSgkPTW4gjVKQBfxm6gwAFA+og1VBrSP2h /yzlKPmzo2DytxWaUc9Ha+DEQMK6shycOdnQppdJrg== X-Received: by 2002:a25:abcd:: with SMTP id v71mr32563199ybi.322.1625684407063; Wed, 07 Jul 2021 12:00:07 -0700 (PDT) MIME-Version: 1.0 References: <08a2e179-3f5f-639e-946f-54cd07ae12fa@kernel.org> <20210707181814.365496-1-ndesaulniers@google.com> <1fd40e80-283f-62e9-a0fa-84ad68047a23@kernel.org> In-Reply-To: <1fd40e80-283f-62e9-a0fa-84ad68047a23@kernel.org> From: =?UTF-8?B?RsSBbmctcnXDrCBTw7JuZw==?= Date: Wed, 7 Jul 2021 11:59:55 -0700 Message-ID: Subject: Re: [PATCH v4] kallsyms: strip LTO suffixes from static functions To: Nick Desaulniers Cc: Kees Cook , Nathan Chancellor , "KE . LI" , Stephen Rothwell , Andrew Morton , Miroslav Benes , Miguel Ojeda , Joe Perches , Stephen Boyd , "Gustavo A. R. Silva" , Randy Dunlap , Sami Tolvanen , linux-kernel@vger.kernel.org, clang-built-linux@googlegroups.com Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jul 7, 2021 at 11:34 AM Nathan Chancellor wrote: > > On 7/7/2021 11:18 AM, Nick Desaulniers wrote: > > Similar to: > > commit 8b8e6b5d3b01 ("kallsyms: strip ThinLTO hashes from static > > functions") > > > > It's very common for compilers to modify the symbol name for static > > functions as part of optimizing transformations. That makes hooking > > static functions (that weren't inlined or DCE'd) with kprobes difficult. > > > > LLVM has yet another name mangling scheme used by thin LTO. Strip off > > these suffixes so that we can continue to hook such static functions. > > > > Reported-by: KE.LI(Lieke) > > Suggested-by: Nathan Chancellor > > Signed-off-by: Nick Desaulniers > > Code looks fine, small comment about a comment below. > > Reviewed-by: Nathan Chancellor > > > --- > > Changes v3 -> v4: > > * Convert this function to use IS_ENABLED rather than provide multiple > > definitions based on preprocessor checks. > > * Add Nathan's suggested-by. > > > > Changes v2 -> v3: > > * Un-nest preprocessor checks, as per Nathan. > > > > Changes v1 -> v2: > > * Both mangling schemes can occur for thinLTO + CFI, this new scheme can > > also occur for thinLTO without CFI. Split cleanup_symbol_name() into > > two function calls. > > * Drop KE.LI's tested by tag. > > * Do not carry Fangrui's Reviewed by tag. > > * Drop the inline keyword; it is meaningless. > > > > kernel/kallsyms.c | 43 ++++++++++++++++++++++++++++++------------- > > 1 file changed, 30 insertions(+), 13 deletions(-) > > > > diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c > > index 4067564ec59f..a10dab216f4f 100644 > > --- a/kernel/kallsyms.c > > +++ b/kernel/kallsyms.c > > @@ -171,26 +171,43 @@ static unsigned long kallsyms_sym_address(int idx) > > return kallsyms_relative_base - 1 - kallsyms_offsets[idx]; > > } > > > > -#if defined(CONFIG_CFI_CLANG) && defined(CONFIG_LTO_CLANG_THIN) > > -/* > > - * LLVM appends a hash to static function names when ThinLTO and CFI are > > - * both enabled, i.e. foo() becomes foo$707af9a22804d33c81801f27dcfe489b. > > - * This causes confusion and potentially breaks user space tools, so we > > - * strip the suffix from expanded symbol names. > > - */ > > -static inline bool cleanup_symbol_name(char *s) > > +static bool cleanup_symbol_name(char *s) > > { > > char *res; > > > > + /* > > + * LLVM appends a suffix for local variables that must be promoted to > > This says local variables but the example uses a function? Is that correct? local functions/variables. Both functions and variables can have a .llvm.[0-9]+ suffix. Aside from this, the updated description looks good to me Reviewed-by: Fangrui Song > > + * global scope as part of ThinLTO. foo() becomes > > + * foo.llvm.974640843467629774. This can break hooking of static > > + * functions with kprobes. > > + */ > > + if (!IS_ENABLED(CONFIG_LTO_CLANG_THIN)) > > + return false; > > + > > + res = strstr(s, ".llvm."); > > + if (res) { > > + *res = '\0'; > > + return true; > > + } > > + > > + /* > > + * LLVM appends a hash to static function names when ThinLTO and CFI > > + * are both enabled, i.e. foo() becomes > > + * foo$707af9a22804d33c81801f27dcfe489b. This causes confusion and > > + * potentially breaks user space tools, so we strip the suffix from > > + * expanded symbol names. > > + */ > > + if (!IS_ENABLED(CONFIG_CFI_CLANG)) > > + return false; > > + > > res = strrchr(s, '$'); > > - if (res) > > + if (res) { > > *res = '\0'; > > + return true; > > + } > > > > - return res != NULL; > > + return false; > > } > > -#else > > -static inline bool cleanup_symbol_name(char *s) { return false; } > > -#endif > > > > /* Lookup the address for this symbol. Returns 0 if not found. */ > > unsigned long kallsyms_lookup_name(const char *name) > >