* [PATCH] mm: swap: make swap_header badpages a flexible array
@ 2026-11-20 6:17 kaleshsingh
2026-11-21 0:31 ` Gustavo A. R. Silva
` (3 more replies)
0 siblings, 4 replies; 5+ messages in thread
From: kaleshsingh @ 2026-11-20 6:17 UTC (permalink / raw)
To: akpm
Cc: kernel-team, android-mm, Kalesh Singh, Kees Cook, stable,
Chris Li, Kairui Song, Kemeng Shi, Nhat Pham, Baoquan He,
Barry Song, Youngjun Park, Gustavo A. R. Silva, linux-mm
From: Kalesh Singh <kaleshsingh@google.com>
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 <akpm@linux-foundation.org>
Cc: Kees Cook <kees@kernel.org>
Cc: stable@vger.kernel.org # 6.6.y, 6.12.y, 6.18.y
Signed-off-by: Kalesh Singh <kaleshsingh@google.com>
---
include/linux/swap.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/include/linux/swap.h b/include/linux/swap.h
index 4f686709d71f..a3a98dc474c3 100644
--- a/include/linux/swap.h
+++ b/include/linux/swap.h
@@ -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
--
2.56.1.750.gf70dc682cf-goog
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] mm: swap: make swap_header badpages a flexible array
2026-11-20 6:17 [PATCH] mm: swap: make swap_header badpages a flexible array kaleshsingh
@ 2026-11-21 0:31 ` Gustavo A. R. Silva
2026-11-21 0:47 ` SJ Park
` (2 subsequent siblings)
3 siblings, 0 replies; 5+ messages in thread
From: Gustavo A. R. Silva @ 2026-11-21 0:31 UTC (permalink / raw)
To: kaleshsingh, akpm
Cc: kernel-team, android-mm, Kees Cook, stable, Chris Li, Kairui Song,
Kemeng Shi, Nhat Pham, Baoquan He, Barry Song, Youngjun Park,
Gustavo A. R. Silva, linux-mm
On 11/20/26 15:17, kaleshsingh@google.com wrote:
> From: Kalesh Singh <kaleshsingh@google.com>
>
> 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 <akpm@linux-foundation.org>
> Cc: Kees Cook <kees@kernel.org>
> Cc: stable@vger.kernel.org # 6.6.y, 6.12.y, 6.18.y
> Signed-off-by: Kalesh Singh <kaleshsingh@google.com>
Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Thanks
-Gustavo
> ---
> include/linux/swap.h | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/include/linux/swap.h b/include/linux/swap.h
> index 4f686709d71f..a3a98dc474c3 100644
> --- a/include/linux/swap.h
> +++ b/include/linux/swap.h
> @@ -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
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] mm: swap: make swap_header badpages a flexible array
2026-11-20 6:17 [PATCH] mm: swap: make swap_header badpages a flexible array kaleshsingh
2026-11-21 0:31 ` Gustavo A. R. Silva
@ 2026-11-21 0:47 ` SJ Park
2026-11-24 1:24 ` Andrew Morton
2026-11-24 1:24 ` + mm-swap-make-swap_header-badpages-a-flexible-array.patch added to mm-hotfixes-unstable branch Andrew Morton
3 siblings, 0 replies; 5+ messages in thread
From: SJ Park @ 2026-11-21 0:47 UTC (permalink / raw)
To: kaleshsingh
Cc: SJ Park, akpm, kernel-team, android-mm, Kees Cook, stable,
Chris Li, Kairui Song, Kemeng Shi, Nhat Pham, Baoquan He,
Barry Song, Youngjun Park, Gustavo A. R. Silva, linux-mm
On Fri, 20 Nov 2026 06:17:49 +0000 kaleshsingh@google.com wrote:
> From: Kalesh Singh <kaleshsingh@google.com>
>
> 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.
Makes sense to me.
>
> Fixes: df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3")
> Cc: Andrew Morton <akpm@linux-foundation.org>
> Cc: Kees Cook <kees@kernel.org>
> Cc: stable@vger.kernel.org # 6.6.y, 6.12.y, 6.18.y
> Signed-off-by: Kalesh Singh <kaleshsingh@google.com>
Reviewed-by: SJ Park <sj@kernel.org>
Thanks,
SJ
[...]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] mm: swap: make swap_header badpages a flexible array
2026-11-20 6:17 [PATCH] mm: swap: make swap_header badpages a flexible array kaleshsingh
2026-11-21 0:31 ` Gustavo A. R. Silva
2026-11-21 0:47 ` SJ Park
@ 2026-11-24 1:24 ` Andrew Morton
2026-11-24 1:24 ` + mm-swap-make-swap_header-badpages-a-flexible-array.patch added to mm-hotfixes-unstable branch Andrew Morton
3 siblings, 0 replies; 5+ messages in thread
From: Andrew Morton @ 2026-11-24 1:24 UTC (permalink / raw)
To: kaleshsingh
Cc: kernel-team, android-mm, Kees Cook, stable, Chris Li, Kairui Song,
Kemeng Shi, Nhat Pham, Baoquan He, Barry Song, Youngjun Park,
Gustavo A. R. Silva, linux-mm, linux-sparse
On Fri, 20 Nov 2026 06:17:49 +0000 kaleshsingh@google.com wrote:
> From: Kalesh Singh <kaleshsingh@google.com>
>
> 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.
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.
> --- a/include/linux/swap.h
> +++ b/include/linux/swap.h
> @@ -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.
^ permalink raw reply [flat|nested] 5+ messages in thread
* + mm-swap-make-swap_header-badpages-a-flexible-array.patch added to mm-hotfixes-unstable branch
2026-11-20 6:17 [PATCH] mm: swap: make swap_header badpages a flexible array kaleshsingh
` (2 preceding siblings ...)
2026-11-24 1:24 ` Andrew Morton
@ 2026-11-24 1:24 ` Andrew Morton
3 siblings, 0 replies; 5+ messages in thread
From: Andrew Morton @ 2026-11-24 1:24 UTC (permalink / raw)
To: mm-commits, youngjun.park, stable, sj, shikemeng, nphamcs, kees,
kasong, gustavo, gustavoars, chrisl, baoquan.he, baohua,
android-mm, kaleshsingh, akpm
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 <kaleshsingh@google.com>
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.
Link: https://patch.msgid.link/20261120061750.2973004-1-kaleshsingh@google.com
Fixes: df8fc4e934c1 ("kbuild: Enable -fstrict-flex-arrays=3")
Signed-off-by: Kalesh Singh <kaleshsingh@google.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Reviewed-by: SJ Park <sj@kernel.org>
Reviewed-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Cc: Kees Cook <kees@kernel.org>
Cc: Christopher Li <chrisl@kernel.org>
Cc: Kairui Song <kasong@tencent.com>
Cc: Kemeng Shi <shikemeng@huaweicloud.com>
Cc: Nhat Pham <nphamcs@gmail.com>
Cc: Baoquan He <baoquan.he@linux.dev>
Cc: Barry Song <baohua@kernel.org>
Cc: Youngjun Park <youngjun.park@lge.com>
Cc: <android-mm@google.com>
Cc: "Gustavo A. R. Silva" <gustavo@embeddedor.com>
Cc: <stable@vger.kernel.org> # 6.6.y, 6.12.y, 6.18.y
include/linux/swap.h | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
--- a/include/linux/swap.h~mm-swap-make-swap_header-badpages-a-flexible-array
+++ a/include/linux/swap.h
@@ -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 kaleshsingh@google.com are
mm-swap-make-swap_header-badpages-a-flexible-array.patch
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2026-11-24 1:24 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 6:17 [PATCH] mm: swap: make swap_header badpages a flexible array kaleshsingh
2026-11-21 0:31 ` Gustavo A. R. Silva
2026-11-21 0:47 ` SJ Park
2026-11-24 1:24 ` Andrew Morton
2026-11-24 1:24 ` + mm-swap-make-swap_header-badpages-a-flexible-array.patch added to mm-hotfixes-unstable branch Andrew Morton
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox