From: Jarkko Sakkinen <jarkko@kernel.org>
To: Mel Gorman <mgorman@techsingularity.net>
Cc: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>,
x86@kernel.org, linux-sgx@vger.kernel.org,
linux-kernel@vger.kernel.org,
Sean Christopherson <sean.j.christopherson@intel.com>,
linux-mm@kvack.org, Andrew Morton <akpm@linux-foundation.org>,
Matthew Wilcox <willy@infradead.org>,
Jethro Beekman <jethro@fortanix.com>,
Darren Kenny <darren.kenny@oracle.com>,
andriy.shevchenko@linux.intel.com, asapek@google.com,
bp@alien8.de, cedric.xing@intel.com, chenalexchen@google.com,
conradparker@google.com, cyhanish@google.com,
dave.hansen@intel.com, haitao.huang@intel.com,
kai.huang@intel.com, kai.svahn@intel.com, kmoy@google.com,
ludloff@google.com, luto@kernel.org, nhorman@redhat.com,
npmccallum@redhat.com, puiterwijk@redhat.com,
rientjes@google.com, tglx@linutronix.de, yaozhangx@google.com,
mikko.ylinen@intel.com, Michal Hocko <mhocko@kernel.org>,
Vlastimil Babka <vbabka@suse.cz>
Subject: Re: [PATCH v40 10/24] mm: Add 'mprotect' hook to struct vm_operations_struct
Date: Fri, 6 Nov 2020 18:51:07 +0200 [thread overview]
Message-ID: <20201106165107.GA52595@kernel.org> (raw)
In-Reply-To: <20201106100409.GD3371@techsingularity.net>
On Fri, Nov 06, 2020 at 10:04:09AM +0000, Mel Gorman wrote:
> On Wed, Nov 04, 2020 at 04:54:16PM +0200, Jarkko Sakkinen wrote:
> > From: Sean Christopherson <sean.j.christopherson@intel.com>
> >
> > Background
> > ==========
> >
> > 1. SGX enclave pages are populated with data by copying from normal memory
> > via ioctl() (SGX_IOC_ENCLAVE_ADD_PAGES), which will be added later in
> > this series.
> > 2. It is desirable to be able to restrict those normal memory data sources.
> > For instance, to ensure that the source data 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().
> >
> > This creates a problem because the original data source may have long since
> > vanished at the time when enclave page permissions are established (mmap()
> > or mprotect()).
> >
> > The solution (elsewhere in this series) is to force enclaves creators to
> > declare their paging permission *intent* up front to the ioctl(). This
> > intent can me immediately compared to the source data???s mapping and
> > rejected if necessary.
> >
> > The ???intent??? is also stashed off for later comparison with enclave
> > PTEs. This ensures that any future mmap()/mprotect() operations
> > performed by the enclave creator or done on behalf of the enclave
> > can be compared with the earlier declared permissions.
> >
> > Problem
> > =======
> >
> > There is an existing mmap() hook which allows SGX to perform this
> > permission comparison at mmap() time. However, there is no corresponding
> > ->mprotect() hook.
> >
> > Solution
> > ========
> >
> > Add a vm_ops->mprotect() hook so that mprotect() operations which are
> > inconsistent with any page's stashed intent can be rejected by the driver.
> >
>
> I have not read the series so this is superficial only. That said...
>
> > Cc: linux-mm@kvack.org
> > Cc: Andrew Morton <akpm@linux-foundation.org>
> > Cc: Matthew Wilcox <willy@infradead.org>
> > Acked-by: Jethro Beekman <jethro@fortanix.com>
> > Reviewed-by: Darren Kenny <darren.kenny@oracle.com>
> > Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com>
> > Co-developed-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
> > Signed-off-by: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>
> > ---
> > include/linux/mm.h | 3 +++
> > mm/mprotect.c | 5 ++++-
> > 2 files changed, 7 insertions(+), 1 deletion(-)
> >
> > diff --git a/include/linux/mm.h b/include/linux/mm.h
> > index ef360fe70aaf..eb38eabc5039 100644
> > --- a/include/linux/mm.h
> > +++ b/include/linux/mm.h
> > @@ -559,6 +559,9 @@ struct vm_operations_struct {
> > void (*close)(struct vm_area_struct * area);
> > int (*split)(struct vm_area_struct * area, unsigned long addr);
> > int (*mremap)(struct vm_area_struct * area);
> > + int (*mprotect)(struct vm_area_struct *vma,
> > + struct vm_area_struct **pprev, unsigned long start,
> > + unsigned long end, unsigned long newflags);
>
> The first user of this uses the following information
>
> ret = sgx_encl_may_map(vma->vm_private_data, start, end, newflags);
>
> It only needs start, end and newflags. The pprev is passed in so the
> hook can call mprotect_fixup() which is redundant as the caller knows it
> should do that. I don't think an arbitrary driver should be responsible
> for poking too much into the mm internals to do the fixup because we do
> not know what other users of this hook might require in the future.
>
> Hence, I would suggest that the hook receive the minimum possible
> information to do the permissions check for the first in-tree user. If
> it returns without failure then mm/mprotect.c would always do the fixup.
>
> > vm_fault_t (*fault)(struct vm_fault *vmf);
> > vm_fault_t (*huge_fault)(struct vm_fault *vmf,
> > enum page_entry_size pe_size);
> > diff --git a/mm/mprotect.c b/mm/mprotect.c
> > index 56c02beb6041..1fd4fa71ce16 100644
> > --- a/mm/mprotect.c
> > +++ b/mm/mprotect.c
> > @@ -616,7 +616,10 @@ static int do_mprotect_pkey(unsigned long start, size_t len,
> > tmp = vma->vm_end;
> > if (tmp > end)
> > tmp = end;
> > - error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
> > + if (vma->vm_ops && vma->vm_ops->mprotect)
> > + error = vma->vm_ops->mprotect(vma, &prev, nstart, tmp, newflags);
> > + else
> > + error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
>
> That would then become
>
> if (vma->vm_ops && vma->vm_ops->mprotect)
> error = vma->vm_ops->mprotect(vma, &prev, nstart, tmp, newflags);
> if (!error)
> error = mprotect_fixup(vma, &prev, nstart, tmp, newflags);
>
> and mprotect_fixup would be removed from the driver.
>
> While vm_operations_struct has borderline zero documentation, a hook for
> one in-kernel user should have a comment explaining what the semantics
> of the hook is -- what is it responsible for (permission check), what
> can it change (nothing), etc. Maybe something like
>
> /*
> * Called by mprotect in the event driver-specific permission
> * checks need to be made before the mprotect is finalised.
> * No modifications should be done to the VMA, returns 0
> * if the mprotect is permitted.
> */
> int (*mprotect)(struct vm_area_struct *vma,
> unsigned long start, unsigned long end,
> unsigned long newflags);
>
> If a future driver *does* need to poke deeper into the VM for mprotect
> then at least they'll have to explain why that's a good idea.
Both comments make sense to me. I'll refine this patch on Monday and
also "x86/sgx: Add SGX misc driver interface", which uses this callback.
Thanks a lot for valuable feedback!
> --
> Mel Gorman
> SUSE Labs
/Jarkko
next prev parent reply other threads:[~2020-11-06 16:51 UTC|newest]
Thread overview: 73+ messages / expand[flat|nested] mbox.gz Atom feed top
2020-11-04 14:54 [PATCH v40 00/24] Intel SGX foundations Jarkko Sakkinen
2020-11-04 14:54 ` [PATCH v40 01/24] x86/sgx: Add SGX architectural data structures Jarkko Sakkinen
2020-11-04 14:54 ` [PATCH v40 02/24] x86/sgx: Add wrappers for ENCLS functions Jarkko Sakkinen
2020-11-04 14:54 ` [PATCH v40 03/24] x86/sgx: Initialize metadata for Enclave Page Cache (EPC) sections Jarkko Sakkinen
2020-11-04 18:21 ` Borislav Petkov
2020-11-04 19:04 ` Jarkko Sakkinen
2020-11-04 19:09 ` Jarkko Sakkinen
2020-11-04 19:12 ` Borislav Petkov
2020-11-04 14:54 ` [PATCH v40 04/24] x86/cpufeatures: x86/msr: Add Intel SGX hardware bits Jarkko Sakkinen
2020-11-04 14:54 ` [PATCH v40 05/24] x86/cpufeatures: x86/msr: Add Intel SGX Launch Control " Jarkko Sakkinen
2020-11-04 14:54 ` [PATCH v40 06/24] x86/mm: x86/sgx: Signal SIGSEGV with PF_SGX Jarkko Sakkinen
2020-11-04 14:54 ` [PATCH v40 07/24] x86/cpu/intel: Detect SGX support Jarkko Sakkinen
2020-11-04 14:54 ` [PATCH v40 08/24] x86/cpu/intel: Add nosgx kernel parameter Jarkko Sakkinen
2020-11-04 14:54 ` [PATCH v40 09/24] x86/sgx: Add SGX page allocator functions Jarkko Sakkinen
2020-11-05 15:08 ` Borislav Petkov
2020-11-04 14:54 ` [PATCH v40 10/24] mm: Add 'mprotect' hook to struct vm_operations_struct Jarkko Sakkinen
2020-11-05 16:04 ` Borislav Petkov
2020-11-05 17:33 ` Dave Hansen
2020-11-06 10:04 ` Mel Gorman
2020-11-06 16:51 ` Jarkko Sakkinen [this message]
2020-11-06 20:37 ` Borislav Petkov
2020-11-06 22:04 ` Jarkko Sakkinen
2020-11-06 22:31 ` Borislav Petkov
2020-11-06 17:43 ` Dr. Greg
2020-11-06 17:54 ` Dave Hansen
2020-11-07 15:09 ` Dr. Greg
2020-11-07 19:16 ` Dave Hansen
2020-11-12 20:58 ` Dr. Greg
2020-11-12 21:31 ` Dave Hansen
2020-11-12 22:41 ` Andy Lutomirski
2020-11-16 18:00 ` Dr. Greg
2020-11-19 1:39 ` Haitao Huang
2020-11-20 17:31 ` Dr. Greg
2020-11-15 18:59 ` Dr. Greg
2020-11-06 21:13 ` Matthew Wilcox
2020-11-06 21:23 ` Dave Hansen
2020-11-07 15:27 ` Dr. Greg
2020-11-04 14:54 ` [PATCH v40 11/24] x86/sgx: Add SGX misc driver interface Jarkko Sakkinen
2020-11-05 1:10 ` Jarkko Sakkinen
2020-11-05 1:16 ` Jarkko Sakkinen
2020-11-05 16:05 ` Borislav Petkov
2020-11-05 17:57 ` Jarkko Sakkinen
2020-11-05 18:10 ` Borislav Petkov
2020-11-06 16:07 ` Jarkko Sakkinen
2020-11-06 17:09 ` Borislav Petkov
2020-11-06 22:01 ` Jarkko Sakkinen
2020-11-04 14:54 ` [PATCH v40 12/24] x86/sgx: Add SGX_IOC_ENCLAVE_CREATE Jarkko Sakkinen
2020-11-04 14:54 ` [PATCH v40 13/24] x86/sgx: Add SGX_IOC_ENCLAVE_ADD_PAGES Jarkko Sakkinen
2020-11-04 14:54 ` [PATCH v40 14/24] x86/sgx: Add SGX_IOC_ENCLAVE_INIT Jarkko Sakkinen
2020-11-04 14:54 ` [PATCH v40 15/24] x86/sgx: Add SGX_IOC_ENCLAVE_PROVISION Jarkko Sakkinen
2020-11-04 14:54 ` [PATCH v40 16/24] x86/vdso: Add support for exception fixup in vDSO functions Jarkko Sakkinen
2020-11-04 14:54 ` [PATCH v40 17/24] x86/fault: Add helper function to sanitize error code Jarkko Sakkinen
2020-11-04 14:54 ` [PATCH v40 18/24] x86/traps: Attempt to fixup exceptions in vDSO before signaling Jarkko Sakkinen
2020-11-04 14:54 ` [PATCH v40 19/24] x86/vdso: Implement a vDSO for Intel SGX enclave call Jarkko Sakkinen
2020-11-08 18:24 ` Jethro Beekman
2020-11-08 20:08 ` Jethro Beekman
2020-11-08 20:26 ` Dave Hansen
2020-11-08 20:20 ` Jethro Beekman
2020-11-08 20:30 ` Dave Hansen
2020-11-04 14:54 ` [PATCH v40 20/24] selftests/x86: Add a selftest for SGX Jarkko Sakkinen
2020-11-04 14:54 ` [PATCH v40 21/24] x86/sgx: Add a page reclaimer Jarkko Sakkinen
2020-11-04 14:54 ` [PATCH v40 22/24] x86/sgx: Add ptrace() support for the SGX driver Jarkko Sakkinen
2020-11-04 14:54 ` [PATCH v40 23/24] docs: x86/sgx: Document SGX kernel architecture Jarkko Sakkinen
2020-11-04 14:54 ` [PATCH v40 24/24] x86/sgx: Update MAINTAINERS Jarkko Sakkinen
2020-11-08 20:48 ` [PATCH v40 00/24] Intel SGX foundations Jethro Beekman
[not found] ` <20201108035630.11540-1-hdanton@sina.com>
2020-11-09 19:59 ` [PATCH v40 21/24] x86/sgx: Add a page reclaimer Jarkko Sakkinen
2020-11-21 15:12 ` [PATCH v40 00/24] Intel SGX foundations Dr. Greg
2020-11-21 16:25 ` Dave Hansen
2020-11-24 10:55 ` Dr. Greg
2020-11-24 17:49 ` Andy Lutomirski
2020-11-21 18:36 ` Andy Lutomirski
2020-11-24 18:40 ` Dr. Greg
2020-11-24 21:57 ` Andy Lutomirski
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=20201106165107.GA52595@kernel.org \
--to=jarkko@kernel.org \
--cc=akpm@linux-foundation.org \
--cc=andriy.shevchenko@linux.intel.com \
--cc=asapek@google.com \
--cc=bp@alien8.de \
--cc=cedric.xing@intel.com \
--cc=chenalexchen@google.com \
--cc=conradparker@google.com \
--cc=cyhanish@google.com \
--cc=darren.kenny@oracle.com \
--cc=dave.hansen@intel.com \
--cc=haitao.huang@intel.com \
--cc=jarkko.sakkinen@linux.intel.com \
--cc=jethro@fortanix.com \
--cc=kai.huang@intel.com \
--cc=kai.svahn@intel.com \
--cc=kmoy@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-mm@kvack.org \
--cc=linux-sgx@vger.kernel.org \
--cc=ludloff@google.com \
--cc=luto@kernel.org \
--cc=mgorman@techsingularity.net \
--cc=mhocko@kernel.org \
--cc=mikko.ylinen@intel.com \
--cc=nhorman@redhat.com \
--cc=npmccallum@redhat.com \
--cc=puiterwijk@redhat.com \
--cc=rientjes@google.com \
--cc=sean.j.christopherson@intel.com \
--cc=tglx@linutronix.de \
--cc=vbabka@suse.cz \
--cc=willy@infradead.org \
--cc=x86@kernel.org \
--cc=yaozhangx@google.com \
/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;
as well as URLs for NNTP newsgroup(s).