Bug 2035731

Summary: Failed to restore vm after saving a booting vm with vtpm device
Product: Red Hat Enterprise Linux 9 Reporter: Fangge Jin <fjin>
Component: libtpmsAssignee: Marc-Andre Lureau <marcandre.lureau>
Status: CLOSED ERRATA QA Contact: Qinghua Cheng <qcheng>
Severity: medium Docs Contact:
Priority: medium    
Version: 9.0CC: coli, jinzhao, marcandre.lureau, qcheng, stefanb, virt-maint, xiaohli, yanqzhan, yfu
Target Milestone: rcKeywords: AutomationTriaged, Triaged
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: libtpms-0.9.1-2.20211126git1ff6fe1f43.el9 Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of:
: 2111433 (view as bug list) Environment:
Last Closed: 2022-11-15 10:26:42 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On:    
Bug Blocks: 2111433    

Description Fangge Jin 2021-12-27 09:48:14 UTC
Description of problem:
Start a vm with vtpm device, then save the vm immediately. Restoring vm will fail.

Version-Release number of selected component (if applicable):
# rpm -q libvirt qemu-kvm libtpms swtpm
libvirt-7.10.0-1.el9.x86_64
qemu-kvm-6.2.0-1.el9.x86_64
libtpms-0.9.1-0.20211126git1ff6fe1f43.el9.x86_64
swtpm-0.7.0-1.20211109gitb79fd91.el9.x86_64


How reproducible:
100%

Steps to Reproduce:
1.Start a vm with vtpm device, and save it immediately
# virsh start vm1; virsh managedsave vm1
    <tpm model='tpm-crb'>
      <backend type='emulator' version='2.0'/>
    </tpm>

2. Restore the vm:
# virsh start vm1
error: Failed to start domain 'vm1'
error: internal error: qemu unexpectedly closed the monitor: 2021-12-27T08:52:14.261131Z qemu-kvm: tpm-emulator: Setting the stateblob (type 2) failed with a TPM error 0x3 a parameter is bad
2021-12-27T08:52:14.261145Z qemu-kvm: error while loading state for instance 0x0 of device 'tpm-emulator'
2021-12-27T08:52:14.261235Z qemu-kvm: load of migration failed: Input/output error


3. Remove state file, try to restore vm again:
# rm /var/lib/libvirt/swtpm/505ee98d-e9af-4597-9eff-168e66f5f6ce/tpm2/tpm2-00.permall
rm: remove regular file '/var/lib/libvirt/swtpm/505ee98d-e9af-4597-9eff-168e66f5f6ce/tpm2/tpm2-00.permall'? y

# virsh start vm1
error: Failed to start domain 'vm1'
error: internal error: qemu unexpectedly closed the monitor: 2021-12-27T09:05:28.928729Z qemu-kvm: tpm-emulator: Setting the stateblob (type 2) failed with a TPM error 0x3 a parameter is bad
2021-12-27T09:05:28.928743Z qemu-kvm: error while loading state for instance 0x0 of device 'tpm-emulator'
2021-12-27T09:05:28.928832Z qemu-kvm: load of migration failed: Input/output error


Actual results:
Failed to restore vm


Expected results:
Restore vm successfully


Additional info:
1. Can't reproduce if save vm after vm is fully booted up.
2. swtpm log:
# cat /var/log/swtpm/libvirt/qemu/vm1-swtpm.log

libtpms/tpm2: STATE_RESET_DATA: s_ContextSlotMask has bad value: 0x0000
Data client disconnected
libtpms/tpm2: STATE_RESET_DATA: s_ContextSlotMask has bad value: 0x0000
Data client disconnected

Comment 1 Qinghua Cheng 2022-01-04 10:21:03 UTC
We don't reproduce it with Win11 and Win2022 guests

Environment:
libtpms-0.9.1-0.20211126git1ff6fe1f43.el9.x86_64
swtpm-0.7.0-1.20211109gitb79fd91.el9.x86_64
qemu-kvm-6.2.0-1.el9.x86_64
libvirt-7.10.0-1.el9.x86_64
edk2-ovmf-20210527gite1999b264f1f-7.el9.noarch

Comment 2 Stefan Berger 2022-01-04 20:10:32 UTC
The fix for this is in a PR here now: https://github.com/stefanberger/libtpms/pull/287

Comment 5 Yanan Fu 2022-01-14 03:28:09 UTC
Hi there,

We i test migration with Win11 + tpm device,  if src and dst vm use same tpm daemon by mistake, can hit the same error info:

