All Virtuozzo development lists (kernel + QEMU)
 help / color / mirror / Atom feed
From: no-reply@sashiko.virtuozzo.com
To: Alexander Ivanov <alexander.ivanov@virtuozzo.com>, den@openvz.org
Cc: devel@openvz.org
Subject: Re: [Devel] [PATCH hci-8.1 v6 01/12] dm-ploop: fix u32 overflow of the delta BAT byte size
Date: Tue, 13 Oct 2026 11:23:00 +0000	[thread overview]
Message-ID: <010701a1350065d1-dd3b16d9-5723-4098-be08-d73cb778e5fb-000000@eu-central-1.amazonses.com> (raw)
In-Reply-To: <20261013111604.142989-2-alexander.ivanov@virtuozzo.com>

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:
- [Medium] dm-ploop: ploop_bat_clu_to_page_nr() still wraps u32 on 32-bit builds
--

Subject: Re: [PATCH] dm-ploop: fix u32 overflow of the delta BAT byte size

commit 637ffe4e923149b9a169a498b933c3d5f2b39ef7
Author: Alexander Ivanov <alexander.ivanov@virtuozzo.com>

dm-ploop: fix u32 overflow of the delta BAT byte size

This patch computes the byte size of the header and BAT in u64 and uses
ploop_nr_bat_clusters() everywhere for the BAT cluster count. It also
rejects a header or a resize whose hb_nr exceeds PLOOP_MAX_HB_NR.

Link: https://virtuozzo.atlassian.net/browse/VSTOR-148171

> diff --git a/drivers/md/dm-ploop.h b/drivers/md/dm-ploop.h
> --- a/drivers/md/dm-ploop.h
> +++ b/drivers/md/dm-ploop.h
> @@ -12,6 +12,8 @@
>  #include <linux/bio.h>
>  
>  #define PLOOP_MAP_OFFSET 16
> +/* Keeps DIV_ROUND_UP(hb_nr, 8) and clu + PLOOP_MAP_OFFSET within u32 */
> +#define PLOOP_MAX_HB_NR round_down(U32_MAX - PLOOP_MAP_OFFSET, 8)
>  typedef u32 map_index_t;
>  #define BAT_ENTRIES_PER_PAGE (PAGE_SIZE / sizeof(map_index_t))

[ ... ]

>  static inline unsigned int ploop_nr_bat_clusters(struct ploop *ploop,
>  						 u32 nr_bat_entries)
>  {
> -	unsigned long size, bat_clusters;
> +	u64 size, bat_clusters;
>  
> -	size = (PLOOP_MAP_OFFSET + nr_bat_entries) * sizeof(map_index_t);
> +	size = ((u64)nr_bat_entries + PLOOP_MAP_OFFSET) * sizeof(map_index_t);
>  	bat_clusters = DIV_ROUND_UP(size, CLU_SIZE(ploop));
>  
>  	return bat_clusters;

[Severity: Medium]
This isn't addressed by the hunk above, but a related helper in the same
header appears to have been left with the same u32 wrap:

drivers/md/dm-ploop.h:ploop_bat_clu_to_page_nr() {
	u64 byte;

	byte = (clu + PLOOP_MAP_OFFSET) * sizeof(map_index_t);
	return byte >> PAGE_SHIFT;
}

Here clu is u32 and sizeof(map_index_t) is size_t, which is 32 bits on
32-bit architectures.  Doesn't the multiply wrap before the result is
widened to the u64 byte?

DM_PLOOP depends only on BLK_DEV_DM, so 32-bit builds are not excluded.
With PLOOP_MAX_HB_NR now allowing hb_nr up to 0xFFFFFFE8, a delta header
m_Size or a resize request with clu + 16 >= 2^30 would make this helper
return a too-small page number on a 32-bit kernel.

That value feeds ploop_prealloc_md_pages():

    new_nr_pages = ploop_bat_clu_to_page_nr(new_nr_bat_entries - 1) + 1;

which is called from ploop_resize() and ploop_read_delta_metadata(), so
too few md pages could be allocated for a large BAT.  init_be_iter() uses
the same helper to compute last_page, so the BAT page coverage could
become inconsistent with nr_bat_entries.

The commit message says the byte size is computed in u64 "in several
places" and that the u32 wrap is fixed everywhere.  Should this helper
use ((u64)clu + PLOOP_MAP_OFFSET) * sizeof(map_index_t) as well?

On 64-bit builds the multiply does not wrap, so this only affects 32-bit
kernels.

-- 
Sashiko AI review · https://sashiko.vzint.dev/#/patchset/20261013111604.142989-1-alexander.ivanov@virtuozzo.com?part=1

_______________________________________________
Devel mailing list
Devel@openvz.org
https://lists.openvz.org/mailman/listinfo/devel

  reply	other threads:[~2026-10-13 11:24 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-10-13 11:15 [Devel] [PATCH hci-8.1 v6 00/12] dm-ploop: give back unused tail of top delta on suspend Alexander Ivanov
2026-10-13 11:15 ` [Devel] [PATCH hci-8.1 v6 01/12] dm-ploop: fix u32 overflow of the delta BAT byte size Alexander Ivanov
2026-10-13 11:23   ` no-reply [this message]
2026-10-13 11:15 ` [Devel] [PATCH hci-8.1 v6 02/12] dm-ploop: fix lost wakeup in ploop_allocator() Alexander Ivanov
2026-10-13 11:21   ` no-reply
2026-10-13 11:15 ` [Devel] [PATCH hci-8.1 v6 03/12] dm-ploop: reject a delta header whose BAT does not cover the target Alexander Ivanov
2026-10-13 11:22   ` no-reply
2026-10-13 11:15 ` [Devel] [PATCH hci-8.1 v6 04/12] dm-ploop: reject empty deltas Alexander Ivanov
2026-10-13 11:24   ` no-reply
2026-10-13 11:15 ` [Devel] [PATCH hci-8.1 v6 05/12] dm-ploop: set delta is_raw before applying its mappings Alexander Ivanov
2026-10-13 11:24   ` no-reply
2026-10-13 11:15 ` [Devel] [PATCH hci-8.1 v6 06/12] dm-ploop: factor out ploop_read_delta() from ploop_add_delta() Alexander Ivanov
2026-10-13 11:25   ` no-reply
2026-10-13 11:15 ` [Devel] [PATCH hci-8.1 v6 07/12] dm-ploop: take bat_lock around md page lookup in ploop_llseek_hole() Alexander Ivanov
2026-10-13 11:26   ` no-reply
2026-10-13 11:16 ` [Devel] [PATCH hci-8.1 v6 08/12] dm-ploop: use wake_up() for inflight_md_wq and dispatcher_wq_prealloc Alexander Ivanov
2026-10-13 11:26   ` no-reply
2026-10-13 11:16 ` [Devel] [PATCH hci-8.1 v6 09/12] dm-ploop: fix use-after-free on the last inflight_md decrement Alexander Ivanov
2026-10-13 11:30   ` no-reply
2026-10-13 11:16 ` [Devel] [PATCH hci-8.1 v6 10/12] dm-ploop: fix flip_upper_deltas racing with resume Alexander Ivanov
2026-10-13 11:28   ` no-reply
2026-10-13 11:16 ` [Devel] [PATCH hci-8.1 v6 11/12] dm-ploop: reload metadata on resume Alexander Ivanov
2026-10-13 11:35   ` no-reply
2026-10-13 12:46     ` Alexander Ivanov
2026-10-13 11:16 ` [Devel] [PATCH hci-8.1 v6 12/12] dm-ploop: truncate unused tail of top delta on suspend Alexander Ivanov
2026-10-13 11:38   ` no-reply
2026-10-13 12:53     ` Alexander Ivanov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=010701a1350065d1-dd3b16d9-5723-4098-be08-d73cb778e5fb-000000@eu-central-1.amazonses.com \
    --to=no-reply@sashiko.virtuozzo.com \
    --cc=alexander.ivanov@virtuozzo.com \
    --cc=den@openvz.org \
    --cc=devel@openvz.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.