-
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.
This commit introduces exercise 2 for using CHERIoT Audit to audit firmware alongside Rego policies. This exercise involves writing a policy to ensure that only specified functions are running with interrupts disabled.
- Loading branch information
1 parent
0840908
commit 367d090
Showing
5 changed files
with
242 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
27 changes: 27 additions & 0 deletions
27
exercises/firmware_auditing/part_2/bad_disable_interrupts.cc
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,27 @@ | ||
// Copyright lowRISC Contributors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include <compartment.h> | ||
|
||
[[cheri::interrupt_state(disabled)]] int not_allowed() | ||
{ | ||
int x = 0; | ||
x += 1; | ||
return x; | ||
} | ||
|
||
[[gnu::noinline]] int other_function() | ||
{ | ||
return 1000000; | ||
} | ||
|
||
/// Thread entry point. | ||
void __cheri_compartment("bad_disable_interrupts") entry_point() | ||
{ | ||
int y = not_allowed(); | ||
other_function(); | ||
while (y < other_function()) | ||
{ | ||
y += 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,41 @@ | ||
// Copyright lowRISC Contributors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
#include <compartment.h> | ||
|
||
[[gnu::noinline]] [[cheri::interrupt_state(disabled)]] int | ||
run_without_interrupts(int x) | ||
{ | ||
return x + 1; | ||
} | ||
|
||
[[gnu::noinline]] [[cheri::interrupt_state(disabled)]] void | ||
also_without_interrupts(int *x) | ||
{ | ||
*x = 1; | ||
} | ||
|
||
[[gnu::noinline]] void other_function_one() | ||
{ | ||
int x = 3; | ||
} | ||
|
||
[[gnu::noinline]] int other_function_two() | ||
{ | ||
return 3; | ||
} | ||
|
||
[[gnu::noinline]] int other_function_three(int arg1, int arg2) | ||
{ | ||
return arg1 + arg2; | ||
} | ||
|
||
/// Thread entry point. | ||
void __cheri_compartment("disable_interrupts") entry_point() | ||
{ | ||
other_function_one(); | ||
int y = other_function_two(); | ||
y = run_without_interrupts(3); | ||
y += other_function_three(4, 7); | ||
also_without_interrupts(&y); | ||
} |
66 changes: 66 additions & 0 deletions
66
exercises/firmware_auditing/part_2/interrupt_disables.rego
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,66 @@ | ||
# Copyright lowRISC Contributors. | ||
# SPDX-License-Identifier: Apache-2.0 | ||
package interrupts | ||
|
||
import future.keywords | ||
|
||
# Note - Any compartment not in this array is not checked by default, | ||
# and so are allowed to have disabled interrupts. | ||
required_disabled_interrupts := [ | ||
{ | ||
"compartment": "disable_interrupts", | ||
"functions": {"run_without_interrupts(int)", "also_without_interrupts(int*)"} | ||
}, { | ||
"compartment": "bad_disable_interrupts", | ||
"functions": {} | ||
# Uncomment the below line (and comment the above line) to allow the disallowed | ||
# function to be run with interrupts disabled | ||
#"functions": {"not_allowed()"} | ||
} | ||
] | ||
required_compartments := {x.compartment | x = required_disabled_interrupts[_]} | ||
|
||
all_exports := [ | ||
{"owner": owner, "export": export} | export = input.compartments[owner].exports[_] | ||
] | ||
|
||
all_required_compartments_present { | ||
compartments := {name | input.compartments[name]} | ||
required_compartments == required_compartments & compartments | ||
} | ||
|
||
exports_with_interrupt_status(status) := [ | ||
export | export = all_exports[_] ; export["export"]["interrupt_status"] = status | ||
] | ||
|
||
patched_export_entry_demangle(compartment, export_symbol) := demangled { | ||
startswith(export_symbol, "__library_export_") | ||
modified_export = concat("", ["__export_", substring(export_symbol, 17, -1)]) | ||
demangled := export_entry_demangle(compartment, modified_export) | ||
} | ||
patched_export_entry_demangle(compartment, export_symbol) := demangled { | ||
not startswith(export_symbol, "__library_export_") | ||
demangled := export_entry_demangle(compartment, export_symbol) | ||
} | ||
|
||
compartment_only_required_disabled_interrupts(compartment) { | ||
symbols := { | ||
patched_export_entry_demangle(e["owner"], e["export"]["export_symbol"]) | | ||
e = exports_with_interrupt_status("disabled")[_] ; | ||
e["owner"] = compartment["compartment"] | ||
} | ||
required := {func | _ = compartment["functions"][func]} | ||
symbols == required | ||
} | ||
|
||
only_required_disabled_interrupts_present { | ||
every compartment in required_disabled_interrupts { | ||
compartment_only_required_disabled_interrupts(compartment) | ||
} | ||
} | ||
|
||
default valid := false | ||
valid { | ||
all_required_compartments_present | ||
only_required_disabled_interrupts_present | ||
} |
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