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
3 changes: 2 additions & 1 deletion agent/cmd/agent/luks_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions agent/internal/luks/compliance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 9 additions & 0 deletions agent/internal/luks/discover.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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]")
Expand Down
13 changes: 11 additions & 2 deletions agent/internal/luks/discover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -161,18 +163,25 @@ 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")
}
}
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) {
Expand Down
29 changes: 16 additions & 13 deletions agent/internal/luks/rotate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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
Expand All @@ -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)
Expand All @@ -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.
Expand Down
14 changes: 11 additions & 3 deletions agent/internal/luks/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,19 +253,27 @@ 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{
SchemaID: "luks:recovery", Key: vi.LuksUUID,
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
Expand Down
Loading