Skip to contentSkip to conversation

fs: clamp FIONREAD result to int range

PATCH Applied Avi Weiss 3 participants 3 messages Oct 31 – Nov 5

Subsystems (3)
lore
Share & tools
lore

Avi Weiss

author
#0 lore raw

PATCH 1/1 fs: clamp FIONREAD result to int range

FIONREAD returns the difference between the file size and the current position in an int. When the difference is outside the int range, the result wraps, so a file with 4 GiB remaining can be reported as empty.

Clamp the result to the signed int range before returning it to userspace. Preserve negative counts for file positions past EOF.

---
 fs/ioctl.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/ioctl.c +3 -2
index 1c152c2b1b67d..ecc2a4cfe6100 100644
@@ -7,6 +7,7 @@
 
 #include <linux/syscalls.h>
 #include <linux/mm.h>
+#include <linux/minmax.h>
 #include <linux/capability.h>
 #include <linux/compat.h>
 #include <linux/file.h>
@@ -550,8 +551,8 @@ static int do_vfs_ioctl(struct file *filp, unsigned int fd,
 		if (!S_ISREG(inode->i_mode) || IS_ANON_FILE(inode))
 			return vfs_ioctl(filp, cmd, arg);
 
-		return put_user(i_size_read(inode) - filp->f_pos,
-				(int __user *)argp);
+		return put_user(clamp_t(loff_t, i_size_read(inode) - filp->f_pos,
+					INT_MIN, INT_MAX), (int __user *)argp);
 
 	case FS_IOC_GETFLAGS:
 		return ioctl_getflags(filp, argp);

base-commit: 36aad21d50d1af3f6954bb0c988257a908202477

Signature · 1 line
2.43.0
reviewer Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Avi Weiss

FIONREAD returns the difference between the file size and the current position in an int. When the difference is outside the int range, the result wraps, so a file with 4 GiB remaining can be reported as empty.

Clamp the result to the signed int range before returning it to userspace. Preserve negative counts for file positions past EOF.

Fair enough. Feel free to add:

Honza

Quoted 32 lines
---
 fs/ioctl.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/ioctl.c +3 -2
index 1c152c2b1b67d..ecc2a4cfe6100 100644
@@ -7,6 +7,7 @@
 
 #include <linux/syscalls.h>
 #include <linux/mm.h>
+#include <linux/minmax.h>
 #include <linux/capability.h>
 #include <linux/compat.h>
 #include <linux/file.h>
@@ -550,8 +551,8 @@ static int do_vfs_ioctl(struct file *filp, unsigned int fd,
 		if (!S_ISREG(inode->i_mode) || IS_ANON_FILE(inode))
 			return vfs_ioctl(filp, cmd, arg);
 
-		return put_user(i_size_read(inode) - filp->f_pos,
-				(int __user *)argp);
+		return put_user(clamp_t(loff_t, i_size_read(inode) - filp->f_pos,
+					INT_MIN, INT_MAX), (int __user *)argp);
 
 	case FS_IOC_GETFLAGS:
 		return ioctl_getflags(filp, argp);

base-commit: 36aad21d50d1af3f6954bb0c988257a908202477

Signature · 1 line
2.43.0
Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer Applied
#2 lore raw
In reply to Avi Weiss
Quoted 8 lines · Avi Weiss

FIONREAD returns the difference between the file size and the current position in an int. When the difference is outside the int range, the result wraps, so a file with 4 GiB remaining can be reported as empty.

Clamp the result to the signed int range before returning it to userspace. Preserve negative counts for file positions past EOF.

[...]

Applied to the vfs.fixes branch of the vfs/vfs.git tree. Patches in the vfs.fixes branch should appear in linux-next soon.

Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.

It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.

Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.

tree:   https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git
branch: vfs.fixes

[1/1] fs: clamp FIONREAD result to int range

      https://git.kernel.org/vfs/vfs/c/f5ced3af5fa1