Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp1006924imu; Fri, 9 Nov 2018 09:25:54 -0800 (PST) X-Google-Smtp-Source: AJdET5fv0clwtwR+fGlcsM7CUdBv+pxZsCUzeaarwcUJeANXcdE9gXmChKWPYowj2ZxZfJCOcK/x X-Received: by 2002:a63:40c6:: with SMTP id n189mr1535155pga.355.1541784354036; Fri, 09 Nov 2018 09:25:54 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1541784354; cv=none; d=google.com; s=arc-20160816; b=deRpnFnP8phQDHdMgNCbuvdC6CP0NEF/13NxvW4f8RcpPBAt55u3+QOzxEgdqJHkVo 78TA3v2t3yKHvlrOv7X/82UrzE4duIWC2M6DmAloiF+8dxdelIyN2FtDTXSQQ0r/+Qom WBCBYfr4jmQzjZCL0QV1WTpxMZpYPflKChKEZzQhVQh+Fqn56gyA8pqu7sjkrlCoXSNh QcU+Q7s95e8JETDB/mCFXtJNOAfYlnyxtSlNOi9XsWQ+5EdLEr8YMbF5Hgg2B9L3byMb N8uexko3yWwgwzXOHPzAYWnqc2mCyBzwYJ30+mGiSGmFjpVXp/NAIvshuNPS4xd+Dxcv SpAQ== 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=ea8GO7yJkcgr4jP7o0yBtEHvsNopTN/4l5s5d6Uu4y8=; b=wEsTgCpw/7Fuikccyaqf3/WWNqkH3pryp+xfkq1Rh+pv4x6KHlB0qZsbM/CguRDOUe i1yAFvYhrl2c/iFRj2XVOqrieDK5Sv0YbHzJUUF0fi2/kYrFUGIrkiV/gyM2rVBzrKdq TKdikEuOP85eBJlQ+OI69mgVuflHRxIPVXqu8JKGPMYWxm8XtWwj2t/ATew0gDT0MYaG Q9sk9sRrWivMnmGSoBNeFN/zxSng1S98m3Js1Xcnd6CLQEJbUAeo1bqg21atDGa9X5I8 fYZ8CvCpL2jl5vrZqqYWC3LvBGZ+Ou5VtYzeb3sVaOU73Bo2jpkbVjTupXUJVP8d4Z+y ZCNQ== 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 z14-v6si9272081pfc.11.2018.11.09.09.25.30; Fri, 09 Nov 2018 09:25:54 -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 S1728229AbeKJDEn (ORCPT + 99 others); Fri, 9 Nov 2018 22:04:43 -0500 Received: from mx1.redhat.com ([209.132.183.28]:56758 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727995AbeKJDEn (ORCPT ); Fri, 9 Nov 2018 22:04:43 -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 C66E081E05; Fri, 9 Nov 2018 17:23:12 +0000 (UTC) Received: from treble (ovpn-124-61.rdu2.redhat.com [10.10.124.61]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 46460600CD; Fri, 9 Nov 2018 17:23:12 +0000 (UTC) Date: Fri, 9 Nov 2018 11:23:09 -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: <20181109172309.myxc2owh3k5v2wfb@treble> References: <20181107202942.s24ej5mnh4b3mflw@treble> <20181107214515.9183-1-asavkov@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline In-Reply-To: <20181107214515.9183-1-asavkov@redhat.com> User-Agent: NeoMutt/20180716 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.25]); Fri, 09 Nov 2018 17:23:12 +0000 (UTC) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. > --- > 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. -- Josh