Skip to content

Code review fixes: bind all claims into the KEK, fail-closed attestation, demo layer scan, symlink escape - #8

Merged
skalvinnathan merged 12 commits into
mainfrom
fix/code-review-2026-11
Nov 15, 2026
Merged

skalvinnathan merged 12 commits into
mainfrom
fix/code-review-2026-11

Conversation

@skalvinnathan

Copy link
Copy Markdown
Member

Summary

This is a code review of the whole repository. It fixes the open issues that could be fixed safely, plus the bugs the review found. Each change is in its own commit.

Issues

  • go.mod module path does not match the repository #7 Module path. go.mod is now github.com/AI2TH/cryptship, every import is rewritten, and the README has a go install line.
  • Bind every license claim into the KEK, not just a subset #1 Every claim is now bound into the KEK (kek-ctx/v2). The context is "cryptship/kek-ctx/v2" NUL SHA-256(canonical claims). The canonical claims are the encoding/json form of Claims with wrapped_keys and unbound_secret cleared.
    • claims.v picks the context: 1 keeps the v1 layout byte for byte, 2 uses the new hash, and any other value is an error with no fallback.
    • v is inside both contexts, so rewriting a v2 license as v1 breaks the unwrap.
    • cryptship issue writes v2 by default. --license-version 1 is kept for fleets that still run older loaders, which reject v2.
    • Parse now rejects negative versions.
    • Found and fixed along the way: wrapPerFingerprint added each fingerprint hash after wrapping with it. Under v2 that gave every pool key a wrong context. It now records all hashes first.
  • --require-attestation accepts any non-empty evidence #3 --require-attestation fails closed. The server refuses to start with the flag, because there is no verifier yet.
    • New attest.Verifier interface: Verify(evidence, nonce) (Measurement, error).
    • licenseserver.Server.Verifier replaces the old Verify func.
    • Evidence must commit to online.AttestationNonce(license_id, nonce), so evidence made for another request nonce or another license is rejected.
    • pkg/attest/attesttest holds the only implementation, for tests. It replaces keyprovider.MockAttester.
  • demo.sh needs python3 on the host, and the layer check can pass falsely #6 Demo. The new scripts/check-image-opacity.sh runs docker save and unpacks every layer blob in a container (tar or gzip, legacy and OCI layouts). It fails on a blob it cannot unpack, including zstd. As a positive control, the unpacked layers must contain /cryptship/payload.enc.
    • The demo app now carries a unique marker string for the scan to find.
    • The tamper step runs in the image's Python, so the host needs only docker.
    • demo-compiled.sh uses the same scan.
  • Decrypted files are readable by the app's own user and by container root #5 (part) Non-root. All three example Dockerfiles now set USER 65532:65532 and create a state dir that user owns. The hardened docker run in deploy/README.md gets --user 65532:65532 and a tmpfs owned by that uid.
  • Offline expiry can be bypassed by patching the loader or rolling back the clock #2 (part) Clock-rollback check. For licenses with not_after, the loader records the latest start time. The record is sealed with AES-GCM under a key derived from the content key and license id. The loader refuses to start if the clock is more than 24h behind that record. This is best effort and documented as such: deleting the state resets it, and a state dir it cannot write disables it with a warning.

Other bugs found in review

  • archive: symlink escape. A pair of entries s -> "." and t -> "s/.." created t pointing at the parent of the extract dir, in either entry order. Targets may now use .. only as leading components, and no higher than the link's own depth. TarDir applies the same rule at pack time and now closes each file right away (the deferred Close inside the walk kept every file open).
  • loader:
    • prepareDir emptied whatever CRYPTSHIP_EXTRACT named, so a typo like /home/user wiped that directory. It now only empties an empty dir, or one marked by an earlier run.
    • The revocation state file name embedded license_id raw, so an id containing / could put it outside the state dir. The id is now escaped.
    • grace: treated a future last-success time as within grace.
    • The manifest write followed symlinks. It now uses O_EXCL.
  • cli: master-key overwrote an existing master key, which breaks every online/attested license issued under it. It now needs --force, as keygen already does.
  • keyprovider: a 403 (attestation rejected) was reported as "revoked". It now returns a new ErrDenied.

Fixes #7
Fixes #1
Fixes #6
Part of #3
Part of #5
Part of #2

Testing

