Received: by 2002:a05:6a10:c604:0:0:0:0 with SMTP id y4csp882474pxt; Fri, 6 Aug 2021 16:52:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwBmr1KPXqlB+/OFFARJqbw2Mktf01Y8WJ+B25ZJh3g1S/f3I/HNzUNZ70eX08LLdKQP2gr X-Received: by 2002:a92:6605:: with SMTP id a5mr930034ilc.15.1628293974678; Fri, 06 Aug 2021 16:52:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1628293974; cv=none; d=google.com; s=arc-20160816; b=svHI0rOCGOklsjS/E6OWQdjtjb5D9eXFjbkw5d+8YYSB+MdiL3XFFYreX4gHvAPsta lYhqOwmIqTE6EskpJJtJgkrPZOos4Tc9W3foz5NV1A975+BPyUCutptuatVckpuHxiwP qbJsXTCIV9gd7wl3uXGO2FGR2zrSLkfDgu1ug/jHAmJKkp1BZKbkIM6J4P6qIyLEzDs9 ZRWPCYE1V/An2QRxTKQoN3l73bb6vUnLr1/mnjTScxJe8bGE6t7G4FRFYSuseZnEia4w Y4DUo7S1vcemBd2nZ0Jl3kAoMgkiYJ0iP/COpXL1Bo4NPA+K+9bJi/h27mW1f1GhmvaA ruDw== 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=M1g7puxrm202RhqkhqVJ0jdDnj+gHqRx1Pn/TRe45VY=; b=CjQ4Txs8kcDUiTeqdj4WGJMOcJhILeUM3nntrmIUj6Sj8pU1YxW+StlPOGWuWjQXA3 4R0NAYHIUcKj4/Z99ygjxNeAfDWLODbr2OT0biY3EsmVsUs5oawLp1ZxPUh2UEzhmrf1 xjeLfqW4ynmgtOT/FjWZ+SrCPpDKxtgwCywrT86rZ/4/2UWxTebh7TnhURj50ROGnL9e /e9SpvdqLa/IFx0QYX/qKJJXdz9/veVosNXPzvNkcXo5mleAcb14fRbHgytwlJsgdPLG rgKdUi1oWT/iFtdcm2/QuyDMvCaVF9yFoiAUMn3C7dBDojRMXbWz0mo9ZVjvOCNBMijc NuLQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=ZSgW6fw3; 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 f22si10928752jam.121.2021.08.06.16.52.43; Fri, 06 Aug 2021 16:52:54 -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=ZSgW6fw3; 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 S231627AbhHFQVY (ORCPT + 99 others); Fri, 6 Aug 2021 12:21:24 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56142 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231453AbhHFQVY (ORCPT ); Fri, 6 Aug 2021 12:21:24 -0400 Received: from mail-yb1-xb32.google.com (mail-yb1-xb32.google.com [IPv6:2607:f8b0:4864:20::b32]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 322FDC0613CF for ; Fri, 6 Aug 2021 09:21:07 -0700 (PDT) Received: by mail-yb1-xb32.google.com with SMTP id x192so16236341ybe.0 for ; Fri, 06 Aug 2021 09:21:07 -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=M1g7puxrm202RhqkhqVJ0jdDnj+gHqRx1Pn/TRe45VY=; b=ZSgW6fw3CIWT31okDRlFQcTrFcT8l65qEdfmz3TZKXYb94+/JcN/c9JpaO+GtuN9BT v5vX46xvzv2T61ua9EKXYyo+ICvK8S47xvvCFfMic4SLW6TwFIY0PfC7TDgehJppE1+D BhMiHmgvU3V/kkSbwlLE3OZvVnA4Iln4mb6sGY1hdSVvYZXIy1Oax2Mv9giloYidKl7m RxrVEVmfTGJzXWi5Z/jLzfPBgiBFdAUXAvjwpzH67uqyFzxX78EZtuRV9dYy0IlPvnRt f6e+rRcEbvRpxL70n7j/LCzYB6BWDF7x+O/ABKm86daxV3ytn9Eef0Ang4vfy/ECsAU2 BgfA== 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=M1g7puxrm202RhqkhqVJ0jdDnj+gHqRx1Pn/TRe45VY=; b=LqjtZHTTrN5FFmMAB3ogoWce9ziwQh0zwq3i2ix5ECMHz9CK/itEjOLs31YZcYILWI 5vO+LCR94yrma5ktodU/sWfx+lkX2NwSeJRreO7Wfpkn4XHesTf4wyRHqLaajzozn2OT mi6CTkhzf/vP9RBlQszxuWrJ2xgvgpOW076WkFgpEmv+k21TSwHTtrb5Ox+ZNhrkM0xZ rZKwInGKEogs13uMtGN6jO93kTuEQCmOQQqhBGl09VcDM1xCOH4+LqM/fOtKG1Fs9gV9 facUMs7CojZrEs4V+PPxtufU12Dwg4VNVqjr/Gi8aufsPz2MOEGtX8IXDHHo5UKrZ/DE 92/A== X-Gm-Message-State: AOAM530sCLo4olZxmhZpAY/Xdk+eiITdnDLgd5DRlrwBnLvDAVZxJp7Q faYcTs5/QyMG65rWUYd//8W9NL4SWXIgFgFVWO3tGg== X-Received: by 2002:a25:2785:: with SMTP id n127mr13814414ybn.235.1628266866064; Fri, 06 Aug 2021 09:21:06 -0700 (PDT) MIME-Version: 1.0 References: <08a2e179-3f5f-639e-946f-54cd07ae12fa@kernel.org> <20210707181814.365496-1-ndesaulniers@google.com> In-Reply-To: <20210707181814.365496-1-ndesaulniers@google.com> From: Sami Tolvanen Date: Fri, 6 Aug 2021 09:20:54 -0700 Message-ID: Subject: Re: [PATCH v4] kallsyms: strip LTO suffixes from static functions To: Nick Desaulniers Cc: Kees Cook , Fangrui Song , "KE . LI" , Nathan Chancellor , Stephen Rothwell , Andrew Morton , Miroslav Benes , Miguel Ojeda , Joe Perches , Stephen Boyd , "Gustavo A. R. Silva" , Randy Dunlap , 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: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 > --- > 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 > + * 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; > + } Note that starting with https://reviews.llvm.org/D97484, the hash separator is '.' instead of '$'. It looks like this change will be in Clang 13. Sami