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: 1 addition & 1 deletion mac-manual/content/04-security.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ If a bug ever writes that shared state wrongly, the enclave stops accepting it a
- With FileVault on, macOS can't unlock its disk and won't start, and the startup picker may not open. Getting the Mac back takes a DFU restore, which erases it.
- With FileVault off, macOS resets its keychain and Touch ID fingerprints and shows a warning.

On a MacBook Neo, setup also reads the sensor's key from the factory records the installer left on the boot partition, into a firmware file only root can read. Turning Touch ID off deletes that file.
On a MacBook Neo or a 16-inch MacBook Pro with M4 Pro, setup also reads the sensor's key (and on the M4 Pro its calibration) from the factory records the installer left on the boot partition, into firmware files only root can read. Turning Touch ID off deletes those files, and turning it on again rebuilds them.

Turning Touch ID off later removes your fingerprints and stops the driver loading. The key store stays.

Expand Down
2 changes: 1 addition & 1 deletion omarchy-mac/README.md

Large diffs are not rendered by default.

22 changes: 17 additions & 5 deletions omarchy-mac/entrypoints/disable-fingerprint
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# omarchy-lifecycle-dispatch disable-fingerprint: Omarchy's fingerprint removal
# runs this as root once the fingerprints are deleted. Removing the owner's
# agreement stops the driver loading, and writing, at boot; the key store stays.
# A Neo's sensor key goes too: turning Touch ID on again rebuilds it.
# The sensor key a Neo or a 16-inch M4 Pro rebuilt goes too, with the M4 Pro's
# calibration: turning Touch ID on again rebuilds them.
omarchy-hw-apple-silicon || exit 0

# Waits for a Touch ID setup still running, which could otherwise install its
Expand All @@ -16,11 +17,22 @@ elif ! flock -w 300 "$lock_fd"; then
exit 1
fi

key=/usr/lib/firmware/updates/apple/msrk-j700.bin
if [[ -e /etc/omarchy-mac/touchid-on || -e /etc/modprobe.d/omarchy-mac-touchid.conf || -e $key || -e $key.new ]]; then
# What Touch ID setup rebuilt from the factory records; the next setup rebuilds it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: "the next setup rebuilds them", now that the list covers three files.

— Marcelo's agent

