generic: test that truncate(2) on fsverity is blocked
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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
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
Signed-off-by Christoph Hellwig <[email protected]>---
Changes since v1:
- add the new binary to .gitignore
- add a_require_test_program"truncate" call
- drop the_require_testcall
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@@ -212,6 +212,7 @@ tags /src/dio-writeback-race /src/unlink-fsync /src/file_attr +/src/truncate # Symlinked files /tests/generic/035.outsrc/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 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@@ -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@@ -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 +exittests/generic/788.out new file +4
-0@@ -0,0 +1,4 @@ +QA output created by 788 + +# Using truncate(2) on a verity file fails +truncate: Operation not permittedSignature · 1 line
2.47.3
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.
Signed-off-by Christoph Hellwig <[email protected]>---
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_testcallI'm surprised that
xfs_iodoesn't have atruncate()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
Reviewed-by "Darrick J. Wong" <[email protected]>--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@@ -212,6 +212,7 @@ tags /src/dio-writeback-race /src/unlink-fsync /src/file_attr +/src/truncate # Symlinked files /tests/generic/035.outsrc/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 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@@ -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@@ -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 +exittests/generic/788.out new file +4
-0@@ -0,0 +1,4 @@ +QA output created by 788 + +# Using truncate(2) on a verity file fails +truncate: Operation not permittedSignature · 1 line
2.47.3
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
Signed-off-by Christoph Hellwig <[email protected]>---
Changes since v1:
- add the new binary to .gitignore
- add a_require_test_program"truncate" call
- drop the_require_testcall
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@@ -212,6 +212,7 @@ tags /src/dio-writeback-race /src/unlink-fsync /src/file_attr +/src/truncate # Symlinked files /tests/generic/035.outsrc/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 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@@ -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@@ -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 +exittests/generic/788.out new file +4
-0@@ -0,0 +1,4 @@ +QA output created by 788 + +# Using truncate(2) on a verity file fails +truncate: Operation not permittedSignature · 1 line
2.47.3