Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp4038215imu; Mon, 12 Nov 2018 04:56:20 -0800 (PST) X-Google-Smtp-Source: AJdET5d0u9v8rTuc+Q0F1I1awoVaaHRfwa2jrl/qBFMUTWAxakMbvhZbb1xAyBm2dRLg+faEWRLw X-Received: by 2002:a63:4b60:: with SMTP id k32mr728714pgl.186.1542027380591; Mon, 12 Nov 2018 04:56:20 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1542027380; cv=none; d=google.com; s=arc-20160816; b=JascILyQgrNfHuZzPHCPdQP7cgKuUH6deDy6+VJvlrA1UKjDfo6uD0qMX0qDvmXK5o OnE8/o1+JDb220qY+hZW21i2lxIangahiwAV+b44NW0/cnMkg1omwtum51qOGoCW9Ppt YrhSWKeUh2/pfjCyMsD+1paSCjNQJ0dFnOTqCOXYHIymvU6WQwtjT5f1mcYakACVNRYc Rko7tkrUvXUeUdAgWR6LYT5ki5aYrcH14tG/DP7lVTGpEZ1rdzXW4dnwINp2BRTcXCIN HDtVDj/ASwedQRBJ0OFqZBIGKG9/o9SBhqFELxJuig7WBbVhLEOtFhK6bh7/S2NQRdly QxiA== 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=VWCeSKH/7mqsbU5gwoc1VH0UKCrFfUkUqZ/7rYG6K14=; b=l1aEPRwoDUSuu9Xo0OUskr8baJIr9qnMx4IGfo1r7/lTTwN3Y3+aDeTnb9VWswQR5G UQ/2MQkVKLHcHiXntC8xVJxlmix8FlmIC/cYot/1jf0MdbvWwZAWLmZQYs93CtNa8Wei +153oFyFchRh/pqCzYC2hFWcGiXrz+YvYjJomNE0yNZvwIsyXxsCrGOnn3wN4utrchoh iUsJp9bNUkj01txrrhjmy/qc7kKv9x/jItsBBZz+tBIsD03UagXdq1XguzosFrgGqVa0 gZmEta4S5SI/2j9f89anRck6wS+BnMHlhP6JwUoStJQmgbod66/qAVicpuj3IsVuNr4Q Wa8w== 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 v123-v6si18460547pfb.65.2018.11.12.04.56.05; Mon, 12 Nov 2018 04:56:20 -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 S1729573AbeKLWsw (ORCPT + 99 others); Mon, 12 Nov 2018 17:48:52 -0500 Received: from mx1.redhat.com ([209.132.183.28]:51854 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726693AbeKLWsv (ORCPT ); Mon, 12 Nov 2018 17:48:51 -0500 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1F59213AAD; Mon, 12 Nov 2018 12:55:42 +0000 (UTC) Received: from shodan.usersys.redhat.com (unknown [10.43.17.28]) by smtp.corp.redhat.com (Postfix) with ESMTP id 4ADC86012E; Mon, 12 Nov 2018 12:55:41 +0000 (UTC) Received: by shodan.usersys.redhat.com (Postfix, from userid 1000) id A1A0D2C0F96; Mon, 12 Nov 2018 13:55:40 +0100 (CET) From: Artem Savkov To: Josh Poimboeuf Cc: Peter Zijlstra , linux-kernel@vger.kernel.org, Artem Savkov Subject: [PATCH v3 2/2] objtool: fix .cold functions parent symbols search Date: Mon, 12 Nov 2018 13:55:19 +0100 Message-Id: <20181112125519.26855-3-asavkov@redhat.com> In-Reply-To: <20181112125519.26855-1-asavkov@redhat.com> References: <20181112033800.ujolxvkwzz72lxhm@treble> <20181112125519.26855-1-asavkov@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.29]); Mon, 12 Nov 2018 12:55:42 +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 3decd43477df..15d9acfb2c97 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); + goto cold_err; + } + + 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.17.2