From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <f.ebner@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id E560172F99
 for <pve-devel@lists.proxmox.com>; Wed, 25 May 2022 14:00:13 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id DAD072AABC
 for <pve-devel@lists.proxmox.com>; Wed, 25 May 2022 13:59:43 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [94.136.29.106])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256)
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id F173E2AAA6
 for <pve-devel@lists.proxmox.com>; Wed, 25 May 2022 13:59:42 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id BF5C043854
 for <pve-devel@lists.proxmox.com>; Wed, 25 May 2022 13:59:42 +0200 (CEST)
From: Fabian Ebner <f.ebner@proxmox.com>
To: pve-devel@lists.proxmox.com
Date: Wed, 25 May 2022 13:59:38 +0200
Message-Id: <20220525115939.347193-2-f.ebner@proxmox.com>
X-Mailer: git-send-email 2.30.2
In-Reply-To: <20220525115939.347193-1-f.ebner@proxmox.com>
References: <20220525115939.347193-1-f.ebner@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.067 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 T_SCC_BODY_TEXT_LINE    -0.01 -
Subject: [pve-devel] [RFC/PATCH v2 qemu 2/3] PVE-Backup: ensure jobs in
 di_list are referenced
X-BeenThere: pve-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox VE development discussion <pve-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pve-devel/>
List-Post: <mailto:pve-devel@lists.proxmox.com>
List-Help: <mailto:pve-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel>, 
 <mailto:pve-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Wed, 25 May 2022 12:00:13 -0000

Ensures that qmp_backup_cancel doesn't pick a job that's already been
freed. With unlucky timings it seems possible that:
1. job_exit -> job_completed -> job_finalize_single starts
2. pvebackup_co_complete_stream gets spawned in completion callback
3. job finalize_single finishes -> job's refcount hits zero -> job is
   freed
4. qmp_backup_cancel comes in and locks backup_state.backup_mutex
   before pvebackup_co_complete_stream can remove the job from the
   di_list
5. qmp_backup_cancel will pick a job that's already been freed

Signed-off-by: Fabian Ebner <f.ebner@proxmox.com>
---

New in v2.

 pve-backup.c | 13 +++++++++++++
 1 file changed, 13 insertions(+)

diff --git a/pve-backup.c b/pve-backup.c
index dfaf4c93f8..3cede98b1d 100644
--- a/pve-backup.c
+++ b/pve-backup.c
@@ -314,6 +314,11 @@ static void coroutine_fn pvebackup_co_complete_stream(void *opaque)
         }
     }
 
+    if (di->job) {
+        job_unref(&di->job->job);
+        di->job = NULL;
+    }
+
     // remove self from job list
     backup_state.di_list = g_list_remove(backup_state.di_list, di);
 
@@ -497,6 +502,9 @@ static void create_backup_jobs_bh(void *opaque) {
         aio_context_release(aio_context);
 
         di->job = job;
+        if (job) {
+            job_ref(&job->job);
+        }
 
         if (!job || local_err) {
             error_setg(errp, "backup_job_create failed: %s",
@@ -531,6 +539,11 @@ static void create_backup_jobs_bh(void *opaque) {
                 aio_context_release(ctx);
                 canceled = true;
             }
+
+            if (di->job) {
+                job_unref(&di->job->job);
+                di->job = NULL;
+            }
         }
     }
 
-- 
2.30.2