linux-kselftest.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sean Christopherson <sean.j.christopherson@intel.com>
To: Nathaniel McCallum <npmccallum@redhat.com>
Cc: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>,
	X86 ML <x86@kernel.org>,
	linux-sgx@vger.kernel.org, LKML <linux-kernel@vger.kernel.org>,
	linux-kselftest@vger.kernel.org,
	Andrew Morton <akpm@linux-foundation.org>,
	Andy Shevchenko <andriy.shevchenko@linux.intel.com>,
	asapek@google.com, Borislav Petkov <bp@alien8.de>,
	"Xing, Cedric" <cedric.xing@intel.com>,
	chenalexchen@google.com, Conrad Parker <conradparker@google.com>,
	cyhanish@google.com, Dave Hansen <dave.hansen@intel.com>,
	"Huang, Haitao" <haitao.huang@intel.com>,
	Josh Triplett <josh@joshtriplett.org>,
	"Huang, Kai" <kai.huang@intel.com>,
	"Svahn, Kai" <kai.svahn@intel.com>, Keith Moyer <kmoy@google.com>,
	Christian Ludloff <ludloff@google.com>,
	Andy Lutomirski <luto@kernel.org>,
	Neil Horman <nhorman@redhat.com>,
	Patrick Uiterwijk <puiterwijk@redhat.com>,
	David Rientjes <rientjes@google.com>,
	Thomas Gleixner <tglx@linutronix.de>,
	yaozhangx@google.com
Subject: Re: [PATCH v36 22/24] selftests/x86: Add a selftest for SGX
Date: Thu, 27 Aug 2020 08:20:51 -0700	[thread overview]
Message-ID: <20200827152051.GB22351@sjchrist-ice> (raw)
In-Reply-To: <CAOASepO-DuQW88hxtAJv5Ki4GNYnkGNz5qK_dTEK_y8roMdPkg@mail.gmail.com>

On Thu, Aug 27, 2020 at 12:47:04AM -0400, Nathaniel McCallum wrote:
> > +int main(int argc, char *argv[], char *envp[])
> > +{
> > +       struct sgx_enclave_exception exception;
> > +       struct vdso_symtab symtab;
> > +       Elf64_Sym *eenter_sym;
> > +       uint64_t result = 0;
> > +       struct encl encl;
> > +       unsigned int i;
> > +       void *addr;
> > +
> > +       if (!encl_load("test_encl.elf", &encl))
> > +               goto err;
> > +
> > +       if (!encl_measure(&encl))
> > +               goto err;
> > +
> > +       if (!encl_build(&encl))
> > +               goto err;
> > +
> > +       /*
> > +        * An enclave consumer only must do this.
> > +        */
> > +       for (i = 0; i < encl.nr_segments; i++) {
> > +               struct encl_segment *seg = &encl.segment_tbl[i];
> > +
> > +               addr = mmap((void *)encl.encl_base + seg->offset, seg->size,
> > +                           seg->prot, MAP_SHARED | MAP_FIXED, encl.fd, 0);
> 
> My patch version is a bit behind (v32), but I suspect this still
> applies. I discovered the following by accident.
> 
> In the Enarx code base, this invocation succeeds:
> mmap(0x200000000000, 0x1000, PROT_READ | PROT_WRITE, MAP_SHARED |
> MAP_FIXED, sgxfd, 0)
> 
> However, this one fails with -EINVAL:
> mmap(0x200000000000, 0x1000, PROT_READ | PROT_WRITE,
> MAP_SHARED_VALIDATE | MAP_FIXED, sgxfd, 0)
> 
> From man mmap:
> 
>        MAP_SHARED_VALIDATE (since Linux 4.15)
>               This flag provides the same behavior as MAP_SHARED
> except that MAP_SHARED mappings ignore unknown
>               flags in flags.  By contrast, when creating a mapping
> using MAP_SHARED_VALIDATE, the kernel veri‐
>               fies  all  passed  flags  are  known  and fails the
> mapping with the error EOPNOTSUPP for unknown
>               flags.  This mapping type is also required to be able to
> use some mapping flags (e.g., MAP_SYNC).
> 
> I can try again on a newer patch set tomorrow if need be. But the
> documentation of mmap() doesn't match the behavior I'm seeing. A brief
> look through the patch set didn't turn up anything obvious that could
> be causing this.

This is a bug in sgx_get_unmapped_area().  EPC must be mapped SHARED, and
so MAP_PRIVATE is disallowed.  The current check is:

  if (flags & MAP_PRIVATE)
          return -EINVAL;

and the base "flags" are:

  #define MAP_SHARED      0x01            /* Share changes */
  #define MAP_PRIVATE     0x02            /* Changes are private */
  #define MAP_SHARED_VALIDATE 0x03        /* share + validate extension flags */

which causes the SGX check to interpret MAP_SHARED_VALIDATE as MAP_PRIVATE.
The types are just that, types, not flag modifiers.  So the SGX code needs
to be:

  if ((flags & MAP_TYPE) == MAP_PRIVATE)
          return -EINVAL;

or

  unsigned long map_type = (flags & MAP_TYPE);

  if (map_type != MAP_SHARED && map_type != MAP_SHARED_VALIDATE)
          return -EINVAL;


Side topic, there is at least one existing bug of this nature, in mm/nommu.c.
I'll send a patch for that and look for any other instances of the bad
pattern.

  reply	other threads:[~2020-08-27 15:21 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
     [not found] <20200716135303.276442-1-jarkko.sakkinen@linux.intel.com>
2020-07-16 13:53 ` [PATCH v36 22/24] selftests/x86: Add a selftest for SGX Jarkko Sakkinen
2020-08-27  4:47   ` Nathaniel McCallum
2020-08-27 15:20     ` Sean Christopherson [this message]
2020-08-28 23:27       ` 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=20200827152051.GB22351@sjchrist-ice \
    --to=sean.j.christopherson@intel.com \
    --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=dave.hansen@intel.com \
    --cc=haitao.huang@intel.com \
    --cc=jarkko.sakkinen@linux.intel.com \
    --cc=josh@joshtriplett.org \
    --cc=kai.huang@intel.com \
    --cc=kai.svahn@intel.com \
    --cc=kmoy@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=linux-sgx@vger.kernel.org \
    --cc=ludloff@google.com \
    --cc=luto@kernel.org \
    --cc=nhorman@redhat.com \
    --cc=npmccallum@redhat.com \
    --cc=puiterwijk@redhat.com \
    --cc=rientjes@google.com \
    --cc=tglx@linutronix.de \
    --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).