Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp365528imu; Tue, 20 Nov 2018 00:07:50 -0800 (PST) X-Google-Smtp-Source: AJdET5e9CXmy4l0BCU8kZxBHPChu3ziDvdeIdNXVDaLBs+TK7SvkuPUD2Nr0ds8ibmXGO88Eas+A X-Received: by 2002:a62:14d1:: with SMTP id 200mr1173908pfu.103.1542701270622; Tue, 20 Nov 2018 00:07:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542701270; cv=none; d=google.com; s=arc-20160816; b=cTVoU2ViG5CqnO+UAEji1DU6jlxwFvXSFQ9lnJYqxqRIZ0Jo6EE70g8CjsWAJYz37N XI9lKthjUKuFsg+4r0BB9mR60x5Ls06Z3jJassqt/aiQsBkkuKENl2dTZtFzHcmOUvrK ILFx68VzBRYFku/TnriY6nvltmOps57il1bTkxeEi+F52H/kv/Sm1ZQXtvhx7pWwibAF /Y6jxqIVc4z4mhi1D+/6tZAXWXjonNo/bEDxKaKKbYWviSGeJTrHK1D9IxL6QnBPp+pM TrCAIf7wjTY9z5xVPv9WxxNZgayz521aRgUN+y7AXSELz80SRVJB807uhF4GmF45r9wT rUDQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from; bh=ZUifHcKN1XT6IQcT7/Y0c1VyehA7bMoRzPvWT04mj0o=; b=W99660CQ/M2DKfZDblyx9zB+IjmOg2lejDruV9pdvD811HKp6UuE9joqrbt/PLjRRq ZFJzc5AtLX62nwYvww+aX/drqS7bksPpgS2nxLCzjyd6bBGNeY7oYabcJJKVrXWD6v7H 9iexBzG1wU8KWYriRZsyKVKJME2EpkWEFL7ZntFyG+0Z7beoFsVinl0253sFS+G0ut2G ARW+6CieqOOEQWPti9PIadFTCL5fzGdrHKlhUvPxy/VQy+pER1KBASCVpdQ7+cqPLjNK 2RWAmpxCKbgzYmlZv8ygu39OtI0w4qJ5sllzv2SueXxlMtIP55NaFDK6GAzVHFRYB1s9 WK1w== 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 a3-v6si40598004pgg.413.2018.11.20.00.07.36; Tue, 20 Nov 2018 00:07:50 -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 S1726183AbeKTSd7 (ORCPT + 99 others); Tue, 20 Nov 2018 13:33:59 -0500 Received: from mx1.redhat.com ([209.132.183.28]:45504 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725828AbeKTSdx (ORCPT ); Tue, 20 Nov 2018 13:33:53 -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 8BD20308FE8F; Tue, 20 Nov 2018 08:06:05 +0000 (UTC) Received: from shodan.usersys.redhat.com (unknown [10.43.17.28]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4DF4517D6B; Tue, 20 Nov 2018 08:06:05 +0000 (UTC) Received: by shodan.usersys.redhat.com (Postfix, from userid 1000) id 831F72C1404; Tue, 20 Nov 2018 09:06:04 +0100 (CET) From: Artem Savkov To: Josh Poimboeuf Cc: Peter Zijlstra , linux-kernel@vger.kernel.org, Artem Savkov Subject: [PATCH v4 2/2] objtool: fix .cold functions parent symbols search Date: Tue, 20 Nov 2018 09:05:48 +0100 Message-Id: <20181120080548.26082-3-asavkov@redhat.com> In-Reply-To: <20181120080548.26082-1-asavkov@redhat.com> References: <20181112125519.26855-1-asavkov@redhat.com> <20181120080548.26082-1-asavkov@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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.49]); Tue, 20 Nov 2018 08:06:05 +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. Fixes: 13810435b9a7 ("objtool: Support GCC 8's cold subfunctions") Signed-off-by: Artem Savkov --- tools/objtool/elf.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/tools/objtool/elf.c b/tools/objtool/elf.c index e7a7ac40e045..b8f3cca8e58b 100644 --- a/tools/objtool/elf.c +++ b/tools/objtool/elf.c @@ -31,6 +31,8 @@ #include "elf.h" #include "warn.h" +#define MAX_NAME_LEN 128 + struct section *find_section_by_name(struct elf *elf, const char *name) { struct section *sec; @@ -298,6 +300,8 @@ 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[MAX_NAME_LEN + 1]; + size_t pnamelen; if (sym->type != STT_FUNC) continue; sym->pfunc = sym->cfunc = sym; @@ -305,9 +309,16 @@ static int read_symbols(struct elf *elf) if (!coldstr) continue; - coldstr[0] = '\0'; - pfunc = find_symbol_by_name(elf, sym->name); - coldstr[0] = '.'; + pnamelen = coldstr - sym->name; + if (pnamelen > MAX_NAME_LEN) { + WARN("%s(): parent function name exceeds maximum length of %d characters", + sym->name, MAX_NAME_LEN); + return -1; + } + + strncpy(pname, sym->name, pnamelen); + pname[pnamelen] = '\0'; + pfunc = find_symbol_by_name(elf, pname); if (!pfunc) { WARN("%s(): can't find parent function", -- 2.19.1