[PATCH v2 15/15] fs: error out when writing on read-only file system

Ahmad Fatoum a.fatoum at pengutronix.de
Fri Apr 30 14:29:49 BST 2021


On an ext4 partition, Running

  echo -a /mnt/disk0.0/file test

will crash barebox because fsdrv->truncate in __write is NULL.

Don't let it come to this and verify earlier that the FS implements
->write. While at it, explicitly compare with O_RDONLY (== 0) for
clarity.

Fixes: b3fbfad7aeaf ("fs: dentry cache implementation")
Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
---
 fs/fs.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

diff --git a/fs/fs.c b/fs/fs.c
index a023324913c4..881dc2fca0ce 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -321,13 +321,13 @@ static ssize_t __write(FILE *f, const void *buf, size_t count)
 	struct fs_driver_d *fsdrv;
 	int ret;
 
-	if (!(f->flags & O_ACCMODE)) {
+	fsdrv = f->fsdev->driver;
+
+	if ((f->flags & O_ACCMODE) == O_RDONLY || !fsdrv->write) {
 		ret = -EBADF;
 		goto out;
 	}
 
-	fsdrv = f->fsdev->driver;
-
 	if (fsdrv != ramfs_driver)
 		assert_command_context();
 
-- 
2.29.2




More information about the barebox mailing list