<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:base="https://jbeekman.nl/">
  <id>https://jbeekman.nl/</id>
  <title>SGX – Technology &amp; Policy</title>
  <updated>2017-03-01T12:00:00Z</updated>
  <link rel="alternate" href="https://jbeekman.nl/"/>
  <link rel="self" href="https://jbeekman.nl/blog/sgx/"/>
  <author>
    <name>Jethro Beekman</name>
    <uri>https://jbeekman.nl/blog</uri>
  </author>
  <icon>https://jbeekman.nl/favicon.ico</icon>
  <entry>
    <id>tag:jbeekman.nl,2017-03-01:/blog/2017/03/sgx-side-channel-attacks/</id>
    <title type="html">On the recent side-channel attacks on Intel SGX – Technology &amp; Policy</title>
    <published>2017-03-01T12:00:00Z</published>
    <updated>2017-03-01T12:00:00Z</updated>
    <link rel="alternate" href="https://jbeekman.nl/blog/2017/03/sgx-side-channel-attacks/"/>
    <content type="html">&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Side-channel_attack"&gt;Side-channel attacks&lt;/a&gt; are 
my favorite attack in computer security because they poke giant holes in the 
abstraction and security models that system designers are using. When I hear of 
a new attack avenue in this space, my first reaction often is “wow, that is 
&lt;em&gt;so&lt;/em&gt; cool” and “I didn’t even think about that.”&lt;/p&gt;

&lt;p&gt;In the past week, &lt;a href="https://arxiv.org/abs/1702.07521"&gt;two&lt;/a&gt; 
&lt;a href="https://arxiv.org/abs/1702.08719"&gt;papers&lt;/a&gt; have been published on arXiv 
detailing side-channel attacks on &lt;a href="https://software.intel.com/en-us/sgx"&gt;Intel 
SGX&lt;/a&gt;. While the existence of such attacks 
should be taken seriously by people designing systems using Intel SGX (which 
includes yours truly), these particular attacks are not very interesting.&lt;/p&gt;

&lt;p&gt;First of all, these attacks really shouldn’t come as a surprise to anyone 
following this space. Cache-based side-channel attacks are a well-known attack 
vector. Many papers detailing new techniques have been published over the last 
couple of years. There’s &lt;a href="http://www.cs.tau.ac.il/~tromer/papers/cache.pdf"&gt;Evict+Time and 
Prime+Probe&lt;/a&gt;, 
&lt;a href="https://www.usenix.org/system/files/conference/usenixsecurity14/sec14-paper-yarom.pdf"&gt;Flush+Reload&lt;/a&gt;, 
&lt;a href="https://arxiv.org/abs/1511.04594"&gt;Flush+Flush&lt;/a&gt;, etc. &lt;a href="https://eprint.iacr.org/2016/613"&gt;Ge et. 
al.&lt;/a&gt; present a good overview of the current 
state of the art. Intel explicitly states in their &lt;a href="https://software.intel.com/sites/default/files/managed/ae/48/Software-Guard-Extensions-Enclave-Writers-Guide.pdf"&gt;Enclave Writer’s 
Guide&lt;/a&gt; 
that they don’t protect against attacks at cache line or higher granularity.&lt;/p&gt;

&lt;p&gt;Second of all, both attacks are exploiting well-known flaws in modular 
exponentiation implementations. We &lt;em&gt;know&lt;/em&gt; how to do constant-time RSA. On top 
of that, both papers are at best slightly misleading in describing their attack 
targets.&lt;/p&gt;

&lt;p&gt;From the &lt;a href="https://arxiv.org/abs/1702.07521"&gt;first paper&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;As our victim enclave we chose an RSA implementation from the Intel IIP 
crypto library in the Intel SGX SDK. The attacked decryption variant is a 
fixed-size sliding window exponentiation, the code is available online at 
&lt;a href="https://github.com/01org/linux-sgx/blob/6662022/external/crypto_px/sources/ippcp/src/pcpngrsamontstuff.c#L336"&gt;[32]&lt;/a&gt;. 
The Intel IIP library includes also a variant of RSA that is hardened against 
cache attacks 
&lt;a href="https://github.com/01org/linux-sgx/blob/6662022/external/crypto_px/sources/ippcp/src/pcpngrsamontstuff.c#L438"&gt;[33]&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you look at how these two variants are used, you can see that only 
&lt;a href="https://github.com/01org/linux-sgx/blob/6662022/external/crypto_px/sources/ippcp/src/pcpngrsaencodec.c#L180"&gt;computations with the public 
exponent&lt;/a&gt; 
are done with the “vulnerable” variant, whereas &lt;a href="https://github.com/01org/linux-sgx/blob/6662022/external/crypto_px/sources/ippcp/src/pcpngrsaencodec.c#L265"&gt;computations with the private 
exponent&lt;/a&gt; 
use the “hardened” variant. So, unless you are somehow swapping your public and 
private exponents, using this crypto library as documented will prevent this 
attack for you.&lt;/p&gt;

&lt;p&gt;From the &lt;a href="https://arxiv.org/abs/1702.08719"&gt;second paper’s abstract&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;We perform a Prime+Probe cache side-channel attack on a co-located SGX 
enclave running an up-to-date RSA implementation that uses a constant-time 
multiplication primitive.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Even though the library they use might have a multiplication primitive that is 
constant-time, as the authors explain further on in the paper, the modular 
exponentiation primitive is not. In fact, the modular exponentiation algorithm 
is the textbook example of an algorithm with a secret-dependent branch:&lt;/p&gt;

&lt;div class="language-c++ highlighter-coderay"&gt;&lt;div class="CodeRay"&gt;
  &lt;div class="code"&gt;&lt;pre&gt;&lt;span style="color:#0a8;font-weight:bold"&gt;int&lt;/span&gt; modexp(&lt;span style="color:#0a8;font-weight:bold"&gt;int&lt;/span&gt; base, &lt;span style="color:#0a8;font-weight:bold"&gt;int&lt;/span&gt; exponent, &lt;span style="color:#0a8;font-weight:bold"&gt;int&lt;/span&gt; modulus) {
    &lt;span style="color:#0a8;font-weight:bold"&gt;int&lt;/span&gt; result = &lt;span style="color:#00D"&gt;1&lt;/span&gt;;
    &lt;span style="color:#080;font-weight:bold"&gt;for&lt;/span&gt; (&lt;span style="color:#0a8;font-weight:bold"&gt;int&lt;/span&gt; i = &lt;span style="color:#00D"&gt;0&lt;/span&gt;; i &amp;lt; exponent.bits(); i++) {
        result = modsqr(result, modulus);
        &lt;span style="color:#080;font-weight:bold"&gt;if&lt;/span&gt; (exponent &amp;amp; (&lt;span style="color:#00D"&gt;1&lt;/span&gt;&amp;lt;&amp;lt;i)) { &lt;span style="color:#777"&gt;// access bit `i`&lt;/span&gt;
            result = modmul(result, base, modulus);
        }
    }
    &lt;span style="color:#080;font-weight:bold"&gt;return&lt;/span&gt; result;
}
&lt;/pre&gt;&lt;/div&gt;
&lt;/div&gt;
&lt;/div&gt;

&lt;p&gt;If for each iteration of the loop you can detect whether the multiplication 
happenned or not, you can reconstruct the individual bits of the exponent. This 
is a fun attack, but as mentioned, this is basically the most well-known timing 
attack, &lt;a href="https://www.eng.tau.ac.il/~yash/infosec-seminar/2005/kocher95.pdf"&gt;first described by Kocher 22 years 
ago&lt;/a&gt;. Oh and 
the library? It implements the blinding mitigation also mentioned in that 
seminal paper.&lt;/p&gt;

&lt;p&gt;To summarize: Yes, programs running with Intel SGX are vulnerable to 
side-channel attacks. The same side-channel attacks that have been used for 
years on modern x86 platforms. This is well-documented. SGX does present a 
slightly different threat model which makes deployment of side-channel attacks 
more likely. Hopefully everyone using SGX is implementing countermeasures. They 
do exist, and are already implemented by most cryptography libraries.&lt;/p&gt;
</content>
    <summary type="html">&lt;p&gt;&lt;a href="https://en.wikipedia.org/wiki/Side-channel_attack"&gt;Side-channel attacks&lt;/a&gt; are 
my favorite attack in computer security because they poke giant holes in the 
abstraction and security models that system designers are using. When I hear of 
a new attack avenue in this space, my first reaction often is &amp;#x201C;wow, that is 
&lt;em&gt;so&lt;/em&gt; cool&amp;#x201D; and &amp;#x201C;I didn&amp;#x2019;t even think about that.&amp;#x201D;&lt;/p&gt;</summary>
  </entry>
  <entry>
    <id>tag:jbeekman.nl,2015-10-13:/blog/2015/10/intel-has-full-control-over-sgx/</id>
    <title type="html">Intel has full control over SGX – Technology &amp; Policy</title>
    <published>2015-10-13T12:00:00Z</published>
    <updated>2015-10-13T12:00:00Z</updated>
    <link rel="alternate" href="https://jbeekman.nl/blog/2015/10/intel-has-full-control-over-sgx/"/>
    <content type="html">&lt;p&gt;Intel has full control over what software you can run in SGX. This might seem 
redundant: Intel makes the processor, so of course they have full control. Yet 
the truth is slightly more inconvenient. When Intel processors don’t run the 
instructions in your standard software (whether incorrectly or at all), that is 
a defect at best and a breach of contract at worst. Yet the SGX instruction set 
&lt;em&gt;includes in its specification&lt;/em&gt; that Intel has the authority to make this 
go/no-go decision.&lt;/p&gt;

&lt;p&gt;Let’s take a closer look at how exactly this is specified, since it is pretty 
well-hidden. After creating and measuring a secure enclave using ECREATE, EADD, 
and EEXTEND, the EINIT instruction needs to be executed before execution 
control can be transferred to the enclave. The EINIT instruction has 2 inputs: 
SIGSTRUCT and EINITTOKEN. SIGSTRUCT contains information about the enclave 
including an expected hash of the memory. As the name implies, SIGSTRUCT is 
also cryptographically signed using some key. EINITTOKEN also contains 
information about the enclave including the same expected hash of the memory as 
well as the expected public key for the signature. EINITTOKEN must be 
&lt;a href="https://en.wikipedia.org/wiki/Message_authentication_code"&gt;MAC&lt;/a&gt;ed using the 
so-called &lt;em&gt;launch key&lt;/em&gt;. Both SIGSTRUCT and EINITTOKEN are checked by EINIT and 
must be valid for execution to proceed succesfully.&lt;/p&gt;

&lt;p&gt;Since the launch key is a symmetric cryptography device, surely this key is not 
widely distributed and most likely is CPU-specific. But how can one obtain this 
key? The EGETKEY instruction can be used to obtain SGX keys, including the 
launch key. But this is a user-mode instruction that can only be executed from 
inside an enclave. There seems to be a chicken-and-egg problem here: to launch 
an enclave, we need the launch key. To get the launch key, we need to launch an 
enclave! Here’s the catch: the EINITTOKEN need not be valid if SIGSTRUCT is 
signed by an Intel key that is baked into the processor.&lt;/p&gt;

&lt;p&gt;Thus, Intel can distribute an Intel-signed “launch enclave” that is able to 
hand out correctly-MACed EINITTOKENs that can then be used to start other 
enclaves. But they can include whatever logic they want in the launch enclave
so Intel can at its sole discretion choose not to MAC a particular EINITTOKEN.&lt;/p&gt;

&lt;p&gt;As most things SGX, this “feature” is severely underdocumented. The terms 
“launch key” and “launch enclave” are only mentioned a few times in the SGX 
programming reference and never in the whitepapers or tutorials. At the time of 
writing, nowhere else on the Internet is there any mention of these keywords, 
except for one &lt;a href="https://www.quora.com/What-are-some-good-uses-for-Intel-Software-Guard-Extensions-SGX"&gt;insightful Quora 
answer&lt;/a&gt; 
that I wish I had read months ago.&lt;/p&gt;

&lt;p&gt;What reason could Intel have for this architecture? Along with the fact that 
&lt;a href="/blog/sgx-hardware-first-look"&gt;SGX is being disabled by default&lt;/a&gt;, this looks 
like Intel is again just setting this security technology up for failure due to 
the lack of widespread adoption by developers and users alike (cf. TXT, SMX, 
TPM).&lt;/p&gt;
</content>
    <summary type="html">&lt;p&gt;Intel has full control over what software you can run in SGX. This might seem 
redundant: Intel makes the processor, so of course they have full control. Yet 
the truth is slightly more inconvenient. When Intel processors don&amp;#x2019;t run the 
instructions in your standard software (whether incorrectly or at all), that is 
a defect at best and a breach of contract at worst. Yet the SGX instruction set 
&lt;em&gt;includes in its specification&lt;/em&gt; that Intel has the authority to make this 
go/no-go decision.&lt;/p&gt;</summary>
  </entry>
  <entry>
    <id>tag:jbeekman.nl,2015-10-08:/blog/2015/10/sgx-hardware-first-look/</id>
    <title type="html">SGX Hardware: A first look – Technology &amp; Policy</title>
    <published>2015-10-08T12:00:00Z</published>
    <updated>2015-12-09T12:00:00Z</updated>
    <link rel="alternate" href="https://jbeekman.nl/blog/2015/10/sgx-hardware-first-look/"/>
    <content type="html">&lt;p&gt;Without much fanfare, Intel has released &lt;a href="https://software.intel.com/en-us/isa-extensions/intel-sgx"&gt;Software Guard Extensions 
(SGX)&lt;/a&gt; in Skylake. 
When I say “without much fanfare,” I mean practically only the following 
paragraph hidden on page 3 of a &lt;a href="http://download.intel.com/newsroom/kits/core/6thgen/pdfs/6th_Gen_Intel_Core-Intel_Xeon_Factsheet.pdf"&gt;press fact 
sheet&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;BETTER SECURITY.&lt;/strong&gt; The Skylake architecture has been designed to enable better 
security, including Intel® Software Guard Extensions (Intel® SGX) that can 
provide an additional level of hardware-based protection by putting data into 
a secure container on the platform, and Intel® Memory Protection Extensions 
(Intel® MPX) that can help prevent buffer flow attacks. [What’s a buffer flow 
attack? &lt;em&gt;Ed.&lt;/em&gt;] To be fully utilized, Intel SGX and Intel MPX require additional 
software capabilities, which will begin to be delivered by the ecosystem 
later this year.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It has been extremely difficult to find actual hardware that supports SGX. BIOS 
support is required–the BIOS needs to set aside memory for the Enclave Page 
Cache (EPC)–but of course no vendor will mention anything about this on their 
website, nor will they (be able to) answer when you inquire regarding this 
specific issue.&lt;/p&gt;

&lt;p&gt;To my delight, by using Google to search past week results for “intel sgx” for 
the last few months, I was finally able to find a driver download site that 
linked this &lt;a href="http://www.dell.com/support/home/us/en/19/Drivers/DriversDetails?driverId=F84XC"&gt;Dell 
driver&lt;/a&gt;.
According to Dell’s website, this driver is compatible with the following machine models:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Inspiron 11 i3153&lt;/li&gt;
  &lt;li&gt;Inspiron 11 i3158&lt;/li&gt;
  &lt;li&gt;Inspiron 13 i7353&lt;/li&gt;
  &lt;li&gt;Inspiron 13 i7359&lt;/li&gt;
  &lt;li&gt;Inspiron 15 i7568&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;At first I couldn’t find these models mentioned anywhere, but a few days later 
the i7359 showed up at 
&lt;a href="http://www.newegg.com/Product/ProductList.aspx?Submit=ENE&amp;amp;Description=dell+i7359"&gt;NewEgg&lt;/a&gt; 
and then at &lt;a href="http://frys.com/search?query_string=dell+i7359"&gt;Frys&lt;/a&gt;. So, I drove 
to Sunnyvale (where Frys had the i7359-2435SLV in stock) and I can now confirm 
that &lt;strong&gt;SGX is real&lt;/strong&gt;:&lt;/p&gt;

&lt;div style="width:100%;text-align:center"&gt;
	&lt;a href="/img/blog/sgx-hardware-first-look/bios1.jpeg"&gt;&lt;img src="/img/blog/sgx-hardware-first-look/bios1-small.jpeg" style="width:45%;margin-right:3.3%" /&gt;&lt;/a&gt;
	&lt;a href="/img/blog/sgx-hardware-first-look/bios2.jpeg"&gt;&lt;img src="/img/blog/sgx-hardware-first-look/bios2-small.jpeg" style="width:45%" /&gt;&lt;/a&gt;
&lt;/div&gt;

