Skip to contentSkip to conversation

fs: fix and deduplicate whence handling in default_llseek()

PATCH Applied Siddharth Chintamaneni 3 participants 5 messages Nov 1 – Nov 5 VFS

lore
Share & tools
lore
#0 lore raw

cover fs: fix and deduplicate whence handling in default_llseek()

This series fixes negative offset validation for SEEK_DATA and SEEK_HOLE in default_llseek() and deduplicates its whence handling with must_set_pos().

Patch 1 updates default_llseek() to read the inode size once via i_size_read(inode) and compare (unsigned long long)offset >= eof for SEEK_DATA and SEEK_HOLE so negative offsets fail with -ENXIO instead of bypassing the range check.

Patch 2 replaces the open-coded switch (whence) block in default_llseek() with a call to must_set_pos().

Siddharth Chintamaneni (2):
fs: reject negative offsets for SEEK_DATA and SEEK_HOLE in
default_llseek()
fs: use must_set_pos() in default_llseek()

 fs/read_write.c | 48 ++++++++++++++----------------------------------
 1 file changed, 14 insertions(+), 34 deletions(-)

base-commit: 36aad21d50d1af3f6954bb0c988257a908202477

Signature · 1 line
2.50.1 (Apple Git-155)
#1 lore raw

PATCH 1/2 fs: reject negative offsets for SEEK_DATA and SEEK_HOLE in default_llseek()

In default_llseek(), the range checks for SEEK_DATA and SEEK_HOLE compare the signed loff_t offset directly against inode->i_size:

	if (offset >= inode->i_size) {
		retval = -ENXIO;
		goto out;
	}

When a negative offset is supplied to SEEK_HOLE (e.g. lseek(fd, -10, SEEK_HOLE) on a sysctl file in /proc/sys), the signed comparison evaluates to false and execution proceeds to:

	offset = inode->i_size;

This overwrites the negative offset with a non-negative value before the final if (offset >= 0 || unsigned_offsets(file)) check, causing default_llseek() to update file->f_pos and return inode->i_size instead of failing with -ENXIO. Similarly, for SEEK_DATA on a file with FOP_UNSIGNED_OFFSET, a negative offset bypasses the range check and succeeds via unsigned_offsets(file).

Read the inode size once via i_size_read(inode) after acquiring the inode lock and compare (unsigned long long)offset >= eof for both SEEK_DATA and SEEK_HOLE, matching must_set_pos().

Fixes 982d816581ee ("fs: add SEEK_HOLE and SEEK_DATA flags")
---
 fs/read_write.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
fs/read_write.c +6 -4
index 676728310899..86ce8cea10d7 100644
@@ -333,13 +333,15 @@ loff_t default_llseek(struct file *file, loff_t offset, int whence)
 {
 	struct inode *inode = file_inode(file);
 	loff_t retval;
+	loff_t eof;
 
 	retval = inode_lock_killable(inode);
 	if (retval)
 		return retval;
+	eof = i_size_read(inode);
 	switch (whence) {
 		case SEEK_END:
-			offset += i_size_read(inode);
+			offset += eof;
 			break;
 		case SEEK_CUR:
 			if (offset == 0) {
@@ -354,7 +356,7 @@ loff_t default_llseek(struct file *file, loff_t offset, int whence)
 			 * long as offset isn't at the end of the file then the
 			 * offset is data.
 			 */
-			if (offset >= inode->i_size) {
+			if ((unsigned long long)offset >= eof) {
 				retval = -ENXIO;
 				goto out;
 			}
@@ -365,11 +367,11 @@ loff_t default_llseek(struct file *file, loff_t offset, int whence)
 			 * as long as offset isn't i_size or larger, return
 			 * i_size.
 			 */
-			if (offset >= inode->i_size) {
+			if ((unsigned long long)offset >= eof) {
 				retval = -ENXIO;
 				goto out;
 			}
-			offset = inode->i_size;
+			offset = eof;
 			break;
 	}
 	retval = -EINVAL;
Signature · 1 line
2.50.1 (Apple Git-155)
#2 lore raw

PATCH 2/2 fs: use must_set_pos() in default_llseek()

In reply to Siddharth Chintamaneni

default_llseek() open-codes the same whence switch for SEEK_END, SEEK_CUR, SEEK_DATA, and SEEK_HOLE that must_set_pos() already implements for generic_file_llseek_size() and generic_llseek_cookie().

Replace the open-coded switch (whence) block in default_llseek() with a call to must_set_pos(file, &offset, whence, i_size_read(inode)).

---
 fs/read_write.c | 50 ++++++++++++++-----------------------------------
 1 file changed, 14 insertions(+), 36 deletions(-)
fs/read_write.c +14 -36
index 86ce8cea10d7..b15522cf96cd 100644
@@ -333,47 +333,25 @@ loff_t default_llseek(struct file *file, loff_t offset, int whence)
 {
 	struct inode *inode = file_inode(file);
 	loff_t retval;
-	loff_t eof;
+	int ret;
 
 	retval = inode_lock_killable(inode);
 	if (retval)
 		return retval;
-	eof = i_size_read(inode);
-	switch (whence) {
-		case SEEK_END:
-			offset += eof;
-			break;
-		case SEEK_CUR:
-			if (offset == 0) {
-				retval = file->f_pos;
-				goto out;
-			}
-			offset += file->f_pos;
-			break;
-		case SEEK_DATA:
-			/*
-			 * In the generic case the entire file is data, so as
-			 * long as offset isn't at the end of the file then the
-			 * offset is data.
-			 */
-			if ((unsigned long long)offset >= eof) {
-				retval = -ENXIO;
-				goto out;
-			}
-			break;
-		case SEEK_HOLE:
-			/*
-			 * There is a virtual hole at the end of the file, so
-			 * as long as offset isn't i_size or larger, return
-			 * i_size.
-			 */
-			if ((unsigned long long)offset >= eof) {
-				retval = -ENXIO;
-				goto out;
-			}
-			offset = eof;
-			break;
+
+	ret = must_set_pos(file, &offset, whence, i_size_read(inode));
+	if (ret < 0) {
+		retval = ret;
+		goto out;
+	}
+	if (ret == 0) {
+		retval = offset;
+		goto out;
 	}
+
+	if (whence == SEEK_CUR)
+		offset += file->f_pos;
+
 	retval = -EINVAL;
 	if (offset >= 0 || unsigned_offsets(file)) {
 		if (offset != file->f_pos)
Signature · 1 line
2.50.1 (Apple Git-155)
reviewer Reviewed-by on the v1 series
#3 lore raw
In reply to Siddharth Chintamaneni
Quoted 9 lines · Siddharth Chintamaneni

This series fixes negative offset validation for SEEK_DATA and SEEK_HOLE in default_llseek() and deduplicates its whence handling with must_set_pos().

Patch 1 updates default_llseek() to read the inode size once via i_size_read(inode) and compare (unsigned long long)offset >= eof for SEEK_DATA and SEEK_HOLE so negative offsets fail with -ENXIO instead of bypassing the range check.

Quoted 7 lines

Patch 2 replaces the open-coded switch (whence) block in default_llseek() with a call to must_set_pos().

Siddharth Chintamaneni (2):
fs: reject negative offsets for SEEK_DATA and SEEK_HOLE in
default_llseek()
fs: use must_set_pos() in default_llseek()

Looks good! Feel free to add:

Honza

Signature · 2 lines
Jan Kara <[email protected]>
SUSE Labs, CR
maintainer Applied
#4 lore raw
In reply to Siddharth Chintamaneni
Quoted 10 lines · Siddharth Chintamaneni

This series fixes negative offset validation for SEEK_DATA and SEEK_HOLE in default_llseek() and deduplicates its whence handling with must_set_pos().

Patch 1 updates default_llseek() to read the inode size once via i_size_read(inode) and compare (unsigned long long)offset >= eof for SEEK_DATA and SEEK_HOLE so negative offsets fail with -ENXIO instead of bypassing the range check.

[...]

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/2] fs: reject negative offsets for SEEK_DATA and SEEK_HOLE in default_llseek()

      https://git.kernel.org/vfs/vfs/c/7a5783cfac71
[2/2] fs: use must_set_pos() in default_llseek()
      https://git.kernel.org/vfs/vfs/c/1284ecd57375