Forwarded: [PATCH] ocfs2: fix use-after-free when reading a bad inode

syzbot posted 1 patch 5 days ago
fs/ocfs2/sysfile.c | 7 +++++++
1 file changed, 7 insertions(+)
Forwarded: [PATCH] ocfs2: fix use-after-free when reading a bad inode
Posted by syzbot 5 days ago
For archival purposes, forwarding an incoming command email to
linux-kernel@vger.kernel.org, syzkaller-bugs@googlegroups.com.

***

Subject: [PATCH] ocfs2: fix use-after-free when reading a bad inode
Author: kartikey406@gmail.com

#syz test: git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git master

When ocfs2_read_locked_inode() fails with -ENOMEM or other errors,
it calls make_bad_inode() to mark the inode as bad but still returns
a valid inode pointer through the iget machinery. The function
_ocfs2_get_system_file_inode() only checks for IS_ERR(inode) but
not for bad inodes, causing the bad inode to be returned to callers.

During orphan recovery, ocfs2_queue_orphans() gets this bad inode
for the orphan directory and passes it to ocfs2_dir_foreach().
Since the inode was never properly initialized due to the earlier
read failure, iterating the directory accesses freed or uninitialized
memory, triggering a use-after-free in ocfs2_check_dir_entry().

Fix this by adding an is_bad_inode() check in _ocfs2_get_system_file_inode()
after ocfs2_iget() returns. If the inode is bad, release it with iput()
and return NULL. This protects all callers of ocfs2_get_system_file_inode()
from receiving bad inodes.

Reported-by: syzbot+c897823f699449cc3eb4@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=c897823f699449cc3eb4
Signed-off-by: Deepanshu Kartikey <kartikey406@gmail.com>
---
 fs/ocfs2/sysfile.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/fs/ocfs2/sysfile.c b/fs/ocfs2/sysfile.c
index d53a6cc866be..f443479f7d3e 100644
--- a/fs/ocfs2/sysfile.c
+++ b/fs/ocfs2/sysfile.c
@@ -145,6 +145,13 @@ static struct inode * _ocfs2_get_system_file_inode(struct ocfs2_super *osb,
 		inode = NULL;
 		goto bail;
 	}
+
+	if(is_bad_inode(inode)) {
+		iput(inode);
+		inode = NULL;
+		goto bail;
+	}
+
 #ifdef CONFIG_DEBUG_LOCK_ALLOC
 	if (type == LOCAL_USER_QUOTA_SYSTEM_INODE ||
 	    type == LOCAL_GROUP_QUOTA_SYSTEM_INODE ||
-- 
2.43.0