From 70ac86aceb5ecd64475976cb749b029215579147 Mon Sep 17 00:00:00 2001 From: Blagovest Petrov Date: Wed, 4 Nov 2026 00:33:12 +0200 Subject: [PATCH 1/2] fix(agent): bind protectors with the key enrolled by a rotation A recovery-key rotation wipes the old Bor recovery slots once the new key is confirmed. When the credential for the sync came from a server release (the active recovery key, handed out for a pending rotation), that credential stopped opening the volume the moment the rotation finished, and the Tang bind right after it failed with "Error adding new binding". A reveal with rotate-after-reveal on a Tang-bound node hit this on every sync. RotateRecoveryKey now returns a copy of the key it enrolled and the sync continues with that credential for TPM2, Tang and bootstrap retirement. --- agent/cmd/agent/luks_cmd.go | 3 ++- agent/internal/luks/rotate.go | 29 ++++++++++++++++------------- agent/internal/luks/sync.go | 14 +++++++++++--- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/agent/cmd/agent/luks_cmd.go b/agent/cmd/agent/luks_cmd.go index 2c38abc8..0d765a56 100644 --- a/agent/cmd/agent/luks_cmd.go +++ b/agent/cmd/agent/luks_cmd.go @@ -186,8 +186,9 @@ func luksAdopt(ctx context.Context, cfg *config.Config, lcfg *luks.Config, devic continue } cred := &luks.Credential{Key: append([]byte(nil), passphrase...), Source: "interactive adoption"} - err := luks.RotateRecoveryKey(ctx, lcfg, client, vi.Volume, vi.Header, cred, + newKey, err := luks.RotateRecoveryKey(ctx, lcfg, client, vi.Volume, vi.Header, cred, pb.RecoveryKeyReason_RECOVERY_KEY_REASON_INITIAL, "", cfg.Server.Address, state) + luks.Zero(newKey) // the daemon resolves its own credential later if err != nil { fmt.Printf("%s: adoption failed: %v\n", vi.MappingName, err) continue diff --git a/agent/internal/luks/rotate.go b/agent/internal/luks/rotate.go index 0327596c..77481955 100644 --- a/agent/internal/luks/rotate.go +++ b/agent/internal/luks/rotate.go @@ -31,15 +31,18 @@ func mustRandRead(b []byte) { // not hold, and the old slot is wiped only after the server confirmed the // new one. Progress (never keys) is persisted in the state file so a crash // resumes instead of leaking slots. rotationID links a server-assisted -// release to the rotation that consumes it. +// release to the rotation that consumes it. On success it returns a copy of +// the new key (the caller zeroes it) so later steps in the same sync can use +// it; the old Bor slots are wiped, which invalidates a key the server +// released for this rotation. A resumed rotation returns no key. func RotateRecoveryKey(ctx context.Context, cfg *Config, client EscrowClient, vol *Volume, header *Header, - cred *Credential, reason pb.RecoveryKeyReason, rotationID, serverName string, state *State) error { + cred *Credential, reason pb.RecoveryKeyReason, rotationID, serverName string, state *State) ([]byte, error) { vs := state.Volume(vol.LuksUUID) // Resume: a slot from a crashed run only needs confirm + cleanup. if vs.Step == RotationStepSlotAdded && vs.EscrowID != "" { if resumeErr := resumeRotation(ctx, cfg, client, vol, header, vs, state); resumeErr == nil { - return nil + return nil, nil } // The pending escrow expired (>24 h): the un-escrowed slot is an // orphan. Wipe it (credential-free) when the invariants allow, then @@ -50,21 +53,21 @@ func RotateRecoveryKey(ctx context.Context, cfg *Config, client EscrowClient, vo var reloadErr error header, reloadErr = DumpHeader(ctx, cfg.CryptsetupBinary(), vol.DevicePath, vol.LuksUUID) if reloadErr != nil { - return reloadErr + return nil, reloadErr } } // 1. Generate the key in memory only. key, err := GenerateRecoveryKey() if err != nil { - return err + return nil, err } defer Zero(key) // 2. Escrow BEFORE any slot exists. escrowID := newEscrowID() if escrowErr := client.EscrowRecoveryKey(ctx, vol.LuksUUID, escrowID, key, reason, rotationID); escrowErr != nil { - return fmt.Errorf("escrow recovery key: %w", escrowErr) + return nil, fmt.Errorf("escrow recovery key: %w", escrowErr) } // Old Bor recovery slots to retire once the new one is ACTIVE. @@ -77,26 +80,26 @@ func RotateRecoveryKey(ctx context.Context, cfg *Config, client EscrowClient, vo keyCopy := append([]byte(nil), key...) slot, err := addSlotWithCredential(ctx, cfg, vol.DevicePath, cred, keyCopy) if err != nil { - return err + return nil, err } if tokenErr := ImportRecoveryToken(ctx, cfg, vol.DevicePath, slot); tokenErr != nil { - return tokenErr + return nil, tokenErr } // 4. Verify the slot really opens with the escrowed key. verifyCopy := append([]byte(nil), key...) if verifyErr := TestKeySlot(ctx, cfg, vol.DevicePath, slot, verifyCopy); verifyErr != nil { - return fmt.Errorf("verify new recovery slot: %w", verifyErr) + return nil, fmt.Errorf("verify new recovery slot: %w", verifyErr) } // Recompute the fingerprint from the fresh header. newHeader, err := DumpHeader(ctx, cfg.CryptsetupBinary(), vol.DevicePath, vol.LuksUUID) if err != nil { - return err + return nil, err } newSlot := newHeader.FindSlot(slot) if newSlot == nil { - return fmt.Errorf("luks: new recovery slot %d vanished from the header", slot) + return nil, fmt.Errorf("luks: new recovery slot %d vanished from the header", slot) } vs.Step = RotationStepSlotAdded @@ -112,7 +115,7 @@ func RotateRecoveryKey(ctx context.Context, cfg *Config, client EscrowClient, vo // 5. Confirm: the server promotes PENDING -> ACTIVE. if err := client.ConfirmRecoveryKey(ctx, vol.LuksUUID, escrowID, slot, newSlot.Fingerprint); err != nil { - return fmt.Errorf("confirm recovery key: %w", err) + return nil, fmt.Errorf("confirm recovery key: %w", err) } vs.Step = RotationStepConfirmed _ = state.Save(cfg) @@ -122,7 +125,7 @@ func RotateRecoveryKey(ctx context.Context, cfg *Config, client EscrowClient, vo // protector. finishRotation(ctx, cfg, vol, newHeader, vs, key, serverName) _ = state.Save(cfg) - return nil + return append([]byte(nil), key...), nil } // addSlotWithCredential dispatches on the credential type. diff --git a/agent/internal/luks/sync.go b/agent/internal/luks/sync.go index c1e10657..0c4d16ae 100644 --- a/agent/internal/luks/sync.go +++ b/agent/internal/luks/sync.go @@ -253,12 +253,13 @@ func enforceVolume(ctx context.Context, cfg *Config, client EscrowClient, pol *p if cred.Key != nil { credCopy.Key = append([]byte(nil), cred.Key...) } - if err := RotateRecoveryKey(ctx, cfg, client, vi.Volume, header, credCopy, reason, rotationID, serverName, state); err != nil { - log.Printf("luks: recovery rotation on %s: %v", vi.LuksUUID, err) + newKey, rotErr := RotateRecoveryKey(ctx, cfg, client, vi.Volume, header, credCopy, reason, rotationID, serverName, state) + if rotErr != nil { + log.Printf("luks: recovery rotation on %s: %v", vi.LuksUUID, rotErr) items = append(items, Item{ SchemaID: "luks:recovery", Key: vi.LuksUUID, Status: pb.ComplianceStatus_COMPLIANCE_STATUS_ERROR, - Message: "recovery key escrow failed: " + err.Error(), + Message: "recovery key escrow failed: " + rotErr.Error(), }) } else { items = append(items, Item{ @@ -266,6 +267,13 @@ func enforceVolume(ctx context.Context, cfg *Config, client EscrowClient, pol *p Status: pb.ComplianceStatus_COMPLIANCE_STATUS_COMPLIANT, Message: "recovery key escrowed", }) + // The rotation wiped the old Bor recovery slots, so a key the + // server released for it no longer opens the volume. The + // protector steps below continue with the key just enrolled. + if newKey != nil { + defer Zero(newKey) + cred = &Credential{Key: newKey, Source: "recovery key enrolled in this sync"} + } // The header changed; re-read it for the following steps. if fresh, err := DumpHeader(ctx, cfg.CryptsetupBinary(), vi.DevicePath, vi.LuksUUID); err == nil { header = fresh From eecb500768784ddbcce35467a0eaa5fb167ac25d Mon Sep 17 00:00:00 2001 From: Blagovest Petrov Date: Wed, 4 Nov 2026 00:33:12 +0200 Subject: [PATCH 2/2] fix(agent): treat zram swap as compliant in the encryption check zram swap lives in compressed RAM and never reaches a disk, yet the system-scope encryption check reported it as an unencrypted swap device, which made every default Fedora Workstation install non-compliant. Flag RAM-backed swap during discovery and report it as compliant. --- agent/internal/luks/compliance.go | 6 ++++++ agent/internal/luks/discover.go | 9 +++++++++ agent/internal/luks/discover_test.go | 13 +++++++++++-- 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/agent/internal/luks/compliance.go b/agent/internal/luks/compliance.go index e5552147..085a689e 100644 --- a/agent/internal/luks/compliance.go +++ b/agent/internal/luks/compliance.go @@ -83,6 +83,12 @@ func CheckStatic(inv *Inventory, pol *pb.DiskEncryptionPolicy) []Item { if pol.GetRequireEncryption() { for _, m := range ScopeMounts(inv.Mounts, pol.GetVolumeScope(), pol.GetMountpoints()) { switch { + case m.InMemory: + items = append(items, Item{ + SchemaID: "luks:encryption", Key: m.Mountpoint, + Status: pb.ComplianceStatus_COMPLIANCE_STATUS_COMPLIANT, + Message: fmt.Sprintf("%s is RAM-backed swap; nothing is written to disk", m.Source), + }) case m.Volume == nil: items = append(items, Item{ SchemaID: "luks:encryption", Key: m.Mountpoint, diff --git a/agent/internal/luks/discover.go b/agent/internal/luks/discover.go index 138e08f1..1bfe85cd 100644 --- a/agent/internal/luks/discover.go +++ b/agent/internal/luks/discover.go @@ -40,6 +40,9 @@ type Mount struct { MajorMinor string // Volume is the backing dm-crypt volume, nil when unencrypted. Volume *Volume + // InMemory: RAM-backed swap (zram). Nothing is written to a disk, so + // there is nothing to encrypt. + InMemory bool } // systemMountpoints are the mounts the SYSTEM volume scope covers. @@ -112,6 +115,12 @@ func Discover() (volumes []*Volume, mounts []*Mount, err error) { continue } mount := &Mount{Mountpoint: "[swap]", Source: dev, Fstype: "swap"} + if strings.HasPrefix(name, "zram") { + // Compressed RAM (the Fedora default swap): no disk behind it. + mount.InMemory = true + mounts = append(mounts, mount) + continue + } if crypt := findCryptBacking(rooted("sys/class/block/"+name), 0); crypt != nil { vol := addVolume(crypt) vol.Mountpoints = append(vol.Mountpoints, "[swap]") diff --git a/agent/internal/luks/discover_test.go b/agent/internal/luks/discover_test.go index 8c5a7cc1..606550e0 100644 --- a/agent/internal/luks/discover_test.go +++ b/agent/internal/luks/discover_test.go @@ -45,7 +45,9 @@ func buildFakeTree(t *testing.T) string { 4 0 0:99 / /proc rw - proc proc rw 5 0 0:36 /var /var rw - btrfs /dev/mapper/luks-cccc0000 rw,subvol=/var `) - write("proc/swaps", "Filename\tType\tSize\tUsed\tPriority\n/dev/dm-3 partition 8388604 0 -2\n") + write("proc/swaps", "Filename\tType\tSize\tUsed\tPriority\n/dev/dm-3 partition 8388604 0 -2\n/dev/zram0 partition 8388604 0 100\n") + // zram swap: RAM-backed, never reaches a disk. + write("dev/zram0", "") // dm-0: LUKS2 crypt under the LVM root. write("sys/class/block/dm-0/dm/uuid", "CRYPT-LUKS2-3f2a9c1e111122223333444455556666-luks-3f2a9c1e\n") @@ -161,11 +163,14 @@ func TestDiscover(t *testing.T) { } // /boot is unencrypted and must appear as a mount without a volume. - var boot *Mount + var boot, zram *Mount for _, m := range mounts { if m.Mountpoint == "/boot" { boot = m } + if m.Source == "/dev/zram0" { + zram = m + } if m.Mountpoint == "/proc" { t.Error("virtual mounts must be filtered out") } @@ -173,6 +178,10 @@ func TestDiscover(t *testing.T) { if boot == nil || boot.Volume != nil { t.Errorf("/boot mount = %+v, want unencrypted", boot) } + // zram swap is RAM-backed: listed, flagged in-memory, no volume. + if zram == nil || !zram.InMemory || zram.Volume != nil { + t.Errorf("zram swap mount = %+v, want in-memory without a volume", zram) + } } func TestScopeVolumes(t *testing.T) {