selinux.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sean Christopherson <sean.j.christopherson@intel.com>
To: Andy Lutomirski <luto@kernel.org>
Cc: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>,
	Stephen Smalley <sds@tycho.nsa.gov>,
	James Morris <jmorris@namei.org>,
	"Serge E. Hallyn" <serge@hallyn.com>,
	LSM List <linux-security-module@vger.kernel.org>,
	Paul Moore <paul@paul-moore.com>,
	Eric Paris <eparis@parisplace.org>,
	selinux@vger.kernel.org, Jethro Beekman <jethro@fortanix.com>,
	"Xing, Cedric" <cedric.xing@intel.com>,
	"Hansen, Dave" <dave.hansen@intel.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	"Dr. Greg" <greg@enjellic.com>,
	Linus Torvalds <torvalds@linux-foundation.org>,
	LKML <linux-kernel@vger.kernel.org>, X86 ML <x86@kernel.org>,
	"linux-sgx@vger.kernel.org" <linux-sgx@vger.kernel.org>,
	Andrew Morton <akpm@linux-foundation.org>,
	"nhorman@redhat.com" <nhorman@redhat.com>,
	"npmccallum@redhat.com" <npmccallum@redhat.com>,
	"Ayoun, Serge" <serge.ayoun@intel.com>,
	"Katz-zamir, Shay" <shay.katz-zamir@intel.com>,
	"Huang, Haitao" <haitao.huang@intel.com>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	"Svahn, Kai" <kai.svahn@intel.com>,
	Borislav Petkov <bp@alien8.de>,
	Josh Triplett <josh@joshtriplett.org>,
	"Huang, Kai" <kai.huang@intel.com>,
	David Rientjes <rientjes@google.com>
Subject: Re: SGX vs LSM (Re: [PATCH v20 00/28] Intel SGX1 support)
Date: Thu, 23 May 2019 16:40:44 -0700	[thread overview]
Message-ID: <20190523234044.GC12078@linux.intel.com> (raw)
In-Reply-To: <CALCETrUzx3LPAKCLFf75P-XshAkRcr+JLET3LA_kHDs9MA11FA@mail.gmail.com>

On Thu, May 23, 2019 at 08:38:17AM -0700, Andy Lutomirski wrote:
> On Thu, May 23, 2019 at 7:17 AM Sean Christopherson
> <sean.j.christopherson@intel.com> wrote:
> >
> > On Thu, May 23, 2019 at 01:26:28PM +0300, Jarkko Sakkinen wrote:
> > > On Wed, May 22, 2019 at 07:35:17PM -0700, Sean Christopherson wrote:
> > > > But actually, there's no need to disallow mmap() after ECREATE since the
> > > > LSM checks also apply to mmap(), e.g. FILE__EXECUTE would be needed to
> > > > mmap() any enclave pages PROT_EXEC.  I guess my past self thought mmap()
> > > > bypassed LSM checks?  The real problem is that mmap()'ng an existing
> > > > enclave would require FILE__WRITE and FILE__EXECUTE, which puts us back
> > > > at square one.
> > >
> > > I'm lost with the constraints we want to set.
> >
> > As is today, SELinux policies would require enclave loaders to have
> > FILE__WRITE and FILE__EXECUTE permissions on /dev/sgx/enclave.  Presumably
> > other LSMs have similar requirements.  Requiring all processes to have
> > FILE__{WRITE,EXECUTE} permissions means the permissions don't add much
> > value, e.g. they can't be used to distinguish between an enclave that is
> > being loaded from an unmodified file and an enclave that is being
> > generated on the fly, e.g. Graphene.
> >
> > Looking back at Andy's mail, he was talking about requiring FILE__EXECUTE
> > to run an enclave, so perhaps it's only FILE__WRITE that we're trying to
> > special case.
> >
> 
> I thought about this some more, and I have a new proposal that helps
> address the ELRANGE alignment issue and the permission issue at the
> cost of some extra verbosity.  Maybe you all can poke holes in it :)
> The basic idea is to make everything more explicit from a user's
> perspective.  Here's how it works:
> 
> Opening /dev/sgx/enclave gives an enclave_fd that, by design, doesn't
> give EXECUTE or WRITE.  mmap() on the enclave_fd only works if you
> pass PROT_NONE and gives the correct alignment.  The resulting VMA
> cannot be mprotected or mremapped.  It can't be mmapped at all until

I assume you're thinking of clearing all VM_MAY* flags in sgx_mmap()?

> after ECREATE because the alignment isn't known before that.

I don't follow.  The alignment is known because userspace knows the size
of its enclave.  The initial unknown is the address, but that becomes
known once the initial mmap() completes.

> Associated with the enclave are a bunch (up to 7) "enclave segment

I assume 7 = R, W, X, RW, RX, WX and RWX?

> inodes".  These are anon_inodes that are created automagically.  An
> enclave segment is a group of pages, not necessary contiguous, with an
> upper bound on the memory permissions.  Each enclave page belongs to a
> segment.  When you do EADD, you tell the driver what segment you're
> adding to. [0]  This means that EADD gets an extra argument that is a
> permission mask for the page -- in addition to the initial SECINFO,
> you also pass to EADD something to the effect of "I promise never to
> map this with permissions greater than RX".
>
> Then we just need some way to mmap a region from an enclave segment.
> This could be done by having a way to get an fd for an enclave segment
> or it could be done by having a new ioctl SGX_IOC_MAP_SEGMENT.  User
> code would use this operation to replace, MAP_FIXED-style, ranges from
> the big PROT_NONE mapping with the relevant pages from the enclave
> segment.  The resulting vma would only have VM_MAYWRITE if the segment
> is W, only have VM_MAYEXEC if the segment is X, and only have
> VM_MAYREAD if the segment is R.  Depending on implementation details,
> the VMAs might need to restrict mremap() to avoid mapping pages that
> aren't part of the segment in question.

If my above assumptions regarding VM_MAY* and the "7 segments" are
correct, IIUC you're proposing that an LSM could have policies for each
of the anon inodes, e.g. grant/deny RWX vs. RW vs RX.  Am I in the
ballpark?

> It's plausible that this whole thing works without the magic segment
> inodes under the hood, but figuring that out would need a careful look
> at how all the core mm bits and LSM bits work together.
>
> To get all the LSM stuff to work, SELinux will need some way to
> automatically assign an appropriate label to the segment inodes.  I
> assume that such a mechanism already exists and gets used for things
> like sockets, but I haven't actually confirmed this.

I (obviously) don't fully understand your proposal, but I don't think we
want to hook inodes, e.g. AppArmor doesn't implement inode_permission()
but does implement file_mprotect() and mmap_file(), which feel like the
natural hooks for this sort of thing.  I also think it's overkill, e.g.
AppArmor doesn't have a concept of EXECMOD, EXECMEM, EXECHEAP, etc.., so
I don't think we need to go beyond detecting W+X scenarios.

Starting with your original idea of tracking "safe to execute" and
Cedric's of propagating the permissions from the source VMA, but tweaked
with your new idea of clearing VM_MAY* and a custom MAP_FIXED/mprotect().

Add SGX_IOC_MPROTECT (or SGX_IOC_MAP_REGION?) that works as follows:

  1. Track VM_MAY{READ,WRITE,EXEC} flags for each enclave page.
  2. SGX_IOC_ADD_REGION, i.e. EADD, initializes the VM_MAY* flags for each
     enclave page based on the source VMA.
  3. sgx_mmap() only works with PROT_NONE, skips alignment stuff if
     MAP_FIXED, and clears VM_MAY{READ,WRITE,EXEC}.
  4. mprotect() on /dev/sgx/enclave doesn't work because the VMA doesn't
     have any VM_MAY{READ,WRITE,EXEC} capabilities.
  5. Deny mremap() post-ECREATE as the address and size of the enclave
     are fixed at ECREATE (in hardware).
  6. SGX_IOC_MPROTECT works like normal mprotect(), except the VM_MAY*
     flags are pulled from the enclave pages, and its call to
     security_file_mprotect() is VM_READ|VM_EXEC by default.  The LSM call
     sets VM_WRITE iff the enclave page has both VM_MAYWRITE and
     VM_MAYEXEC.  The idea here is to require READ and EXECUTE to run an
     enclave, and only require WRITE on /dev/sgx/enclave when the enclave
     can execute modified memory.

To support SGX2 down the road, which will want to convert a page to
executable on the fly, we could add:

  7. SGX_IOC_EXTEND_PERMISSIONS enables userspace to extend the VM_MAY*
     flags for an enclave page, e.g. to make a page executable.
     SGX_IOC_MPROTECT is still required to actually map the page.
     Notably, adding a RW page to the enclave, e.g. to grow its heap,
     doesn't require WRITE, whereas adding a RWX page, e.g. for dynamic
     loading, would require WRITE.  This can only extend!  E.g. userspace
     can't circumvent the WRITE requirement by clearing VM_MAYWRITE.

Note, FILE__WRITE on /dev/sgx/enclave is essentially equivalent to
FILE__EXECMOD.  Using FILE__WRITE in this way means there are no changes
to SELinux (triggering FILE__EXECMOD would be awkward), and AppArmor also
picks up extra protections for enclaves.

> [0] There needs to be some vaguely intelligent semantics if you EADD
> the *same* address more than once.  A simple solution would be to
> disallow it if the segments don't match.

I don't see any reason to allow duplicate EADD as it serves no purpose,
e.g. doing so changes the enclave's measurement and that's it.

  reply	other threads:[~2019-05-23 23:40 UTC|newest]

Thread overview: 127+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <8fe520bb-30bd-f246-a3d8-c5443e47a014@intel.com>
     [not found] ` <358e9b36-230f-eb18-efdb-b472be8438b4@fortanix.com>
     [not found]   ` <960B34DE67B9E140824F1DCDEC400C0F4E886094@ORSMSX116.amr.corp.intel.com>
     [not found]     ` <6da269d8-7ebb-4177-b6a7-50cc5b435cf4@fortanix.com>
     [not found]       ` <CALCETrWCZQwg-TUCm58DVG43=xCKRsMe1tVHrR8vdt06hf4fWA@mail.gmail.com>
     [not found]         ` <20190513102926.GD8743@linux.intel.com>
     [not found]           ` <20190514104323.GA7591@linux.intel.com>
     [not found]             ` <CALCETrVbgTCnPo=PAq0-KoaRwt--urrPzn==quAJ8wodCpkBkw@mail.gmail.com>
     [not found]               ` <20190514204527.GC1977@linux.intel.com>
     [not found]                 ` <CALCETrX6aL367mMJh5+Y1Seznfu-AvhPV6P7GkWF4Dhu0GV8cw@mail.gmail.com>
     [not found]                   ` <20190515013031.GF1977@linux.intel.com>