&lt;p&gt;It’s interesting to note that SGX was disabled in the BIOS by default, so most 
consumers will not be able to benefit from this feature at all.&lt;/p&gt;

&lt;p&gt;The maximum size of the EPC on this laptop is 128MB. This means that enclaves 
requiring more memory than that will need regular paging between the EPC and 
main memory. It’s not clear whether such a copy would require re-encryption of 
the page, the EPC itself is already encrypted so it might not be necessary.&lt;/p&gt;

&lt;p&gt;The laptop comes with Windows 10 which runs excruciatingly slow–not surprising 
considering it only has 4GB of RAM. I installed Arch Linux on it because it’s 
one of the few distro’s that has an installer with a very recent kernel 
(4.2.2), required for such new hardware.&lt;/p&gt;

&lt;p&gt;I collected some CPUID and MSR information to see what SGX features are 
supported:&lt;/p&gt;

&lt;blockquote&gt;
  &lt;p&gt;&lt;strong&gt;CPUID&lt;/strong&gt;&lt;/p&gt;

  &lt;pre&gt;&lt;code&gt;                         bit 2 (SGX) is set
                         v
 7h(0h) 00000000h 029c67afh 00000000h 00000000h

     Max. enclave size 2^31 bytes (32-bit mode)
  Max. enclave size 2^36 bytes (64-bit mode) |
    No extended SSA features supported    |  |
SGX version 1 supported  |                |\/|
               v         v                vvvv
12h(0h) 00000001h 00000000h 00000000h 0000241fh

              all enclave attributes supported
              |\       all XSAVE bits supported
              vv                  vv
12h(1h) 00000036h 00000000h 0000001fh 00000000h

   EPC physical address 80200000h
        ////|     |\\\\\\\  EPC size 93.5 MiB
        vvvvv     vvvvvvvv  vvvvv     vvvvvvvv
12h(2h) 80200001h 00000000h 05d80001h 00000000h

&lt;/code&gt;&lt;/pre&gt;

  &lt;p&gt;&lt;strong&gt;MSR&lt;/strong&gt;&lt;/p&gt;

  &lt;pre&gt;&lt;code&gt;                bit 18 (SGX_ENABLE) is set
                |   bit 0 (LOCK) is set
                v   v
3ah 00000000_00040005h (IA32_FEATURE_CONTROL)
&lt;/code&gt;&lt;/pre&gt;
&lt;/blockquote&gt;

&lt;p&gt;I’m currently writing a simple Linux kernel driver to be able to actually use 
SGX. I managed to generate a Page Fault using the &lt;code&gt;ENCLS[EBLOCK]&lt;/code&gt; instruction, 
so at least something seems to be working.&lt;/p&gt;

&lt;p&gt;I really wish Intel would be more forthcoming with information about and 
developer support for SGX. The hardly-announced release and default-disabled 
BIOS setting don’t warrant much hope for the future of SGX.&lt;/p&gt;

&lt;p&gt;In the mean time, I intend to write more blog posts in the near future as I try 
to get SGX up and running. Here’s a cliff hanger for you: the Dell driver 
package mentioned earlier contains a file &lt;code&gt;aesm_service.exe&lt;/code&gt; that contains the 
string “SGX EPID provisioning network failure.” I’ll try to tell you more about 
it next time.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Update 2015-12-09:&lt;/strong&gt; Please see my 
&lt;a href="https://github.com/jethrogb/sgx-utils"&gt;sgx-utils&lt;/a&gt; repository for any 
open-source SGX utilities, including a bare-bones development Linux driver.&lt;/p&gt;
</content>
    <summary type="html">&lt;p&gt;Without much fanfare, Intel has released &lt;a href="https://software.intel.com/en-us/isa-extensions/intel-sgx"&gt;Software Guard Extensions 
(SGX)&lt;/a&gt; in Skylake. 
When I say &amp;#x201C;without much fanfare,&amp;#x201D; I mean practically only the following 
paragraph hidden on page 3 of a &lt;a href="http://download.intel.com/newsroom/kits/core/6thgen/pdfs/6th_Gen_Intel_Core-Intel_Xeon_Factsheet.pdf"&gt;press fact 
sheet&lt;/a&gt;:&lt;/p&gt;</summary>
  </entry>
</feed>

