diff options
| author | Dag-Erling Smørgrav <des@FreeBSD.org> | 2026-11-13 20:28:38 +0000 |
|---|---|---|
| committer | Mark Johnston <markj@FreeBSD.org> | 2026-11-17 16:57:23 +0000 |
| commit | 223cce4e03e4a44a60b8bb4c6d551b51249aad5c (patch) | |
| tree | 17da41c9ca82823a4f26fb3af535f2a3cc4b39cb | |
| parent | 9d7c65d1c79f636037f74030ed01d65c11f8b225 (diff) | |
tests: Add test for fdescfs truncation bug
Add a test case that verifies that it is not possible to truncate a
file opened read-only through fdescfs in default mode, and another that
verifies that it is possible when fdescfs was mounted in nodup mode.
Approved by: so
Security: FreeBSD-SA-26:91.fdescfs
Reviewed by: kib
Differential Revision: https://reviews.freebsd.org/D61236
| -rw-r--r-- | tests/sys/fs/fdescfs/fdescfs_test.c | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/tests/sys/fs/fdescfs/fdescfs_test.c b/tests/sys/fs/fdescfs/fdescfs_test.c index f80d335108e6..4f1151f7d221 100644 --- a/tests/sys/fs/fdescfs/fdescfs_test.c +++ b/tests/sys/fs/fdescfs/fdescfs_test.c @@ -223,6 +223,77 @@ ATF_TC_BODY(nodup_ioctl_cap_intersection, tc) ATF_REQUIRE_EQ(0, unmount("mnt", 0)); } +#define TEXT "Hello, world!\n" +#define LEN 14 + +FDESCFS_TC(truncate_ro, + "disallow truncating read-only files (plain)"); +ATF_TC_BODY(truncate_ro, tc) +{ + struct stat sb; + char fd_path[16]; + int fd; + + /* create read-only target file */ + ATF_REQUIRE((fd = open("file", O_RDWR | O_CREAT, 0400)) >= 0); + ATF_REQUIRE_EQ(LEN, write(fd, TEXT, LEN)); + ATF_REQUIRE_EQ(0, close(fd)); + + /* reopen the file read-only */ + ATF_REQUIRE((fd = open("file", O_RDONLY)) >= 0); + + /* mount fdescfs */ + mount_fdescfs(NULL); + + /* construct and stat path */ + snprintf(fd_path, sizeof(fd_path), "mnt/%d", fd); + ATF_REQUIRE_EQ(0, stat(fd_path, &sb)); + ATF_CHECK_EQ(S_IFCHR, sb.st_mode & S_IFMT); + ATF_CHECK_EQ(0555, sb.st_mode & ALLPERMS); + ATF_CHECK_EQ(0, sb.st_size); + + /* attempt to truncate */ + ATF_CHECK(truncate(fd_path, 0) < 0); + ATF_CHECK_EQ(LEN, lseek(fd, 0, SEEK_END)); + + /* done */ + ATF_CHECK_EQ(0, close(fd)); +} + +FDESCFS_TC(nodup_truncate_ro, + "allow truncating read-only files (nodup)"); +ATF_TC_BODY(nodup_truncate_ro, tc) +{ + struct stat sb; + char fd_path[16]; + int fd; + + /* create read-only target file */ + ATF_REQUIRE((fd = open("file", O_RDWR | O_CREAT, 0400)) >= 0); + ATF_REQUIRE_EQ(LEN, write(fd, TEXT, LEN)); + ATF_REQUIRE_EQ(0, close(fd)); + + /* reopen the file read-only */ + ATF_REQUIRE((fd = open("file", O_RDONLY)) >= 0); + + /* mount fdescfs */ + mount_fdescfs(nodup); + + /* construct and stat path */ + snprintf(fd_path, sizeof(fd_path), "mnt/%d", fd); + ATF_REQUIRE_EQ(0, stat(fd_path, &sb)); + ATF_CHECK_EQ(S_IFREG, sb.st_mode & S_IFMT); + ATF_CHECK_EQ(0400, sb.st_mode & ALLPERMS); + ATF_CHECK_EQ(LEN, sb.st_size); + + /* attempt to truncate */ + ATF_CHECK(truncate(fd_path, LEN / 2) == 0); + ATF_CHECK_EQ(LEN / 2, lseek(fd, 0, SEEK_END)); + + /* done */ + ATF_CHECK_EQ(0, close(fd)); +} + ATF_TP_ADD_TCS(tp) { ATF_TP_ADD_TC(tp, cap_rights); @@ -232,5 +303,7 @@ ATF_TP_ADD_TCS(tp) ATF_TP_ADD_TC(tp, ioctl_caps); ATF_TP_ADD_TC(tp, nodup_ioctl_caps); ATF_TP_ADD_TC(tp, nodup_ioctl_cap_intersection); + ATF_TP_ADD_TC(tp, truncate_ro); + ATF_TP_ADD_TC(tp, nodup_truncate_ro); return (atf_no_error()); } |
