Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/workflows/check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ jobs:
run: python3 tools/ios27/builds/builds-test.py
- name: sbca's QuartzCore tables per iOS build (tables, headers, checker)
run: python3 tools/ios27/ca-gpu/sbca_table_test.py
- name: corecrypto's FIPS seal is recomputed only where it was true, and restored exactly (fips-seal.py, made-up Mach-O files)
run: python3 tools/ios27/a64e/fips-seal-test.py
- name: shared-cache header facts for the AOT rewrite
run: python3 tools/ios27/fullroot/pipeline/cache/aot/dsc_header_test.py
- name: The S9's firmware in the iOS 27 root build is complete and checked or the build stops, and the root says what it has (made-up files)
Expand Down
59 changes: 59 additions & 0 deletions docs/ios27/CORECRYPTO.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# libcorecrypto on the S9: the hottest arm64e traps, and its FIPS seal (18 November 2026)

## What the phone was trapping on

On phone 1 (iOS 27.2.1, 24B156, the `o272` boot image, root v9) the kernel's `STARLTE A64E hot` line (the ten sites that trapped most in
the last ten seconds) read, at idle, a pair of `RETAB` words at about 2,000-15,000 traps each per ten seconds, and during the re-render of
the home screen's icons after a switch to dark mode (`iconservicesagent` at ~150 % CPU for two minutes) **106,512 and 104,880** traps in one
ten-second interval at those two words, with a dozen more sites at 2,000-17,000 (`RETAB` `d65f0fff`, `BLRAAZ x8` `d63f091f`,
`BLRAA x16, x17` `d73f0a11`, `BRAAZ x5` `d61f08bf`). `STARLTE LSE hot` had one more site next to them.

The addresses in those lines are **slid**: the same sites read `0x1da0a3c5c` in a boot whose `CRASH` lines say `slide 0x63a8000` and
`0x1d919fc5c` in one with `slide 0x54a4000`; both are `0x1d3cfbc5c` unslid. Every hot site, unslid, is in
`/usr/lib/system/libcorecrypto.dylib`'s `__TEXT,__text`: the hottest pair is the return of `ccder_blob_decode_range` (`0x1d3cfbbe0`) and of
the local function right after it (`0x1d3cfbc5c`), then `ccder_sizeof` and local functions whose names are not in the cache (DER decoding,
most likely of signatures and certificates). The LSE site is the library's one `SWP` (`0x1d3cfa77c`).

## Why they were left

Not an encoding the rewrite misses, not a section it does not scan: `a64e-rewrite.py` handles all of them. The cache pipeline passed it
`--skip libcorecrypto` (and `lse-rewrite.py`, `a64e-rewrite2.py`, `rewrite-grow.py` skip it by default), because corecrypto's FIPS
power-on self-test checks a MAC of its own code, and a rewritten library fails it. So the whole library, 3,538 words (1,500 `RETA*`, 67
`BRA*`, 1,312 `BLRA*`, 659 `PAC*`/`AUT*` register forms), stayed arm64e and went through the kernel's emulation on every call.

`a64e-rewrite.py census` (new) shows it at once: on the v9 root's cache it prints exactly three images with words left, the three
corecrypto images, and nothing else in 4,759.

## The seal, and why it can simply be recomputed

corecrypto's integrity test (`fipspost_post_integrity`, run in user space by `/usr/libexec/cc_fips_test`; apple/corecrypto
`cc_fips/src/fipspost_get_hmac.c`) is HMAC-SHA256 with a key of 32 zero bytes over the image's first `__TEXT,__text` section, compared with
the 32 bytes of its `__TEXT,__fips_hmacs` section. There is no secret and nothing outside the image: on the 24B156 cache the stored value
of `libcorecrypto.dylib` is exactly that HMAC of its `__text` as it sits in the cache. `tools/ios27/a64e/fips-seal.py` recomputes it after
the rewrite and writes it into `__fips_hmacs`, **only for an image whose seal was true in the reference** (the stock cache, or the cache
before the step) and whose stored value is still the reference one. The test is as strong as before: any change after the reseal fails it
again. The `_noasm` and `_trace` variants are not loaded by anything and their seals are already false in the stock cache; they stay
untranslated and untouched.

So the stand-in for `cc_fips_test` that the project's roots carry (`docs/ios27/SPEED.md`, "Not published") is no longer needed for this:
the real test's integrity check passes on a resealed cache (the stand-in is still on the roots; the real helper has not been run on a phone).

## The tools

- `a64e-rewrite.py cache ... --log FILE` writes `ADDR OLD NEW` for every changed word; `verify --log` checks every word holds its new value,
`restore --log` puts the originals back (round trip, as `lse-rewrite.py verify/restore`).
- `a64e-rewrite.py census CACHE [--allow SUBSTR ...]`: per image, the words the rewrite would still change, nothing skipped; exit 1 for an image
not allowed. The pipeline runs it last (step 9) with only the two unloaded variants allowed.
- `fips-seal.py check|reseal|restore` (pure Python; `fips-seal-test.py` exercises it on made-up Mach-O files, in CI).
- `pipeline/cache-pipeline.sh`: steps 1, 2 and 4 now skip only `libcorecrypto_noasm` and `libcorecrypto_trace`; step 5b reseals and checks
(before the `PRE` reference of the grow step, which skips corecrypto); step 9 is the census.

The LSE pass over corecrypto must run with the text-data skip map (`TEXTDATA_SKIP`, `docs/ios27/TEXT-DATA.md`), as it does in the pipeline:
two words of corecrypto's SHA-256 constant table (`0x78a5636f`) have the bit pattern of `LDUMAXAH` and would otherwise become branches.

## On the v9 root (off the phone)

As one root step on a clone of root v9 (the step script and its undo are private, on the mini): 3,538 words rewritten and verified, the
one `SWP` given a thunk (the two table words left alone), the seal recomputed and checked; 3,560 words of one subcache file and 4 of
another differ from v9, nothing else. `textdata-guard.py scan --pac` against v9 flags six of the changed words by rule D; all six are
ordinary code (epilogues, `BLRAAZ` calls, `PACIZA x16` before a store). The census then shows only the two unloaded variants.
7 changes: 7 additions & 0 deletions docs/ios27/INSTALL-S9.md
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,13 @@ have not been booted together.**
`prepare-root-inputs.sh` makes the build's private directory (`tools/ios27/fullroot/private/`, never in git): instruction tables
from your cache, the checked patch list, and one script of wardevsa's fetched from his public repository at a fixed commit.

Since 18 November 2026 the pipeline rewrites `libcorecrypto` like every other library and then recomputes its FIPS integrity seal
(`tools/ios27/a64e/fips-seal.py`, step 5b): the self-test is HMAC-SHA256 with an all-zero key over the library's `__text`, so the
seal of the rewritten code is made the same way, and only where the stock seal was true (`docs/ios27/CORECRYPTO.md`). Before that the
library was skipped and its 3,538 pointer-authentication words trapped into the kernel's emulation: on 24B156 they were the
phone's hottest traps (about 100,000 in ten seconds while the icons re-render after a switch to dark mode). Its two unloaded variants
(`_noasm`, `_trace`) are still left exactly as Apple built them. The paragraph below describes the builds before that change.

One library is left exactly as Apple built it: `libcorecrypto` (and its two variants). It checks a hash of its own code when it
starts, so rewriting it makes its self-test fail; the project's own phones run a rewritten copy with a local stand-in for the
self-test's helper, and neither the stand-in nor a way to make one is published (`docs/ios27/SPEED.md`, "Not published"). Your root
Expand Down
1 change: 1 addition & 0 deletions docs/ios27/SPEED.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ configd's InterfaceNamer never reports IOKit quiet on this kernel. SystemConfigu
`pacibsp` (unslid address `0x1ab5bc514` in the iOS 27.0.1 cache the work used); changing that word to `ret` ends the wait. The real fix is the IOService that
keeps IOKit busy; it was not found.

**Superseded on 18 November 2026** by `tools/ios27/a64e/fips-seal.py`, which recomputes the self-test's seal for the rewritten code (`docs/ios27/CORECRYPTO.md`).
**Not published.** An earlier step rewrote libcorecrypto too, which makes its FIPS self-test fail, and the work ran for a while with a locally made stand-in
for the test's helper. That is a local, optional step the builder took on their own root: this repository has no stand-in, no way to make one and no tool that
needs one (`a64e-rewrite2.py` skips libcorecrypto unless told not to), and `docs/ios27/EIGHT-CORES.md`'s rewrite skips it too. Root images, the originals of
Expand Down
128 changes: 120 additions & 8 deletions tools/ios27/a64e/a64e-rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,18 @@
Left for the kernel (sweet_a64e.c) to emulate on trap: PACGA, FJCVTZS, FCADD,
LDRA* with writeback or out-of-range offsets.

a64e-rewrite.py cache DIR/dyld_shared_cache_arm64e [--only SUBSTR ...] [--dry-run]
a64e-rewrite.py cache DIR/dyld_shared_cache_arm64e [--only SUBSTR ...] [--skip SUBSTR ...] [--dry-run] [--log FILE]
a64e-rewrite.py macho FILE ... [--dry-run] (thin or fat; arm64e slices)
Files are modified in place (clone them first: cp -c).
a64e-rewrite.py census CACHE_MAIN [--allow SUBSTR ...] per image, the words this rewrite would still change (no image
skipped): exit 1 if an image not named by --allow has any
a64e-rewrite.py verify CACHE_MAIN --log FILE every logged word holds its new value
a64e-rewrite.py restore CACHE_MAIN --log FILE every logged word gets its original value back
Files are modified in place (clone them first: cp -c). --log writes "ADDR OLD NEW" per changed word ("# image PATH" before
each image), so a cache rewrite can be verified and undone word for word.

libcorecrypto: its FIPS self-test (cc_fips_test) MACs its own __text. Rewrite it and then reseal it with fips-seal.py (the
seal is recomputed only where it was true before; docs/ios27/CORECRYPTO.md). Skipping it, as the pipeline did until
18 November 2026, leaves its RETAB/BLRAA/BRAA words to the kernel's emulation: the hottest traps of the S9.

