Skip to content

Commit d325dc6

Browse files
konisakpm00
authored andcommitted
nilfs2: fix use-after-free bug of struct nilfs_root
If the beginning of the inode bitmap area is corrupted on disk, an inode with the same inode number as the root inode can be allocated and fail soon after. In this case, the subsequent call to nilfs_clear_inode() on that bogus root inode will wrongly decrement the reference counter of struct nilfs_root, and this will erroneously free struct nilfs_root, causing kernel oopses. This fixes the problem by changing nilfs_new_inode() to skip reserved inode numbers while repairing the inode bitmap. Link: https://lkml.kernel.org/r/[email protected] Signed-off-by: Ryusuke Konishi <[email protected]> Reported-by: [email protected] Reported-by: Khalid Masum <[email protected]> Tested-by: Ryusuke Konishi <[email protected]> Cc: <[email protected]> Signed-off-by: Andrew Morton <[email protected]>
1 parent b1f44cd commit d325dc6

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

fs/nilfs2/inode.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,6 +328,7 @@ struct inode *nilfs_new_inode(struct inode *dir, umode_t mode)
328328
struct inode *inode;
329329
struct nilfs_inode_info *ii;
330330
struct nilfs_root *root;
331+
struct buffer_head *bh;
331332
int err = -ENOMEM;
332333
ino_t ino;
333334

@@ -343,11 +344,25 @@ struct inode *nilfs_new_inode(struct inode *dir, umode_t mode)
343344
ii->i_state = BIT(NILFS_I_NEW);
344345
ii->i_root = root;
345346

346-
err = nilfs_ifile_create_inode(root->ifile, &ino, &ii->i_bh);
347+
err = nilfs_ifile_create_inode(root->ifile, &ino, &bh);
347348
if (unlikely(err))
348349
goto failed_ifile_create_inode;
349350
/* reference count of i_bh inherits from nilfs_mdt_read_block() */
350351

352+
if (unlikely(ino < NILFS_USER_INO)) {
353+
nilfs_warn(sb,
354+
"inode bitmap is inconsistent for reserved inodes");
355+
do {
356+
brelse(bh);
357+
err = nilfs_ifile_create_inode(root->ifile, &ino, &bh);
358+
if (unlikely(err))
359+
goto failed_ifile_create_inode;
360+
} while (ino < NILFS_USER_INO);
361+
362+
nilfs_info(sb, "repaired inode bitmap for reserved inodes");
363+
}
364+
ii->i_bh = bh;
365+
351366
atomic64_inc(&root->inodes_count);
352367
inode_init_owner(&init_user_ns, inode, dir, mode);
353368
inode->i_ino = ino;

0 commit comments

Comments
 (0)