text
stringlengths
0
1.99k
In my view, I don't quite see why this idea of quarantining should be
preferred over zeroing memory as part of the free operation as this would
also have the added benefit of removing the tag bit from any capabilities
that were contained *within* the allocation (for example, if a struct
containing function pointers was allocated on the heap) and therefore
preventing legitimate capabilities from possibly being re-used at a later
time. Perhaps the reasoning is to do with memory latency again and the cost
of zeroing arbitrarily large memory regions during `free()`.
Let's see an example of the quarantining allocator in action:
```
#include <stdio.h>
#include <stdlib.h>
int main(void) {
void* ptr = NULL;
ptr = malloc(64);
printf("ptr @ %#p\n", ptr);
free(ptr);
ptr = malloc(64);
printf("ptr @ %#p\n", ptr);
free(ptr);
return 0;
}
```
On a non-CHERI system, we'd typically expect to see the same address
printed each time for `ptr`, despite the fact that we've free'd and
malloc'd in between the calls to `printf`. However, in our CheriBSD VM,
we see:
```
root@cheribsd-morello-purecap:~/vuln # ./heap1
ptr @ 0x40c0f000 [rwRW,0x40c0f000-0x40c0f040]
ptr @ 0x40c0f040 [rwRW,0x40c0f040-0x40c0f080]
```
Notice how the `ptr` capability changes - in fact the second capability is
always 64 bytes after the first. This is because, despite freeing `ptr`,
the memory it pointed to has been quarantined. The next time we try to
allocate something, we get the next free *non-quarantined* chunk which
happens to be immediately after the first chunk that we got.
How does any of this affect exploitation? Well, let's try to concoct a very
simple use-after-free example to see if and how CHERI complains to us:
```
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char* ptr1 = malloc(64);
strcpy(ptr1, "CHERI1");
free(ptr1);
/* Leave this commented out for, we'll come back to it afterwards */
//malloc_revoke();
char* ptr2 = malloc(64);
strcpy(ptr2, "CHERI2");
free(ptr2);
printf("ptr1 @ %#p => %s\n", ptr1, ptr1);
printf("ptr2 @ %#p => %s\n", ptr2, ptr2);
return 0;
}
```
On a non-CHERI system, the behaviour of the above program depends on the
allocator. On my host system, the addresses allocated for both `ptr1` and
`ptr2` are the same, but contain junk data by the time `printf` is called.
On a different system with a different allocator, the strings `CHERI1` and
`CHERI2` might still be present. However, in the CheriBSD VM, we see:
```
root@cheribsd-morello-purecap:~/vuln # ./heap2
ptr1 @ 0x40c0f000 [rwRW,0x40c0f000-0x40c0f040] => CHERI1
ptr2 @ 0x40c0f040 [rwRW,0x40c0f040-0x40c0f080] => CHERI2
```
In particular, notice how `ptr1` and `ptr2` DO NOT have the same value
despite `ptr2` being allocated after `ptr1` was freed. This is the
quarantining allocator at work again. Notice also that the `CHERI1` and
`CHERI2` strings are still in place. This tells us that the call to
`free()` *doesn't clear the tag bit from the pointers that are passed to
it*.
If we now uncomment the call to `malloc_revoke()` between the two
allocations, we get a very different result:
```
root@cheribsd-morello-purecap:~/vuln # ./heap2
In-address space security exception (core dumped)