diff mbox series

[7/8] tpm_emulator: Fix memory leak of vmstate_tpm_emulator

Message ID 20201226103347.868-8-gaojinhao@huawei.com (mailing list archive)
State New, archived
Headers show
Series Fix memory leak of some device state in migration | expand

Commit Message

gaojinhao Dec. 26, 2020, 10:33 a.m. UTC
From: Jinhao Gao <gaojinhao@huawei.com>

When VM migrate VMState of tpm-emulator, the fields(state_blobs.
permanent.buffer, state_blobs.volatil.buffer and state_blobs.savestate.
buffer) of tpm-emulator having a flag of VMS_ALLOC need to allocate
memory. If the dst doesn't free memory which has been allocated for
SaveStateEntry of tpm-emulator before dst loads device state, it may
result that the pointers of state_blobs.permanent.buffer, state_blobs.
volatil.buffer and state_blobs.savestate.buffer are overlaid when vm
loads. We add the pre_load func to free memory, which prevents memory
leak.

Signed-off-by: Jinhao Gao <gaojinhao@huawei.com>
---
 backends/tpm/tpm_emulator.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)
diff mbox series

Patch

diff --git a/backends/tpm/tpm_emulator.c b/backends/tpm/tpm_emulator.c
index a012adc193..7ffa95dbce 100644
--- a/backends/tpm/tpm_emulator.c
+++ b/backends/tpm/tpm_emulator.c
@@ -857,6 +857,18 @@  static int tpm_emulator_pre_save(void *opaque)
     return tpm_emulator_get_state_blobs(tpm_emu);
 }
 
+static int tpm_emulator_pre_load(void *opaque)
+{
+    TPMBackend *tb = opaque;
+    TPMEmulator *tpm_emu = TPM_EMULATOR(tb);
+    TPMBlobBuffers *state_blobs = &tpm_emu->state_blobs;
+
+    tpm_sized_buffer_reset(&state_blobs->volatil);
+    tpm_sized_buffer_reset(&state_blobs->permanent);
+    tpm_sized_buffer_reset(&state_blobs->savestate);
+    return 0;
+}
+
 /*
  * Load the TPM state blobs into the TPM.
  *
@@ -883,6 +895,7 @@  static const VMStateDescription vmstate_tpm_emulator = {
     .name = "tpm-emulator",
     .version_id = 0,
     .pre_save = tpm_emulator_pre_save,
+    .pre_load = tpm_emulator_pre_load,
     .post_load = tpm_emulator_post_load,
     .fields = (VMStateField[]) {
         VMSTATE_UINT32(state_blobs.permanent_flags, TPMEmulator),