forked from capablevms/cheri-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
From review comments from [Laurie](capablevms#88 (comment)) and [Andrei](capablevms#88 (comment)). Thanks both!
- Loading branch information
1 parent
b84412b
commit 6165f93
Showing
4 changed files
with
57 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,32 @@ | ||
#include "compartment_alloc.c" | ||
#include "../../include/common.h" | ||
#include "compartment_alloc.c" | ||
|
||
int main() { | ||
// Create capabilities which the heap compartments will construct capabilities against | ||
// (as if they were the DDC when the allocation is made). | ||
// One will be read-only, the other write-only, so we can differentiate when printing. | ||
void *__capability dc1 = cheri_perms_and(cheri_ddc_get(), CHERI_PERM_STORE); | ||
void *__capability dc2 = cheri_perms_and(cheri_ddc_get(), CHERI_PERM_LOAD); | ||
int main() | ||
{ | ||
// Create capabilities which the heap compartments will construct capabilities against | ||
// (as if they were the DDC when the allocation is made). | ||
// One will be read-only, the other write-only, so we can differentiate when printing. | ||
void *__capability dc1 = cheri_perms_and(cheri_ddc_get(), CHERI_PERM_STORE); | ||
void *__capability dc2 = cheri_perms_and(cheri_ddc_get(), CHERI_PERM_LOAD); | ||
|
||
// Create two compartments, and receive the capability that gives us authority to use them | ||
void *__capability compartment1 = init_compartment(4096, dc1); | ||
void *__capability compartment2 = init_compartment(4096, dc2); | ||
// Create two compartments, and receive the capability that gives us authority to use them | ||
void *__capability compartment1 = init_compartment(4096, dc1); | ||
void *__capability compartment2 = init_compartment(4096, dc2); | ||
|
||
// Allocate memory in each compartment | ||
void *__capability c1_allocated_memory = malloc_compartment(64, compartment1); | ||
void *__capability c2_allocated_memory = malloc_compartment(256, compartment2); | ||
// Allocate memory in each compartment | ||
void *__capability c1_allocated_memory = malloc_compartment(64, compartment1); | ||
void *__capability c2_allocated_memory = malloc_compartment(256, compartment2); | ||
|
||
// Print an explainer of each allocation (and the capability itself) | ||
printf("\n\n\tThis capability is heap-allocated within our first compartment, which is write-only.\n\tWe allocated 64 bytes.\n"); | ||
pp_cap(c1_allocated_memory); | ||
// Print an explainer of each allocation (and the capability itself) | ||
printf("\n\n\tThis capability is heap-allocated within our first compartment, which is " | ||
"write-only.\n\tWe allocated 64 bytes.\n"); | ||
pp_cap(c1_allocated_memory); | ||
|
||
printf("\n\n\tThis capability is heap-allocated within our second compartment, which is read-only.\n\tWe allocated 256 bytes.\n"); | ||
pp_cap(c2_allocated_memory); | ||
printf("\n\n\tThis capability is heap-allocated within our second compartment, which is " | ||
"read-only.\n\tWe allocated 256 bytes.\n"); | ||
pp_cap(c2_allocated_memory); | ||
|
||
printf("\nCompleted successfully.\n"); | ||
printf("\nCompleted successfully.\n"); | ||
|
||
return 0; | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,16 @@ | ||
#include <sys/sysctl.h> | ||
|
||
// A shortcut to turn a pointer into a sealed capability | ||
#define sealed_reference(ptr) cheri_seal((void *__capability)ptr, cheri_get_sealcap()) | ||
#define sealed_reference(ptr) cheri_seal((void *__capability) ptr, cheri_get_sealcap()) | ||
|
||
void *__capability cheri_get_sealcap() { | ||
void *__capability sealcap; | ||
size_t sealcap_size = sizeof(sealcap); | ||
if (sysctlbyname("security.cheri.sealcap", &sealcap, &sealcap_size, NULL, 0) < 0) | ||
void *__capability cheri_get_sealcap() | ||
{ | ||
void *__capability sealcap; | ||
size_t sealcap_size = sizeof(sealcap); | ||
if (sysctlbyname("security.cheri.sealcap", &sealcap, &sealcap_size, NULL, 0) < 0) | ||
{ | ||
printf("Fatal error. Cannot get `security.cheri.sealcap`."); | ||
exit(1); | ||
printf("Fatal error. Cannot get `security.cheri.sealcap`."); | ||
exit(1); | ||
} | ||
return sealcap; | ||
return sealcap; | ||
} | ||
|