From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8FF771DDCE; Tue, 16 Jul 2024 15:51:02 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721145062; cv=none; b=nVt37Do09LmBcCRJxmWmD5Tp/RCJqVhhW7Dzp6jAKtC41q9WSR7dZ9ZFBOzezAY1d+AmHeWVgIUC5W8O1H89WJGCE9QYUcq+HVdsD1H9R8cu4FOu0OoQogQV7uhc7KtbTc55+izcqGCkD+4QouSLCT8c3xGp+SVaYw8D14yZkvs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1721145062; c=relaxed/simple; bh=HK6afoSxeumJXzTj/M/OeM+0UmV2W0OdEfpjSy0g5CM=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=aQCa4Faz94WZkr+OsGgHeGI4+vxfvD5XVN3K0NKlahT6vDV5wkuGTzTpH1OtR3EkWJYHFGtWbU6EW+pW7UhlkWfZuL8nCJiEk5cVWd5FetkkMDf/YeGllCW5NVpAOeu/z+zZVH4mYie2V4TfeQXcfmysZJ6sW2YvDK8O3VA2sXM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b=GVC0lPyR; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.b="GVC0lPyR" Received: by smtp.kernel.org (Postfix) with ESMTPSA id B75B9C116B1; Tue, 16 Jul 2024 15:51:01 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1721145062; bh=HK6afoSxeumJXzTj/M/OeM+0UmV2W0OdEfpjSy0g5CM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GVC0lPyRJOSjllcbmM2DMSzkjZJUdp2lZXDE3NxFxNWoNiy3zLWd+I4Wqs3vBBqEO NFBDF+V5io+/uTp86ZfytBZrbY0uUpFtgeZEpVKtCC38sg1o+wGkpMJj/5IBSQiklF N/53ynugoVZhQnqbarZlEYneGHk+1ZsG4wgSlP0Y= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Ryusuke Konishi , syzbot+d3abed1ad3d367fa2627@syzkaller.appspotmail.com, Andrew Morton Subject: [PATCH 6.9 085/143] nilfs2: fix kernel bug on rename operation of broken directory Date: Tue, 16 Jul 2024 17:31:21 +0200 Message-ID: <20240716152759.244223676@linuxfoundation.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240716152755.980289992@linuxfoundation.org> References: <20240716152755.980289992@linuxfoundation.org> User-Agent: quilt/0.67 X-stable: review X-Patchwork-Hint: ignore Precedence: bulk X-Mailing-List: stable@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 6.9-stable review patch. If anyone has any objections, please let me know. ------------------ From: 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(). Link: https://lkml.kernel.org/r/20240628165107.9006-1-konishi.ryusuke@gmail.com Signed-off-by: Ryusuke Konishi Reported-by: syzbot+d3abed1ad3d367fa2627@syzkaller.appspotmail.com Closes: https://syzkaller.appspot.com/bug?extid=d3abed1ad3d367fa2627 Fixes: 2ba466d74ed7 ("nilfs2: directory entry operations") Tested-by: Ryusuke Konishi Cc: Signed-off-by: Andrew Morton Signed-off-by: Greg Kroah-Hartman --- fs/nilfs2/dir.c | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) --- a/fs/nilfs2/dir.c +++ b/fs/nilfs2/dir.c @@ -384,11 +384,39 @@ found: struct nilfs_dir_entry *nilfs_dotdot(struct inode *dir, struct folio **foliop) { - struct nilfs_dir_entry *de = nilfs_get_folio(dir, 0, foliop); + struct folio *folio; + struct nilfs_dir_entry *de, *next_de; + size_t limit; + char *msg; + de = nilfs_get_folio(dir, 0, &folio); 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; + } + *foliop = folio; + return next_de; + +fail: + nilfs_error(dir->i_sb, "directory #%lu %s", dir->i_ino, msg); + folio_release_kmap(folio, de); + return NULL; } ino_t nilfs_inode_by_name(struct inode *dir, const struct qstr *qstr)