All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] dm-integrity: reject recalculation when recalc_wq was never allocated
@ 2026-11-20 15:31 Yang Luohan
  2026-11-20 15:44 ` sashiko-bot
  0 siblings, 1 reply; 2+ messages in thread
From: Yang Luohan @ 2026-11-20 15:31 UTC (permalink / raw)
  To: Mikulas Patocka; +Cc: dm-devel, linux-kernel, Mike Snitzer, Benjamin Marzinski

dm_integrity_resume() re-reads the superblock from the underlying device
so that it picks up the flags and the recalculation position.  The
constructor refuses a superblock that carries SB_FLAG_RECALCULATING when
no internal hash is configured, because recalculation needs
ic->recalc_work and ic->recalc_wq is only allocated when
ic->internal_hash is set:

	} else {
		if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING)) {
			ti->error = "Recalculate can only be specified with internal_hash";
			r = -EINVAL;
			goto bad;
		}
	}

The superblock that is read back on resume is not subject to that check.
superblock_changed() only compares magic, log2_interleave_sectors,
integrity_tag_size, journal_sections, log2_sectors_per_block, the salt
and the SB_FLAG_HAVE_JOURNAL_MAC, SB_FLAG_FIXED_PADDING,
SB_FLAG_FIXED_HMAC and SB_FLAG_INLINE flags, so SB_FLAG_RECALCULATING
can be added to the on-disk superblock while the device is suspended.
Resume then reaches

	if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING)) {
		__u64 recalc_pos = le64_to_cpu(ic->sb->recalc_sector);

		if (recalc_pos < ic->provided_data_sectors) {
			queue_work(ic->recalc_wq, &ic->recalc_work);

with ic->recalc_wq == NULL and dereferences a null pointer.

When the recalculation support was introduced, the call was guarded:

	if (ic->recalc_wq && ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING))

commit 468dfca38b1a ("dm integrity: add a bitmap mode") dropped the
ic->recalc_wq part of the condition while restructuring the surrounding
code, so the null dereference has been reachable since that commit.

commit 875a7161fe15 ("dm-integrity: don't recalculate with HMAC because
of a dirty bitmap") re-applied the constructor's policy inside the
SB_FLAG_DIRTY_BITMAP branch, which avoids the null dereference for a
superblock that gets SB_FLAG_RECALCULATING set by resume itself, but the
standalone SB_FLAG_RECALCULATING branch further down in the same function
still queues the work unconditionally.

Check ic->recalc_wq before queueing the work.  If recalculation is
requested but no internal hash is configured, report the inconsistent
superblock and leave the device alone instead of panicking.

Reproduced on 7.3.0 by loading a dm-integrity target in J mode without
internal_hash, suspending it, writing SB_FLAG_RECALCULATING with
recalc_sector = 0 into the on-disk superblock of the backing device, and
resuming:

	Oops: general protection fault, probably for non-canonical address 0xdffffc0000000020
	KASAN: null-ptr-deref in range [0x0000000000000100-0x0000000000000107]
	RIP: 0010:__queue_work+0x95/0x1030
	Call Trace:
	 queue_work_on
	 dm_integrity_resume
	 dm_table_resume_targets
	 __dm_resume
	 dm_resume
	 dev_suspend
	 ctl_ioctl
	Kernel panic - not syncing: Fatal exception

Fixes: 468dfca38b1a ("dm integrity: add a bitmap mode")
Cc: stable@vger.kernel.org
Reported-by: Yang Luohan <ylh440104@gmail.com>
Signed-off-by: Yang Luohan <ylh440104@gmail.com>

--- a/drivers/md/dm-integrity.c
+++ b/drivers/md/dm-integrity.c
@@ -4046,6 +4046,11 @@ static void dm_integrity_resume(struct dm_target *ti)
 	if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING)) {
 		__u64 recalc_pos = le64_to_cpu(ic->sb->recalc_sector);
 
+		if (unlikely(!ic->recalc_wq)) {
+			DMERR("Recalculate requested, but internal_hash is not used");
+			dm_integrity_io_error(ic, "inconsistent superblock", -EINVAL);
+			goto skip_writes;
+		}
 		DEBUG_print("recalc pos: %llx / %llx\n", recalc_pos, ic->provided_data_sectors);
 		if (recalc_pos < ic->provided_data_sectors) {
 			queue_work(ic->recalc_wq, &ic->recalc_work);

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] dm-integrity: reject recalculation when recalc_wq was never allocated
  2026-11-20 15:31 [PATCH] dm-integrity: reject recalculation when recalc_wq was never allocated Yang Luohan
@ 2026-11-20 15:44 ` sashiko-bot
  0 siblings, 0 replies; 2+ messages in thread
From: sashiko-bot @ 2026-11-20 15:44 UTC (permalink / raw)
  To: Yang Luohan, Mikulas Patocka; +Cc: dm-devel, Mike Snitzer, Benjamin Marzinski

Thank you for your contribution! Sashiko AI review found 1 potential
issue to consider:
- [Medium] dm-integrity: unrestored superblock on recalc error in
  dm_integrity_resume()
--

commit 1f55191efead476e43582fdac18ace4fb279e159
Author: Yang Luohan <ylh440104@gmail.com>

dm-integrity: reject recalculation when recalc_wq was never allocated

This commit checks that ic->recalc_wq is non-NULL in dm_integrity_resume()
before queueing ic->recalc_work when SB_FLAG_RECALCULATING is set in the
superblock.

> diff --git a/drivers/md/dm-integrity.c b/drivers/md/dm-integrity.c
> index 6968e38888db9..deddec9de3e1b 100644
> --- a/drivers/md/dm-integrity.c
> +++ b/drivers/md/dm-integrity.c
> @@ -4046,6 +4046,11 @@ static void dm_integrity_resume(struct dm_target *ti)
>  	if (ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING)) {
>  		__u64 recalc_pos = le64_to_cpu(ic->sb->recalc_sector);
>  
> +		if (unlikely(!ic->recalc_wq)) {
> +			DMERR("Recalculate requested, but internal_hash is not used");
> +			dm_integrity_io_error(ic, "inconsistent superblock", -EINVAL);
> +			goto skip_writes;
> +		}
>  		DEBUG_print("recalc pos: %llx / %llx\n", recalc_pos, ic->provided_data_sectors);
>  		if (recalc_pos < ic->provided_data_sectors) {
>  			queue_work(ic->recalc_wq, &ic->recalc_work);

[Severity: Medium]
Should this check run alongside superblock_changed() and restore ic->sb
from ic->sb_copy before jumping to skip_writes?

Checking !ic->recalc_wq at the end of dm_integrity_resume() runs after the
journal replay and superblock write paths have already executed, and leaves
SB_FLAG_RECALCULATING set in the in-memory ic->sb->flags.  As a result,
dm_integrity_status() reports recalculate enabled without internal_hash,
and if the on-disk superblock is repaired while suspended, a later
dm_integrity_resume() captures SB_FLAG_RECALCULATING from the stale
in-memory ic->sb into flags before sync_rw_sb(ic, REQ_OP_READ) and writes
it back to disk:

drivers/md/dm-integrity.c:dm_integrity_resume() {
	...
	flags = ic->sb->flags & cpu_to_le32(SB_FLAG_RECALCULATING);
	if (ic->discard_keyed)
		flags |= cpu_to_le32(SB_FLAG_DISCARD_KEYED);
	r = sync_rw_sb(ic, REQ_OP_READ);
	...
	if ((ic->sb->flags & flags) != flags) {
		ic->sb->flags |= flags;
		r = sync_rw_sb(ic, REQ_OP_WRITE | REQ_FUA);
	...
}

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/179518869380.2402.12815883988413214937@gmail.com?part=1

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-20 15:44 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 15:31 [PATCH] dm-integrity: reject recalculation when recalc_wq was never allocated Yang Luohan
2026-11-20 15:44 ` sashiko-bot

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.