Looks like this was introduced in commit
50e438972b8f6e4c7486c38beb542539de0298c7.

If ftruncate failed, we may set exit_status to EXIT_FAILURE depending
on the file type. However, that value would always be overwritten by
dd_copy.

--- 8< ---

* src/dd.c (main): Initialize exit_status to EXIT_SUCCESS. Don't reset
exit_status if dd_copy succeeds.
* tests/dd/fail-ftruncate-fstat.sh: New test.
* tests/local.mk (all_tests): Add the new test.
* NEWS: Mention the bug fix.
---
 NEWS                             |  3 ++
 src/dd.c                         |  4 +-
 tests/dd/fail-ftruncate-fstat.sh | 72 ++++++++++++++++++++++++++++++++
 tests/local.mk                   |  1 +
 4 files changed, 78 insertions(+), 2 deletions(-)
 create mode 100755 tests/dd/fail-ftruncate-fstat.sh

diff --git a/NEWS b/NEWS
index 58cf776a9..75d730228 100644
--- a/NEWS
+++ b/NEWS
@@ -4,6 +4,9 @@ GNU coreutils NEWS                                    -*- 
outline -*-
 
 ** Bug fixes
 
+  'dd seek=N' no longer exits successfully if ftruncate fails.
+  [bug introduced in coreutils-9.1]
+
   du and ls no longer modify strings returned by getenv.
   POSIX says this is not portable.
   [bug introduced in fileutils-4.1.6]
diff --git a/src/dd.c b/src/dd.c
index 117d61908..75bd401aa 100644
--- a/src/dd.c
+++ b/src/dd.c
@@ -2379,7 +2379,6 @@ synchronize_output (void)
 int
 main (int argc, char **argv)
 {
-  int exit_status;
   off_t offset;
 
   install_signal_handlers ();
@@ -2426,6 +2425,7 @@ main (int argc, char **argv)
   input_offset = MAX (0, offset);
   input_seek_errno = errno;
 
+  int exit_status = EXIT_SUCCESS;
   if (output_file == nullptr)
     {
       output_file = _("standard output");
@@ -2494,7 +2494,7 @@ main (int argc, char **argv)
   start_time = gethrxtime ();
   next_time = start_time + XTIME_PRECISION;
 
-  exit_status = dd_copy ();
+  exit_status |= dd_copy ();
 
   int sync_status = synchronize_output ();
   if (sync_status)
diff --git a/tests/dd/fail-ftruncate-fstat.sh b/tests/dd/fail-ftruncate-fstat.sh
new file mode 100755
index 000000000..8297e1eae
--- /dev/null
+++ b/tests/dd/fail-ftruncate-fstat.sh
@@ -0,0 +1,72 @@
+#!/bin/sh
+# Check that 'dd' does not succeed if ftruncate and fstat fail.
+
+# Copyright (C) 2025 Free Software Foundation, Inc.
+
+# This program is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+. "${srcdir=.}/tests/init.sh"; path_prepend_ ./src
+print_ver_ dd
+require_gcc_shared_
+require_selinux_
+
+cat > k.c <<'EOF' || framework_failure_
+#include <sys/stat.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <errno.h>
+
+int
+ftruncate (int fd, off_t length)
+{
+  /* Prove that LD_PRELOAD works: create the evidence file "x".  */
+  fclose (fopen ("x", "w"));
+
+  /* Pretend failure.  */
+  errno = EPERM;
+  return -1;
+}
+
+int
+fstat (int fd, struct stat *statbuf)
+{
+  /* Prove that LD_PRELOAD works: create the evidence file "y".  */
+  fclose (fopen ("y", "w"));
+
+  /* Pretend failure.  */
+  errno = EPERM;
+  return -1;
+}
+EOF
+
+# Then compile/link it:
+gcc_shared_ k.c k.so \
+  || framework_failure_ 'failed to build shared library'
+
+LD_PRELOAD=$LD_PRELOAD:./k.so dd if=/dev/zero of=out count=1 \
+                              seek=1 status=none 2>err
+ret=$?
+
+test -f x && test -f y \
+  || skip_ "internal test failure: maybe LD_PRELOAD doesn't work?"
+
+# After ftruncate fails, we use fstat to get the file type.
+echo "dd: cannot fstat 'out': Operation not permitted" > exp
+compare exp err || fail=1
+
+# If fstat fails, then we should not mistakenly succeed, as would occur with
+# coreutils 9.1 to 9.9.
+test "$ret" = 1 || fail=1
+
+Exit $fail
diff --git a/tests/local.mk b/tests/local.mk
index 59aa18adf..56a37c524 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -583,6 +583,7 @@ all_tests =                                 \
   tests/dd/ascii.sh                            \
   tests/dd/conv-case.sh                                \
   tests/dd/direct.sh                           \
+  tests/dd/fail-ftruncate-fstat.sh             \
   tests/dd/misc.sh                             \
   tests/dd/no-allocate.sh                      \
   tests/dd/nocache.sh                          \
-- 
2.52.0


Reply via email to