18:23:27 INFO | [qemu output] qemu-kvm: tpm-emulator: Setting the stateblob (type 2) failed with a TPM error 0x84
18:23:27 INFO | [qemu output] qemu-kvm: error while loading state for instance 0x0 of device 'tpm-emulator'
18:23:27 INFO | [qemu output] qemu-kvm: load of migration failed: Input/output error
18:23:27 INFO | [qemu output] (Process terminated with status 1)


Just for a reference, thanks!

Comment 6 Stefan Berger 2022-01-14 15:06:37 UTC
(In reply to Yanan Fu from comment #5)
> Hi there,
> 
> We i test migration with Win11 + tpm device,  if src and dst vm use same tpm
> daemon by mistake, can hit the same error info:

What is 'same tpm daemon by mistake'? 

> 
> 18:23:27 INFO | [qemu output] qemu-kvm: tpm-emulator: Setting the stateblob
> (type 2) failed with a TPM error 0x84
> 18:23:27 INFO | [qemu output] qemu-kvm: error while loading state for
> instance 0x0 of device 'tpm-emulator'
> 18:23:27 INFO | [qemu output] qemu-kvm: load of migration failed:
> Input/output error
> 18:23:27 INFO | [qemu output] (Process terminated with status 1)
> 
> 
> Just for a reference, thanks!

Is this with the fix applied to libptms on both machines or without?

Comment 7 Yanan Fu 2022-01-14 15:35:03 UTC
(In reply to Stefan Berger from comment #6)
> (In reply to Yanan Fu from comment #5)
> > Hi there,
> > 
> > We i test migration with Win11 + tpm device,  if src and dst vm use same tpm
> > daemon by mistake, can hit the same error info:
> 
> What is 'same tpm daemon by mistake'? 


It is for local migration test:
1. src vm, qemu cli:

    -chardev socket,id=char_vtpm_tpm0,path=/tmp/avocado_5l5j2ys4/avocado-vt-vm1_tpm0_swtpm.sock \    <--- here
    -tpmdev emulator,chardev=char_vtpm_tpm0,id=emulator_vtpm_tpm0 \
    -device tpm-crb,id=tpm-crb_vtpm_tpm0,tpmdev=emulator_vtpm_tpm0 \


2. dst vm qemu cli:
    -chardev socket,id=char_vtpm_tpm0,path=/tmp/avocado_5l5j2ys4/avocado-vt-vm1_tpm0_swtpm.sock \    <--- here
    -tpmdev emulator,chardev=char_vtpm_tpm0,id=emulator_vtpm_tpm0 \
    -device tpm-crb,id=tpm-crb_vtpm_tpm0,tpmdev=emulator_vtpm_tpm0 \

I hit the same error when use same 'path' for both src and dst vm.
This is incorrect usage,  so i say 'by mistake'.

The correct usage is using unique path for each one, in this way, it works normally.

> 
> > 
> > 18:23:27 INFO | [qemu output] qemu-kvm: tpm-emulator: Setting the stateblob
> > (type 2) failed with a TPM error 0x84
> > 18:23:27 INFO | [qemu output] qemu-kvm: error while loading state for
> > instance 0x0 of device 'tpm-emulator'
> > 18:23:27 INFO | [qemu output] qemu-kvm: load of migration failed:
> > Input/output error
> > 18:23:27 INFO | [qemu output] (Process terminated with status 1)
> > 
> > 
> > Just for a reference, thanks!
> 
> Is this with the fix applied to libptms on both machines or without?

Without any fix.

Comment 8 Stefan Berger 2022-01-14 20:14:21 UTC
Anyway, thanks for finding the bug!

Comment 13 Yanan Fu 2022-06-24 02:48:02 UTC
QE bot(pre verify): Set 'Verified:Tested,SanityOnly' as gating/tier1 test pass.

Comment 14 Qinghua Cheng 2022-06-28 03:20:26 UTC
Verified for on rhel 9.1

kernel: 5.14.0-114.el9.x86_64
qemu: qemu-kvm-7.0.0-6.el9.x86_64
libtpms-0.9.1-2.20211126git1ff6fe1f43.el9.x86_64
swtpm-0.7.0-3.20211109gitb79fd91.el9.x86_64
edk2-ovmf-20220526git16779ede2d36-1.el9.noarch

guest: win11

Looped the actions 

virsh start <vm> (save it immediately)
virsh managedsave <vm>
virsh start <vm>

40 times, guest vm works normally. 

Set bug status to verified.

Comment 16 errata-xmlrpc 2022-11-15 10:26:42 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory (libtpms bug fix and enhancement update), and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHBA-2022:8141