Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp103414imu; Wed, 7 Nov 2018 13:46:12 -0800 (PST) X-Google-Smtp-Source: AJdET5c+Y/Bo5N08/GF/04NTnQjA8JawxIUFud0inpk4I/PAyk65g20LGUgQ1FH0AzBNFIgN1x8p X-Received: by 2002:a62:178c:: with SMTP id 134-v6mr2021271pfx.29.1541627172662; Wed, 07 Nov 2018 13:46:12 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541627172; cv=none; d=google.com; s=arc-20160816; b=ALZOLtjn2AXRM0dSMnhs5+ssNG+pZsGdylhq+fQeie1tv+qDLRmv1R97foMmH2XWhM 9JnPdqjhhvU2ukgO793NXUIagcMjPYyusBNvzoZz6NfdTfRSl2sES0HwDKlIMTy0avvO QiJ1vbv09xmZMILmhCjc3L8+oVIbg2z1/YC0bD1izeKG2cSjer9LF23Sq9RYvFv1kV8p OtIEviNomA5juXrHOS/Ji1kMIXSecKsuK/jnEAal0erBqTm1pF14BX7ywCXJXWrxIQ7+ uwiFQ7HQYb0JXp6JSbK08e5/uzVwvp2Hc3LaoddNDfyKIpx+Cdq9aKqv/1GjlDWRwuaC i8Kg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from; bh=IIMtzNzN5g+QJmX1GmwyxoijtYNO+59LlZthrdcJDXo=; b=pJLXH8vHpQf+ILvgVtz5eyBaDjAIVm13xGnHw0xwR3RXv7gGtD1p6U1706vM5J7JCB KU+EN6XIxZf9AHVLgw7AnlwrhU4Csd8pQIpvSfSBa8W8vJnUto7W5TdKwl2GC5wn2qw5 YYuQgIs2LitumlNpD14VnYU5hlgCtv5zPvJUxji2WbMN41IBDx1L/BUj0B6EpvkIYjXP yjz/Us9kjOjBvHch3jUeBZWR6iP+tzRbAlb78AtUNNhIQGFtKHnYqo55e9DBr56sNXrW cpGl7cyIlia0EIW6jhatnG2VpMewD6hC+YfRyDWiFCqLZRME/o97KVj0oiCM42ixl+sU OgCw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a91-v6si1882914pld.375.2018.11.07.13.45.57; Wed, 07 Nov 2018 13:46:12 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727360AbeKHHRp (ORCPT + 99 others); Thu, 8 Nov 2018 02:17:45 -0500 Received: from mx1.redhat.com ([209.132.183.28]:52336 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726653AbeKHHRp (ORCPT ); Thu, 8 Nov 2018 02:17:45 -0500 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id C069630821EB; Wed, 7 Nov 2018 21:45:28 +0000 (UTC) Received: from shodan.usersys.redhat.com (unknown [10.43.17.28]) by smtp.corp.redhat.com (Postfix) with ESMTP id 8A14F18BA2; Wed, 7 Nov 2018 21:45:28 +0000 (UTC) Received: by shodan.usersys.redhat.com (Postfix, from userid 1000) id E15712C1A13; Wed, 7 Nov 2018 22:45:27 +0100 (CET) From: Artem Savkov To: Josh Poimboeuf Cc: Peter Zijlstra , linux-kernel@vger.kernel.org, Artem Savkov Subject: [PATCH v2] objtool: fix .cold. functions parent symbols search Date: Wed, 7 Nov 2018 22:45:15 +0100 Message-Id: <20181107214515.9183-1-asavkov@redhat.com> In-Reply-To: <20181107202942.s24ej5mnh4b3mflw@treble> References: <20181107202942.s24ej5mnh4b3mflw@treble> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.47]); Wed, 07 Nov 2018 21:45:28 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Because find_symbol_by_name() traverses the same lists as read_symbols() changing sym->name in place without copying it affects the result of find_symbol_by_name() and, in case when ".cold" function precedes it's parent in sec->symbol_list, can result in function being considered a parent of itself. This leads to function length being set to 0 and other consequent side-effects including a segfault in add_switch_table(). The effects of this bug are only visible when building with -ffunction-sections in KCFLAGS. Fix by copying the search string instead of modifying it in place. Signed-off-by: Artem Savkov --- tools/objtool/elf.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index 6dbb9fae0f9d..781c8afb29b9 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -298,6 +298,7 @@ static int read_symbols(struct elf *elf) /* Create parent/child links for any cold subfunctions */ list_for_each_entry(sec, &elf->sections, list) { list_for_each_entry(sym, &sec->symbol_list, list) { + char *pname; if (sym->type != STT_FUNC) continue; sym->pfunc = sym->cfunc = sym; @@ -305,9 +306,9 @@ static int read_symbols(struct elf *elf) if (!coldstr) continue; - coldstr[0] = '\0'; - pfunc = find_symbol_by_name(elf, sym->name); - coldstr[0] = '.'; + pname = strndup(sym->name, coldstr - sym->name); + pfunc = find_symbol_by_name(elf, pname); + free(pname); if (!pfunc) { WARN("%s(): can't find parent function", -- 2.17.2