Commit 29daa8a45e for qemu.org

commit 29daa8a45eb78caa237ccce6d1cea2bae8278aa6
Author: Peter Maydell <peter.maydell@linaro.org>
Date:   Fri Nov 27 14:44:39 2026 +0000

    hw/block/pflash_cfi02: register VM change state handler once

    In the pflash_cfi02 post-load function we register a VM change state
    handler callback, which is a one-shot (it deletes itself when run).

    However, loading twice before the VM resumes means the post_load hook
    adds the handler again, overwriting the old pfl->vmstate.  This can
    result in a use-after-free on resume because the first-registered
    callback does not delete itself (which is permitted) but the
    second-registered callback (which is not).

    Follow the pattern used by vapic.c and only register the callback
    if it has not already been registered.

    Cc: qemu-stable@nongnu.org
    Fixes: be71ba48d5c632 ("hw/block/pflash_cfi02: Add migration support")
    Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
    Reviewed-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>
    Message-ID: <20261127144439.627586-4-peter.maydell@linaro.org>
    Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>

diff --git a/hw/block/pflash_cfi02.c b/hw/block/pflash_cfi02.c
index 8d130e14d1..28e8c27d72 100644
--- a/hw/block/pflash_cfi02.c
+++ b/hw/block/pflash_cfi02.c
@@ -1070,7 +1070,7 @@ static int pflash_post_load(void *opaque, int version_id)
 {
     PFlashCFI02 *pfl = opaque;

-    if (!pfl->ro) {
+    if (!pfl->ro && !pfl->vmstate) {
         pfl->vmstate = qemu_add_vm_change_state_handler(postload_update_cb, pfl);
     }