Source: backuppc-rsync
Version: 3.1.3.0-5
Severity: important
Tags: patch upstream fixed-upstream
Forwarded: https://github.com/backuppc/rsync-bpc/issues/55
Control: found -1 3.1.3.0-3
Control: found -1 3.1.3.0-4

Dear Maintainer,

all backuppc-rsync versions in Debian (3.1.3.0-3 in bookworm, -4 in
trixie, -5 in forky/sid) contain a double free in
bpc_poolWrite_repeatPoolWrite() that breaks parallel backups into the
same pool. I reported it upstream with a patch, and the maintainer merged
it on 2026-11-18:

  https://github.com/backuppc/rsync-bpc/issues/55
  https://github.com/backuppc/rsync-bpc/pull/56  (merge commit 41b5ba3)

There is no upstream release with the fix yet (3.1.3.2 does not contain
it). The attached patch is the upstream commit; it applies to 3.1.3.0
with an offset of -17 lines.

What happens
------------

Since 3.1.3.0, bpc_poolWrite_repeatPoolWrite() calls
bpc_poolWrite_cleanup(), which returns info->tmpFileName to the
bpc_strBuf free list, and then keeps using it; bpc_poolWrite_close()
frees it a second time. The free list then hands the same buffer out
twice, so the temp file name gets overwritten with a pool path or a pool
directory. 3.1.2.2 is not affected (it used a fixed char array).

The retry path runs when two transfers add a file with the same digest at the same moment, e.g. parallel first full backups of hosts with the same OS. With the Debian default MaxBackups = 4 parallel backups are normal.
This is how I ran into it: the first backups of two new hosts with the
same OS (Ubuntu 26.04), running in parallel into an empty pool, ended
with "can't open/create <pool path> for writing" errors.

Symptoms with 3.1.3.0 (the Debian version): one rsync_bpc receiver loops
forever at 100% CPU in bpc_poolWrite_updateMatches (it reads from a
directory fd and adds -1 per read), so that backup hangs; the other
transfer finishes with missing files and errors. With upstream master
before the fix the hang is gone, but the same race gives "can't
open/create <pool path> for writing" errors and in one run unbounded
memory growth of the receiver (about 42 GB, ended by the OOM killer).

Reproduction
------------

BackupPC 4.4.0 with an empty pool, two hosts backing up the same tree of
about 30,500 files via rsync, BackupPC_dump -f for both at the same time:

  3.1.2.2           clean
  3.1.3.0           one receiver hangs at 100% CPU, the other has errors
  3.1.3.1           code 12 on one host, 27 xferErrs on the other
  3.1.3.1 + patch   clean in 4 of 4 runs (about 3,800 retries per host)

Details are in the upstream issue.

Stable
------

Since the patch is small, self-contained and merged upstream, it would be great to have it in trixie (and bookworm, if you consider it worth it) via a stable update as well.

Thanks for maintaining BackupPC in Debian!

Best Regards
 Arnd
From 864670281b9eca61b0eff543796dc73c34242212 Mon Sep 17 00:00:00 2001
From: Arnd Hannemann <[email protected]>
Date: Wed, 18 Nov 2026 08:46:55 +0100
Subject: [PATCH] bpc_poolWrite_repeatPoolWrite: fix double free of tmpFileName

bpc_poolWrite_repeatPoolWrite() calls bpc_poolWrite_cleanup(), which
returns info->tmpFileName to the bpc_strBuf free list, and then keeps
using it; bpc_poolWrite_close() frees it a second time. The free list
then hands the same buffer out twice, so the temp file name gets
overwritten with a pool path or directory. fileNameTmp itself points into
a freed strBuf (info->tmpFileName, or info->match[i].fileName for a v3
match).

Copy the name before cleanup and give info a fresh tmpFileName.

With 3.1.3.0 this showed up as a receiver looping forever in
bpc_poolWrite_updateMatches; on master as "can't open/create <pool path>
for writing" errors, when two transfers add the same new file at the
same time. Fixes #55.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
---
 backuppc/bpc_poolWrite.c | 19 ++++++++++++++++++-
 1 file changed, 18 insertions(+), 1 deletion(-)

diff --git a/backuppc/bpc_poolWrite.c b/backuppc/bpc_poolWrite.c
index 6c7e668..7f19645 100644
--- a/backuppc/bpc_poolWrite.c
+++ b/backuppc/bpc_poolWrite.c
@@ -674,26 +674,43 @@ void bpc_poolWrite_close(bpc_poolWrite_info *info, int *match, bpc_digest *diges
     *errorCnt     = info->errorCnt;
 }
 
-void bpc_poolWrite_repeatPoolWrite(bpc_poolWrite_info *info, char *fileNameTmp)
+void bpc_poolWrite_repeatPoolWrite(bpc_poolWrite_info *info, char *fileNameArg)
 {
+    /*
+     * fileNameArg points into a bpc_strBuf (info->tmpFileName or a match
+     * fileName) that bpc_poolWrite_cleanup() returns to the free list, and
+     * cleanup frees info->tmpFileName, which bpc_poolWrite_close() would free
+     * a second time.  Copy the name first and give info a new tmpFileName.
+     */
+    char *fileNameTmp = strdup(fileNameArg);
+
+    if ( !fileNameTmp ) {
+        bpc_logErrf("bpc_poolWrite_repeatPoolWrite: can't allocate file name\n");
+        info->errorCnt++;
+        return;
+    }
     bpc_poolWrite_cleanup(info);
+    info->tmpFileName = bpc_strBuf_new();
 
     if ( BPC_LogLevel >= 5 ) bpc_logMsgf("bpc_poolWrite_repeatPoolWrite: rewriting %s\n", fileNameTmp);
     if ( info->retryCnt++ > 8 ) {
         bpc_logErrf("bpc_poolWrite_repeatPoolWrite: giving up on %s after %d attempts\n", fileNameTmp, info->retryCnt);
         info->errorCnt++;
         unlink(fileNameTmp);
+        free(fileNameTmp);
         return;
     }
     bpc_strBuf_strcpy(info->tmpFileName, 0, fileNameTmp);
     if ( bpc_fileZIO_open(&info->fd, fileNameTmp, 0, info->compress) < 0 ) {
         bpc_logErrf("bpc_poolWrite_repeatPoolWrite: can't open %s for reading", fileNameTmp);
         info->errorCnt++;
+        free(fileNameTmp);
         return;
     }
     info->eof    = 1;
     info->state  = 2;
     info->fdOpen = 1;
+    free(fileNameTmp);
     bpc_poolWrite_write(info, NULL, 0);
 }
 
-- 
2.53.0

Reply via email to