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=1795059974; b=JcgR/4vi1I3z/hOnYjrKvFKCwrq3w2F8Z3KPqg4yt4pEoT0ogzO3bTF+d6G1W2GHUQdi x/JJWX7hVtFcsXBQVwu5h3babkBDMGGEnc1gsSvlLOEtJXepAtiKmyEepe3/BkrK5sw8v Vkex4376Jhg04qQ412VHBWxlf3AXXKnlgltJN+dKFf0J41B86ASp5Oqle4TlOxS9V5fXS cGIQ0Oe8MyXd9xmeYCqpFzkpnkq0qZOO3qjbFnSJfa+59IlYOSAwkqX0ep8i6tQEkoO6b 13GM3SiblCuU1QgQLgMeWwljzzvCDx30YT7GQ3TMEdetmcF/3ja29hyoqvBUw5/09tw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1795059974; 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=uHPT7w+yGbQOzg5pCzx+/LtR8+fGUsh+miZe+mdSTyQ=; b=ugGzGEDyTLiF8f0pI4ztDJW//TQgOm7t3R0hkU54IhucApDCXHdoPG69Rrq3IOXebBNC La24kAY0aziEBVbrp0xuNLDkyTktefhv3rGOI2nKWx55qVU8gIyg8UONHy5eORc8igTO2 xYDY9qhxmawu5J0TO/tNcbKdpFqDmUDt+a5/RMDoJMgQXAT7/yvbhJ8Q0NvQETsh1Z5la 28fnvU03GnOIjCP4VtU4UqZwIkZ1D0ND9oaexeNh5FY6q5biZu24o4MZubNNZyL0xduD7 hRabjlMOQGAGSMNxeYsJdxSiqfr6hcnDg5kL4m6yADSfVIXaLv8KwURQPiBYrhsDgBg== 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=CdqKFwbR; 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 A4D66606A325; Thu, 19 Nov 2026 03:46:14 +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=CdqKFwbR Received: by smtp.kernel.org (Postfix) with ESMTPSA id 06C921F000C6; Thu, 19 Nov 2026 03:46:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=korg; t=1795059974; bh=uHPT7w+yGbQOzg5pCzx+/LtR8+fGUsh+miZe+mdSTyQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=CdqKFwbR274mGk0ks1zkMNYZ8T2RiCz1ZRkbBtCKJsh6umP1Y2/sXtTn4LiZ+7lgZ 9YRo/mE338wiwqEHvfp75KadMzlfIro9o4ntAu4wKyWWcc+TuG3cV6g4RE/HDIPPev 6zSNiiP7X0CLhOGCRXRoUvbwmqpVuqbbs3fWcX1U= 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 5.10 1034/1848] nilfs2: validate directory position after llseek in readdir Date: Wed, 18 Nov 2026 15:51:12 +0100 Message-ID: <20261118150000.657331864@linuxfoundation.org> X-Mailer: git-send-email 2.56.0 In-Reply-To: <20261118145911.150052362@linuxfoundation.org> References: <20261118145911.150052362@linuxfoundation.org> User-Agent: quilt/0.69 X-stable: review X-Patchwork-Hint: ignore X-Mailing-List: patches@lists.linux.dev List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 5.10-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 [ sashal: adjusted helper insertion context to follow nilfs_set_de_type() instead of nilfs_next_entry() ] 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 3d7e692f3e7f1..2ff8f8d3c3d6d 100644 --- a/fs/nilfs2/dir.c +++ b/fs/nilfs2/dir.c @@ -265,11 +265,27 @@ static void nilfs_set_de_type(struct nilfs_dir_entry *de, struct inode *inode) de->file_type = nilfs_type_by_mode[(mode & S_IFMT)>>S_SHIFT]; } +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); @@ -288,6 +304,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