text
stringlengths
0
1.99k
*or will it?*. Let's run it and see what happens!
```
root@cheribsd-morello-purecap:~/vuln # ./stack 01234
Calling overflow()
buffer @ 0xfffffff7fef0 [rwRW,0xfffffff7fef0-0xfffffff7ff00]
Returned from overflow()
```
Let's dissect this as it's our first actual capability:
```
buffer @ 0xfffffff7fef0 [rwRW,0xfffffff7fef0-0xfffffff7ff00]
| | |
| | +---------> The range that the capability is
| | valid for.
| |
| +--------------> The permissions: lower-case are
| for data, upper-case for
| capabilities.
|
+------------------------------> The "pointer"-component.
```
So, we can see that `&buffer` is bounded to only be able to access bytes in
the range `0xfffffff7fef0-0xfffffff7ff00`, which matches the size of the
`buffer` array in our program: 16 bytes. Furthermore, this capability can
be used to read and write both data and capabilities from this range, but
notably it *cannot fetch instructions*. Let's see what happens if we try to
run the program again, but supply it enough bytes to overflow `buffer`:
```
root@cheribsd-morello-purecap:~/vuln # ./stack 0123456789abcdef
Calling overflow()
buffer @ 0xfffffff7fef0 [rwRW,0xfffffff7fef0-0xfffffff7ff00]
In-address space security exception (core dumped)
```
Hmm, okay - we crashed. Not entirely unexpected, but let's find out why.
Fortunately we built GDB for CheriBSD so we can take a look at the coredump
that got generated.
```
root@cheribsd-morello-purecap:~/vuln # gdb -q ./stack stack.core
Reading symbols from ./stack...
[New LWP 100085]
Core was generated by `./stack 0123456789abcdef'.
Program terminated with signal SIGPROT, CHERI protection violation.
Capability bounds fault.
#0 0x000000004037c7f8 in strcpy (to=0xfffffff7ff00 [rwRW,0xfffffff7fef0-
0xfffffff7ff00] "\210\375\367\277\377\377", from=<optimized out>)
at /home/user/cheri/cheribsd/lib/libc/string/strcpy.c:48
```
As we may have guessed, our call to `strcpy()` triggered a CHERI exception,
and that's why the kernel killed our process. We can disassemble the
`overflow` function in GDB to get a closer look at the CHERI-augmented
Aarch64 instructions. However, it's probably easier for us to do this
analysis outside of CheriBSD. Fortunately, when we built the Morello SDK, a
version of binutils was compiled with Aarch64 CHERI support which lives in
`~/cheri/output/morello-sdk/bin/`. If you prefer to disassemble your
compiled binaries outside of the VM, then `objdump` in this directory will
work as expected. Alternatively, you can just `disas overflow` in GDB if
you prefer.
```
00000000000108e0 <overflow>:
108e0: 028183ff sub csp, csp, #96
108e4: 42827bfd stp c29, c30, [csp, #64]
108e8: 020103fd add c29, csp, #64
108ec: 020083e1 add c1, csp, #32
108f0: c2c83821 scbnds c1, c1, #16 // =16
108f4: c20007e1 str c1, [csp, #16]
108f8: a21f03a0 stur c0, [c29, #-16]
108fc: c2c1d3e0 mov c0, csp
10900: c2000001 str c1, [c0, #0]
10904: c2c83809 scbnds c9, c0, #16 // =16
10908: 90800080 adrp c0, 0x20000 <main+0x18>
1090c: c2428400 ldr c0, [c0, #2576]
10910: 94000034 bl 0x109e0 <printf@plt>
10914: c24007e0 ldr c0, [csp, #16]
10918: a25f03a1 ldur c1, [c29, #-16]
1091c: 94000035 bl 0x109f0 <strcpy@plt>
10920: 42c27bfd ldp c29, c30, [csp, #64]
10924: 020183ff add csp, csp, #96
10928: c2c253c0 ret c30
1092c: d503201f nop
```
Now, even if your somewhat familiar with Aarch64 assembly, this probably
looks quite strange to you. Not to worry - this really is Aarch64
assembly, but just has a few extras added on. Seeing as this is our first
crash in CHERI code, let's walk through what's going on.
The first four instructions in the prologue to `overflow()` at first appear
to be pretty familiar; namely `sub`, `stp` and two `add`s. However, upon
closer inspection we see that the registers in these instructions aren't
the familiar Aarch64 ones. Instead, they've been replaced by `c`-variants,
which are the CHERI-ised double-width versions that we've already talked
about. As you might expect, `csp` is the "CHERI stack pointer" and `c1`,