<feed xmlns='http://www.w3.org/2005/Atom'>
<title>kernel.git/arch/x86/kernel/cpu/sgx, branch linux-5.12.y</title>
<subtitle>Hosts the 0x221E linux distro kernel.
</subtitle>
<id>https://git.0xinfinity.dev/distro/kernel.git/atom?h=linux-5.12.y</id>
<link rel='self' href='https://git.0xinfinity.dev/distro/kernel.git/atom?h=linux-5.12.y'/>
<link rel='alternate' type='text/html' href='https://git.0xinfinity.dev/distro/kernel.git/'/>
<updated>2021-02-21T03:13:18Z</updated>
<entry>
<title>Merge tag 'x86_sgx_for_v5.12' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip</title>
<updated>2021-02-21T03:13:18Z</updated>
<author>
<name>Linus Torvalds</name>
</author>
<published>2021-02-21T03:13:18Z</published>
<link rel='alternate' type='text/html' href='https://git.0xinfinity.dev/distro/kernel.git/commit/?id=4bf0b820d146682d997248ff1d49665475f9df16'/>
<id>urn:sha1:4bf0b820d146682d997248ff1d49665475f9df16</id>
<content type='text'>
Pull x86 SGX fixes from Borislav Petkov:
 "Random small fixes which missed the initial SGX submission. Also, some
  procedural clarifications"

* tag 'x86_sgx_for_v5.12' of git://git.kernel.org/pub/scm/linux/kernel/git/tip/tip:
  MAINTAINERS: Add Dave Hansen as reviewer for INTEL SGX
  x86/sgx: Drop racy follow_pfn() check
  MAINTAINERS: Fix the tree location for INTEL SGX patches
  x86/sgx: Fix the return type of sgx_init()
</content>
</entry>
<entry>
<title>x86/sgx: Maintain encl-&gt;refcount for each encl-&gt;mm_list entry</title>
<updated>2021-02-08T18:11:30Z</updated>
<author>
<name>Jarkko Sakkinen</name>
</author>
<published>2021-02-07T22:14:01Z</published>
<link rel='alternate' type='text/html' href='https://git.0xinfinity.dev/distro/kernel.git/commit/?id=2ade0d60939bcd54197c133b03b460fe62a4ec47'/>
<id>urn:sha1:2ade0d60939bcd54197c133b03b460fe62a4ec47</id>
<content type='text'>
This has been shown in tests:

[  +0.000008] WARNING: CPU: 3 PID: 7620 at kernel/rcu/srcutree.c:374 cleanup_srcu_struct+0xed/0x100

This is essentially a use-after free, although SRCU notices it as
an SRCU cleanup in an invalid context.

== Background ==

SGX has a data structure (struct sgx_encl_mm) which keeps per-mm SGX
metadata.  This is separate from struct sgx_encl because, in theory,
an enclave can be mapped from more than one mm.  sgx_encl_mm includes
a pointer back to the sgx_encl.

This means that sgx_encl must have a longer lifetime than all of the
sgx_encl_mm's that point to it.  That's usually the case: sgx_encl_mm
is freed only after the mmu_notifier is unregistered in sgx_release().

However, there's a race.  If the process is exiting,
sgx_mmu_notifier_release() can be called in parallel with sgx_release()
instead of being called *by* it.  The mmu_notifier path keeps encl_mm
alive past when sgx_encl can be freed.  This inverts the lifetime rules
and means that sgx_mmu_notifier_release() can access a freed sgx_encl.

== Fix ==

Increase encl-&gt;refcount when encl_mm-&gt;encl is established. Release
this reference when encl_mm is freed. This ensures that encl outlives
encl_mm.

 [ bp: Massage commit message. ]

