Skip to contentSkip to conversation

scsi_debug: add support to corrupt data and/or reftag v3

PATCH Applied Christoph Hellwig 3 participants 5 messages Aug 31 – Sep 17 SCSI

lore
Share & tools
lore
#0 lore raw

This will be used for testing PI support in xfstests.

Sashiko complains about a few existing issues, and "duplicate" existing issues because of the capacity wraparound. I do not plan to address them as the use case of wrap-around does not overlap with this, and fixing it partially would be a mess.

Similarly the long non-preemptible sections if injecting a long consecutive range of LBA are intentional, but probably a bad idea for a user to perform. But putting an abritrary limit on the number of blocks would not be helpful either.

Changes since v2:
- protect against fake_rw=1,

Changes since v1:
- use match_int to parse the reftag_adjust option
- don't bypass error cleanup on parsing failure
- allow to corrupt the entire capacity
- set the bug_if_fake_rw to devip2sip to false
- fix the check for unmapped LBAs
- require DIX for reftag corruption injection
- make the kernel messages a bit less verbose
- add calls to sdeb_data_lock and sdeb_data_sector_lock

#1 lore raw

PATCH 1/1 scsi_debug: add support to corrupt data and/or reftag

Add a new debugfs file to inject corruptions of the data and/or reftag. This will be used to detect that protection information and/or file system checksumming can detect random bit errors or misplaced writes.

To use this echo the start LBA, number of logical blocks and type of corruption into the new "corrupt" debugfs file for each scsi_debug device.

For example:

	echo lba=42,num=1,bit_errors=2 > /sys/kernel/debug/scsi_debug/1:0:0:0/corrupt
or
	echo lba=2,num=4,reftag_adjust=8 > /sys/kernel/debug/scsi_debug/1:0:0:0/corrupt

The injection will be used by new xfstests test cases.

---
 drivers/scsi/scsi_debug.c | 168 ++++++++++++++++++++++++++++++++++++++
 1 file changed, 168 insertions(+)
drivers/scsi/scsi_debug.c +168 -0
index 3346562275f8..959177adfc9a 100644
@@ -44,6 +44,7 @@
 #include <linux/debugfs.h>
 #include <linux/async.h>
 #include <linux/cleanup.h>
+#include <linux/parser.h>
 
 #include <net/checksum.h>
 
@@ -1168,6 +1169,95 @@ static const struct file_operations sdebug_error_fops = {
 	.release = single_release,
 };
 
