Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / native_client_sdk / doc_generated / reference / sandbox_internals / arm-32-bit-sandbox.html
1 {{+bindTo:partials.standard_nacl_article}}
2
3 <section id="arm-32-bit-sandbox">
4 <span id="id1"></span><h1 id="arm-32-bit-sandbox"><span id="id1"></span>ARM 32-bit Sandbox</h1>
5 <p>Native Client for ARM is a sandboxing technology for running
6 programs&#8212;even malicious ones&#8212;safely, on computers that use 32-bit
7 ARM processors. The ARM sandbox is an extension of earlier work on
8 Native Client for x86 processors. Security is provided with a low
9 performance overhead of about 10% over regular ARM code, and as you&#8217;ll
10 see in this document the sandbox model is beautifully simple, meaning
11 that the trusted codebase is much easier to validate.</p>
12 <p>As an implementation detail, the Native Client 32-bit ARM sandbox is
13 currently used by Portable Native Client to execute code on 32-bit ARM
14 machines in a safe manner. The portable bitcode contained in a <strong>pexe</strong>
15 is translated to a 32-bit ARM <strong>nexe</strong> before execution. This may change
16 at a point in time: Portable Native Client doesn&#8217;t necessarily need this
17 sandbox to execute code on ARM. Note that the Portable Native Client
18 compiler itself is also untrusted: it too runs in the ARM sandbox
19 described in this document.</p>
20 <p>On this page, we describe how Native Client works on 32-bit ARM. We
21 assume no prior knowledge about the internals of Native Client, on x86
22 or any other architecture, but we do assume some familiarity with
23 assembly languages in general.</p>
24 <div class="contents local" id="contents" style="display: none">
25 <ul class="small-gap">
26 <li><p class="first"><a class="reference internal" href="#an-introduction-to-the-arm-architecture" id="id3">An Introduction to the ARM Architecture</a></p>
27 <ul class="small-gap">
28 <li><a class="reference internal" href="#about-arm-and-armv7-a" id="id4">About ARM and ARMv7-A</a></li>
29 <li><a class="reference internal" href="#arm-programmer-s-model" id="id5">ARM Programmer&#8217;s Model</a></li>
30 </ul>
31 </li>
32 <li><p class="first"><a class="reference internal" href="#the-native-client-approach" id="id6">The Native Client Approach</a></p>
33 <ul class="small-gap">
34 <li><p class="first"><a class="reference internal" href="#nacl-arm-pure-software-fault-isolation" id="id7">NaCl/ARM: Pure Software Fault Isolation</a></p>
35 <ul class="small-gap">
36 <li><a class="reference internal" href="#load-and-store" id="id8"><em>Load</em> and <em>Store</em></a></li>
37 <li><a class="reference internal" href="#the-stack-pointer-thread-pointer-and-program-counter" id="id9">The Stack Pointer, Thread Pointer, and Program Counter</a></li>
38 <li><a class="reference internal" href="#indirect-branch" id="id10"><em>Indirect Branch</em></a></li>
39 <li><a class="reference internal" href="#literal-pools-and-data-bundles" id="id11">Literal Pools and Data Bundles</a></li>
40 </ul>
41 </li>
42 <li><p class="first"><a class="reference internal" href="#trampolines-and-memory-layout" id="id12">Trampolines and Memory Layout</a></p>
43 <ul class="small-gap">
44 <li><a class="reference internal" href="#memory-map" id="id13">Memory Map</a></li>
45 <li><a class="reference internal" href="#inside-a-trampoline" id="id14">Inside a Trampoline</a></li>
46 </ul>
47 </li>
48 <li><p class="first"><a class="reference internal" href="#loose-ends" id="id15">Loose Ends</a></p>
49 <ul class="small-gap">
50 <li><a class="reference internal" href="#forbidden-instructions" id="id16">Forbidden Instructions</a></li>
51 <li><a class="reference internal" href="#coprocessors" id="id17">Coprocessors</a></li>
52 <li><a class="reference internal" href="#validator-code" id="id18">Validator Code</a></li>
53 </ul>
54 </li>
55 </ul>
56 </li>
57 </ul>
58
59 </div><h2 id="an-introduction-to-the-arm-architecture">An Introduction to the ARM Architecture</h2>
60 <p>In this section, we summarize the relevant parts of the ARM processor
61 architecture.</p>
62 <h3 id="about-arm-and-armv7-a">About ARM and ARMv7-A</h3>
63 <p>ARM is one of the older commercial &#8220;RISC&#8221; processor designs, dating back
64 to the early 1980s. Today, it is used primarily in embedded systems:
65 everything from toys, to home automation, to automobiles. However, its
66 most visible use is in cellular phones, tablets and some
67 laptops.</p>
68 <p>Through the years, there have been many revisions of the ARM
69 architecture, written as ARMv<em>X</em> for some version <em>X</em>. Native Client
70 specifically targets the ARMv7-A architecture commonly used in high-end
71 phones and smartbooks. This revision, defined in the mid-2000s, adds a
72 number of useful instructions, and specifies some portions of the system
73 that used to be left to individual chip manufacturers. Critically,
74 ARMv7-A specifies the &#8220;eXecute Never&#8221; bit, or <em>XN</em>. This pagetable
75 attribute lets us mark memory as non-executable. Our security relies on
76 the presence of this feature.</p>
77 <p>ARMv8 adds a new 64-bit instruction set architecture called A64, while
78 also enhancing the 32-bit A32 ISA. For Native Client&#8217;s purposes the A32
79 ISA is equivalent to the ARMv7 ARM ISA, albeit with a few new
80 instructions. This document only discussed the 32-bit A32 instruction
81 set: A64 would require a different sandboxing model.</p>
82 <h3 id="arm-programmer-s-model">ARM Programmer&#8217;s Model</h3>
83 <p>While modern ARM chips support several instruction encodings, 32-bit
84 Native Client on ARM focuses on a single one: a fixed-width encoding
85 where every instruction is 32-bits wide called A32 (previously, and
86 confusingly, called simply ARM). Thumb, Thumb2 (now confusingly called
87 T32), Jazelle, ThumbEE and such aren&#8217;t supported by Native Client. This
88 dramatically simplifies some of our analyses, as we&#8217;ll see later. Nearly
89 every instruction can be conditionally executed based on the contents of
90 a dedicated condition code register.</p>
91 <p>ARM processors have 16 general-purpose registers used for integer and
92 memory operations, written <code>r0</code> through <code>r15</code>. Of these, two have
93 special roles baked in to the hardware:</p>
94 <ul class="small-gap">
95 <li><code>r14</code> is the Link Register. The ARM <em>call</em> instruction
96 (<em>branch-with-link</em>) doesn&#8217;t use the stack directly. Instead, it
97 stashes the return address in <code>r14</code>. In other circumstances, <code>r14</code>
98 can be (and is!) used as a general-purpose register. When <code>r14</code> is
99 playing its Link Register role, it&#8217;s referred to as <code>lr</code>.</li>
100 <li><code>r15</code> is the Program Counter. While it can be read and written like
101 any other register, setting it to a new value will cause execution to
102 jump to a new address. Using it in some circumstances is also
103 undefined by the ARM architecture. Because of this, <code>r15</code> is never
104 used for anything else, and is referred to as <code>pc</code>.</li>
105 </ul>
106 <p>Other registers are given roles by convention. The only important
107 registers to Native Client are <code>r9</code> and <code>r13</code>, which are used as the
108 Thread Pointer location and Stack Pointer. When playing this role,
109 they&#8217;re referred to as <code>tp</code> and <code>sp</code>.</p>
110 <p>Like other RISC-inspired designs, ARM programs use explicit <em>load</em> and
111 <em>store</em> instructions to access memory. All other instructions operate
112 only on registers, or on registers and small constants called
113 immediates. Because both instructions and data words are 32-bits, we
114 can&#8217;t simply embed a 32-bit number into an instruction. ARM programs use
115 three methods to work around this, all of which Native Client exploits:</p>
116 <ol class="arabic simple">
117 <li>Many instructions can encode a modified immediate, which is an 8-bit
118 number rotated right by an even number of bits.</li>
119 <li>The <code>movw</code> and <code>movt</code> instructions can be used to set the top and
120 bottom 16-bits of a register, and can therefore encode any 32-bit
121 immediate.</li>
122 <li>For values that can&#8217;t be represented as modified immediates, ARM
123 programs use <code>pc</code>-relative loads to load data from inside the
124 code&#8212;hidden in a place where it won&#8217;t be executed such as &#8220;constant
125 pools&#8221;, just past the final return of a function.</li>
126 </ol>
127 <p>We&#8217;ll introduce more details of the ARM instruction set later, as we
128 walk through the system.</p>
129 <h2 id="the-native-client-approach">The Native Client Approach</h2>
130 <p>Native Client runs an untrusted program, potentially from an unknown or
131 malicious source, inside a sandbox created by a trusted runtime. The
132 trusted runtime allows the untrusted program to &#8220;call-out&#8221; and perform
133 certain actions, such as drawing graphics, but prevents it from
134 accessing the operating system directly. This &#8220;call-out&#8221; facility,
135 called a trampoline, looks like a standard function call to the
136 untrusted program, but it allows control to escape from the sandbox in a
137 controlled way.</p>
138 <p>The untrusted program and trusted runtime inhabit the same process, or
139 virtual address space, maintained by the operating system. To keep the
140 trusted runtime behaving the way we expect, we must prevent the
141 untrusted program from accessing and modifying its internals. Since they
142 share a virtual address space, we can&#8217;t rely on the operating system for
143 this. Instead, we isolate the untrusted program from the trusted
144 runtime.</p>
145 <p>Unlike modern operating systems, we use a cooperative isolation
146 method. Native Client can&#8217;t run any off-the-shelf program compiled for
147 an off-the-shelf operating system. The program must be compiled to
148 comply with Native Client&#8217;s rules. The details vary on each platform,
149 but in general, the untrusted program:</p>
150 <ul class="small-gap">
151 <li>Must not attempt to use certain forbidden instructions, such as system
152 calls.</li>
153 <li>Must not attempt to modify its own code without abiding by Native
154 Client&#8217;s code modification rules.</li>
155 <li>Must not jump into the middle of an instruction group, or otherwise do
156 tricky things to cause instructions to be interpreted multiple ways.</li>
157 <li>Must use special, strictly-defined instruction sequences to perform
158 permitted but potentially dangerous actions. We call these sequences
159 pseudo-instructions.</li>
160 </ul>
161 <p>We can&#8217;t simply take the program&#8217;s word that it complies with these
162 rules&#8212;we call it &#8220;untrusted&#8221; for a reason! Nor do we require it to be
163 produced by a special compiler; in practice, we don&#8217;t trust our
164 compilers either. Instead, we apply a load-time validator that
165 disassembles the program. The validator either proves that the program
166 complies with our rules, or rejects it as unsafe. By keeping the rules
167 simple, we keep the validator simple, small, and fast. We like to put
168 our trust in small, simple things, and the validator is key to the
169 system&#8217;s security.</p>
170 <aside class="note">
171 For the computationally-inclined, all our validators scale linearly in
172 the size of the program.
173 </aside>
174 <h3 id="nacl-arm-pure-software-fault-isolation">NaCl/ARM: Pure Software Fault Isolation</h3>
175 <p>In the original Native Client system for the x86, we used unusual
176 hardware features of that processor (the segment registers) to isolate
177 untrusted programs. This was simple and fast, but won&#8217;t work on ARM,
178 which has nothing equivalent. Instead, we use pure software fault
179 isolation.</p>
180 <p>We use a fixed address space layout: the untrusted program gets the
181 lowest gigabyte, addresses <code>0</code> through <code>0x3FFFFFFF</code>. The rest of the
182 address space holds the trusted runtime and the operating system. We
183 isolate the program by requiring every <em>load</em>, <em>store</em>, and <em>indirect
184 branch</em> (to an address in a register) to use a pseudo-instruction. The
185 pseudo-instructions ensure that the address stays within the
186 sandbox. The <em>indirect branch</em> pseudo-instruction, in turn, ensures that
187 such branches won&#8217;t split up other pseudo-instructions.</p>
188 <p>At either side of the sandbox, we place small (8KiB) guard
189 regions. These are simply areas in the process&#8217;s address space that are
190 mapped without read, write, or execute permissions, so any attempt to
191 access them for any reason&#8212;<em>load</em>, <em>store</em>, or <em>jump</em>&#8212;will cause a
192 fault.</p>
193 <p>Finally, we ban the use of certain instructions, notably direct system
194 calls. This is to ensure that the untrusted program can be run on any
195 operating system supported by Native Client, and to prevent access to
196 certain system features that might be used to subvert the sandbox. As a
197 side effect, it helps to prevent programs from exploiting buggy
198 operating system APIs.</p>
199 <p>Let&#8217;s walk through the details, starting with the simplest part: <em>load</em>
200 and <em>store</em>.</p>
201 <h4 id="load-and-store"><em>Load</em> and <em>Store</em></h4>
202 <p>All access to memory must be through <em>load</em> and <em>store</em>
203 pseudo-instructions. These are simply a native <em>load</em> or <em>store</em>
204 instruction, preceded by a guard instruction.</p>
205 <p>Each <em>load</em> or <em>store</em> pseudo-instruction is similar to the <em>load</em> shown
206 below. We use abstract &#8220;placeholder&#8221; registers instead of specific
207 numbered registers for the sake of discussion. <code>rA</code> is the register
208 holding the address to load from. <code>rD</code> is the destination for the
209 loaded data.</p>
210 <pre>
211 bic    rA,  #0xC0000000
212 ldr    rD,  [rA]
213 </pre>
214 <p>The first instruction, <code>bic</code>, clears the top two bits of <code>rA</code>. In
215 this case, that means that the value in <code>rA</code> is forced to an address
216 inside our sandbox, between <code>0</code> and <code>0x3FFFFFFF</code>, inclusive.</p>
217 <p>The second instruction, <code>ldr</code>, uses the previously-sandboxed address
218 to load a value. This address might not be the address that the program
219 intended, and might cause an access to an unmapped memory location
220 within the sandbox: <code>bic</code> forces the address to be valid, by clearing
221 the top two bits. This is a no-op in a correct program.</p>
222 <p>This illustrates a common property of all Native Client systems: we aim
223 for safety, not correctness. A program using an invalid address in
224 <code>rA</code> here is simply broken, so we are free to do whatever we want to
225 preserve safety. In this case the program might load an invalid (but
226 safe) value, or cause a segmentation fault limited to the untrusted
227 code.</p>
228 <p>Now, if we allowed arbitrary branches within the program, a malicious
229 program could set up carefully-crafted values in <code>rA</code>, and then jump
230 straight to the <code>ldr</code>. This is why we validate that programs never
231 split pseudo-instructions.</p>
232 <h5 id="alternative-sandboxing">Alternative Sandboxing</h5>
233 <pre>
234 tst    rA,  #0xC0000000
235 ldreq  rD,  [rA]
236 </pre>
237 <p>The first instruction, <code>tst</code>, performs a bitwise-<code>AND</code> of <code>rA</code>
238 and the modified immediate literal, <code>0xC0000000</code>. It sets the
239 condition flags based on the result, but does not write the result to a
240 register. In particular, it sets the <code>Z</code> condition flag if the result
241 was zero&#8212;if the two values had no set bits in common. In this case,
242 that means that the value in <code>rA</code> was an address inside our sandbox,
243 between <code>0</code> and <code>0x3FFFFFFF</code>, inclusive.</p>
244 <p>The second instruction, <code>ldreq</code>, is a conditional load if equal. As we
245 mentioned before, nearly all ARM instructions can be made
246 conditional. In assembly language, we simply stick the desired condition
247 on the end of the instruction&#8217;s mnemonic name. Here, the condition is
248 <code>EQ</code>, which causes the instruction to execute only if the <code>Z</code> flag
249 is set.</p>
250 <p>Thus, when the pseudo-instruction executes, the <code>tst</code> sets <code>Z</code> if
251 (and only if) the value in <code>rA</code> is an address within the bounds of the
252 sandbox, and then the <code>ldreq</code> loads if (and only if) it was. If <code>rA</code>
253 held an invalid address, the <em>load</em> does not execute, and <code>rD</code> is
254 unchanged.</p>
255 <aside class="note">
256 The <code>tst</code>-based sequence is faster than the <code>bic</code>-based sequence
257 on modern ARM chips. It avoids a data dependency in the address
258 register. This is why we keep both around. The <code>tst</code>-based sequence
259 unfortunately leaks information on some processors, and is therefore
260 forbidden on certain processors. This effectively means that it cannot
261 be used for regular Native Client <strong>nexe</strong> files, but can be used with
262 Portable Native Client because the target processor is known at
263 translation time from <strong>pexe</strong> to <strong>nexe</strong>.
264 </aside>
265 <h5 id="addressing-modes">Addressing Modes</h5>
266 <p>ARM has an unusually rich set of addressing modes. We allow all but one:
267 register-indexed, where two registers are added to determine the
268 address.</p>
269 <p>We permit simple <em>load</em> and <em>store</em>, as shown above. We also permit
270 displacement, pre-index, and post-index memory operations:</p>
271 <pre>
272 bic    rA,  #0xC0000000
273 ldr    rD,  [rA, #1234]    ; This is fine.
274 bic    rA,  #0xC0000000
275 ldr    rD,  [rA, #1234]!   ; Also fine.
276 bic    rA,  #0xC0000000
277 ldr    rD,  [rA], #1234    ; Looking good.
278 </pre>
279 <p>In each case, we know <code>rA</code> points into the sandbox when the <code>ldr</code>
280 executes. We allow adding an immediate displacement to <code>rA</code> to
281 determine the final address (as in the first two examples here) because
282 the largest immediate displacement is Â±4095 bytes, while our guard pages
283 are 8192 bytes wide.</p>
284 <p>We also allow ARM&#8217;s more unusual <em>load</em> and <em>store</em> instructions, such
285 as <em>load-multiple</em> and <em>store-multiple</em>, etc.</p>
286 <h5 id="conditional-load-and-store">Conditional <em>Load</em> and <em>Store</em></h5>
287 <p>There&#8217;s one problem with the pseudo-instructions shown above: they are
288 unconditional (assuming <code>rA</code> is valid). ARM compilers regularly use
289 conditional <em>load</em> and <em>store</em>, so we should support this in Native
290 Client. We do so by defining alternate, predictable
291 pseudo-instructions. Here is a conditional <em>store</em>
292 (<em>store-if-greater-than</em>) using this pseudo-instruction sequence:</p>
293 <pre>
294 bicgt  rA,  #0xC0000000
295 strgt  rX,  [rA, #123]
296 </pre>
297 <h4 id="the-stack-pointer-thread-pointer-and-program-counter">The Stack Pointer, Thread Pointer, and Program Counter</h4>
298 <h5 id="stack-pointer">Stack Pointer</h5>
299 <p>In C-like languages, the stack is used to store return addresses during
300 function calls, as well as any local variables that won&#8217;t fit in
301 registers. This makes stack operations very common.</p>
302 <p>Native Client does not require guard instructions on any <em>load</em> or
303 <em>store</em> involving the stack pointer, <code>sp</code>. This improves performance
304 and reduces code size. However, ARM&#8217;s stack pointer isn&#8217;t special: it&#8217;s
305 just another register, called <code>sp</code> only by convention. To make it safe
306 to use this register as a <em>load</em> or <em>store</em> address without guards, we
307 add a rule: <code>sp</code> must always contain a valid address.</p>
308 <p>We enforce this rule by restricting the sorts of operations that
309 programs can use to alter <code>sp</code>. Programs can alter <code>sp</code> by adding or
310 subtracting an immediate, as a side-effect of a <em>load</em> or <em>store</em>:</p>
311 <pre>
312 ldr  rX,  [sp],  #4!   ; Load from stack, then add 4 to sp.
313 str  rX,  [sp, #1234]! ; Add 1234 to sp, then store to stack.
314 </pre>
315 <p>These are safe because, as we mentioned before, the largest immediate
316 available in a <em>load</em> or <em>store</em> is Â±4095. Even after adding or
317 subtracting 4095, the stack pointer will still be within the sandbox or
318 guard regions.</p>
319 <p>Any other operation that alters <code>sp</code> must be followed by a guard
320 instruction. The most common alterations, in practice, are addition and
321 subtraction of arbitrary integers:</p>
322 <pre>
323 add  sp,  rX
324 bic  sp,  #0xC0000000
325 </pre>
326 <p>The <code>bic</code> is similar to the one we used for conditional <em>load</em> and
327 <em>store</em>, and serves exactly the same purpose: after it completes, <code>sp</code>
328 is a valid address.</p>
329 <aside class="note">
330 Clever assembly programmers and compilers may want to use this
331 &#8220;trusted&#8221; property of <code>sp</code> to emit more efficient code: in a hot
332 loop instead of using <code>sp</code> as a stack pointer it can be temporarily
333 used as an index pointer (e.g. to traverse an array). This avoids the
334 extra <code>bic</code> whenever the pointer is updated in the loop.
335 </aside>
336 <h5 id="thread-pointer-loads">Thread Pointer Loads</h5>
337 <p>The thread pointer and IRT thread pointer are stored in the trusted
338 address space. All uses and definitions of <code>r9</code> from untrusted code
339 are forbidden except as follows:</p>
340 <pre>
341 ldr Rn, [r9]     ; Load user thread pointer.
342 ldr Rn, [r9, #4] ; Load IRT thread pointer.
343 </pre>
344 <h5 id="pc-relative-loads"><code>pc</code>-relative Loads</h5>
345 <p>By extension, we also allow <em>load</em> through the <code>pc</code> without a
346 mask. The explanation is quite similar:</p>
347 <ul class="small-gap">
348 <li>Our control-flow isolation rules mean that the <code>pc</code> will always
349 point into the sandbox.</li>
350 <li>The maximum immediate displacement that can be used in a
351 <code>pc</code>-relative <em>load</em> is smaller than the width of the guard pages.</li>
352 </ul>
353 <p>We do not allow <code>pc</code>-relative stores, because they look suspiciously
354 like self-modifying code, or any addressing mode that would alter the
355 <code>pc</code> as a side effect of the <em>load</em>.</p>
356 <h4 id="indirect-branch"><em>Indirect Branch</em></h4>
357 <p>There are two types of control flow on ARM: direct and indirect. Direct
358 control flow instructions have an embedded target address or
359 offset. Indirect control flow instructions take their destination
360 address from a register. The <code>b</code> (branch) and <code>bl</code>
361 (<em>branch-with-link</em>) instructions are <em>direct branch</em> and <em>call</em>,
362 respectively. The <code>bx</code> (<em>branch-exchange</em>) and <code>blx</code>
363 (<em>branch-with-link-exchange</em>) are the indirect equivalents.</p>
364 <p>Because the program counter <code>pc</code> is simply another register, ARM also
365 has many implicit indirect control flow instructions. Programs can
366 operate on the <code>pc</code> using <em>add</em> or <em>load</em>, or even outlandish (and
367 often specified as having unpredictable-behavior) things like multiply!
368 In Native Client we ban all such instructions. Indirect control flow is
369 exclusively through <code>bx</code> and <code>blx</code>. Because all of ARM&#8217;s control
370 flow instructions are called <em>branch</em> instructions, we&#8217;ll use the term
371 <em>indirect branch</em> from here on, even though this includes things like
372 <em>virtual call</em>, <em>return</em>, and the like.</p>
373 <h5 id="the-trouble-with-indirection">The Trouble with Indirection</h5>
374 <p><em>Indirect branch</em> present two problems for Native Client:</p>
375 <ul class="small-gap">
376 <li>We must ensure that they don&#8217;t send execution outside the sandbox.</li>
377 <li>We must ensure that they don&#8217;t break up the instructions inside a
378 pseudo-instruction, by landing on the second one.</li>
379 </ul>
380 <aside class="note">
381 On the x86 architectures we must also ensure that it doesn&#8217;t land
382 inside an instruction. This is unnecessary on ARM, where all
383 instructions are 32-bit wide.
384 </aside>
385 <p>Checking both of these for <em>direct branch</em> is easy: the validator just
386 pulls the (fixed) target address out of the instruction and checks what
387 it points to.</p>
388 <h5 id="the-native-client-solution-bundles">The Native Client Solution: &#8220;Bundles&#8221;</h5>
389 <p>For <em>indirect branch</em>, we can address the first problem by simply
390 masking some high-order bits off the address, like we did for <em>load</em> and
391 <em>store</em>. The second problem is more subtle. Detecting every possible
392 route that every <em>indirect branch</em> might take is difficult. Instead, we
393 take the approach pioneered by the original Native Client: we restrict
394 the possible places that any <em>indirect branch</em> can land. On Native
395 Client for ARM, <em>indirect branch</em> can target any address that has its
396 bottom four bits clear&#8212;any address that&#8217;s <code>0 mod 16</code>. We call these
397 16-byte chunks of code &#8220;bundles&#8221;. The validator makes sure that no
398 pseudo-instruction straddles a bundle boundary. Compilers must pad with
399 <code>nop</code> to ensure that every pseudo-instruction fits entirely inside one
400 bundle.</p>
401 <p>Here is the <em>indirect branch</em> pseudo-instruction. As you can see, it
402 clears the top two and bottom four bits of the address:</p>
403 <pre>
404 bic  rA,  #0xC000000F
405 bx   rA
406 </pre>
407 <p>This particular pseudo-instruction (a <code>bic</code> followed by a <code>bx</code>) is
408 used for computed jumps in switch tables and returning from functions,
409 among other uses. Recall that, under ARM&#8217;s modified immediate rules, we
410 can fit the constant <code>0xC000000F</code> into the <code>bic</code> instruction&#8217;s
411 immediate field: <code>0xC000000F</code> is the 8-bit constant <code>0xFC</code>, rotated
412 right by 4 bits.</p>
413 <p>The other useful variant is the <em>indirect branch-with-link</em>, which is
414 the ARM equivalent to <em>call</em>:</p>
415 <pre>
416 bic  rA,  #0xC000000F
417 blx  rA
418 </pre>
419 <p>This is used for indirect function calls&#8212;commonly seen in C++ programs
420 as virtual calls, but also for calling function pointers in C.</p>
421 <p>Note that both <em>indirect branch</em> pseudo-instructions use <code>bic</code>, rather
422 than the <code>tst</code> instruction we allow for <em>load</em> and <em>store</em>. There are
423 two reasons for this:</p>
424 <ol class="arabic simple">
425 <li>Conditional <em>branch</em> is very common. Much more common than
426 conditional <em>load</em> and <em>store</em>. If we supported an alternative
427 <code>tst</code>-based sequence for <em>branch</em>, it would be rare.</li>
428 <li>There&#8217;s no performance benefit to using <code>tst</code> here on modern ARM
429 chips. <em>Branch</em> consumes its operands later in the pipeline than
430 <em>load</em> and <em>store</em> (since they don&#8217;t have to generate an address,
431 etc) so this sequence doesn&#8217;t stall.</li>
432 </ol>
433 <aside class="note">
434 <p>At this point astute readers are wondering what the <code>x</code> in <code>bx</code>
435 and <code>blx</code> means. We told you it stood for &#8220;exchange&#8221;, but exchange
436 to what? ARM, for all the reduced-ness of its instruction set, can
437 change execution mode from A32 (ARM) to T32 (Thumb) and back with
438 these <em>branch</em> instructions, called <em>interworking branch</em>. Recall that
439 A32 instructions are 32-bit wide, and T32 instructions are a mix of
440 both 16-bit or 32-bit wide. The destination address given to a
441 <em>branch</em> therefore cannot sensibly have its bottom bit set in either
442 instruction set: that would be an unaligned instruction in both cases,
443 and ARM simply doesn&#8217;t support this. The bottom bit for the <em>indirect
444 branch</em> was therefore cleverly recycled by the ARM architecture to
445 mean &#8220;switch to T32 mode&#8221; when set!</p>
446 <p>As you&#8217;ve figured out by now, Native Client&#8217;s sandbox won&#8217;t be very
447 happy if A32 instructions were to be executed as T32 instructions: who
448 know what they correspond to?  A malicious person could craft valid
449 A32 code that&#8217;s actually very naughty T32 code, somewhat like forming
450 a sentence that happens to be valid in English and French but with
451 completely different meanings, complimenting the reader in one
452 language and insulting them in the other.</p>
453 <p>You&#8217;ve figured out by now that the bundle alignment restrictions of
454 the Native Client sandbox already take care of making this travesty
455 impossible: by masking off the bottom 4 bits of the destination the
456 interworking nature of ARM&#8217;s <em>indirect branch</em> is completely avoided.</p>
457
458 </aside>
459 <h5 id="call-and-return"><em>Call</em> and <em>Return</em></h5>
460 <p>On ARM, there is no <em>call</em> or <em>return</em> instruction. A <em>call</em> is simply a
461 <em>branch</em> that just happen to load a return address into <code>lr</code>, the link
462 register. If the called function is a leaf (that is, if it calls no
463 other functions before returning), it simply branches to the address
464 stored in <code>lr</code> to <em>return</em> to its caller:</p>
465 <pre>
466 bic  lr,  #0xC000000F
467 bx   lr
468 </pre>
469 <p>If the function called other functions, however, it had to spill <code>lr</code>
470 onto the stack. On x86, this is done implicitly, but it is explicit on
471 ARM:</p>
472 <pre>
473 push { lr }
474 ; Some code here...
475 pop  { lr }
476 bic  lr,  #0xC000000F
477 bx   lr
478 </pre>
479 <p>There are two things to note about this code.</p>
480 <ol class="arabic simple">
481 <li>As we mentioned before, we don&#8217;t allow arbitrary instructions to
482 write to the Program Counter, <code>pc</code>. Thus, while a traditional ARM
483 program might have popped directly into <code>pc</code> to end the function,
484 we require a pop into a register, followed by a pseudo-instruction.</li>
485 <li>Function returns really are just <em>indirect branch</em>, with the same
486 restrictions. This means that functions can only return to addresses
487 that are bundle-aligned: <code>0 mod 16</code>.</li>
488 </ol>
489 <p>The implication here is that a <em>call</em>&#8212;the <em>branch</em> that enters
490 functions&#8212;must be placed at the end of the bundle, so that the return
491 address they generate is <code>0 mod 16</code>. Otherwise, when we clear the
492 bottom four bits, the program would enter an infinite loop!  (Native
493 Client doesn&#8217;t try to prevent infinite loops, but the validator actually
494 does check the alignment of calls. This is because, when we were writing
495 the compiler, it was annoying to find out our calls were in the wrong
496 place by having the program run forever!)</p>
497 <aside class="note">
498 Properly balancing the CPU&#8217;s <em>call</em>/<em>return</em> actually allows it to
499 perform much better by allowing it to speculatively execute the return
500 address&#8217; code. For more information on ARM&#8217;s <em>call</em>/<em>return</em> stack see
501 ARM&#8217;s technical reference manual.
502 </aside>
503 <h4 id="literal-pools-and-data-bundles">Literal Pools and Data Bundles</h4>
504 <p>In the section where we described the ARM architecture, we mentioned
505 ARM&#8217;s unusual immediate forms. To restate:</p>
506 <ul class="small-gap">
507 <li>ARM instructions are fixed-length, 32-bits, so we can&#8217;t have an
508 instruction that includes an arbitrary 32-bit constant.</li>
509 <li>Many ARM instructions can include a modified immediate constant, which
510 is flexible, but limited.</li>
511 <li>For any other value (particularly addresses), ARM programs explicitly
512 load constants from inside the code itself.</li>
513 </ul>
514 <aside class="note">
515 ARMv7 introduces some instructions, <code>movw</code> and <code>movt</code>, that try to
516 address this by letting us directly load larger constants. Our
517 toolchain uses this capability in some cases.
518 </aside>
519 <p>Here&#8217;s a typical example of the use of a literal pool. ARM assemblers
520 typically hide the details&#8212;this is the sort of code you&#8217;d see produced
521 by a disassembler, but with more comments.</p>
522 <pre>
523 ; C equivalent: &quot;table[3] = 4&quot;
524 ; 'table' is a static array of bytes.
525 ldr   r0,  [pc, #124]    ; Load the address of the 'table',
526                          ; &quot;124&quot; is the offset from here
527                          ; to the constant below.
528 add   r0,  #3            ; Add the immediate array index.
529 mov   r1,  #4            ; Get the constant '4' into a register.
530 bic   r0,  #0xC0000000   ; Mask our array address.
531 strb  r1,  [r0]          ; Store one byte.
532 ; ...
533 .word table              ; Constant referenced above.
534 </pre>
535 <p>Because table is a static array, the compiler knew its address at
536 compile-time&#8212;but the address didn&#8217;t fit in a modified immediate. (Most
537 don&#8217;t).  So, instead of loading an immediate into <code>r0</code> with a <code>mov</code>,
538 we stashed the address in the code, generated its address using <code>pc</code>,
539 and loaded the constant. ARM compilers will typically group all the
540 embedded data together into a literal pool. These typically live just
541 past the end of functions, where they won&#8217;t be executed.</p>
542 <p>This is an important trick in ARM code, so it&#8217;s important to support it
543 in Native Client... but there&#8217;s a potential flaw. If we let programs
544 contain arbitrary data, mingled in with the code, couldn&#8217;t they hide
545 malicious instructions this way?</p>
546 <p>The answer is no, because the validator disassembles the entire
547 executable region of the program, without regard to whether the
548 programmer said a certain chunk was code or data. But this brings the
549 opposite problem: what if the program needs to contain a certain
550 constant that just happens to encode a malicious instruction?  We want
551 to allow this, but we have to be certain it will never be executed as
552 code!</p>
553 <h5 id="data-bundles-to-the-rescue">Data Bundles to the Rescue</h5>
554 <p>As we discussed in the last section, ARM code in Native Client is
555 structured in 16-byte bundles. We allow literal pools by putting them in
556 special bundles, called data bundles. Each data bundle can contain 12
557 bytes of arbitrary data, and the program can have as many data bundles
558 as it likes.</p>
559 <p>Each data bundle starts with a breakpoint instruction, <code>bkpt</code>. This
560 way, if an <em>indirect branch</em> tries to enter the data bundle, the process
561 will take a fault and the trusted runtime will intervene (by terminating
562 the program). For example:</p>
563 <pre>
564 .p2align 4
565 bkpt #0x5BE0          ; Must be aligned 0 mod 16!
566 .word 0xDEADBEEF      ; Arbitrary constants are A-OK.
567 svc #30               ; Trying to make a syscall? OK!
568 str r0, [r1]          ; Unmasked stores are fine too.
569 </pre>
570 <p>So, we have a way for programs to create an arbitrary, even dangerous,
571 chunk of data within their code. We can prevent <em>indirect branch</em> from
572 entering it. We can also prevent fall-through from the code just before
573 it, by the <code>bkpt</code>. But what about <em>direct branch</em> straight into the
574 middle?</p>
575 <p>The validator detects all data bundles (because this <code>bkpt</code> has a
576 special encoding) and marks them as off-limits for <em>direct branch</em>. If
577 it finds a <em>direct branch</em> into a data bundle, the entire program is
578 rejected as unsafe. Because <em>direct branch</em> cannot be modified at
579 runtime, the data bundles cannot be executed.</p>
580 <aside class="note">
581 Clever readers may wonder: why use <code>bkpt #0x5BE0</code>, that seems
582 awfully specific when you just need a special &#8220;roadblock&#8221; instruction!
583 Quite true, young Padawan! It happens that this odd <code>bkpt</code>
584 instruction is encoded as <code>0xE125BE70</code> in A32, and in T32 the
585 <code>bkpt</code> instruction is encoded as <code>0xBExx</code> (where <code>xx</code> could be
586 any 8-bit immediate, say <code>0x70</code>) and <code>0xE125</code> encodes the <em>branch</em>
587 instruction <code>b.n #0x250</code>. The special roadblock instruction
588 therefore doubles as a roadblock in T32, if anything were to go so
589 awry that we tried to execute it as a T32 instruction! Much defense,
590 such depth, wow!
591 </aside>
592 <h3 id="trampolines-and-memory-layout">Trampolines and Memory Layout</h3>
593 <p>So far, the rules we&#8217;ve described make for boring programs: they can&#8217;t
594 communicate with the outside world!</p>
595 <ul class="small-gap">
596 <li>The program can&#8217;t call an external library, or the operating system,
597 even to do something simple like draw some pixels on the screen.</li>
598 <li>It also can&#8217;t read or write memory outside of its dedicated sandbox,
599 so communicating that way is right out.</li>
600 </ul>
601 <p>We fix this by allowing the untrusted program to call into the trusted
602 runtime using a trampoline. A trampoline is simply a short stretch of
603 code, placed by the trusted runtime at a known location within the
604 sandbox, that is permitted to do things the untrusted program can&#8217;t.</p>
605 <p>Even though trampolines are inside the sandbox, the untrusted program
606 can&#8217;t modify them: the trusted runtime marks them read-only. It also
607 can&#8217;t do anything clever with the special instructions inside the
608 trampoline&#8212;for example, call it at a slightly offset address to bypass
609 some checks&#8212;because the validator only allows trampolines to be
610 reached by <em>indirect branch</em> (or <em>branch-with-link</em>). We structure the
611 trampolines carefully so that they&#8217;re safe to enter at any <code>0 mod 16</code>
612 address.</p>
613 <p>The validator can detect attempts to use the trampolines because they&#8217;re
614 loaded at a fixed location in memory. Let&#8217;s look at the memory map of
615 the Native Client sandbox.</p>
616 <h4 id="memory-map">Memory Map</h4>
617 <p>The ARM sandbox is always at virtual address <code>0</code>, and is exactly 1GiB
618 in size. This includes the untrusted program&#8217;s code and data, the
619 trampolines, and a small guard region to detect null pointer
620 dereferences. In practice, the untrusted program takes up a bit more
621 room than this, because of the need for additional guard regions at
622 either end of the sandbox.</p>
623 <table border="1" class="docutils">
624 <colgroup>
625 </colgroup>
626 <thead valign="bottom">
627 <tr class="row-odd"><th class="head">Address</th>
628 <th class="head">Size</th>
629 <th class="head">Name</th>
630 <th class="head">Purpose</th>
631 </tr>
632 </thead>
633 <tbody valign="top">
634 <tr class="row-even"><td><code>-0x2000</code></td>
635 <td>8KiB</td>
636 <td>Bottom Guard</td>
637 <td>Keeps negative-displacement <em>load</em> or <em>store</em> from escaping.</td>
638 </tr>
639 <tr class="row-odd"><td><code>0</code></td>
640 <td>64KiB</td>
641 <td>Null Guard</td>
642 <td>Catches null pointer dereferences, guards against kernel exploits.</td>
643 </tr>
644 <tr class="row-even"><td><code>0x10000</code></td>
645 <td>64KiB</td>
646 <td>Trampolines</td>
647 <td>Up to 2048 unique syscall entry points.</td>
648 </tr>
649 <tr class="row-odd"><td><code>0x20000</code></td>
650 <td>~1GiB</td>
651 <td>Untrusted Sandbox</td>
652 <td>Contains untrusted code, followed by its heap/stack/memory.</td>
653 </tr>
654 <tr class="row-even"><td><code>0x40000000</code></td>
655 <td>8KiB</td>
656 <td>Top Guard</td>
657 <td>Keeps positive-displacement <em>load</em> or <em>store</em> from escaping.</td>
658 </tr>
659 </tbody>
660 </table>
661 <p>Within the trampolines, the untrusted program can call any address
662 that&#8217;s <code>0 mod 16</code>. However, only even slots are used, so useful
663 trampolines are always <code>0 mod 32</code>. If the program calls an odd slot,
664 it will fault, and the trusted runtime will shut it down.</p>
665 <aside class="note">
666 This is a bit of speculative flexibility. While the current bundle
667 size of Native Client on ARM is 16 bytes, we&#8217;ve considered the
668 possibility of optional 32-byte bundles, to enable certain compiler
669 improvements. While this option isn&#8217;t available to untrusted programs
670 today, we&#8217;re trying to keep the system &#8220;32-byte clean&#8221;.
671 </aside>
672 <h4 id="inside-a-trampoline">Inside a Trampoline</h4>
673 <p>When we introduced trampolines, we mentioned that they can do things
674 that untrusted programs can&#8217;t. To be more specific, trampolines can jump
675 to locations outside the sandbox. On ARM, this is all they do. Here&#8217;s a
676 typical trampoline fragment on ARM:</p>
677 <pre>
678 ; Even trampoline bundle:
679 push  { r0-r3 }     ; Save arguments that may be in registers.
680 push  { lr }        ; Save the untrusted return address,
681                     ; separate step because it must be on top.
682 ldr   r0,  [pc, #4] ; Load the destination address from
683                     ; the next bundle.
684 blx   r0            ; Go!
685 ; The odd trampoline that immediately follows:
686 bkpt 0x5be0         ; Prevent entry to this data bundle.
687 .word address_of_routine
688 </pre>
689 <p>The only odd thing here is that we push the incoming value of <code>lr</code>,
690 and then use <code>blx</code>&#8212;not <code>bx</code>&#8212;to escape the sandbox. This is
691 because, in practice, all trampolines jump to the same routine in the
692 trusted runtime, called the syscall hook. It uses the return address
693 produced by the final <code>blx</code> instruction to determine which trampoline
694 was called.</p>
695 <h3 id="loose-ends">Loose Ends</h3>
696 <h4 id="forbidden-instructions">Forbidden Instructions</h4>
697 <p>To complete the sandbox, the validator ensures that the program does not
698 try to use certain forbidden instructions.</p>
699 <ul class="small-gap">
700 <li>We forbid instructions that directly interact with the operating
701 system by going around the trusted runtime. We prevent this to limit
702 the functionality of the untrusted program, and to ensure portability
703 across operating systems.</li>
704 <li>We forbid instructions that change the processor&#8217;s execution mode to
705 Thumb, ThumbEE, or Jazelle. This would cause the code to be
706 interpreted differently than the validator&#8217;s original 32-bit ARM
707 disassembly, so the validator results might be invalidated.</li>
708 <li>We forbid instructions that aren&#8217;t available to user code (i.e. have
709 to be used by an operating system kernel). This is purely out of
710 paranoia, because the hardware should prevent the instructions from
711 working. Essentially, we consider it &#8220;suspicious&#8221; if a program
712 contains these instructions&#8212;it might be trying to exploit a hardware
713 bug.</li>
714 <li>We forbid instructions, or variants of instructions, that are
715 implementation-defined (&#8220;unpredictable&#8221;) or deprecated in the ARMv7-A
716 architecture manual.</li>
717 <li>Finally, we forbid a small number of instructions, such as <code>setend</code>,
718 purely out of paranoia. It&#8217;s easier to loosen the validator&#8217;s
719 restrictions than to tighten them, so we err on the side of rejecting
720 safe instructions.</li>
721 </ul>
722 <p>If an instruction can&#8217;t be decoded at all within the ARMv7-A instruction
723 set specification, it is forbidden.</p>
724 <aside class="note">
725 <p>Here is a list of instructions currently forbidden for security
726 reasons (that is, excluding deprecated or undefined instructions):</p>
727 <ul class="small-gap">
728 <li><code>BLX</code> (immediate): always changes to Thumb mode.</li>
729 <li><code>BXJ</code>: always changes to Jazelle mode.</li>
730 <li><code>CPS</code>: not available to user code.</li>
731 <li><code>LDM</code>, exception return version: not available to user code.</li>
732 <li><code>LDM</code>, kernel version: not available to user code.</li>
733 <li><code>LDR*T</code> (unprivileged load operations): theoretically harmless,
734 but suspicious when found in user code. Use <code>LDR</code> instead.</li>
735 <li><code>MSR</code>, kernel version: not available to user code.</li>
736 <li><code>RFE</code>: not available to user code.</li>
737 <li><code>SETEND</code>: theoretically harmless, but suspicious when found in
738 user code. May make some future validator extensions difficult.</li>
739 <li><code>SMC</code>: not available to user code.</li>
740 <li><code>SRS</code>: not available to user code.</li>
741 <li><code>STM</code>, kernel version: not available to user code.</li>
742 <li><code>STR*T</code> (unprivileged store operations): theoretically harmless,
743 but suspicious when found in user code. Use <code>STR</code> instead.</li>
744 <li><code>SVC</code>/<code>SWI</code>: allows direct operating system interaction.</li>
745 <li>Any unassigned hint instruction: difficult to reason about, so
746 treated as suspicious.</li>
747 </ul>
748 <p>More details are available in the <a class="reference external" href="http://src.chromium.org/viewvc/native_client/trunk/src/native_client/src/trusted/validator_arm/armv7.table">ARMv7 instruction table definition</a>.</p>
749
750 </aside>
751 <h4 id="coprocessors">Coprocessors</h4>
752 <p>ARM has traditionally added new instruction set features through
753 coprocessors. Coprocessors are accessed through a small set of
754 instructions, and often have their own register files. Floating point
755 and the NEON vector extensions are both implemented as coprocessors, as
756 is the MMU.</p>
757 <p>We&#8217;re confident that the side-effects of coprocessors in slots 10 and 11
758 (that is, floating point, NEON, etc.) are well-understood. These are in
759 the coprocessor space reserved by ARM Ltd. for their own extensions
760 (<code>CP8</code>&#8211;<code>CP15</code>), and are unlikely to change significantly. So, we
761 allow untrusted code to use coprocessors 10 and 11, and we mandate the
762 presence of at least VFPv3 and NEON/AdvancedSIMD. Multiprocessor
763 Extension, VFPv4, FP16 and other extensions are allowed but not
764 required, and may fail on processors that do not support them, it is
765 therefore the program&#8217;s responsibility to validate their availability
766 before executing them.</p>
767 <p>We don&#8217;t allow access to any other ARM-reserved coprocessor
768 (<code>CP8</code>&#8211;<code>CP9</code> or <code>CP12</code>&#8211;<code>CP15</code>). It&#8217;s possible that read
769 access to <code>CP15</code> might be useful, and we might allow it in the
770 future&#8212;but again, it&#8217;s easier to loosen the restrictions than tighten
771 them, so we ban it for now.</p>
772 <p>We do not, and probably never will, allow access to the vendor-specific
773 coprocessor space, <code>CP0</code>&#8211;<code>CP7</code>. We&#8217;re simply not confident in our
774 ability to model the operations on these coprocessors, given that
775 vendors often leave them poorly-specified. Unfortunately this eliminates
776 some legacy floating point and vector implementations, but these are
777 superceded on ARMv7-A parts anyway.</p>
778 <h4 id="validator-code">Validator Code</h4>
779 <p>By now you&#8217;re itching to see the sandbox validator&#8217;s code and dissect
780 it. You&#8217;ll have a disapointing read: at less that 500 lines of code
781 <a class="reference external" href="http://src.chromium.org/viewvc/native_client/trunk/src/native_client/src/trusted/validator_arm/validator.cc">validator.cc</a>
782 is quite simple to understand and much shorter than this document. It&#8217;s
783 of course dependent on the <a class="reference external" href="http://src.chromium.org/viewvc/native_client/trunk/src/native_client/src/trusted/validator_arm/armv7.table">ARMv7 instruction table definition</a>,
784 which teaches it about the ARMv7 instruction set.</p>
785 </section>
786
787 {{/partials.standard_nacl_article}}