···1010use defmt::trace;
1111use eepy_sys::syscall::SyscallNumber;
1212use crate::exception::StackFrame;
1313+use crate::SRAM_END;
1414+1515+const PROGRAM_SRAM_START: usize = 0x20020000;
1616+const FLASH_START: usize = 0x10000000;
1717+const KERNEL_FLASH_END: usize = 0x10020000;
1818+const FLASH_END: usize = 0x11000000;
13191420global_asm!(include_str!("syscall.s"));
1521···2733extern "C" fn handle_syscall(sp: *mut StackFrame, using_psp: bool) {
2834 // Stack contains R0, R1, R2, R3, R12, LR, ReturnAddress, xPSR
2935 let stack_values = unsafe { &mut *sp };
3030- let syscall_num = unsafe { *stack_values.pc.sub(2) };
3636+ let syscall_num = unsafe { *((stack_values.pc - 2) as *const u8) };
31373238 trace!("syscall: sp={} imm={:x} {}", sp, syscall_num, stack_values);
3339···4147 Some(SyscallNumber::Flash) => flash::handle_flash(stack_values),
4248 Some(SyscallNumber::KvStore) => kv_store::handle_kv_store(stack_values),
4349 None => panic!("illegal syscall"),
5050+ }
5151+}
5252+5353+/// Check that a pointer provided by the program via a syscall is valid (i.e. that the caller is
5454+/// allowed to access it). This prevents programs from providing pointers into kernel memory or
5555+/// MMIO registers to certain syscalls to bypass memory protection.
5656+/// Panics if the pointer is not valid (TODO: proper error handling for syscalls).
5757+///
5858+/// If the caller is not the kernel (determined by `pc`), check that the whole of the memory region
5959+/// starting at `addr` with length `len` bytes is entirely within either the program region of
6060+/// memory or flash.
6161+fn assert_address(addr: usize, len: usize, pc: usize) {
6262+ // TODO: return proper errors from syscalls instead of panicking
6363+6464+ if !(FLASH_START..KERNEL_FLASH_END).contains(&pc) {
6565+ assert!((PROGRAM_SRAM_START..SRAM_END).contains(&addr) || (FLASH_START..FLASH_END).contains(&addr));
6666+ assert!((PROGRAM_SRAM_START..=SRAM_END).contains(&(addr + len)) || (FLASH_START..=FLASH_END).contains(&(addr + len)));
4467 }
4568}
+3-3
eepy/src/syscall/exec.rs
···2929 panic!("tried to exec invalid program");
3030 }
31313232- stack_values.pc = core::mem::transmute((*program).entry);
3333- stack_values.lr = program_return_handler as *const u8;
3232+ stack_values.pc = (*program).entry as *const () as usize;
3333+ stack_values.lr = program_return_handler as *const () as usize;
34343535 // Move the saved registers to the top of program memory
3636 // This makes sure all programs start with an empty program stack
3737 if using_psp {
3838- let ptr: *mut StackFrame = SRAM_END.sub(size_of::<StackFrame>()).cast();
3838+ let ptr: *mut StackFrame = (SRAM_END - size_of::<StackFrame>()) as *mut StackFrame;
3939 ptr.write(*stack_values);
4040 asm!(
4141 "msr psp, {ptr}",