2019-05-15 18:27                     ` SGX vs LSM (Re: [PATCH v20 00/28] Intel SGX1 support) Andy Lutomirski
2019-05-15 19:58                       ` James Morris
2019-05-15 20:35                         ` Andy Lutomirski
2019-05-15 22:46                           ` James Morris
2019-05-15 23:13                             ` Andy Lutomirski
2019-05-16  3:03                               ` Xing, Cedric
2019-05-16  4:40                                 ` Andy Lutomirski
2019-05-16 22:23                                   ` Xing, Cedric
2019-05-17  0:35                                     ` Andy Lutomirski
2019-05-17  1:06                                       ` Xing, Cedric
2019-05-17  1:21                                         ` Andy Lutomirski
2019-05-17 16:05                                       ` Sean Christopherson
2019-05-17 13:53                                     ` Stephen Smalley
2019-05-17 15:09                                       ` Sean Christopherson
2019-05-17 16:20                                         ` Stephen Smalley
2019-05-17 16:24                                           ` Andy Lutomirski
2019-05-17 16:37                                           ` Stephen Smalley
2019-05-17 17:12                                             ` Andy Lutomirski
2019-05-17 18:05                                               ` Stephen Smalley
2019-05-17 19:20                                                 ` Stephen Smalley
2019-05-17 19:28                                                 ` Sean Christopherson
2019-05-17 20:09                                                   ` Stephen Smalley
2019-05-17 20:14                                                     ` Andy Lutomirski
2019-05-17 20:34                                                       ` Stephen Smalley
2019-05-17 21:36                                                     ` Sean Christopherson
2019-05-17 17:29                                             ` Sean Christopherson
2019-05-17 17:42                                               ` Stephen Smalley
2019-05-17 17:50                                                 ` Sean Christopherson
2019-05-17 18:16                                                   ` Stephen Smalley
2019-05-17 17:43                                               ` Andy Lutomirski
2019-05-17 17:55                                                 ` Sean Christopherson
2019-05-17 18:04                                                   ` Linus Torvalds
2019-05-17 18:21                                                     ` Sean Christopherson
2019-05-17 18:33                                                       ` Linus Torvalds
2019-05-17 18:52                                                         ` Sean Christopherson
2019-05-17 18:53                                                       ` Andy Lutomirski
2019-05-16  7:24                               ` James Morris
2019-05-16 21:00                                 ` Andy Lutomirski
2019-05-20  9:38                                 ` Dr. Greg
2019-05-15 21:38                       ` Sean Christopherson
2019-05-16  1:19                         ` Haitao Huang
2019-05-16  5:16                       ` Jarkko Sakkinen
2019-05-16 21:02                         ` Andy Lutomirski
2019-05-16 22:45                           ` Sean Christopherson
2019-05-16 23:29                             ` Xing, Cedric
2019-05-20 11:29                             ` Jarkko Sakkinen
2019-05-20 11:33                           ` Jarkko Sakkinen
2019-05-17  0:03                       ` Sean Christopherson
2019-05-17  0:26                         ` Andy Lutomirski
2019-05-17 15:41                           ` Sean Christopherson
2019-05-20 11:42                             ` Jarkko Sakkinen
2019-05-20 11:41                           ` Jarkko Sakkinen
2019-05-21 15:19                             ` Jarkko Sakkinen
2019-05-21 15:24                               ` Jethro Beekman
2019-05-22 13:10                                 ` Jarkko Sakkinen
2019-05-21 15:51                               ` Sean Christopherson
2019-05-22 13:20                                 ` Jarkko Sakkinen
2019-05-22 13:22                                   ` Jarkko Sakkinen
2019-05-22 13:56                                     ` Stephen Smalley
2019-05-22 15:38                                       ` Sean Christopherson
2019-05-22 22:42                                         ` Andy Lutomirski
2019-05-23  2:35                                           ` Sean Christopherson
2019-05-23 10:26                                             ` Jarkko Sakkinen
2019-05-23 14:17                                               ` Sean Christopherson
2019-05-23 15:38                                                 ` Andy Lutomirski
2019-05-23 23:40                                                   ` Sean Christopherson [this message]
2019-05-24  1:17                                                     ` Andy Lutomirski
2019-05-24  7:24                                                       ` Xing, Cedric
2019-05-24 15:41                                                         ` Stephen Smalley
2019-05-24 16:57                                                           ` Xing, Cedric
2019-05-24 17:42                                                           ` Sean Christopherson
2019-05-24 17:54                                                             ` Andy Lutomirski
2019-05-24 17:56                                                               ` Sean Christopherson
2019-05-24 17:54                                                             ` Sean Christopherson
2019-05-24 18:34                                                               ` Xing, Cedric
2019-05-24 19:13                                                                 ` Sean Christopherson
2019-05-24 19:30                                                                   ` Andy Lutomirski
2019-05-24 20:42                                                                   ` Xing, Cedric
2019-05-24 21:11                                                                     ` Sean Christopherson
2019-05-24 19:37                                                                 ` Andy Lutomirski
2019-05-24 20:03                                                                   ` Sean Christopherson
2019-05-24 20:58                                                                     ` Xing, Cedric
2019-05-24 21:27                                                                     ` Andy Lutomirski
2019-05-24 22:41                                                                       ` Sean Christopherson
2019-05-24 23:42                                                                         ` Andy Lutomirski
2019-05-25 22:40                                                                           ` Xing, Cedric
2019-05-26  0:57                                                                             ` Andy Lutomirski
2019-05-26  6:09                                                                               ` Xing, Cedric
2019-05-28 20:24                                                                                 ` Sean Christopherson
2019-05-28 20:48                                                                                   ` Andy Lutomirski
2019-05-28 21:41                                                                                     ` Sean Christopherson
2019-05-30  5:38                                                                                       ` Xing, Cedric
2019-05-30 17:21                                                                                         ` Sean Christopherson
2019-05-29 14:08                                                                                   ` Stephen Smalley
2019-05-30  6:12                                                                                     ` Xing, Cedric
2019-05-30 14:22                                                                                       ` Stephen Smalley
2019-05-30 14:31                                                                                         ` Andy Lutomirski
2019-05-30 15:04                                                                                           ` Stephen Smalley
2019-05-30 16:14                                                                                             ` Andy Lutomirski
2019-05-30 18:01                                                                                               ` Sean Christopherson
2019-05-30 19:20                                                                                                 ` Andy Lutomirski
2019-05-30 21:16                                                                                                   ` Sean Christopherson
2019-05-30 21:23                                                                                                     ` Andy Lutomirski
2019-05-30 21:36                                                                                                       ` Sean Christopherson
2019-06-03  9:12                                                                                                         ` Dr. Greg
2019-06-03 21:08                                                                                                         ` Jarkko Sakkinen
2019-05-30 21:48                                                                                                   ` Xing, Cedric
2019-05-30 22:24                                                                                                     ` Sean Christopherson
2019-06-03 21:05                                                                                                 ` Jarkko Sakkinen
2019-06-03 20:54                                                                                               ` Jarkko Sakkinen
2019-06-03 21:23                                                                                                 ` Sean Christopherson
2019-06-04 11:39                                                                                                   ` Jarkko Sakkinen
2019-06-03 21:37                                                                                                 ` Andy Lutomirski
2019-06-03 20:47                                                                                             ` Jarkko Sakkinen
2019-06-03 20:43                                                                                           ` Jarkko Sakkinen
2019-05-25 17:31                                                                     ` Dr. Greg
2019-05-24 16:43                                                         ` Andy Lutomirski
2019-05-24 17:07                                                           ` Sean Christopherson
2019-05-24 17:51                                                             ` Andy Lutomirski
2019-05-24 14:44                                                   ` Stephen Smalley
2019-05-27 13:48                                                   ` Jarkko Sakkinen
2019-05-23 19:58                                                 ` Sean Christopherson
2019-05-27 13:34                                                 ` Jarkko Sakkinen
2019-05-27 13:38                                                   ` Jarkko Sakkinen
2019-05-23  8:10                                           ` Jarkko Sakkinen
2019-05-23  8:23                                             ` Jarkko Sakkinen
2019-05-20 11:36                         ` Jarkko Sakkinen

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=20190523234044.GC12078@linux.intel.com \
    --to=sean.j.christopherson@intel.com \
    --cc=akpm@linux-foundation.org \
    --cc=andriy.shevchenko@linux.intel.com \
    --cc=bp@alien8.de \
    --cc=cedric.xing@intel.com \
    --cc=dave.hansen@intel.com \
    --cc=eparis@parisplace.org \
    --cc=greg@enjellic.com \
    --cc=haitao.huang@intel.com \
    --cc=jarkko.sakkinen@linux.intel.com \
    --cc=jethro@fortanix.com \
    --cc=jmorris@namei.org \
    --cc=josh@joshtriplett.org \
    --cc=kai.huang@intel.com \
    --cc=kai.svahn@intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-security-module@vger.kernel.org \
    --cc=linux-sgx@vger.kernel.org \
    --cc=luto@kernel.org \
    --cc=nhorman@redhat.com \
    --cc=npmccallum@redhat.com \
    --cc=paul@paul-moore.com \
    --cc=rientjes@google.com \
    --cc=sds@tycho.nsa.gov \
    --cc=selinux@vger.kernel.org \
    --cc=serge.ayoun@intel.com \
    --cc=serge@hallyn.com \
    --cc=shay.katz-zamir@intel.com \
    --cc=tglx@linutronix.de \
    --cc=torvalds@linux-foundation.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;
as well as URLs for NNTP newsgroup(s).