code wiki / _hdl_build / nx_warden_paths.nx
nx_warden_paths.nx source
↩ module page · 76 lines · 2781 B
1// nx_warden_paths.nx -- the WARDEN's real teeth: classify whether an autonomous
2// action on a PATH is additive-safe or must be DENIED. Gives the crew council's
3// Warden leg actual protected-path safety (vs a hand-set flag), so the loop can
4// self-heal/build but can NEVER overwrite the canonical assets.
5//
6// Protected (OVERWRITE / DELETE = HARD DENY):
7// - the known-good compiler _offc/nx_cc_known_good.elf (the canonical root of
8// trust everything is proven against -- the operator's #1 asset)
9// - any *.nx SOURCE (autonomous overwrite-in-place forbidden; real edits land
10// ADDITIVELY + via a gate + review, never an in-place clobber)
11// Safe:
12// - CREATE a new file (additive), APPEND (journals), READ
13// - OVERWRITE of a regenerable build artifact (_offc/*.s, *.o, *.elf scratch, /tmp/*)
14// license_tier: ORIGINAL
15
16import "nx_syscalls.nx"
17
18const WP_SAFE: i64 = 1
19const WP_DENY: i64 = 0
20
21const WP_READ: i64 = 0
22const WP_APPEND: i64 = 1
23const WP_CREATE: i64 = 2 // create a NEW path (additive)
24const WP_OVERWRITE: i64 = 3 // write over an EXISTING path
25const WP_DELETE: i64 = 4
26
27func wp_slen(s: *u8) -> i64 { var n: i64 = 0; while s[n] != (0 as u8) { n = n + 1 } return n }
28
29// does `hay` contain the NUL-terminated substring `needle`?
30func wp_contains(hay: *u8, needle: *u8) -> i64 {
31 let hn: i64 = wp_slen(hay)
32 let nn: i64 = wp_slen(needle)
33 if nn == 0 { return 1 }
34 var i: i64 = 0
35 while i + nn <= hn {
36 var j: i64 = 0
37 var hit: i64 = 1
38 while j < nn {
39 if hay[i + j] != needle[j] { hit = 0 }
40 j = j + 1
41 }
42 if hit == 1 { return 1 }
43 i = i + 1
44 }
45 return 0
46}
47
48// does `s` end with `suf`?
49func wp_endswith(s: *u8, suf: *u8) -> i64 {
50 let sn: i64 = wp_slen(s)
51 let fn: i64 = wp_slen(suf)
52 if fn > sn { return 0 }
53 var i: i64 = 0
54 while i < fn {
55 if s[sn - fn + i] != suf[i] { return 0 }
56 i = i + 1
57 }
58 return 1
59}
60
61// Is this path PROTECTED from autonomous overwrite/delete?
62func wp_is_protected(path: *u8) -> i64 {
63 if wp_contains(path, "nx_cc_known_good" as *u8) == 1 { return 1 } // the canonical compiler
64 if wp_endswith(path, ".nx" as *u8) == 1 { return 1 } // a language source
65 return 0
66}
67
68// The Warden's verdict on (action, path).
69func nx_warden_path_safe(action: i64, path: *u8) -> i64 {
70 if action == WP_READ { return WP_SAFE }
71 if action == WP_APPEND { return WP_SAFE }
72 if action == WP_CREATE { return WP_SAFE } // additive: a new file
73 // OVERWRITE / DELETE: DENY iff the target is protected.
74 if wp_is_protected(path) == 1 { return WP_DENY }
75 return WP_SAFE // regenerable artifact -> ok
76}