From: Matthias Goergens <matthias.goergens@gmail.com>
To: brauner@kernel.org
Cc: viro@zeniv.linux.org.uk, jack@suse.cz, benquike@gmail.com,
dlemoal@kernel.org, al@alarsen.net,
linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH 2/2] qnx6: avoid undefined shifts in qnx6_block_map()
Date: Fri, 25 Sep 2026 23:14:49 +0800 [thread overview]
Message-ID: <20260925151449.1517608-3-matthias.goergens@gmail.com> (raw)
In-Reply-To: <20260925151449.1517608-1-matthias.goergens@gmail.com>
qnx6_block_map() takes each level's index from the 32-bit block number
by shifting it right by ptrbits * depth. QNX6_PTR_MAX_LEVELS allows
five levels, and ptrbits is 7 for 512 byte blocks and 10 for 4K
blocks, so a depth-5 tree with 512 byte blocks shifts by 35 and a
depth-4 tree with 4K blocks by 40. Such trees are deeper than any
32-bit block number needs, but the driver accepts them, and a shift by
32 or more is undefined. UBSAN reports shift-out-of-bounds, and on
x86, which masks the shift count, files in such trees read back with
the wrong contents; at depth 5 with 512 byte blocks UBSAN also reports
index 16 out of range for di_block_ptr[].
The index bits at those offsets are zero for any 32-bit block number,
so use zero for them explicitly and walk the remaining levels as
before. Casting the block number to u64 would not be enough: with 64K
blocks, which sb_set_blocksize() accepts on 64K-page kernels, ptrbits
is 14 and the shift reaches 70.
Fixes: 5d026c724220 ("fs: initial qnx6fs addition")
Cc: stable@vger.kernel.org
Signed-off-by: Matthias Goergens <matthias.goergens@gmail.com>
---
fs/qnx6/inode.c | 10 ++++++++--
1 file changed, 8 insertions(+), 2 deletions(-)
diff --git a/fs/qnx6/inode.c b/fs/qnx6/inode.c
index 0dfe8a3dab83..fd61cf27b81d 100644
--- a/fs/qnx6/inode.c
+++ b/fs/qnx6/inode.c
@@ -124,8 +124,13 @@ static unsigned qnx6_block_map(struct inode *inode, unsigned no)
int depth = ei->di_filelevels;
int i;
+ /*
+ * For valid levels bitdelta can reach the width of no (e.g. 35 for
+ * 512 byte blocks at depth 5). Index bits beyond no are zero, and
+ * shifting by that much would be undefined.
+ */
bitdelta = ptrbits * depth;
- levelptr = no >> bitdelta;
+ levelptr = bitdelta < BITS_PER_TYPE(no) ? no >> bitdelta : 0;
if (levelptr > QNX6_NO_DIRECT_POINTERS - 1) {
pr_err("Requested file block number (%u) too big.", no);
@@ -141,7 +146,8 @@ static unsigned qnx6_block_map(struct inode *inode, unsigned no)
return 0;
}
bitdelta -= ptrbits;
- levelptr = (no >> bitdelta) & mask;
+ levelptr = bitdelta < BITS_PER_TYPE(no) ?
+ (no >> bitdelta) & mask : 0;
ptr = ((__fs32 *)bh->b_data)[levelptr];
if (!qnx6_check_blockptr(ptr)) {
--
2.55.0
prev parent reply other threads:[~2026-09-25 15:15 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-09-25 15:14 [PATCH 0/2] qnx6: stale superblock #1 use and undefined shifts Matthias Goergens
2026-09-25 15:14 ` [PATCH 1/2] qnx6: use the active superblock after choosing superblock #2 Matthias Goergens
2026-09-25 15:14 ` Matthias Goergens [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20260925151449.1517608-3-matthias.goergens@gmail.com \
--to=matthias.goergens@gmail.com \
--cc=al@alarsen.net \
--cc=benquike@gmail.com \
--cc=brauner@kernel.org \
--cc=dlemoal@kernel.org \
--cc=jack@suse.cz \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=viro@zeniv.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®