Demonstrate CHERI Tag Protection
This exercise demonstrates CHERI's capability provenance tags, in particular by showing that capabilities and their constituent bytes are subtly different things!
-
Compile
corrupt-pointer.cfor the baseline architecture to the binarycorrupt-pointer-baselineand for the CHERI-aware architecture tocorrupt-pointer-cheri. -
Run both programs and observe the output.
-
Use
gdbto inspect theSIGPROTthrown to the CHERI program.Print out the pseudoregister
$_siginfo.si_signo34isSIGPROT, a new signal introduced for conveying CHERI traps to user programs. Thesi_codevalues forSIGPROTsignals are defined as the variousPROT_CHERI_*values in<sys/signal.h>(which can be found in/usr/includein a CheriBSD system). -
Examine the disassembly of the construction of
q,uint8_t *q = (uint8_t*)(((uintptr_t)p.ptr) & ~0xFF) + 5;and the byte-wise mutation of
p.ptrto constructr,p.bytes[0] = 5; uint8_t *r = p.ptr;in both baseline and CHERI-enabled programs.
What stands out?
-
Given that
qandrappear to have identical byte representation in memory, why does the CHERI version crash when dereferencingr?
Source
corrupt-pointer.c
/*
* SPDX-License-Identifier: BSD-2-Clause
* Copyright (c) 2022 Microsoft Corporation
*/
#include <assert.h>
#include <stdint.h>
#include <stdio.h>
#ifdef __CHERI_PURE_CAPABILITY__
#define PRINTF_PTR "#p"
#else
#define PRINTF_PTR "p"
#endif
int
main(void)
{
char buf[0x1FF];
volatile union {
char *ptr;
char bytes[sizeof(char*)];
} p;
for (size_t i = 0; i < sizeof(buf); i++) {
buf[i] = i;
}
p.ptr = &buf[0x10F];
printf("buf=%" PRINTF_PTR " &p=%" PRINTF_PTR "\n", buf, &p);
printf("p.ptr=%" PRINTF_PTR " (0x%zx into buf) *p.ptr=%02x\n",
p.ptr, p.ptr - buf, *p.ptr);
/* One way to align the address down */
char *q = (char*)(((uintptr_t)p.ptr) & ~0xFF);
printf("q=%" PRINTF_PTR " (0x%zx into buf)\n", q, q - buf);
printf("*q=%02x\n", *q);
/* Maybe another, assuming a little-endian machine. */
p.bytes[0] = 0;
char *r = p.ptr;
printf("r=%" PRINTF_PTR " (0x%zx)\n", r, r - buf);
printf("*r=%02x\n", *r);
return 0;
}
Courseware
This exercise has presentation materials available.