From 2a46f541f22b25e554681672de8f7a58eb8595ee Mon Sep 17 00:00:00 2001 From: bunnie Date: Mon, 15 Feb 2021 19:15:58 +0800 Subject: [PATCH] add PID identification facility to help with debugging --- kernel/src/services.rs | 2 +- macros/src/lib.rs | 33 +++++++++++++++++++++++++++++++++ xous-rs/src/lib.rs | 4 +++- xous-rs/src/process.rs | 19 +++++++++++++++++++ 4 files changed, 56 insertions(+), 2 deletions(-) create mode 100644 xous-rs/src/process.rs diff --git a/kernel/src/services.rs b/kernel/src/services.rs index 0f27cf136..0dd3b1844 100644 --- a/kernel/src/services.rs +++ b/kernel/src/services.rs @@ -314,7 +314,7 @@ impl SystemServices { process.state = ProcessState::Setup(ThreadInit::new( unsafe { core::mem::transmute::(init.entrypoint) }, MemoryRange::new(init.sp, crate::arch::process::DEFAULT_STACK_SIZE).unwrap(), - None, + MemoryAddress::new(pid), [0u8; 12], )); } diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 1138db2db..665707142 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -104,6 +104,39 @@ pub fn xous_main(args: TokenStream, input: TokenStream) -> TokenStream { } // Creates a random identifier +/* + Historical note -- this identifier was inherited from the Cortex libraries. + Apparently, it serves just to prove that the initializing function was run at + all, and does not create any sort of security property. It could be replaced + with a "magic number" instead and achieve the same goal. From reading the rationale + behind why the Cortex ecosystem does this, it's that a name like "__main" could accidentally + be used by a developer and cause the system to be unsafe, and so by going with + a pseudorandom identifier, it discourages programmers from accidental + copy/pasta of well-known symbols and skipping certain initializations that are + critical in the runtime to guarantee the safety properties that Rust depends + upon. + + Safety properties meaning, the Rust memory system has to reason about the type + safety and initialization state of a program, and this starts with a base set + of assumptions. The runtime is responsible for setting up these assumptions, + and if they are wrong then the whole Rust memory system analysis is for naught. + Because the Rust compiler can't "reason beyond the runtime", it becomes a hazard + that a programmer unwittingly names their "main" function with a well-known + symbol that doesn't have the proper initializations, and become confused as to + why their programs don't work. Thus it seems that this random identifier is + a way to "prove" that the run-time did its thing, without relying upon a symbol + or name that could just be copy/pasted by the programmer without also copy/pasting + its semantic significance. + + This is not a "security-critical" random number because it's more of a check on + programmer behavior; any attacker with the ability to write or modify this number + also has the ability to modify the initialization routines anyways, and therefore + promoting this to a true cryptographic random number doesn't solve any problem + or necessarily protect the Rust type safety system from attacks against the + initialization frameworks. To protect against that, the code base needs to be + hashed and signed and checked against a signature, and not rely upon random + identifiers which have no cryptographically essential correlation with the code around it. + */ fn random_ident() -> Ident { let secs = SystemTime::now() .duration_since(UNIX_EPOCH) diff --git a/xous-rs/src/lib.rs b/xous-rs/src/lib.rs index 16e9838b2..970d2a4b7 100644 --- a/xous-rs/src/lib.rs +++ b/xous-rs/src/lib.rs @@ -16,6 +16,7 @@ mod messages; pub mod string; pub mod buffer; pub mod syscall; +pub mod process; pub mod names; @@ -160,7 +161,8 @@ macro_rules! maybe_main { } #[export_name = "_start"] - pub extern "C" fn _start() { + pub extern "C" fn _start(pid: u32) { + xous::process::set_id(pid); unsafe { xous_entry() }; } }; diff --git a/xous-rs/src/process.rs b/xous-rs/src/process.rs new file mode 100644 index 000000000..c04e9740b --- /dev/null +++ b/xous-rs/src/process.rs @@ -0,0 +1,19 @@ +use core::sync::atomic::AtomicU32; + +#[cfg(not(target_os = "none"))] +pub fn id() -> u32 { + std::process::id() +} + +#[cfg(target_os = "none")] +static PID_KEEPER: core::sync::atomic::AtomicU32 = core::sync::atomic::AtomicU32::new(0); + +#[cfg(target_os = "none")] +pub fn id() -> u32 { + PID_KEEPER.load(core::sync::atomic::Ordering::Relaxed) +} + +#[cfg(target_os = "none")] +pub fn set_id(id: u32) { + PID_KEEPER.store(id, core::sync::atomic::Ordering::Relaxed) +}