dm-integrity: reject recalculation when recalc_wq was never allocated
PATCH 1/1 dm-integrity: reject recalculation when recalc_wq was never allocated
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
drivers/md/dm-integrity.c +5 -0
@@ -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);
[email protected]
AI reviewThank 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() --
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 <[email protected]>
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.
Quoted 6 lines
drivers/md/dm-integrity.c +0
-0@@ -4046,6 +4046,11 @@ static void dm_integrity_resume(struct dm_target *ti)if (ic->sb->flags &cpu_to_le32 (SB_FLAG_RECALCULATING )) {
drivers/md/dm-integrity.c static void dm_integrity_resume(struct dm_target *ti) · line 4056
Quoted 10 lines
drivers/md/dm-integrity.c +5
-0__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);
...
}Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1