Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp3920064pxv; Mon, 28 Jun 2021 16:41:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxzwbTtKtNd73zzVAf7EOHet++GSoWsiLOycq3CeMjfkcbLSeOk0QhyUlMYgC66IyjQSKGq X-Received: by 2002:a92:d246:: with SMTP id v6mr4255341ilg.191.1624923687620; Mon, 28 Jun 2021 16:41:27 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624923687; cv=none; d=google.com; s=arc-20160816; b=nhGiI4idgRutQybuGdGomo+EQRdDRJuUdFT75YZi3jafG4Kmd39iRJ7eta3RdUAA5S vCaibvgf/utG2BHl1LLtufoh6UI9GB0pHBNECsoQ3fzPXXWw3vaOy7DEuTCfMLkSTREA z1R31q0OodeY7kzZ8eum9QAu2tdR/lCVJ/rQYCfa3gOdJOCMucUvhl23QoC8uv86jQDp /9wU0ltfy//nqtJ0IWb5x3lK6R2p4YPPg+8gt0ejOR8aKxTJmv8Xxd+ul+cioFX2Z/9m BcqFYbtxWM91rgGa2QFU3UKz4Yk/S7DkaoZr2RUFruecVRfrAvtRp9gUucnAQgTQiv2J 26wA== 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:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=6T6NsHzWWvCAH0u6x7ev+GFXhfHMd6xAAjEggw6hDHk=; b=WxG03iv5VoamUwHtqR1DefyOY9A8s9yiG0gQH3PwYuqfDVLi1hVwgQS3R/s/p1hVDX Fffv5ao6d/eYeICJsvvqVOggRhMZMY1TmqqPo5dF9EDJMr3MpXgHJstm60t2xugVJo28 0L1eaLB6wu5ocJkPpP+N7XP8OdhQDarekUPwjK8zVSlTqtGyx9FGruBqb8MFHwbQPMY0 zGMvP7F14YEy0RUw7iMcb6PbTbGYqcmL1y65wtiOjXhm4NQ3f1huj9Nyk5Am3kCqm45H J3vgedQrx2FWuF6DVEjPYunExeIgX9zfcStwe0Z1Rcq7QTNVeNP57t2mCeq8ltDAla0b HgJQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b="vz/oIBoe"; 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 g2si23982292ioo.75.2021.06.28.16.41.16; Mon, 28 Jun 2021 16:41:27 -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="vz/oIBoe"; 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 S236135AbhF1Udk (ORCPT + 99 others); Mon, 28 Jun 2021 16:33:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:32774 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233832AbhF1Udk (ORCPT ); Mon, 28 Jun 2021 16:33:40 -0400 Received: from mail-qt1-x849.google.com (mail-qt1-x849.google.com [IPv6:2607:f8b0:4864:20::849]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 52070C061574 for ; Mon, 28 Jun 2021 13:31:14 -0700 (PDT) Received: by mail-qt1-x849.google.com with SMTP id c17-20020ac87d910000b029024ee21abd54so13735329qtd.19 for ; Mon, 28 Jun 2021 13:31:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=6T6NsHzWWvCAH0u6x7ev+GFXhfHMd6xAAjEggw6hDHk=; b=vz/oIBoeBx1vbFb4xLONG/UTG+UiVDS7Ei7xhUA2M/y9VbFNqDK2qZHJmN4VClPl22 3KYKcdv6lAFr9ww2u7fX7cUbPnSJVOV+T4XBVef5COjhjnE5QAQYOCsflJrHdb1gY5cW xGVM9Kxx6CNe6IKwkE9nzUSGcy0K5XmoiUIPtwMSRMLqKjqHsFtFEZljUidN9huhlJVx 8RhaDNYm0mvWy+bjjw4rjsJp4jcsl21gpwnURPihQBl2s3A1u8ky1wZ0B8t4Pt+sshod /v4eSmGlh/pDGufWj4tn1WLWvsPHXlVkjo12BHYcPJeq36B3VASYPUOwy4/EMvFgiU4f 6ltw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=6T6NsHzWWvCAH0u6x7ev+GFXhfHMd6xAAjEggw6hDHk=; b=sVm9H4Ve0NkjcxnhTvy441h9iAC8e2AlhL/uXLX6ciq768eXb2GRoIOeFB1PkhllWp HwITViHrcoQtYOhaxH7VPRJVCVs1n5o4FQGpJVy4/KVc8/IBSBBfv5ykZVJEo8SFPSP0 Zr/5FTVSj8/Nt94qqHDD1cSS8Miext7p3aYYUFRUn4gv9n6doSsAmu8jpe57612XiTii YL7RFvnju+DMCm7meCzp1NQQyz8PgZPvDxPjeqr89mZejebK9dE8pa+w8r+4U4basKbD L2HP5s7bfkMmGt4okEcT06X6AIxVKOn7CCZFKPQfCqr572FSOwdYJdIbXNmPeF+rRqGk eLcQ== X-Gm-Message-State: AOAM531rBeKIq/ZjvSQTMKp8HDkTI9c2vRObVJGrHa/iSjXn2ismzf43 NS6Rd0Iy972UUWkZPVDcPepzMiSwRM87YOJsvpQ= X-Received: from ndesaulniers1.mtv.corp.google.com ([2620:15c:211:202:6f7:d09:f550:3380]) (user=ndesaulniers job=sendgmr) by 2002:a05:6214:2a88:: with SMTP id jr8mr27049614qvb.6.1624912273427; Mon, 28 Jun 2021 13:31:13 -0700 (PDT) Date: Mon, 28 Jun 2021 13:31:06 -0700 In-Reply-To: Message-Id: <20210628203109.2501792-1-ndesaulniers@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.32.0.93.g670b81a890-goog Subject: [PATCH v3] kallsyms: strip LTO suffixes from static functions From: Nick Desaulniers To: Kees Cook Cc: Fangrui Song , Nick Desaulniers , "KE . LI" , Nathan Chancellor , Andrew Morton , Stephen Rothwell , Miguel Ojeda , Joe Perches , "Gustavo A. R. Silva" , Randy Dunlap , Sami Tolvanen , Stephen Boyd , 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 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) Signed-off-by: Nick Desaulniers --- 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 | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/kernel/kallsyms.c b/kernel/kallsyms.c index 4067564ec59f..143c69e7e75d 100644 --- a/kernel/kallsyms.c +++ b/kernel/kallsyms.c @@ -171,6 +171,26 @@ static unsigned long kallsyms_sym_address(int idx) return kallsyms_relative_base - 1 - kallsyms_offsets[idx]; } +#ifdef CONFIG_LTO_CLANG_THIN +/* + * LLVM appends a suffix for local variables that must be promoted to global + * scope as part of thin LTO. foo() becomes foo.llvm.974640843467629774. This + * can break hooking of static functions with kprobes. + */ +static bool cleanup_symbol_name_thinlto(char *s) +{ + char *res; + + res = strstr(s, ".llvm."); + if (res) + *res = '\0'; + + return res != NULL; +} +#else +static bool cleanup_symbol_name_thinlto(char *s) { return false; } +#endif /* CONFIG_LTO_CLANG_THIN */ + #if defined(CONFIG_CFI_CLANG) && defined(CONFIG_LTO_CLANG_THIN) /* * LLVM appends a hash to static function names when ThinLTO and CFI are @@ -178,7 +198,7 @@ static unsigned long kallsyms_sym_address(int idx) * 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_thinlto_cfi(char *s) { char *res; @@ -189,8 +209,14 @@ static inline bool cleanup_symbol_name(char *s) return res != NULL; } #else -static inline bool cleanup_symbol_name(char *s) { return false; } -#endif +static bool cleanup_symbol_name_thinlto_cfi(char *s) { return false; } +#endif /* CONFIG_CFI_CLANG && CONFIG_LTO_CLANG_THIN */ + +static bool cleanup_symbol_name(char *s) +{ + return cleanup_symbol_name_thinlto(s) && + cleanup_symbol_name_thinlto_cfi(s); +} /* Lookup the address for this symbol. Returns 0 if not found. */ unsigned long kallsyms_lookup_name(const char *name) -- 2.32.0.93.g670b81a890-goog