All runs used Docker (colima, Docker 29.5.2) with golang:1.24 (go1.24.13 linux/arm64).

  • Same checks as CI. gofmt -l . (empty), then go vet ./..., go build ./..., go build -tags cryptship_devfp ./cmd/cryptship-loader and go test -race -count=1 ./.... Result: pass, 8 packages with tests, 0 failures.
  • New tests.
    • Golden vectors for v1 (taken from the code before this change) and for v2. The v2 hash was also recomputed by hand outside Go and matches.
    • Every claim and binding field changes the v2 context, and a table test fails if a field is added without coverage.
    • With the signature check bypassed, editing revocation_url, entitlements, edition, signing_key_id or binding.max stops a v2 license from unwrapping. The same edits still unwrap a v1 license.
    • Rewriting v from 1 to 2 or from 2 to 1 does not unwrap.
    • A pool license unwraps with every fingerprint under both versions.
    • Server returns 403 for missing, arbitrary, other-nonce and other-license evidence, and --require-attestation with no verifier returns an error.
    • Untar refuses the chained-symlink escape in both entry orders.
    • Loader: prepareDir, manifest, state-file name, grace and clock tests.
  • Mutation checks. Making v2 fall back to the v1 context makes TestV2EditedClaimsDoNotUnwrap fail. Restoring the old wrap order makes TestWrapIntoPoolEveryFingerprintUnwraps fail.
  • End to end in a container (CLI plus the devfp loader):
    • v2, v1 and a license issued by the old main CLI all run.
    • A v2 pool license runs on node-a and node-b and is refused on node-c.
    • A foreign non-empty extract dir is refused and its file is kept.
    • A tampered signature is refused.
    • A second master-key is refused.
    • cryptship-server --require-attestation exits 1.
  • Demos. bash scripts/demo.sh, bash scripts/demo-compiled.sh and bash scripts/demo-online.sh all exit 0. The images run as uid 65532, and the hardened docker run from deploy/README.md (with seccomp) runs the demo.
  • Layer scan.
    • Docker 29 with the containerd store saves gzip layers. On a leaky image, the old grep passed falsely and the new scan reports the leak.
    • A plain-tar variant is also caught.
    • Garbage and zstd blobs fail the scan rather than pass it.
  • Mermaid. All 5 blocks render with the pinned mermaid-cli image.

Not fixed (and why)

  • --require-attestation accepts any non-empty evidence #3, step 3: real SEV-SNP/TDX verifiers need hardware and new dependencies. The attested protocol also has a gap: the client chooses the nonce and the secret is not encrypted to a TEE-held key, so a captured request with genuine evidence can be replayed. This needs a server challenge and an ephemeral key in REPORT_DATA, as Trustee does. It is documented in ARCHITECTURE.md.
  • The server cannot tell an attested license from an online one. A non-verifying server that has the same master key releases attested secrets. This is documented in HOWTO.md: use a separate master key.
  • Offline expiry can be bypassed by patching the loader or rolling back the clock #2, steps 2 (rest) and 3: the expiry attestation from the server is not done, and the clock record can still be reset by deleting it.
  • online provider: no outage grace, and revocation needs a server restart #4 (grace cache, live denylist, seats): larger features, so not done here.
  • Decrypted files are readable by the app's own user and by container root #5, steps 2 to 4 (separate-uid extraction, memfd_create/fexecve, PR_SET_DUMPABLE helpers): not small, so not done here.
  • Revocation check is unsigned. Any 200 from revocation_url (an on-path attacker, a captive portal) counts as "not revoked", even with CRYPTSHIP_REVOCATION=closed. The grace state file is plain text, so anyone who can write the state dir can extend grace. Fixing this needs a signed response, which is a protocol change.
  • --expires YYYY-MM-DD expires at 00:00 UTC at the start of that day. Changing it would change existing behaviour, so it is left as is.
  • scripts/check-mermaid.sh's docker fallback fails on a Linux host: its mktemp -d dir is 0700, so the mermaid-cli container user cannot read it. CI uses mmdc and is not affected.
  • API changes for anyone importing the packages: the module path, Claims.BindingContext() now returns ([]byte, error), licenseserver.Server.Verify is now Verifier, and keyprovider.MockAttester is removed.

go.mod declared github.com/cryptship/cryptship, so
go install github.com/AI2TH/cryptship/cmd/...@latest failed. Rewrite the
module line and every import, and document go install in the README.

Fixes #7
The v1 KEK context covered only version, license and customer id, product,
payload id, policy, issued_at and not_after. Edition, entitlements,
revocation_url, binding.server/max/fingerprint_hashes and signing_key_id were
protected only by the signature, so a loader with the signature check patched
out would run a license with revocation_url removed.

Format v2 binds "cryptship/kek-ctx/v2" NUL SHA-256(canonical claims), where the
canonical claims are the encoding/json form of Claims with wrapped_keys and
unbound_secret cleared. The context is selected by claims.v: 1 keeps the
byte-identical v1 layout (golden vector), 2 uses the new hash, anything else is
an error. Parse now also rejects negative versions.

cryptship issue writes v2 by default; --license-version 1 is available for
fleets still running older loaders. wrapPerFingerprint now records every
fingerprint hash before wrapping, since v2 covers those hashes.

Tests: golden vectors for v1 and v2 (v2 cross-checked outside Go), every claim
changes the v2 context, edited revocation_url / entitlements / edition /
signing_key_id / binding.max do not unwrap a v2 license with the signature
bypassed, v1 still unwraps, v1<->v2 rewrites do not unwrap, and pool licenses
unwrap with every fingerprint under both versions.

Fixes #1
--require-attestation accepted any non-empty evidence, so an operator who
turned it on got no protection. There is no SEV-SNP/TDX verifier yet, so the
server now refuses to start with the flag and says why.

Add pkg/attest with a Verifier interface (Verify(evidence, nonce)
(Measurement, error)) and replace licenseserver.Server.Verify with a Verifier
field. Evidence must commit to online.AttestationNonce(license_id, nonce), so
evidence for another request nonce or another license is rejected; the
attested provider now obtains evidence after choosing the nonce.

pkg/attest/attesttest holds the only implementation, for tests: its evidence
proves nothing about the runtime. keyprovider.MockAttester, which returned
fixed evidence, is replaced by attesttest.Attester.

Docs: describe the fail-closed flag, that a non-verifying server with the same
master key releases attested secrets, and that the client-chosen nonce still
allows replay of a captured request (needs a server challenge, left for the
real verifier work).

Part of #3
The opacity check grepped the docker save output without unpacking each
layer, so source inside a gzip-compressed layer blob would not be found and
the check would pass falsely. scripts/check-image-opacity.sh now streams the
save into a throwaway container, unpacks every layer blob (tar or gzip, legacy
and OCI layouts) and fails on any blob it cannot unpack, including zstd. As a
positive control it requires /cryptship/payload.enc in the unpacked layers.

The demo app now carries a unique marker string that the scan looks for, and
demo-compiled.sh uses the same scan.

The tamper step ran python3 on the host although the README says only docker
is needed. It now runs in the runtime image's Python over stdin/stdout.

Fixes #6
None of the example Dockerfiles set USER, so the loader and the app ran as
container root. Add USER 65532:65532 (with a state dir owned by that uid) to
all three, and --user 65532:65532 plus a tmpfs owned by that uid to the
hardened docker run in deploy/README.md. Note that --read-only leaves no
writable state dir unless one is mounted.

Part of #5
checkLink only checked that a link target was lexically inside dest. A target
that passes through another symlink resolves against that symlink's target,
so the pair s -> "." and t -> "s/.." created t pointing at the parent of
dest, in either entry order. Writes stayed inside dest (os.Root), but the
extracted tree handed the app a link out of the extract directory.

Symlink targets may now use ".." only as leading components, climbing no
higher than the link's own depth; ".." after an ordinary component is
refused. Every accepted link then resolves inside dest whatever the entry
order.

TarDir applies the same rule and refuses unsupported file types at pack time,
so a bad tree fails on the vendor's machine instead of on every customer's.
It also closes each file as soon as it is copied; the deferred Close inside
the walk callback kept every file open until the walk ended.
…e gaps

- prepareDir deleted the contents of whatever CRYPTSHIP_EXTRACT named, so a
  typo such as /home/user emptied that directory. It now empties only a
  directory that is empty or carries the marker an earlier run left
  (.cryptship-extract-dir, or the manifest written by older loaders).
- The revocation state file was named "revocation-"+license_id, so an id
  containing "/" could place it outside CRYPTSHIP_STATE_DIR. The id is now
  path-escaped (plain ids keep their old file names).
- grace:<dur> treated a last-success time in the future (clock set back, or a
  hand-edited file) as within grace. Such times are now ignored.
- The manifest was written with os.WriteFile, which follows a symlink the
  payload placed at that name. It is now created with O_EXCL, and a failure is
  reported as a warning instead of being dropped.
…nses

An unedited expired license kept running if the system clock was set back.
For licenses with not_after, the loader now records the latest start time in
CRYPTSHIP_STATE_DIR, sealed with AES-GCM under a key derived from the content
key and license id, and refuses to start when the clock is more than 24h
behind that record. The tolerance absorbs NTP corrections and restored
snapshots.

This is best effort and documented as such: deleting the state file resets
it, and an unwritable state dir disables it with a warning rather than
blocking startup. Hard expiry still needs the online provider.

Part of #2
cryptship master-key overwrote master.key unconditionally. The server derives
every online/attested license secret from it, so rerunning the command (for
example a copied demo script) silently broke every license issued under the
old key. Require --force, as keygen already does for the signing key.
The server answers 410 for a revoked license and 403 when it rejects
attestation evidence, but the provider mapped both to ErrRevoked, so a
failed attestation told the operator the license was revoked. Map 403 to a
new ErrDenied.
@skalvinnathan
skalvinnathan force-pushed the fix/code-review-2026-11 branch from c38ccf2 to 77bc5aa Compare November 15, 2026 17:23
@skalvinnathan
skalvinnathan merged commit 4f047ec into main Nov 15, 2026
2 checks passed
@skalvinnathan
skalvinnathan deleted the fix/code-review-2026-11 branch November 15, 2026 17:26
skalvinnathan added a commit that referenced this pull request Nov 15, 2026
Code review fixes: bind all claims into the KEK, fail-closed attestation, demo layer scan, symlink escape
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant