Skip to contentSkip to conversation

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

PATCH Superseded Christoph Hellwig 2 participants 5 messages Jan 19 – Jan 22

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 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.

---
 src/Makefile          |  2 +-
 src/truncate.c        | 17 +++++++++++++++++
 tests/generic/788     | 37 +++++++++++++++++++++++++++++++++++++
 tests/generic/788.out |  4 ++++
 4 files changed, 59 insertions(+), 1 deletion(-)
 create mode 100644 src/truncate.c
 create mode 100755 tests/generic/788
 create mode 100644 tests/generic/788.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 +37 -0
new file mode 100755
index 000000000000..02959037e7c3
@@ -0,0 +1,37 @@
+#! /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_test
+_require_scratch_verity
+_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
#1 lore raw
Quoted 22 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.

---
 src/Makefile          |  2 +-
 src/truncate.c        | 17 +++++++++++++++++
 tests/generic/788     | 37 +++++++++++++++++++++++++++++++++++++
 tests/generic/788.out |  4 ++++
 4 files changed, 59 insertions(+), 1 deletion(-)
 create mode 100644 src/truncate.c
 create mode 100755 tests/generic/788
 create mode 100644 tests/generic/788.out
src/Makefile +0 -0
index d0a4106e6be8..577d816ae859 100644

src/Makefile TARGETS = dirstress fill fill2 getpagesize holes lstat64 \ · line 24

Quoted 5 lines
src/Makefile +1 -1
@@ -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

Need to add this into .gitignore too :)

Quoted 44 lines
src/Makefile +0 -0
 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 +12 -0
new file mode 100755
index 000000000000..02959037e7c3
@@ -0,0 +1,37 @@
+#! /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()

tests/generic/788 line 22

Quoted 10 lines
tests/generic/788 new file +10 -0
+{
+	cd /
+	_restore_fsverity_signatures
+	rm -f $tmp.*
+}
+
+. ./common/filter
+. ./common/verity
+
+_require_test

Does this case need TEST_DEV and TEST_DIR?

tests/generic/788 line 33

Quoted 11 lines
tests/generic/788 new file +11 -0
+_require_scratch_verity
+_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

_require_test_program "truncate"

Quoted 18 lines
tests/generic/788 new file +4 -0
+
+# 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 3 lines · Zorro Lang

src/Makefile TARGETS = dirstress fill fill2 getpagesize holes lstat64 \ · line 24

Quoted 1 line
src/Makefile +1 -0
+	dio-writeback-race unlink-fsync truncate

Need to add this into .gitignore too :)

True.

Quoted 3 lines
Quoted 1 line

+_require_test

Does this case need TEST_DEV and TEST_DIR?

No, just copied and pasted.

Quoted 3 lines
Quoted 1 line

+$here/src/truncate $fsv_file

_require_test_program "truncate"

Ok.

#3 lore raw
Quoted 11 lines · Christoph Hellwig
Quoted 3 lines · Zorro Lang

src/Makefile TARGETS = dirstress fill fill2 getpagesize holes lstat64 \ · line 24

Quoted 1 line
src/Makefile +1 -0
+	dio-writeback-race unlink-fsync truncate

Need to add this into .gitignore too :)

True.

Quoted 3 lines
Quoted 1 line

+_require_test

Does this case need TEST_DEV and TEST_DIR?

Quoted 7 lines

No, just copied and pasted.

Quoted 3 lines
Quoted 1 line

+$here/src/truncate $fsv_file

_require_test_program "truncate"

Ok.

Thanks Christoph, BTW, as you said "btrfs was missing this check", so is there a known fix/commit related with this test case, if there's, feel free to mark that with _fixed_by_kernel_commit :)

Quoted 1 line
#4 lore raw
Quoted 16 lines · Zorro Lang
Quoted 15 lines · Christoph Hellwig
Quoted 3 lines · Zorro Lang

src/Makefile TARGETS = dirstress fill fill2 getpagesize holes lstat64 \ · line 24

Quoted 1 line
src/Makefile +1 -0
+	dio-writeback-race unlink-fsync truncate

Need to add this into .gitignore too :)

True.

Quoted 3 lines
Quoted 1 line

+_require_test

Does this case need TEST_DEV and TEST_DIR?

No, just copied and pasted.

Quoted 2 lines
Quoted 1 line

+$here/src/truncate $fsv_file

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

_require_test_program "truncate"

Ok.

Thanks Christoph, BTW, as you said "btrfs was missing this check", so is there a known fix/commit related with this test case, if there's, feel free to mark that with _fixed_by_kernel_commit :)

I sent a fix, but it hasn't been applied anywhere yet.