linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Josh Poimboeuf <jpoimboe@redhat.com>
To: Artem Savkov <asavkov@redhat.com>
Cc: Peter Zijlstra <peterz@infradead.org>, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v2] objtool: fix .cold. functions parent symbols search
Date: Sun, 11 Nov 2018 21:38:00 -0600	[thread overview]
Message-ID: <20181112033800.ujolxvkwzz72lxhm@treble> (raw)
In-Reply-To: <20181110121836.GG11183@shodan.usersys.redhat.com>

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 <asavkov@redhat.com>
> > 
> > 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

  reply	other threads:[~2018-11-12  3:38 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-11-07 14:05 [PATCH] objtool: fix .cold. functions parent symbols search Artem Savkov
2018-11-07 17:08 ` Josh Poimboeuf
2018-11-07 18:42   ` Artem Savkov
2018-11-07 20:29     ` Josh Poimboeuf
2018-11-07 21:45       ` [PATCH v2] " Artem Savkov
2018-11-09 17:23         ` Josh Poimboeuf
2018-11-10 12:18           ` Artem Savkov
2018-11-12  3:38             ` Josh Poimboeuf [this message]
2018-11-12 12:55               ` [PATCH v3 0/2] objtool: read_symbols() fixes Artem Savkov
2018-11-12 12:55                 ` [PATCH v3 1/2] objtool: fix failed cold symbol doublefree Artem Savkov
2018-11-19 17:57                   ` Josh Poimboeuf
2018-11-12 12:55                 ` [PATCH v3 2/2] objtool: fix .cold functions parent symbols search Artem Savkov
2018-11-19 18:02                   ` Josh Poimboeuf
2018-11-20  8:05                 ` [PATCH v4 0/2] objtool: read_symbols() fixes Artem Savkov
2018-11-20  8:05                   ` [PATCH v4 1/2] objtool: fix failed cold symbol doublefree Artem Savkov
2018-11-20  8:05                   ` [PATCH v4 2/2] objtool: fix .cold functions parent symbols search Artem Savkov
2018-11-20 14:12                   ` [PATCH v4 0/2] objtool: read_symbols() fixes Josh Poimboeuf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20181112033800.ujolxvkwzz72lxhm@treble \
    --to=jpoimboe@redhat.com \
    --cc=asavkov@redhat.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=peterz@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).