The Linux Kernel Mailing List
 help / color / mirror / Atom feed
From: Thomas Gleixner <tglx@linutronix.de>
To: Kees Cook <keescook@chromium.org>
Cc: "Luis R. Rodriguez" <mcgrof@kernel.org>,
	Steven Rostedt <rostedt@goodmis.org>,
	LKML <linux-kernel@vger.kernel.org>,
	"x86@kernel.org" <x86@kernel.org>,
	Masami Hiramatsu <mhiramat@kernel.org>,
	Peter Zijlstra <peterz@infradead.org>
Subject: Re: [PATCH V2] x86/ftrace: Make sure that ftrace trampolines are not RWX
Date: Fri, 26 May 2017 09:03:13 +0200 (CEST)	[thread overview]
Message-ID: <alpine.DEB.2.20.1705260859290.1902@nanos> (raw)
In-Reply-To: <CAGXu5j+=-3Po0SoO8N=LMzSTEA1y6sFnMQwq71Nwiw4dAXeABA@mail.gmail.com>

On Thu, 25 May 2017, Kees Cook wrote:
> On Thu, May 25, 2017 at 10:46 AM, Luis R. Rodriguez <mcgrof@kernel.org> wrote:
> > On Thu, May 25, 2017 at 10:57:51AM +0200, Thomas Gleixner wrote:
> >> ftrace use module_alloc() to allocate trampoline pages. The mapping of
> >> module_alloc() is RWX, which makes sense as the memory is written to right
> >> after allocation. But nothing makes these pages RO after writing to them.
> >>
> >> Add proper set_memory_rw/ro() calls to protect the trampolines after
> >> modification.
> >>
> >> Signed-off-by: Thomas Gleixner <tglx@linutronix.de>
> >> ---
> >>  arch/x86/kernel/ftrace.c |   20 ++++++++++++++------
> >>  1 file changed, 14 insertions(+), 6 deletions(-)
> >>
> >> --- a/arch/x86/kernel/ftrace.c
> >> +++ b/arch/x86/kernel/ftrace.c
> >> @@ -689,8 +689,12 @@ static inline void *alloc_tramp(unsigned
> >>  {
> >>       return module_alloc(size);
> >>  }
> >> -static inline void tramp_free(void *tramp)
> >> +static inline void tramp_free(void *tramp, int size)
> >>  {
> >> +     int npages = PAGE_ALIGN(size) >> PAGE_SHIFT;
> >> +
> >> +     set_memory_nx((unsigned long)tramp, npages);
> >> +     set_memory_rw((unsigned long)tramp, npages);
> >>       module_memfree(tramp);
> >>  }
> >
> > Can/should module_memfree() just do this for users? With Masami's fix that'd
> > be 2 users already.
> 
> It seems like it really should. That would put it in a single place
> and avoid this mistake again in the future. Does module_memfree() have
> access to the allocation size, or does that need to get plumbed?

No, it doesn't. But the number of instances is pretty limited.

Btw, looking at BPF. It allocates memory via module_alloc() which means
it's RWX. There is nothing in that BPF code which changes the permissions
afterwards ....

Thanks,

	tglx

  reply	other threads:[~2017-05-26  7:03 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-05-24 13:47 [PATCH] x86/ftrace: Make sure that ftrace trampolines are not RWX Thomas Gleixner
2017-05-24 14:33 ` Masami Hiramatsu
2017-05-24 15:04 ` Steven Rostedt
2017-05-24 17:47 ` Steven Rostedt
2017-05-24 18:16   ` Luis R. Rodriguez
2017-05-24 18:53     ` Thomas Gleixner
2017-05-24 19:34       ` Luis R. Rodriguez
2017-05-24 19:13   ` Thomas Gleixner
2017-05-24 22:25     ` Steven Rostedt
2017-05-24 23:18       ` Luis R. Rodriguez
2017-05-25  6:25       ` Thomas Gleixner
2017-05-25  8:57         ` [PATCH V2] " Thomas Gleixner
2017-05-25 15:15           ` Steven Rostedt
2017-05-25 17:46           ` Luis R. Rodriguez
2017-05-25 19:51             ` Kees Cook
2017-05-26  7:03               ` Thomas Gleixner [this message]
2017-05-26  9:27                 ` Heiko Carstens
2017-05-26  9:56                   ` Thomas Gleixner
2017-05-26 11:40                   ` Michael Ellerman
2017-05-26  9:49               ` Masami Hiramatsu
2017-05-26 13:37           ` Steven Rostedt
2017-05-26 13:50             ` Thomas Gleixner
2017-05-26 13:58               ` Steven Rostedt
2017-05-25  9:09       ` [PATCH] " Masami Hiramatsu
2017-05-25 10:34         ` Masami Hiramatsu
2017-05-25 15:18           ` Steven Rostedt
2017-05-26  1:34             ` Masami Hiramatsu

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=alpine.DEB.2.20.1705260859290.1902@nanos \
    --to=tglx@linutronix.de \
    --cc=keescook@chromium.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mcgrof@kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=peterz@infradead.org \
    --cc=rostedt@goodmis.org \
    --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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox