text
stringlengths
0
1.99k
+-----+------------------+------------------------------------------------+
| 17 | Load | Can load bytes from memory |
| 16 | Store | Can store bytes into memory |
| 15 | Execute | Can fetch instructions from memory |
| 14 | LoadCap | Can load a capability to a register |
| 13 | StoreCap | Can store a capability from a register |
| 12 | StoreLocalCap | Can store a "local" (see "Global" below) |
| | | capability from a register |
| 11 | Seal | Can "seal" an unsealed capability |
| 10 | Unseal | Can "unseal" a sealed capability |
| 9 | System | Can access system registers |
| 8 | BranchSealedPair | Can be used by a "branch sealed pair" |
| | | instruction |
| 7 | CompartmentID | Indicates that this capability is a |
| | | "compartment" ID |
| 6 | MutableLoad | Loading a capability using a capability without|
| | | this bit will clear the Store* and MutableLoad |
| | | permissions |
| 5-2 | User[3:0] | Software-defined |
| 1 | Executive | Indicates an instruction fetch executes in |
| | | executive vs restrictive mode (visibility of |
| | | global registers) |
| 0 | Global | Indicates whether this capability is local/ |
| | | global |
+-----+------------------+------------------------------------------------+
There are a couple of terms in the above table that we haven't covered yet
(and some we won't cover). Don't worry too much for now, if we don't cover
it in this article, by the time you reach the end, you'll be able to go and
research into them further.
* Following the permissions is the "ObjectType" (15 bits) which indicates
whether and how a capability is "sealed". A sealed capability is one that
is valid (i.e. it is recognised as a capability), but is not allowed to be
used (apart from being unsealed). This is useful, for example, when passing
capabilities between different contexts or threads. The use of sealed
capabilities is important in "CHERI compartmentalisation". Associating an
ObjectType with a sealed capability allows for finer granularity in
identifying sealed capabilities with "types".
* The encoding of the "Bounds" field is pretty complex. If you want to read
up on it yourself, you can [3; Section 2.5.1], but for the purposes of this
article, suffice to say that the bounds field potentially takes up 87 bits
and overlaps with the value and flags fields. Determining the bounds of a
capability depends on the context.
* The "Flags" field is just 8 bits and is up to the user to device if and
how to use. There are CHERI instructions like `BICFLG` for Aarch64 (bit-
wise clear immediate on flags) for operating directly on this field without
clearing the tag bit.
* Lastly, the "Value" field comprises the lower 64 bits of the capability.
As the name indicates, this is the actual value that we think of
numerically being stored in the register. It could be an integer (in the
case where we have data rather than a capability) or a memory address (in
the event where we DO have a capability).
This all may seem like a lot but, as you'll see in our examples, we really
don't need to spend any time decoding capabilities manually, and any
information that we *do* need is very easy to extract.
## Vulnerable Programs
### A Simple Stack Buffer Overflow
Let's start by trying to exploit the canonical stack buffer overflow, we'll
call it `stack.c`:
```
#include <stdio.h>
#include <string.h>
void __attribute__((noinline)) overflow(char* src) {
char buffer[16];
printf("buffer @ %#p\n", (char*)&buffer);
strcpy(buffer, src);
}
int main(int argc, char** argv) {
if (argc >= 2) {
printf("Calling overflow()\n");
overflow(argv[1]);
printf("Returned from overflow()\n");
}
return 0;
}
```
Two things to notice briefly:
* We use the `noinline` attribute on `overflow()` to prevent Clang from
optimising out the function call.
* When we print `&buffer` (which is a CAPABILITY), we use the `%#p`
format specifier. This is specific to the CHERI SDK and will print the
metadata about the capability in a pretty way.
We can compile and upload this with: `./build.sh stack.c stack`. Over in
the VM, we should now have a `~/vuln/stack` binary waiting for us. The
program itself should be fairly obvious - passing an argument of more than
16 bytes will overflow the `buffer` array in the `overflow()` function...