From: Penglei Jiang <superman.xpt@gmail.com>
To: mjguzik@gmail.com
Cc: brauner@kernel.org, jack@suse.cz, linux-fsdevel@vger.kernel.org,
linux-kernel@vger.kernel.org, superman.xpt@gmail.com,
syzbot+5d8e79d323a13aa0b248@syzkaller.appspotmail.com,
viro@zeniv.linux.org.uk
Subject: [PATCH v2] vfs: Fix anon_inode triggering VFS_BUG_ON_INODE in may_open()
Date: Thu, 3 Apr 2025 20:29:38 -0700 [thread overview]
Message-ID: <20250404032938.76632-1-superman.xpt@gmail.com> (raw)
In-Reply-To: <iufbqsvdp52sgpsjkyulfqgfpvksev3guds5hd556q7zxestgq@ixog46pumnry>
Some anon_inodes do not set the S_IFLNK, S_IFDIR, S_IFBLK, S_IFCHR,
S_IFIFO, S_IFSOCK, or S_IFREG flags after initialization. As a result,
opening these files triggers VFS_BUG_ON_INODE in the may_open() function.
Here is the relevant code snippet:
static int may_open(struct mnt_idmap *idmap, const struct path *path,
int acc_mode, int flag)
{
...
switch (inode->i_mode & S_IFMT) {
case S_IFLNK:
case S_IFDIR:
case S_IFBLK:
case S_IFCHR:
case S_IFIFO:
case S_IFSOCK:
case S_IFREG:
default:
VFS_BUG_ON_INODE(1, inode);
~~~~~~~~~~~~~~~~~~~~~~~~~~
}
...
}
To address this, we modify the code so that only non-anon_inodes trigger
VFS_BUG_ON_INODE, and we also check MAY_EXEC.
To determine if an inode is an anon_inode, we consider two cases:
1. If the inode is the same as anon_inode_inode, it is the default
anon_inode.
2. Anonymous inodes created with alloc_anon_inode() set the S_PRIVATE
flag. If S_PRIVATE is set, we consider it an anonymous inode.
The bug can be reproduced with the following code:
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/timerfd.h>
int main(int argc, char **argv) {
int fd = timerfd_create(CLOCK_MONOTONIC, 0);
if (fd != -1) {
char path[256];
sprintf(path, "/proc/self/fd/%d", fd);
open(path, O_RDONLY);
}
return 0;
}
Finally, after testing, anon_inodes no longer trigger VFS_BUG_ON_INODE.
Fixes: af153bb63a336 ("vfs: catch invalid modes in may_open()")
Reported-by: syzbot+5d8e79d323a13aa0b248@syzkaller.appspotmail.com
Closes: https://lore.kernel.org/all/67ed3fb3.050a0220.14623d.0009.GAE@google.com"
Signed-off-by: Penglei Jiang <superman.xpt@gmail.com>
---
V1 -> V2: added MAY_EXEC check, added some comments
fs/anon_inodes.c | 12 ++++++++++++
fs/namei.c | 8 +++++++-
include/linux/anon_inodes.h | 1 +
3 files changed, 20 insertions(+), 1 deletion(-)
diff --git a/fs/anon_inodes.c b/fs/anon_inodes.c
index 583ac81669c2..bf124d53973e 100644
--- a/fs/anon_inodes.c
+++ b/fs/anon_inodes.c
@@ -303,6 +303,18 @@ int anon_inode_create_getfd(const char *name, const struct file_operations *fops
return __anon_inode_getfd(name, fops, priv, flags, context_inode, true);
}
+/**
+ * is_default_anon_inode - Checks if the given inode is the default
+ * anonymous inode (anon_inode_inode)
+ *
+ * @inode: [in] the inode to be checked
+ *
+ * Returns true if the given inode is anon_inode_inode, otherwise returns false.
+ */
+inline bool is_default_anon_inode(const struct inode *inode)
+{
+ return anon_inode_inode == inode;
+}
static int __init anon_inode_init(void)
{
diff --git a/fs/namei.c b/fs/namei.c
index 360a86ca1f02..e8cc00a7c31a 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -40,6 +40,7 @@
#include <linux/bitops.h>
#include <linux/init_task.h>
#include <linux/uaccess.h>
+#include <linux/anon_inodes.h>
#include "internal.h"
#include "mount.h"
@@ -3429,7 +3430,12 @@ static int may_open(struct mnt_idmap *idmap, const struct path *path,
return -EACCES;
break;
default:
- VFS_BUG_ON_INODE(1, inode);
+ if (!is_default_anon_inode(inode)
+ && !(inode->i_flags & S_PRIVATE))
+ VFS_BUG_ON_INODE(1, inode);
+ if (acc_mode & MAY_EXEC)
+ return -EACCES;
+ break;
}
error = inode_permission(idmap, inode, MAY_OPEN | acc_mode);
diff --git a/include/linux/anon_inodes.h b/include/linux/anon_inodes.h
index edef565c2a1a..eca4a3913ba7 100644
--- a/include/linux/anon_inodes.h
+++ b/include/linux/anon_inodes.h
@@ -30,6 +30,7 @@ int anon_inode_create_getfd(const char *name,
const struct file_operations *fops,
void *priv, int flags,
const struct inode *context_inode);
+bool is_default_anon_inode(const struct inode *inode);
#endif /* _LINUX_ANON_INODES_H */
--
2.17.1
next prev parent reply other threads:[~2025-04-04 3:30 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-04-03 1:59 [PATCH 2/2] " Penglei Jiang
2025-04-03 2:26 ` Mateusz Guzik
2025-04-04 3:29 ` Penglei Jiang [this message]
2025-04-04 5:14 ` [PATCH v2] " Mateusz Guzik
2025-04-04 8:31 ` [PATCH 2/2] " Christoph Hellwig
2025-04-04 8:45 ` Christian Brauner
2025-04-04 8:47 ` Christoph Hellwig
2025-04-04 9:09 ` Christian Brauner
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=20250404032938.76632-1-superman.xpt@gmail.com \
--to=superman.xpt@gmail.com \
--cc=brauner@kernel.org \
--cc=jack@suse.cz \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=mjguzik@gmail.com \
--cc=syzbot+5d8e79d323a13aa0b248@syzkaller.appspotmail.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®