storage: fix memory leak with encrypted images
storage: fix memory leak with encrypted images

Jim Fehlig reported a regression found by libvirt-TCK tests:

> ~ # perl /usr/share/libvirt-tck/tests/qemu/100-disk-encryption.t
...
> ok 4 - defined persistent domain config
> # Starting inactive domain config
> libvirt error code: 1, message: internal error: unable to execute QEMU command
> 'cont': 'drive-ide0-0-1'
> (/var/cache/libvirt-tck/300-disk-encryption/demo.qcow2) is encrypted

Commit 2279d560 converted a boolean into a pointer with the intent of
transferring that pointer out of a temporary object into the caller's
data structure.  The temporary structure meant that meta->encryption
was always NULL on entry, so we could get away with blindly allocating
the pointer when the header said so.  But later, commit 8823272d
tweaked things to do backing chain detection in-place, rather than via
a temporary object; this has the net result that meta->encryption can
be non-NULL on entry.  Not only did this turn the latent behavior into
a memory leak, it is also a behavior regression: blindly allocating a
new pointer wipes out what secrets we already knew about the chain,
making it impossible to restart the domain.

Of course, no one in their right mind should be relying on qcow2
encryption - it is fundamentally flawed.  And sadly, the TCK tests
don't get run often enough, and this shows that our virstoragetest
does not exercise encrypted images at all.  Otherwise, we could
have avoided a release containing this regression.

* src/util/virstoragefile.c (virStorageFileGetMetadataInternal):
Don't nuke an already-existing encryption.

Signed-off-by: Eric Blake <a9da18479c871fe903d9590822cfe2629c6a4d11@redhat.com>

agx/libvirt,nertpinx/libvirt,eskultety/libvirt,elmarco/libvirt,nertpinx/libvirt,elmarco/libvirt,zippy2/libvirt,shugaoye/libvirt,andreabolognani/libvirt,shugaoye/libvirt,taget/libvirt,eskultety/libvirt,jardasgit/libvirt,eskultety/libvirt,jfehlig/libvirt,olafhering/libvirt,VenkatDatta/libvirt,rlaager/libvirt,rlaager/libvirt,libvirt/libvirt,taget/libvirt,jfehlig/libvirt,shugaoye/libvirt,crobinso/libvirt,nertpinx/libvirt,datto/libvirt,jardasgit/libvirt,olafhering/libvirt,crobinso/libvirt,fabianfreyer/libvirt,agx/libvirt,VenkatDatta/libvirt,jfehlig/libvirt,agx/libvirt,VenkatDatta/libvirt,eskultety/libvirt,nertpinx/libvirt,libvirt/libvirt,libvirt/libvirt,shugaoye/libvirt,shugaoye/libvirt,fabianfreyer/libvirt,rlaager/libvirt,crobinso/libvirt,jardasgit/libvirt,jardasgit/libvirt,fabianfreyer/libvirt,rlaager/libvirt,fabianfreyer/libvirt,VenkatDatta/libvirt,andreabolognani/libvirt,agx/libvirt,zippy2/libvirt,taget/libvirt,nertpinx/libvirt,fabianfreyer/libvirt,datto/libvirt,datto/libvirt,eskultety/libvirt,jfehlig/libvirt,andreabolognani/libvirt,olafhering/libvirt,olafhering/libvirt,rlaager/libvirt,libvirt/libvirt,elmarco/libvirt,jardasgit/libvirt,datto/libvirt,datto/libvirt,zippy2/libvirt,VenkatDatta/libvirt,taget/libvirt,crobinso/libvirt,elmarco/libvirt,agx/libvirt,andreabolognani/libvirt,zippy2/libvirt,elmarco/libvirt,andreabolognani/libvirt,taget/libvirt