Skip to contentSkip to conversation

mm: swap: make swap_header badpages a flexible array

PATCH In review [email protected] 4 participants 5 messages Nov 20 – Nov 24

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

PATCH 1/1 mm: swap: make swap_header badpages a flexible array

From: Kalesh Singh <[email protected]>

The swap header declares its list of bad pages as a one-element array, even though the list can fill the rest of the header page. Since the kernel is built with -fstrict-flex-arrays=3, the compiler takes that size literally, so with UBSAN bounds checking, swapon() of a swap area with two or more bad pages reports an out-of-bounds access:

  UBSAN: array-index-out-of-bounds in mm/swapfile.c:3674:26
  index 1 is out of range for type '__u32[1]' (aka 'unsigned int[1]')

Kernels that trap on UBSAN reports and panic on oops, such as Android GKI kernels, crash instead.

Declare the list as a flexible array. The layout of the swap header does not change.

Fixes df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3")
Cc Andrew Morton <[email protected]>
Cc Kees Cook <[email protected]>
Cc [email protected] # 6.6.y, 6.12.y, 6.18.y
---
 include/linux/swap.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/swap.h +1 -1
index 4f686709d71f..a3a98dc474c3 100644
@@ -129,7 +129,7 @@ union swap_header {
 		unsigned char	sws_uuid[16];
 		unsigned char	sws_volume[16];
 		__u32		padding[117];
-		__u32		badpages[1];
+		__u32		badpages[];
 	} info;
 };
 

base-commit: 65e4fd942f72a9dba0e4e257290c4fa1aa756854

Signature · 1 line
2.56.1.750.gf70dc682cf-goog
Reviewed-by on v1 1/1
#1 lore raw
Quoted 14 lines · On 11/20/26 15:17, [email protected] wrote:

From: Kalesh Singh <[email protected]>

The swap header declares its list of bad pages as a one-element array, even though the list can fill the rest of the header page. Since the kernel is built with -fstrict-flex-arrays=3, the compiler takes that size literally, so with UBSAN bounds checking, swapon() of a swap area with two or more bad pages reports an out-of-bounds access:

   UBSAN: array-index-out-of-bounds in mm/swapfile.c:3674:26
   index 1 is out of range for type '__u32[1]' (aka 'unsigned int[1]')

Kernels that trap on UBSAN reports and panic on oops, such as Android GKI kernels, crash instead.

Quoted 8 lines

Declare the list as a flexible array. The layout of the swap header does not change.

Fixes df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3")
Cc Andrew Morton <[email protected]>
Cc Kees Cook <[email protected]>
Cc [email protected] # 6.6.y, 6.12.y, 6.18.y

Thanks
-Gustavo

Quoted 19 lines
---
  include/linux/swap.h | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/swap.h +1 -1
index 4f686709d71f..a3a98dc474c3 100644
@@ -129,7 +129,7 @@ union swap_header {
  		unsigned char	sws_uuid[16];
  		unsigned char	sws_volume[16];
  		__u32		padding[117];
-		__u32		badpages[1];
+		__u32		badpages[];
  	} info;
  };
 

base-commit: 65e4fd942f72a9dba0e4e257290c4fa1aa756854

Reviewed-by on v1 1/1
#2 lore raw
In reply to [email protected]
Quoted 8 lines · On Fri, 20 Nov 2026 06:17:49 +0000 [email protected] wrote:

From: Kalesh Singh <[email protected]>

The swap header declares its list of bad pages as a one-element array, even though the list can fill the rest of the header page. Since the kernel is built with -fstrict-flex-arrays=3, the compiler takes that size literally, so with UBSAN bounds checking, swapon() of a swap area with two or more bad pages reports an out-of-bounds access:

Quoted 8 lines
  UBSAN: array-index-out-of-bounds in mm/swapfile.c:3674:26
  index 1 is out of range for type '__u32[1]' (aka 'unsigned int[1]')

Kernels that trap on UBSAN reports and panic on oops, such as Android GKI kernels, crash instead.

Declare the list as a flexible array. The layout of the swap header does not change.

Makes sense to me.

Quoted 6 lines
Fixes df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3")
Cc Andrew Morton <[email protected]>
Cc Kees Cook <[email protected]>
Cc [email protected] # 6.6.y, 6.12.y, 6.18.y

Thanks,
SJ

[...]

maintainer
#3 lore raw
In reply to [email protected]
Quoted 8 lines · On Fri, 20 Nov 2026 06:17:49 +0000 [email protected] wrote:

From: Kalesh Singh <[email protected]>

The swap header declares its list of bad pages as a one-element array, even though the list can fill the rest of the header page. Since the kernel is built with -fstrict-flex-arrays=3, the compiler takes that size literally, so with UBSAN bounds checking, swapon() of a swap area with two or more bad pages reports an out-of-bounds access:

Quoted 8 lines
  UBSAN: array-index-out-of-bounds in mm/swapfile.c:3674:26
  index 1 is out of range for type '__u32[1]' (aka 'unsigned int[1]')

Kernels that trap on UBSAN reports and panic on oops, such as Android GKI kernels, crash instead.

Declare the list as a flexible array. The layout of the swap header does not change.

OK.

I worry about these flex_array conversions because there's a trap.

The conversion is safe only if nobody copies swap_header.info using assignment or memcpy(..., sizeof(info)). Both would have previously copied badpages[0], but would omit it after this change because sizeof(info) shrinks by four bytes.

I couldn't find any such copy operations (didn't expect to) so we're good.

However it is a trap. Having a static checker warn about this would be helpful. And afaict no checkers we presently have will pick up on this.

(cc sparse peeps)

Can sparse warn when a struct containing a flexible array is copied by assignment or by memcpy(..., sizeof(struct))? Both will copy only the fixed fields, which may silently omit flexibly-allocated array elements.

include/linux/swap.h union swap_header { · line 134

Quoted 10 lines
include/linux/swap.h +1 -1
@@ -129,7 +129,7 @@ union swap_header {
 		unsigned char	sws_uuid[16];
 		unsigned char	sws_volume[16];
 		__u32		padding[117];
-		__u32		badpages[1];
+		__u32		badpages[];
 	} info;
 };

Thanks, tossed onto the mm-unstable pile.

maintainer tree notice
#4 lore raw

+ mm-swap-make-swap_header-badpages-a-flexible-array.patch added to mm-hotfixes-unstable branch

In reply to [email protected]
The patch titled Subject: mm: swap: make swap_header badpages a flexible array has been added to the -mm mm-hotfixes-unstable branch. Its filename is…

The patch titled
Subject: mm: swap: make swap_header badpages a flexible array
has been added to the -mm mm-hotfixes-unstable branch. Its filename is
mm-swap-make-swap_header-badpages-a-flexible-array.patch

This patch will shortly appear at
https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/mm-swap-make-swap_header-badpages-a-flexible-array.patch

This patch will later appear in the mm-hotfixes-unstable branch at
git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm

Before you just go and hit "reply", please:
a) Consider who else should be cc'ed
b) Prefer to cc a suitable mailing list as well
c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's

*** Remember to use Documentation/process/submit-checklist.rst when testing your code ***

The -mm tree is included into linux-next via various branches at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm
and is updated there most days

------------------------------------------------------
From: Kalesh Singh <[email protected]>
Subject: mm: swap: make swap_header badpages a flexible array
Date: Fri, 20 Nov 2026 06:17:49 +0000

The swap header declares its list of bad pages as a one-element array, even though the list can fill the rest of the header page. Since the kernel is built with -fstrict-flex-arrays=3, the compiler takes that size literally, so with UBSAN bounds checking, swapon() of a swap area with two or more bad pages reports an out-of-bounds access:

  UBSAN: array-index-out-of-bounds in mm/swapfile.c:3674:26
  index 1 is out of range for type '__u32[1]' (aka 'unsigned int[1]')

Kernels that trap on UBSAN reports and panic on oops, such as Android GKI kernels, crash instead.

Declare the list as a flexible array. The layout of the swap header does not change.

Fixes df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3")
Cc Kees Cook <[email protected]>
Cc Christopher Li <[email protected]>
Cc Kairui Song <[email protected]>
Cc Kemeng Shi <[email protected]>
Cc Nhat Pham <[email protected]>
Cc Baoquan He <[email protected]>
Cc Barry Song <[email protected]>
Cc Youngjun Park <[email protected]>
Cc "Gustavo A. R. Silva" <[email protected]>
Cc <[email protected]> # 6.6.y, 6.12.y, 6.18.y
 include/linux/swap.h |    2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/swap.h renamed from include/linux/swap.h~mm-swap-make-swap_header-badpages-a-flexible-array +1 -1
@@ -129,7 +129,7 @@ union swap_header {
 		unsigned char	sws_uuid[16];
 		unsigned char	sws_volume[16];
 		__u32		padding[117];
-		__u32		badpages[1];
+		__u32		badpages[];
 	} info;
 };
 

_

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

mm-swap-make-swap_header-badpages-a-flexible-array.patch