text
stringlengths
0
1.99k
./cheribuild.py run-morello-purecap
```
It will take a little while to boot, but once we're in (username "root",no
password), we can see that for the most part it looks and feels exactly
like regular FreeBSD. Here are a few things to note:
* If you want to shutdown the VM, the keyboard shortcut to kill a QEMU
console session is `Ctrl+a; x`.
* CheriBSD should have automatically spawned an SSH server for us which
QEMU should have port forwarded to 10005 for us. If you copied your
keys into the CheriBSD rootfs during the `disk-image-morello-purecap`
step, you should be able to just `ssh -p 10005 root@localhost` from
your host.
## Compiling programs for CheriBSD
Let's set ourselves up so that we can easily compile simple programs to
start probing how CHERI works. The current CHERI buildsystem is a bit
convoluted (e.g. going through `cheribuild.py`) but we're only going to
write a few short C programs that don't need all the heavy lifting that
provides. If you *do* want to explore more complex programs, then I suggest
you dive into how `cheribuild.py` works, but that's beyond the scope of
this article. It's worth pointing out that several open source projects can
already be built such as FFmpeg, Nginx, or even the Plasma desktop with
Wayland.
After running all the commands above, you'll have a `~/cheri` directory
with all the artifacts of the build. Staying in the `cheribuild` directory,
we'll create a folder called `vuln` where we'll store our intentionally
vulnerable programs. We'll *also* create a `vuln` folder in the CheriBSD VM
to keep things tidy. Create a bash script called `build.sh` in your
*HOST'S* `vuln` folder (i.e. under `cheribuild/`) with the following
contents:
```
#!/bin/sh
if [ "$#" -ne 2 ]; then
echo "Usage: $0 input.c output"
exit 1;
fi
~/cheri/output/morello-sdk/bin/clang \
-target aarch64-unknown-freebsd13 \
--sysroot=$HOME/cheri/output/rootfs-morello-purecap \
-B $HOME/cheri/output/morello-sdk/bin \
-mcpu=rainier \
-march=morello \
-mapi=purecap \
-Xclang -morello-vararg=new \
-Xclang -morello-bounded-memargs \
-Wall \
-Wcheri \
-g \
-fuse-ld=lld \
-o $2 \
$1 &&
scp -P 10005 $2 root@localhost:vuln/$2
```
Now we can write C programs on our host system and compile/upload them with
`./build.sh input.c output`! Let's crack on and explore CHERI...
## Capability Encoding
At this point, for our own understanding we should probably take a quick
look at what is contained in these extra bits of CHERI registers. The
precise encoding format for each CHERI-supported architecture varies a
little, but largely includes the same information. Note that whenever we
need to dissect a capability for its metadata, it's MUCH easier to just
rely on either GDB or the handy `%#p` format-specifier (more on that
shortly) to format it for us. But we're exploit developers, so we should
still have a solid understanding of how things work even if we'll end up
making the computer do the hard work for us. For Morello, a capability
register is defined as [3; Section 2.5]:
<- Bit 128 Bit 0 ->
+-+--------+--------+----------------+-----+------------------------------+
|T| Permi- | Object | Bounds |Flags| Bounds |
| | ssions | Type | (Upper) | | (Lower) |
+-+--------+--------+----------------+-----+------------------------------+
|T| Permi- | Object | Bounds |Flags| Value |
| | ssions | Type | (Upper) | | |
+-+--------+--------+----------------+-----+------------------------------+
* First comes the `T` bit which is the "tag bit" that we were talking about
earlier. This is the bit that indicates whether value in the register is a
valid capability or not. The architecture specifies that this bit isn't
actually loadable in the normal sense (being bit 128, it's really the 129th
bit yet we can only load 128-bits into a register), but instead comes from
the corresponding tag bit that the memory controller is responsible for
providing during loads/stores. This bit CAN be set by the CPU using special
instructions, for example when a new capability is being created
intentionally.
* Next up are the "Permissions" bits, of which there are 18 defined.
The format is as follows:
+-----+------------------+------------------------------------------------+
| Bit | Permission | Meaning |