Data inside the code: the words of an LC_DATA_IN_CODE range are never touched (macho mode); a LDRAx / LDAPR candidate
with an undecodable word within 8 words before and after it is left alone too (macho mode; A64E_NO_RULE_C=1 turns
Expand Down Expand Up @@ -124,7 +133,8 @@ def text_sections(lc, ncmds):
off += csz


def do_cache(main, only, dry, skip=()):
def do_cache(main, only, dry, skip=(), log=None, per=None):
"""log: a file to write "ADDR OLD NEW" lines to. per: a list that gets (path, counts) per image (census)."""
maps = []
files = {}
for p in sorted(glob.glob(main + '*')):
Expand Down Expand Up @@ -173,17 +183,31 @@ def read(addr, n):
continue
ncmds, sz = struct.unpack_from('<II', mh, 16)
lc = read(im[0] + 32, sz)
ci = {}
if log:
log.write(f'# image {path}\n')
for addr, size, _ in text_sections(lc, ncmds):
p, o = loc(addr)
if p is None:
continue
mm = files[p]
buf = np.array(mm[o:o + (size & ~3)]).view(np.uint32) if dry else mm[o:o + (size & ~3)].view(np.uint32)
orig = np.array(buf) if (log or per is not None) else None
left = []
add(tot, rewrite_words(buf, textdata_idx(addr, len(buf)), left=left))
for i in left:
print(f'textdata: {addr + 4 * i:#x} {int(buf[i]):08x} D {path}')
c = rewrite_words(buf, textdata_idx(addr, len(buf)), left=left)
add(tot, c)
if orig is not None:
ch = np.flatnonzero(orig != buf)
c['changed'] = len(ch)
if log:
log.writelines(f'{addr + 4 * int(i):#x} {int(orig[i]):08x} {int(buf[i]):08x}\n' for i in ch)
add(ci, c)
if per is None:
for i in left:
print(f'textdata: {addr + 4 * i:#x} {int(buf[i]):08x} D {path}')
dirty += size
if per is not None:
per.append((path, ci))
nimg += 1
if not dry and dirty > 256 << 20: # write back as it goes: a slow (USB) image would pile up dirty pages
for mm in files.values():
Expand Down Expand Up @@ -245,18 +269,106 @@ def do_macho(path, dry, left=None):
return len(slices), tot


def check_log(main, logp, restore=False, dry=False):
"""verify (restore=False): every logged word holds NEW. restore: every logged word that holds NEW gets OLD back.
Returns (ok, bad)."""
maps, files = [], {}
for p in sorted(glob.glob(main + '*')):
if p.endswith(('.map', '.atlas', '.symbols')):
continue
with open(p, 'rb') as f:
h = f.read(0x200)
if not h.startswith(b'dyld_v1'):
continue
mo, mc = struct.unpack_from('<II', h, 0x10)
with open(p, 'rb') as f:
f.seek(mo)
for _ in range(mc):
a, sz, o, _, _ = struct.unpack('<QQQII', f.read(32))
maps.append((a, sz, p, o))
files[p] = np.memmap(p, dtype=np.uint8, mode='r+' if restore and not dry else 'r')
ok = bad = 0
for line in open(logp):
if line.startswith('#') or not line.strip():
continue
ad, old, new = line.split()
ad, old, new = int(ad, 16), int(old, 16), int(new, 16)
for a, sz, p, o in maps:
if a <= ad < a + sz:
v = files[p][o + ad - a:o + ad - a + 4].view(np.uint32)
break
else:
bad += 1
continue
cur = int(v[0])
if restore:
if cur == new:
if not dry:
v[0] = old
ok += 1
elif cur == old:
ok += 1
else:
bad += 1
if bad <= 10:
print(f'restore: {ad:#x} holds {cur:08x}, neither {old:08x} nor {new:08x}: left alone')
else:
good = cur == new
ok += good
bad += not good
if not good and bad <= 10:
print(f'verify: {ad:#x} holds {cur:08x}, logged {old:08x} -> {new:08x}')
if restore and not dry:
for mm in files.values():
mm.flush()
return ok, bad


def census(main, allow):
"""the words a64e-rewrite would still change, per image, with nothing skipped; exit status 1 if an image not in allow
has any. A skipped image (libcorecrypto until 18 November 2026) shows up here instead of in the kernel's trap counts."""
per = []
_, tot = do_cache(main, (), True, (), per=per)
keys = ('ret', 'br', 'blr', 'pac', 'ldra', 'ldapr')
bad = 0
rows = sorted(((c.get('changed', 0), p, c) for p, c in per), reverse=True)
for n, p, c in rows:
if not n:
break
allowed = any(x in p for x in allow)
bad += not allowed
print(f'{"allowed" if allowed else "LEFT":8s} {n:7d} {p} ' + ' '.join(f'{k} {c.get(k, 0)}' for k in keys if c.get(k)))
left = {k: v for k, v in tot.items() if k.endswith('_left') and v}
print(f'census: {len(per)} images, {sum(1 for n, _, _ in rows if n)} with words to rewrite, {bad} not allowed; '
f'left for the kernel by design: {left}')
return bad


def main():
ap = argparse.ArgumentParser()
ap.add_argument('kind', choices=['cache', 'macho'])
ap.add_argument('kind', choices=['cache', 'macho', 'census', 'verify', 'restore'])
ap.add_argument('--log', help='cache: write ADDR OLD NEW per changed word; verify/restore: the log of a cache run')
ap.add_argument('--allow', nargs='*', default=[], help='census: images allowed to keep words to rewrite')
ap.add_argument('paths', nargs='+')
ap.add_argument('--only', nargs='*', default=[])
ap.add_argument('--dry-run', action='store_true')
ap.add_argument('--skip', nargs='*', default=[],
help='leave images whose path contains one of these untranslated '
'(libcorecrypto: its FIPS self-test MACs its own code)')
a = ap.parse_args()
if a.kind == 'census':
sys.exit(1 if census(a.paths[0], a.allow) else 0)
if a.kind in ('verify', 'restore'):
if not a.log:
ap.error(f'{a.kind} needs --log')
ok, bad = check_log(a.paths[0], a.log, a.kind == 'restore', a.dry_run)
print(f'{a.kind}: {ok} ok, {bad} bad')
sys.exit(1 if bad else 0)
if a.kind == 'cache':
n, tot = do_cache(a.paths[0], a.only, a.dry_run, a.skip)
log = open(a.log, 'w') if a.log else None
n, tot = do_cache(a.paths[0], a.only, a.dry_run, a.skip, log=log)
if log:
log.close()
print(f'{n} images', tot)
if os.environ.get('TEXTDATA_SKIP'):
print(f'textdata: {tot.get("data_left", 0)} words left alone')
Expand Down
Loading
Loading