Re: [RFC] Have insn decoder functions return success/failure

From: Andy Lutomirski
Date: Sat Oct 24 2020 - 12:20:46 EST


On Sat, Oct 24, 2020 at 1:23 AM Borislav Petkov <bp@xxxxxxxxx> wrote:
>
> On Fri, Oct 23, 2020 at 05:12:49PM -0700, Andy Lutomirski wrote:
> > I disagree. A real CPU does exactly what I'm describing. If I stick
>
> A real modern CPU fetches up to 32 bytes insn window which it tries
> to decode etc. I don't know, though, what it does when that fetch
> encounters a fault - I will have to ask people. I'm not sure it would
> even try to feed a shorter stream of bytes to the decoder but lemme
> ask...
>

I can pretty much guarantee that a real modern CPU is able to decode a
<15 byte instruction that is followed by unmapped or non-executable
pages. I don't know specifically how the CPU implements it, but it
works.

> > 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.
>
> We can do that but why would you put all that logic in the insn decoder?
> Is that use case sooo important?

It's not sooo important, but I think it would be nice to at least try
to be fully correct.

>
> I mean, it would work that way anyway *even* *now* - the insn decoder
> will tell you that the insn it decoded wasn't valid and you, as a
> caller, know that you didn't fetch the whole 15 bytes so that means
> that you still need to fetch some more. You've got all the relevant
> information.

How so?

If I have a page that ends in 0x0F followed by an unmapped page, then
the correct response to an attempt to decode is SIGSEGV or -EFAULT.
If there's a page there that contains garbage, then the correct
response is SIGILL or -EINVAL or similar. These are different
scenarios, and I don't think the current decoder API can be used to
distinguish them.

>
> > Eventually we should wrap this whole mess up in an insn_decode_user()
> > helper that does the right thing.
>
> Oh sure, you can do that easily. Just put the logic which determines
> that it copied a shorter buffer and that it attempts to decode the
> shorter buffer first in it. Yah, that can work.
>
> > 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.
> >
> > Does this make sense?
>
> Sure, but you could point me to those cases so that I can get a better
> idea what they do exactly.

Take a look at fixup_umip_exception(). It currently has two bugs:

1. If it tries to decode a short instruction followed by something
like a userfaultfd page, it will incorrectly trigger the userfaultfd.
This is because it tries to fetch MAX_INSN_SIZE even if the
instruction is shorter than that.

2. It will fail on execute-only memory, and it will succeed on NX
memory. copy_from_user() is the wrong API to use here. We don't have
the right API, and we should add it. (Hi Dave - what's the best way
to do this? New get_user_pages() mode? Try to fault it in, hold an
appropriate lock, walk the page tables to check permissions, and then
access the user address directly?)

I don't know how much anyone really cares about this for UMIP, but
with SEV-ES and such, I can see this becoming more important.