rebuilt=(/usr/lib/firmware/updates/apple/msrk-j700.bin /usr/lib/firmware/updates/apple/msrk-j616s.bin
/usr/lib/firmware/updates/apple/mesacal-j616s.bin)
records=(/etc/omarchy-mac/touchid-on /etc/modprobe.d/omarchy-mac-touchid.conf)
present=()
for file in "${records[@]}" "${rebuilt[@]}" "${rebuilt[@]/%/.new}"; do
[[ -e $file ]] && present+=("$file")
done
if (( ${#present[@]} )); then
# A record left behind, on a read-only root say, loads the driver again.
if ! rm -f /etc/omarchy-mac/touchid-on /etc/modprobe.d/omarchy-mac-touchid.conf "$key" "$key.new"; then
echo "Touch ID could not be turned off: remove /etc/omarchy-mac/touchid-on, /etc/modprobe.d/omarchy-mac-touchid.conf and $key" >&2
if ! rm -f "${present[@]}"; then
left=()
for file in "${present[@]}"; do
[[ -e $file ]] && left+=("$file")
done
echo "Touch ID could not be turned off: remove ${left[*]}" >&2
exit 1
fi
echo "Touch ID stops loading at the next boot. The key store for Linux stays on this Mac."
Expand Down
36 changes: 26 additions & 10 deletions omarchy-mac/lib/touchid-eligibility
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ can_extract() {
}

sensor=""
for node in "$tree"/soc/*/fingerprint@*; do
# The sensor sits on an SPI controller under /soc, or at the root on the M4 Pro.
for node in "$tree"/soc/*/fingerprint@* "$tree"/*/fingerprint@*; do
if [[ -r $node/compatible ]] && tr '\0' '\n' <"$node/compatible" | grep -Fxq apple,mesa-fingerprint &&
[[ ! -r $node/status || $(tr -d '\0' <"$node/status") == "okay" ]]; then
sensor=$node
Expand All @@ -51,7 +52,8 @@ if compatible apple,t8103 || compatible apple,t6000 || compatible apple,t6001; t
echo unsupported:m1-family
exit 0
elif ! compatible apple,t8112 && ! compatible apple,t6020 && ! compatible apple,t6021 && ! compatible apple,t8140 &&
! { compatible apple,t8122 && { compatible apple,j613 || compatible apple,j615; }; }; then
! { compatible apple,t8122 && { compatible apple,j613 || compatible apple,j615; }; } &&
! { compatible apple,t6040 && compatible apple,j616s; }; then
echo unsupported:board
exit 0
fi
Expand All @@ -62,20 +64,34 @@ if [[ ! -r $tree/chosen/apfs-preboot-uuid ]]; then
exit 0
fi

# The Neo's calibration must be the sealed form its installer writes; the
# other boards' can be carved from the iBoot System Container at enable time.
# The node's firmware-name lists the calibration, then, where the board names
# it, the sensor root key.
calibration=apple/mesa_calibration.bin
root_key=apple/msrk-j700.bin
if [[ -r $sensor/firmware-name ]]; then
calibration=$(tr -d '\0' <"$sensor/firmware-name")
mapfile -t names < <(tr '\0' '\n' <"$sensor/firmware-name")
calibration=${names[0]:-$calibration}
root_key=${names[1]:-$root_key}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now reads a second firmware-name entry on every board. Could you confirm the Neo's node names nothing other than apple/msrk-j700.bin there?

— Marcelo's agent

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checked on a MacBook Neo (7.3.0-0test4), read-only: /proc/device-tree/soc/spi@385108000/fingerprint@0/firmware-name holds one entry, apple/mesacal-j700.bin, with no second entry. So root_key keeps the default apple/msrk-j700.bin, which is the name disable-fingerprint deletes. No change needed here.

fi
if ! installed "$calibration" && { compatible apple,t8140 || ! can_extract extract-mesa-calibration; }; then
# touchid-root-key (Python) rebuilds sealed factory data once the owner agrees,
# from the factory records asahi-installer leaves on the ESP.
can_reseal() {
[[ -r $esp/asahi/all_firmware.tar.gz ]] && command -v "$python" >/dev/null
}
# The Neo's calibration must be the sealed form its installer writes, and the
# 16-inch M4 Pro's is rebuilt from its factory records like its root key; the
# other boards' can be carved from the iBoot System Container at enable time.
if compatible apple,j616s; then
if ! installed "$calibration" && ! can_reseal; then
echo unsupported:no-calibration
exit 0
fi
elif ! installed "$calibration" && { compatible apple,t8140 || ! can_extract extract-mesa-calibration; }; then
echo unsupported:no-calibration
exit 0
fi
# The Neo's sensor key is rebuilt once the owner agrees, by touchid-root-key
# (Python), from the factory records asahi-installer leaves on the ESP.
if compatible apple,t8140 && ! installed apple/msrk-j700.bin &&
{ [[ ! -r $esp/asahi/all_firmware.tar.gz ]] || ! command -v "$python" >/dev/null; }; then
# The Neo's and the M4 Pro's sensor root key is rebuilt the same way.
if { compatible apple,t8140 || compatible apple,j616s; } && ! installed "$root_key" && ! can_reseal; then
echo unsupported:no-root-key
exit 0
fi
Expand Down
50 changes: 40 additions & 10 deletions omarchy-mac/lib/touchid-enable
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/bin/bash
# Turns Touch ID on once the owner has agreed: carves this Mac's calibration
# (and on the Neo installs its sensor root key) if they are missing, records the
# agreement so the driver loads at every boot, and loads it now.
# Turns Touch ID on once the owner has agreed: installs this Mac's calibration
# and, on the Neo and the 16-inch M4 Pro, its sensor root key if missing, records
# the agreement so the driver loads at every boot, and loads it now.

tree=${OMARCHY_MAC_TOUCHID_TREE:-/proc/device-tree}
firmware=${OMARCHY_MAC_TOUCHID_FIRMWARE:-/usr/lib/firmware}
Expand All @@ -27,12 +27,18 @@ omarchy-hw-apple-silicon || exit 0
# restart. A record from an earlier agreement stays.
recorded=0
rebuilt=0
rebuilt_calibration=0
# The sensor root key's name; the 16-inch M4 Pro's sensor node names its own.
root_key=apple/msrk-j700.bin
fail() {
if (( recorded )); then
rm -f "$agreed" "$options"
fi
if (( rebuilt )); then
rm -f "$firmware/updates/apple/msrk-j700.bin"
rm -f "$firmware/updates/$root_key"
fi
if (( rebuilt_calibration )); then
rm -f "$firmware/updates/$calibration"
fi
echo "Touch ID was not turned on: $1" >&2
exit 1
Expand All @@ -54,7 +60,7 @@ installed() {
root_key_installed() {
local directory key found=1 checked
for directory in "$firmware" "$firmware/vendor" "$firmware/updates"; do
key=$directory/apple/msrk-j700.bin
key=$directory/$root_key
if [[ ! -e $key ]]; then
continue
elif ! command -v "$python" >/dev/null; then
Expand Down Expand Up @@ -90,11 +96,35 @@ eligibility=$("$helpers/touchid-eligibility")

# Factory data from this Mac's own iBoot System Container, never another's.
sensor=$("$helpers/touchid-eligibility" sensor)
# The node's firmware-name lists the calibration, then, where the board names
# it, the sensor root key.
calibration=apple/mesa_calibration.bin
if [[ -r $sensor/firmware-name ]]; then
calibration=$(tr -d '\0' <"$sensor/firmware-name")
mapfile -t names < <(tr '\0' '\n' <"$sensor/firmware-name")
calibration=${names[0]:-$calibration}
root_key=${names[1]:-$root_key}
fi
for name in "$calibration" "$root_key"; do
[[ $name =~ ^apple/[A-Za-z0-9._-]+\.bin$ ]] || fail "the sensor names an unexpected firmware file ($name)"
done
# The Macs whose sensor loads a sealed root key after a nonce session, by the
# SoC their seal record names: the Neo, and the 16-inch M4 Pro, whose
# calibration is sealed the same way and rebuilt with the key.
chip=""
if tr '\0' '\n' <"$tree/compatible" | grep -Fxq apple,t8140; then
chip=00008140
elif tr '\0' '\n' <"$tree/compatible" | grep -Fxq apple,j616s; then
chip=00006040
fi
if ! installed "$calibration"; then
if [[ $chip == 00006040 ]] && ! installed "$calibration"; then
# Rebuilt from the factory records asahi-installer leaves on the ESP and
# checked against this Mac's iBoot System Container, like the root key.
echo "Reading this Mac's Touch ID calibration..."
mkdir -p -m 0755 "$firmware/updates/${calibration%/*}"
rebuilt_calibration=1

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing tests this rollback yet: please add a case where the calibration rebuild succeeds and the key step fails.

— Marcelo's agent

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 6b5d94a: the consent test adds a J616s run with a container holding the FSC2 record, the seal and the trust bundle but not the MSRk record. The calibration rebuilds, the key step fails, and the test checks that mesacal-j616s.bin (and any .new) and touchid-on are gone. With the rollback removed (lines 40-42 and rebuilt_calibration=1), that case fails.

"$python" -I "$helpers/touchid-root-key" --record FSC2 --chip "$chip" --archive "$esp/asahi/all_firmware.tar.gz" \
--container "$container" -o "$firmware/updates/$calibration" || fail "this Mac's calibration could not be read"
elif ! installed "$calibration"; then
echo "Copying this Mac's Touch ID calibration..."
mkdir -p -m 0755 "$firmware/updates/${calibration%/*}"
# A partial carve never takes the calibration's name, which a retry would
Expand All @@ -104,7 +134,7 @@ if ! installed "$calibration"; then
{ rm -f "$carved.new"; fail "this Mac's calibration could not be read"; }
chmod 0644 "$carved.new" && mv -f "$carved.new" "$carved" || { rm -f "$carved.new"; fail "this Mac's calibration could not be installed"; }
fi
if tr '\0' '\n' <"$tree/compatible" | grep -Fxq apple,t8140 && ! root_key_installed; then
if [[ -n $chip ]] && ! root_key_installed; then
# Rebuilt only now, from the factory records asahi-installer leaves on the
# ESP and checked against this Mac's iBoot System Container; root-only, and
# it takes its name only once whole. Turning Touch ID off deletes it.
Expand All @@ -114,8 +144,8 @@ if tr '\0' '\n' <"$tree/compatible" | grep -Fxq apple,t8140 && ! root_key_instal
# a failure, even one the extractor reports after its rename, takes it back
# with the agreement.
rebuilt=1
"$python" -I "$helpers/touchid-root-key" --archive "$esp/asahi/all_firmware.tar.gz" --container "$container" \
-o "$firmware/updates/apple/msrk-j700.bin" || fail "this Mac's sensor key could not be read"
"$python" -I "$helpers/touchid-root-key" --chip "$chip" --archive "$esp/asahi/all_firmware.tar.gz" \
--container "$container" -o "$firmware/updates/$root_key" || fail "this Mac's sensor key could not be read"
fi

# The record is the agreement: omarchy-mac-touchid-load.service loads the
Expand Down
64 changes: 37 additions & 27 deletions omarchy-mac/lib/touchid-root-key
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
#!/usr/bin/python3 -I
# SPDX-License-Identifier: MIT
"""Rebuilds a MacBook Neo's Touch ID sensor root key, apple/msrk-j700.bin,
once its owner has agreed to turn Touch ID on.

The key is device-private. asahi-installer leaves this Mac's factory records
on the ESP, in asahi/all_firmware.tar.gz; the key is the MSRk record's IM4P
under the FDR sealing manifest (seal-<ECID>), with the trust bundle as secb,
as macOS stores the other records in its factory cache. Every byte comes from
"""Rebuilds a Touch ID sensor's sealed factory data once its owner has agreed
to turn Touch ID on: the sensor root key (the MSRk record) on a MacBook Neo,
apple/msrk-j700.bin, or a 16-inch M4 Pro, apple/msrk-j616s.bin; and with
--record FSC2 the M4 Pro's calibration, apple/mesacal-j616s.bin.

Both are device-private. asahi-installer leaves this Mac's factory records
on the ESP, in asahi/all_firmware.tar.gz; each file is the record's IM4P
under the FDR sealing manifest (seal-<CHIP>-<ECID>), with the trust bundle as
secb, as macOS stores the other records in its factory cache. Every byte comes from
those records, and each must also be found, byte for byte, somewhere in this
Mac's own iBoot System Container. That proves each record is present there, not
that it is the active or an authentic one. The logic follows
omarchy-mac-installer's factory_root_key().

With --check FILE it only says whether FILE is a whole sensor root key: it exits
3 when FILE is not one, and 1 when it could not tell."""
With --check FILE it only says whether FILE is a whole sealed record of that
kind: it exits 3 when FILE is not one, and 1 when it could not tell."""

import argparse
import hashlib
Expand All @@ -28,10 +30,14 @@ RECORD_MAXIMUM = 16 << 20
MEMBER_MAXIMUM = 1 << 14
RETAINED_MAXIMUM = 32 << 20
# The records the key is rebuilt from; anything else in the cache is skipped.
RECORDS = ("MSRk-", "seal-", "trustobject-")
RECORDS = ("MSRk-", "FSC2-", "seal-", "trustobject-")
CONTAINER_MAXIMUM = 1 << 30
# The Neo's SoC, as its seal-<ECID> record names it.
# The SoCs whose sensors load sealed factory data, as their seal records name
# them, and what a refusal calls the Mac. The Neo's is the default.
CHIPS = {"00008140": "a MacBook Neo", "00006040": "an M4 Pro MacBook Pro"}
CHIP = "00008140"
# What each record kind is called in a refusal.
KINDS = {"MSRk": "sensor root key", "FSC2": "sealed calibration"}


class Refused(Exception):
Expand Down Expand Up @@ -86,8 +92,8 @@ def der(tag, value):
return bytes([tag]) + head + value


def check_root_key(data):
"""data, if it is one complete comb { fdrd: IMG4 { IM4P(MSRk), [0] IM4M },
def check_root_key(data, kind="MSRk"):
"""data, if it is one complete comb { fdrd: IMG4 { IM4P(kind), [0] IM4M },
secb: trust bundle } with nothing after it. The envelope only."""
try:
if not 0 < len(data) <= RECORD_MAXIMUM:
Expand Down Expand Up @@ -115,7 +121,7 @@ def check_root_key(data):
raise ValueError("missing signed envelope")
payload = children(data, parts[1][1], parts[1][2])
if (len(payload) != 4 or ia5(data, payload[0]) != b"IM4P"
or ia5(data, payload[1]) != b"MSRk" or payload[2][0] != 0x16
or ia5(data, payload[1]) != kind.encode() or payload[2][0] != 0x16
or payload[2][1] == payload[2][2] or payload[3][0] != 4
or payload[3][1] == payload[3][2]):
raise ValueError("invalid payload")
Expand All @@ -126,32 +132,32 @@ def check_root_key(data):
if len(manifest) < 2 or ia5(data, manifest[0]) != b"IM4M":
raise ValueError("missing manifest")
except (ValueError, IndexError) as error:
raise Refused(f"the key is not a whole sensor root key: {error}") from error
raise Refused(f"the file is not a whole {KINDS[kind]}: {error}") from error
return data


def reseal(record, seal, trust):
"""comb { fdrd: IMG4 { the MSRk record's IM4P, [0] seal }, secb: trust }."""
def reseal(record, seal, trust, kind="MSRk"):
"""comb { fdrd: IMG4 { the record's IM4P, [0] seal }, secb: trust }."""
try:
outer = tlv(record, 0)
parts = children(record, outer[1], outer[2])
if (outer[0] != 0x30 or outer[2] != len(record) or len(parts) != 3
or ia5(record, parts[0]) != b"IMG4" or parts[1][0] != 0x30):
raise ValueError("the MSRk record is not an IMG4")
raise ValueError(f"the {kind} record is not an IMG4")
im4p = bytes(record[parts[0][2]:parts[1][2]])
manifest = tlv(seal, 0)
if manifest[0] != 0x30 or manifest[2] != len(seal):
raise ValueError("the seal record is not one DER manifest")
except ValueError as error:
raise Refused(f"cannot rebuild the sensor root key: {error}") from error
raise Refused(f"cannot rebuild the {KINDS[kind]}: {error}") from error
image = der(0x30, der(0x16, b"IMG4") + im4p + der(0xA0, seal))
return der(0x30, der(0x16, b"comb")
+ der(0x30, der(0x16, b"fdrd") + der(0x04, image))
+ der(0x30, der(0x16, b"secb") + der(0x04, trust)))


def factory_records(archive):
"""name -> bytes for the MSRk, seal and trust bundle records in the
"""name -> bytes for the MSRk, FSC2, seal and trust bundle records in the
archive's com.apple.factorydata directory, read into memory only. Other
entries there (a subdirectory or record a later macOS adds) are skipped."""
records = {}
Expand Down Expand Up @@ -284,15 +290,15 @@ def apple_silicon():
NOT_WHOLE = 3


def check(path):
def check(path, kind):
try:
with open(path, "rb") as fd:
data = fd.read(RECORD_MAXIMUM + 1)
except OSError as error:
print(f"touchid-root-key: cannot read {path}: {error}", file=sys.stderr)
return 1
try:
check_root_key(data)
check_root_key(data, kind)
except Refused as error:
print(f"touchid-root-key: {path}: {error}", file=sys.stderr)
return NOT_WHOLE
Expand All @@ -306,20 +312,24 @@ def main():
parser = argparse.ArgumentParser(description=__doc__.split("\n\n")[0])
parser.add_argument("--archive", default="/boot/efi/asahi/all_firmware.tar.gz")
parser.add_argument("--container", default="/dev/disk/by-partlabel/iBootSystemContainer")
parser.add_argument("--chip", choices=sorted(CHIPS), default=CHIP,
help="the SoC the seal record must name (default: the Neo's)")
parser.add_argument("--record", choices=sorted(KINDS), default="MSRk",
help="MSRk for the sensor root key, FSC2 for the calibration")
action = parser.add_mutually_exclusive_group(required=True)
action.add_argument("-o", "--output")
action.add_argument("--check", metavar="FILE")
arguments = parser.parse_args()
if arguments.check:
return check(arguments.check)
return check(arguments.check, arguments.record)
try:
records = factory_records(arguments.archive)
_, record = one(records, "MSRk-")
_, record = one(records, f"{arguments.record}-")
seal_name, seal = one(records, "seal-")
if not seal_name.upper().startswith(f"SEAL-{CHIP}-"):
raise Refused(f"the seal record {seal_name} is not a MacBook Neo's")
if not seal_name.upper().startswith(f"SEAL-{arguments.chip}-"):
raise Refused(f"the seal record {seal_name} is not {CHIPS[arguments.chip]}'s")
trust = trust_bundle(records)
key = check_root_key(reseal(record, seal, trust))
key = check_root_key(reseal(record, seal, trust, arguments.record), arguments.record)
if not in_container(arguments.container, [record, seal, trust]):
raise Refused(f"the factory records in {arguments.archive} are not all in this Mac's "
f"iBoot System Container ({arguments.container}): they are another Mac's, "
Expand Down
Loading
Loading