All of lore.kernel.org
 help / color / mirror / Atom feed
From: Josh Poimboeuf <jpoimboe@redhat.com>
To: Petr Mladek <pmladek@suse.cz>
Cc: Thomas Gleixner <tglx@linutronix.de>,
	Ingo Molnar <mingo@redhat.com>, "H. Peter Anvin" <hpa@zytor.com>,
	Michal Marek <mmarek@suse.cz>,
	Peter Zijlstra <peterz@infradead.org>,
	x86@kernel.org, live-patching@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: Re: [PATCH 1/2] x86, stackvalidate: Compile-time stack frame pointer validation
Date: Tue, 28 Apr 2015 12:54:28 -0500	[thread overview]
Message-ID: <20150428175428.GA29346@treble.redhat.com> (raw)
In-Reply-To: <20150428164414.GG21044@dhcp128.suse.cz>

On Tue, Apr 28, 2015 at 06:44:15PM +0200, Petr Mladek wrote:
> On Mon 2015-04-27 08:56:27, Josh Poimboeuf wrote:
> > +		case 0x89:
> > +			insn_get_modrm(&insn);
> > +			if (insn.modrm.bytes[0] == 0xe5)
> > +				/* mov sp, bp */
> > +				mov++;
> > +			break;
> 
> I am a bit courious. You check 0x89 and 0x5e but I see the following
> in the disasembly:
> 
>        	   48 89 e5                mov    %rsp,%rbp
> 
> I wonder if 48 is just a prefix that is filtered by insn_get_opcode or
> what.
> 
> Sigh, I still need to learn a lot about assembly.

Yeah, 0x48 is a prefix which means the operands are 64-bit.  Here is a
useful, albeit very dense, x86 assembly reference:

  http://ref.x86asm.net/coder64.html


> > +		case 0xc3: /* ret */
> > +			ret++;
> > +			break;
> > +		}
> > +	}
> > +
> > +	if (push != 1 || mov != 1 || !pop || !ret || pop != ret) {
> > +		WARN("%s() is missing frame pointer logic.  Please use FUNC_ENTER.",
> > +		     func->name);
> 
> This looks quite weak condition to me. It accepts the instructions in
> any order and at any place.

It's really designed to determine whether the frame pointer is being
updated, with some basic sanity checks.  It doesn't guarantee that it's
being done _correctly_.  Which is ok I think, since all assembly code is
hand-coded by people who (hopefully) are being careful and know exactly
what they're doing.

And as it turns out, today, >99% of callable asm functions don't have
any frame pointer logic and will fail this check.

> Also livepatching will rely on having fentry before the prologue. Do
> you plan to add support for this ordering?

No, because this is only intended to analyze hand-crafted asm code,
which generally doesn't have the fentry call (and so live patching can't
patch it).

However, if we wanted to enable the patching of asm functions in the
future, we could consider adding the fentry call to the FUNC_ENTER macro
later on.

> > +	for (i = 0; i < sections_nr; i++) {
> > +		sec = malloc(sizeof(*sec));
> > +		if (!sec) {
> > +			perror("malloc");
> > +			return -1;
> > +		}
> > +		memset(sec, 0, sizeof(*sec));
> > +
> > +		INIT_LIST_HEAD(&sec->symbols);
> > +		INIT_LIST_HEAD(&sec->relas);
> > +
> > +		s = elf_getscn(elf->elf, i);
> > +		if (!s) {
> > +			perror("elf_getscn");
> > +			return -1;
> 
> This is leaking "sec". It is not added to the list, so you could not
> free it later. The same problem is on many other locations.

Ah, right.  It really doesn't matter much since this is a short running
userspace program, but I'll fix it up.

> > +	/* sanity check, one more call to elf_nextscn() should return NULL */
> > +	if (elf_nextscn(elf->elf, s)) {
> > +		WARN("section entry mismatch");
> > +		return -1;
> > +	}
> > +
> > +	return 0;
> > +}
> 
> I basically ended here. I was rather curious how such a thing could
> get implemented than doing a proper review.

Thanks a lot for looking!

-- 
Josh

  reply	other threads:[~2015-04-28 17:54 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-04-27 13:56 [PATCH 0/2] Compile-time stack frame pointer validation Josh Poimboeuf
2015-04-27 13:56 ` [PATCH 1/2] x86, stackvalidate: " Josh Poimboeuf
2015-04-28 12:16   ` Peter Zijlstra
2015-04-28 14:04     ` Josh Poimboeuf
2015-04-28 14:08       ` Peter Zijlstra
2015-04-28 14:21         ` Josh Poimboeuf
2015-04-28 14:26           ` Peter Zijlstra
2015-04-28 16:44   ` Petr Mladek
2015-04-28 17:54     ` Josh Poimboeuf [this message]
2015-04-27 13:56 ` [PATCH 2/2] x86, stackvalidate: Add asm frame pointer setup macros 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=20150428175428.GA29346@treble.redhat.com \
    --to=jpoimboe@redhat.com \
    --cc=hpa@zytor.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=live-patching@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=mmarek@suse.cz \
    --cc=peterz@infradead.org \
    --cc=pmladek@suse.cz \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.