From 8c053e955ea87e1cc2c1292237ff7fcf836cf495 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Duval?= Date: Sun, 18 Nov 2018 00:46:57 +0100 Subject: [PATCH] vfs: fail write_stat() on file descriptors opened read-only. Change-Id: I20d586c606c47df6625cc9272f153250a5a621d6 Reviewed-on: https://review.haiku-os.org/706 Reviewed-by: waddlesplash --- src/system/kernel/fs/vfs.cpp | 3 +++ src/tests/system/libroot/posix/truncate.cpp | 15 +++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/src/system/kernel/fs/vfs.cpp b/src/system/kernel/fs/vfs.cpp index c06660f875..e5de2b366f 100644 --- a/src/system/kernel/fs/vfs.cpp +++ b/src/system/kernel/fs/vfs.cpp @@ -6562,6 +6562,9 @@ common_write_stat(struct file_descriptor* descriptor, const struct stat* stat, FUNCTION(("common_write_stat(vnode = %p, stat = %p, statMask = %d)\n", vnode, stat, statMask)); + if ((descriptor->open_mode & O_RWMASK) == O_RDONLY) + return B_BAD_VALUE; + if (!HAS_FS_CALL(vnode, write_stat)) return B_READ_ONLY_DEVICE; diff --git a/src/tests/system/libroot/posix/truncate.cpp b/src/tests/system/libroot/posix/truncate.cpp index c0256760d3..df1e94b2b0 100644 --- a/src/tests/system/libroot/posix/truncate.cpp +++ b/src/tests/system/libroot/posix/truncate.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include #include @@ -48,5 +49,19 @@ main(int argc, char **argv) return 1; } + int fd = open(argv[1], O_RDONLY); + if (fd < 0) { + fprintf(stderr, "%s: could open the file read-only \"%s\": %s\n", + __progname, argv[1], strerror(errno)); + return 1; + } + if (ftruncate(fd, newSize) == 0 || errno != EINVAL) { + fprintf(stderr, "%s: could truncate a file opened read-only \"%s\": %s\n", + __progname, argv[1], strerror(errno)); + close(fd); + return 1; + } + close(fd); + return 0; }