-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
More examples in the compartments and libraries chapter.
- Loading branch information
1 parent
eed4ecb
commit 58a32fe
Showing
13 changed files
with
336 additions
and
29 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright CHERIoT Contributors. | ||
// SPDX-License-Identifier: MIT | ||
|
||
#include "hello.h" | ||
#include <cheri.hh> | ||
|
||
using namespace CHERI; | ||
|
||
/// Thread entry point. | ||
void __cheri_compartment("hello") entry() | ||
{ | ||
// attacks#begin | ||
char unterminatedString[] = { | ||
'N', 'o', ' ', 'n', 'u', 'l', 'l'}; | ||
uart_puts(unterminatedString); | ||
Capability invalidPermissions = "Invalid permissions"; | ||
invalidPermissions.permissions() &= Permission::Store; | ||
uart_puts(invalidPermissions); | ||
char *invalidPointer = reinterpret_cast<char *>(12345); | ||
uart_puts(invalidPointer); | ||
uart_puts("Non-malicious string"); | ||
// attacks#end | ||
} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright CHERIoT Contributors. | ||
// SPDX-License-Identifier: MIT | ||
|
||
#include <compartment-macros.h> | ||
|
||
/** | ||
* Write `msg` to the default UART, including a trailing | ||
* newline. | ||
* | ||
* Returns 0 on success, or a negative error code on | ||
* failure. | ||
* | ||
* If the string is not null-terminated, prints only the | ||
* length of the capability. | ||
*/ | ||
int __cheri_compartment("uart") uart_puts(const char *msg); |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright CHERIoT Contributors. | ||
// SPDX-License-Identifier: MIT | ||
|
||
#include "hello.h" | ||
#include <debug.hh> | ||
#include <futex.h> | ||
#include <locks.hh> | ||
#include <platform-uart.hh> | ||
|
||
// Import some useful things from the CHERI namespace. | ||
using namespace CHERI; | ||
|
||
// safe_uart#begin | ||
/// Write a message to the UART. | ||
int uart_puts(const char *msg) | ||
{ | ||
static FlagLockPriorityInherited lock; | ||
// Prevent concurrent invocation | ||
LockGuard g(lock); | ||
Timeout t{UnlimitedTimeout}; | ||
// Make sure that this is not going to be deallocated out | ||
// from under us. | ||
if (heap_claim_fast(&t, msg) != 0) | ||
{ | ||
return -EINVAL; | ||
} | ||
// Check that this is a valid pointer with the correct | ||
// permissions. | ||
if (!check_pointer<PermissionSet{Permission::Load}>(msg)) | ||
{ | ||
return -EINVAL; | ||
} | ||
// Get the bounds (distance from address to top) of the | ||
// pointer. | ||
Capability buffer{msg}; | ||
size_t length = buffer.bounds(); | ||
// Write the data, one byte at a time. | ||
for (size_t i = 0; i < length; i++) | ||
{ | ||
char c = msg[i]; | ||
if (c == '\0') | ||
{ | ||
break; | ||
} | ||
MMIO_CAPABILITY(Uart, uart)->blocking_write(c); | ||
} | ||
MMIO_CAPABILITY(Uart, uart)->blocking_write('\n'); | ||
return 0; | ||
} | ||
// safe_uart#end |
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
-- Copyright Microsoft and CHERIoT Contributors. | ||
-- SPDX-License-Identifier: MIT | ||
|
||
set_project("CHERIoT example") | ||
|
||
sdkdir = os.getenv("CHERIOT_SDK") or | ||
"../../../rtos-source/sdk/" | ||
includes(sdkdir) | ||
|
||
set_toolchains("cheriot-clang") | ||
|
||
option("board") | ||
set_default("sail") | ||
|
||
-- An example compartment that we can call | ||
compartment("uart") | ||
add_files("uart.cc") | ||
|
||
-- Our entry-point compartment | ||
compartment("hello") | ||
add_files("hello.cc") | ||
|
||
-- Firmware image for the example. | ||
firmware("hello_world") | ||
-- RTOS-provided libraries | ||
add_deps("freestanding", "compartment_helpers") | ||
-- Our compartments | ||
add_deps("hello", "uart") | ||
on_load(function(target) | ||
-- The board to target | ||
target:values_set("board", "$(board)") | ||
-- Threads to select | ||
target:values_set("threads", { | ||
{ | ||
compartment = "hello", | ||
priority = 1, | ||
entry_point = "entry", | ||
stack_size = 0x400, | ||
trusted_stack_frames = 2 | ||
} | ||
}, {expand = false}) | ||
end) |
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,9 +1,8 @@ | ||
#include <compartment-macros.h> | ||
|
||
/** | ||
* Check whether a guess is correct. The compartment holds a secret value that | ||
* is a number from 0-9. Returns true if the guess is correct. | ||
* Check whether a guess is correct. The compartment holds | ||
* a secret value that is a number from 0-9. Returns true | ||
* if the guess is correct. | ||
*/ | ||
__cheri_compartment("safebox") | ||
bool check_guess(int guess); | ||
|
||
__cheri_compartment("safebox") bool check_guess(int guess); |
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 |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Copyright CHERIoT Contributors. | ||
// SPDX-License-Identifier: MIT | ||
|
||
#include "hello.h" | ||
#include <cheri.hh> | ||
|
||
using namespace CHERI; | ||
|
||
/// Thread entry point. | ||
void __cheri_compartment("hello") entry() | ||
{ | ||
// attacks#begin | ||
char unterminatedString[] = { | ||
'N', 'o', ' ', 'n', 'u', 'l', 'l'}; | ||
uart_puts(unterminatedString); | ||
Capability invalidPermissions = "Invalid permissions"; | ||
invalidPermissions.permissions() &= Permission::Store; | ||
uart_puts(invalidPermissions); | ||
char *invalidPointer = reinterpret_cast<char *>(12345); | ||
uart_puts(invalidPointer); | ||
uart_puts("Non-malicious string"); | ||
// attacks#end | ||
} |
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 |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// Copyright CHERIoT Contributors. | ||
// SPDX-License-Identifier: MIT | ||
|
||
#include <compartment-macros.h> | ||
|
||
/** | ||
* Write `msg` to the default UART, including a trailing | ||
* newline. | ||
* | ||
* Returns 0 on success, or a negative error code on | ||
* failure. | ||
* | ||
* If the string is not null-terminated, prints only the | ||
* length of the capability. | ||
*/ | ||
int __cheri_compartment("uart") uart_puts(const char *msg); |
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 |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright CHERIoT Contributors. | ||
// SPDX-License-Identifier: MIT | ||
|
||
#include "hello.h" | ||
#include <debug.hh> | ||
#include <futex.h> | ||
#include <locks.hh> | ||
#include <platform-uart.hh> | ||
#include <unwind.h> | ||
|
||
// Import some useful things from the CHERI namespace. | ||
using namespace CHERI; | ||
|
||
// safe_uart#begin | ||
/// Write a message to the UART. | ||
int uart_puts(const char *msg) | ||
{ | ||
// Prevent information disclosure, check that this does | ||
// not overlap with our stack region. Check for obvious | ||
// errors at the same time. | ||
if (!check_pointer(msg)) | ||
{ | ||
return -EINVAL; | ||
} | ||
static FlagLockPriorityInherited lock; | ||
// Prevent concurrent invocation | ||
LockGuard g(lock); | ||
int result = 0; | ||
// Assume this is a null-terminated string, report an | ||
// error on exceptions if not. | ||
on_error( | ||
[&]() { | ||
for (const char *m = msg; *m != '\0'; m++) | ||
{ | ||
MMIO_CAPABILITY(Uart, uart)->blocking_write(*m); | ||
} | ||
}, | ||
[&]() { result = -EINVAL; }); | ||
MMIO_CAPABILITY(Uart, uart)->blocking_write('\n'); | ||
return result; | ||
} | ||
// safe_uart#end |
Oops, something went wrong.