Fixes: 1728ab54b4be ("x86/sgx: Add a page reclaimer")
Reported-by: Haitao Huang &lt;haitao.huang@linux.intel.com&gt;
Signed-off-by: Jarkko Sakkinen &lt;jarkko@kernel.org&gt;
Signed-off-by: Borislav Petkov &lt;bp@suse.de&gt;
Acked-by: Dave Hansen &lt;dave.hansen@linux.intel.com&gt;
Link: https://lkml.kernel.org/r/20210207221401.29933-1-jarkko@kernel.org
</content>
</entry>
<entry>
<title>x86/sgx: Drop racy follow_pfn() check</title>
<updated>2021-02-05T09:45:11Z</updated>
<author>
<name>Daniel Vetter</name>
</author>
<published>2021-02-04T18:45:19Z</published>
<link rel='alternate' type='text/html' href='https://git.0xinfinity.dev/distro/kernel.git/commit/?id=dc9b7be557ca94301ea5c06c0d72307e642ffb18'/>
<id>urn:sha1:dc9b7be557ca94301ea5c06c0d72307e642ffb18</id>
<content type='text'>
PTE insertion is fundamentally racy, and this check doesn't do anything
useful. Quoting Sean:

  "Yeah, it can be whacked. The original, never-upstreamed code asserted
  that the resolved PFN matched the PFN being installed by the fault
  handler as a sanity check on the SGX driver's EPC management. The
  WARN assertion got dropped for whatever reason, leaving that useless
  chunk."

Jason stumbled over this as a new user of follow_pfn(), and I'm trying
to get rid of unsafe callers of that function so it can be locked down
further.

This is independent prep work for the referenced patch series:

  https://lore.kernel.org/dri-devel/20201127164131.2244124-1-daniel.vetter@ffwll.ch/

Fixes: 947c6e11fa43 ("x86/sgx: Add ptrace() support for the SGX driver")
Reported-by: Jason Gunthorpe &lt;jgg@ziepe.ca&gt;
Signed-off-by: Daniel Vetter &lt;daniel.vetter@intel.com&gt;
Signed-off-by: Borislav Petkov &lt;bp@suse.de&gt;
Reviewed-by: Jarkko Sakkinen &lt;jarkko@kernel.org&gt;
Link: https://lkml.kernel.org/r/20210204184519.2809313-1-daniel.vetter@ffwll.ch
</content>
</entry>
<entry>
<title>x86/sgx: Fix the return type of sgx_init()</title>
<updated>2021-01-21T13:04:06Z</updated>
<author>
<name>Sami Tolvanen</name>
</author>
<published>2021-01-13T23:23:11Z</published>
<link rel='alternate' type='text/html' href='https://git.0xinfinity.dev/distro/kernel.git/commit/?id=31bf92881714fe9962d43d097b5114a9b4ad0a12'/>
<id>urn:sha1:31bf92881714fe9962d43d097b5114a9b4ad0a12</id>
<content type='text'>
device_initcall() expects a function of type initcall_t, which returns
an integer. Change the signature of sgx_init() to match.

Fixes: e7e0545299d8c ("x86/sgx: Initialize metadata for Enclave Page Cache (EPC) sections")
Signed-off-by: Sami Tolvanen &lt;samitolvanen@google.com&gt;
Signed-off-by: Borislav Petkov &lt;bp@suse.de&gt;
Reviewed-by: Darren Kenny &lt;darren.kenny@oracle.com&gt;
Reviewed-by: Jarkko Sakkinen &lt;jarkko@kernel.org&gt;
Link: https://lkml.kernel.org/r/20210113232311.277302-1-samitolvanen@google.com
</content>
</entry>
<entry>
<title>x86/sgx: Return -EINVAL on a zero length buffer in sgx_ioc_enclave_add_pages()</title>
<updated>2020-12-03T18:54:40Z</updated>
<author>
<name>Jarkko Sakkinen</name>
</author>
<published>2020-12-03T18:35:27Z</published>
<link rel='alternate' type='text/html' href='https://git.0xinfinity.dev/distro/kernel.git/commit/?id=a4b9c48b96517ff4780b22a784e7537eac5dc21b'/>
<id>urn:sha1:a4b9c48b96517ff4780b22a784e7537eac5dc21b</id>
<content type='text'>
The sgx_enclave_add_pages.length field is documented as

 * @length:     length of the data (multiple of the page size)

Fail with -EINVAL, when the caller gives a zero length buffer of data
to be added as pages to an enclave. Right now 'ret' is returned as
uninitialized in that case.

 [ bp: Flesh out commit message. ]

