omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / om / sha256 / om2-evidence / audit / audit3.py
| 1 | #!/usr/bin/env python3 |
| 2 | # L838 audit part 3: one level of wrappers that forward a parameter as sha256_hash data and another as |
| 3 | # the digest; every call of such a wrapper whose two arguments share a base is listed for review. |
| 4 | import re, json, sys, subprocess, collections |
| 5 | sys.argv = [sys.argv[0], sys.argv[1]] |
| 6 | from audit import strip_comments, args_at, norm, base, ROOT |
| 7 | calls = [c for c in json.load(open('/tmp/zl/sha-audit/calls.json')) if not c['defn'] and not c['file'].startswith('evidence/')] |
| 8 | files = [f for f in subprocess.run(['git', '-C', ROOT, 'ls-files', '--', '*.c', '*.h'], capture_output=True, text=True).stdout.split() |
| 9 | if re.match(r'(src|tools|tests|research|om)/', f)] |
| 10 | txt = {f: strip_comments(open(f'{ROOT}/{f}', errors='replace').read()) for f in files} |
| 11 | wrappers = {} |
| 12 | HDR = re.compile(r'([A-Za-z_]\w*)\s*\(([^;{}()]*)\)\s*\{') |
| 13 | for f, s in txt.items(): |
| 14 | for m in HDR.finditer(s): |
| 15 | name = m.group(1) |
| 16 | if name in ('if', 'for', 'while', 'switch', 'return', 'sizeof'): continue |
| 17 | params = [re.findall(r'([A-Za-z_]\w*)\s*(\[[^\]]*\])?\s*$', p.strip()) for p in m.group(2).split(',')] |
| 18 | params = [p[0][0] if p else None for p in params] |
| 19 | # body extent |
| 20 | i = m.end() - 1; d = 0 |
| 21 | for j in range(i, len(s)): |
| 22 | if s[j] == '{': d += 1 |
| 23 | elif s[j] == '}': |
| 24 | d -= 1 |
| 25 | if d == 0: break |
| 26 | body = s[i:j] |
| 27 | for h in re.finditer(r'\bsha256_hash\s*\(', body): |
| 28 | a, _ = args_at(body, h.end() - 1) |
| 29 | if len(a) != 3: continue |
| 30 | bd, bo = base(a[0]), base(a[2]) |
| 31 | if bd in params and bo in params and bd != bo: |
| 32 | wrappers[name] = (f, params.index(bd), params.index(bo)) |
| 33 | print('wrappers forwarding (data, digest) params into sha256_hash:', len(wrappers)) |
| 34 | for w, (f, di, oi) in sorted(wrappers.items()): |
| 35 | print(f' {w} ({f}) data=arg{di} digest=arg{oi}') |
| 36 | for f2, s in txt.items(): |
| 37 | for m in re.finditer(r'\b' + re.escape(w) + r'\s*\(', s): |
| 38 | a, end = args_at(s, m.end() - 1) |
| 39 | if len(a) <= max(di, oi): continue |
| 40 | if s[end + 1:end + 20].lstrip().startswith('{'): continue |
| 41 | bd, bo = base(a[di]), base(a[oi]) |
| 42 | if bd.split('->')[0].split('.')[0] == bo.split('->')[0].split('.')[0]: |
| 43 | line = s.count('\n', 0, m.start()) + 1 |
| 44 | print(f' SAME-BASE {f2}:{line}: {w}({", ".join(norm(x) for x in a)})') |