-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add firmware auditing exercise 3 & further ideas
This commit introduces exercise 3 for using CHERIoT Audit to audit firmware alongside Rego policies. This exercise involves writing a policy to ensure that specific allocation limits are respected, with individual allocation limits per allocation capability, per compartment (total) and across all compartments.
- Loading branch information
1 parent
367d090
commit 3420747
Showing
7 changed files
with
297 additions
and
0 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,19 @@ | ||
// Copyright lowRISC Contributors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#define MALLOC_QUOTA 1024 // Quota of 1 KiB | ||
|
||
#include <compartment.h> | ||
|
||
/// Thread entry point. | ||
[[noreturn]] void __cheri_compartment("malloc1024") entry_point() | ||
{ | ||
void *mem = malloc(1024 * sizeof(char)); | ||
free(mem); | ||
|
||
int x = 0; | ||
while (true) | ||
{ | ||
x = x + 1; | ||
}; | ||
} |
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,19 @@ | ||
// Copyright lowRISC Contributors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#define MALLOC_QUOTA 2048 // Quota of 2 KiB | ||
|
||
#include <compartment.h> | ||
|
||
/// Thread entry point. | ||
[[noreturn]] void __cheri_compartment("malloc2048") entry_point() | ||
{ | ||
void *mem = malloc(2048 * sizeof(char)); | ||
free(mem); | ||
|
||
int x = 0; | ||
while (true) | ||
{ | ||
x = x + 1; | ||
}; | ||
} |
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,19 @@ | ||
// Copyright lowRISC Contributors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#define MALLOC_QUOTA 4096 // Quota of 4 KiB | ||
|
||
#include <compartment.h> | ||
|
||
/// Thread entry point. | ||
[[noreturn]] void __cheri_compartment("malloc4096") entry_point() | ||
{ | ||
void *mem = malloc(4096 * sizeof(char)); | ||
free(mem); | ||
|
||
int x = 0; | ||
while (true) | ||
{ | ||
x = x + 1; | ||
}; | ||
} |
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,51 @@ | ||
# Copyright lowRISC Contributors. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
package malloc_check | ||
|
||
import future.keywords | ||
|
||
allocator_capabilities := [ | ||
{"owner": owner, "capability": data.rtos.decode_allocator_capability(cap)} | | ||
cap = input.compartments[owner].imports[_] ; | ||
data.rtos.is_allocator_capability(cap) | ||
] | ||
|
||
all_allocations_leq(limit) { | ||
every cap in allocator_capabilities { | ||
cap.capability.quota <= limit | ||
} | ||
} | ||
|
||
unique_allocator_compartments contains owner if { | ||
some {"owner": owner} in allocator_capabilities | ||
} | ||
|
||
total_quota_per_compartment[owner] = quota if { | ||
some owner in unique_allocator_compartments | ||
quota := sum([cap.capability.quota | cap = allocator_capabilities[_]; cap.owner == owner]) | ||
} | ||
|
||
all_compartments_allocate_leq(limit) { | ||
some quotas | ||
quotas = [ quota | quota = total_quota_per_compartment[_] ] | ||
every quota in quotas { | ||
quota <= limit | ||
} | ||
} | ||
|
||
total_quota := sum([cap.capability.quota | cap = allocator_capabilities[_]]) | ||
|
||
total_allocation_leq(limit) { | ||
total_quota <= limit | ||
} | ||
|
||
default valid := false | ||
valid { | ||
data.rtos.all_sealed_allocator_capabilities_are_valid | ||
# No individual allocation should be able to allocate more than 20 KiB at one time | ||
all_allocations_leq(20480) | ||
# No individual compartment should be able to allocate more than 40 KiB simultaneously | ||
all_compartments_allocate_leq(40960) | ||
# The entire firmware image cannot allocate more than 100 KiB simultaneously | ||
total_allocation_leq(102400) | ||
} |
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,58 @@ | ||
// Copyright lowRISC Contributors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#define MALLOC_QUOTA 1024 // Quota of 1 KiB | ||
|
||
#include <compartment.h> | ||
|
||
DECLARE_AND_DEFINE_ALLOCATOR_CAPABILITY(__second_malloc_capability, | ||
MALLOC_QUOTA * 2) | ||
DECLARE_AND_DEFINE_ALLOCATOR_CAPABILITY(__third_malloc_capability, | ||
MALLOC_QUOTA * 4) | ||
DECLARE_AND_DEFINE_ALLOCATOR_CAPABILITY(__fourth_malloc_capability, | ||
MALLOC_QUOTA * 8) | ||
DECLARE_AND_DEFINE_ALLOCATOR_CAPABILITY(__fifth_malloc_capability, | ||
MALLOC_QUOTA * 16) | ||
#define MALLOC_CAPABILITY_2 STATIC_SEALED_VALUE(__second_malloc_capability) | ||
#define MALLOC_CAPABILITY_3 STATIC_SEALED_VALUE(__third_malloc_capability) | ||
#define MALLOC_CAPABILITY_4 STATIC_SEALED_VALUE(__fourth_malloc_capability) | ||
#define MALLOC_CAPABILITY_5 STATIC_SEALED_VALUE(__fifth_malloc_capability) | ||
|
||
#define MALLOC_WITH_CAPABILITY_FUNC(name, malloc_num) \ | ||
static inline void *malloc##malloc_num(size_t size) \ | ||
{ \ | ||
Timeout t = {0, MALLOC_WAIT_TICKS}; \ | ||
void *ptr = \ | ||
heap_allocate(&t, name, size, AllocateWaitRevocationNeeded); \ | ||
if (!__builtin_cheri_tag_get(ptr)) \ | ||
{ \ | ||
ptr = NULL; \ | ||
} \ | ||
return ptr; \ | ||
} | ||
|
||
MALLOC_WITH_CAPABILITY_FUNC(MALLOC_CAPABILITY_2, 2) | ||
MALLOC_WITH_CAPABILITY_FUNC(MALLOC_CAPABILITY_3, 3) | ||
MALLOC_WITH_CAPABILITY_FUNC(MALLOC_CAPABILITY_4, 4) | ||
MALLOC_WITH_CAPABILITY_FUNC(MALLOC_CAPABILITY_5, 5) | ||
|
||
/// Thread entry point. | ||
[[noreturn]] void __cheri_compartment("malloc_many") entry_point() | ||
{ | ||
void *mem = malloc(MALLOC_QUOTA * sizeof(char)); | ||
free(mem); | ||
void *mem2 = malloc2(MALLOC_QUOTA * 2 * sizeof(char)); | ||
free(mem2); | ||
void *mem3 = malloc3(MALLOC_QUOTA * 4 * sizeof(char)); | ||
free(mem3); | ||
void *mem4 = malloc4(MALLOC_QUOTA * 8 * sizeof(char)); | ||
free(mem4); | ||
void *mem5 = malloc5(MALLOC_QUOTA * 16 * sizeof(char)); | ||
free(mem5); | ||
|
||
int x = 0; | ||
while (true) | ||
{ | ||
x = x + 1; | ||
}; | ||
} |
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