From: Siddharth Chintamaneni <sidchintamaneni@gmail.com>
To: Alexander Viro <viro@zeniv.linux.org.uk>,
Christian Brauner <brauner@kernel.org>
Cc: Jan Kara <jack@suse.cz>,
linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org,
apais@microsoft.com, rlmenge@gmail.com,
hargar@linux.microsoft.com,
Siddharth Chintamaneni <sidchintamaneni@gmail.com>
Subject: [PATCH v1 1/2] fs: reject negative offsets for SEEK_DATA and SEEK_HOLE in default_llseek()
Date: Sat, 31 Oct 2026 23:00:48 -0700 [thread overview]
Message-ID: <20261101060049.81441-2-sidchintamaneni@gmail.com> (raw)
In-Reply-To: <20261101060049.81441-1-sidchintamaneni@gmail.com>
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")
Signed-off-by: Siddharth Chintamaneni <sidchintamaneni@gmail.com>
---
fs/read_write.c | 10 ++++++----
1 file changed, 6 insertions(+), 4 deletions(-)
diff --git a/fs/read_write.c b/fs/read_write.c
index 676728310899..86ce8cea10d7 100644
--- a/fs/read_write.c
+++ b/fs/read_write.c
@@ -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;
--
2.50.1 (Apple Git-155)
next prev parent reply other threads:[~2026-11-01 6:00 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-01 6:00 [PATCH v1 0/2] fs: fix and deduplicate whence handling " Siddharth Chintamaneni
2026-11-01 6:00 ` Siddharth Chintamaneni [this message]
2026-11-01 6:00 ` [PATCH v1 2/2] fs: use must_set_pos() " Siddharth Chintamaneni
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261101060049.81441-2-sidchintamaneni@gmail.com \
--to=sidchintamaneni@gmail.com \
--cc=apais@microsoft.com \
--cc=brauner@kernel.org \
--cc=hargar@linux.microsoft.com \
--cc=jack@suse.cz \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=rlmenge@gmail.com \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®