Skip to contentSkip to conversation

ocfs2: check new file size on fallocate call

PATCH In review Luís Henriques 4 participants 8 messages May 29, 2023 – Jun 1, 2023 OCFS2

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ocfs2: check new file size on fallocate call

When changing a file size with fallocate() the new size isn't being checked. In particular, the FSIZE ulimit isn't being checked, which makes fstest generic/228 fail. Simply adding a call to inode_newsize_ok() fixes this issue.

---
 fs/ocfs2/file.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
fs/ocfs2/file.c +7 -1
index efb09de4343d..b173c36bcab3 100644
@@ -2100,14 +2100,20 @@ static long ocfs2_fallocate(struct file *file, int mode, loff_t offset,
 	struct ocfs2_space_resv sr;
 	int change_size = 1;
 	int cmd = OCFS2_IOC_RESVSP64;
+	int ret = 0;
 
 	if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
 		return -EOPNOTSUPP;
 	if (!ocfs2_writes_unwritten_extents(osb))
 		return -EOPNOTSUPP;
 
-	if (mode & FALLOC_FL_KEEP_SIZE)
+	if (mode & FALLOC_FL_KEEP_SIZE) {
 		change_size = 0;
+	} else {
+		ret = inode_newsize_ok(inode, offset + len);
+		if (ret)
+			return ret;
+	}
 
 	if (mode & FALLOC_FL_PUNCH_HOLE)
 		cmd = OCFS2_IOC_UNRESVSP64;
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Luís Henriques

When changing a file size with fallocate() the new size isn't being checked. In particular, the FSIZE ulimit isn't being checked, which makes fstest generic/228 fail. Simply adding a call to inode_newsize_ok() fixes this issue.

Looks good, thanks Luis.

maintainer
#2 lore raw
In reply to Luís Henriques
Quoted 15 lines · Luís Henriques

When changing a file size with fallocate() the new size isn't being checked. In particular, the FSIZE ulimit isn't being checked, which makes fstest generic/228 fail. Simply adding a call to inode_newsize_ok() fixes this issue.

---
 fs/ocfs2/file.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
fs/ocfs2/file.c +0 -0
index efb09de4343d..b173c36bcab3 100644

fs/ocfs2/file.c static long ocfs2_fallocate(struct file *file, int mode, loff_t offset, · line 2106

Quoted 7 lines
fs/ocfs2/file.c +1 -0
@@ -2100,14 +2100,20 @@ static long ocfs2_fallocate(struct file *file, int mode, loff_t offset,
 	struct ocfs2_space_resv sr;
 	int change_size = 1;
 	int cmd = OCFS2_IOC_RESVSP64;
+	int ret = 0;
 
 	if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
 		return -EOPNOTSUPP;

This means we only support keep-size and pouch_hole. And it seems pouch_hole will also imply keep-size.

fs/ocfs2/file.c static long ocfs2_fallocate(struct file *file, int mode, loff_t offset, · line 2112

Quoted 7 lines
fs/ocfs2/file.c +2 -1
 	if (!ocfs2_writes_unwritten_extents(osb))
 		return -EOPNOTSUPP;
 
-	if (mode & FALLOC_FL_KEEP_SIZE)
+	if (mode & FALLOC_FL_KEEP_SIZE) {
 		change_size = 0;
+	} else {

Seems this will be a dead branch?

Thanks,
Joseph

Quoted 7 lines
fs/ocfs2/file.c +4 -0
+		ret = inode_newsize_ok(inode, offset + len);
+		if (ret)
+			return ret;
+	}
 
 	if (mode & FALLOC_FL_PUNCH_HOLE)
 		cmd = OCFS2_IOC_UNRESVSP64;
#3 lore raw
Quoted 21 lines · Joseph Qi
Quoted 20 lines · Luís Henriques

When changing a file size with fallocate() the new size isn't being checked. In particular, the FSIZE ulimit isn't being checked, which makes fstest generic/228 fail. Simply adding a call to inode_newsize_ok() fixes this issue.

---
 fs/ocfs2/file.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
fs/ocfs2/file.c +1 -0
index efb09de4343d..b173c36bcab3 100644
@@ -2100,14 +2100,20 @@ static long ocfs2_fallocate(struct file *file, int mode, loff_t offset,
 	struct ocfs2_space_resv sr;
 	int change_size = 1;
 	int cmd = OCFS2_IOC_RESVSP64;
+	int ret = 0;
 
Quoted 5 lines

fs/ocfs2/file.c static long ocfs2_fallocate(struct file *file, int mode, loff_t offset, · line 2106

Quoted 2 lines
fs/ocfs2/file.c +0 -0
 	if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
 		return -EOPNOTSUPP;

This means we only support keep-size and pouch_hole. And it seems pouch_hole will also imply keep-size.

I think you're forgetting about mode = 0, which is also valid. And the default '0' will allow size to be changed.

Quoted 9 lines

fs/ocfs2/file.c static long ocfs2_fallocate(struct file *file, int mode, loff_t offset, · line 2112

Quoted 7 lines
fs/ocfs2/file.c +2 -1
 	if (!ocfs2_writes_unwritten_extents(osb))
 		return -EOPNOTSUPP;
 
-	if (mode & FALLOC_FL_KEEP_SIZE)
+	if (mode & FALLOC_FL_KEEP_SIZE) {
 		change_size = 0;
+	} else {

Seems this will be a dead branch?

Again, you need to consider '0' as a valid mode value. If you run generic/228 without this patch you'll see that test failing because it *does* hit this branch.

Cheers,

Signature · 1 line
Luís
Quoted 11 lines

Thanks,
Joseph

Quoted 7 lines
fs/ocfs2/file.c +4 -0
+		ret = inode_newsize_ok(inode, offset + len);
+		if (ret)
+			return ret;
+	}
 
 	if (mode & FALLOC_FL_PUNCH_HOLE)
 		cmd = OCFS2_IOC_UNRESVSP64;
maintainer
#4 lore raw
Quoted 23 lines · Luís Henriques
Quoted 21 lines · Joseph Qi
Quoted 20 lines · Luís Henriques

When changing a file size with fallocate() the new size isn't being checked. In particular, the FSIZE ulimit isn't being checked, which makes fstest generic/228 fail. Simply adding a call to inode_newsize_ok() fixes this issue.

---
 fs/ocfs2/file.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
fs/ocfs2/file.c +1 -0
index efb09de4343d..b173c36bcab3 100644
@@ -2100,14 +2100,20 @@ static long ocfs2_fallocate(struct file *file, int mode, loff_t offset,
 	struct ocfs2_space_resv sr;
 	int change_size = 1;
 	int cmd = OCFS2_IOC_RESVSP64;
+	int ret = 0;
 
Quoted 9 lines
Quoted 5 lines

fs/ocfs2/file.c static long ocfs2_fallocate(struct file *file, int mode, loff_t offset, · line 2106

Quoted 2 lines
fs/ocfs2/file.c +0 -0
 	if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
 		return -EOPNOTSUPP;

This means we only support keep-size and pouch_hole. And it seems pouch_hole will also imply keep-size.

I think you're forgetting about mode = 0, which is also valid. And the default '0' will allow size to be changed.

Oops... You are right.

Quoted 15 lines
Quoted 9 lines

fs/ocfs2/file.c static long ocfs2_fallocate(struct file *file, int mode, loff_t offset, · line 2112

Quoted 7 lines
fs/ocfs2/file.c +2 -1
 	if (!ocfs2_writes_unwritten_extents(osb))
 		return -EOPNOTSUPP;
 
-	if (mode & FALLOC_FL_KEEP_SIZE)
+	if (mode & FALLOC_FL_KEEP_SIZE) {
 		change_size = 0;
+	} else {

Seems this will be a dead branch?

Again, you need to consider '0' as a valid mode value. If you run generic/228 without this patch you'll see that test failing because it *does* hit this branch.

Cheers,

maintainer Reviewed-by on v1 1/1
#5 lore raw
In reply to Luís Henriques
Quoted 6 lines · Luís Henriques

When changing a file size with fallocate() the new size isn't being checked. In particular, the FSIZE ulimit isn't being checked, which makes fstest generic/228 fail. Simply adding a call to inode_newsize_ok() fixes this issue.

Quoted 20 lines
---
 fs/ocfs2/file.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)
fs/ocfs2/file.c +1 -1
index efb09de4343d..b173c36bcab3 100644
@@ -2100,14 +2100,20 @@ static long ocfs2_fallocate(struct file *file, int mode, loff_t offset,
 	struct ocfs2_space_resv sr;
 	int change_size = 1;
 	int cmd = OCFS2_IOC_RESVSP64;
+	int ret = 0;
 
 	if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
 		return -EOPNOTSUPP;
 	if (!ocfs2_writes_unwritten_extents(osb))
 		return -EOPNOTSUPP;
 
-	if (mode & FALLOC_FL_KEEP_SIZE)

fs/ocfs2/file.c static long ocfs2_fallocate(struct file *file, int mode, loff_t offset, · line 2119

Quoted 10 lines
fs/ocfs2/file.c +6 -0
+	if (mode & FALLOC_FL_KEEP_SIZE) {
 		change_size = 0;
+	} else {
+		ret = inode_newsize_ok(inode, offset + len);
+		if (ret)
+			return ret;
+	}
 
 	if (mode & FALLOC_FL_PUNCH_HOLE)
 		cmd = OCFS2_IOC_UNRESVSP64;

_______________________________________________
Ocfs2-devel mailing list
[email protected]
https://oss.oracle.com/mailman/listinfo/ocfs2-devel

#6 lore raw
In reply to Luís Henriques
Quoted 19 lines · On Mon, 29 May 2023 16:26:45 +0100 Luís Henriques via Ocfs2-devel…

When changing a file size with fallocate() the new size isn't being checked. In particular, the FSIZE ulimit isn't being checked, which makes fstest generic/228 fail. Simply adding a call to inode_newsize_ok() fixes this issue.

...

fs/ocfs2/file.c +1 -0
@@ -2100,14 +2100,20 @@ static long ocfs2_fallocate(struct file *file, int mode, loff_t offset,
 	struct ocfs2_space_resv sr;
 	int change_size = 1;
 	int cmd = OCFS2_IOC_RESVSP64;
+	int ret = 0;
 
 	if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
 		return -EOPNOTSUPP;
 	if (!ocfs2_writes_unwritten_extents(osb))
 		return -EOPNOTSUPP;

fs/ocfs2/file.c static long ocfs2_fallocate(struct file *file, int mode, loff_t offset, · line 2117

Quoted 10 lines
fs/ocfs2/file.c +6 -1
 
-	if (mode & FALLOC_FL_KEEP_SIZE)
+	if (mode & FALLOC_FL_KEEP_SIZE) {
 		change_size = 0;
+	} else {
+		ret = inode_newsize_ok(inode, offset + len);
+		if (ret)
+			return ret;
+	}
 

So userspace can exceed rlimit(RLIMIT_FSIZE).

Do we think this flaw is serious enough to justify backporting the fix into earlier -stable kernels?

_______________________________________________
Ocfs2-devel mailing list
[email protected]
https://oss.oracle.com/mailman/listinfo/ocfs2-devel

maintainer
#7 lore raw
Quoted 34 lines · Andrew Morton
Quoted 19 lines · On Mon, 29 May 2023 16:26:45 +0100 Luís Henriques via Ocfs2-devel…

When changing a file size with fallocate() the new size isn't being checked. In particular, the FSIZE ulimit isn't being checked, which makes fstest generic/228 fail. Simply adding a call to inode_newsize_ok() fixes this issue.

...

fs/ocfs2/file.c +1 -0
@@ -2100,14 +2100,20 @@ static long ocfs2_fallocate(struct file *file, int mode, loff_t offset,
 	struct ocfs2_space_resv sr;
 	int change_size = 1;
 	int cmd = OCFS2_IOC_RESVSP64;
+	int ret = 0;
 
 	if (mode & ~(FALLOC_FL_KEEP_SIZE | FALLOC_FL_PUNCH_HOLE))
 		return -EOPNOTSUPP;
 	if (!ocfs2_writes_unwritten_extents(osb))
 		return -EOPNOTSUPP;

fs/ocfs2/file.c static long ocfs2_fallocate(struct file *file, int mode, loff_t offset, · line 2117

Quoted 10 lines
fs/ocfs2/file.c +6 -1
 
-	if (mode & FALLOC_FL_KEEP_SIZE)
+	if (mode & FALLOC_FL_KEEP_SIZE) {
 		change_size = 0;
+	} else {
+		ret = inode_newsize_ok(inode, offset + len);
+		if (ret)
+			return ret;
+	}
 

So userspace can exceed rlimit(RLIMIT_FSIZE).

Quoted 2 lines

Do we think this flaw is serious enough to justify backporting the fix into earlier -stable kernels?

I think it's worth ccing stable kernel as well.

Thanks,
Joseph

_______________________________________________
Ocfs2-devel mailing list
[email protected]
https://oss.oracle.com/mailman/listinfo/ocfs2-devel