<?xml version="1.0" encoding="UTF-8"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
    <title></title>
    <link rel="self" type="application/atom+xml" href="https://jackbondpreston.me/atom.xml"/>
    <link rel="alternate" type="text/html" href="https://jackbondpreston.me"/>
    <generator uri="https://www.getzola.org/">Zola</generator>
    <updated>2025-12-18T00:00:00+00:00</updated>
    <id>https://jackbondpreston.me/atom.xml</id>
    <entry xml:lang="en">
        <title>unique hanzi extractor</title>
        <published>2025-12-18T00:00:00+00:00</published>
        <updated>2025-12-18T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jackbondpreston.me/unique-hanzi/"/>
        <id>https://jackbondpreston.me/unique-hanzi/</id>
        
        <content type="html" xml:base="https://jackbondpreston.me/unique-hanzi/">&lt;style&gt;
    div#output-box, textarea#input-box {
        width: 100%;
        min-height: 30vh;
        padding: 10px;
        font-size: 1rem;
    }

    div#output-box {
        border: 2px solid rgb(252, 169, 184);
    }
&lt;&#x2F;style&gt;
&lt;script src=&quot;&#x2F;js&#x2F;hanzi.js&quot; defer&gt;&lt;&#x2F;script&gt;
&lt;p&gt;when learning Mandarin Chinese, one may want to extract the unique characters from a block of text. for example, one could take an article and create worksheets using the &lt;a href=&quot;https:&#x2F;&#x2F;mandarinminutes.com&#x2F;worksheets&quot;&gt;Mandarin Minutes worksheet tool&lt;&#x2F;a&gt; in order to practice writing all the unique characters used within said text.&lt;&#x2F;p&gt;
&lt;p&gt;the tool below allows this - input the text into the top box, and the bottom box will contain the list of unique characters. the tool makes a best effort to filter out non-useful characters like punctuation and spacing. the tool should also work with other languages. for non-logographical languages it should be largely uninteresting; for any sufficiently large text the output will just consist of the whole alphabet in lower and upper case.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;input&quot;&gt;input&lt;a href=&quot;#input&quot; aria-label=&quot;Anchor link for: input&quot; class=&quot;header-link&quot;&gt;[&lt;img src=&quot;&#x2F;images&#x2F;link.svg&quot; &#x2F;&gt;]&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;textarea id=&quot;input-box&quot;&gt;&lt;&#x2F;textarea&gt;
&lt;h2 id=&quot;output&quot;&gt;output&lt;a href=&quot;#output&quot; aria-label=&quot;Anchor link for: output&quot; class=&quot;header-link&quot;&gt;[&lt;img src=&quot;&#x2F;images&#x2F;link.svg&quot; &#x2F;&gt;]&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;div id=&quot;output-box&quot;&gt;&lt;&#x2F;div&gt;</content>
        
    </entry>
    <entry xml:lang="en">
        <title>DPDK Summit 2024: OpenSSL Crypto PMD - Analysis and Optimisations</title>
        <published>2024-10-23T00:00:00+00:00</published>
        <updated>2024-10-23T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jackbondpreston.me/dpdk-summit-2024/"/>
        <id>https://jackbondpreston.me/dpdk-summit-2024/</id>
        
        <content type="html" xml:base="https://jackbondpreston.me/dpdk-summit-2024/">&lt;h2 id=&quot;summary&quot;&gt;summary&lt;a href=&quot;#summary&quot; aria-label=&quot;Anchor link for: summary&quot; class=&quot;header-link&quot;&gt;[&lt;img src=&quot;&#x2F;images&#x2F;link.svg&quot; &#x2F;&gt;]&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;this year I had the opportunity to present my recent work on &lt;a href=&quot;https:&#x2F;&#x2F;www.dpdk.org&#x2F;&quot;&gt;DPDK&lt;&#x2F;a&gt;. the work consists of performance analysis and optimisations to the OpenSSL crypto poll mode driver (PMD). I identified several major performance flaws with the implementation, and integration with OpenSSL, and upstreamed changes to address these. the talk goes through the details of the findings, performance impacts of the changes, and discussions around future work.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;recording&quot;&gt;recording&lt;a href=&quot;#recording&quot; aria-label=&quot;Anchor link for: recording&quot; class=&quot;header-link&quot;&gt;[&lt;img src=&quot;&#x2F;images&#x2F;link.svg&quot; &#x2F;&gt;]&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;iframe width=&quot;560&quot; height=&quot;315&quot; src=&quot;https:&#x2F;&#x2F;www.youtube.com&#x2F;embed&#x2F;sPhktvMBYhs?si=UInBMrAUxs2R_raf&quot; title=&quot;YouTube video player&quot; frameborder=&quot;0&quot; allow=&quot;accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share&quot; referrerpolicy=&quot;strict-origin-when-cross-origin&quot; allowfullscreen&gt;&lt;&#x2F;iframe&gt;
&lt;h2 id=&quot;slides&quot;&gt;slides&lt;a href=&quot;#slides&quot; aria-label=&quot;Anchor link for: slides&quot; class=&quot;header-link&quot;&gt;[&lt;img src=&quot;&#x2F;images&#x2F;link.svg&quot; &#x2F;&gt;]&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;a href=&quot;&#x2F;pdf&#x2F;dpdksummit2024.pdf&quot;&gt;pdf of the slides used for the talk&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>Linux syscall hooking</title>
        <published>2024-09-26T00:00:00+00:00</published>
        <updated>2024-09-26T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jackbondpreston.me/syscalls/"/>
        <id>https://jackbondpreston.me/syscalls/</id>
        
        <content type="html" xml:base="https://jackbondpreston.me/syscalls/">&lt;p&gt;sometimes, you might want to hook Linux syscalls from a binary. it&#x27;s probably not for a very elegant reason, but I&#x27;m not here to judge. in this article we discuss a few options for doing this. these will be focussed on x86 platforms running Linux, but many of these techniques should also be possible on other ISAs, with appropriate changes.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;binary-rewriting&quot;&gt;binary rewriting&lt;a href=&quot;#binary-rewriting&quot; aria-label=&quot;Anchor link for: binary-rewriting&quot; class=&quot;header-link&quot;&gt;[&lt;img src=&quot;&#x2F;images&#x2F;link.svg&quot; &#x2F;&gt;]&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;one way to approach the problem is simply to rewrite binaries at runtime. one can set executable text pages to be writeable, swap out some instructions, and then set them back to be read-only and executable. how can this look in practice?&lt;&#x2F;p&gt;
&lt;p&gt;one has to consider the limitations of binary rewriting in this manner. we can only replace syscall instructions with instructions of the same length. thus, we have limited flexibility in how to implement a syscall hooking function. absolute calls to some custom handler address would require further code modification than is possible, in order to prepare a register with the jump address. we can do relative jumps, but we need to find some space within the maximum range to put our handler function.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;zpoline&quot;&gt;zpoline&lt;&#x2F;h3&gt;
&lt;p&gt;zpoline&lt;sup&gt;&lt;a href=&quot;#ref1&quot; style=&quot;text-decoration: none;&quot;&gt;[1]&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; is one approach to get around these limitations, on x86 platforms. zpoline replaces all &lt;code&gt;syscall&lt;&#x2F;code&gt;&#x2F;&lt;code&gt;sysenter&lt;&#x2F;code&gt; instructions with &lt;code&gt;call *%rax&lt;&#x2F;code&gt;. this takes advantage of two things:&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;code&gt;rax&lt;&#x2F;code&gt; will always contain the target syscall number, thus holding a bounded low number. this allows us to rewrite the virtual memory in the range &lt;code&gt;[0x00, (SYSCALL_MAX + n)]&lt;&#x2F;code&gt; to have some custom handler code of length &lt;code&gt;n&lt;&#x2F;code&gt;.&lt;&#x2F;li&gt;
&lt;li&gt;&lt;code&gt;call *%rax&lt;&#x2F;code&gt; is the same size as &lt;code&gt;syscall&lt;&#x2F;code&gt; (2 bytes)&lt;sup&gt;&lt;a href=&quot;#ref2&quot; style=&quot;text-decoration: none;&quot;&gt;[2]&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;.
this means we can &lt;code&gt;mmap(0)&lt;&#x2F;code&gt; at the start of runtime, and create fall-through nops, followed by a jump to a syscall hook function. the performance of this should be pretty good, not much different to adding an extra function call to each syscall, which is all we want to do anyway.&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;okay, but is it ok to &lt;code&gt;mmap()&lt;&#x2F;code&gt; the zero virtual address? well, not really. for starters, you probably can&#x27;t do it as a non-privileged user. Linux has a setting &lt;code&gt;mmap_min_addr&lt;&#x2F;code&gt;, which disallows any &lt;code&gt;mmap()&lt;&#x2F;code&gt;s below a given address&lt;sup&gt;&lt;a href=&quot;#ref3&quot; style=&quot;text-decoration: none;&quot;&gt;[3]&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;. secondly, this can cause certain null-pointer bugs to exhibit weird behaviour, instead of throwing segmentation faults.&lt;&#x2F;p&gt;
&lt;p&gt;thus, this option does work, and may be suitable for certain usecases, but it does have large flaws (like all of the options).&lt;&#x2F;p&gt;
&lt;h3 id=&quot;custom-trampolines&quot;&gt;custom trampolines&lt;&#x2F;h3&gt;
&lt;p&gt;an alternative that doesn&#x27;t involve &lt;code&gt;mmap()&lt;&#x2F;code&gt;ing the zero page is used by AMD&#x27;s Onload&lt;sup&gt;&lt;a href=&quot;#ref4&quot; style=&quot;text-decoration: none;&quot;&gt;[4]&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; - try to replace&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;b8 NN NN NN NN          mov    eax,0xN         ; N = syscall number
&lt;&#x2F;span&gt;&lt;span&gt;0f 05                   syscall
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;with&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;b8 TT TT TT TT          mov    eax,0xTTTTTTTT  ; 0xTTTTTTTT = trampoline func address
&lt;&#x2F;span&gt;&lt;span&gt;ff d0                   call   rax
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;where &lt;code&gt;0xTTTTTTTT&lt;&#x2F;code&gt; is the address of some trampoline function we have crafted. we can get an address to put this function at by using &lt;code&gt;mmap(..., MMAP_32BIT)&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;another option here is to try to get some virtual memory within a certain distance from the syscall code, and use relative call instructions.&lt;&#x2F;p&gt;
&lt;p&gt;note that this method is not as reliable. it relies on being able to find syscall-performing code which can be modified in this way, which is not guaranteed. thus, it could fail in some situations. it does circumvent the requirement for an ugly &lt;code&gt;mmap(0)&lt;&#x2F;code&gt;, however.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;syscall-user-dispatch-sud&quot;&gt;syscall user dispatch (SUD)&lt;a href=&quot;#syscall-user-dispatch-sud&quot; aria-label=&quot;Anchor link for: syscall-user-dispatch-sud&quot; class=&quot;header-link&quot;&gt;[&lt;img src=&quot;&#x2F;images&#x2F;link.svg&quot; &#x2F;&gt;]&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;SUD&lt;sup&gt;&lt;a href=&quot;#ref5&quot; style=&quot;text-decoration: none;&quot;&gt;[5]&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; is probably the most &quot;correct&quot; way to go about doing this. this kernel feature was initially created for Wine. SUD allows the userspace program to request the kernel traps all syscalls, raising a &lt;code&gt;SIGSYS&lt;&#x2F;code&gt; signal every time. a certain range of program memory can be excluded from this trapping, and a flag can be used to toggle trapping off or on at runtime (although the excluded range will never trap, regardless of this flag).&lt;&#x2F;p&gt;
&lt;p&gt;this is is enabled via &lt;code&gt;prctl()&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;prctl&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PR_SET_SYSCALL_USER_DISPATCH&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PR_SYS_DISPATCH_ON&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;unsigned long &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;off&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;unsigned long &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;size&lt;&#x2F;span&gt;&lt;span&gt;, int8_t *&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;switch&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;prctl&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PR_SET_SYSCALL_USER_DISPATCH&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;PR_SYS_DISPATCH_OFF&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;L&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;L&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;L&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;where &lt;code&gt;off&lt;&#x2F;code&gt; is the start of the non-trapping memory region, and &lt;code&gt;size&lt;&#x2F;code&gt; is it&#x27;s length. &lt;code&gt;switch&lt;&#x2F;code&gt; is a pointer to the aforementioned toggleable flag.&lt;&#x2F;p&gt;
&lt;p&gt;here is a very basic example of using SUD:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#define &lt;&#x2F;span&gt;&lt;span&gt;_GNU_SOURCE
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;signal.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdbool.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stddef.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdlib.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;string.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;sys&#x2F;prctl.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;sys&#x2F;syscall.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;sys&#x2F;types.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;bool&lt;&#x2F;span&gt;&lt;span&gt; selector = SYSCALL_DISPATCH_FILTER_ALLOW;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;static volatile void &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;handle_sigsys&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sig&lt;&#x2F;span&gt;&lt;span&gt;, siginfo_t *&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;info&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span&gt;*&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;ucontext&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt; buf[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;128&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; len;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; ret;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    len = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;snprintf&lt;&#x2F;span&gt;&lt;span&gt;(buf, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1024&lt;&#x2F;span&gt;&lt;span&gt;, &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;Caught syscall with number 0x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, info-&amp;gt;si_syscall);
&lt;&#x2F;span&gt;&lt;span&gt;    selector = SYSCALL_DISPATCH_FILTER_ALLOW;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;write&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;, buf, len);
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void&lt;&#x2F;span&gt;&lt;span&gt;) {
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; err;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;* install SIGSYS signal handler *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;struct&lt;&#x2F;span&gt;&lt;span&gt; sigaction act;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;memset&lt;&#x2F;span&gt;&lt;span&gt;(&amp;amp;act, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, sizeof(act));
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    sigset_t mask;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sigemptyset&lt;&#x2F;span&gt;&lt;span&gt;(&amp;amp;mask);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    act.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sa_sigaction &lt;&#x2F;span&gt;&lt;span&gt;= handle_sigsys;
&lt;&#x2F;span&gt;&lt;span&gt;    act.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sa_flags &lt;&#x2F;span&gt;&lt;span&gt;= SA_SIGINFO;
&lt;&#x2F;span&gt;&lt;span&gt;    act.&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sa_mask &lt;&#x2F;span&gt;&lt;span&gt;= mask;
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(err = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sigaction&lt;&#x2F;span&gt;&lt;span&gt;(SIGSYS, &amp;amp;act, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;NULL&lt;&#x2F;span&gt;&lt;span&gt;)) {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;sigaction failed: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, err);
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;exit&lt;&#x2F;span&gt;&lt;span&gt;(-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;* enable SUD *&#x2F;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;if &lt;&#x2F;span&gt;&lt;span&gt;(err = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;prctl&lt;&#x2F;span&gt;&lt;span&gt;(PR_SET_SYSCALL_USER_DISPATCH, PR_SYS_DISPATCH_ON, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;, &amp;amp;selector)) {
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;prctl failed: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, err);
&lt;&#x2F;span&gt;&lt;span&gt;        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;exit&lt;&#x2F;span&gt;&lt;span&gt;(-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;span&gt;    }
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    selector = SYSCALL_DISPATCH_FILTER_BLOCK;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;syscall&lt;&#x2F;span&gt;&lt;span&gt;(SYS_write);
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;running this will produce the following:&lt;&#x2F;p&gt;
&lt;pre style=&quot;background-color:#2b303b;color:#c0c5ce;&quot;&gt;&lt;code&gt;&lt;span&gt;Caught syscall with number 0x38
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;where syscall number &lt;code&gt;0x38&lt;&#x2F;code&gt; is &lt;code&gt;SYS_write&lt;&#x2F;code&gt; (on my system).&lt;&#x2F;p&gt;
&lt;blockquote class=&quot;note&quot;&gt;
    
    &lt;div class=&quot;icon&quot;&gt;
        
&lt;?xml version=&quot;1.0&quot; ?&gt;&lt;svg height=&quot;48&quot; viewBox=&quot;0 0 48 48&quot; width=&quot;48&quot; xmlns=&quot;http:&#x2F;&#x2F;www.w3.org&#x2F;2000&#x2F;svg&quot;&gt;&lt;path d=&quot;M22 34h4v-12h-4v12zm2-30c-11.05 0-20 8.95-20 20s8.95 20 20 20 20-8.95 20-20-8.95-20-20-20zm0 36c-8.82 0-16-7.18-16-16s7.18-16 16-16 16 7.18 16 16-7.18 16-16 16zm-2-22h4v-4h-4v4z&quot; fill=&quot;currentColor&quot;&#x2F;&gt;&lt;&#x2F;svg&gt;
    &lt;&#x2F;div&gt;
    &lt;div class=&quot;content&quot;&gt;
        
        &lt;p&gt;&lt;strong&gt;note&lt;&#x2F;strong&gt;&lt;&#x2F;p&gt;
        
        &lt;p&gt;we&#x27;ve had to disable syscall hooking within &lt;code&gt;handle_sigsys()&lt;&#x2F;code&gt;, before we perform the &lt;code&gt;write&lt;&#x2F;code&gt; syscall. this could also be achieved using the offset exclusion mechanism, with a caveat: SUD also needs to be disabled at signal handler return. this is because, depending on some factors, the signal handler will return to a signal trampoline, which exists somewhere else in memory (VDSO or libc). this trampoline will then perform the &lt;code&gt;sigreturn&lt;&#x2F;code&gt; syscall, which - if you had only excluded the handler function memory itself - would cause us to raise a &lt;code&gt;SIGSYS&lt;&#x2F;code&gt; inside a &lt;code&gt;SIGSYS&lt;&#x2F;code&gt; handler (oops).&lt;&#x2F;p&gt;

    &lt;&#x2F;div&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;SUD should have pretty good performance, although it is still switching from user space to kernel space (on syscall), and back again (on SIGSYS handling) - unlike the binary rewriting methods.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;rewriting-the-kernel-syscall-table&quot;&gt;rewriting the kernel syscall table&lt;a href=&quot;#rewriting-the-kernel-syscall-table&quot; aria-label=&quot;Anchor link for: rewriting-the-kernel-syscall-table&quot; class=&quot;header-link&quot;&gt;[&lt;img src=&quot;&#x2F;images&#x2F;link.svg&quot; &#x2F;&gt;]&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;Linux kernel modules could also try to patch the kernel&#x27;s syscall table.&lt;&#x2F;p&gt;
&lt;p&gt;it used to be the case that if Linux is configured with &lt;code&gt;CONFIG_KALLSYMS_ALL=y&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;shell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-shell &quot;&gt;&lt;code class=&quot;language-shell&quot; data-lang=&quot;shell&quot;&gt;&lt;span&gt;$ zgrep CONFIG_KALLSYMS_ALL &#x2F;proc&#x2F;config.gz
&lt;&#x2F;span&gt;&lt;span&gt;CONFIG_KALLSYMS_ALL=y
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;then we can find the address of the syscall table in &lt;code&gt;kallsyms&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;shell&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-shell &quot;&gt;&lt;code class=&quot;language-shell&quot; data-lang=&quot;shell&quot;&gt;&lt;span&gt;$ sudo grep sys_call_table &#x2F;proc&#x2F;kallsyms
&lt;&#x2F;span&gt;&lt;span&gt;ffffffff854017e0 D sys_call_table
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;and thus programmatically:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void &lt;&#x2F;span&gt;&lt;span&gt;*sys_call_table = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;kallsyms_lookup_name&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;sys_call_table&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;once the syscall table address has been obtained, the handlers for specific syscall(s) could then be overwritten, in order to hook them.
however, this kind of symbol lookup was generally considered very naughty, and &lt;code&gt;kallsyms_lookup_name()&lt;&#x2F;code&gt; was unexported in Linux 5.7&lt;sup&gt;&lt;a href=&quot;#ref6&quot; style=&quot;text-decoration: none;&quot;&gt;[6]&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;. there should be some feasible alternatives to do the same thing, but they would be slower, uglier, and more painful.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;modifying-ia32-lstar-msr&quot;&gt;modifying IA32_LSTAR MSR&lt;a href=&quot;#modifying-ia32-lstar-msr&quot; aria-label=&quot;Anchor link for: modifying-ia32-lstar-msr&quot; class=&quot;header-link&quot;&gt;[&lt;img src=&quot;&#x2F;images&#x2F;link.svg&quot; &#x2F;&gt;]&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;another method which has been used successfully&lt;sup&gt;&lt;a href=&quot;#ref7&quot; style=&quot;text-decoration: none;&quot;&gt;[7]&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt; involves rewriting the x86 MSR &lt;code&gt;IA32_LSTAR&lt;&#x2F;code&gt;. this register holds the destination address which the CPU will load into &lt;code&gt;RIP&lt;&#x2F;code&gt; when taking a syscall&lt;sup&gt;&lt;a href=&quot;#ref2&quot; style=&quot;text-decoration: none;&quot;&gt;[2]&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;. one can write a kernel module which modifies this register to contain a custom syscall handler address. this syscall handler function can hook syscalls before dispatching as normal (or not).&lt;&#x2F;p&gt;
&lt;p&gt;now that &lt;code&gt;kallsysms_lookup_name()&lt;&#x2F;code&gt; is no longer exported, this method also becomes problematic. as the example kernel module in &lt;a href=&quot;#ref7&quot; style=&quot;text-decoration: none;&quot;&gt;[7]&lt;&#x2F;a&gt; demonstrates, we still need to be able to grab some kernel symbol addresses in order to correctly implement the dispatcher.&lt;&#x2F;p&gt;
&lt;p&gt;however, this does propose another idea for implementing the previous method: using &lt;code&gt;IA32_LSTAR&lt;&#x2F;code&gt; to ascertain the address of the Linux syscall handler, instead of kallsyms. indeed, Onload has also exploited this method as a workaround for the unexporting of &lt;code&gt;kallsyms_lookup_name()&lt;&#x2F;code&gt;&lt;sup&gt;&lt;a href=&quot;#ref8&quot; style=&quot;text-decoration: none;&quot;&gt;[8]&lt;&#x2F;a&gt;&lt;&#x2F;sup&gt;.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;citations&quot;&gt;citations&lt;a href=&quot;#citations&quot; aria-label=&quot;Anchor link for: citations&quot; class=&quot;header-link&quot;&gt;[&lt;img src=&quot;&#x2F;images&#x2F;link.svg&quot; &#x2F;&gt;]&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p id=&quot;ref1&quot;&gt;[1] &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;www.usenix.org&amp;#x2F;conference&amp;#x2F;atc23&amp;#x2F;presentation&amp;#x2F;yasukata&quot;&gt;https:&amp;#x2F;&amp;#x2F;www.usenix.org&amp;#x2F;conference&amp;#x2F;atc23&amp;#x2F;presentation&amp;#x2F;yasukata&lt;&#x2F;a&gt; &lt;a href=&quot;javascript:history.back()&quot; style=&quot;text-decoration: none;&quot;&gt;(↑)&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;&lt;p id=&quot;ref2&quot;&gt;[2] &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;www.intel.com&amp;#x2F;content&amp;#x2F;www&amp;#x2F;us&amp;#x2F;en&amp;#x2F;developer&amp;#x2F;articles&amp;#x2F;technical&amp;#x2F;intel-sdm.html&quot;&gt;https:&amp;#x2F;&amp;#x2F;www.intel.com&amp;#x2F;content&amp;#x2F;www&amp;#x2F;us&amp;#x2F;en&amp;#x2F;developer&amp;#x2F;articles&amp;#x2F;technical&amp;#x2F;intel-sdm.html&lt;&#x2F;a&gt; &lt;a href=&quot;javascript:history.back()&quot; style=&quot;text-decoration: none;&quot;&gt;(↑)&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;&lt;p id=&quot;ref3&quot;&gt;[3] &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;elixir.bootlin.com&amp;#x2F;linux&amp;#x2F;v6.11&amp;#x2F;source&amp;#x2F;arch&amp;#x2F;s390&amp;#x2F;mm&amp;#x2F;mmap.c#L137&quot;&gt;https:&amp;#x2F;&amp;#x2F;elixir.bootlin.com&amp;#x2F;linux&amp;#x2F;v6.11&amp;#x2F;source&amp;#x2F;arch&amp;#x2F;s390&amp;#x2F;mm&amp;#x2F;mmap.c#L137&lt;&#x2F;a&gt; &lt;a href=&quot;javascript:history.back()&quot; style=&quot;text-decoration: none;&quot;&gt;(↑)&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;&lt;p id=&quot;ref4&quot;&gt;[4] &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;Xilinx-CNS&amp;#x2F;onload&amp;#x2F;blob&amp;#x2F;d5984bf52fcfba0cd75df8a1f8828f3363f6e164&amp;#x2F;src&amp;#x2F;lib&amp;#x2F;transport&amp;#x2F;unix&amp;#x2F;fdtable.c#L311&quot;&gt;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;Xilinx-CNS&amp;#x2F;onload&amp;#x2F;blob&amp;#x2F;d5984bf52fcfba0cd75df8a1f8828f3363f6e164&amp;#x2F;src&amp;#x2F;lib&amp;#x2F;transport&amp;#x2F;unix&amp;#x2F;fdtable.c#L311&lt;&#x2F;a&gt; &lt;a href=&quot;javascript:history.back()&quot; style=&quot;text-decoration: none;&quot;&gt;(↑)&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;&lt;p id=&quot;ref5&quot;&gt;[5] &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;docs.kernel.org&amp;#x2F;admin-guide&amp;#x2F;syscall-user-dispatch.html&quot;&gt;https:&amp;#x2F;&amp;#x2F;docs.kernel.org&amp;#x2F;admin-guide&amp;#x2F;syscall-user-dispatch.html&lt;&#x2F;a&gt; &lt;a href=&quot;javascript:history.back()&quot; style=&quot;text-decoration: none;&quot;&gt;(↑)&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;&lt;p id=&quot;ref6&quot;&gt;[6] &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;git.kernel.org&amp;#x2F;pub&amp;#x2F;scm&amp;#x2F;linux&amp;#x2F;kernel&amp;#x2F;git&amp;#x2F;torvalds&amp;#x2F;linux.git&amp;#x2F;commit&amp;#x2F;?id=0bd476e6c67190b5eb7b6e105c8db8ff61103281&quot;&gt;https:&amp;#x2F;&amp;#x2F;git.kernel.org&amp;#x2F;pub&amp;#x2F;scm&amp;#x2F;linux&amp;#x2F;kernel&amp;#x2F;git&amp;#x2F;torvalds&amp;#x2F;linux.git&amp;#x2F;commit&amp;#x2F;?id=0bd476e6c67190b5eb7b6e105c8db8ff61103281&lt;&#x2F;a&gt; &lt;a href=&quot;javascript:history.back()&quot; style=&quot;text-decoration: none;&quot;&gt;(↑)&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;&lt;p id=&quot;ref7&quot;&gt;[7] &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;vvdveen.com&amp;#x2F;data&amp;#x2F;lstar.txt&quot;&gt;https:&amp;#x2F;&amp;#x2F;vvdveen.com&amp;#x2F;data&amp;#x2F;lstar.txt&lt;&#x2F;a&gt; &lt;a href=&quot;javascript:history.back()&quot; style=&quot;text-decoration: none;&quot;&gt;(↑)&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;&lt;p id=&quot;ref8&quot;&gt;[8] &lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;Xilinx-CNS&amp;#x2F;onload&amp;#x2F;blob&amp;#x2F;083e5959ef76632ae3cd3d4356fb079ce0c570d1&amp;#x2F;src&amp;#x2F;driver&amp;#x2F;linux_resource&amp;#x2F;syscall_x86.c#L347&quot;&gt;https:&amp;#x2F;&amp;#x2F;github.com&amp;#x2F;Xilinx-CNS&amp;#x2F;onload&amp;#x2F;blob&amp;#x2F;083e5959ef76632ae3cd3d4356fb079ce0c570d1&amp;#x2F;src&amp;#x2F;driver&amp;#x2F;linux_resource&amp;#x2F;syscall_x86.c#L347&lt;&#x2F;a&gt; &lt;a href=&quot;javascript:history.back()&quot; style=&quot;text-decoration: none;&quot;&gt;(↑)&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;</content>
        
    </entry>
    <entry xml:lang="en">
        <title>学中文学了一年了</title>
        <published>2024-09-23T00:00:00+00:00</published>
        <updated>2024-09-23T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jackbondpreston.me/xue-zhong-wen-xue-liao-yi-nian-liao/"/>
        <id>https://jackbondpreston.me/xue-zhong-wen-xue-liao-yi-nian-liao/</id>
        
        <content type="html" xml:base="https://jackbondpreston.me/xue-zhong-wen-xue-liao-yi-nian-liao/">&lt;p&gt;大约一年前，我开始认真地学习中文。因此，我觉得我应该写一下我的经历。&lt;&#x2F;p&gt;
&lt;p&gt;我刚开始学习时，我只用了一个方法：&lt;a href=&quot;https:&#x2F;&#x2F;apps.ankiweb.net&#x2F;&quot;&gt;Anki&lt;&#x2F;a&gt;卡片。我也认为在语言学习方法上，Anki是最有效的。但是为了提高听力和口语，它不太好。&lt;&#x2F;p&gt;
&lt;p&gt;这就是为什么去年我也开始在Italki上上课。因为&lt;a href=&quot;https:&#x2F;&#x2F;www.italki.com&#x2F;&quot;&gt;Italki&lt;&#x2F;a&gt;有很多的中文老师，所以你可以选择一个最适合你的学习方式的。对我来说，这是最有效方法提高我的口语。&lt;&#x2F;p&gt;
&lt;p&gt;因为在今年上半年的时候，我还感觉我的听力水平不好，所以我找了方法来提高。我发现了大部分人建议听博客，然后我找到一些有用的博客。我最喜欢的是这些：&lt;&#x2F;p&gt;
&lt;ol&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;open.spotify.com&#x2F;show&#x2F;04re9FvL1xviHGWvXKoAhZ?si=d7bc25d6f481427b&quot;&gt;听故事说中文&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;open.spotify.com&#x2F;show&#x2F;7sHdRAysnMygox20EuYlwp?si=6ec8583891484aef&quot;&gt;猫咪中文&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;open.spotify.com&#x2F;show&#x2F;6mJNegfDGmNaG1mWJtZJed?si=15f5851993d74a48&quot;&gt;茶歇中文&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ol&gt;
&lt;p&gt;未来将要发生什么？我还不知道，但是我希望我可以继续进步。明年三月我打算和我的对象一起去中国旅游。其实我十分期待这个旅行，因为我不但没去过中国，甚至连亚洲也没去过！既然中国菜非常好吃，我应该学习怎么流利地点菜。我也希望在中国我可以够有信心尝试和中国人聊聊天儿。&lt;&#x2F;p&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>CHERI</title>
        <published>2022-11-19T00:00:00+00:00</published>
        <updated>2022-11-19T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jackbondpreston.me/cheri/"/>
        <id>https://jackbondpreston.me/cheri/</id>
        
        <content type="html" xml:base="https://jackbondpreston.me/cheri/">&lt;h2 id=&quot;preamble&quot;&gt;preamble&lt;a href=&quot;#preamble&quot; aria-label=&quot;Anchor link for: preamble&quot; class=&quot;header-link&quot;&gt;[&lt;img src=&quot;&#x2F;images&#x2F;link.svg&quot; &#x2F;&gt;]&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.cl.cam.ac.uk&#x2F;research&#x2F;security&#x2F;ctsrd&#x2F;cheri&#x2F;&quot;&gt;CHERI&lt;&#x2F;a&gt; 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.&lt;&#x2F;p&gt;
&lt;p&gt;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 &lt;em&gt;very simplistic&lt;&#x2F;em&gt; introduction to CHERI, and I&#x27;m going to attempt to explain the basics behind everything I cover. a basic understanding of C will be necessary.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;note:&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt; &lt;a href=&quot;https:&#x2F;&#x2F;www.arm.com&#x2F;architecture&#x2F;cpu&#x2F;morello&quot;&gt;the Morello platform&lt;&#x2F;a&gt; is an evaluation board produced by &lt;a href=&quot;https:&#x2F;&#x2F;www.arm.com&#x2F;&quot;&gt;Arm&lt;&#x2F;a&gt; to provide a physical implementation of CHERI extending &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;AArch64&quot;&gt;the Arm AArch64 ISA&lt;&#x2F;a&gt;. I previously worked on this platform at Arm, &lt;a href=&quot;https:&#x2F;&#x2F;git.morello-project.org&#x2F;morello&#x2F;musl-libc&#x2F;&quot;&gt;porting the musl C library to Morello&lt;&#x2F;a&gt;. implementations for CHERI that are worth looking into from a more open perspective &lt;a href=&quot;https:&#x2F;&#x2F;www.cl.cam.ac.uk&#x2F;techreports&#x2F;UCAM-CL-TR-951.pdf&quot;&gt; are the MIPS (chapter 4) and RISC-V (chapter 5) ones&lt;&#x2F;a&gt;. Morello is the only implementation that exists in a true hard core format, afaik - but this is obviously hard to obtain so you&#x27;ll just be playing around with emulators&#x2F;models anyway.&lt;&#x2F;p&gt;
&lt;h2 id=&quot;memory-safety-bugs&quot;&gt;memory safety bugs&lt;a href=&quot;#memory-safety-bugs&quot; aria-label=&quot;Anchor link for: memory-safety-bugs&quot; class=&quot;header-link&quot;&gt;[&lt;img src=&quot;&#x2F;images&#x2F;link.svg&quot; &#x2F;&gt;]&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;to understand how CHERI tries to fix some simple issues, we&#x27;ll first look at some simplified examples of issues that arise when we aren&#x27;t using a CHERI-based architecture.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;a-simple-memory-safety-bug&quot;&gt;a simple memory safety bug&lt;&#x2F;h3&gt;
&lt;p&gt;let&#x27;s take a look at this C code:&lt;&#x2F;p&gt;
&lt;pre data-linenos data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;1&lt;&#x2F;td&gt;&lt;td&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;2&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;3&lt;&#x2F;td&gt;&lt;td&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;4&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt; my_perfect_string[] = &amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;what a beautiful string&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; so beautiful, I sure hope no-one touches it
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;5&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;char&lt;&#x2F;span&gt;&lt;span&gt; user_name[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;32&lt;&#x2F;span&gt;&lt;span&gt;];
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;6&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;7&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;enter your name: &lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;8&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;fgets&lt;&#x2F;span&gt;&lt;span&gt;(user_name, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1000&lt;&#x2F;span&gt;&lt;span&gt;, stdin); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; get user&amp;#39;s name from stdin
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;9&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;hello &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, user_name);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;10&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;my_perfect_string: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%s&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, my_perfect_string);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;11&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;12&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;13&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;and try running the compiled output of said program:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;console&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-console &quot;&gt;&lt;code class=&quot;language-console&quot; data-lang=&quot;console&quot;&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;membug
&lt;&#x2F;span&gt;&lt;span&gt;enter your name: jack
&lt;&#x2F;span&gt;&lt;span&gt;hello jack
&lt;&#x2F;span&gt;&lt;span&gt;my_perfect_string: what a beautiful string
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;works on my machine boss! code review +1, and merged...&lt;&#x2F;p&gt;
&lt;p&gt;...until our good friend &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Hubert_Blaine_Wolfeschlegelsteinhausenbergerdorff_Sr.&quot;&gt;Hubert Blaine Wolfeschlegelsteinhausenbergerdorff Sr.&lt;&#x2F;a&gt; comes along. he emails me a strange error he&#x27;s running into:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;console&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-console &quot;&gt;&lt;code class=&quot;language-console&quot; data-lang=&quot;console&quot;&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;membug
&lt;&#x2F;span&gt;&lt;span&gt;enter your name: Hubert Blaine Wolfeschlegelsteinhausenbergerdorff Sr.
&lt;&#x2F;span&gt;&lt;span&gt;hello Hubert Blaine Wolfeschlegelsteinhausenbergerdorff Sr.
&lt;&#x2F;span&gt;&lt;span&gt;my_perfect_string: hausenbergerdorff Sr.
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;note:&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt; if you compile and run this on your machine, you may not get the same output. that&#x27;s because we&#x27;re invoking &lt;em&gt;undefined behaviour&lt;&#x2F;em&gt; here, so the compiler can kind of do whatever it wants. I&#x27;ll always provide the output that demonstrates what I&#x27;m trying to show when giving examples like this. for what it&#x27;s worth, I&#x27;m running &lt;code&gt;clang 10.0.0-4ubuntu1&lt;&#x2F;code&gt; with target &lt;code&gt;x86_64-pc-linux-gnu&lt;&#x2F;code&gt;. compilation options, the &lt;code&gt;Makefile&lt;&#x2F;code&gt;, and such are available &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;jackbondpreston&#x2F;jackbondpreston.github.io&#x2F;tree&#x2F;master&#x2F;_posts&#x2F;cheri&#x2F;code&quot;&gt;code subdirectory of this article&#x27;s source&lt;&#x2F;a&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;that&#x27;s not supposed to happen! his name has spilled over into our &lt;code&gt;my_perfect_string[]&lt;&#x2F;code&gt; array! turns out our issue is that when we use &lt;code&gt;fgets(char *str, int count, FILE *stream)&lt;&#x2F;code&gt;, we&#x27;ve set the second parameter (&lt;code&gt;size&lt;&#x2F;code&gt;) to &lt;code&gt;1000&lt;&#x2F;code&gt; - but our &lt;code&gt;user_name[32]&lt;&#x2F;code&gt; array can only fit 32 characters (and the last of these should be a null terminator, so 31 usable characters).&lt;&#x2F;p&gt;
&lt;p&gt;&lt;code&gt;fgets()&lt;&#x2F;code&gt; fills up &lt;code&gt;user_name&lt;&#x2F;code&gt;, but it hasn&#x27;t finished with the name yet! it doesn&#x27;t care (or know) that &lt;code&gt;user_name&lt;&#x2F;code&gt; is full, it&#x27;s just going to keep going until it finishes our user input, or reads 999 characters from standard input. thus it keeps mindlessly writing, overwriting the section memory we&#x27;ve used to store our precious perfect string (which happens to be immediately after &lt;code&gt;user_name&lt;&#x2F;code&gt;).&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;note:&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt; &lt;code&gt;fgets()&lt;&#x2F;code&gt; has a cousin, &lt;code&gt;gets(char *s)&lt;&#x2F;code&gt;, which is particularly poor with regards to memory safety (due to lack of &lt;code&gt;size&lt;&#x2F;code&gt; parameter), &lt;a href=&quot;https:&#x2F;&#x2F;linux.die.net&#x2F;man&#x2F;3&#x2F;fgets&quot;&gt;and has largely been moved away from in modern C&lt;&#x2F;a&gt;:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;LSB deprecates &lt;code&gt;gets()&lt;&#x2F;code&gt;. POSIX.1-2008 marks &lt;code&gt;gets()&lt;&#x2F;code&gt; obsolescent. ISO C11 removes the specification of &lt;code&gt;gets()&lt;&#x2F;code&gt; from the C language, and since version 2.16, glibc header files don&#x27;t expose the function declaration if the &lt;code&gt;_ISOC11_SOURCE&lt;&#x2F;code&gt; feature test macro is defined.&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;let&#x27;s take a look at the stack in GDB to see how this happens:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;gdb&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-gdb &quot;&gt;&lt;code class=&quot;language-gdb&quot; data-lang=&quot;gdb&quot;&gt;&lt;span&gt;(gdb) b memdebug.c:7 
&lt;&#x2F;span&gt;&lt;span&gt;(gdb) run 
&lt;&#x2F;span&gt;&lt;span&gt;Breakpoint 1, main () at membug.c:7
&lt;&#x2F;span&gt;&lt;span&gt;7           printf(&amp;quot;enter your name: &amp;quot;); 
&lt;&#x2F;span&gt;&lt;span&gt;(gdb) n
&lt;&#x2F;span&gt;&lt;span&gt;8           fgets(user_name, 1000, stdin); &#x2F;&#x2F; get user&amp;#39;s name from stdin
&lt;&#x2F;span&gt;&lt;span&gt;(gdb) n
&lt;&#x2F;span&gt;&lt;span&gt;9           printf(&amp;quot;hello %s&amp;quot;, user_name);
&lt;&#x2F;span&gt;&lt;span&gt;(gdb) x&#x2F;56bc $sp
&lt;&#x2F;span&gt;&lt;span&gt;0x7fffffffdbf0: 106 &amp;#39;j&amp;#39;     97  &amp;#39;a&amp;#39;     99  &amp;#39;c&amp;#39;     107 &amp;#39;k&amp;#39;     10  &amp;#39;\n&amp;#39;    0   &amp;#39;\000&amp;#39;  0   &amp;#39;\000&amp;#39;  0   &amp;#39;\000&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;0x7fffffffdbf8: 77  &amp;#39;M&amp;#39;     82  &amp;#39;R&amp;#39;     85  &amp;#39;U&amp;#39;     85  &amp;#39;U&amp;#39;     85  &amp;#39;U&amp;#39;     85  &amp;#39;U&amp;#39;     0   &amp;#39;\000&amp;#39;  0   &amp;#39;\000&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;0x7fffffffdc00: -24 &amp;#39;\350&amp;#39;  -78 &amp;#39;\262&amp;#39;  -5  &amp;#39;\373&amp;#39;  -9  &amp;#39;\367&amp;#39;  -1  &amp;#39;\377&amp;#39;  127 &amp;#39;\177&amp;#39;  0   &amp;#39;\000&amp;#39;  0   &amp;#39;\000&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;0x7fffffffdc08: 0   &amp;#39;\000&amp;#39;  82  &amp;#39;R&amp;#39;     85  &amp;#39;U&amp;#39;     85  &amp;#39;U&amp;#39;     85  &amp;#39;U&amp;#39;     85  &amp;#39;U&amp;#39;     0   &amp;#39;\000&amp;#39;  0   &amp;#39;\000&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;0x7fffffffdc10: 119 &amp;#39;w&amp;#39;     104 &amp;#39;h&amp;#39;     97  &amp;#39;a&amp;#39;     116 &amp;#39;t&amp;#39;     32  &amp;#39; &amp;#39;     97  &amp;#39;a&amp;#39;     32  &amp;#39; &amp;#39;     98  &amp;#39;b&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;0x7fffffffdc18: 101 &amp;#39;e&amp;#39;     97  &amp;#39;a&amp;#39;     117 &amp;#39;u&amp;#39;     116 &amp;#39;t&amp;#39;     105 &amp;#39;i&amp;#39;     102 &amp;#39;f&amp;#39;     117 &amp;#39;u&amp;#39;     108 &amp;#39;l&amp;#39;
&lt;&#x2F;span&gt;&lt;span&gt;0x7fffffffdc20: 32  &amp;#39; &amp;#39;     115 &amp;#39;s&amp;#39;     116 &amp;#39;t&amp;#39;     114 &amp;#39;r&amp;#39;     105 &amp;#39;i&amp;#39;     110 &amp;#39;n&amp;#39;     103 &amp;#39;g&amp;#39;     0   &amp;#39;\000&amp;#39;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;we can see our two character arrays are right next to each other on the stack (&lt;code&gt;user_name&lt;&#x2F;code&gt; contains some gibberish as it is not zero-initialised).&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;note:&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt; this code was compiled with &lt;code&gt;-fno-stack-protector&lt;&#x2F;code&gt; to reproduce this behaviour. compilers have certain techniques which can help protect against such attacks, but there are often ways around these by using less primitive attacks. we are just ignoring these in this article for simplicity.&lt;&#x2F;p&gt;
&lt;p&gt;okay, at least it&#x27;s a pretty easy fix: we just need to change the &lt;code&gt;fgets()&lt;&#x2F;code&gt; parameter &lt;code&gt;size&lt;&#x2F;code&gt; to &lt;code&gt;32&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;note:&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt; you may initially think &quot;why not &lt;code&gt;31&lt;&#x2F;code&gt;? don&#x27;t we need to save a character for the null byte at the end?&quot;. thankfully, &lt;code&gt;fgets&lt;&#x2F;code&gt; does this for us. excerpt from &lt;code&gt;man fgets&lt;&#x2F;code&gt;:&lt;&#x2F;p&gt;
&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;&quot;fgets() reads in _at most one less than size_ characters from stream and stores them into the buffer pointed to by s [...] A terminating null byte (&#x27;\0&#x27;) is stored after the last character in the buffer&quot;.&lt;&#x2F;code&gt;&lt;&#x2F;p&gt;
&lt;&#x2F;blockquote&gt;
&lt;p&gt;this is a good question to be asking though, being careful is key when it comes to these kinds of things.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;why-hardware&quot;&gt;why hardware?&lt;&#x2F;h3&gt;
&lt;p&gt;okay, that wasn&#x27;t too bad. why are we talking about doing anything in hardware here? just write the code correctly!&lt;&#x2F;p&gt;
&lt;p&gt;we&#x27;ve looked at a very simplistic situation, with no real stakes and nothing to exploit (and an unrealistically simple bug). if this bug was exploitable for malicious gain, it could already be too late by the time we found it.&lt;&#x2F;p&gt;
&lt;p&gt;memory safety problems make up the vast majority of problematic security issues. the Chromium project &lt;a href=&quot;https:&#x2F;&#x2F;www.chromium.org&#x2F;Home&#x2F;chromium-security&#x2F;memory-safety&#x2F;&quot;&gt;found 70% of its serious security bugs were memory safety related&lt;&#x2F;a&gt; and &lt;a href=&quot;https:&#x2F;&#x2F;msrc-blog.microsoft.com&#x2F;2019&#x2F;07&#x2F;16&#x2F;a-proactive-approach-to-more-secure-code&#x2F;&quot;&gt;Microsoft found the same prevalence&lt;&#x2F;a&gt;. 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 layers upon layers of software written in different languages, and there are going to be bugs in there. CHERI aims to give us some protection at a hardware level.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;&lt;strong&gt;Note:&lt;&#x2F;strong&gt;&lt;&#x2F;em&gt; some languages (e.g. Rust) are going to offer you strong memory safety guarantees at compile-time, but I&#x27;m not going to include the discussions around this and how it compares to CHERI in this article. this article will focussed on how CHERI applies to C (and to some extent, C++ by extension).&lt;&#x2F;p&gt;
&lt;h2 id=&quot;pointers-recap&quot;&gt;pointers recap&lt;a href=&quot;#pointers-recap&quot; aria-label=&quot;Anchor link for: pointers-recap&quot; class=&quot;header-link&quot;&gt;[&lt;img src=&quot;&#x2F;images&#x2F;link.svg&quot; &#x2F;&gt;]&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;let&#x27;s quickly recap a basic idea of what a pointer is. we&#x27;re going to ignore things like &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Virtual_memory&quot;&gt;virtual memory&lt;&#x2F;a&gt; 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:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; val = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1593&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span&gt;*x = &amp;amp;val; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; x points to val
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;&lt;svg xmlns=&quot;http:&#x2F;&#x2F;www.w3.org&#x2F;2000&#x2F;svg&quot; viewBox=&quot;0 0 1920 314&quot;&gt;&lt;defs&gt;&lt;style&gt;.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}&lt;&#x2F;style&gt;&lt;&#x2F;defs&gt;&lt;g id=&quot;prefix__prefix__a&quot;&gt;&lt;path fill=&quot;#0c1114&quot; d=&quot;M0 0h1920v314H0z&quot;&#x2F;&gt;&lt;text class=&quot;prefix__prefix__h&quot; transform=&quot;translate(577.46 133.41)&quot;&gt;&lt;tspan x=&quot;0&quot; y=&quot;0&quot;&gt;int *x&lt;&#x2F;tspan&gt;&lt;&#x2F;text&gt;&lt;text class=&quot;prefix__prefix__f&quot; transform=&quot;translate(490.97 177.1)&quot;&gt;&lt;tspan x=&quot;0&quot; y=&quot;0&quot;&gt;0x0000010000000004&lt;&#x2F;tspan&gt;&lt;&#x2F;text&gt;&lt;path d=&quot;M481.16 206v18.5M760.5 206v18.5m-279 0h279&quot; stroke=&quot;#fcfcfc&quot; fill=&quot;none&quot; stroke-miterlimit=&quot;10&quot; stroke-linecap=&quot;square&quot; stroke-width=&quot;3&quot;&#x2F;&gt;&lt;text transform=&quot;translate(578.78 241.33)&quot; font-size=&quot;20&quot; font-family=&quot;Source Code Pro&quot; fill=&quot;#fcfcfc&quot;&gt;&lt;tspan x=&quot;0&quot; y=&quot;0&quot;&gt;address&lt;&#x2F;tspan&gt;&lt;&#x2F;text&gt;&lt;path stroke-width=&quot;4&quot; stroke=&quot;#fcfcfc&quot; fill=&quot;none&quot; stroke-miterlimit=&quot;10&quot; d=&quot;M752 171h204.56&quot;&#x2F;&gt;&lt;path class=&quot;prefix__prefix__k&quot; d=&quot;M948.64 182.62L992 171.01l-43.36-11.63v23.24z&quot;&#x2F;&gt;&lt;text transform=&quot;translate(1272.76 177.16)&quot; fill=&quot;#fcfcfc&quot; font-size=&quot;24&quot;&gt;&lt;tspan class=&quot;prefix__prefix__m&quot; x=&quot;0&quot; y=&quot;0&quot;&gt;mem[&lt;&#x2F;tspan&gt;&lt;tspan class=&quot;prefix__prefix__l&quot; x=&quot;57.6&quot; y=&quot;0&quot;&gt;0x0000010000000004&lt;&#x2F;tspan&gt;&lt;tspan class=&quot;prefix__prefix__m&quot; x=&quot;316.79&quot; y=&quot;0&quot;&gt;]&lt;&#x2F;tspan&gt;&lt;&#x2F;text&gt;&lt;text class=&quot;prefix__prefix__i&quot; transform=&quot;translate(1272.76 133.16)&quot;&gt;&lt;tspan class=&quot;prefix__prefix__m&quot; x=&quot;0&quot; y=&quot;0&quot;&gt;mem[&lt;&#x2F;tspan&gt;&lt;tspan class=&quot;prefix__prefix__l&quot; x=&quot;57.6&quot; y=&quot;0&quot;&gt;0x0000010000000000&lt;&#x2F;tspan&gt;&lt;tspan class=&quot;prefix__prefix__m&quot; x=&quot;316.79&quot; y=&quot;0&quot;&gt;]&lt;&#x2F;tspan&gt;&lt;&#x2F;text&gt;&lt;text class=&quot;prefix__prefix__i&quot; transform=&quot;translate(1271.76 224.16)&quot;&gt;&lt;tspan class=&quot;prefix__prefix__m&quot; x=&quot;0&quot; y=&quot;0&quot;&gt;mem[&lt;&#x2F;tspan&gt;&lt;tspan class=&quot;prefix__prefix__l&quot; x=&quot;57.6&quot; y=&quot;0&quot;&gt;0x0000010000000008&lt;&#x2F;tspan&gt;&lt;tspan class=&quot;prefix__prefix__m&quot; x=&quot;316.79&quot; y=&quot;0&quot;&gt;]&lt;&#x2F;tspan&gt;&lt;&#x2F;text&gt;&lt;&#x2F;g&gt;&lt;g id=&quot;prefix__prefix__b&quot;&gt;&lt;path class=&quot;prefix__prefix__d&quot; d=&quot;M1260 58v48H985V58&quot;&#x2F;&gt;&lt;path d=&quot;M1258 195v40H987v-40h271m4-4H983v48h279v-48zm-4-84v40H987v-40h271m4-4H983v48h279v-48z&quot; fill=&quot;gray&quot;&#x2F;&gt;&lt;path class=&quot;prefix__prefix__k&quot; d=&quot;M756.16 150.93v40h-271v-40h271m4-4h-279v48h279v-48zM1258 151v40H987v-40h271m4-4H983v48h279v-48z&quot;&#x2F;&gt;&lt;text class=&quot;prefix__prefix__f&quot; transform=&quot;translate(1094 177.09)&quot;&gt;&lt;tspan x=&quot;0&quot; y=&quot;0&quot;&gt;1593&lt;&#x2F;tspan&gt;&lt;&#x2F;text&gt;&lt;text class=&quot;prefix__prefix__h&quot; transform=&quot;translate(1007.6 45.16)&quot;&gt;&lt;tspan x=&quot;0&quot; y=&quot;0&quot;&gt;memory (as ints)&lt;&#x2F;tspan&gt;&lt;&#x2F;text&gt;&lt;path class=&quot;prefix__prefix__d&quot; d=&quot;M1260 284v-48H985v48&quot;&#x2F;&gt;&lt;&#x2F;g&gt;&lt;&#x2F;svg&gt;&lt;&#x2F;p&gt;
&lt;p&gt;and on these normal architectures, this pointer generally is just a number. we can do weird things with it, treating it as a number...&lt;&#x2F;p&gt;
&lt;pre data-linenos data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;1&lt;&#x2F;td&gt;&lt;td&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;2&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;3&lt;&#x2F;td&gt;&lt;td&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;4&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; magic = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;9999&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;5&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void&lt;&#x2F;span&gt;&lt;span&gt;)magic; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; suppress unused warning
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;6&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;7&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; arr[] = { &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;1234&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;5678 &lt;&#x2F;span&gt;&lt;span&gt;};
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;8&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;9&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span&gt;*x = &amp;amp;(arr[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]); &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; x is a pointer to first element of arr
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;10&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;*(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%lx&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;)=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, (size_t) x, *x);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;11&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;12&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;unsigned long&lt;&#x2F;span&gt;&lt;span&gt; x_addr = (size_t) x; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; we&amp;#39;re going to assume size_t = unsigned long here
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;13&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    x_addr += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;; &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; sizeof(int) == 4
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;14&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    x = (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span&gt;*) x_addr;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;15&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;*(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%lx&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;)=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, (size_t) x, *x);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;16&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;17&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    x_addr += &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;18&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    x = (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span&gt;*) x_addr;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;19&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;*(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%lx&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;)=&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, (size_t) x, *x);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;20&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;21&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;22&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;...and this code will often still work:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;console&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-console &quot;&gt;&lt;code class=&quot;language-console&quot; data-lang=&quot;console&quot;&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;ptrs_as_numbers
&lt;&#x2F;span&gt;&lt;span&gt;*(7fff98640c20)=1234
&lt;&#x2F;span&gt;&lt;span&gt;*(7fff98640c24)=5678
&lt;&#x2F;span&gt;&lt;span&gt;*(7fff98640c28)=9999
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;yikes! now, when you start messing with pointers like this, you&#x27;re bound to run into a bunch of undefined behaviour. but C programmers write undefined behaviour all the time (and not always by accident), and my computer executes this program fine without complaining at all. doesn&#x27;t it feel a bit weird that we can take a pointer to &lt;code&gt;arr[0]&lt;&#x2F;code&gt; and modify it to load &lt;code&gt;magic&lt;&#x2F;code&gt;? they&#x27;re not even part of the same array...&lt;&#x2F;p&gt;
&lt;h2 id=&quot;introducting-capabilities&quot;&gt;introducting capabilities&lt;a href=&quot;#introducting-capabilities&quot; aria-label=&quot;Anchor link for: introducting-capabilities&quot; class=&quot;header-link&quot;&gt;[&lt;img src=&quot;&#x2F;images&#x2F;link.svg&quot; &#x2F;&gt;]&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;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).&lt;&#x2F;p&gt;
&lt;p&gt;as you might have guessed, this &quot;extra information&quot; takes up extra bits of the capability (how much is actually a little complicated - we will touch on that). bits are assigned to three key pieces of metadata: &lt;em&gt;bounds&lt;&#x2F;em&gt;, &lt;em&gt;permissions&lt;&#x2F;em&gt;, and &lt;em&gt;object type&lt;&#x2F;em&gt;. there is also an additional 1-bit &lt;em&gt;tag&lt;&#x2F;em&gt; 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 association for us. the diagram below is provided as a rough overview of this. note that it is not to scale.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;svg xmlns=&quot;http:&#x2F;&#x2F;www.w3.org&#x2F;2000&#x2F;svg&quot; viewBox=&quot;0 0 1920 314&quot;&gt;&lt;defs&gt;&lt;style&gt;.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}&lt;&#x2F;style&gt;&lt;&#x2F;defs&gt;&lt;g id=&quot;prefix__a&quot;&gt;&lt;path fill=&quot;#0c1114&quot; d=&quot;M0 0h1920v314H0z&quot;&#x2F;&gt;&lt;text transform=&quot;translate(101.86 232.41)&quot; font-family=&quot;Source Code Pro&quot; font-weight=&quot;700&quot; fill=&quot;#fcfcfc&quot; font-size=&quot;24&quot;&gt;&lt;tspan x=&quot;0&quot; y=&quot;0&quot;&gt;int *x (capability)&lt;&#x2F;tspan&gt;&lt;&#x2F;text&gt;&lt;text transform=&quot;translate(1205.97 232.1)&quot; font-family=&quot;Source Code Pro&quot; fill=&quot;#fcfcfc&quot; font-size=&quot;24&quot;&gt;&lt;tspan x=&quot;0&quot; y=&quot;0&quot;&gt;0x0000010000000004&lt;&#x2F;tspan&gt;&lt;&#x2F;text&gt;&lt;path class=&quot;prefix__c&quot; d=&quot;M1016 261v18.5M1656 261v18.5M1016 279.5h640&quot;&#x2F;&gt;&lt;text class=&quot;prefix__f&quot; transform=&quot;translate(1293.78 296.33)&quot;&gt;&lt;tspan x=&quot;0&quot; y=&quot;0&quot;&gt;address&lt;&#x2F;tspan&gt;&lt;&#x2F;text&gt;&lt;path class=&quot;prefix__c&quot; d=&quot;M700 191.5V173M1020 191.5V173M700 173h320&quot;&#x2F;&gt;&lt;text class=&quot;prefix__f&quot; transform=&quot;translate(823.78 167.74)&quot;&gt;&lt;tspan x=&quot;0&quot; y=&quot;0&quot;&gt;bounds&lt;&#x2F;tspan&gt;&lt;&#x2F;text&gt;&lt;path class=&quot;prefix__c&quot; d=&quot;M554 260.34v18.5M704 260.34v18.5M554 278.84h150&quot;&#x2F;&gt;&lt;text class=&quot;prefix__f&quot; transform=&quot;translate(562.78 295.68)&quot;&gt;&lt;tspan x=&quot;0&quot; y=&quot;0&quot;&gt;object type&lt;&#x2F;tspan&gt;&lt;&#x2F;text&gt;&lt;g&gt;&lt;path class=&quot;prefix__c&quot; d=&quot;M391.89 191.56v-18.5M541.89 191.56v-18.5M391.89 173.06h150&quot;&#x2F;&gt;&lt;&#x2F;g&gt;&lt;text class=&quot;prefix__f&quot; transform=&quot;translate(400.67 167.8)&quot;&gt;&lt;tspan x=&quot;0&quot; y=&quot;0&quot;&gt;permissions&lt;&#x2F;tspan&gt;&lt;&#x2F;text&gt;&lt;text class=&quot;prefix__f&quot; transform=&quot;translate(304.67 31.07)&quot;&gt;&lt;tspan x=&quot;0&quot; y=&quot;0&quot;&gt;tag (out-of-band)&lt;&#x2F;tspan&gt;&lt;&#x2F;text&gt;&lt;g&gt;&lt;path class=&quot;prefix__c&quot; d=&quot;M391.33 55.92v-18.5M421.33 55.92v-18.5M391.33 37.42h30&quot;&#x2F;&gt;&lt;&#x2F;g&gt;&lt;&#x2F;g&gt;&lt;g id=&quot;prefix__b&quot;&gt;&lt;path class=&quot;prefix__g&quot; d=&quot;M1651.66 205.93v40h-632v-40h632m4-4h-640v48h640v-48z&quot;&#x2F;&gt;&lt;path class=&quot;prefix__g&quot; d=&quot;M1016 206v40H704v-40h312m4-4H700v48h320v-48z&quot;&#x2F;&gt;&lt;path class=&quot;prefix__g&quot; d=&quot;M700 206v40H558v-40h142m4-4H554v48h150v-48z&quot;&#x2F;&gt;&lt;path class=&quot;prefix__g&quot; d=&quot;M554 206v40h-12v-40h12m4-4h-20v48h20v-48z&quot;&#x2F;&gt;&lt;path class=&quot;prefix__g&quot; d=&quot;M538 206v40H396v-40h142m4-4H392v48h150v-48zM418.5 70v40h-22V70h22m4-4h-30v48h30V66z&quot;&#x2F;&gt;&lt;&#x2F;g&gt;&lt;&#x2F;svg&gt;&lt;&#x2F;p&gt;
&lt;p&gt;I am mostly going to focus on &lt;em&gt;bounds&lt;&#x2F;em&gt; 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 won&#x27;t let us do this!&lt;&#x2F;p&gt;
&lt;p&gt;&lt;strong&gt;&lt;em&gt;note:&lt;&#x2F;em&gt;&lt;&#x2F;strong&gt; 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 shouldn&#x27;t need to think about this much, just know it will be managed for us. the key take-away from this is that &lt;em&gt;bounds can&#x27;t always be 100% precise for all addresses and ranges&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;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 &lt;code&gt;user_name&lt;&#x2F;code&gt; which we pass to &lt;code&gt;fgets&lt;&#x2F;code&gt;, such that the capability may only access the contents of the array. this means that when &lt;code&gt;fgets&lt;&#x2F;code&gt; tries to write past the end of the &lt;code&gt;user_name&lt;&#x2F;code&gt; array, the processor will throw a &lt;em&gt;capability fault&lt;&#x2F;em&gt;, and execution of our program will cease.&lt;&#x2F;p&gt;
&lt;p&gt;the idea behind CHERI is that we as the C programmer don&#x27;t have to set up these bounds ourselves most of the time---this is something the compiler can generate code for. the compiler knows that the &lt;code&gt;user_name&lt;&#x2F;code&gt; array has a length of &lt;code&gt;32&lt;&#x2F;code&gt;, and can set the bounds accordingly on capabilities created that point to it. let&#x27;s try it...&lt;&#x2F;p&gt;
&lt;h2 id=&quot;playing-with-cheri-risc-v&quot;&gt;playing with CHERI RISC-V&lt;a href=&quot;#playing-with-cheri-risc-v&quot; aria-label=&quot;Anchor link for: playing-with-cheri-risc-v&quot; class=&quot;header-link&quot;&gt;[&lt;img src=&quot;&#x2F;images&#x2F;link.svg&quot; &#x2F;&gt;]&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;unless you&#x27;re 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 &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;QEMU&quot;&gt;QEMU&lt;&#x2F;a&gt; emulator to emulate a &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;RISC-V&quot;&gt;RISC-V&lt;&#x2F;a&gt; CHERI environment. running &lt;a href=&quot;https:&#x2F;&#x2F;www.cheribsd.org&#x2F;&quot;&gt;CheriBSD&lt;&#x2F;a&gt; on this emulator will allow us to have a nice &lt;a href=&quot;https:&#x2F;&#x2F;www.freebsd.org&#x2F;&quot;&gt;FreeBSD&lt;&#x2F;a&gt;-based capability-enabled environment to play around with. I&#x27;ll use &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;CTSRD-CHERI&#x2F;cheribuild&quot;&gt;cheribuild&lt;&#x2F;a&gt; to easily get set up (the &lt;code&gt;cheribuild.py&lt;&#x2F;code&gt; step will take a very long time the first time):&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;console&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-console &quot;&gt;&lt;code class=&quot;language-console&quot; data-lang=&quot;console&quot;&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;sudo&lt;&#x2F;span&gt;&lt;span&gt; apt install autoconf automake libtool pkg-config clang bison cmake \
&lt;&#x2F;span&gt;&lt;span&gt;ninja-build samba flex texinfo time libglib2.0-dev libpixman-1-dev \
&lt;&#x2F;span&gt;&lt;span&gt;libarchive-dev libarchive-tools libbz2-dev libattr1-dev libcap-ng-dev
&lt;&#x2F;span&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;git&lt;&#x2F;span&gt;&lt;span&gt; clone git@github.com:CTSRD-CHERI&#x2F;cheribuild
&lt;&#x2F;span&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;cd&lt;&#x2F;span&gt;&lt;span&gt; cheribuild
&lt;&#x2F;span&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;cheribuild.py --include-dependencies --run&#x2F;ssh-forwarding-port 2222 run-riscv64-purecap 
&lt;&#x2F;span&gt;&lt;span&gt;CheriBSD&#x2F;riscv (cheribsd-riscv64-purecap) (ttyu0)
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;login: root
&lt;&#x2F;span&gt;&lt;span&gt;root@cheribsd-riscv64-purecap:~ #
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;now we have our shell inside our CheriBSD emulated platform, we can start to try things out. let&#x27;s compile our &lt;code&gt;membug&lt;&#x2F;code&gt; program again, this time with the toolchain targetting CheriBSD RISC-V - this will have been built as part of the dependencies already.&lt;&#x2F;p&gt;
&lt;p&gt;once our &lt;code&gt;membug-cheribsd&lt;&#x2F;code&gt; executable is built, we can &lt;code&gt;scp&lt;&#x2F;code&gt; it over to the CheriBSD filesystem. remember, we set up the SSH forwarding port to &lt;code&gt;1111&lt;&#x2F;code&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;from a terminal on your host machine:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;console&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-console &quot;&gt;&lt;code class=&quot;language-console&quot; data-lang=&quot;console&quot;&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;~&#x2F;cheri&#x2F;output&#x2F;sdk&#x2F;utils&#x2F;cheribsd-riscv64-purecap-clang&lt;&#x2F;span&gt;&lt;span&gt; membug.c&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt; -Wall -g -fno-stack-protector -o&lt;&#x2F;span&gt;&lt;span&gt; membug-cheribsd
&lt;&#x2F;span&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;scp -P&lt;&#x2F;span&gt;&lt;span&gt; 2222 .&#x2F;membug-cheribsd root@localhost:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;~&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;and now we can see what happens when we explore our bug with CHERI:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;console&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-console &quot;&gt;&lt;code class=&quot;language-console&quot; data-lang=&quot;console&quot;&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;membug-cheribsd
&lt;&#x2F;span&gt;&lt;span&gt;enter your name: jack
&lt;&#x2F;span&gt;&lt;span&gt;hello jack
&lt;&#x2F;span&gt;&lt;span&gt;my_perfect_string: what a beautiful string
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;membug-cheribsd
&lt;&#x2F;span&gt;&lt;span&gt;enter your name: Hubert Blaine Wolfeschlegelsteinhausenbergerdorff Sr.
&lt;&#x2F;span&gt;&lt;span&gt;In-address space security exception (core dumped)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;it&#x27;s working! we are getting a capability fault as we exceed the bounds of the
&lt;code&gt;user_name&lt;&#x2F;code&gt; capability bounds. we can use gdb to verify this is caused by the bounds fault:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;gdb&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-gdb &quot;&gt;&lt;code class=&quot;language-gdb&quot; data-lang=&quot;gdb&quot;&gt;&lt;span&gt;(gdb) run
&lt;&#x2F;span&gt;&lt;span&gt;Starting program: &#x2F;root&#x2F;membug-cheribsd 
&lt;&#x2F;span&gt;&lt;span&gt;enter your name: Hubert Blaine Wolfeschlegelsteinhausenbergerdorff Sr.
&lt;&#x2F;span&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;span&gt;Program received signal SIGPROT, CHERI protection violation.
&lt;&#x2F;span&gt;&lt;span&gt;Capability bounds fault caused by register ca6.
&lt;&#x2F;span&gt;&lt;span&gt;0x0000000040314ce8 in memcpy (dst0=0x3fffdfff44, src0=&amp;lt;optimized out&amp;gt;, length=54) at &#x2F;home&#x2F;jack&#x2F;cheri&#x2F;cheribsd&#x2F;lib&#x2F;libc&#x2F;string&#x2F;bcopy.c:143
&lt;&#x2F;span&gt;&lt;span&gt;(gdb) p $ca6
&lt;&#x2F;span&gt;&lt;span&gt;$1 = () 0x3fffdfff78 [rwRW,0x3fffdfff44-0x3fffdfff64]
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;as we can see, the bounds for our &lt;code&gt;user_name&lt;&#x2F;code&gt; capability (which is stored in capability register &lt;code&gt;ca6&lt;&#x2F;code&gt;) are &lt;code&gt;0x3fffdfff44-0x3fffdfff64&lt;&#x2F;code&gt;, but the address is &lt;code&gt;0x3fffdfff78&lt;&#x2F;code&gt;. 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:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;asm&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-asm &quot;&gt;&lt;code class=&quot;language-asm&quot; data-lang=&quot;asm&quot;&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;0000000000001ce8 &amp;lt;main&amp;gt;:
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;; int main() {
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;    cincoffset	csp&lt;&#x2F;span&gt;&lt;span&gt;,    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;csp&lt;&#x2F;span&gt;&lt;span&gt;,        -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;160
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;    csc	        cra&lt;&#x2F;span&gt;&lt;span&gt;,    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;144         &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;(csp)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;    csc	        cs0&lt;&#x2F;span&gt;&lt;span&gt;,    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;128         &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;(csp)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;    cincoffset  cs0&lt;&#x2F;span&gt;&lt;span&gt;,    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;csp&lt;&#x2F;span&gt;&lt;span&gt;,        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;160
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;    cincoffset	ca0&lt;&#x2F;span&gt;&lt;span&gt;,    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;cs0&lt;&#x2F;span&gt;&lt;span&gt;,        -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;36
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;    csetbounds	ca2&lt;&#x2F;span&gt;&lt;span&gt;,    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;ca0&lt;&#x2F;span&gt;&lt;span&gt;,        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;4
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;    cincoffset	ca0&lt;&#x2F;span&gt;&lt;span&gt;,    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;cs0&lt;&#x2F;span&gt;&lt;span&gt;,        -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;60
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;    csetbounds	ca0&lt;&#x2F;span&gt;&lt;span&gt;,    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;ca0&lt;&#x2F;span&gt;&lt;span&gt;,        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;24
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;    csc	        ca0&lt;&#x2F;span&gt;&lt;span&gt;,    -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;128        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;(cs0)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;    cincoffset	ca1&lt;&#x2F;span&gt;&lt;span&gt;,    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;cs0&lt;&#x2F;span&gt;&lt;span&gt;,        -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;92
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;    csetbounds	ca1&lt;&#x2F;span&gt;&lt;span&gt;,    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;ca1&lt;&#x2F;span&gt;&lt;span&gt;,        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;32
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;    csc	        ca1&lt;&#x2F;span&gt;&lt;span&gt;,    -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;144        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;(cs0)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;    mv	        a1&lt;&#x2F;span&gt;&lt;span&gt;,     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;zero
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;    csd	        a1&lt;&#x2F;span&gt;&lt;span&gt;,     -&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;104        &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;(cs0)
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;    csw	        a1&lt;&#x2F;span&gt;&lt;span&gt;,     &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0           &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;(ca2)
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h3 id=&quot;chains-of-capabilities&quot;&gt;chains of capabilities&lt;&#x2F;h3&gt;
&lt;p&gt;at this point you may be thinking &quot;okay, that&#x27;s great, but if we can just set the bounds of a capability with an instruction then what&#x27;s the point? surely I can just set global bounds on some random pointer and access whatever I want?&quot;&lt;&#x2F;p&gt;
&lt;p&gt;fundamental to the idea of capabilities is their &lt;em&gt;provenance&lt;&#x2F;em&gt; and &lt;em&gt;monotonicity&lt;&#x2F;em&gt;.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;provenance&lt;&#x2F;em&gt;, simply put, means we can only construct a capability from an existing capability, using specific instructions. we can&#x27;t just create a capability from some random &lt;code&gt;size_t&lt;&#x2F;code&gt; and use it to load&#x2F;store something. let&#x27;s see what happens when we try to run our &lt;code&gt;ptrs_as_numbers&lt;&#x2F;code&gt; program on CheriBSD:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;gdb&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-gdb &quot;&gt;&lt;code class=&quot;language-gdb&quot; data-lang=&quot;gdb&quot;&gt;&lt;span&gt;(gdb) run
&lt;&#x2F;span&gt;&lt;span&gt;Starting program: &#x2F;root&#x2F;ptrs_as_numbers-cheribsd 
&lt;&#x2F;span&gt;&lt;span&gt;*x=1234
&lt;&#x2F;span&gt;&lt;span&gt;Program received signal SIGPROT, CHERI protection violation.
&lt;&#x2F;span&gt;&lt;span&gt;Capability tag fault caused by register ca1.
&lt;&#x2F;span&gt;&lt;span&gt;0x0000000000101c66 in main () at ptrs_as_numbers.c:1414
&lt;&#x2F;span&gt;&lt;span&gt;          printf(&amp;quot;*x=%d\n&amp;quot;, *x);
&lt;&#x2F;span&gt;&lt;span&gt;(gdb) p $ca1
&lt;&#x2F;span&gt;&lt;span&gt;$1 = () 0x3fffdfff74
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;we get a fault, because the tag isn&#x27;t 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 &lt;code&gt;unsigned long&lt;&#x2F;code&gt;, we just copied the 64-bit address.&lt;&#x2F;p&gt;
&lt;p&gt;&lt;em&gt;monotonicity&lt;&#x2F;em&gt; is what stops us taking an existing capability and creating a capability with more permissions and&#x2F;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 less than or equal to 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.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;cheri-fying-code&quot;&gt;CHERI-fying code&lt;&#x2F;h3&gt;
&lt;p&gt;you&#x27;ll notice we got a lot of these benefits &quot;for free&quot;. we only had to recompile our code, and we gained this extra security. of course, CHERI does require changes to program sources. naturally, the compiler was changed a lot to implement this behaviour. in particular, CHERI also requires changes to things like the C library and kernel in order to take advantage of the features fully. sufficiently large userspace programs will generally require source changes.&lt;&#x2F;p&gt;
&lt;p&gt;one common issue is that a lot of existing C code assumes that &lt;code&gt;sizeof (*void) == sizeof(size_t)&lt;&#x2F;code&gt;. with CHERI, our pointers are now twice as big. however, &lt;code&gt;size_t&lt;&#x2F;code&gt; hasn&#x27;t changed size, as the address space size hasn&#x27;t changed - for example, if we index into an array with &lt;code&gt;size_t&lt;&#x2F;code&gt;, the index should still be the same size; the extra data in our &lt;code&gt;void *&lt;&#x2F;code&gt; capability is the metadata, not extra address data. any program that tries to convert from some &lt;code&gt;unsigned long&lt;&#x2F;code&gt; or &lt;code&gt;size_t&lt;&#x2F;code&gt; 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. in CHERI, we can use &lt;code&gt;ptraddr_t&lt;&#x2F;code&gt; to store addresses and &lt;code&gt;[u]intptr_t&lt;&#x2F;code&gt; to store capabilities.&lt;&#x2F;p&gt;
&lt;p&gt;let&#x27;s make a program to see some differences in types, and demonstrate how &lt;code&gt;uintptr_t&lt;&#x2F;code&gt; can preserve capabilities:&lt;&#x2F;p&gt;
&lt;pre data-linenos data-lang=&quot;c&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-c &quot;&gt;&lt;code class=&quot;language-c&quot; data-lang=&quot;c&quot;&gt;&lt;table&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;1&lt;&#x2F;td&gt;&lt;td&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdint.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;2&lt;&#x2F;td&gt;&lt;td&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#include &lt;&#x2F;span&gt;&lt;span&gt;&amp;lt;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;stdio.h&lt;&#x2F;span&gt;&lt;span&gt;&amp;gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;3&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;4&lt;&#x2F;td&gt;&lt;td&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span style=&quot;color:#8fa1b3;&quot;&gt;main&lt;&#x2F;span&gt;&lt;span&gt;() {
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;5&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;type          size (hex)   size (dec)&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;6&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;=====================================&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;7&lt;&#x2F;td&gt;&lt;td&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#ifdef&lt;&#x2F;span&gt;&lt;span&gt; __PTRADDR_TYPE__
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;8&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;ptraddr_t     0x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%.2lx         %.2lu&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, sizeof(ptraddr_t), sizeof(ptraddr_t));
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;9&lt;&#x2F;td&gt;&lt;td&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#endif
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;10&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;uintptr_t     0x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%.2lx         %.2lu&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, sizeof(uintptr_t), sizeof(uintptr_t));
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;11&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;size_t        0x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%.2lx         %.2lu&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, sizeof(size_t), sizeof(size_t));
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;12&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;void*         0x&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%.2lx         %.2lu&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, sizeof(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void&lt;&#x2F;span&gt;&lt;span&gt;*), sizeof(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;void&lt;&#x2F;span&gt;&lt;span&gt;*));
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;13&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;=====================================&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;14&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;15&lt;&#x2F;td&gt;&lt;td&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#ifdef&lt;&#x2F;span&gt;&lt;span&gt; __CHERI__
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;16&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; x = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;111&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;17&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt; y[] = { &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;888&lt;&#x2F;span&gt;&lt;span&gt;, &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;999 &lt;&#x2F;span&gt;&lt;span&gt;};
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;18&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;19&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span&gt;*a = &amp;amp;x;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;20&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int &lt;&#x2F;span&gt;&lt;span&gt;*b = &amp;amp;(y[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;21&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;22&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; transplant address from capability a to capability b
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;23&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;*b: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, *b);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;24&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    ptraddr_t a_addr = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;__builtin_cheri_address_get&lt;&#x2F;span&gt;&lt;span&gt;(a);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;25&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    b = &lt;&#x2F;span&gt;&lt;span style=&quot;color:#bf616a;&quot;&gt;__builtin_cheri_address_set&lt;&#x2F;span&gt;&lt;span&gt;(a, a_addr);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;26&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;*b: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, *b);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;27&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;28&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    b = &amp;amp;(y[&lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;]);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;29&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#65737e;&quot;&gt;&#x2F;&#x2F; uintptr_t is an unsigned integer type that preserves capabilities
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;30&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    uintptr_t b_uintptr = (uintptr_t) b;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;31&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    b_uintptr += sizeof(&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;32&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    b = (&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;int&lt;&#x2F;span&gt;&lt;span&gt;*) b_uintptr;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;33&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;printf&lt;&#x2F;span&gt;&lt;span&gt;(&amp;quot;&lt;&#x2F;span&gt;&lt;span style=&quot;color:#a3be8c;&quot;&gt;*b: &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;%d&lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;\n&lt;&#x2F;span&gt;&lt;span&gt;&amp;quot;, *b);
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;34&lt;&#x2F;td&gt;&lt;td&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;#endif
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;35&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;36&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;    &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b48ead;&quot;&gt;return &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d08770;&quot;&gt;0&lt;&#x2F;span&gt;&lt;span&gt;;
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;tr&gt;&lt;td&gt;37&lt;&#x2F;td&gt;&lt;td&gt;&lt;span&gt;}
&lt;&#x2F;span&gt;&lt;&#x2F;td&gt;&lt;&#x2F;tr&gt;&lt;&#x2F;tbody&gt;&lt;&#x2F;table&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;running this on our non-CHERI host will give us:&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;console&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-console &quot;&gt;&lt;code class=&quot;language-console&quot; data-lang=&quot;console&quot;&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;ptrtypes                                                        
&lt;&#x2F;span&gt;&lt;span&gt;type          size (hex)   size (dec)
&lt;&#x2F;span&gt;&lt;span&gt;=====================================
&lt;&#x2F;span&gt;&lt;span&gt;uintptr_t     0x08         08
&lt;&#x2F;span&gt;&lt;span&gt;size_t        0x08         08
&lt;&#x2F;span&gt;&lt;span&gt;void*         0x08         08
&lt;&#x2F;span&gt;&lt;span&gt;=====================================
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;p&gt;running this on CHERI (64-bit):&lt;&#x2F;p&gt;
&lt;pre data-lang=&quot;console&quot; style=&quot;background-color:#2b303b;color:#c0c5ce;&quot; class=&quot;language-console &quot;&gt;&lt;code class=&quot;language-console&quot; data-lang=&quot;console&quot;&gt;&lt;span&gt;$ &lt;&#x2F;span&gt;&lt;span style=&quot;color:#96b5b4;&quot;&gt;.&lt;&#x2F;span&gt;&lt;span&gt;&#x2F;ptrtypes-cheribsd
&lt;&#x2F;span&gt;&lt;span&gt;type          size (hex)   size (dec)
&lt;&#x2F;span&gt;&lt;span&gt;=====================================
&lt;&#x2F;span&gt;&lt;span&gt;ptraddr_t     0x08         08
&lt;&#x2F;span&gt;&lt;span&gt;uintptr_t     0x10         16
&lt;&#x2F;span&gt;&lt;span&gt;size_t        0x08         08
&lt;&#x2F;span&gt;&lt;span&gt;void*         0x10         16
&lt;&#x2F;span&gt;&lt;span&gt;=====================================
&lt;&#x2F;span&gt;&lt;span&gt;*b: 888
&lt;&#x2F;span&gt;&lt;span&gt;*b: 111
&lt;&#x2F;span&gt;&lt;span&gt;*b: 999
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
&lt;h2 id=&quot;epilogue&quot;&gt;epilogue&lt;a href=&quot;#epilogue&quot; aria-label=&quot;Anchor link for: epilogue&quot; class=&quot;header-link&quot;&gt;[&lt;img src=&quot;&#x2F;images&#x2F;link.svg&quot; &#x2F;&gt;]&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;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 I&#x27;ve touched on here, so please, read more about everything - and get your hands dirty trying out messing about with qemu and CheriBSD!&lt;&#x2F;p&gt;
&lt;p&gt;here are some links to check out:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.cl.cam.ac.uk&#x2F;research&#x2F;security&#x2F;ctsrd&#x2F;cheri&#x2F;&quot;&gt;CHERI homepage @ CUCL&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.cl.cam.ac.uk&#x2F;techreports&#x2F;UCAM-CL-TR-941.pdf&quot;&gt;technical report: An Introduction to CHERI&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.cl.cam.ac.uk&#x2F;techreports&#x2F;UCAM-CL-TR-947.pdf&quot;&gt;technical report: CHERI C&#x2F;C++ Programming Guide&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.cl.cam.ac.uk&#x2F;techreports&#x2F;UCAM-CL-TR-951.pdf&quot;&gt;technical report: CHERI ISAv8&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.arm.com&#x2F;architecture&#x2F;cpu&#x2F;morello&quot;&gt;Morello homepage @ Arm&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;developer.arm.com&#x2F;documentation&#x2F;ddi0606&#x2F;latest&quot;&gt;Morello Architecture Reference Manual @ Arm&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
</content>
        
    </entry>
    <entry xml:lang="en">
        <title>sensor watch</title>
        <published>2022-11-14T00:00:00+00:00</published>
        <updated>2022-11-14T00:00:00+00:00</updated>
        
        <author>
          <name>
            
              Unknown
            
          </name>
        </author>
        
        <link rel="alternate" type="text/html" href="https://jackbondpreston.me/sensor-watch/"/>
        <id>https://jackbondpreston.me/sensor-watch/</id>
        
        <content type="html" xml:base="https://jackbondpreston.me/sensor-watch/">&lt;h2 id=&quot;preamble&quot;&gt;preamble&lt;a href=&quot;#preamble&quot; aria-label=&quot;Anchor link for: preamble&quot; class=&quot;header-link&quot;&gt;[&lt;img src=&quot;&#x2F;images&#x2F;link.svg&quot; &#x2F;&gt;]&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;some time back I was browsing &lt;a href=&quot;https:&#x2F;&#x2F;www.crowdsupply.com&#x2F;&quot;&gt;Crowd Supply&lt;&#x2F;a&gt; when I came across &lt;a href=&quot;https:&#x2F;&#x2F;www.crowdsupply.com&#x2F;oddly-specific-objects&#x2F;sensor-watch&quot;&gt;the Sensor Watch project&lt;&#x2F;a&gt; by &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;joeycastillo&quot;&gt;Joey Castillo&lt;&#x2F;a&gt;. I had wanted some kind of &quot;hackable&quot; watch for a while, and had looked at things like &lt;a href=&quot;https:&#x2F;&#x2F;www.crowdsupply.com&#x2F;sqfmi&#x2F;watchy&quot;&gt;Watchy&lt;&#x2F;a&gt;, but this project hit the sweet spot for me. I love my existing F91-W, and this project was a good combination of open source with community software support. one key feature that was important to me is battery life - the Sensor Watch battery life in an average usage scenario is so long that &lt;a href=&quot;https:&#x2F;&#x2F;twitter.com&#x2F;josecastillo&#x2F;status&#x2F;1590066358351298560&quot;&gt;Joey&#x27;s is still going strong&lt;&#x2F;a&gt;!&lt;&#x2F;p&gt;
&lt;p&gt;I was excited to pick one up and start messing around with it, but the first issue I came across was availability - the delivery date for Crowd Supply orders was summer 2023 (I think they ended upbeing delivered sooner than this, not sure). on top of this, shipping and import fees made it pretty prohibitively expensive. I&#x27;ve always found this to be an issue with Crowd Supply as someone based in the UK, even some things designed in the UK are very expensive from Crowd Supply as they are assembled in&#x2F;shipped from the US. so I decided to build one myself! of course, this is more expensive than just buying it, but this was a learning experience and &lt;a href=&quot;https:&#x2F;&#x2F;www.reddit.com&#x2F;r&#x2F;AskReddit&#x2F;comments&#x2F;dxosj&#x2F;what_word_or_phrase_did_you_totally_misunderstand&#x2F;c13pbyc&#x2F;&quot;&gt;knowledge is power&lt;&#x2F;a&gt;!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;component-acquisition&quot;&gt;component acquisition&lt;a href=&quot;#component-acquisition&quot; aria-label=&quot;Anchor link for: component-acquisition&quot; class=&quot;header-link&quot;&gt;[&lt;img src=&quot;&#x2F;images&#x2F;link.svg&quot; &#x2F;&gt;]&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;the first challenge was acquiring all the necessary parts to actually build one. I downloaded &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;joeycastillo&#x2F;Sensor-Watch&#x2F;tree&#x2F;main&#x2F;PCB&#x2F;Main%20Boards&quot;&gt;the PCB files&lt;&#x2F;a&gt; and generated a &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Bill_of_materials&quot;&gt;BOM&lt;&#x2F;a&gt; to figure out exactly what I needed to acquire. I&#x27;m sure in ordinary times this would be easy enough, but the current state of some electronics&#x2F;silicon supply chains had other things to say. some parts are of course still easy to come across, e.g. 10pF 0402 caps and 10k 0603 resistors; most of the components of the Sensor Watch are this kind of commonplace part. what quickly became clear from some scouring of the internet was that my main problem was going to be two parts: the &lt;a href=&quot;https:&#x2F;&#x2F;www.microchip.com&#x2F;en-us&#x2F;product&#x2F;ATSAML22J18A&quot;&gt;ATSAML22J18A-MUT&lt;&#x2F;a&gt;(the processor driving the Sensor Watch), and the &lt;a href=&quot;https:&#x2F;&#x2F;www.hirose.com&#x2F;en&#x2F;product&#x2F;series&#x2F;FH19C__FH19SC&quot;&gt;FH19C-9S-0.5SH(10)&lt;&#x2F;a&gt; (the connector used to attach the extra sensor boards).&lt;&#x2F;p&gt;
&lt;h3 id=&quot;atsaml22j18a-mut&quot;&gt;ATSAML22J18A-MUT&lt;&#x2F;h3&gt;
&lt;p&gt;the former of these was a fairly well discussed shortage that had been ongoing for a while. it was
&lt;a href=&quot;https:&#x2F;&#x2F;www.crowdsupply.com&#x2F;oddly-specific-objects&#x2F;sensor-watch&#x2F;updates&#x2F;blue-boards-shipping-check-your-address-green-boards-delayed-and-other-news-of-the-watch&quot;&gt;the driving force of the Sensor Watch Crowd Supply delay&lt;&#x2F;a&gt;. I spent quite a lot of time searching around the internet, looking at various sites on the English-speaking and Chinese-speaking web. sadly this part was clearly in very short supply, and prices could get pretty insane from vendors that did have some stock. I received quotes for unit prices that include the following (USD&#x2F;GBP): $79.35, $6.56, $13.61, $6.83 (MOQ 4000), £6.45. I guess some people are desperate enough to pay $79.35 :(. I spent so long looking for them that they ended up randomly coming back in stock on &lt;a href=&quot;https:&#x2F;&#x2F;www.microchipdirect.com&#x2F;&quot;&gt;MicrochipDirect&lt;&#x2F;a&gt;. as of the time of writing this article, &lt;a href=&quot;https:&#x2F;&#x2F;www.microchipdirect.com&#x2F;product&#x2F;ATSAML22J18A-MUT&quot;&gt;they are again out of stock&lt;&#x2F;a&gt;. the unit price I bought them for was £3.92, shipping and handling was ~£12.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;fh19c-9s-0-5sh-10&quot;&gt;FH19C-9S-0.5SH(10)&lt;&#x2F;h3&gt;
&lt;p&gt;this part was out of stock everywhere I initially looked (the usual contenders for parts). I searched around in a similar manner as the ATSAML22J18A-MUT, and found some similarly wild pricing. I ended up purchasing a small quantity at a unit price of £0.44 from a website called
&lt;a href=&quot;http:&#x2F;&#x2F;archive.today&#x2F;2022.11.13-230943&#x2F;https:&#x2F;&#x2F;www.dacikeys.net&#x2F;&quot;&gt;dacikeys&lt;&#x2F;a&gt;. yes, the site is actually called this. yes, the unit price is cheaper than digikey and mouser. yes, I actually received all of my order, consisting of working parts. I was definitely shocked that this happened, but sometimes bravery pays off I guess. I still can&#x27;t endorse this shop.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;pcb-and-stencil&quot;&gt;PCB and stencil&lt;&#x2F;h3&gt;
&lt;p&gt;for the PCB I opted to go with &lt;a href=&quot;https:&#x2F;&#x2F;jlcpcb.com&#x2F;&quot;&gt;JLCPCB&lt;&#x2F;a&gt;. I simply uploaded the relevant gerbers, and adjusted the necessary settings. notably, &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;joeycastillo&#x2F;Sensor-Watch&#x2F;issues&#x2F;14#issuecomment-922974276%22%3E&quot;&gt;the thickness should be 0.6mm&lt;&#x2F;a&gt; - this does narrow the choice of manufacturer (for example, OSH Park doesn&#x27;t go this thin). I haven&#x27;t yet ordered any sensor board PCBs, but &lt;a href=&quot;https:&#x2F;&#x2F;www.pcbway.com&#x2F;&quot;&gt;PCBWay&lt;&#x2F;a&gt; seems to be &lt;em&gt;the&lt;&#x2F;em&gt; option there. The PCB turned out great, although the silkscreen is a little hard to read at this size due to lack of sharpness:&lt;&#x2F;p&gt;


&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;jackbondpreston.me&amp;#x2F;images&amp;#x2F;sensor-watch&amp;#x2F;pcb.jpg&quot;&gt;
&lt;img src=&quot;https:&amp;#x2F;&amp;#x2F;jackbondpreston.me&amp;#x2F;processed_images&amp;#x2F;pcb.afc2c7e177b84872.webp&quot; alt=&quot;a closeup of a green sensor watch PCB&quot; &#x2F;&gt;
&lt;&#x2F;a&gt;&lt;h2 id=&quot;assembly&quot;&gt;assembly&lt;a href=&quot;#assembly&quot; aria-label=&quot;Anchor link for: assembly&quot; class=&quot;header-link&quot;&gt;[&lt;img src=&quot;&#x2F;images&#x2F;link.svg&quot; &#x2F;&gt;]&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;I decided to assemble myself. partially because the logistics of paying for assembly when I had to source parts from many different providers seemed like a headache, partially because I thought it would be a fun challenge and learning experience!&lt;&#x2F;p&gt;
&lt;p&gt;a few things were necessary to solder the components to this PCB. I&#x27;m sure someone talented could hand solder this with an iron, but I can name a lot of things I&#x27;d rather do than try to do that
(especially the &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Flat_no-leads_package&quot;&gt;QFN&lt;&#x2F;a&gt; SAML) - and that list includes unpleasant things. I opted to go with
&lt;a href=&quot;http:&#x2F;&#x2F;www.flyelectric.org.uk&#x2F;hot_plate.htm&quot;&gt;hotplate soldering&lt;&#x2F;a&gt;, which is a cheaper way to access the ease of reflow soldering. for a PCB like the Sensor Watch, where almost all the components are on one side, it&#x27;s ideal. the hotplate I have is the ever-popular
&lt;a href=&quot;https:&#x2F;&#x2F;www.miniware.com.cn&#x2F;product&#x2F;mhp30-mini-hot-plate-preheater&#x2F;&quot;&gt;MHP30&lt;&#x2F;a&gt;, which I run &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;Ralim&#x2F;IronOS&quot;&gt;IronOS&lt;&#x2F;a&gt; on. I highly recommend it, it&#x27;s great! my soldering iron is the iconic
&lt;a href=&quot;https:&#x2F;&#x2F;pine64.com&#x2F;product&#x2F;pinecil-smart-mini-portable-soldering-iron&#x2F;&quot;&gt;Pinecil&lt;&#x2F;a&gt; (not the &lt;a href=&quot;https:&#x2F;&#x2F;www.pine64.org&#x2F;2022&#x2F;07&#x2F;28&#x2F;july-update-a-pinecil-evolved&#x2F;&quot;&gt;fancy new V2&lt;&#x2F;a&gt; though :[) which also runs IronOS. nice!&lt;&#x2F;p&gt;
&lt;h3 id=&quot;process&quot;&gt;process&lt;&#x2F;h3&gt;
&lt;p&gt;the assembly process is as follows:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;apply solder paste to the PCB with the stencil. make sure the stencil is really flush and justkind of squeegee it on with a plastic card. I used tape to hold it in place. then carefully removed the stencil, avoiding smudging the paste in doing so.&lt;&#x2F;li&gt;
&lt;li&gt;place components on the PCB. this was by far the most painful part of the whole process. a steady hand is not something I was blessed with, and some of these parts are really small. I used a microscope from Amazon for this, the ample manouverable lighting was a big help. a lot of time and patience is required, but it&#x27;s very first time doable with no prior experience! simply go through the parts one by one, or by area of the board - whatever you prefer. then pick up the respective part with some fine tweezers, and slowly put in place on the solder paste. thankfully, the paste will lightly stick the component in place once you&#x27;ve done this (it is not knock-proof though!).&lt;&#x2F;li&gt;
&lt;li&gt;carefully (really!) place the PCB onto the hotplate and heat up. keep on until everything seems to be melted, and the components have hopefully been pulled into place. that&#x27;s the top side done! let it cool down, then move on to the bottom.&lt;&#x2F;li&gt;
&lt;li&gt;time for some hand soldering. the button is pretty small, and very fiddly to do. I found you don&#x27;t need too much precision, but you have to be really careful with your iron as the plastic button will melt if you touch it. once that&#x27;s in place, it&#x27;s just a matter of
&lt;a href=&quot;https:&#x2F;&#x2F;youtu.be&#x2F;Zr0pKeC2VFU?t=185&quot;&gt;removing the buzzer connector from your old PCB and soldering it onto the back of the Sensor Watch PCB.&lt;&#x2F;a&gt; this will feel blissfully easy after the button! you also have to place the battery clip, but no soldering needed here :).&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;one area I found particularly difficult was the area with the oscillator crystal and the two 0402 capacitors, C7 and C8. things are a bit cramped here, so extra care was needed:&lt;&#x2F;p&gt;


&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;jackbondpreston.me&amp;#x2F;images&amp;#x2F;sensor-watch&amp;#x2F;c7c8.jpg&quot;&gt;
&lt;img src=&quot;https:&amp;#x2F;&amp;#x2F;jackbondpreston.me&amp;#x2F;processed_images&amp;#x2F;c7c8.848a11200795defd.webp&quot; alt=&quot;a closeup of a green sensor watch PCB, with an area circled. the area contains some small, and closely grouped copper pads&quot; &#x2F;&gt;
&lt;&#x2F;a&gt;&lt;h2 id=&quot;software&quot;&gt;software&lt;a href=&quot;#software&quot; aria-label=&quot;Anchor link for: software&quot; class=&quot;header-link&quot;&gt;[&lt;img src=&quot;&#x2F;images&#x2F;link.svg&quot; &#x2F;&gt;]&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;at this point the watch was assembled with all components in place. did it work? at this stage, no idea. hopefully yes, and I could progress to the more familiar world of embedded software.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;bootloader&quot;&gt;bootloader&lt;&#x2F;h3&gt;
&lt;p&gt;the next necessary step is to flash the bootloader, so that we can put the firmware in place. unfortunately this requires a little more real-world action. we need to access the SWD points on the board to write the bootloader. ideally you could do this with some kind of
&lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Pogo_pin&quot;&gt;pogo pin&lt;&#x2F;a&gt; jig - and if you were doing any number exceeding about 5 I&#x27;m sure this would be worth the time. however, I decided to just solder some jump wires (stripped on one end, solid tip female on the other) to the points on the board. they&#x27;re all close, but it&#x27;s easy enough to do (albeit ugly). then I connected these to my
&lt;a href=&quot;https:&#x2F;&#x2F;www.adafruit.com&#x2F;product&#x2F;1501&quot;&gt;Adafruit Trinket M0&lt;&#x2F;a&gt; (PyRuler would also work).the pin mapping is as follows: SWD=0, SWC=1, RST=3, V+=3V, GND=GND.&lt;&#x2F;p&gt;
&lt;p&gt;I used the
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;joeycastillo&#x2F;Sensor-Watch&#x2F;blob&#x2F;main&#x2F;utils&#x2F;flash_watch_pyruler&#x2F;flash_watch_pyruler.ino&quot;&gt;flasher from the sensor watch repo to flash the bootloader&lt;&#x2F;a&gt;. note that you could build the bootloader yourself first and put the generated binary into bootloader.h - the source is located
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;joeycastillo&#x2F;uf2-samdx1&quot;&gt;here&lt;&#x2F;a&gt;. personally, I just used the prebuilt version from the repo. I had to change part of the Adafruit DAP library and add the SAM L22 DID to get this to work,
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;joeycastillo&#x2F;Sensor-Watch&#x2F;issues&#x2F;83#issuecomment-1229353899&quot;&gt;I provided the diff of this change&lt;&#x2F;a&gt; in a Sensor Watch GitHub issue (I just now am remembering I promised to upstream this, oops!). mercifully, I got the red blinky LED, and all was good! I unsoldered the wires from the board, and tried to clean up most of the solder blob to keep the board fairly flat.&lt;&#x2F;p&gt;
&lt;h3 id=&quot;movement&quot;&gt;movement&lt;&#x2F;h3&gt;
&lt;p&gt;now the bootloader is in place, the main firmware can be installed!
&lt;a href=&quot;https:&#x2F;&#x2F;www.sensorwatch.net&#x2F;docs&#x2F;movement&#x2F;&quot;&gt;the community firmware, Movement&lt;&#x2F;a&gt;is great, so this is what I installed. there are a bunch of different useful faces available, and more functionality is always being added.
flashing firmware was easy: I plugged the PCB into the end of a USB Micro B cable (plugged  on the other end into my computer) and double tapped the reset button (I find this has to be done quite quickly, using my fingernail was the trick to doing this reliably on such a small button). done successfully, the LED on the board pulses and a new drive labelled &quot;WATCHBOOT&quot; appears on the computer. now a built UF2 firmware file can just be dragged onto the device to flash, thanks to the bootloader flashed earlier. for the initial test, I just used a
&lt;a href=&quot;https:&#x2F;&#x2F;www.sensorwatch.net&#x2F;docs&#x2F;firmware&#x2F;prebuilt&#x2F;&quot;&gt;prebuilt image&lt;&#x2F;a&gt; to check everything was working. I flashed this, and the LED pulsed and turned off, signalling success.&lt;&#x2F;p&gt;
&lt;p&gt;from here I just assembled the watch with the Sensor Watch PCB, and it worked! I verified LED and buzzer function by playing around with various functionality. success!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;developing-on-movement&quot;&gt;developing on movement&lt;a href=&quot;#developing-on-movement&quot; aria-label=&quot;Anchor link for: developing-on-movement&quot; class=&quot;header-link&quot;&gt;[&lt;img src=&quot;&#x2F;images&#x2F;link.svg&quot; &#x2F;&gt;]&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;one face I found particularly cool was the &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;joeycastillo&#x2F;Sensor-Watch&#x2F;blob&#x2F;main&#x2F;movement&#x2F;watch_faces&#x2F;complication&#x2F;totp_face.c&quot;&gt;TOTP face&lt;&#x2F;a&gt;. I use &lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Time-based_one-time_password&quot;&gt;TOTP&lt;&#x2F;a&gt;
&lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Multi-factor_authentication&quot;&gt;2FA&lt;&#x2F;a&gt; on various accounts, so having access to the codes on my wrist at all times was really appealing. at the time, the TOTP face only supported one key - so I decided to improve it.&lt;&#x2F;p&gt;
&lt;p&gt;thankfully, Sensor Watch has an emulator for development. without this, development would be pretty tiresome with the flashing and reassembling of the watch getting tiring if you needed to iterate on some code and test it on the watch. the emulator runs inside the browser and uses
&lt;a href=&quot;https:&#x2F;&#x2F;en.wikipedia.org&#x2F;wiki&#x2F;Emscripten&quot;&gt;Emscripten&lt;&#x2F;a&gt;.
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;joeycastillo&#x2F;Sensor-Watch#using-the-movement-framework&quot;&gt;some minimal instructions on how to build this is available on the README&lt;&#x2F;a&gt;. this allowed me to extend the TOTP face easily and allow for multiple keys.
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;joeycastillo&#x2F;Sensor-Watch&#x2F;pull&#x2F;84&quot;&gt;my PR was merged&lt;&#x2F;a&gt;, and the functionality is now available for anyone to use. the keys are added at compile time, so they are baked into the firmware on flashing. for my purposes this is fine, as I never really change them. however, with the recent addition of a
&lt;a href=&quot;https:&#x2F;&#x2F;os.mbed.com&#x2F;blog&#x2F;entry&#x2F;littlefs-high-integrity-embedded-fs&#x2F;&quot;&gt;LittleFS&lt;&#x2F;a&gt; filesystem, the community have added &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;joeycastillo&#x2F;Sensor-Watch&#x2F;blob&#x2F;main&#x2F;movement&#x2F;watch_faces&#x2F;complication&#x2F;totp_face_lfs.c&quot;&gt;a version of the face which stores the keys on the filesystem&lt;&#x2F;a&gt;. awesome!&lt;&#x2F;p&gt;
&lt;p&gt;some more details on using Sensor Watch for TOTP is available
&lt;a href=&quot;https:&#x2F;&#x2F;blog.singleton.io&#x2F;posts&#x2F;2022-10-17-otp-on-wrist&#x2F;&quot;&gt;on this blog post&lt;&#x2F;a&gt;
(&lt;a href=&quot;https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=33243434&quot;&gt;HN discussion, if you dare&lt;&#x2F;a&gt;). it&#x27;s even running my code :)!&lt;&#x2F;p&gt;
&lt;h2 id=&quot;epilogue&quot;&gt;epilogue&lt;a href=&quot;#epilogue&quot; aria-label=&quot;Anchor link for: epilogue&quot; class=&quot;header-link&quot;&gt;[&lt;img src=&quot;&#x2F;images&#x2F;link.svg&quot; &#x2F;&gt;]&lt;&#x2F;a&gt;&lt;&#x2F;h2&gt;
&lt;p&gt;some summary thoughts:&lt;&#x2F;p&gt;
&lt;ul&gt;
&lt;li&gt;shoutout to Joey Castillo. for creating the Sensor Watch as a beautifully open source project (the fact I could independently make my own is what it&#x27;s all about!). for being
&lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;joeycastillo&#x2F;Sensor-Watch&#x2F;issues&#x2F;83&quot;&gt;so helpful and kind&lt;&#x2F;a&gt; when I asked for help. for having such a positive attitude towards those in the community who are using Sensor Watch to learn about all kinds of things (seriously, check out the
&lt;a href=&quot;https:&#x2F;&#x2F;discord.gg&#x2F;NtMVTBNca7&quot;&gt;Oddly Specific Objects Discord&lt;&#x2F;a&gt; to see how much this guy is giving to the community).&lt;&#x2F;li&gt;
&lt;li&gt;sometimes it&#x27;s worth just trying things that are difficult. this is my first time successfully doing and small-scale soldering of this kind, and it worked out great with some patience. having the motivation from making something I thought was really cool was an important factor here I think.&lt;&#x2F;li&gt;
&lt;li&gt;if you have a Sensor Watch (or are planning to!) please go ahead and &lt;a href=&quot;https:&#x2F;&#x2F;github.com&#x2F;joeycastillo&#x2F;Sensor-Watch&quot;&gt;contribute to movement&lt;&#x2F;a&gt; if you have a cool idea. I&#x27;m sure some reviews would be helpful to spot any issues on existing PRs before a maintainer gets to them to save some time.&lt;&#x2F;li&gt;
&lt;li&gt;the one issue I&#x27;ve had with using my Sensor Watch for TOTP is clock accuracy. the clock drifts over time, so I have to set the time once or twice a week to keep it nice and accurate for the TOTP functionality to be nice to use. but a community member is working on this, and it&#x27;s going to get a lot better. check out the Discord channel to see some seriously cool engineering going into this calibration effort.&lt;&#x2F;li&gt;
&lt;&#x2F;ul&gt;
&lt;p&gt;

&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;jackbondpreston.me&amp;#x2F;images&amp;#x2F;sensor-watch&amp;#x2F;watch1.jpg&quot;&gt;
&lt;img src=&quot;https:&amp;#x2F;&amp;#x2F;jackbondpreston.me&amp;#x2F;processed_images&amp;#x2F;watch1.8eb1a1bb66f57f56.webp&quot; alt=&quot;a shot of a yellow and black assembled sensor watch, lying horizontally on a surface&quot; &#x2F;&gt;
&lt;&#x2F;a&gt;


&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;jackbondpreston.me&amp;#x2F;images&amp;#x2F;sensor-watch&amp;#x2F;watch2.jpg&quot;&gt;
&lt;img src=&quot;https:&amp;#x2F;&amp;#x2F;jackbondpreston.me&amp;#x2F;processed_images&amp;#x2F;watch2.368eeb0770ca74e2.webp&quot; alt=&quot;a shot of a yellow and black assembled sensor watch, lying vertically on a surface&quot; &#x2F;&gt;
&lt;&#x2F;a&gt;


&lt;a href=&quot;https:&amp;#x2F;&amp;#x2F;jackbondpreston.me&amp;#x2F;images&amp;#x2F;sensor-watch&amp;#x2F;wrist.jpg&quot;&gt;
&lt;img src=&quot;https:&amp;#x2F;&amp;#x2F;jackbondpreston.me&amp;#x2F;processed_images&amp;#x2F;wrist.2912484e1c6755ee.webp&quot; alt=&quot;a shot of a yellow and black assembled sensor watch, on the author&amp;#x27;s wrist&quot; &#x2F;&gt;
&lt;&#x2F;a&gt;&lt;&#x2F;p&gt;
</content>
        
    </entry>
</feed>
