Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp3620506imu; Sun, 11 Nov 2018 19:38:47 -0800 (PST) X-Google-Smtp-Source: AJdET5ct+Y8g5W8X8oS2AbDzvuuYC4hMwCHNI5gwwMTcze9GmIXdtkocDRrJBoeLYzxQCWWqL6do X-Received: by 2002:a17:902:b68d:: with SMTP id c13mr253944pls.102.1541993927847; Sun, 11 Nov 2018 19:38:47 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541993927; cv=none; d=google.com; s=arc-20160816; b=s/QAbiAhdTFjUBucvpTivW6EhnWZcwX7hl4PS9ZtLVeL5A21kBrB4R+HhvQa9776fn DeyPwEh0ESpOWuBHdNvo9j89ozbozXlR7UKPuwtlhlPIbzQ1j3A/SWI8udOf4ZhWrnQE Q5rSPtcwsq0aW4crncmoo6/Jh6SGcsjWAbjJlfIl3mLoEuZETIzjm0JHrcFGP1VQKhv3 YxXSMXcItOry1i4/e5thRcughvXbu2qUyQsOQ6y3WsVp5SzZ/ZgTH+oJq8mRm+H1ELe8 T/5JFGb3gYbQWhY9p7Rak1srjBFAqzBlN4Xdwv18bOCIFLT0/RWTnYaspvqijk+MYApC 1Wqw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date; bh=Z8XopAQXDf7AchvjMPuE3tNMZgxrtCqUFEpOiqmQbeU=; b=aokfoH3vssw9Quv2IgDJua/we+guGeGPN5scHqQTioTBG97aY0IpKsG9KcYV47m4gb j4riRy55FtnoBZHv8rMVM0zIZt6Y0M+Ni0TiifvQoJM2QKgVxL3kvRYB307bR9HE9bAl pC9N4UUHcTu5hpclpsuNd961HObMLPMkX/SdubLMbnnqUVvqvAjn46O6WmDdhhh/lDLH wjLJcAdAkwD/A0cMS6tD9YAMz/uywiaf5n/MWAr5dEKxzg90lyduPyATYSPR2cRjurhv B8eHPiPWSnIBAIIh3MhR+5WI6SVCYhiCctj4IW5CHNcZ7xVsXvo8CIZKBOv/ZEIg8Sah +1gA== 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 s82-v6si17389204pfk.197.2018.11.11.19.38.30; Sun, 11 Nov 2018 19:38:47 -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 S1730420AbeKLN3R (ORCPT + 99 others); Mon, 12 Nov 2018 08:29:17 -0500 Received: from mx1.redhat.com ([209.132.183.28]:59590 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726270AbeKLN3R (ORCPT ); Mon, 12 Nov 2018 08:29:17 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 0F27D3081256; Mon, 12 Nov 2018 03:38:05 +0000 (UTC) Received: from treble (ovpn-121-1.rdu2.redhat.com [10.10.121.1]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0C604101961E; Mon, 12 Nov 2018 03:38:02 +0000 (UTC) Date: Sun, 11 Nov 2018 21:38:00 -0600 From: Josh Poimboeuf To: Artem Savkov Cc: Peter Zijlstra , linux-kernel@vger.kernel.org Subject: Re: [PATCH v2] objtool: fix .cold. functions parent symbols search Message-ID: <20181112033800.ujolxvkwzz72lxhm@treble> References: <20181107202942.s24ej5mnh4b3mflw@treble> <20181107214515.9183-1-asavkov@redhat.com> <20181109172309.myxc2owh3k5v2wfb@treble> <20181110121836.GG11183@shodan.usersys.redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20181110121836.GG11183@shodan.usersys.redhat.com> User-Agent: NeoMutt/20180716 X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.49]); Mon, 12 Nov 2018 03:38:05 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Sat, Nov 10, 2018 at 01:18:36PM +0100, Artem Savkov wrote: > On Fri, Nov 09, 2018 at 11:23:09AM -0600, Josh Poimboeuf wrote: > > On Wed, Nov 07, 2018 at 10:45:15PM +0100, Artem Savkov wrote: > > > 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 > > > > This needs a "Fixes" tag to identify the patch which introduced the bug. > > Ok, will do. > > > > --- > > > 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", > > > > strndup()'s return code needs to be checked. > > > > Also, for such a short-lived allocation, I think a stack-allocated > > string would be better. > > Hm, there seems to be no limit on lengths of strings in string table. > What size would you consider reasonable for this stack-allocated string? I think it's fine to pick a reasonable maximum (128?) and then verify the string fits in the array before copying it. -- Josh