From: Masami Hiramatsu <mhiramat@kernel.org>
To: Andy Lutomirski <luto@kernel.org>
Cc: Borislav Petkov <bp@alien8.de>,
Masami Hiramatsu <mhiramat@kernel.org>, x86-ml <x86@kernel.org>,
Joerg Roedel <jroedel@suse.de>,
lkml <linux-kernel@vger.kernel.org>
Subject: Re: [RFC] Have insn decoder functions return success/failure
Date: Sat, 24 Oct 2020 16:21:03 +0900 [thread overview]
Message-ID: <20201024162103.a479e06af9bbc6c83ffea1a7@kernel.org> (raw)
In-Reply-To: <CALCETrVQDVLPwTTXgsRYSjxVmzeK5ekmrEiT2rWkQKO0inRLGQ@mail.gmail.com>
On Fri, 23 Oct 2020 17:12:49 -0700
Andy Lutomirski <luto@kernel.org> wrote:
> On Fri, Oct 23, 2020 at 4:27 PM Borislav Petkov <bp@alien8.de> wrote:
> >
> > On Fri, Oct 23, 2020 at 07:47:04PM +0900, Masami Hiramatsu wrote:
> > > Thanks! I look forward to it.
> >
> > Ok, here's a first stab, it is a single big diff and totally untested
> > but it should show what I mean. I've made some notes while converting,
> > as I went along.
> >
> > Have a look at insn_decode() and its call sites: they are almost trivial
> > now because caller needs simply to do:
> >
> > if (insn_decode(insn, buffer, ...))
> >
> > and not care about any helper functions.
> >
> > For some of the call sites it still makes sense to do a piecemeal insn
> > decoding and I've left them this way but they can be converted too, if
> > one wants.
> >
> > In any case, just have a look please and lemme know if that looks OKish.
> > I'll do the actual splitting and testing afterwards.
> >
> > And what Andy wants can't be done with the decoder because it already
> > gets a fixed size buffer and length - it doesn't do the fetching. The
> > caller does.
> >
> > What you wanna do:
> >
> > > len = min(15, remaining bytes in page);
> > > fetch len bytes;
> > > insn_init();
> > > ret = insn_decode_fully();
> >
> > <--- you can't always know here whether the insn is valid if you don't
> > have all the bytes. But you can always fetch *all* bytes and then give
> > it to the decoder for checking.
> >
> > Also, this doesn't make any sense: try insn decode on a subset of bytes
> > and then if it fails, try it on the whole set of bytes. Why even try the
> > subset - it will almost always fail.
>
> I disagree. A real CPU does exactly what I'm describing. If I stick
> 0xcc at the end of a page and a make the next page not-present, I get
> #BP, not #PF. But if I stick 0x0F at the end of a page and mark the
> next page not-present, I get #PF. If we're trying to decode an
> instruction in user memory, we can kludge it by trying to fetch 15
> bytes and handling -EFAULT by fetching fewer bytes, but that's gross
> and doesn't really have the right semantics. What we actually want is
> to fetch up to the page boundary and try to decode it. If it's a
> valid instruction or if it's definitely invalid, we're done.
> Otherwise we fetch across the page boundary.
>
> Eventually we should wrap this whole mess up in an insn_decode_user()
> helper that does the right thing. And we can then make that helper
> extra fancy by getting PKRU and EPT-hacker-execute-only right, whereas
> we currently get these cases wrong.
+1. To handle the user-space (untrusted) instruction, we need to
take more care about page boundary and presense. Also less side-effect
is perferrable.
Thank you,
--
Masami Hiramatsu <mhiramat@kernel.org>
next prev parent reply other threads:[~2020-10-24 7:21 UTC|newest]
Thread overview: 29+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-10-20 12:02 Borislav Petkov
2020-10-20 14:27 ` Masami Hiramatsu
2020-10-20 14:37 ` Borislav Petkov
2020-10-21 0:50 ` Masami Hiramatsu
2020-10-21 9:27 ` Borislav Petkov
2020-10-21 14:26 ` Masami Hiramatsu
2020-10-21 16:45 ` Borislav Petkov
2020-10-22 7:31 ` Masami Hiramatsu
2020-10-22 9:30 ` Borislav Petkov
2020-10-22 13:21 ` Masami Hiramatsu
2020-10-22 17:58 ` Andy Lutomirski
2020-10-23 9:20 ` Borislav Petkov
2020-10-23 9:28 ` Masami Hiramatsu
2020-10-23 9:32 ` Borislav Petkov
2020-10-23 10:47 ` Masami Hiramatsu
2020-10-23 23:27 ` Borislav Petkov
2020-10-24 0:12 ` Andy Lutomirski
2020-10-24 7:21 ` Masami Hiramatsu [this message]
2020-10-24 8:23 ` Borislav Petkov
2020-10-24 16:10 ` Andy Lutomirski
2020-10-27 13:42 ` Borislav Petkov
2020-10-28 11:36 ` Masami Hiramatsu
2020-10-24 7:13 ` Masami Hiramatsu
2020-10-24 8:24 ` Borislav Petkov
2020-10-29 12:42 ` Borislav Petkov
2020-10-30 1:24 ` Masami Hiramatsu
2020-10-30 13:07 ` Borislav Petkov
2020-10-23 9:17 ` Borislav Petkov
2020-10-22 8:04 ` Peter Zijlstra
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=20201024162103.a479e06af9bbc6c83ffea1a7@kernel.org \
--to=mhiramat@kernel.org \
--cc=bp@alien8.de \
--cc=jroedel@suse.de \
--cc=linux-kernel@vger.kernel.org \
--cc=luto@kernel.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
all inboxes | Powered by JetHome®