Skip to contentSkip to conversation

nilfs2: fix kernel bug on rename operation of broken directory

PATCH In review Ryusuke Konishi 2 participants 2 messages Jul 15, 2024 – Jul 16, 2024 Stable branch

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nilfs2: fix kernel bug on rename operation of broken directory

commit a9e1ddc09ca55746079cc479aa3eb6411f0d99d4 upstream.

Syzbot reported that in rename directory operation on broken directory on nilfs2, __block_write_begin_int() called to prepare block write may fail BUG_ON check for access exceeding the folio/page size.

This is because nilfs_dotdot(), which gets parent directory reference entry ("..") of the directory to be moved or renamed, does not check consistency enough, and may return location exceeding folio/page size for broken directories.

Fix this issue by checking required directory entries ("." and "..") in the first chunk of the directory in nilfs_dotdot().

---
Please apply this patch to the stable trees indicated by the subject prefix instead of the patch that failed.

This patch is tailored to take page/folio conversion into account and can be applied to these stable trees.

Also, all the builds and tests I did on each stable tree passed.

Thanks,
Ryusuke Konishi

 fs/nilfs2/dir.c | 32 ++++++++++++++++++++++++++++++--
 1 file changed, 30 insertions(+), 2 deletions(-)
fs/nilfs2/dir.c +30 -2
index 51c982ad9608..53e4e63c607e 100644
@@ -396,11 +396,39 @@ nilfs_find_entry(struct inode *dir, const struct qstr *qstr,
 
 struct nilfs_dir_entry *nilfs_dotdot(struct inode *dir, struct page **p)
 {
-	struct nilfs_dir_entry *de = nilfs_get_page(dir, 0, p);
+	struct page *page;
+	struct nilfs_dir_entry *de, *next_de;
+	size_t limit;
+	char *msg;
 
+	de = nilfs_get_page(dir, 0, &page);
 	if (IS_ERR(de))
 		return NULL;
-	return nilfs_next_entry(de);
+
+	limit = nilfs_last_byte(dir, 0);  /* is a multiple of chunk size */
+	if (unlikely(!limit || le64_to_cpu(de->inode) != dir->i_ino ||
+		     !nilfs_match(1, ".", de))) {
+		msg = "missing '.'";
+		goto fail;
+	}
+
+	next_de = nilfs_next_entry(de);
+	/*
+	 * If "next_de" has not reached the end of the chunk, there is
+	 * at least one more record.  Check whether it matches "..".
+	 */
+	if (unlikely((char *)next_de == (char *)de + nilfs_chunk_size(dir) ||
+		     !nilfs_match(2, "..", next_de))) {
+		msg = "missing '..'";
+		goto fail;
+	}
+	*p = page;
+	return next_de;
+
+fail:
+	nilfs_error(dir->i_sb, "directory #%lu %s", dir->i_ino, msg);
+	nilfs_put_page(page);
+	return NULL;
 }
 
 ino_t nilfs_inode_by_name(struct inode *dir, const struct qstr *qstr)
Signature · 1 line
2.43.5
maintainer
#1 lore raw
Quoted 22 lines · Ryusuke Konishi

commit a9e1ddc09ca55746079cc479aa3eb6411f0d99d4 upstream.

Syzbot reported that in rename directory operation on broken directory on nilfs2, __block_write_begin_int() called to prepare block write may fail BUG_ON check for access exceeding the folio/page size.

This is because nilfs_dotdot(), which gets parent directory reference entry ("..") of the directory to be moved or renamed, does not check consistency enough, and may return location exceeding folio/page size for broken directories.

Fix this issue by checking required directory entries ("." and "..") in the first chunk of the directory in nilfs_dotdot().

Quoted 8 lines

---
Please apply this patch to the stable trees indicated by the subject prefix instead of the patch that failed.

This patch is tailored to take page/folio conversion into account and can be applied to these stable trees.

Also, all the builds and tests I did on each stable tree passed.

Now queued up, thanks!

greg k-h