Linux SPARSE checker discussions
 help / color / mirror / Atom feed
* Re: [PATCH] mm: swap: make swap_header badpages a flexible array
       [not found] <20261120061750.2973004-1-kaleshsingh@google.com>
@ 2026-11-24  1:24 ` Andrew Morton
  0 siblings, 0 replies; only message 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] only message in thread

only message in thread, other threads:[~2026-11-24  1:24 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <20261120061750.2973004-1-kaleshsingh@google.com>
2026-11-24  1:24 ` [PATCH] mm: swap: make swap_header badpages a flexible array Andrew Morton

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox