From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1794064540; b=LOba/HztUpJ2I1hj461DbzQG4vvOe0xnaJ4iLu8rROesXLMiJQXe/iC8kzac9un2bkHZ L4aWqINFXp4zJzlDrjLqQ/9H7DHsx2S10lmBV2MlW3KhVGRwiJb1tu1NxjDlbp5fhWgID QMH4iZ7nmvCViKqPKYUq7BfzcjawWzqCo089lcjIxfFIFzD98p8T3CrCTkieDPg6hF1gQ m9fVLXcKjniy4+LGKMS7GmI0ZuDA6uYSHa3QIaE8Cc77eGz/GURjIvkY7pRBYIytrNhvd 2ZaUfjz14ELxMC+1QFKlMpFOq6DHGu//PAlY/BYOsLZSsiXn1/H6GiVmu17BoJqi90g== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1794064540; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:User-Agent:X-stable:X-Patchwork-Hint: MIME-Version:Content-Transfer-Encoding; bh=Po6xxb/BWT2BMu0D1/oc3fY9xnMO1usUoX46IRDImtc=; b=Mld0S0Tg3SFP58E5hINBV2slN70q/9GaQJbgbst7DBoRfwHQg4op1cPTdOLIimiLKaWs v8rCBAtCkbKATqCGwKKDOIARlTLvJ2fD/rkJns8s6PF2rMAP/NxzIAvFbT3oxbjPrelLB vNkEIr0s40ieVG+FdWq1LmCGZrRPzR0gPv2Xp4xb4saBNhEQw2Y1GUgTxDTwmW7ROK5+i rejfzbRF3ayDGWh/5FOXMORKECUB26owNt6Vf+NhuTlQapFFNWu7Q1xvGwsZVDpdc5tVy E/EkyhPVPdg9j/D4M1cDDF3ZU74iSsMgDctWhS9+zkivXlxfcHQjHFKHkY3DDkm3XcA== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=tI57Cs0M; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 965FE606B8A6; Sat, 7 Nov 2026 15:15:40 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linuxfoundation.org header.i=@linuxfoundation.org header.a=rsa-sha256 header.s=korg header.b=tI57Cs0M Received: by smtp.kernel.org (Postfix) with ESMTPSA id EED4D1F0089F; Sat, 7 Nov 2026 15:15:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1794064540; bh=Po6xxb/BWT2BMu0D1/oc3fY9xnMO1usUoX46IRDImtc=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=tI57Cs0M1gh19Npe9R7de9pzS20J3HKP0hpoNCgZKUgnMrrNDAdnAnCxDHzSZwf07 tHD6qTY4+oEc2h+5qTRRO/Nje0uN7V2xYTzysRsWhLMte895DWRlD7IQtP6cGPAgkV PX8KeS9OGUQ9VqhiKVWbB+qD4YankTEYbca0I7Es= From: Greg Kroah-Hartman To: stable@vger.kernel.org Cc: Greg Kroah-Hartman , patches@lists.linux.dev, Jake Labelle , Ryusuke Konishi , Viacheslav Dubeyko , Sasha Levin Subject: [PATCH 7.3 0567/1257] nilfs2: validate directory position after llseek in readdir Date: Sat, 7 Nov 2026 08:47:43 -0500 Message-ID: <20261107134955.024635460@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261107134939.434104957@linuxfoundation.org> References: <20261107134939.434104957@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: stable@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 7.3-stable review patch. If anyone has any objections, please let me know. ------------------ From: Jake Labelle [ Upstream commit de7f49c12566cc5c7437a2f26c6c754b9d4d5648 ] nilfs_readdir() uses ctx->pos, which userspace can set to an arbitrary value via llseek, as an offset into the directory folio and parses whatever bytes sit there as a directory entry. A position pointing into the middle of an entry -- or at unused bytes of a corrupted image that the page validator never inspected -- makes dir_emit() copy up to 255 (name_len) bytes from a bogus entry, possibly reading past the end of the folio and returning unrelated memory contents to userspace. Snap unaligned positions onto a valid entry boundary by walking the rec_len chain from the chunk start before parsing, as ext2 does in ext2_validate_entry(). [ryusuke: fixed offset wrap-around in nilfs_validate_entry() and conformed AI tag to guidelines] Fixes: 2ba466d74ed7 ("nilfs2: directory entry operations") Assisted-by: Claude:claude-mythos-5 Signed-off-by: Jake Labelle Cc: stable@vger.kernel.org Signed-off-by: Ryusuke Konishi Signed-off-by: Viacheslav Dubeyko Signed-off-by: Sasha Levin --- fs/nilfs2/dir.c | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/fs/nilfs2/dir.c b/fs/nilfs2/dir.c index 8b53802b6ebd7..7686ca9228fc8 100644 --- a/fs/nilfs2/dir.c +++ b/fs/nilfs2/dir.c @@ -231,11 +231,27 @@ static struct nilfs_dir_entry *nilfs_next_entry(struct nilfs_dir_entry *p) nilfs_rec_len_from_disk(p->rec_len)); } +static inline unsigned int +nilfs_validate_entry(char *base, unsigned int offset, unsigned int mask) +{ + struct nilfs_dir_entry *de = (struct nilfs_dir_entry *)(base + offset); + struct nilfs_dir_entry *p = + (struct nilfs_dir_entry *)(base + (offset & mask)); + + while ((char *)p < (char *)de) { + if (p->rec_len == 0) + break; + p = nilfs_next_entry(p); + } + return (char *)p - base; +} + static int nilfs_readdir(struct file *file, struct dir_context *ctx) { loff_t pos = ctx->pos; struct inode *inode = file_inode(file); struct super_block *sb = inode->i_sb; + unsigned int chunk_size = nilfs_chunk_size(inode); unsigned int offset = pos & ~PAGE_MASK; unsigned long n = pos >> PAGE_SHIFT; unsigned long npages = dir_pages(inode); @@ -254,6 +270,24 @@ static int nilfs_readdir(struct file *file, struct dir_context *ctx) ctx->pos += PAGE_SIZE - offset; return -EIO; } + /* + * ctx->pos comes from userspace via llseek and may point + * into the middle of an entry -- or at unvalidated bytes + * of a crafted image. offset is only nonzero here on the + * very first iteration (subsequent pages always start at + * offset 0, which -- like any chunk boundary -- is always + * a legitimate entry start, since no rec_len chain crosses + * a chunk boundary). Snap a non-chunk-aligned offset onto + * a real entry boundary by walking the chain from the + * enclosing chunk's start, as ext2 does; otherwise + * dir_emit() copies name_len bytes from a fake entry, + * reading past the end of the folio. + */ + if (offset & (chunk_size - 1)) { + offset = nilfs_validate_entry(kaddr, offset, + ~(chunk_size - 1)); + ctx->pos = ((loff_t)n << PAGE_SHIFT) + offset; + } de = (struct nilfs_dir_entry *)(kaddr + offset); limit = kaddr + nilfs_last_byte(inode, n) - NILFS_DIR_REC_LEN(1); -- 2.53.0