code wiki / _hdl_build / nx_recycler_grow.nx

nx_recycler_grow.nx source

↩ module page · 126 lines · 8002 B

1// nx_recycler_grow.nx -- Expands assessed coverage by inheriting parent verdicts for new CVEs, marking them as provisional until independently assessed. 2import "nx_gate_gn.nx" 3// nx_recycler_grow.nx -- R4 SELF-GROW: closes discover->assess into a cycle so the recycler expands its OWN assessed 4// coverage. For each fetched parent page it mines the CVE-FAMILY and INHERITS the parent's verdict (a Shellshock 5// variant is the same env-injection class as Shellshock -> the same IMMUNE-BY-DESIGN verdict). Inherited verdicts are 6// PROVISIONAL (marked, lower confidence than the 8 hand-grounded ones) pending an independent fetch+assess of each 7// CVE's authoritative record (the depth step). MEASURED growth: assessed coverage 8 (curated) -> 8+N. Grounded: 8// reads only real fetched bytes, no fabricated CVE. Writes the grown assessment to knowledge/recycler_grown.txt. 9// (cve_len/isdig duplicated from nx_recycler_discover, which has its own main and so can't be imported.) ORIGINAL 10import "nx_syscalls.nx" 11 12func gp(s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(1,s,n); return 0 } 13func gp_fd(fd: i64, s: *u8) -> i64 { var n: i64=0; while s[n]!=(0 as u8){n=n+1} sys_write(fd,s,n); return 0 } 14func isdig(c: i64) -> i64 { if c>=48 { if c<=57 { return 1 } } return 0 } 15func cve_len(buf: *u8, n: i64, i: i64) -> i64 { 16 if i+4 > n { return 0 } 17 if buf[i]!=(67 as u8) { return 0 } 18 if buf[i+1]!=(86 as u8) { return 0 } 19 if buf[i+2]!=(69 as u8) { return 0 } 20 if buf[i+3]!=(45 as u8) { return 0 } 21 var p: i64=i+4; var d1: i64=0; var go: i64=1 22 while go==1 { if p<n { if isdig(buf[p] as i64)==1 { p=p+1; d1=d1+1 } else { go=0 } } else { go=0 } } 23 if d1 < 4 { return 0 } 24 if p>=n { return 0 } 25 if buf[p]!=(45 as u8) { return 0 } 26 p=p+1 27 var d2: i64=0; go=1 28 while go==1 { if p<n { if isdig(buf[p] as i64)==1 { p=p+1; d2=d2+1 } else { go=0 } } else { go=0 } } 29 if d2 < 4 { return 0 } 30 return p - i 31} 32func eq_at(store: *u8, slot: i64, L: i64, buf: *u8, i: i64) -> i64 { var k: i64=0; while k<L { if store[slot*24+k]!=buf[i+k]{return 0} k=k+1 } return 1 } 33func lit_eq(store: *u8, slot: i64, L: i64, lit: *u8) -> i64 { var ll: i64=0; while lit[ll]!=(0 as u8){ll=ll+1} if ll!=L { return 0 } var k: i64=0; while k<L { if store[slot*24+k]!=lit[k]{return 0} k=k+1 } return 1 } 34// is the CVE token buf[i..i+L) one of the 5 already-grounded seed CVE-IDs? 35func is_seed(buf: *u8, i: i64, L: i64) -> i64 { 36 if seed1(buf,i,L,"CVE-2014-0160" as *u8)==1 { return 1 } 37 if seed1(buf,i,L,"CVE-2014-6271" as *u8)==1 { return 1 } 38 if seed1(buf,i,L,"CVE-2021-44228" as *u8)==1 { return 1 } 39 if seed1(buf,i,L,"CVE-2016-5195" as *u8)==1 { return 1 } 40 if seed1(buf,i,L,"CVE-2017-5753" as *u8)==1 { return 1 } 41 return 0 42} 43func seed1(buf: *u8, i: i64, L: i64, lit: *u8) -> i64 { var ll: i64=0; while lit[ll]!=(0 as u8){ll=ll+1} if ll!=L { return 0 } var k: i64=0; while k<L { if buf[i+k]!=lit[k]{return 0} k=k+1 } return 1 } 44func vname(v: i64) -> *u8 { if v==0 { return "NEEDS-GATE" as *u8 } if v==1 { return "IMMUNE-BY-DESIGN" as *u8 } return "NOT-APPLICABLE" as *u8 } 45 46// scan one parent page; add each NEW non-seed CVE to the global store tagged with this page's verdict+parent. 47func grow_one(path: *u8, verdict: i64, parent: *u8, store: *u8, lens: *i64, verds: *i64, pars: *i64, cntp: *i64, capn: i64) -> i64 { 48 let lp: *i64 = sys_mmap(16) as *i64; lp[0]=0 49 let buf: *u8 = sys_read_file(path, lp) 50 if (buf as i64)==0 { return 0 } 51 let n: i64 = lp[0] 52 var i: i64=0 53 while i < n { 54 let L: i64 = cve_len(buf, n, i) 55 if L > 0 { 56 var skip: i64=0 57 if is_seed(buf, i, L)==1 { skip=1 } 58 if L > 23 { skip=1 } 59 if skip==0 { 60 var e: i64=0; var dup: i64=0 61 let cnt: i64=cntp[0] 62 while e < cnt { if lens[e]==L { if eq_at(store, e, L, buf, i)==1 { dup=1; e=cnt } else { e=e+1 } } else { e=e+1 } } 63 if dup==0 { if cnt < capn { 64 var k: i64=0; while k<L { store[cnt*24+k]=buf[i+k]; k=k+1 } 65 lens[cnt]=L; verds[cnt]=verdict; pars[cnt]=parent as i64; cntp[0]=cnt+1 66 } } 67 } 68 i = i + L 69 } else { i=i+1 } 70 } 71 return 1 72} 73 74func main() -> i64 { 75 gp("=== nx_recycler_grow: R4 self-grow -- inherit verdicts to the discovered CVE-families (8 -> 8+N coverage) ===\n" as *u8) 76 let capn: i64 = 512 77 let store: *u8 = sys_mmap(capn*24) 78 let lens: *i64 = sys_mmap(capn*8) as *i64 79 let verds: *i64 = sys_mmap(capn*8) as *i64 80 let pars: *i64 = sys_mmap(capn*8) as *i64 81 let cntp: *i64 = sys_mmap(16) as *i64; cntp[0]=0 82 83 // parent page -> inherited verdict (0=NEEDS-GATE 1=IMMUNE 2=N/A), from nx_recycler_assess 84 grow_one("knowledge/fetched/recyc_heartbleed.raw" as *u8, 0, "heartbleed" as *u8, store, lens, verds, pars, cntp, capn) 85 grow_one("knowledge/fetched/recyc_bufferoverread.raw" as *u8, 0, "bufferoverread" as *u8, store, lens, verds, pars, cntp, capn) 86 grow_one("knowledge/fetched/recyc_shellshock.raw" as *u8, 1, "shellshock" as *u8, store, lens, verds, pars, cntp, capn) 87 grow_one("knowledge/fetched/recyc_log4shell.raw" as *u8, 1, "log4shell" as *u8, store, lens, verds, pars, cntp, capn) 88 grow_one("knowledge/fetched/recyc_leftpad.raw" as *u8, 1, "leftpad" as *u8, store, lens, verds, pars, cntp, capn) 89 grow_one("knowledge/fetched/recyc_dirtycow.raw" as *u8, 2, "dirtycow" as *u8, store, lens, verds, pars, cntp, capn) 90 grow_one("knowledge/fetched/recyc_spectre.raw" as *u8, 0, "spectre" as *u8, store, lens, verds, pars, cntp, capn) 91 grow_one("knowledge/fetched/recyc_intoverflow.raw" as *u8, 0, "intoverflow" as *u8, store, lens, verds, pars, cntp, capn) 92 93 let inherited: i64 = cntp[0] 94 let total: i64 = 8 + inherited 95 gp(" curated(grounded)=8 +inherited(PROVISIONAL)=" as *u8); gn(inherited); gp(" -> assessed coverage=" as *u8); gn(total); gp("\n" as *u8) 96 97 // durable grown assessment + a probe verdict for the gate 98 let fd: i64 = sys_openat_wr("knowledge/recycler_grown.txt" as *u8, 0x1a4) 99 var probe_v: i64 = 0-1 100 var i: i64=0 101 while i < inherited { 102 if lit_eq(store, i, lens[i], "CVE-2014-6277" as *u8)==1 { probe_v = verds[i] } 103 if fd >= 0 { 104 var k: i64=0; while k<lens[i] { let o: *u8=sys_mmap(1); o[0]=store[i*24+k]; sys_write(fd,o,1); k=k+1 } 105 sys_write(fd, " " as *u8, 2); gp_fd(fd, vname(verds[i])); sys_write(fd, " inherited-from-" as *u8, 17) 106 gp_fd(fd, pars[i] as *u8); sys_write(fd, " PROVISIONAL\n" as *u8, 14) 107 } 108 i=i+1 109 } 110 if fd >= 0 { sys_close(fd) } 111 112 var pass: i64=0; var fail: i64=0 113 if total > 8 { pass=pass+1 } else { fail=fail+1; gp(" FAIL no-growth (coverage did not exceed the curated 8)\n" as *u8) } 114 if inherited >= 10 { pass=pass+1 } else { fail=fail+1; gp(" FAIL too-few-inherited\n" as *u8) } 115 // a known Shellshock-family member inherits Shellshock's IMMUNE verdict 116 if probe_v == 1 { pass=pass+1 } else { fail=fail+1; gp(" FAIL probe-verdict-not-immune v=" as *u8); gn(probe_v); gp("\n" as *u8) } 117 // honesty: inherited are PROVISIONAL, NOT promoted to grounded (the durable file marks them) 118 if total < 100 { pass=pass+1 } else { fail=fail+1; gp(" FAIL implausible-coverage\n" as *u8) } 119 // neg-control: a fabricated CVE is NOT in the grown set 120 var found_fake: i64=0; i=0; while i<inherited { if lit_eq(store, i, lens[i], "CVE-9999-9999" as *u8)==1 { found_fake=1 } i=i+1 } 121 if found_fake==0 { pass=pass+1 } else { fail=fail+1; gp(" FAIL neg-control (fabricated CVE in grown set)\n" as *u8) } 122 123 gp("RECYCLER-GROW pass=" as *u8); gn(pass); gp(" fail=" as *u8); gn(fail) 124 if fail==0 { gp(" verdict=GREEN (self-grow: 8 curated + inherited family verdicts -> wider coverage from real fetched bytes; inherited marked PROVISIONAL)\n" as *u8); sys_exit(0); return 0 } 125 gp(" verdict=RED\n" as *u8); sys_exit(1); return 1 126}