code wiki / (root) / nx_nxe_lib.nx

nx_nxe_lib.nx source

↩ module page · 51 lines · 3197 B

1// nx_nxe_lib.nx -- the SOVEREIGN NXE writer + native VERIFY-THEN-EXECUTE loader, extracted (rule-15 DRY) so both the 2// loader gate (nx_nxe_loader) and the bootable spore (nx_spore_boot) compose ONE copy. The format is our OWN file 3// (magic 'NXE1'), NOT an ELF clone; the loader refuses to map-exec any binary that fails integrity/capability/ 4// never-brick -- verify-BEFORE-execute BY CONSTRUCTION. RWX-mmap + cast-fn-ptr execution is proven live 5// (nx_nxe_exec_probe). Integrity = canonical FIPS-180-4 SHA-256 of the code section. 6// NXE header (8-byte fields): [0]magic'NXE1' [8]ver=1 [16]arch(1=x86_64) [24]flags [32]caps [40]entry [48]code_off=96 7// [56]code_len [64..96]code_hash(32B SHA-256) ; code@96. flags: HW_WRITE=1 NEVER_BRICK_PROVEN=2. 8// caps bits: FILE_READ=0 FILE_WRITE=1 NET=2 EXEC=3 RAW_HW=4. NEVER-BRICK: RAM + a file, 0 firmware writes. 9// license_tier: ORIGINAL 10import "nx_syscalls.nx" 11import "nx_sha256.nx" 12 13const NXE_HOFF: i64 = 96 14func nxe_mmap_rwx(size: i64) -> *u8 { let r: i64 = __syscall(SYS_MMAP, 0, size, 7, 0x22, -1, 0); return r as *u8 } // PROT_RWX|MAP_PRIV|ANON 15func nxe_p64(m: *u8, o: i64, v: i64) -> i64 { var i: i64=0; while i<8 { m[o+i]=((v>>(i*8))&255) as u8; i=i+1 } return 0 } 16func nxe_r64(m: *u8, o: i64) -> i64 { var v: i64=0; var i: i64=0; while i<8 { v=v|((m[o+i] as i64)<<(i*8)); i=i+1 } return v } 17 18func nxe_write(m: *u8, arch: i64, flags: i64, caps: i64, entry: i64, code: *u8, clen: i64) -> i64 { 19 m[0]=0x4E as u8; m[1]=0x58 as u8; m[2]=0x45 as u8; m[3]=0x31 as u8 20 nxe_p64(m,8,1); nxe_p64(m,16,arch); nxe_p64(m,24,flags); nxe_p64(m,32,caps); nxe_p64(m,40,entry); nxe_p64(m,48,NXE_HOFF); nxe_p64(m,56,clen) 21 var i: i64=0; while i<clen { m[NXE_HOFF+i]=code[i]; i=i+1 } 22 sha256_digest(((m as i64)+NXE_HOFF) as *u8, clen, ((m as i64)+64) as *u8) 23 return NXE_HOFF+clen 24} 25func nxe_size(m: *u8) -> i64 { return NXE_HOFF + nxe_r64(m,56) } 26func nxe_magic_ok(m: *u8) -> i64 { if m[0]==(0x4E as u8) { if m[1]==(0x58 as u8) { if m[2]==(0x45 as u8) { if m[3]==(0x31 as u8) { return 1 } } } } return 0 } 27func nxe_integrity_ok(m: *u8) -> i64 { 28 let clen: i64=nxe_r64(m,56) 29 let want: *u8=sys_mmap(32); sha256_digest(((m as i64)+NXE_HOFF) as *u8, clen, want) 30 var i: i64=0; while i<32 { if want[i]!=m[64+i] { return 0 } i=i+1 } 31 return 1 32} 33func nxe_may(m: *u8, capbit: i64) -> i64 { return (nxe_r64(m,32)>>capbit)&1 } 34 35// verify -> (only on full pass) mmap RWX + copy + cast + EXECUTE with `arg`. 36// resbox[0]=result; ranbox[0]=1 IFF a code byte executed. return: 0 ok, -1 magic, -2 integrity, -3 never-brick, -4 no EXEC. 37func nxe_load_exec(m: *u8, arg: i64, resbox: *i64, ranbox: *i64) -> i64 { 38 ranbox[0]=0; resbox[0]=0 39 if nxe_magic_ok(m)==0 { return 0-1 } 40 if nxe_integrity_ok(m)==0 { return 0-2 } 41 let flags: i64=nxe_r64(m,24) 42 if (flags&1)==1 { if (flags&2)==0 { return 0-3 } } 43 if nxe_may(m,3)==0 { return 0-4 } 44 let clen: i64=nxe_r64(m,56); let entry: i64=nxe_r64(m,40) 45 let ex: *u8=nxe_mmap_rwx(clen+64) 46 var i: i64=0; while i<clen { ex[i]=m[NXE_HOFF+i]; i=i+1 } 47 let fp: func(i64) -> i64 = ((ex as i64)+entry) as func(i64) -> i64 48 ranbox[0]=1 49 resbox[0]=fp(arg) 50 return 0 51}