Re: [PATCH v38 10/24] mm: Add vm_ops->mprotect()
From: Dave Hansen
Date: Wed Sep 30 2020 - 10:35:58 EST
On 9/29/20 5:20 PM, Jarkko Sakkinen wrote:
> On Tue, Sep 29, 2020 at 07:24:24AM -0700, Dave Hansen wrote:
>> On 9/28/20 9:05 PM, Jarkko Sakkinen wrote:
>>> On Mon, Sep 28, 2020 at 06:37:54PM -0700, Andy Lutomirski wrote:
>>>> I don’t personally care that much about EMODPE but, you could probably
>>>> get the point across with something like:
>>>>
>>>> SGX’s EPCM permission bits do not obviate the need to enforce these
>>>> rules in the PTEs because enclaves can freely modify the EPCM
>>>> permissions using EMODPE.
>>>>
>>>> IOW, EMODPE is not really special here; rather, EMODPE’s existence
>>>> demonstrates that EADD / EEXTEND are not special.
>>>
>>> So I did "disagree and commit" with this one. I'm not actually
>>> diagreeing on anything what Dave wrote, on the contrary it is an
>>> understandable high level description. I just thought that it would not
>>> hurt to remark that the ISA contains such peculiarities as EMODPE.
>>>
>>> I did only very rudimentary clean up for the text (e.g. fix the ioctl
>>> name, add shortt summary and not much else).
>>>
>>> Does not make sense to waste more time to this. I'll move on to
>>> implement the missing boot time patching for the vDSO so that we
>>> get the next version out.
>>>
>>> "
>>> mm: Add 'mprotect' hook to struct vm_operations_struct
>>>
>>> Background
>>> ==========
>>>
>>> 1. SGX enclave pages are populated with data by copying data to them
>>> from normal memory via ioctl(fd, SGX_IOC_ENCLAVE_ADD_PAGES).
>>> 2. We want to be able to restrict those normal memory data sources. For
>>> instance, before copying data to an executable enclave page, we might
>>> ensure that the source is executable.
>>
>> I know I wrote that. I suck, and I wrote it in a changelog-unacceptable
>> way. Folks dislike the use of "we" in these things. Here's a better
>> version:
>>
>> 2. It is desirable to be able to restrict those normal memory data
>> sources. For instance, the kernel can ensure that the source is
>> executable, before copying data to an executable enclave page.
>>
>>> 3. Enclave page permissions are dynamic just like normal permissions and
>>> can be adjusted at runtime with mprotect() (along with a
>>> corresponding special instruction inside the enclave).
>>> 4. The original data source may have have long since vanished at the
>>> time when enclave page permission are established (mmap() or
>>> mprotect()).
>>>
>>> Solution
>>> ========
>>>
>>> The solution is to force enclaves creators to declare their intent up front
>>> to ioctl(fd, SGX_IOC_ENCLAVE_ADD_PAGES). This intent can me immediately
>>> compared to the source data mapping (and rejected if necessary). It is
>>> also stashed off and then later compared with enclave PTEs to ensure that
>>> any future mmap()/mprotect() operations performed by the enclave creator or
>>> the enclave itself are consistent with the earlier declared permissions.
>>
>> Let's also say "... or *requested* by the enclave itself ...", since the
>> enclave itself can't directly make syscalls.
>
> Yes, it is definitely more understandable way to say it. Do you mind
> if I rephrase it as:
>
> "It is also stashed off and then later compared with enclave PTEs to
> ensure that any future mmap()/mprotect() operations performed by the
> enclave creator or requested the enclave by itself (e.g. by issuing
> ECLU[EMODPE]) are consistent with the earlier declared permissions."
>
> I'd just mention EMODPE as an example, but I'm also perfectly fine
> leaving that out :-) Not a big deal for me.
If I say it's a big deal for me, will you remove the bloody thing?
Mentioning EMODPE is a distraction for this patch. It's a big
distraction because it makes it sound like it is some kind of *peer* of
mmap()/mprotect(). It's not. It's subservient to x86 paging
protections and thus *IRRELEVANT* for this unless you care about the
intricacies of writing enclaves.
It's a big deal to me.
Also, I've tried to give this feedback previously, but the paging
permissions are also essentially irrelevant.
> Also, should there be commas, i.e. ", or requested the enclave by
> itself,"? I suck with English comma rules.
I'd just say this:
It is also stashed off and then later compared with enclave PTEs
to ensure that any future mmap()/mprotect() operations are
consistent with the earlier declared permissions.
Yours was starting to look pretty run on.
> "ioctl(fd, SGX_IOC_ENCLAVE_ADD_PAGES, ...) checks for every page that
> Thread Control Structure (TCS) pages are always added with zero
> permissions and no pages are sourced from noexec partitions. TCS pages
> are pages that work as entry points to the enclave. This is the basic
> acceptance criteria for any enclave page before it gets mapped.
This is going off into the SGX weeds again.
We don't need to justify the ABI for an ioctl() introduced in a
different patch in *THIS* patch. Just remove this, please.
> After finishing this, the ioctl will project the enclave permissions to
> the corresponding VMA permissions and stores the result for later
> lookup.
That sounds vaguely relevant, although I'm not sure what permission
projection is. You use that terminology over and over, so you probably
need to define it.
> For regular pages this is an identity mapping but as an
> exception TCS pages are unconditionally mapped as RW VMA permssion even
> though their enclave permissions are zero. This required by the ISA.
I don't think this is relevant.
> This information will be used by sgx_mmap() and sgx_vma_protect() to
> enforce that higher permissions than the projected permissions will
> not be used by checking this for each every page in the address
> range.
I've given this feedback before. Please don't use "higher" and "lower"
permissions. "Stronger/weaker" is my preferred terminology.
You also don't have to *NAME* the functions. If I want to know where a
structure field is, grep is a better way to find that.
Does this lose any meaning if we just say:
This information will be to ensure that enclave PTEs will not be
created with permissions weaker than the source data
?
> By doing this, we give assets for LSM's to make decisions during the
> build time based on projected VMA permissions and the source VMA
> (either a file or anonymous mapping) that hold when the enclave is
> finally mapped to the visible memory."