Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 21 additions & 37 deletions app/umode.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,72 +12,56 @@ extern uint32_t __switch_sp(uint32_t new_sp);
*/
void umode_validation_task(void)
{
/* --- Phase 1: Kernel Stack Isolation Test --- */
umode_printf("Phase 1: Testing Kernel Stack Isolation\n");
umode_printf("\n");

/* Test 1-1: Baseline - Syscall with normal SP */
umode_printf("Test 1-1: sys_tid() with normal SP\n");
/* Test 1: Basic syscall */
umode_printf("Test 1: Basic syscall\n");
umode_printf("Calling sys_tid()...\n");
int my_tid = sys_tid();
if (my_tid > 0) {
umode_printf("PASS: sys_tid() returned %d\n", my_tid);
umode_printf("[PASS] returned tid=%d\n", my_tid);
} else {
umode_printf("FAIL: sys_tid() failed (ret=%d)\n", my_tid);
umode_printf("[FAIL] returned tid=%d\n", my_tid);
}
umode_printf("\n");

/* Test 1-2: Verify ISR uses mscratch, not malicious user SP */
umode_printf("Test 1-2: sys_tid() with malicious SP\n");
/* Test 2: Syscall with corrupted SP */
umode_printf("Test 2: Syscall with corrupted SP\n");
umode_printf("Setting SP to 0xDEADBEEF...\n");

uint32_t saved_sp = __switch_sp(0xDEADBEEF);
int my_tid_bad_sp = sys_tid();
__switch_sp(saved_sp);

if (my_tid_bad_sp > 0) {
umode_printf(
"PASS: sys_tid() succeeded, ISR correctly used kernel "
"stack\n");
umode_printf("[PASS] kernel stack isolation working\n");
} else {
umode_printf("FAIL: Syscall failed with malicious SP (ret=%d)\n",
my_tid_bad_sp);
umode_printf("[FAIL] syscall failed (ret=%d)\n", my_tid_bad_sp);
}
umode_printf("\n");

/* Test 1-3: Verify syscall functionality is still intact */
umode_printf("Test 1-3: sys_uptime() with normal SP\n");
/* Test 3: Syscall after recovery */
umode_printf("Test 3: Syscall after recovery\n");
umode_printf("Calling sys_uptime()...\n");
int uptime = sys_uptime();
if (uptime >= 0) {
umode_printf("PASS: sys_uptime() returned %d\n", uptime);
umode_printf("[PASS] returned uptime=%d\n", uptime);
} else {
umode_printf("FAIL: sys_uptime() failed (ret=%d)\n", uptime);
umode_printf("[FAIL] returned uptime=%d\n", uptime);
}
umode_printf("\n");

umode_printf("Phase 1 All tests passed.\n");
umode_printf("\n");

/* --- Phase 2: Security Check (Privileged Access) --- */
umode_printf("========================================\n");
umode_printf("\n");
umode_printf("Phase 2: Testing Security Isolation\n");
umode_printf("\n");
umode_printf("Action: Attempting to read 'mstatus' CSR from U-mode.\n");
umode_printf("Expect: Kernel Panic with 'Illegal instruction'.\n");
umode_printf("\n");
/* Delay before suicide to ensure logs are flushed from
* buffer to UART.
/* Test 4: Privileged CSR access
* Delay before triggering exception to ensure logs are flushed.
*/
umode_printf("Test 4: Privileged CSR access\n");
sys_tdelay(10);

/* Privileged Instruction Trigger */
umode_printf("Reading mstatus from U-mode...\n");
umode_printf("Result: \n");
uint32_t mstatus;
asm volatile("csrr %0, mstatus" : "=r"(mstatus));

/* If execution reaches here, U-mode isolation failed (still has
* privileges).
*/
umode_printf("FAIL: Privileged instruction executed! (mstatus=0x%lx)\n",
/* If execution reaches here, U-mode isolation failed */
umode_printf("[FAIL] privileged instruction executed (mstatus=0x%lx)\n",
(long) mstatus);

/* Spin loop to prevent further execution. */
Expand Down
Loading