Fixes: c6d26d370767 ("x86/sgx: Add SGX_IOC_ENCLAVE_ADD_PAGES")
Reported-by: Dan Carpenter &lt;dan.carpenter@oracle.com&gt;
Signed-off-by: Jarkko Sakkinen &lt;jarkko@kernel.org&gt;
Signed-off-by: Borislav Petkov &lt;bp@suse.de&gt;
Link: https://lore.kernel.org/linux-sgx/X8ehQssnslm194ld@mwanda/
Link: https://lkml.kernel.org/r/20201203183527.139317-1-jarkko@kernel.org
</content>
</entry>
<entry>
<title>x86/sgx: Fix sgx_ioc_enclave_provision() kernel-doc comment</title>
<updated>2020-11-24T09:46:01Z</updated>
<author>
<name>Borislav Petkov</name>
</author>
<published>2020-11-23T10:11:17Z</published>
<link rel='alternate' type='text/html' href='https://git.0xinfinity.dev/distro/kernel.git/commit/?id=afe76eca862ccde2a0c30105fc97a46a0b59339b'/>
<id>urn:sha1:afe76eca862ccde2a0c30105fc97a46a0b59339b</id>
<content type='text'>
Fix

  ./arch/x86/kernel/cpu/sgx/ioctl.c:666: warning: Function parameter or member \
	  'encl' not described in 'sgx_ioc_enclave_provision'
  ./arch/x86/kernel/cpu/sgx/ioctl.c:666: warning: Excess function parameter \
	  'enclave' description in 'sgx_ioc_enclave_provision'

Reported-by: Stephen Rothwell &lt;sfr@canb.auug.org.au&gt;
Signed-off-by: Borislav Petkov &lt;bp@suse.de&gt;
Link: https://lkml.kernel.org/r/20201123181922.0c009406@canb.auug.org.au
</content>
</entry>
<entry>
<title>x86/sgx: Return -ERESTARTSYS in sgx_ioc_enclave_add_pages()</title>
<updated>2020-11-19T09:51:24Z</updated>
<author>
<name>Jarkko Sakkinen</name>
</author>
<published>2020-11-18T21:39:32Z</published>
<link rel='alternate' type='text/html' href='https://git.0xinfinity.dev/distro/kernel.git/commit/?id=14132a5b807bb5caf778fe7ae1597e630971e949'/>
<id>urn:sha1:14132a5b807bb5caf778fe7ae1597e630971e949</id>
<content type='text'>
Return -ERESTARTSYS instead of -EINTR in sgx_ioc_enclave_add_pages()
when interrupted before any pages have been processed. At this point
ioctl can be obviously safely restarted.

Reported-by: Haitao Huang &lt;haitao.huang@intel.com&gt;
Signed-off-by: Jarkko Sakkinen &lt;jarkko@kernel.org&gt;
Signed-off-by: Borislav Petkov &lt;bp@suse.de&gt;
Link: https://lkml.kernel.org/r/20201118213932.63341-1-jarkko@kernel.org
</content>
</entry>
<entry>
<title>x86/sgx: Clarify 'laundry_list' locking</title>
<updated>2020-11-18T17:16:28Z</updated>
<author>
<name>Dave Hansen</name>
</author>
<published>2020-11-16T22:25:31Z</published>
<link rel='alternate' type='text/html' href='https://git.0xinfinity.dev/distro/kernel.git/commit/?id=67655b57f8f59467506463055d9a8398d2836377'/>
<id>urn:sha1:67655b57f8f59467506463055d9a8398d2836377</id>
<content type='text'>
Short Version:

The SGX section-&gt;laundry_list structure is effectively thread-local, but
declared next to some shared structures. Its semantics are clear as mud.
Fix that. No functional changes. Compile tested only.

Long Version:

The SGX hardware keeps per-page metadata. This can provide things like
permissions, integrity and replay protection. It also prevents things
like having an enclave page mapped multiple times or shared between
enclaves.

But, that presents a problem for kexec()'d kernels (or any other kernel
that does not run immediately after a hardware reset). This is because
the last kernel may have been rude and forgotten to reset pages, which
would trigger the "shared page" sanity check.

To fix this, the SGX code "launders" the pages by running the EREMOVE
instruction on all pages at boot. This is slow and can take a long
time, so it is performed off in the SGX-specific ksgxd instead of being
synchronous at boot. The init code hands the list of pages to launder in
a per-SGX-section list: -&gt;laundry_list. The only code to touch this list
is the init code and ksgxd. This means that no locking is necessary for
-&gt;laundry_list.

However, a lock is required for section-&gt;page_list, which is accessed
while creating enclaves and by ksgxd. This lock (section-&gt;lock) is
acquired by ksgxd while also processing -&gt;laundry_list. It is easy to
confuse the purpose of the locking as being for -&gt;laundry_list and
-&gt;page_list.

