Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp5782450pxv; Wed, 7 Jul 2021 11:36:50 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyldFCpapKJenUdXm2VhdLLQz7WDHmG/TJHHNDeb/zvnfPdmqV4SBalKUKXswlw7c856iUb X-Received: by 2002:a17:907:7848:: with SMTP id lb8mr26180633ejc.494.1625683010118; Wed, 07 Jul 2021 11:36:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1625683010; cv=none; d=google.com; s=arc-20160816; b=Mbj5hiwuDEqWKsfy378ACzuF3Daab5lge/xHRBjqUnRO9gI3O1UCUYUAaRX/AGU1xA eDlzuHmUwPM4RxBmhWkBqOyaMUtk12oi6ocXeLDjQGk4ri/qAu7goyUdMEVq1x8c+8ES kQs8gSkUfeugXR2XBFIvndq6FuBOY0MfPyS+f72U2pX8ukRqjW/2AmyXu2H3WgKLfk6W ho8WffeI9yZCl8gxdaJ01t+WDZQOBDEd0Cs6HMjogETbloZxnWqjuyeNOOT891GL9IzY dQlyO7ZcqrP0hApbhnnsEuAf5Gs2ZPrqZ+IMXeKG7yIOTjZsus6Qqe75c+WJh2ESoNfx HYCw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:content-language :in-reply-to:mime-version:user-agent:date:message-id:from:references :cc:to:subject:dkim-signature; bh=rjUDaaYsStd0WL6zi2l8coFWTg6bnQ3TaNhypVGrUhM=; b=gyCr7mgDtg+ZTWCjuPyr2G7Nk9jD7IaaAproMis8knXjUbUUL/yonsfDG59ytUe2Xb 87yr9Em2QKT8TbT2zAlastFZJp9wyQHEFQG21IBVV9wqN9NVnimYHz7tpxXzzvSMaiqV Ayo3CUs0gj7SPj1KWabtx1T+HJ/zI17cXiBgNOp9sZLQt9GS6tgjnntRHWDULtuAifR4 g19yBjDfsXfWtycoCKracS322Qc5/YPtUKD+/m8L3A5UCYwguQ2X0YL5gNNHMqTH0vVo 430hc5K5yLR7RSlAj7omUD12g451WJb+EPv/6lNkAdMTKfylyHQkuD9HW5u/Ol13o/tW gx0Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=sM0sNH3f; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id p20si18079480ejr.572.2021.07.07.11.36.26; Wed, 07 Jul 2021 11:36:50 -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=@kernel.org header.s=k20201202 header.b=sM0sNH3f; 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=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231537AbhGGShF (ORCPT + 99 others); Wed, 7 Jul 2021 14:37:05 -0400 Received: from mail.kernel.org ([198.145.29.99]:43388 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230481AbhGGShF (ORCPT ); Wed, 7 Jul 2021 14:37:05 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 115D961CCE; Wed, 7 Jul 2021 18:34:22 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1625682864; bh=nyc7yMokQlmMv1mq9Bp2N+VPsvgWfTlOdurggJxiwlU=; h=Subject:To:Cc:References:From:Date:In-Reply-To:From; b=sM0sNH3fv4GBtDOfKrh0OIByTQwVgFLHZzfmrFUJosMK8bi04aELXq5Vpk+cNHKe+ Sv2lJ+6tGT8+sydwWLCJ+AzyCtzyu12CpGdnqL0NJUTFKez9Yqkk/W57smaJOFRiHX YFw17w0q8/sFOyjqTxEug3DjPYgaTzMJlyVdpmC5+o7J7Ekq/J/bwy6rzSouNtiDfg XjKqD2iy1gN9mIlANST4gqSd/lPolDPyUj+Vw24KF5O8+Hf0oW9/aVUkbuFNhY9a+X eb0LG62+8kLpHsK1FFdhprEBlRxM3O3Vq3bgCo9ip7OGJrq2TXw2YZe8QQ3+SgGqxk C8aP4ym9f/Bqw== Subject: Re: [PATCH v4] kallsyms: strip LTO suffixes from static functions To: Nick Desaulniers , Kees Cook Cc: Fangrui Song , "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 References: <08a2e179-3f5f-639e-946f-54cd07ae12fa@kernel.org> <20210707181814.365496-1-ndesaulniers@google.com> From: Nathan Chancellor Message-ID: <1fd40e80-283f-62e9-a0fa-84ad68047a23@kernel.org> Date: Wed, 7 Jul 2021 11:34:20 -0700 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0 MIME-Version: 1.0 In-Reply-To: <20210707181814.365496-1-ndesaulniers@google.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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? > + * 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) >