Skip to contentSkip to conversation

jfs: validate l2nbperpage in diMount to prevent shift-out-of-bounds

PATCH In review Tristan Madani 2 participants 2 messages Apr 18 – Jun 1 JFS filesystem

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 jfs: validate l2nbperpage in diMount to prevent shift-out-of-bounds

From: Tristan Madani <[email protected]>

diMount() reads l2nbperpage from the disk inode aggregate without validation. A corrupted filesystem image can set this field to a value exceeding the bit width of the shift operand in jfs_statfs(), causing UBSAN shift-out-of-bounds.

Add validation of l2nbperpage against reasonable bounds before using it in arithmetic operations.

Reported-by [email protected]
Fixes 1da177e4c3f4 ("Linux-2.6.12-rc2")
---
 fs/jfs/jfs_imap.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)
fs/jfs/jfs_imap.c +12 -0
index b84ba4d7dfb44..eafbd2b55df75 100644
@@ -124,6 +124,18 @@ int diMount(struct inode *ipimap)
 	atomic_set(&imap->im_numfree, le32_to_cpu(dinom_le->in_numfree));
 	imap->im_nbperiext = le32_to_cpu(dinom_le->in_nbperiext);
 	imap->im_l2nbperiext = le32_to_cpu(dinom_le->in_l2nbperiext);
+
+	if (imap->im_l2nbperiext < 0 ||
+	    imap->im_l2nbperiext > 30 ||
+	    imap->im_nbperiext != (1 << imap->im_l2nbperiext)) {
+		jfs_err("diMount: invalid imap parameters: "
+			"nbperiext(%d) l2nbperiext(%d)",
+			imap->im_nbperiext, imap->im_l2nbperiext);
+		release_metapage(mp);
+		kfree(imap);
+		return -EINVAL;
+	}
+
 	for (index = 0; index < MAXAG; index++) {
 		imap->im_agctl[index].inofree =
 		    le32_to_cpu(dinom_le->in_agctl[index].inofree);
Signature · 1 line
2.47.3
Reviewed-by on v1 1/1
#1 lore raw
Quoted 2 lines · Tristan Madani

Add validation of l2nbperpage against reasonable bounds before using it in arithmetic operations.

The fix looks correct to me. Minor nit: the subject and commit message say "l2nbperpage" but the actual field being validated is "im_l2nbperiext" (and im_nbperiext). Could you respin with the corrected naming?