website/_site/2022/11/19/cheri.html

331 lines
36 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<!DOCTYPE html>
<!--
  l、
゙(゚、 。
 l、゙ ~ヽ
 じしf_, )
-->
<html lang=" en-US">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta charset="utf-8">
<title>CHERI - jack bond-preston</title>
<link rel="stylesheet" href="/assets/css/main.css">
<link rel="stylesheet" href="/assets/css/pygments.css">
</head>
<body>
<div class="article">
<h1><a href="/2022/11/19/cheri.html">CHERI</a></h1>
<h2 id="preamble">preamble<a href="#preamble" class="header-link">[<img src="/assets/images/link.svg" />]</a></h2>
<p><a href="https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/">CHERI</a> is an acronym for Capability Hardware Enhanced RISC Instructions. it is a security-focussed project aimed at improving memory protection at the hardware level. the project is complex and it has many potential applications.</p>
<p>in this article I will go into some basics to give an understanding behind some changes that CHERI makes to how programs execute and are written. this will be focussed almost entirely in C, as this is where my experience lies - it is also where some of the effects of CHERI are most easily felt.this article is going to be a <em>very simplistic</em> introduction to CHERI, and Im going to attempt to explain the basics behind everything I cover. a basic understanding of C will be beneficial.</p>
<p><strong><em>note:</em></strong> <a href="https://www.arm.com/architecture/cpu/morello">the Morello platform</a> is an evaluation board produced by <a href="https://www.arm.com/">Arm</a> to provide a physical implementation of CHERI extending <a href="https://en.wikipedia.org/wiki/AArch64">the Arm AArch64 ISA</a>. I previously worked on this platform at Arm, <a href="https://git.morello-project.org/morello/musl-libc/">porting the musl C library to Morello</a>. implementations for CHERI that are worth looking into from a more open perspective <a href="https://www.cl.cam.ac.uk/techreports/UCAM-CL-TR-951.pdf"> are the MIPS (chapter 4) and RISC-V (chapter 5) ones</a>. Morello is the only implementation that exists in a true hard core format, afaik - but this is obviously hard to obtain so youll just be playing around with emulators/models anyway.</p>
<h2 id="memory-safety-bugs">memory safety bugs<a href="#memory-safety-bugs" class="header-link">[<img src="/assets/images/link.svg" />]</a></h2>
<p>to first understand how CHERI tries to fix some simple issues, lets first look at some simplified examples of issues that arise when we arent using a CHERI-based architecture.</p>
<h3 id="a-simple-memory-safety-bug">a simple memory safety bug</h3>
<p>lets take a look at this C code:</p>
<figure class="highlight"><pre><code class="language-c" data-lang="c"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
</pre></td><td class="code"><pre><span class="cp">#include</span> <span class="cpf">&lt;stdio.h&gt;</span><span class="cp">
</span>
<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
<span class="kt">char</span> <span class="n">my_perfect_string</span><span class="p">[]</span> <span class="o">=</span> <span class="s">"what a beautiful string"</span><span class="p">;</span> <span class="c1">// so beautiful, I sure hope no-one touches it</span>
<span class="kt">char</span> <span class="n">user_name</span><span class="p">[</span><span class="mi">32</span><span class="p">];</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"enter your name: "</span><span class="p">);</span>
<span class="n">fgets</span><span class="p">(</span><span class="n">user_name</span><span class="p">,</span> <span class="mi">1000</span><span class="p">,</span> <span class="n">stdin</span><span class="p">);</span> <span class="c1">// get user's name from stdin</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"hello %s"</span><span class="p">,</span> <span class="n">user_name</span><span class="p">);</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"my_perfect_string: %s</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="n">my_perfect_string</span><span class="p">);</span>
<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre></td></tr></tbody></table></code></pre></figure>
<p>now lets try using our new program:</p>
<figure class="highlight"><pre><code class="language-console" data-lang="console"><span class="gp">$</span><span class="w"> </span>./membug
<span class="go">enter your name: jack
hello jack
my_perfect_string: what a beautiful string</span></code></pre></figure>
<p>works on my machine boss! code review +1, and merged… until our good friend <a href="https://en.wikipedia.org/wiki/Hubert_Blaine_Wolfeschlegelsteinhausenbergerdorff_Sr.">Hubert Blaine Wolfeschlegelsteinhausenbergerdorff Sr.</a> comes along. he emails me a strangeerror hes seen:</p>
<figure class="highlight"><pre><code class="language-console" data-lang="console"><span class="gp">$</span><span class="w"> </span>./membug
<span class="go">enter your name: Hubert Blaine Wolfeschlegelsteinhausenbergerdorff Sr.
hello Hubert Blaine Wolfeschlegelsteinhausenbergerdorff Sr.
my_perfect_string: hausenbergerdorff Sr.</span></code></pre></figure>
<p>thats not supposed to happen! his name has spilled over into our <code class="language-plaintext highlighter-rouge">my_perfect_string[]</code> array! turns out our issue is that when we use <code class="language-plaintext highlighter-rouge">fgets()</code>, weve set the second parameter, <code class="language-plaintext highlighter-rouge">size</code>, to <code class="language-plaintext highlighter-rouge">1000</code> - but our <code class="language-plaintext highlighter-rouge">user_name[32]</code> array c1593an only fit 32 characters (and the last of these should be a null terminator, so 31 usable characters).</p>
<p><code class="language-plaintext highlighter-rouge">fgets</code> fills up <code class="language-plaintext highlighter-rouge">user_name</code>, but it hasnt finished with the name yet! it doesnt care (or know) that <code class="language-plaintext highlighter-rouge">user_name</code> is full, its just going to keep going until it finishes our user input, or reads 999 characters from standard input. and thus it keeps mindlessly writing, overwriting the memory weve used to store our precious perfect string (which happens to be immediately after <code class="language-plaintext highlighter-rouge">user_name</code>). lets take a look at the stack in GDB to see why this happens:</p>
<figure class="highlight"><pre><code class="language-plaintext" data-lang="plaintext">(gdb) b memdebug.c:7
(gdb) run
Breakpoint 1, main () at membug.c:7
7 printf("enter your name: ");
(gdb) n
8 fgets(user_name, 1000, stdin); // get user's name from stdin
(gdb) n
9 printf("hello %s", user_name);
(gdb) x/56bc $sp
0x7fffffffdbf0: 106 'j' 97 'a' 99 'c' 107 'k' 10 '\n' 0 '\000' 0 '\000' 0 '\000'
0x7fffffffdbf8: 77 'M' 82 'R' 85 'U' 85 'U' 85 'U' 85 'U' 0 '\000' 0 '\000'
0x7fffffffdc00: -24 '\350' -78 '\262' -5 '\373' -9 '\367' -1 '\377' 127 '\177' 0 '\000' 0 '\000'
0x7fffffffdc08: 0 '\000' 82 'R' 85 'U' 85 'U' 85 'U' 85 'U' 0 '\000' 0 '\000'
0x7fffffffdc10: 119 'w' 104 'h' 97 'a' 116 't' 32 ' ' 97 'a' 32 ' ' 98 'b'
0x7fffffffdc18: 101 'e' 97 'a' 117 'u' 116 't' 105 'i' 102 'f' 117 'u' 108 'l'
0x7fffffffdc20: 32 ' ' 115 's' 116 't' 114 'r' 105 'i' 110 'n' 103 'g' 0 '\000'</code></pre></figure>
<p>we can see our two character arrays are right next to each other on the stack (<code class="language-plaintext highlighter-rouge">user_name</code> contains some gibberish as it is not zero-initialised).</p>
<p><strong><em>note:</em></strong> this code was compiled with <code class="language-plaintext highlighter-rouge">-fno-stack-protector</code> to reproduce this behaviour. compilers have certain techniques like this which can help protect against such attacks, but there are often ways around these by using less primitive attacks.</p>
<p>okay, its a pretty easy fix, we just need to change the <code class="language-plaintext highlighter-rouge">fgets(char *s, int size, FILE *stream)</code> parameter <code class="language-plaintext highlighter-rouge">size</code> to <code class="language-plaintext highlighter-rouge">32</code>.</p>
<p><strong><em>note:</em></strong> you may initially think “why not 31? dont we need to save a character for the null byte at the end?”. thankfully, <code class="language-plaintext highlighter-rouge">fgets</code> does this for us. excerpt from <code class="language-plaintext highlighter-rouge">man fgets</code>:</p>
<blockquote>
<p>“fgets() reads in <em>at most one less than size</em> characters from stream and stores them into the buffer pointed to by s […] A terminating null byte (\0) is stored after the last character in the buffer”.</p>
</blockquote>
<p>this is a good question to be asking though, being careful is key when it comes to these kinds of things.</p>
<h3 id="why-hardware">why hardware?</h3>
<p>okay, so thats an easy fix. why are we talking about doing anything in hardware here? just write the code correctly! the issue is code gets very complex, and this is a very simplistic situation. some memory safety bugs can be incredibly complicated and go unnoticed for decades. the C language especially gives the programmer many, many opportunities to make mistakes - and it only takes one to be a problem. a lot of the software we are using these days is based on stacks upon stacks of software written in different languages, and there are going to be bugs in there. CHERI should give us some protection “for free” (its not this simple, in actuality).</p>
<p>some languages (e.g. Rust) are going to offer you strong memory safety guarantees at compile-time, but thats not the topic of this article. the differences between doing this kind of protection in software or hardware (or both) is more complex than the scope of this article. in addition, CHERIs benefits are more wide in breadth than just protecting against this kind of issue.</p>
<h2 id="pointers-recap">pointers recap<a href="#pointers-recap" class="header-link">[<img src="/assets/images/link.svg" />]</a></h2>
<p>lets quickly recap a basic idea of what a pointer is. were going to ignore things like <a href="https://en.wikipedia.org/wiki/Virtual_memory">virtual memory</a> for brevity. we can think of a pointer in a normal 64-bit architecture (e.g. AArch64) simply as a 64-bit unsigned value that holds the memory address of something we care about. this is a simplification (as are most things), but it can help us reason about the general idea:</p>
<figure class="highlight"><pre><code class="language-c" data-lang="c"><span class="kt">int</span> <span class="n">val</span> <span class="o">=</span> <span class="mi">1593</span><span class="p">;</span>
<span class="kt">int</span> <span class="o">*</span><span class="n">x</span> <span class="o">=</span> <span class="o">&amp;</span><span class="n">val</span><span class="p">;</span> <span class="c1">// x points to val</span></code></pre></figure>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1920 314"><defs><style>.prefix__prefix__d{fill:none;stroke-miterlimit:10}.prefix__prefix__f,.prefix__prefix__h,.prefix__prefix__i{font-size:24px}.prefix__prefix__f,.prefix__prefix__h,.prefix__prefix__k{fill:#fcfcfc}.prefix__prefix__f,.prefix__prefix__l{font-family:Source Code Pro}.prefix__prefix__d{stroke:gray;stroke-width:4px}.prefix__prefix__h,.prefix__prefix__m{font-family:Source Code Pro;font-weight:700}.prefix__prefix__i{fill:gray}</style></defs><g id="prefix__prefix__a"><path fill="#0c1114" d="M0 0h1920v314H0z" /><text class="prefix__prefix__h" transform="translate(577.46 133.41)"><tspan x="0" y="0">int *x</tspan></text><text class="prefix__prefix__f" transform="translate(490.97 177.1)"><tspan x="0" y="0">0x0000010000000004</tspan></text><path d="M481.16 206v18.5M760.5 206v18.5m-279 0h279" stroke="#fcfcfc" fill="none" stroke-miterlimit="10" stroke-linecap="square" stroke-width="3" /><text transform="translate(578.78 241.33)" font-size="20" font-family="Source Code Pro" fill="#fcfcfc"><tspan x="0" y="0">address</tspan></text><path stroke-width="4" stroke="#fcfcfc" fill="none" stroke-miterlimit="10" d="M752 171h204.56" /><path class="prefix__prefix__k" d="M948.64 182.62L992 171.01l-43.36-11.63v23.24z" /><text transform="translate(1272.76 177.16)" fill="#fcfcfc" font-size="24"><tspan class="prefix__prefix__m" x="0" y="0">mem[</tspan><tspan class="prefix__prefix__l" x="57.6" y="0">0x0000010000000004</tspan><tspan class="prefix__prefix__m" x="316.79" y="0">]</tspan></text><text class="prefix__prefix__i" transform="translate(1272.76 133.16)"><tspan class="prefix__prefix__m" x="0" y="0">mem[</tspan><tspan class="prefix__prefix__l" x="57.6" y="0">0x0000010000000000</tspan><tspan class="prefix__prefix__m" x="316.79" y="0">]</tspan></text><text class="prefix__prefix__i" transform="translate(1271.76 224.16)"><tspan class="prefix__prefix__m" x="0" y="0">mem[</tspan><tspan class="prefix__prefix__l" x="57.6" y="0">0x0000010000000008</tspan><tspan class="prefix__prefix__m" x="316.79" y="0">]</tspan></text></g><g id="prefix__prefix__b"><path class="prefix__prefix__d" d="M1260 58v48H985V58" /><path d="M1258 195v40H987v-40h271m4-4H983v48h279v-48zm-4-84v40H987v-40h271m4-4H983v48h279v-48z" fill="gray" /><path class="prefix__prefix__k" d="M756.16 150.93v40h-271v-40h271m4-4h-279v48h279v-48zM1258 151v40H987v-40h271m4-4H983v48h279v-48z" /><text class="prefix__prefix__f" transform="translate(1094 177.09)"><tspan x="0" y="0">1593</tspan></text><text class="prefix__prefix__h" transform="translate(1007.6 45.16)"><tspan x="0" y="0">memory (as ints)</tspan></text><path class="prefix__prefix__d" d="M1260 284v-48H985v48" /></g></svg>
<p>and on these normal architectures, this pointer generally is just a number. we can do weird things with it, treating it as a number…</p>
<figure class="highlight"><pre><code class="language-c" data-lang="c"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
</pre></td><td class="code"><pre><span class="cp">#include</span> <span class="cpf">&lt;stdio.h&gt;</span><span class="cp">
</span>
<span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span>
<span class="kt">int</span> <span class="n">magic</span> <span class="o">=</span> <span class="mi">9999</span><span class="p">;</span>
<span class="p">(</span><span class="kt">void</span><span class="p">)</span><span class="n">magic</span><span class="p">;</span>
<span class="kt">int</span> <span class="n">arr</span><span class="p">[]</span> <span class="o">=</span> <span class="p">{</span> <span class="mi">1234</span><span class="p">,</span> <span class="mi">5678</span> <span class="p">};</span>
<span class="kt">int</span> <span class="o">*</span><span class="n">x</span> <span class="o">=</span> <span class="o">&amp;</span><span class="p">(</span><span class="n">arr</span><span class="p">[</span><span class="mi">0</span><span class="p">]);</span> <span class="c1">// x is a pointer to first element of arr</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"*x=%d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="o">*</span><span class="n">x</span><span class="p">);</span>
<span class="kt">unsigned</span> <span class="kt">long</span> <span class="n">x_addr</span> <span class="o">=</span> <span class="p">(</span><span class="kt">size_t</span><span class="p">)</span> <span class="n">x</span><span class="p">;</span> <span class="c1">// we're going to assume size_t = unsigned long here</span>
<span class="n">x_addr</span> <span class="o">+=</span> <span class="mi">4</span><span class="p">;</span> <span class="c1">// sizeof(int) == 4</span>
<span class="n">x</span> <span class="o">=</span> <span class="p">(</span><span class="kt">int</span> <span class="o">*</span><span class="p">)</span> <span class="n">x_addr</span><span class="p">;</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"*x=%d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="o">*</span><span class="n">x</span><span class="p">);</span>
<span class="n">x_addr</span> <span class="o">+=</span> <span class="mi">4</span><span class="p">;</span>
<span class="n">x</span> <span class="o">=</span> <span class="p">(</span><span class="kt">int</span> <span class="o">*</span><span class="p">)</span> <span class="n">x_addr</span><span class="p">;</span>
<span class="n">printf</span><span class="p">(</span><span class="s">"*x=%d</span><span class="se">\n</span><span class="s">"</span><span class="p">,</span> <span class="o">*</span><span class="n">x</span><span class="p">);</span>
<span class="k">return</span> <span class="mi">0</span><span class="p">;</span>
<span class="p">}</span>
</pre></td></tr></tbody></table></code></pre></figure>
<p>…and this code will often still work:</p>
<figure class="highlight"><pre><code class="language-console" data-lang="console"><span class="gp">$</span><span class="w"> </span>./ptrs_as_numbers
<span class="go">*x=1234
*x=5678
*x=9999</span></code></pre></figure>
<p>yikes! now, when you start messing with pointers like this, youre bound to run into a bunch of undefined behaviour. but C programmers write undefined behaviour all the time, and my computer executes this program fine without complaining at all. doesnt it feel a bit weird that we can take a pointer to <code class="language-plaintext highlighter-rouge">arr[0]</code> and modify it to load <code class="language-plaintext highlighter-rouge">secret</code>? theyre not even part of the same array…</p>
<h2 id="introducting-capabilities">introducting capabilities<a href="#introducting-capabilities" class="header-link">[<img src="/assets/images/link.svg" />]</a></h2>
<p>CHERI introduces capabilities, which can be thought of as an extension to pointers. they still store an address of something we care about, but they have extra information too! in a 64-bit system, a pointer would typically be a 64-bit value (as dicussed previously). the corresponding capability in a CHERI platform is 128 bits (or 129 bits if you look at it a certain way, more about that later…).</p>
<p>as you might have guessed, this “extra information” takes up 64 bits of the capability. bits are assigned to three key pieces of metadata: <em>bounds</em>, <em>permissions</em>, and <em>object type</em>. there is also an additional 1-bit <em>tag</em> which is stored out-of-band: it is not a 129-bit value - instead each 128-bit capability can be thought of as being associated with a 1-bit validity tag. the architecture manages this. the diagram below is provided as a rough overview of this. note that it is not to scale.</p>
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 1920 314"><defs><style>.prefix__c{fill:none;stroke:#fcfcfc;stroke-linecap:square;stroke-miterlimit:10;stroke-width:3px}.prefix__f,.prefix__g{fill:#fcfcfc}.prefix__f{font-family:Source Code Pro;font-size:20px}</style></defs><g id="prefix__a"><path fill="#0c1114" d="M0 0h1920v314H0z" /><text transform="translate(101.86 232.41)" font-family="Source Code Pro" font-weight="700" fill="#fcfcfc" font-size="24"><tspan x="0" y="0">int *x (capability)</tspan></text><text transform="translate(1205.97 232.1)" font-family="Source Code Pro" fill="#fcfcfc" font-size="24"><tspan x="0" y="0">0x0000010000000004</tspan></text><path class="prefix__c" d="M1016 261v18.5M1656 261v18.5M1016 279.5h640" /><text class="prefix__f" transform="translate(1293.78 296.33)"><tspan x="0" y="0">address</tspan></text><path class="prefix__c" d="M700 191.5V173M1020 191.5V173M700 173h320" /><text class="prefix__f" transform="translate(823.78 167.74)"><tspan x="0" y="0">bounds</tspan></text><path class="prefix__c" d="M554 260.34v18.5M704 260.34v18.5M554 278.84h150" /><text class="prefix__f" transform="translate(562.78 295.68)"><tspan x="0" y="0">object type</tspan></text><g><path class="prefix__c" d="M391.89 191.56v-18.5M541.89 191.56v-18.5M391.89 173.06h150" /></g><text class="prefix__f" transform="translate(400.67 167.8)"><tspan x="0" y="0">permissions</tspan></text><text class="prefix__f" transform="translate(304.67 31.07)"><tspan x="0" y="0">tag (out-of-band)</tspan></text><g><path class="prefix__c" d="M391.33 55.92v-18.5M421.33 55.92v-18.5M391.33 37.42h30" /></g></g><g id="prefix__b"><path class="prefix__g" d="M1651.66 205.93v40h-632v-40h632m4-4h-640v48h640v-48z" /><path class="prefix__g" d="M1016 206v40H704v-40h312m4-4H700v48h320v-48z" /><path class="prefix__g" d="M700 206v40H558v-40h142m4-4H554v48h150v-48z" /><path class="prefix__g" d="M554 206v40h-12v-40h12m4-4h-20v48h20v-48z" /><path class="prefix__g" d="M538 206v40H396v-40h142m4-4H392v48h150v-48zM418.5 70v40h-22V70h22m4-4h-30v48h30V66z" /></g></svg>
<p>I am mostly going to focus on <em>bounds</em> in this article, as it is not too difficult to grasp, and the impact is fairly easy to demonstrate for some simple examples. the bounds represent an upper and lower bound on the memory region (address space) that this capability is allowed to access. if we try to use the capability to access some address outside of this range, the hardware will throw a fault - it simply wont let us do this!</p>
<p><strong><em>note:</em></strong> it is important to note that I am going to oversimplify the way the bounds are stored in this article. this especially includes the diagram above. in reality, there is a complex compression method, necessitated by the range and sizes required by bounds. this depends on the address value, alignment, etc. for now, we shouldnt need to think about this much, just know it will be managed for us. the key take-away from this is that <em>bounds cant always be 100% precise for all addresses and ranges</em>.</p>
<p>can you imagine how we can use bounds to prevent our previous memory safety bug from occurring? the key is that we can set the bounds on the capability pointing to <code class="language-plaintext highlighter-rouge">user_name</code> which we pass to <code class="language-plaintext highlighter-rouge">fgets</code>, such that the capability may only access the contents of the array. this means that when <code class="language-plaintext highlighter-rouge">fgets</code> tries to write past the end of the <code class="language-plaintext highlighter-rouge">user_name</code> array, the processor will throw a <em>capability fault</em>, and execution of our program will cease.</p>
<p>the idea behind CHERI is that we dont have to set up these bounds ourselves. this is something the compiler can generate code for. the compiler knows that the <code class="language-plaintext highlighter-rouge">user_name</code> array has a length of <code class="language-plaintext highlighter-rouge">32</code>, and can set the bounds accordingly on capabilities created that point to it. lets try it…</p>
<h2 id="playing-with-cheri-risc-v">playing with CHERI RISC-V<a href="#playing-with-cheri-risc-v" class="header-link">[<img src="/assets/images/link.svg" />]</a></h2>
<p>unless youre lucky enough to have access to a physical Morello board, there is the issue of actually using a CHERI implementation. for this article I will be making use of the <a href="https://en.wikipedia.org/wiki/QEMU">QEMU</a> emulator to emulate a <a href="https://en.wikipedia.org/wiki/RISC-V">RISC-V</a> CHERI environment. running <a href="https://www.cheribsd.org/">CheriBSD</a> on this emulator will allow us to have a nice <a href="https://www.freebsd.org/">FreeBSD</a>-based capability-enabled environment to play around with. Ill use <a href="https://github.com/CTSRD-CHERI/cheribuild">cheribuild</a> to easily get set up (the <code class="language-plaintext highlighter-rouge">cheribuild.py</code> step will take a very long time the first time):</p>
<figure class="highlight"><pre><code class="language-console" data-lang="console"><span class="gp">$</span><span class="w"> </span><span class="nb">sudo </span>apt <span class="nb">install </span>autoconf automake libtool pkg-config clang bison cmake <span class="se">\</span>
<span class="go">ninja-build samba flex texinfo time libglib2.0-dev libpixman-1-dev \
libarchive-dev libarchive-tools libbz2-dev libattr1-dev libcap-ng-dev
</span><span class="gp">$</span><span class="w"> </span>git clone git@github.com:CTSRD-CHERI/cheribuild
<span class="gp">$</span><span class="w"> </span><span class="nb">cd </span>cheribuild
<span class="gp">$</span><span class="w"> </span>./cheribuild.py <span class="nt">--include-dependencies</span> <span class="nt">--run</span>/ssh-forwarding-port 2222 run-riscv64-purecap
<span class="go">CheriBSD/riscv (cheribsd-riscv64-purecap) (ttyu0)
login: root
</span><span class="gp">root@cheribsd-riscv64-purecap:~ #</span></code></pre></figure>
<p>now we have our shell inside our CheriBSD emulated platform, we can start to try things out. lets compile our <code class="language-plaintext highlighter-rouge">membug</code> program again, this time with the toolchain targetting CheriBSD RISC-V - this will have been built as part of the dependencies already. once its built, we can <code class="language-plaintext highlighter-rouge">scp</code> it over to the CheriBSD filesystem, as we set up the SSH forwarding port to
<code class="language-plaintext highlighter-rouge">1111</code>.</p>
<figure class="highlight"><pre><code class="language-console" data-lang="console"><span class="gp">#</span><span class="w"> </span>on a separate terminal on your host machine
<span class="gp">$</span><span class="w"> </span>~/cheri/output/sdk/utils/cheribsd-riscv64-purecap-clang membug.c <span class="nt">-Wall</span> <span class="nt">-g</span> <span class="nt">-fno-stack-protector</span> <span class="nt">-o</span> membug-cheribsd
<span class="gp">$</span><span class="w"> </span>scp <span class="nt">-P</span> 2222 ./membug-cheribsd root@localhost:~/</code></pre></figure>
<p>and now we can see what happens when we explore our bug with CHERI:</p>
<figure class="highlight"><pre><code class="language-console" data-lang="console"><span class="gp">$</span>./membug-cheribsd
<span class="go">enter your name: jack
hello jack
my_perfect_string: what a beautiful string
</span><span class="gp">$</span><span class="w"> </span>./membug-cheribsd
<span class="go">enter your name: Hubert Blaine Wolfeschlegelsteinhausenbergerdorff Sr.
In-address space security exception (core dumped)</span></code></pre></figure>
<p>its working! we are getting a capability fault as we exceed the bounds of the
<code class="language-plaintext highlighter-rouge">user_name</code> capability bounds. we can use gdb to verify this is caused by the bounds fault:</p>
<figure class="highlight"><pre><code class="language-plaintext" data-lang="plaintext"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre>(gdb) run
Starting program: /root/membug-cheribsd
enter your name: Hubert Blaine Wolfeschlegelsteinhausenbergerdorff Sr.
Program received signal SIGPROT, CHERI protection violation.
Capability bounds fault caused by register ca6.
0x0000000040314ce8 in memcpy (dst0=0x3fffdfff44, src0=&lt;optimized out&gt;, length=54) at /home/jack/cheri/cheribsd/lib/libc/string/bcopy.c:143
(gdb) p $ca6
$1 = () 0x3fffdfff78 [rwRW,0x3fffdfff44-0x3fffdfff64]
</pre></td></tr></tbody></table></code></pre></figure>
<p>as we can see, the bounds for our <code class="language-plaintext highlighter-rouge">user_name</code> capability (which is stored in capability register <code class="language-plaintext highlighter-rouge">ca6</code>) are <code class="language-plaintext highlighter-rouge">0x3fffdfff44-0x3fffdfff64</code>, but the address is <code class="language-plaintext highlighter-rouge">0x3fffdfff78</code>. this is out of the bounds allowed by the capability, so the architecture throws a fault. if we look at the assembly generated by the compiler, we can see it set our capability bounds to a size of 32 to enforce this behaviour:</p>
<figure class="highlight"><pre><code class="language-armasm" data-lang="armasm"><table class="rouge-table"><tbody><tr><td class="gutter gl"><pre class="lineno">1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
</pre></td><td class="code"><pre><span class="nl">0000000000001ce8</span> <span class="err">&lt;</span><span class="nb">main</span><span class="err">&gt;:</span>
<span class="c">; int main() {</span>
<span class="nb">cincoffset</span> <span class="nv">csp</span><span class="o">,</span> <span class="nv">csp</span><span class="o">,</span> <span class="o">-</span><span class="mi">160</span>
<span class="nb">csc</span> <span class="nv">cra</span><span class="o">,</span> <span class="mi">144</span> <span class="o">(</span><span class="nv">csp</span><span class="o">)</span>
<span class="nb">csc</span> <span class="nv">cs0</span><span class="o">,</span> <span class="mi">128</span> <span class="o">(</span><span class="nv">csp</span><span class="o">)</span>
<span class="nb">cincoffset</span> <span class="nv">cs0</span><span class="o">,</span> <span class="nv">csp</span><span class="o">,</span> <span class="mi">160</span>
<span class="nb">cincoffset</span> <span class="nv">ca0</span><span class="o">,</span> <span class="nv">cs0</span><span class="o">,</span> <span class="o">-</span><span class="mi">36</span>
<span class="nb">csetbounds</span> <span class="nv">ca2</span><span class="o">,</span> <span class="nv">ca0</span><span class="o">,</span> <span class="mi">4</span>
<span class="nb">cincoffset</span> <span class="nv">ca0</span><span class="o">,</span> <span class="nv">cs0</span><span class="o">,</span> <span class="o">-</span><span class="mi">60</span>
<span class="nb">csetbounds</span> <span class="nv">ca0</span><span class="o">,</span> <span class="nv">ca0</span><span class="o">,</span> <span class="mi">24</span>
<span class="nb">csc</span> <span class="nv">ca0</span><span class="o">,</span> <span class="o">-</span><span class="mi">128</span> <span class="o">(</span><span class="nv">cs0</span><span class="o">)</span>
<span class="nb">cincoffset</span> <span class="nv">ca1</span><span class="o">,</span> <span class="nv">cs0</span><span class="o">,</span> <span class="o">-</span><span class="mi">92</span>
<span class="nb">csetbounds</span> <span class="nv">ca1</span><span class="o">,</span> <span class="nv">ca1</span><span class="o">,</span> <span class="mi">32</span>
<span class="nb">csc</span> <span class="nv">ca1</span><span class="o">,</span> <span class="o">-</span><span class="mi">144</span> <span class="o">(</span><span class="nv">cs0</span><span class="o">)</span>
<span class="nb">mv</span> <span class="nv">a1</span><span class="o">,</span> <span class="nv">zero</span>
<span class="nb">csd</span> <span class="nv">a1</span><span class="o">,</span> <span class="o">-</span><span class="mi">104</span> <span class="o">(</span><span class="nv">cs0</span><span class="o">)</span>
<span class="nb">csw</span> <span class="nv">a1</span><span class="o">,</span> <span class="mi">0</span> <span class="o">(</span><span class="nv">ca2</span><span class="o">)</span>
</pre></td></tr></tbody></table></code></pre></figure>
<h3 id="capability-monotonicity">capability monotonicity</h3>
<p>at this point you may be thinking “okay, thats great, but if we can just set the bounds of a capability with an instruction then whats the point? surely I can just set global bounds on some random pointer and access whatever I want?”</p>
<p>fundamental to the idea of capabilities is their <em>provenance</em> and <em>monotonicity</em>. simply put, the first says we can only construct a capability using specific instructions, from an existing capability. we cant just create a capability from some random number. lets see what happens when we try to run our <code class="language-plaintext highlighter-rouge">ptrs_as_numbers</code> program on CheriBSD:</p>
<figure class="highlight"><pre><code class="language-plaintext" data-lang="plaintext">(gdb) runStarting program: /root/ptrs_as_numbers-cheribsd
*x=1234
Program received signal SIGPROT, CHERI protection violation.Capability tag fault caused by register ca1.0x0000000000101c66 in main () at ptrs_as_numbers.c:1414 printf("*x=%d\n", *x);
(gdb) p $ca1
$1 = () 0x3fffdfff74</code></pre></figure>
<p>we can see we get a fault - the tag isnt set. any capability with a tag not set to 1 cannot be dereferenced - it is invalid. in fact, this capability has no capability metadata - when we copied it into our <code class="language-plaintext highlighter-rouge">unsigned long</code>, we just copied the 64-bit address.</p>
<p><em>monotonicity</em> is what stops us taking an existing capability, and creating a capability with more permissions and/or access than the original. it stipulates that when we create a capability from another capability (which we have to do - provenance), the permissions and bounds of the new capability must be equal to or less than the original. so our bounds can only get narrower as we create new capabilites from an existing capability. this means that capabilities trace back in a chain - they are all created from other capabilities, and narrowed as necessary. in this case, (simplified) when the kernel loads our program it will give us capabilities that are wide enough to do everything we need to do, and the compiler will try and make sure all the capabilities that we make and use from these are as tightly bound and unpermissive as possible.</p>
<h3 id="cheri-fying-code">CHERI-fying code</h3>
<p>youll notice we got a lot of these benefits “for free”. we only had to recompile our code, and we got this extra security. of course, CHERI does require changes to programs. naturally, the compiler had to be changed a lot to implement this behaviour. it also especially requires changes to things like the C library and kernel in order to take advantage of the features fully. sufficiently large userspace programs do need changes too. one common issue is that a lot of existing C code assumes that <code class="language-plaintext highlighter-rouge">sizeof (*void) == sizeof(size_t)</code>. with CHERI, our pointers are now twice as big. however, <code class="language-plaintext highlighter-rouge">size_t</code> hasnt changed size, as the address space size hasnt changed - for example, if we index into an array with <code class="language-plaintext highlighter-rouge">size_t</code>, the index should still be the same size; the extra data in our <code class="language-plaintext highlighter-rouge">void *</code> capability is the metadata, not extra address data. any program that tries to convert from some <code class="language-plaintext highlighter-rouge">unsigned long</code> or <code class="language-plaintext highlighter-rouge">size_t</code> to a capability will fault - this violates provenance. so, sometimes code changes have to be made to ensure we are keeping the capability metadata around.</p>
<h2 id="epilogue">epilogue<a href="#epilogue" class="header-link">[<img src="/assets/images/link.svg" />]</a></h2>
<p>I appreciate this has been a fragmented and surface level introduction to CHERI. hopefully it has provided some education in some basic aims of CHERI regardless. potential benefits and uses for CHERI go much deeper than anything Ive touched on here, so please, read more about everything - and get your hands dirty trying out messing about with qemu and CheriBSD!</p>
<p>here are some links to check out:</p>
<ul>
<li><a href="https://www.cl.cam.ac.uk/research/security/ctsrd/cheri/">CHERI homepage @ CUCL</a></li>
<li><a href="https://www.cl.cam.ac.uk/techreports/UCAM-CL-TR-941.pdf">technical report: An Introduction to CHERI</a></li>
<li><a href="https://www.cl.cam.ac.uk/techreports/UCAM-CL-TR-947.pdf">technical report: CHERI C/C++ Programming Guide</a></li>
<li><a href="https://www.cl.cam.ac.uk/techreports/UCAM-CL-TR-951.pdf">technical report: CHERI ISAv8</a></li>
<li><a href="https://www.arm.com/architecture/cpu/morello">Morello homepage @ Arm</a></li>
<li><a href="https://developer.arm.com/documentation/ddi0606/latest">Morello Architecture Reference Manual @ Arm</a></li>
</ul>
<hr>
<p><a href="mailto:jackbondpreston@outlook.com">email me</a> to have a conversation</p>
</div>
</body>
</html>