Skip to contentSkip to conversation

mm/swapfile: really fix integer overflow in swap_show() and swapon()

PATCH Applied Oleksandr Natalenko 4 participants 5 messages Nov 18 – Nov 23

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm/swapfile: really fix integer overflow in swap_show() and swapon()

One of the enterprise customers uses 6 TiB of swap, and the values reported in /proc/swaps as well as in dmesg are still not correct.

Cast si->pages to long explicitly so that the bitshift in K() doesn't truncate the value to 32 bits.

Also do the same in swapon() for the same reason.

Fixes 642929a2ded0 ("mm/swapfile: fix an integer overflow in swap_show()")
---
 mm/swapfile.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
mm/swapfile.c +3 -3
index 626f3e9afc01e..3eadddc49a9d4 100644
@@ -3450,7 +3450,7 @@ static int swap_show(struct seq_file *swap, void *v)
 		return 0;
 	}
 
-	bytes = K(si->pages);
+	bytes = K((unsigned long)si->pages);
 	inuse = K(swap_usage_in_pages(si));
 
 	file = si->swap_file;
@@ -3959,8 +3959,8 @@ SYSCALL_DEFINE2(swapon, const char __user *, specialfile, int, swap_flags)
 	/* Sets SWP_WRITEOK, resurrect the percpu ref, expose the swap device */
 	enable_swap_info(si);
 
-	pr_info("Adding %uk swap on %s.  Priority:%d extents:%d across:%lluk %s%s%s%s\n",
-		K(si->pages), name->name, si->prio, nr_extents,
+	pr_info("Adding %luk swap on %s.  Priority:%d extents:%d across:%lluk %s%s%s%s\n",
+		K((unsigned long)si->pages), name->name, si->prio, nr_extents,
 		K((unsigned long long)span),
 		(si->flags & SWP_SOLIDSTATE) ? "SS" : "",
 		(si->flags & SWP_DISCARDABLE) ? "D" : "",
Signature · 1 line
2.56.1
reviewer Reviewed-by on v1 1/1
#1 lore raw

Hi Oleksandr,

Quoted 2 lines

Cast si->pages to long explicitly so that the bitshift in K() doesn't truncate the value to 32 bits.

Nice catch. 642929a2ded0 widened bytes but left the shift in 32 bits.

Quoted 1 line
Fixes 642929a2ded0 ("mm/swapfile: fix an integer overflow in swap_show()")

stable kernels show the same wrong size, should this also carry Cc: [email protected]?

Thanks,
Youngjun

#2 lore raw

Hello.

Quoted 11 lines · středoevropský standardní čas Youngjun Park

Hi Oleksandr,

Quoted 2 lines

Cast si->pages to long explicitly so that the bitshift in K() doesn't truncate the value to 32 bits.

Nice catch. 642929a2ded0 widened bytes but left the shift in 32 bits.

Quoted 1 line
Fixes 642929a2ded0 ("mm/swapfile: fix an integer overflow in swap_show()")

stable kernels show the same wrong size, should this also carry Cc: [email protected]?

Yes, picking this for stable@ is a good idea.

Quoted 1 line

Thank you!

Quoted 3 lines

Thanks,
Youngjun

Signature · 1 line
Oleksandr Natalenko, MSE
reviewer Reviewed-by on v1 1/1
#3 lore raw
In reply to Oleksandr Natalenko
Quoted 11 lines · Oleksandr Natalenko

One of the enterprise customers uses 6 TiB of swap, and the values reported in /proc/swaps as well as in dmesg are still not correct.

Cast si->pages to long explicitly so that the bitshift in K() doesn't truncate the value to 32 bits.

Also do the same in swapon() for the same reason.

Fixes 642929a2ded0 ("mm/swapfile: fix an integer overflow in swap_show()")
maintainer tree notice Applied
#4 lore raw

[merged mm-hotfixes-stable] mm-swapfile-really-fix-integer-overflow-in-swap_show-and-swapon.patch removed from -mm tree

In reply to Oleksandr Natalenko
The quilt patch titled Subject: mm/swapfile: really fix integer overflow in swap_show() and swapon() has been removed from the -mm tree. Its filename was…

The quilt patch titled
Subject: mm/swapfile: really fix integer overflow in swap_show() and swapon()
has been removed from the -mm tree. Its filename was
mm-swapfile-really-fix-integer-overflow-in-swap_show-and-swapon.patch

This patch was dropped because it was merged into the mm-hotfixes-stable branch of git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

------------------------------------------------------
From: Oleksandr Natalenko <[email protected]>
Subject: mm/swapfile: really fix integer overflow in swap_show() and swapon()
Date: Wed, 18 Nov 2026 12:26:10 +0100

One of the enterprise customers uses 6 TiB of swap, and the values reported in /proc/swaps as well as in dmesg are still not correct.

Cast si->pages to long explicitly so that the bitshift in K() doesn't truncate the value to 32 bits.

Also do the same in swapon() for the same reason.

Fixes 642929a2ded0 ("mm/swapfile: fix an integer overflow in swap_show()")
Cc Rafael Aquini <[email protected]>
Cc Christopher Li <[email protected]>
Cc Kairui Song <[email protected]>
Cc Kemeng Shi <[email protected]>
Cc Baoquan He <[email protected]>
Cc Barry Song <[email protected]>
 mm/swapfile.c |    6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
mm/swapfile.c renamed from mm/swapfile.c~mm-swapfile-really-fix-integer-overflow-in-swap_show-and-swapon +3 -3
@@ -3452,7 +3452,7 @@ static int swap_show(struct seq_file *sw
 		return 0;
 	}
 
-	bytes = K(si->pages);
+	bytes = K((unsigned long)si->pages);
 	inuse = K(swap_usage_in_pages(si));
 
 	file = si->swap_file;
@@ -3968,8 +3968,8 @@ SYSCALL_DEFINE2(swapon, const char __use
 	/* Sets SWP_WRITEOK, resurrect the percpu ref, expose the swap device */
 	enable_swap_info(si);
 
-	pr_info("Adding %uk swap on %s.  Priority:%d extents:%d across:%lluk %s%s%s%s\n",
-		K(si->pages), name->name, si->prio, nr_extents,
+	pr_info("Adding %luk swap on %s.  Priority:%d extents:%d across:%lluk %s%s%s%s\n",
+		K((unsigned long)si->pages), name->name, si->prio, nr_extents,
 		K((unsigned long long)span),
 		(si->flags & SWP_SOLIDSTATE) ? "SS" : "",
 		(si->flags & SWP_DISCARDABLE) ? "D" : "",

_

Patches currently in -mm which might be from [email protected] are