Rename -&gt;laundry_list to -&gt;init_laundry_list to make it clear that this
is not normally used at runtime. Also add some comments clarifying the
locking, and reorganize 'sgx_epc_section' to put 'lock' near the things
it protects.

Note: init_laundry_list is 128 bytes of wasted space at runtime. It
could theoretically be dynamically allocated and then freed after
the laundering process. But it would take nearly 128 bytes of extra
instructions to do that.

Signed-off-by: Jarkko Sakkinen &lt;jarkko@kernel.org&gt;
Signed-off-by: Dave Hansen &lt;dave.hansen@linux.intel.com&gt;
Signed-off-by: Borislav Petkov &lt;bp@suse.de&gt;
Link: https://lkml.kernel.org/r/20201116222531.4834-1-dave.hansen@intel.com
</content>
</entry>
<entry>
<title>x86/sgx: Add ptrace() support for the SGX driver</title>
<updated>2020-11-18T17:04:11Z</updated>
<author>
<name>Jarkko Sakkinen</name>
</author>
<published>2020-11-12T22:01:33Z</published>
<link rel='alternate' type='text/html' href='https://git.0xinfinity.dev/distro/kernel.git/commit/?id=947c6e11fa4310b31c10016ae9816cdca3f1694e'/>
<id>urn:sha1:947c6e11fa4310b31c10016ae9816cdca3f1694e</id>
<content type='text'>
Enclave memory is normally inaccessible from outside the enclave. This
makes enclaves hard to debug. However, enclaves can be put in a debug
mode when they are being built. In that mode, enclave data *can* be read
and/or written by using the ENCLS[EDBGRD] and ENCLS[EDBGWR] functions.

This is obviously only for debugging and destroys all the protections
present with normal enclaves. But, enclaves know their own debug status
and can adjust their behavior appropriately.

Add a vm_ops-&gt;access() implementation which can be used to read and write
memory inside debug enclaves.  This is typically used via ptrace() APIs.

 [ bp: Massage. ]

Signed-off-by: Jarkko Sakkinen &lt;jarkko@kernel.org&gt;
Signed-off-by: Borislav Petkov &lt;bp@suse.de&gt;
Tested-by: Jethro Beekman &lt;jethro@fortanix.com&gt;
Link: https://lkml.kernel.org/r/20201112220135.165028-23-jarkko@kernel.org
</content>
</entry>
<entry>
<title>x86/sgx: Add a page reclaimer</title>
<updated>2020-11-18T17:04:11Z</updated>
<author>
<name>Jarkko Sakkinen</name>
</author>
<published>2020-11-12T22:01:32Z</published>
<link rel='alternate' type='text/html' href='https://git.0xinfinity.dev/distro/kernel.git/commit/?id=1728ab54b4be94aed89276eeb8e750a345659765'/>
<id>urn:sha1:1728ab54b4be94aed89276eeb8e750a345659765</id>
<content type='text'>
Just like normal RAM, there is a limited amount of enclave memory available
and overcommitting it is a very valuable tool to reduce resource use.
Introduce a simple reclaim mechanism for enclave pages.

In contrast to normal page reclaim, the kernel cannot directly access
enclave memory.  To get around this, the SGX architecture provides a set of
functions to help.  Among other things, these functions copy enclave memory
to and from normal memory, encrypting it and protecting its integrity in
the process.

Implement a page reclaimer by using these functions. Picks victim pages in
LRU fashion from all the enclaves running in the system.  A new kernel
thread (ksgxswapd) reclaims pages in the background based on watermarks,
similar to normal kswapd.

All enclave pages can be reclaimed, architecturally.  But, there are some
limits to this, such as the special SECS metadata page which must be
reclaimed last.  The page version array (used to mitigate replaying old
reclaimed pages) is also architecturally reclaimable, but not yet
implemented.  The end result is that the vast majority of enclave pages are
currently reclaimable.

Co-developed-by: Sean Christopherson &lt;sean.j.christopherson@intel.com&gt;
Signed-off-by: Sean Christopherson &lt;sean.j.christopherson@intel.com&gt;
Signed-off-by: Jarkko Sakkinen &lt;jarkko@kernel.org&gt;
Signed-off-by: Borislav Petkov &lt;bp@suse.de&gt;
Acked-by: Jethro Beekman &lt;jethro@fortanix.com&gt;
Link: https://lkml.kernel.org/r/20201112220135.165028-22-jarkko@kernel.org
</content>
</entry>
</feed>
