vfs: Fix anon_inode triggering VFS_BUG_ON_INODE in may_open()
PATCH 2/2 vfs: Fix anon_inode triggering VFS_BUG_ON_INODE in may_open()
may_open()
{
...
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);
}
...
}Since some anonymous inodes do not have S_IFLNK, S_IFDIR, S_IFBLK, S_IFCHR, S_IFIFO, S_IFSOCK, or S_IFREG flags set when created, they end up in the default case branch.
When creating some anon_inode instances, the i_mode in the switch statement is not properly set, which causes the open operation to follow the default branch when opening anon_inode.
We could check whether the inode is an anon_inode before VFS_BUG_ON_INODE and trigger the assertion only if it's not. However, a more reasonable approach might be to set a flag during creation in alloc_anon_inode(), such as inode->i_flags |= S_ANON, to explicitly mark anonymous inodes.
The code that triggers the BUG:
#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;
}Thank you!
--- fs/anon_inodes.c | 4 ++++ fs/namei.c | 5 ++++- include/linux/anon_inodes.h | 1 + 3 files changed, 9 insertions(+), 1 deletion(-)
fs/anon_inodes.c +4 -0
@@ -303,6 +303,10 @@ int anon_inode_create_getfd(const char *name, const struct file_operations *fopsreturn __anon_inode_getfd (name, fops, priv, flags, context_inode, true); } +inline bool is_default_anon_inode (const struct inode *inode) +{ +return anon_inode_inode == inode; +}static int __init anon_inode_init (void ) {
fs/namei.c +4 -1
@@ -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,9 @@ 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); } error =inode_permission (idmap, inode,MAY_OPEN | acc_mode);
include/linux/anon_inodes.h +1 -0
@@ -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 */
Signature · 1 line
2.17.1
Quoted 21 lines · Penglei Jiang
may_open(){ ... 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); } ... }Since some anonymous inodes do not have
S_IFLNK,S_IFDIR,S_IFBLK,S_IFCHR,S_IFIFO,S_IFSOCK, orS_IFREGflags set when created, they end up in the default case branch.
Quoted 9 lines
When creating some
anon_inodeinstances, thei_modein the switch statement is not properly set, which causes the open operation to follow the default branch when openinganon_inode.We could check whether the inode is an
anon_inodebeforeVFS_BUG_ON_INODEand trigger the assertion only if it's not. However, a more reasonable approach might be to set a flag during creation inalloc_anon_inode(), such asinode->i_flags|=S_ANON, to explicitly mark anonymous inodes.
I think this is the right step, but there is a missing bit.
The assert was added because of a prior bug report involving ntfs, where a misconstructed inode did not have an i_mode and was passed to execve. That eventually landed in may_open() which did not check for MAY_EXEC as the inode did not fit any of the modes. It was only caught by a hardening check later.
Note all other cases handle MAY_EXEC.
So I think the safest way forward has 2 steps in the default case:
- detect inodes which purposefully don't have a valid mode and elide the assert if so, for example like in your patch
- add the permission check:
if (acc_mode & MAY_EXEC) return -EACCES;
The hardening check comes with a WARN_ON if the inode is not S_ISREG.
No need to hope none of the anon inodes reach it, this should just be short-circuited.
PATCH v2 1/1 vfs: Fix anon_inode triggering VFS_BUG_ON_INODE in may_open()
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.
---
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(-)
fs/anon_inodes.c +12 -0
@@ -303,6 +303,18 @@ int anon_inode_create_getfd(const char *name, const struct file_operations *fopsreturn __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 ) {
fs/namei.c +7 -1
@@ -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);
include/linux/anon_inodes.h +1 -0
@@ -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 */
Signature · 1 line
2.17.1
Quoted 77 lines · Penglei Jiang
Some
anon_inodesdo not set theS_IFLNK,S_IFDIR,S_IFBLK,S_IFCHR,S_IFIFO,S_IFSOCK, orS_IFREGflags after initialization. As a result, opening these files triggersVFS_BUG_ON_INODEin themay_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 checkMAY_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 defaultanon_inode.
2. Anonymous inodes created withalloc_anon_inode()set theS_PRIVATEflag. IfS_PRIVATEis 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_inodesno longer triggerVFS_BUG_ON_INODE.Fixes af153bb63a336 ("vfs: catch invalid modes in may_open()")Reported-by [email protected]Signed-off-by Penglei Jiang <[email protected]>---
V1 -> V2: addedMAY_EXECcheck, added some commentsfs/anon_inodes.c | 12 ++++++++++++ fs/namei.c | 8 +++++++- include/linux/anon_inodes.h | 1 + 3 files changed, 20 insertions(+), 1 deletion(-)fs/anon_inodes.c +3
-0@@ -303,6 +303,18 @@ int anon_inode_create_getfd(const char *name, const struct file_operations *fopsreturn __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)
fs/anon_inodes.c int anon_inode_create_getfd(const char *name, const struct file_operations *fops · line 318
Quoted 10 lines
fs/anon_inodes.c +9
-0+* +* @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; +}
I would drop the inline.
Quoted 16 lines
fs/anon_inodes.c +0
-0static int __init anon_inode_init (void ) {fs/namei.c +1
-0@@ -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 ;
fs/namei.c static int may_open(struct mnt_idmap *idmap, const struct path *path, · line 3439
Quoted 10 lines
fs/namei.c +6
-1break ;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 ; }
Semantically this looks ok to me.
It may be this happens to still be too restrictive, but then we are erroring on the side of some test suite blowing up, as opposed to something in production, so I think it's fine.
I would fold these conditions into the debug macro though, as in:VFS_BUG_ON_INODE(!is_default_anon_inode(inode) && !(inode->i_flags & S_PRIVATE)), inode);
Quoted 17 lines
fs/namei.c +0
-0error =inode_permission (idmap, inode,MAY_OPEN | acc_mode);include/linux/anon_inodes.h +1
-1@@ -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
Signature · 1 line
Mateusz Guzik <mjguzik gmail.com>
Please make sure anon inodes have a valid mode set. Leaving this landmine around where we have inodes without a valid mode is just going to create more problems in the future.
Christian Brauner
maintainerQuoted 3 lines · Christoph Hellwig
Please make sure anon inodes have a valid mode set. Leaving this landmine around where we have inodes without a valid mode is just going to create more problems in the future.
We've tried to change it, it immediately leads to userspace tools regressions as they've started to rely on that behavior because it's been like that since the dawn of time. We even had to work around that crap in pidfs because lsof regressed. So now, we can't do this.
Quoted 1 line
Quoted 9 lines · Christian Brauner
Quoted 3 lines · Christoph Hellwig
Please make sure anon inodes have a valid mode set. Leaving this landmine around where we have inodes without a valid mode is just going to create more problems in the future.
We've tried to change it, it immediately leads to userspace tools regressions as they've started to rely on that behavior because it's been like that since the dawn of time. We even had to work around that crap in pidfs because lsof regressed. So now, we can't do this.
Just because i_mode has something useful, we don't need to report that to userspace. We can still clear kstat.mode (with a big fat comment explaining why).
Christian Brauner
maintainerQuoted 6 lines · Christoph Hellwig
Quoted 5 lines · Christian Brauner
Quoted 3 lines · Christoph Hellwig
Please make sure anon inodes have a valid mode set. Leaving this landmine around where we have inodes without a valid mode is just going to create more problems in the future.
Quoted 8 lines
Quoted 4 lines
We've tried to change it, it immediately leads to userspace tools regressions as they've started to rely on that behavior because it's been like that since the dawn of time. We even had to work around that crap in pidfs because lsof regressed. So now, we can't do this.
Just because
i_modehas something useful, we don't need to report that to userspace. We can still clear kstat.mode (with a big fat comment explaining why).
Yes, that's what we do in pidfs. I can do this for anon_inode as well.