Skip to content

Commit

Permalink
fs/jfs: cast inactags to s64 to prevent potential overflow
Browse files Browse the repository at this point in the history
[ Upstream commit 70ca324 ]

The expression "inactags << bmp->db_agl2size" in the function
dbFinalizeBmap() is computed using int operands. Although the
values (inactags and db_agl2size) are derived from filesystem
parameters and are usually small, there is a theoretical risk that
the shift could overflow a 32-bit int if extreme values occur.

According to the C standard, shifting a signed 32-bit int can lead
to undefined behavior if the result exceeds its range. In our
case, an overflow could miscalculate free blocks, potentially
leading to erroneous filesystem accounting.

To ensure the arithmetic is performed in 64-bit space, we cast
"inactags" to s64 before shifting. This defensive fix prevents any
risk of overflow and complies with kernel coding best practices.

Found by Linux Verification Center (linuxtesting.org) with SVACE.

Signed-off-by: Rand Deeb <rand.sec96@gmail.com>
Signed-off-by: Dave Kleikamp <dave.kleikamp@oracle.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
  • Loading branch information
Rand Deeb authored and Greg Kroah-Hartman committed May 2, 2025
1 parent 4f10732 commit 761e36c
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions fs/jfs/jfs_dmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -3732,8 +3732,8 @@ void dbFinalizeBmap(struct inode *ipbmap)
* system size is not a multiple of the group size).
*/
inactfree = (inactags && ag_rem) ?
((inactags - 1) << bmp->db_agl2size) + ag_rem
: inactags << bmp->db_agl2size;
(((s64)inactags - 1) << bmp->db_agl2size) + ag_rem
: ((s64)inactags << bmp->db_agl2size);

/* determine how many free blocks are in the active
* allocation groups plus the average number of free blocks
Expand Down

0 comments on commit 761e36c

Please sign in to comment.