Skip to contentSkip to conversation

fs/jfs: fix potential integer overflow on shift of a int

PATCH In review Colin King 2 participants 2 messages Feb 11, 2021

Subsystems (2)
lore
Share & tools
lore

Colin King

author
#0 lore raw

PATCH v2 1/1 fs/jfs: fix potential integer overflow on shift of a int

From: Colin Ian King <[email protected]>

The left shift of int 32 bit integer constant 1 is evaluated using 32 bit arithmetic and then assigned to a signed 64 bit integer. In the case where l2nb is 32 or more this can lead to an overflow. Avoid this by shifting the value 1LL instead.

Addresses-Coverity: ("Uninitentional integer overflow")

Fixes b40c2e665cd5 ("fs/jfs: TRIM support for JFS Filesystem")

---

V2: shift 1LL rather than using BIT_ULL macro as suggested by
Dave Kleikamp.

---
 fs/jfs/jfs_dmap.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
fs/jfs/jfs_dmap.c +1 -1
index 94b7c1cb5ceb..7aee15608619 100644
@@ -1656,7 +1656,7 @@ s64 dbDiscardAG(struct inode *ip, int agno, s64 minlen)
 		} else if (rc = -ENOSPC) {
 			/* search for next smaller log2 block */
 			l2nb = BLKSTOL2(nblocks) - 1;
-			nblocks = 1 << l2nb;
+			nblocks = 1LL << l2nb;
 		} else {
 			/* Trim any already allocated blocks */
 			jfs_error(bmp->db_ipbmap->i_sb, "-EIO\n");
Signature · 1 line
2.30.0
#1 lore raw
Quoted 10 lines · Colin King

From: Colin Ian King <[email protected]>

The left shift of int 32 bit integer constant 1 is evaluated using 32 bit arithmetic and then assigned to a signed 64 bit integer. In the case where l2nb is 32 or more this can lead to an overflow. Avoid this by shifting the value 1LL instead.

Addresses-Coverity: ("Uninitentional integer overflow")

Fixes b40c2e665cd5 ("fs/jfs: TRIM support for JFS Filesystem")

Looks good. I'll include it in the next jfs drop.

Thanks,
Shaggy

Quoted 23 lines

---

V2: shift 1LL rather than using BIT_ULL macro as suggested by
Dave Kleikamp.

---
  fs/jfs/jfs_dmap.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
fs/jfs/jfs_dmap.c +1 -1
index 94b7c1cb5ceb..7aee15608619 100644
@@ -1656,7 +1656,7 @@ s64 dbDiscardAG(struct inode *ip, int agno, s64 minlen)
  		} else if (rc = -ENOSPC) {
  			/* search for next smaller log2 block */
  			l2nb = BLKSTOL2(nblocks) - 1;
-			nblocks = 1 << l2nb;
+			nblocks = 1LL << l2nb;
  		} else {
  			/* Trim any already allocated blocks */
  			jfs_error(bmp->db_ipbmap->i_sb, "-EIO\n");