Skip to content

Commit 7c02250

Browse files
Yuto Ohnukibrauner
authored andcommitted
fs: improve dump_inode() to safely access inode fields
Use get_kernel_nofault() to safely access inode and related structures (superblock, file_system_type) to avoid crashing when the inode pointer is invalid. This allows the same pattern as dump_mapping(). Note: The original access method for i_state and i_count is preserved, as get_kernel_nofault() is unnecessary once the inode structure is verified accessible. Reviewed-by: Jan Kara <jack@suse.cz> Signed-off-by: Yuto Ohnuki <ytohnuki@amazon.com> Link: https://patch.msgid.link/20260112181443.81286-1-ytohnuki@amazon.com Reviewed-by: Mateusz Guzik <mjguzik@gmail.com> Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent 58ecde9 commit 7c02250

1 file changed

Lines changed: 34 additions & 13 deletions

File tree

fs/inode.c

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2984,24 +2984,45 @@ umode_t mode_strip_sgid(struct mnt_idmap *idmap,
29842984
EXPORT_SYMBOL(mode_strip_sgid);
29852985

29862986
#ifdef CONFIG_DEBUG_VFS
2987-
/*
2988-
* Dump an inode.
2989-
*
2990-
* TODO: add a proper inode dumping routine, this is a stub to get debug off the
2991-
* ground.
2987+
/**
2988+
* dump_inode - dump an inode.
2989+
* @inode: inode to dump
2990+
* @reason: reason for dumping
29922991
*
2993-
* TODO: handle getting to fs type with get_kernel_nofault()?
2994-
* See dump_mapping() above.
2992+
* If inode is an invalid pointer, we don't want to crash accessing it,
2993+
* so probe everything depending on it carefully with get_kernel_nofault().
29952994
*/
29962995
void dump_inode(struct inode *inode, const char *reason)
29972996
{
2998-
struct super_block *sb = inode->i_sb;
2997+
struct super_block *sb;
2998+
struct file_system_type *s_type;
2999+
const char *fs_name_ptr;
3000+
char fs_name[32] = {};
3001+
umode_t mode;
3002+
unsigned short opflags;
3003+
unsigned int flags;
3004+
unsigned int state;
3005+
int count;
3006+
3007+
if (get_kernel_nofault(sb, &inode->i_sb) ||
3008+
get_kernel_nofault(mode, &inode->i_mode) ||
3009+
get_kernel_nofault(opflags, &inode->i_opflags) ||
3010+
get_kernel_nofault(flags, &inode->i_flags)) {
3011+
pr_warn("%s: unreadable inode:%px\n", reason, inode);
3012+
return;
3013+
}
29993014

3000-
pr_warn("%s encountered for inode %px\n"
3001-
"fs %s mode %ho opflags 0x%hx flags 0x%x state 0x%x count %d\n",
3002-
reason, inode, sb->s_type->name, inode->i_mode, inode->i_opflags,
3003-
inode->i_flags, inode_state_read_once(inode), atomic_read(&inode->i_count));
3004-
}
3015+
state = inode_state_read_once(inode);
3016+
count = atomic_read(&inode->i_count);
30053017

3018+
if (!sb ||
3019+
get_kernel_nofault(s_type, &sb->s_type) || !s_type ||
3020+
get_kernel_nofault(fs_name_ptr, &s_type->name) || !fs_name_ptr ||
3021+
strncpy_from_kernel_nofault(fs_name, fs_name_ptr, sizeof(fs_name) - 1) < 0)
3022+
strscpy(fs_name, "<unknown, sb unreadable>");
3023+
3024+
pr_warn("%s: inode:%px fs:%s mode:%ho opflags:%#x flags:%#x state:%#x count:%d\n",
3025+
reason, inode, fs_name, mode, opflags, flags, state, count);
3026+
}
30063027
EXPORT_SYMBOL(dump_inode);
30073028
#endif

0 commit comments

Comments
 (0)