Skip to content

storage: keep a volume's encrypt format when the copy answer omits it - #11

Open
Kukunin wants to merge 2 commits into
4.22-testingfrom
fix-encrypt-format-cleared-on-staged-volume-copy
Open

Kukunin wants to merge 2 commits into
4.22-testingfrom
fix-encrypt-format-cleared-on-staged-volume-copy

Conversation

@Kukunin

@Kukunin Kukunin commented Nov 19, 2026 •

Copy link
Copy Markdown

storage: keep a volume's encrypt format when the copy answer omits it

Symptom

Migrating an encrypted volume between two host-scoped primary storage pools
completes without an error, but silently sets cloud.volumes.encrypt_format
to NULL. The qcow2 on disk is still LUKS-encrypted and passphrase_id is
still set, so the row now claims the volume is plaintext while its bytes are
not.

Nothing can open the volume after that. The disk is attached without an
<encryption> element in the domain XML, no secret is handed to qemu, and
the guest fails to start with:

qemu-img: Parameter 'encrypt.key-secret' is required for cipher

Confirmed on a live system.

Mechanism

  1. AncientDataMotionStrategy.canDirectlyCopyBetweenDataStoreScopes
    (engine/storage/datamotion/src/main/java/org/apache/cloudstack/storage/motion/AncientDataMotionStrategy.java:501-531)
    has cases for two scopes that are the same, for HOST↔CLUSTER and for
    HOST↔ZONE, but no case for HOST→HOST. Two distinct host-local pools
    are not the same scope, match none of the cross-scope cases and fall through
    to return false.

  2. The copy therefore takes the staged two-leg path through secondary storage
    rather than a direct primary-to-primary CopyCommand.

  3. Leg 2 is KVMStorageProcessor.copyVolumeFromImageCacheToPrimary
    (plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/storage/KVMStorageProcessor.java:623-628).
    It builds its VolumeObjectTO out of a format and a path and never calls
    setEncryptFormat, so the answer carries a null encrypt format. Contrast
    copyVolumeFromPrimaryToPrimary, which does set it
    (KVMStorageProcessor.java:3212): that is the only copy route in the KVM
    processor that reports the destination's encryption back to the management
    server.

  4. VolumeObject.updateVolumeInfo
    (engine/storage/volume/src/main/java/org/apache/cloudstack/storage/volume/VolumeObject.java:695)
    then copies that absent value over the good one, unguarded:

    volumeVo.setEncryptFormat(newVolume.getEncryptFormat());

    The size and format assignments either side of it are guarded against
    exactly this — newVolumeSize != null && setVolumeSize for the size, and
    setVolumeFormat checking newVolume.getFormat() != null for the format —
    so an answer that says nothing about a field leaves that field alone
    everywhere except for encryption.

Why upstream never hits it

Upstream refuses encrypted-volume migration outright at
VolumeApiServiceImpl.java:3515-3517:

if (vol.getPassphraseId() != null && !srcAndDestOnStorPool
        && !srcStoragePoolVO.getPoolType().equals(Storage.StoragePoolType.PowerFlex)) {
    throw new InvalidParameterValueException("Migration of encrypted volumes is unsupported");
}

An encrypted volume therefore never reaches AncientDataMotionStrategy on a
non-StorPool, non-PowerFlex pool at all, and the unguarded assignment is never
reached with a LUKS volume on the other side of it. This fork relaxes that
precondition to admit offline KVM file-based encrypted migration
(isOfflineKvmFileBasedEncryptedVolumeMigration, VolumeApiServiceImpl.java:3702),
which is what exposes the latent bug. The defect itself is upstream's and the
fix below stands on its own against upstream.

The fix

Treat an absent encrypt format on the answer as "unchanged", matching how the
size and the format beside it are already handled:

if (newVolume.getEncryptFormat() != null) {
    volumeVo.setEncryptFormat(newVolume.getEncryptFormat());
}

No copy route in the tree ever sets a VolumeObjectTO's encrypt format to
null deliberately to signal that a volume has been decrypted. The three
producers — KVMStorageProcessor.java:538, :1766 and :3212, plus
StorPool's StorPoolSetVolumeEncryptionCommandWrapper — only ever write a
non-null value, and only when the disk actually is encrypted. "Absent" has
never carried meaning on this field, so reading it as "unchanged" is correct.

Safety for the fork's decrypt-on-migration path

A migration that is meant to decrypt a volume does not rely on the answer to
clear encrypt_format, so the guard changes nothing for it.

The destination row is created by
VolumeServiceImpl.duplicateVolumeOnAnotherStorage
(engine/storage/volume/src/main/java/org/apache/cloudstack/storage/volume/VolumeServiceImpl.java:1742).
It starts from new VolumeVO(volume), and passphraseId / encryptFormat
are set inside the isEncryptedByItsDiskOffering(newVol, volume) branch. When
the destination disk offering is unencrypted that branch does not run, and the
row is persisted with encrypt_format NULL.

updateVolumeInfo acts on the destination volume — copyVolume calls
destVolume.processEvent(...) on the VolumeInfo built from that row
(VolumeServiceImpl.java:1969-1977) — so on the decrypt path there is nothing
for the assignment to clear.

Second commit: make that safety explicit

Until now the NULL on the decrypt path came from an omission rather than from
a decision. The VolumeVO copy constructor
(engine/schema/src/main/java/com/cloud/storage/VolumeVO.java:257-288) copies
most of a volume's columns but happens to carry over neither passphraseId
nor encryptFormat, and with no else branch the destination row simply
inherited that gap. The guard above was therefore safe only for as long as
that constructor stayed as it is.

Teaching the copy constructor to carry the two columns — a plausible-looking
change, and one its other callers might even want — would have been enough to
reintroduce the same class of bug from the other side: a destination row left
claiming encrypt_format = 'LUKS' over plaintext bytes, with the guard now
declining to clear it. Nothing in either file said so.

The non-encrypted branch now states the destination's encryption itself:

} else {
    newVol.setPassphraseId(null);
    newVol.setEncryptFormat(null);
}

The behaviour is unchanged today; what changes is that it is decided by the
migration code instead of inherited from a shared constructor, and that a
future change to VolumeVO(Volume) cannot silently alter it. The copy
constructor itself is deliberately left alone — it has many other callers and
widening it is a separate decision.

Follow-ups (deliberately not in this PR)

Both are real and were found while root-causing this, but each is a separate
change and neither is needed to stop the data-integrity bug:

  1. The API-level precondition does not check scope.
    isOfflineKvmFileBasedEncryptedVolumeMigration (VolumeApiServiceImpl.java:3702-3708)
    admits an encrypted migration on hypervisor, liveness and pool type alone.
    It never asks whether the two pools' scopes permit a direct
    primary-to-primary copy, so a HOST→HOST pair passes the precondition at
    the API and only runs into trouble much further down. The precondition
    should refuse what the data motion layer cannot carry out.

  2. copyVolumeFromPrimaryToSecondary discards the copyPhysicalDisk return
    value.
    At KVMStorageProcessor.java:672 the call is made for its side
    effect and the answer is then built from the names the method guessed
    beforehand, rather than from the KVMPhysicalDisk that was actually
    written. copyVolumeFromImageCacheToPrimary does it correctly at
    KVMStorageProcessor.java:619 (final KVMPhysicalDisk newDisk = ...).

Testing

VolumeObjectTest already covers updateVolumeInfo's guarded size handling.
Two cases were added alongside those, in the same style:

  • validateUpdateVolumeInfoEncryptFormatNullDoNotSetEncryptFormat — a null
    encrypt format on the answer leaves the stored value untouched.
  • validateUpdateVolumeInfoEncryptFormatNotNullSetEncryptFormat — a non-null
    one is still written through.

The first fails on the unfixed code and passes with the fix.

The second commit needs no new test. VolumeServiceTest already pins the
invariant it makes explicit —
duplicateVolumeOnAnotherStorageLeavesADecryptedCopyWithoutAPassphrase asserts
a null passphrase and a null encrypt format on the destination of a decrypting
migration, and duplicateVolumeOnAnotherStorageFallsBackToTheSourceWhenTheDiskOfferingIsGone
asserts the same for a plaintext source whose offering has been removed. Those
assertions are exactly what would start failing if VolumeVO(Volume) ever grew
the two fields without the explicit else, so the guard against that
regression is already in place; a further test would only duplicate it.

Full module run on both commits:
Tests run: 64, Failures: 0, Errors: 0, Skipped: 0 (VolumeObjectTest 37,
VolumeServiceTest 24).

🤖 Generated with Claude Code

Kukunin and others added 2 commits November 19, 2026 12:46
A CopyCmdAnswer that says nothing about encryption was taken to mean the
volume is no longer encrypted. updateVolumeInfo wrote the answer's
encryptFormat over the stored one unconditionally, while the size and the
format assigned next to it are only written when the answer actually
carries a value.

Only copyVolumeFromPrimaryToPrimary sets it. The two-leg copy through
secondary storage ends in copyVolumeFromImageCacheToPrimary, which builds
its answer out of a path and a format alone, so a volume that took the
staged path came back with volumes.encrypt_format NULL while its qcow2 on
disk still carried a LUKS header and the volume still held its passphrase.
Nothing can open it after that: qemu-img asks for encrypt.key-secret and
libvirt never supplies one, because no encryption element is written into
the domain XML of a volume that claims to be plaintext, and the guest will
not start.

An absent encrypt format now leaves the stored one alone. The destination
row of a migration that is meant to decrypt is built by
duplicateVolumeOnAnotherStorage with no encrypt format to begin with - the
VolumeVO copy constructor carries over neither the passphrase nor the
format - so that path has nothing to clear and is unaffected.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
duplicateVolumeOnAnotherStorage builds the destination row from the
VolumeVO copy constructor, which carries over neither passphraseId nor
encryptFormat, and sets both only when the destination disk offering
encrypts. A copy that a migration is decrypting therefore ended up with
a NULL encrypt format by omission rather than by intent.

That omission is also what makes the guard in updateVolumeInfo safe. An
answer carrying no encrypt format now leaves the stored value alone, and
on the decrypt path there is no stored value to leave. Were the copy
constructor ever taught to carry the two columns across - it already
copies most of the others - the destination row would start out claiming
LUKS over plaintext bytes and nothing would clear it again, which is the
same defect mirrored, with nothing in either file to warn whoever made
the change.

Set both fields to null explicitly when the offering does not encrypt,
so the row's encryption is stated by the migration code rather than
inherited from whichever columns a shared constructor happens to skip.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant