mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH master v2] fs: fix NULL pointer dereference of fsdrv->truncate for read-only FS
@ 2021-07-07  6:52 Ahmad Fatoum
  2021-07-09 20:54 ` Sascha Hauer
  0 siblings, 1 reply; 2+ messages in thread
From: Ahmad Fatoum @ 2021-07-07  6:52 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum, Xogium

fsdrv->truncate is dereferenced at times without checking for NULL before,
leading to crashes, e.g. doing:

  edit -o /mnt/myext4/FILE some text

on ext4 crashes. Fix this by returning -EROFS when truncate is unimplemented.

Reported-by: Xogium <contact@xogium.me>
Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
---
v1 -> v2:
  - use dev parameter instead of going through &f->fsdev->dev.
    No functional change, but makes diff easier to verify.
---
 fs/fs.c | 16 ++++++++++------
 1 file changed, 10 insertions(+), 6 deletions(-)

diff --git a/fs/fs.c b/fs/fs.c
index a9dbf1dd4e2f..b6431227d60c 100644
--- a/fs/fs.c
+++ b/fs/fs.c
@@ -208,9 +208,15 @@ int creat(const char *pathname, mode_t mode)
 }
 EXPORT_SYMBOL(creat);
 
+static int fsdev_truncate(struct device_d *dev, FILE *f, loff_t length)
+{
+	struct fs_driver_d *fsdrv = f->fsdev->driver;
+
+	return fsdrv->truncate ? fsdrv->truncate(dev, f, length) : -EROFS;
+}
+
 int ftruncate(int fd, loff_t length)
 {
-	struct fs_driver_d *fsdrv;
 	FILE *f = fd_to_file(fd);
 	int ret;
 
@@ -220,9 +226,7 @@ int ftruncate(int fd, loff_t length)
 	if (f->size == FILE_SIZE_STREAM)
 		return 0;
 
-	fsdrv = f->fsdev->driver;
-
-	ret = fsdrv->truncate(&f->fsdev->dev, f, length);
+	ret = fsdev_truncate(&f->fsdev->dev, f, length);
 	if (ret) {
 		errno = -ret;
 		return ret;
@@ -332,7 +336,7 @@ static ssize_t __write(FILE *f, const void *buf, size_t count)
 		assert_command_context();
 
 	if (f->size != FILE_SIZE_STREAM && f->pos + count > f->size) {
-		ret = fsdrv->truncate(&f->fsdev->dev, f, f->pos + count);
+		ret = fsdev_truncate(&f->fsdev->dev, f, f->pos + count);
 		if (ret) {
 			if (ret != -ENOSPC)
 				goto out;
@@ -2463,7 +2467,7 @@ int open(const char *pathname, int flags, ...)
 	}
 
 	if (flags & O_TRUNC) {
-		error = fsdrv->truncate(&fsdev->dev, f, 0);
+		error = fsdev_truncate(&fsdev->dev, f, 0);
 		f->size = 0;
 		inode->i_size = 0;
 		if (error)
-- 
2.32.0


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH master v2] fs: fix NULL pointer dereference of fsdrv->truncate for read-only FS
  2021-07-07  6:52 [PATCH master v2] fs: fix NULL pointer dereference of fsdrv->truncate for read-only FS Ahmad Fatoum
@ 2021-07-09 20:54 ` Sascha Hauer
  0 siblings, 0 replies; 2+ messages in thread
From: Sascha Hauer @ 2021-07-09 20:54 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox, Xogium

On Wed, Jul 07, 2021 at 08:52:51AM +0200, Ahmad Fatoum wrote:
> fsdrv->truncate is dereferenced at times without checking for NULL before,
> leading to crashes, e.g. doing:
> 
>   edit -o /mnt/myext4/FILE some text
> 
> on ext4 crashes. Fix this by returning -EROFS when truncate is unimplemented.
> 
> Reported-by: Xogium <contact@xogium.me>
> Signed-off-by: Ahmad Fatoum <ahmad@a3f.at>
> ---
> v1 -> v2:
>   - use dev parameter instead of going through &f->fsdev->dev.
>     No functional change, but makes diff easier to verify.
> ---
>  fs/fs.c | 16 ++++++++++------
>  1 file changed, 10 insertions(+), 6 deletions(-)

Applied, thanks

Sascha

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2021-07-09 20:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-07-07  6:52 [PATCH master v2] fs: fix NULL pointer dereference of fsdrv->truncate for read-only FS Ahmad Fatoum
2021-07-09 20:54 ` Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox