Merge pull request #5003 from meta/rm-dst-artefact-race

programs: only remove the output file a failed operation created
diff --git a/programs/fileio.c b/programs/fileio.c
index 9ec8894..9160bc4 100644
--- a/programs/fileio.c
+++ b/programs/fileio.c
@@ -782,11 +782,12 @@
 /** FIO_removeFile() :
  * Unlink `path`, even if it's read-only.
  * When `expected` is not NULL, only unlink if `path` still resolves to the
- * file it describes (same device and inode, typically the source file that
- * was actually opened and processed). If a parent directory was swapped
- * between processing and removal, `path` now points at a different file :
- * refuse instead of deleting it. Pass NULL when there is no identity to
- * bind to (destination artefacts).
+ * file it describes (same device and inode) : typically the fstat() of the
+ * file actually opened, either the source file under --rm or the destination
+ * artefact of a failed operation. If a parent directory was swapped since,
+ * `path` now points at a different file : refuse instead of deleting it.
+ * Pass NULL when there is no identity to bind to (an existing destination
+ * file being overwritten).
  * note : identity is checked by UTIL_isSameFileStat(), which can't do it yet
  *        on Windows, where stat_t carries no file identity.
  * @result : 0 on success or safe refusal, nonzero on removal failure. */
@@ -2164,6 +2165,8 @@
     int result;
     int transferStat = 0;
     int dstFd = -1;
+    stat_t dstFileStat;
+    int dstFileStatValid = 0;
 
     if (ress->io.dstFile == NULL) {
         int dstFileInitialPermissions = DEFAULT_FILE_PERMISSIONS;
@@ -2180,6 +2183,9 @@
             FILE *dstFile = FIO_openDstFile(fCtx, prefs, srcFileName, dstFileName, dstFileInitialPermissions);
             if (dstFile==NULL) return 1;  /* could not open dstFileName */
             dstFd = fileno(dstFile);
+            /* identity of the file just created : on failure, remove only that one */
+            if (strcmp(dstFileName, stdoutmark))
+                dstFileStatValid = UTIL_fstat(dstFd, dstFileName, &dstFileStat);
             FIO_SyncCompressIO_setDst(&ress->io, dstFile);
         }
         /* Must only be added after FIO_openDstFile() succeeds. */
@@ -2206,8 +2212,9 @@
         }
 
         if ( (result != 0)
-          && strcmp(dstFileName, stdoutmark) ) {
-            FIO_removeFile(dstFileName, NULL);
+          && strcmp(dstFileName, stdoutmark)
+          && dstFileStatValid ) {
+            FIO_removeFile(dstFileName, &dstFileStat);
         }
     }
 
@@ -3181,6 +3188,8 @@
     int releaseDstFile = 0;
     int transferStat = 0;
     int dstFd = 0;
+    stat_t dstFileStat;
+    int dstFileStatValid = 0;
 
     if ((AIO_WritePool_getFile(ress.writeCtx) == NULL) && (prefs->testMode == 0)) {
         FILE *dstFile;
@@ -3197,6 +3206,9 @@
         dstFile = FIO_openDstFile(fCtx, prefs, srcFileName, dstFileName, dstFilePermissions);
         if (dstFile==NULL) return 1;
         dstFd = fileno(dstFile);
+        /* identity of the file just created : on failure, remove only that one */
+        if (strcmp(dstFileName, stdoutmark))
+            dstFileStatValid = UTIL_fstat(dstFd, dstFileName, &dstFileStat);
         AIO_WritePool_setFile(ress.writeCtx, dstFile);
 
         /* Must only be added after FIO_openDstFile() succeeds.
@@ -3226,8 +3238,8 @@
 
         if ( (result != 0)  /* operation failure */
           && strcmp(dstFileName, stdoutmark)  /* special case : don't remove() stdout */
-          ) {
-            FIO_removeFile(dstFileName, NULL);  /* remove decompression artefact; note: don't do anything special if remove() fails */
+          && dstFileStatValid ) {
+            FIO_removeFile(dstFileName, &dstFileStat);  /* remove decompression artefact; note: don't do anything special if remove() fails */
         }
     }
 
