Commit 4c3e4cd885 for qemu.org

commit 4c3e4cd88527f865718b2e74df619728f72e675b
Author: Peter Maydell <peter.maydell@linaro.org>
Date:   Fri Nov 27 14:44:38 2026 +0000

    hw/nvram/spapr_nvram: register VM change state handler once

    In the spapr_nvram 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 nvram->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: cf472f48d57a533c ("spapr: fix spapr-nvram migration")
    Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
    Reviewed-by: Amit Machhiwal <amachhiw@linux.ibm.com>
    Message-ID: <20261127144439.627586-3-peter.maydell@linaro.org>
    Signed-off-by: Philippe Mathieu-Daudé <philmd@oss.qualcomm.com>

diff --git a/hw/nvram/spapr_nvram.c b/hw/nvram/spapr_nvram.c
index b0f9fe8633..0b85412e3e 100644
--- a/hw/nvram/spapr_nvram.c
+++ b/hw/nvram/spapr_nvram.c
@@ -233,7 +233,7 @@ static int spapr_nvram_post_load(void *opaque, int version_id)
 {
     SpaprNvram *nvram = VIO_SPAPR_NVRAM(opaque);

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