Re: [PATCH] block/partitions: lock the mapping while reading a partition sector
From: Jan Kara
Date: Fri Nov 27 2026 - 09:42:54 EST
On Fri 27-11-26 15:45:00, Zhao Gongyi wrote:
> A concurrent BLKBSZSET and BLKRRPART reproducer reports
> "Unable to handle kernel NULL pointer dereference" in
> create_empty_buffers(). The relevant call trace is:
>
> create_empty_buffers
> block_read_full_folio
> blkdev_read_folio
> read_cache_folio
> read_part_sector
> msdos_partition
>
> read_part_sector() calls read_mapping_folio() without the mapping's
> invalidate_lock. The race can allocate a folio at the old minimum order
> and then create buffer heads using the new block size. When the folio is
> smaller than the block size, create_empty_buffers() dereferences NULL.
>
> Hold the shared invalidate_lock across the read, pairing with the
> exclusive lock in set_blocksize().
>
> Fixes: 3c20917120ce ("block/bdev: enable large folio support for large logical block sizes")
> Cc: stable@xxxxxxxxxxxxxxx
> Signed-off-by: Zhao Gongyi <zhaogongyi@xxxxxxxxxxxxx>
Looks sensible. The partition rereading code tends to get called from
unexpected places at times so I was pondering for a while whether it
doesn't get called in a context where we could not grab invalidate_lock but
I didn't find anything. So feel free to add:
Reviewed-by: Jan Kara <jack@xxxxxxx>
Honza
> ---
> Testing (arm64 QEMU/KVM, GCC 8.3, KASAN/KCOV/UBSAN disabled):
> Unpatched: the reproducer triggered a NULL dereference in
> create_empty_buffers() in 3/3 runs.
> Patched: no crash in 5/5 runs with the same reproducer.
>
> block/partitions/core.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/block/partitions/core.c b/block/partitions/core.c
> index b5c59b79ca7cb..a36eeb235d5e0 100644
> --- a/block/partitions/core.c
> +++ b/block/partitions/core.c
> @@ -721,7 +721,9 @@ void *read_part_sector(struct parsed_partitions *state, sector_t n, Sector *p)
> goto out;
> }
>
> + filemap_invalidate_lock_shared(mapping);
> folio = read_mapping_folio(mapping, n >> PAGE_SECTORS_SHIFT, NULL);
> + filemap_invalidate_unlock_shared(mapping);
> if (IS_ERR(folio))
> goto out;
>
>
> base-commit: 5a29cfaf51105039d156dd8a61d2783e8b653667
> --
> 2.39.5 (Apple Git-154)
--
Jan Kara <jack@xxxxxxxx>
SUSE Labs, CR