diff --git a/tests/cli-tests/common/rm-parent-race.c b/tests/cli-tests/common/rm-parent-race.c
new file mode 100644
index 0000000..b264e95
--- /dev/null
+++ b/tests/cli-tests/common/rm-parent-race.c
@@ -0,0 +1,58 @@
+/*
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
+ * All rights reserved.
+ *
+ * This source code is licensed under both the BSD-style license (found in the
+ * LICENSE file in the root directory of this source tree) and the GPLv2 (found
+ * in the COPYING file in the root directory of this source tree).
+ * You may select, at your option, one of the above-listed licenses.
+ */
+
+/* Swap the parent of ZSTD_TEST_RM_RACE_TARGET for a symlink right before the
+ * stream open on that file is closed, so a later removal re-resolves the
+ * pathname to a different file. The stream is matched by file identity, so
+ * it works for files opened with fopen() (source) or open()+fdopen()
+ * (destination) alike. */
+#define _GNU_SOURCE
+#include <dlfcn.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+typedef int (*fclose_fn)(FILE*);
+
+static int done = 0;
+
+static int isTarget(FILE* stream)
+{
+    const char* const target = getenv("ZSTD_TEST_RM_RACE_TARGET");
+    struct stat streamStat;
+    struct stat targetStat;
+    int const fd = fileno(stream);
+    return target != NULL && fd >= 0
+        && fstat(fd, &streamStat) == 0
+        && stat(target, &targetStat) == 0
+        && streamStat.st_dev == targetStat.st_dev
+        && streamStat.st_ino == targetStat.st_ino;
+}
+
+int fclose(FILE* stream)
+{
+    static fclose_fn realClose = NULL;
+    if (realClose == NULL)
+        realClose = (fclose_fn)dlsym(RTLD_NEXT, "fclose");
+    if (!done && isTarget(stream)) {
+        const char* const parent = getenv("ZSTD_TEST_RM_RACE_PARENT");
+        const char* const saved = getenv("ZSTD_TEST_RM_RACE_SAVED");
+        const char* const replacement = getenv("ZSTD_TEST_RM_RACE_REPLACEMENT");
+        done = 1;
+        if (parent == NULL || saved == NULL || replacement == NULL
+         || rename(parent, saved) != 0 || symlink(replacement, parent) != 0) {
+            perror("rm-parent-race");
+            _exit(91);
+        }
+        fputs("rm-parent-race: changed parent\n", stderr);
+    }
+    return realClose(stream);
+}
diff --git a/tests/cli-tests/common/source-rm-parent-race.c b/tests/cli-tests/common/source-rm-parent-race.c
deleted file mode 100644
index a31547d..0000000
--- a/tests/cli-tests/common/source-rm-parent-race.c
+++ /dev/null
@@ -1,60 +0,0 @@
-/*
- * Copyright (c) Meta Platforms, Inc. and affiliates.
- * All rights reserved.
- *
- * This source code is licensed under both the BSD-style license (found in the
- * LICENSE file in the root directory of this source tree) and the GPLv2 (found
- * in the COPYING file in the root directory of this source tree).
- * You may select, at your option, one of the above-listed licenses.
- */
-
-/* Swap the source parent for a symlink right before its fclose, so --rm
- * re-resolves the pathname to a different file. */
-#define _GNU_SOURCE
-#include <dlfcn.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-
-typedef FILE* (*fopen_fn)(const char*, const char*);
-typedef int (*fclose_fn)(FILE*);
-
-static FILE* watched = NULL;
-static int done = 0;
-
-FILE* fopen(const char* path, const char* mode)
-{
-    static fopen_fn realFopen = NULL;
-    FILE* f;
-    if (realFopen == NULL)
-        realFopen = (fopen_fn)dlsym(RTLD_NEXT, "fopen");
-    f = realFopen(path, mode);
-    if (f != NULL && !done) {
-        const char* const target = getenv("ZSTD_TEST_RM_RACE_TARGET");
-        if (target != NULL && !strcmp(path, target))
-            watched = f;
-    }
-    return f;
-}
-
-int fclose(FILE* stream)
-{
-    static fclose_fn realClose = NULL;
-    if (realClose == NULL)
-        realClose = (fclose_fn)dlsym(RTLD_NEXT, "fclose");
-    if (!done && watched != NULL && stream == watched) {
-        const char* const parent = getenv("ZSTD_TEST_RM_RACE_PARENT");
-        const char* const saved = getenv("ZSTD_TEST_RM_RACE_SAVED");
-        const char* const replacement = getenv("ZSTD_TEST_RM_RACE_REPLACEMENT");
-        done = 1;
-        watched = NULL;
-        if (parent == NULL || saved == NULL || replacement == NULL
-         || rename(parent, saved) != 0 || symlink(replacement, parent) != 0) {
-            perror("source-rm-parent-race");
-            _exit(91);
-        }
-        fputs("source-rm-parent-race: changed source parent\n", stderr);
-    }
-    return realClose(stream);
-}
diff --git a/tests/cli-tests/file-handling/dst-rm-parent-race.sh b/tests/cli-tests/file-handling/dst-rm-parent-race.sh
new file mode 100755
index 0000000..acdc665
--- /dev/null
+++ b/tests/cli-tests/file-handling/dst-rm-parent-race.sh
@@ -0,0 +1,39 @@
+#!/bin/sh
+
+set -e
+
+# The interposer requires a native 64-bit Linux binary.
+[ -z "${EXEC_PREFIX:-}" ] || exit 0
+[ "$(uname -s)" = Linux ] || exit 0
+[ "$(getconf LONG_BIT)" = 64 ] || exit 0
+zstd --version | grep -Fq '(64-bit)' || exit 0
+
+${CC:-cc} -Wall -Wextra -Werror -shared -fPIC \
+    "$COMMON/rm-parent-race.c" -ldl -o rm-parent-race.so
+
+# A truncated frame : decompression fails after work/victim was created,
+# so zstd removes that partial output.
+datagen -g1M > data
+zstd -q data -o data.zst
+mkdir -p work protected
+head -c $(( $(wc -c < data.zst) / 2 )) data.zst > work/victim.zst
+printf 'protected content\n' > protected/victim
+cp protected/victim sentinel
+
+# fclose() on work/victim (the output) swaps work/ for a symlink to
+# protected/ before the failed run removes its partial output.
+if LD_PRELOAD="$PWD/rm-parent-race.so" \
+    ZSTD_TEST_RM_RACE_TARGET="work/victim" \
+    ZSTD_TEST_RM_RACE_PARENT="work" \
+    ZSTD_TEST_RM_RACE_SAVED="held" \
+    ZSTD_TEST_RM_RACE_REPLACEMENT="$PWD/protected" \
+    zstd -q -d work/victim.zst 2> stderr; then
+    die 'decompressing a truncated frame should fail'
+fi
+
+grep -Fxq 'rm-parent-race: changed parent' stderr
+[ -L work ] || die 'output parent was not replaced'
+# Unfixed code deletes the protected file here; fixed code refuses.
+cmp sentinel protected/victim
+# The partial output itself is left in place.
+[ -f held/victim ] || die 'partial output disappeared'
diff --git a/tests/cli-tests/file-handling/source-rm-parent-race.sh b/tests/cli-tests/file-handling/source-rm-parent-race.sh
index f3f46d1..1952a56 100755
--- a/tests/cli-tests/file-handling/source-rm-parent-race.sh
+++ b/tests/cli-tests/file-handling/source-rm-parent-race.sh
@@ -9,7 +9,7 @@
 zstd --version | grep -Fq '(64-bit)' || exit 0
 
 ${CC:-cc} -Wall -Wextra -Werror -shared -fPIC \
-    "$COMMON/source-rm-parent-race.c" -ldl -o source-rm-parent-race.so
+    "$COMMON/rm-parent-race.c" -ldl -o rm-parent-race.so
 
 printf 'attacker content\n' > work-input
 mkdir -p work protected
@@ -19,14 +19,14 @@
 
 # fclose() on work/victim swaps work/ for a symlink to protected/ before
 # --rm re-resolves the source pathname.
-LD_PRELOAD="$PWD/source-rm-parent-race.so" \
+LD_PRELOAD="$PWD/rm-parent-race.so" \
     ZSTD_TEST_RM_RACE_TARGET="work/victim" \
     ZSTD_TEST_RM_RACE_PARENT="work" \
     ZSTD_TEST_RM_RACE_SAVED="held" \
     ZSTD_TEST_RM_RACE_REPLACEMENT="$PWD/protected" \
     zstd -q --rm -o out.zst work/victim 2> stderr
 
-grep -Fxq 'source-rm-parent-race: changed source parent' stderr
+grep -Fxq 'rm-parent-race: changed parent' stderr
 [ -L work ] || die 'source parent was not replaced'
 # Unfixed code deletes the protected file here; fixed code refuses.
 cmp sentinel protected/victim
diff --git a/tests/cli-tests/file-stat/compress-file-to-file.sh.stderr.glob b/tests/cli-tests/file-stat/compress-file-to-file.sh.stderr.glob
index c677f35..1f34de8 100644
--- a/tests/cli-tests/file-stat/compress-file-to-file.sh.stderr.glob
+++ b/tests/cli-tests/file-stat/compress-file-to-file.sh.stderr.glob
@@ -30,6 +30,8 @@
 Trace:FileStat:  > UTIL_stat(*, )
 Trace:FileStat:  < 1
 Trace:FileStat: < 1
+Trace:FileStat: > UTIL_stat(*, file.zst)
+Trace:FileStat: < 1
 Trace:FileStat: > UTIL_isRegularFile(file.zst)
 Trace:FileStat:  > UTIL_stat(-1, file.zst)
 Trace:FileStat:  < 1
diff --git a/tests/cli-tests/file-stat/compress-stdin-to-file.sh.stderr.glob b/tests/cli-tests/file-stat/compress-stdin-to-file.sh.stderr.glob
index b315792..7034c48 100644
--- a/tests/cli-tests/file-stat/compress-stdin-to-file.sh.stderr.glob
+++ b/tests/cli-tests/file-stat/compress-stdin-to-file.sh.stderr.glob
@@ -18,6 +18,8 @@
 Trace:FileStat:  > UTIL_stat(*, )
 Trace:FileStat:  < 1
 Trace:FileStat: < 1
+Trace:FileStat: > UTIL_stat(*, file.zst)
+Trace:FileStat: < 1
 Trace:FileStat: > UTIL_isRegularFile(file.zst)
 Trace:FileStat:  > UTIL_stat(-1, file.zst)
 Trace:FileStat:  < 1
diff --git a/tests/cli-tests/file-stat/decompress-file-to-file.sh.stderr.glob b/tests/cli-tests/file-stat/decompress-file-to-file.sh.stderr.glob
index 7531efe..0555a91 100644
--- a/tests/cli-tests/file-stat/decompress-file-to-file.sh.stderr.glob
+++ b/tests/cli-tests/file-stat/decompress-file-to-file.sh.stderr.glob
@@ -30,6 +30,8 @@
 Trace:FileStat:  > UTIL_stat(*, )
 Trace:FileStat:  < 1
 Trace:FileStat: < 1
+Trace:FileStat: > UTIL_stat(*, file)
+Trace:FileStat: < 1
 Trace:FileStat: > UTIL_isRegularFile(file)
 Trace:FileStat:  > UTIL_stat(-1, file)
 Trace:FileStat:  < 1
diff --git a/tests/cli-tests/file-stat/decompress-stdin-to-file.sh.stderr.glob b/tests/cli-tests/file-stat/decompress-stdin-to-file.sh.stderr.glob
index 88fd1af..196f485 100644
--- a/tests/cli-tests/file-stat/decompress-stdin-to-file.sh.stderr.glob
+++ b/tests/cli-tests/file-stat/decompress-stdin-to-file.sh.stderr.glob
@@ -18,6 +18,8 @@
 Trace:FileStat:  > UTIL_stat(*, )
 Trace:FileStat:  < 1
 Trace:FileStat: < 1
+Trace:FileStat: > UTIL_stat(*, file)
+Trace:FileStat: < 1
 Trace:FileStat: > UTIL_isRegularFile(file)
 Trace:FileStat:  > UTIL_stat(-1, file)
 Trace:FileStat:  < 1