+enum corrupt_options {
+	Opt_lba			= (1u << 0),
+	Opt_num			= (1u << 1),
+	Opt_bit_errors		= (1u << 2),
+	Opt_reftag_adjust	= (1u << 3),
+
+	Opt_invalid,
+};
+
+static const match_table_t corrupt_tokens = {
+	{ Opt_lba,			"lba=%u"		},
+	{ Opt_num,			"num=%u"		},
+	{ Opt_bit_errors,		"bit_errors=%u"		},
+	{ Opt_reftag_adjust,		"reftag_adjust=%d"	},
+	{ Opt_invalid,			NULL,			},
+};
+
+static int corrupt_lbas(struct sdebug_dev_info *devip, u64 lba, u32 num,
+		u32 nr_bit_errors, s32 reftag_adjust);
+static ssize_t sdebug_corrupt_write(struct file *file, const char __user *ubuf,
+		size_t count, loff_t *ppos)
+{
+	struct scsi_device *sdev = file->f_inode->i_private;
+	struct sdebug_dev_info *devip = sdev->hostdata;
+	substring_t args[MAX_OPT_ARGS];
+	char *buf, *options, *p;
+	int error = 0;
+	u64 lba = 0;
+	u32 num = 1;
+	u32 nr_bit_errors = 0;
+	s32 reftag_adjust = 0;
+
+	buf = memdup_user_nul(ubuf, count);
+	if (IS_ERR(buf))
+		return PTR_ERR(buf);
+
+	options = buf;
+	while ((p = strsep(&options, ",\n")) != NULL) {
+		if (!*p)
+			continue;
+		switch (match_token(p, corrupt_tokens, args)) {
+		case Opt_lba:
+			error = match_u64(args, &lba);
+			break;
+		case Opt_num:
+			error = match_uint(args, &num);
+			break;
+		case Opt_bit_errors:
+			error = match_uint(args, &nr_bit_errors);
+			break;
+		case Opt_reftag_adjust:
+			error = match_int(args, &reftag_adjust);
+			break;
+		default:
+			pr_warn("unknown parameter or missing value '%s'\n", p);
+			error = -EINVAL;
+		}
+		if (error)
+			goto out_free_buf;
+	}
+
+	if (num == 0) {
+		pr_warn("invalid number of logical blocks: %u\n", num);
+		error = -EINVAL;
+		goto out_free_buf;
+	}
+
+	if (reftag_adjust &&
+	    (!sdebug_dix ||
+	     (sdebug_dif != T10_PI_TYPE1_PROTECTION &&
+	      sdebug_dif != T10_PI_TYPE2_PROTECTION))) {
+		pr_warn("reftag adjust requires type 1 or type 2 PI with DIX\n");
+		error = -EINVAL;
+		goto out_free_buf;
+	}
+
+	error = corrupt_lbas(devip, lba, num, nr_bit_errors, reftag_adjust);
+
+out_free_buf:
+	kfree(buf);
+	if (error)
+		return error;
+	return count;
+}
+
+static const struct file_operations sdebug_corrupt_fops = {
+	.write		= sdebug_corrupt_write,
+};
+
 static int sdebug_target_reset_fail_show(struct seq_file *m, void *p)
 {
 	struct scsi_target *starget = (struct scsi_target *)m->private;
@@ -4862,6 +4952,82 @@ static int resp_write_tape(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
 	return 0;
 }
 
+static int corrupt_lbas(struct sdebug_dev_info *devip, u64 lba, u32 num,
+		u32 nr_bit_errors, s32 reftag_adjust)
+{
+	struct sdeb_store_info *sip = devip2sip(devip, false);
+	bool meta_data_locked = false;
+	u32 block, num_mapped, b, i;
+	int error = 0;
+
+	if (sdebug_dev_is_zoned(devip) ||
+	    sdebug_dix ||
+	    scsi_debug_lbp())  {
+		sdeb_meta_write_lock(sip);
+		meta_data_locked = true;
+	}
+
+	if (!sip) {
+		pr_err("can't corrupt with fake_rw\n");
+		error = -EINVAL;
+		goto out_unlock;
+	}
+
+	if (num > sdebug_capacity || lba > sdebug_capacity - num) {
+		pr_err("logical blocks out of bounds: %llu:%u", lba, num);
+		error = -EINVAL;
+		goto out_unlock;
+	}
+
+	if (scsi_debug_lbp() &&
+	    (!map_state(sip, lba, &num_mapped) || num > num_mapped)) {
+		pr_err("can't modify unmapped logical blocks: %llu:%u",
+			lba, num);
+		error = -EINVAL;
+		goto out_unlock;
+	}
+
+	/*
+	 * For each possible bit error, flip the same bit in each corrupted
+	 * block to simulate repeated patterns like errors in cables.
+	 */
+	sdeb_data_lock(sip, false);
+	for (b = 0; b < nr_bit_errors; b++) {
+		unsigned int bit;
+
+		bit = get_random_u32_below(sdebug_sector_size * BITS_PER_BYTE);
+		pr_info("flipping bit %u in LBAs %llu:%llu\n",
+				bit, lba, lba + num - 1);
+
+		div_u64_rem(lba, sdebug_store_sectors, &block);
+		for (i = 0; i < num; i++) {
+			u8 *p = sip->storep + (block * sdebug_sector_size);
+
+			sdeb_data_sector_lock(sip, true);
+			p[bit / BITS_PER_BYTE] ^= (1 << (bit % BITS_PER_BYTE));
+			sdeb_data_sector_unlock(sip, true);
+			if (++block >= sdebug_store_sectors)
+				block = 0;
+		}
+	}
+
+	if (reftag_adjust) {
+		pr_info("adjusting reftag by %d sectors for LBAs %llu:%llu\n",
+			reftag_adjust, lba, lba + num - 1);
+		for (i = 0; i < num; i++) {
+			struct t10_pi_tuple *sdt = dif_store(sip, lba + i);
+
+			be32_add_cpu(&sdt->ref_tag, reftag_adjust);
+		}
+	}
+	sdeb_data_unlock(sip, false);
+
+out_unlock:
+	if (meta_data_locked)
+		sdeb_meta_write_unlock(sip);
+	return error;
+}
+
 static int resp_write_dt0(struct scsi_cmnd *scp, struct sdebug_dev_info *devip)
 {
 	bool check_prot;
@@ -6628,6 +6794,8 @@ static int scsi_debug_sdev_configure(struct scsi_device *sdp,
 	if (IS_ERR_OR_NULL(dentry))
 		pr_info("failed to create error file for device %s\n",
 			dev_name(&sdp->sdev_gendev));
+	debugfs_create_file("corrupt", 0600, devip->debugfs_entry, sdp,
+				&sdebug_corrupt_fops);
 
 	return 0;
 }
Signature · 1 line
2.53.0
#2 lore raw

Re: [PATCH] scsi_debug: add support to corrupt data and/or reftag

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider:

New issues:
- [High] Unbounded loops with preemption disabled, combined with excessive kernel logging and wrap-around logic errors, cause CPU lockups and state corruption.
- [Medium] The debugfs write handler passes the raw user-provided count to memdup_user_nul() without capping it, risking kernel allocator warnings.

Pre-existing issues:
- [High] Out-of-bounds memory read in map_state() when Logical Block Provisioning (LBP) and virtual_gb are used together. --

commit 9a79e34fe4a44a1c0a0765e0674426353e4ac98e
Author: Christoph Hellwig <[email protected]>

scsi_debug: add support to corrupt data and/or reftag

This commit adds a new debugfs file "corrupt" to inject data and/or reftag corruptions into scsi_debug devices. This enables testing file system and protection information mechanisms via random bit errors or misplaced writes.

Quoted 4 lines
drivers/scsi/scsi_debug.c +0 -0
index 3346562275f80..959177adfc9a8 100644

[ ... ]

Quoted 3 lines
drivers/scsi/scsi_debug.c +3 -0
+static ssize_t sdebug_corrupt_write(struct file *file, const char __user *ubuf,
+		size_t count, loff_t *ppos)
+{

[ ... ]

drivers/scsi/scsi_debug.c static const struct file_operations sdebug_error_fops = { · line 1204

Quoted 6 lines
drivers/scsi/scsi_debug.c +6 -0
+	u64 lba = 0;
+	u32 num = 1;
+	u32 nr_bit_errors = 0;
+	s32 reftag_adjust = 0;
+
+	buf = memdup_user_nul(ubuf, count);

[Severity: Medium]
Could passing the raw VFS count directly to memdup_user_nul() trigger page allocator warnings?

Since count can be up to ~2GB (MAX_RW_COUNT), and memdup_user_nul() internally uses kmalloc_track_caller() without the __GFP_NOWARN flag, a large write request might cause loud warning stack traces or allocation failures.

[ ... ]

Quoted 3 lines
drivers/scsi/scsi_debug.c +3 -0
+static int corrupt_lbas(struct sdebug_dev_info *devip, u64 lba, u32 num,
+		u32 nr_bit_errors, s32 reftag_adjust)
+{

[ ... ]

drivers/scsi/scsi_debug.c static int resp_write_tape(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) · line 4983

Quoted 2 lines
drivers/scsi/scsi_debug.c +2 -0
+	if (scsi_debug_lbp() &&
+	    (!map_state(sip, lba, &num_mapped) || num > num_mapped)) {

[Severity: High]
This is a pre-existing issue, but does calling map_state() with an un-folded lba cause an out-of-bounds read if virtual_gb > 0?

When map_state() uses test_bit() on lba_to_map_index(lba), it doesn't verify if the index exceeds the allocated map_size. If lba goes up to sdebug_capacity, it can easily exceed the backing store size. This vector was already reachable via block I/O (like GET LBA STATUS) but this patch now exposes it directly via debugfs.

[ ... ]

Quoted 13 lines
drivers/scsi/scsi_debug.c +13 -0
+	/*
+	 * For each possible bit error, flip the same bit in each corrupted
+	 * block to simulate repeated patterns like errors in cables.
+	 */
+	sdeb_data_lock(sip, false);
+	for (b = 0; b < nr_bit_errors; b++) {
+		unsigned int bit;
+
+		bit = get_random_u32_below(sdebug_sector_size * BITS_PER_BYTE);
+		pr_info("flipping bit %u in LBAs %llu:%llu\n",
+				bit, lba, lba + num - 1);
+
+		div_u64_rem(lba, sdebug_store_sectors, &block);

drivers/scsi/scsi_debug.c static int resp_write_tape(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) · line 5012

Quoted 10 lines
drivers/scsi/scsi_debug.c +10 -0
+		for (i = 0; i < num; i++) {
+			u8 *p = sip->storep + (block * sdebug_sector_size);
+
+			sdeb_data_sector_lock(sip, true);
+			p[bit / BITS_PER_BYTE] ^= (1 << (bit % BITS_PER_BYTE));
+			sdeb_data_sector_unlock(sip, true);
+			if (++block >= sdebug_store_sectors)
+				block = 0;
+		}
+	}

[Severity: High]
Can these nested loops cause CPU soft lockups?

Since nr_bit_errors and num are parsed from user input without strict upper bounds, large values might stall the CPU while holding sdeb_data_lock(), which acquires a rwlock in read mode and disables preemption.

Also, if num is larger than sdebug_store_sectors, the wrap-around logic will visit the same physical backing memory multiple times. If a sector is visited an even number of times, does this accidentally undo the bit flips?

drivers/scsi/scsi_debug.c static int resp_write_tape(struct scsi_cmnd *scp, struct sdebug_dev_info *devip) · line 5023

Quoted 10 lines
drivers/scsi/scsi_debug.c +10 -0
+	if (reftag_adjust) {
+		pr_info("adjusting reftag by %d sectors for LBAs %llu:%llu\n",
+			reftag_adjust, lba, lba + num - 1);
+		for (i = 0; i < num; i++) {
+			struct t10_pi_tuple *sdt = dif_store(sip, lba + i);
+
+			be32_add_cpu(&sdt->ref_tag, reftag_adjust);
+		}
+	}
+	sdeb_data_unlock(sip, false);

[Severity: High]
Similar to the wrap-around logic above, if num exceeds sdebug_store_sectors, will dif_store() wrap around and redundantly apply reftag_adjust multiple times to the exact same tuple?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
maintainer Applied
#3 lore raw

Re: [PATCH] scsi_debug: add support to corrupt data and/or reftag

In reply to Christoph Hellwig

Christoph,

Quoted 4 lines

Add a new debugfs file to inject corruptions of the data and/or reftag. This will be used to detect that protection information and/or file system checksumming can detect random bit errors or misplaced writes.

Applied to 7.4/scsi-staging, thanks!

Signature · 1 line
Martin K. Petersen
maintainer Applied
#4 lore raw

Re: [PATCH] scsi_debug: add support to corrupt data and/or reftag

In reply to Christoph Hellwig
Quoted 9 lines · Christoph Hellwig

Add a new debugfs file to inject corruptions of the data and/or reftag. This will be used to detect that protection information and/or file system checksumming can detect random bit errors or misplaced writes.

To use this echo the start LBA, number of logical blocks and type of corruption into the new "corrupt" debugfs file for each scsi_debug device.

[...]

Applied to 7.4/scsi-queue, thanks!

[1/1] scsi_debug: add support to corrupt data and/or reftag

      https://git.kernel.org/mkp/scsi/c/a09d77bcbe22
Signature · 1 line
Martin K. Petersen