code wiki / (root) / nx_os_fs.nx

nx_os_fs.nx source

↩ module page · 36 lines · 1976 B

1// nx_os_fs.nx -- OS FILESYSTEM-NAMESPACE SEAM (the write-safety half; sibling of nx_os_proc.nx). 2// Answers ONE question for the IO layer: is this path in the OS's device/kernel/firmware namespace, 3// where a file write could touch hardware or kernel state? Rule 26 (never-brick) demands the answer 4// be BY CONSTRUCTION -- compiled in, not config-disableable -- so the deny lives here, in code, and 5// callers cannot toggle it off with a conf line. 6// 7// LINUX BACKEND (current): the kernel exposes devices/firmware knobs as FILES under /dev, /sys, /proc 8// (e.g. /sys/firmware/efi/efivars -- an errant write there can brick a board; /dev/sda -- raw disk). 9// A path is write-forbidden iff it IS or is UNDER one of those roots. 10// 11// NISHIOS-NATIVE (target): NishiOS has no ambient device files -- device access is capability-routed 12// through typed channels, so the ambient-namespace hazard class does not exist; the native backend 13// returns forbid only for its reserved kernel-object namespace. This file is the SOURCE-SWAP seam 14// (same contract, swapped backend), exactly like nx_os_proc.nx. license_tier: ORIGINAL 15import "nx_syscalls.nx" 16 17const OSF_SLASH: i64 = 47 // '/' -- path separator (namespace-boundary test) 18 19// is path EXACTLY root or UNDER root/ ? (blocks "/dev" and "/dev/null", not "/devdata") 20func osf_under(path: *u8, root: *u8) -> i64 { 21 var i: i64 = 0 22 while root[i] != (0 as u8) { 23 if path[i] != root[i] { return 0 } 24 i = i + 1 25 } 26 if path[i] == (0 as u8) { return 1 } // exactly the root 27 if path[i] == (OSF_SLASH as u8) { return 1 } // inside the root 28 return 0 29} 30// WRITE-FORBIDDEN check: 1 = the OS device/kernel/firmware namespace, never writable through the IO layer. 31func osf_write_forbidden(path: *u8) -> i64 { 32 if osf_under(path, "/dev" as *u8) == 1 { return 1 } 33 if osf_under(path, "/sys" as *u8) == 1 { return 1 } 34 if osf_under(path, "/proc" as *u8) == 1 { return 1 } 35 return 0 36}