Skip to contentSkip to conversation

generic: test that truncate(2) on fsverity is blocked

PATCH In review Christoph Hellwig 3 participants 4 messages Jan 26 – Jan 29

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 generic: test that truncate(2) on fsverity is blocked

truncate(2) doesn't go through open and needs special code to block it on fsverity files. btrfs was missing this check.

Note that truncate(2) in current core utils uses openat and ftruncate, so add our own little test program for it.

---

Changes since v1:
- add the new binary to .gitignore
- add a _require_test_program "truncate" call
- drop the _require_test call

 .gitignore            |  1 +
 src/Makefile          |  2 +-
 src/truncate.c        | 17 +++++++++++++++++
 tests/generic/788     | 38 ++++++++++++++++++++++++++++++++++++++
 tests/generic/788.out |  4 ++++
 5 files changed, 61 insertions(+), 1 deletion(-)
 create mode 100644 src/truncate.c
 create mode 100755 tests/generic/788
 create mode 100644 tests/generic/788.out
.gitignore +1 -0
index 82c57f415301..2b5bedd59fce 100644
@@ -212,6 +212,7 @@ tags
 /src/dio-writeback-race
 /src/unlink-fsync
 /src/file_attr
+/src/truncate
 
 # Symlinked files
 /tests/generic/035.out
src/Makefile +1 -1
index d0a4106e6be8..577d816ae859 100644
@@ -21,7 +21,7 @@ TARGETS = dirstress fill fill2 getpagesize holes lstat64 \
 	t_mmap_writev_overlap checkpoint_journal mmap-rw-fault allocstale \
 	t_mmap_cow_memory_failure fake-dump-rootino dio-buf-fault rewinddir-test \
 	readdir-while-renames dio-append-buf-fault dio-write-fsync-same-fd \
-	dio-writeback-race unlink-fsync
+	dio-writeback-race unlink-fsync truncate
 
 LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
 	preallo_rw_pattern_writer ftrunc trunc fs_perms testx looptest \
src/truncate.c new file +17 -0
new file mode 100644
index 000000000000..4c106d26f29e
@@ -0,0 +1,17 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2026 Christoph Hellwig.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int main(int argc, char **argv)
+{
+	if (truncate(argv[1], 0) < 0) {
+		perror("truncate");
+		exit(1);
+	}
+
+	fprintf(stderr, "truncate succeeded\n");
+}
tests/generic/788 new file +38 -0
new file mode 100755
index 000000000000..ac4720d740f0
@@ -0,0 +1,38 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2026 Christoph Hellwig.
+#
+# FS QA Test 788
+#
+# Test that truncate(2) is blocked on fsverity files.
+#
+. ./common/preamble
+_begin_fstest auto quick verity
+
+_cleanup()
+{
+	cd /
+	_restore_fsverity_signatures
+	rm -f $tmp.*
+}
+
+. ./common/filter
+. ./common/verity
+
+_require_scratch_verity
+_require_test_program "truncate"
+
+_disable_fsverity_signatures
+
+_scratch_mkfs_verity &>> $seqres.full
+_scratch_mount
+
+fsv_file=$SCRATCH_MNT/file.fsv
+
+_fsv_scratch_begin_subtest "Using truncate(2) on a verity file fails"
+_fsv_create_enable_file $fsv_file
+$here/src/truncate $fsv_file
+
+# success, all done
+status=0
+exit
tests/generic/788.out new file +4 -0
new file mode 100644
index 000000000000..11074b71f3eb
@@ -0,0 +1,4 @@
+QA output created by 788
+
+# Using truncate(2) on a verity file fails
+truncate: Operation not permitted
Signature · 1 line
2.47.3
Reviewed-by on v2 1/1
#1 lore raw
Quoted 6 lines · Christoph Hellwig

truncate(2) doesn't go through open and needs special code to block it on fsverity files. btrfs was missing this check.

Note that truncate(2) in current core utils uses openat and ftruncate, so add our own little test program for it.

Quoted 7 lines

---

Changes since v1:
- add the new binary to .gitignore
- add a _require_test_program "truncate" call
- drop the _require_test call

I'm surprised that xfs_io doesn't have a truncate() wrapper but indeed it does not.

--D

Quoted 117 lines
 .gitignore            |  1 +
 src/Makefile          |  2 +-
 src/truncate.c        | 17 +++++++++++++++++
 tests/generic/788     | 38 ++++++++++++++++++++++++++++++++++++++
 tests/generic/788.out |  4 ++++
 5 files changed, 61 insertions(+), 1 deletion(-)
 create mode 100644 src/truncate.c
 create mode 100755 tests/generic/788
 create mode 100644 tests/generic/788.out
.gitignore +1 -0
index 82c57f415301..2b5bedd59fce 100644
@@ -212,6 +212,7 @@ tags
 /src/dio-writeback-race
 /src/unlink-fsync
 /src/file_attr
+/src/truncate
 
 # Symlinked files
 /tests/generic/035.out
src/Makefile +1 -1
index d0a4106e6be8..577d816ae859 100644
@@ -21,7 +21,7 @@ TARGETS = dirstress fill fill2 getpagesize holes lstat64 \
 	t_mmap_writev_overlap checkpoint_journal mmap-rw-fault allocstale \
 	t_mmap_cow_memory_failure fake-dump-rootino dio-buf-fault rewinddir-test \
 	readdir-while-renames dio-append-buf-fault dio-write-fsync-same-fd \
-	dio-writeback-race unlink-fsync
+	dio-writeback-race unlink-fsync truncate
 
 LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
 	preallo_rw_pattern_writer ftrunc trunc fs_perms testx looptest \
src/truncate.c new file +17 -0
new file mode 100644
index 000000000000..4c106d26f29e
@@ -0,0 +1,17 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2026 Christoph Hellwig.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int main(int argc, char **argv)
+{
+	if (truncate(argv[1], 0) < 0) {
+		perror("truncate");
+		exit(1);
+	}
+
+	fprintf(stderr, "truncate succeeded\n");
+}
tests/generic/788 new file +38 -0
new file mode 100755
index 000000000000..ac4720d740f0
@@ -0,0 +1,38 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2026 Christoph Hellwig.
+#
+# FS QA Test 788
+#
+# Test that truncate(2) is blocked on fsverity files.
+#
+. ./common/preamble
+_begin_fstest auto quick verity
+
+_cleanup()
+{
+	cd /
+	_restore_fsverity_signatures
+	rm -f $tmp.*
+}
+
+. ./common/filter
+. ./common/verity
+
+_require_scratch_verity
+_require_test_program "truncate"
+
+_disable_fsverity_signatures
+
+_scratch_mkfs_verity &>> $seqres.full
+_scratch_mount
+
+fsv_file=$SCRATCH_MNT/file.fsv
+
+_fsv_scratch_begin_subtest "Using truncate(2) on a verity file fails"
+_fsv_create_enable_file $fsv_file
+$here/src/truncate $fsv_file
+
+# success, all done
+status=0
+exit
tests/generic/788.out new file +4 -0
new file mode 100644
index 000000000000..11074b71f3eb
@@ -0,0 +1,4 @@
+QA output created by 788
+
+# Using truncate(2) on a verity file fails
+truncate: Operation not permitted
Signature · 1 line
2.47.3
#2 lore raw
Quoted 10 lines · Darrick J. Wong
Quoted 9 lines · Christoph Hellwig

truncate(2) doesn't go through open and needs special code to block it on fsverity files. btrfs was missing this check.

Note that truncate(2) in current core utils uses openat and ftruncate, so add our own little test program for it.

---

Quoted 7 lines
Quoted 4 lines

Changes since v1:
- add the new binary to .gitignore
- add a _require_test_program "truncate" call
- drop the _require_test call

I'm surprised that xfs_io doesn't have a truncate() wrapper but indeed it does not.

Yeah, xfs_io is designed to operate on open files, so its truncate command naturally uses ftruncate (which takes a file descriptor). However, the truncate syscall requires a "char *path". How should we handle this discrepancy if we'd like to support the truncate syscall in xfs_io? Should we truncate the path of the currently open file? Should we perform a close-truncate-open sequence? Or should we add a specific option to allow the user to provide another file path?

Thanks,
Zorro

Quoted 123 lines

--D

Quoted 117 lines
 .gitignore            |  1 +
 src/Makefile          |  2 +-
 src/truncate.c        | 17 +++++++++++++++++
 tests/generic/788     | 38 ++++++++++++++++++++++++++++++++++++++
 tests/generic/788.out |  4 ++++
 5 files changed, 61 insertions(+), 1 deletion(-)
 create mode 100644 src/truncate.c
 create mode 100755 tests/generic/788
 create mode 100644 tests/generic/788.out
.gitignore +1 -0
index 82c57f415301..2b5bedd59fce 100644
@@ -212,6 +212,7 @@ tags
 /src/dio-writeback-race
 /src/unlink-fsync
 /src/file_attr
+/src/truncate
 
 # Symlinked files
 /tests/generic/035.out
src/Makefile +1 -1
index d0a4106e6be8..577d816ae859 100644
@@ -21,7 +21,7 @@ TARGETS = dirstress fill fill2 getpagesize holes lstat64 \
 	t_mmap_writev_overlap checkpoint_journal mmap-rw-fault allocstale \
 	t_mmap_cow_memory_failure fake-dump-rootino dio-buf-fault rewinddir-test \
 	readdir-while-renames dio-append-buf-fault dio-write-fsync-same-fd \
-	dio-writeback-race unlink-fsync
+	dio-writeback-race unlink-fsync truncate
 
 LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
 	preallo_rw_pattern_writer ftrunc trunc fs_perms testx looptest \
src/truncate.c new file +17 -0
new file mode 100644
index 000000000000..4c106d26f29e
@@ -0,0 +1,17 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2026 Christoph Hellwig.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int main(int argc, char **argv)
+{
+	if (truncate(argv[1], 0) < 0) {
+		perror("truncate");
+		exit(1);
+	}
+
+	fprintf(stderr, "truncate succeeded\n");
+}
tests/generic/788 new file +38 -0
new file mode 100755
index 000000000000..ac4720d740f0
@@ -0,0 +1,38 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2026 Christoph Hellwig.
+#
+# FS QA Test 788
+#
+# Test that truncate(2) is blocked on fsverity files.
+#
+. ./common/preamble
+_begin_fstest auto quick verity
+
+_cleanup()
+{
+	cd /
+	_restore_fsverity_signatures
+	rm -f $tmp.*
+}
+
+. ./common/filter
+. ./common/verity
+
+_require_scratch_verity
+_require_test_program "truncate"
+
+_disable_fsverity_signatures
+
+_scratch_mkfs_verity &>> $seqres.full
+_scratch_mount
+
+fsv_file=$SCRATCH_MNT/file.fsv
+
+_fsv_scratch_begin_subtest "Using truncate(2) on a verity file fails"
+_fsv_create_enable_file $fsv_file
+$here/src/truncate $fsv_file
+
+# success, all done
+status=0
+exit
tests/generic/788.out new file +4 -0
new file mode 100644
index 000000000000..11074b71f3eb
@@ -0,0 +1,4 @@
+QA output created by 788
+
+# Using truncate(2) on a verity file fails
+truncate: Operation not permitted
Signature · 1 line
2.47.3
Reviewed-by on v2 1/1
#3 lore raw
In reply to Christoph Hellwig
Quoted 6 lines · Christoph Hellwig

truncate(2) doesn't go through open and needs special code to block it on fsverity files. btrfs was missing this check.

Note that truncate(2) in current core utils uses openat and ftruncate, so add our own little test program for it.

Quoted 7 lines

---

Changes since v1:
- add the new binary to .gitignore
- add a _require_test_program "truncate" call
- drop the _require_test call

Thanks, this version is good to me.

Quoted 117 lines
 .gitignore            |  1 +
 src/Makefile          |  2 +-
 src/truncate.c        | 17 +++++++++++++++++
 tests/generic/788     | 38 ++++++++++++++++++++++++++++++++++++++
 tests/generic/788.out |  4 ++++
 5 files changed, 61 insertions(+), 1 deletion(-)
 create mode 100644 src/truncate.c
 create mode 100755 tests/generic/788
 create mode 100644 tests/generic/788.out
.gitignore +1 -0
index 82c57f415301..2b5bedd59fce 100644
@@ -212,6 +212,7 @@ tags
 /src/dio-writeback-race
 /src/unlink-fsync
 /src/file_attr
+/src/truncate
 
 # Symlinked files
 /tests/generic/035.out
src/Makefile +1 -1
index d0a4106e6be8..577d816ae859 100644
@@ -21,7 +21,7 @@ TARGETS = dirstress fill fill2 getpagesize holes lstat64 \
 	t_mmap_writev_overlap checkpoint_journal mmap-rw-fault allocstale \
 	t_mmap_cow_memory_failure fake-dump-rootino dio-buf-fault rewinddir-test \
 	readdir-while-renames dio-append-buf-fault dio-write-fsync-same-fd \
-	dio-writeback-race unlink-fsync
+	dio-writeback-race unlink-fsync truncate
 
 LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
 	preallo_rw_pattern_writer ftrunc trunc fs_perms testx looptest \
src/truncate.c new file +17 -0
new file mode 100644
index 000000000000..4c106d26f29e
@@ -0,0 +1,17 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Copyright (c) 2026 Christoph Hellwig.
+ */
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+int main(int argc, char **argv)
+{
+	if (truncate(argv[1], 0) < 0) {
+		perror("truncate");
+		exit(1);
+	}
+
+	fprintf(stderr, "truncate succeeded\n");
+}
tests/generic/788 new file +38 -0
new file mode 100755
index 000000000000..ac4720d740f0
@@ -0,0 +1,38 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2026 Christoph Hellwig.
+#
+# FS QA Test 788
+#
+# Test that truncate(2) is blocked on fsverity files.
+#
+. ./common/preamble
+_begin_fstest auto quick verity
+
+_cleanup()
+{
+	cd /
+	_restore_fsverity_signatures
+	rm -f $tmp.*
+}
+
+. ./common/filter
+. ./common/verity
+
+_require_scratch_verity
+_require_test_program "truncate"
+
+_disable_fsverity_signatures
+
+_scratch_mkfs_verity &>> $seqres.full
+_scratch_mount
+
+fsv_file=$SCRATCH_MNT/file.fsv
+
+_fsv_scratch_begin_subtest "Using truncate(2) on a verity file fails"
+_fsv_create_enable_file $fsv_file
+$here/src/truncate $fsv_file
+
+# success, all done
+status=0
+exit
tests/generic/788.out new file +4 -0
new file mode 100644
index 000000000000..11074b71f3eb
@@ -0,0 +1,4 @@
+QA output created by 788
+
+# Using truncate(2) on a verity file fails
+truncate: Operation not permitted
Signature · 1 line
2.47.3