mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [BUG] md: slot_store() lets raid1/raid10 index past conf->mirrors with a stale delta_disks from the superblock
@ 2026-11-17  5:10 Kyuwon Cho
  0 siblings, 0 replies; only message in thread
From: Kyuwon Cho @ 2026-11-17  5:10 UTC (permalink / raw)
  To: Song Liu, Yu Kuai; +Cc: Li Nan, Xiao Ni, Yogesh Gaur, linux-raid, linux-kernel

[-- Attachment #1: Type: text/plain, Size: 3289 bytes --]

Hi Song, Kuai,

While testing v7.4-rc3 with an LLM-guided test generator that we are
developing as a research prototype, we found out-of-bounds reads in
raid10_add_disk() and raid1_add_disk(), reached by writing a spare's
sysfs "slot" on an array whose superblocks say that no reshape is in
progress but still carry a positive delta_disks:

  BUG: KASAN: slab-out-of-bounds in raid10_add_disk+0x648/0x7b0
  Read of size 8 at addr ff110000137f6f60 by task test/106
  Call Trace:
   <TASK>
   raid10_add_disk+0x648/0x7b0
   slot_store+0x6f9/0xb30
   rdev_attr_store+0x17a/0x440
   kernfs_fop_write_iter+0x2f0/0x490
   vfs_write+0x5f8/0x11e0
   ksys_write+0xf4/0x1c0
   do_syscall_64+0x116/0x630
   entry_SYSCALL_64_after_hwframe+0x77/0x7f
   </TASK>

  BUG: KASAN: slab-out-of-bounds in raid1_add_disk+0x47b/0x560
  Read of size 8 at addr ff11000012dadb80 by task test/108
  Call Trace:
   <TASK>
   raid1_add_disk+0x47b/0x560
   slot_store+0x6f9/0xb30
   rdev_attr_store+0x17a/0x440
   ...

This comes from the state that 0669c7dbd1d3 ("md/raid5: don't BUG() on
an inconsistent reshape state from the superblock") made raid5_run()
reject. super_90_validate() (minor version 91) and super_1_validate()
(MD_FEATURE_RESHAPE_ACTIVE) copy delta_disks from disk without checking
it against reshape_position, so a RAID1 or RAID10 array runs with
reshape_position == MaxSector and delta_disks > 0. slot_store() then
accepts any slot below raid_disks + delta_disks:

		if (slot >= rdev->mddev->raid_disks &&
		    slot >= rdev->mddev->raid_disks + rdev->mddev->delta_disks)
			return -ENOSPC;

but neither personality sized conf->mirrors for those slots: raid10's
setup_conf() allocates raid_disks + max(0, -delta_disks) entries and
raid1's allocates raid_disks * 2, and raid10_add_disk()/raid1_add_disk()
use the slot as an index. With four RAID10 disks and delta_disks = 1,
or two RAID1 disks and delta_disks = 3, writing slot=4 reads the entry
just past the array.

Letting slot_store() go beyond raid_disks only while a reshape is in
progress (reshape_position != MaxSector) makes both go away here: each
reproducer ran three times without a report, and the slot write fails
with -ENOSPC. Rejecting the inconsistent state when the superblock is
validated, or in each personality as 0669c7dbd1d3 does for raid5, would
be other ways.

Both reproducers need root (CAP_SYS_ADMIN for the md ioctls and sysfs
files). They write superblocks with reshape position MaxSector and a
positive delta_disks to loop or ram devices: 0.90, minor version 91, for
RAID10, and 1.2 with the reshape feature bit for RAID1. Each then
assembles the array with ADD_NEW_DISK and RUN_ARRAY, adds a spare and
writes "4" to its slot attribute; the RAID10 one also has a control case
with delta_disks 0, where the write gets -ENOSPC. On an x86_64 QEMU guest
with 2 vCPUs and KASAN (gcc 13.3, config attached), each triggers in
every run (3 of 3).

Checked against v7.4-rc3 (aebb72744764); slot_store(), the validators
and raid1.c/raid10.c are the same in the md tree's md-7.4 branch. We
found no earlier report on syzbot or on linux-raid. The
super_90_validate() desc_nr fix posted on 2026-11-16 is a different
problem.

The reproducers (repro-raid10.c, repro-raid1.c) and the kernel config
are attached.

Thanks,
Kyuwon Cho

[-- Attachment #2: repro-raid10.c --]
[-- Type: text/plain, Size: 11018 bytes --]

// Run as root. Assembles a RAID10 from four loop devices whose 0.90 superblocks
// (minor_version 91) say "no reshape" but carry delta_disks = +1 (V6), adds a
// spare, and writes slot=4 for it through sysfs. V7 is the same with delta_disks
// = 0, where slot=4 is rejected.
//
// Reach super_90_validate() with a 0.90 superblock (minor_version 91) whose
// reshape_position is MaxSector (no reshape) but whose delta_disks / new_level /
// new_layout / new_chunk describe a reshape. Assembly is done through md ioctls
// (SET_ARRAY_INFO raid_disks=0 -> ADD_NEW_DISK -> RUN_ARRAY), so the validator
// runs from analyze_sbs() with mddev->raid_disks == 0 and copies the fields.
#define _GNU_SOURCE
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <sys/types.h>
#include <unistd.h>

/* ---- md uapi (include/uapi/linux/raid/md_u.h, MD_MAJOR 9) ---- */
#define MDT 9
#define IOC_W(nr, sz) ((1u << 30) | ((unsigned)(sz) << 16) | (MDT << 8) | (nr))
#define IOC_N(nr)     ((MDT << 8) | (nr))
#define ADD_NEW_DISK_CMD    IOC_W(0x21, 20)
#define SET_ARRAY_INFO_CMD  IOC_W(0x23, 72)
#define RUN_ARRAY_CMD       IOC_W(0x30, 12)
#define STOP_ARRAY_CMD      IOC_N(0x32)

typedef struct {
	int major_version, minor_version, patch_version;
	unsigned int ctime;
	int level, size, nr_disks, raid_disks, md_minor, not_persistent;
	unsigned int utime;
	int state, active_disks, working_disks, failed_disks, spare_disks;
	int layout, chunk_size;
} mdu_array_info_t;

typedef struct {
	int number, major, minor, raid_disk, state;
} mdu_disk_info_t;

typedef struct {
	int personality, chunk_size, max_fault;
} mdu_param_t;

_Static_assert(sizeof(mdu_array_info_t) == 72, "array info size");
_Static_assert(sizeof(mdu_disk_info_t) == 20, "disk info size");
_Static_assert(sizeof(mdu_param_t) == 12, "param size");

/* ---- loop uapi (loop.h) ---- */
#define LOOP_SET_FD_CMD     0x4C00
#define LOOP_CLR_FD_CMD     0x4C01
#define LOOP_CTL_GET_FREE_CMD 0x4C82

/* MD_DISK_FAULTY is bit 0, MD_DISK_ACTIVE bit 1, MD_DISK_SYNC bit 2 (md_p.h). */
#define DISK_ACT_SYNC ((1u << 1) | (1u << 2))

#define SB_WORDS   1024          /* 4096-byte mdp_super_t, in u32 words */
#define IMG_BYTES  (64ULL << 20)
#define MaxSectorU (~0ULL)       /* MaxSector as u64 */

struct var {
	const char *name;
	int minor;                  /* md minor (/dev/mdN) */
	int level, nd, delta, new_level;
	uint32_t layout, new_layout, chunk;   /* chunk in bytes */
	int io;                     /* do I/O through the array if it runs */
	int spare;                  /* 1: one extra device whose sb is a spare (raid_disk -1) */
};

static const struct var vars[] = {
	/* V6: raid10 with stale delta_disks=+1 (MaxSector). A spare (raid_disk -1) is
	 * bound to the running array; sysfs slot=4 passes slot_store's
	 * "slot >= raid_disks + delta_disks" check and reaches raid10_add_disk. */
	{ "V6 raid10 delta=+1 spare slot=4 (stale delta)", 16, 10, 4, 1, 10, 0x102, 0x102, 65536, 0, 1 },
	/* V7: control, identical except delta=0 -- slot=4 must be rejected with ENOSPC */
	{ "V7 raid10 delta=0 spare slot=4 (control)",      17, 10, 4, 0, 10, 0x102, 0x102, 65536, 0, 1 },
};

static uint32_t csum_sb(const uint32_t *w)
{
	uint64_t s = 0;
	for (int i = 0; i < SB_WORDS; i++)
		s += w[i];
	return (uint32_t)((s & 0xffffffffULL) + (s >> 32));
}

/* Word offsets follow mdp_super_t in include/uapi/linux/raid/md_p.h. */
static void build_sb(uint32_t *w, const struct var *v, int idx, int spare)
{
	memset(w, 0, 4096);
	w[0]  = 0xa92b4efc;          /* MD_SB_MAGIC */
	w[1]  = 0;                   /* major_version */
	w[2]  = 91;                  /* minor_version: reshape fields valid */
	w[3]  = 0;                   /* patch_version */
	w[5]  = 0x1111;              /* set_uuid0..3 */
	w[6]  = 1700000000;          /* ctime */
	w[7]  = v->level;
	w[8]  = 16384;               /* size, KiB per device */
	w[9]  = v->nd;               /* nr_disks */
	w[10] = v->nd;               /* raid_disks */
	w[11] = v->minor;            /* md_minor */
	w[12] = 0;                   /* not_persistent */
	w[13] = 0x2222;
	w[14] = 0x3333;
	w[15] = 0x4444;
	w[32] = 1700000000;          /* utime */
	w[33] = 1;                   /* state: MD_SB_CLEAN */
	w[34] = v->nd;               /* active_disks */
	w[35] = v->nd;               /* working_disks */
	w[39] = 1;                   /* events_lo (little endian) */
	w[41] = 1;                   /* cp_events_lo */
	w[43] = 0;                   /* recovery_cp */
	w[44] = (uint32_t)(MaxSectorU & 0xffffffffu);   /* reshape_position = MaxSector */
	w[45] = (uint32_t)(MaxSectorU >> 32);
	w[46] = (uint32_t)v->new_level;
	w[47] = (uint32_t)v->delta;  /* delta_disks, two's complement */
	w[48] = v->new_layout;
	w[49] = v->chunk;            /* new_chunk, bytes */
	w[64] = v->layout;           /* layout */
	w[65] = v->chunk;            /* chunk_size, bytes */
	for (int k = 0; k < v->nd; k++) {
		uint32_t *d = &w[128 + 32 * k];   /* disks[k], 32 words each */
		d[0] = k; d[1] = 7; d[2] = k; d[3] = k; d[4] = DISK_ACT_SYNC;
	}
	uint32_t *t = &w[992];        /* this_disk */
	if (spare) {
		/* spare: desc state 0, raid_disk -1 (validate leaves rdev->raid_disk at -1) */
		t[0] = idx; t[1] = 7; t[2] = idx; t[3] = 0xffffffffu; t[4] = 0;
	} else {
		t[0] = idx; t[1] = 7; t[2] = idx; t[3] = idx; t[4] = DISK_ACT_SYNC;
	}
	w[38] = 0;                    /* sb_csum must be 0 while summing */
	w[38] = csum_sb(w);
}

static void dump_path(const char *path, const char *what)
{
	char s[64] = {0};
	int f = open(path, O_RDONLY);
	if (f < 0) {
		printf("%s: open %s errno=%d (%s)\n", what, path, errno, strerror(errno));
		return;
	}
	ssize_t n = read(f, s, sizeof s - 1);
	close(f);
	if (n > 0 && s[n - 1] == '\n')
		s[n - 1] = 0;
	printf("%s: %s\n", what, n > 0 ? s : "(empty)");
}

static void dump_attr(int minor, const char *attr)
{
	char p[96];
	snprintf(p, sizeof p, "/sys/block/md%d/md/%s", minor, attr);
	dump_path(p, attr);
}

static void ensure_node(const char *path, mode_t mode, unsigned maj, unsigned min)
{
	if (mknod(path, mode, makedev(maj, min)) < 0 && errno != EEXIST)
		perror(path);
}

static int run_var(const struct var *v, int lc)
{
	uint32_t w[SB_WORDS] __attribute__((aligned(4096)));
	int lfd[8], loopn[8];
	char img[80], ldev[32], mdp[32], sysp[96];
	int err_ret = 0;

	printf("\n== %s\n", v->name);
	fflush(stdout);

	int ndev = v->nd + v->spare;

	/* Backing files with 0.90 superblocks at MD_NEW_SIZE_SECTORS(size). */
	for (int k = 0; k < ndev; k++) {
		snprintf(img, sizeof img, "/tmp/mdimg-%d-%d", v->minor, k);
		int fd = open(img, O_CREAT | O_RDWR | O_TRUNC, 0600);
		if (fd < 0) { perror("open img"); return -1; }
		if (ftruncate(fd, IMG_BYTES) < 0) { perror("ftruncate"); return -1; }
		build_sb(w, v, k, k >= v->nd);
		uint64_t sectors = IMG_BYTES >> 9;
		uint64_t sb_sector = (sectors & ~127ULL) - 128;
		if (pwrite(fd, w, 4096, sb_sector * 512) != 4096) { perror("pwrite sb"); return -1; }
		close(fd);

		int n = ioctl(lc, LOOP_CTL_GET_FREE_CMD);
		if (n < 0) { perror("LOOP_CTL_GET_FREE"); return -1; }
		loopn[k] = n;
		snprintf(ldev, sizeof ldev, "/dev/loop%d", n);
		ensure_node(ldev, S_IFBLK | 0600, 7, n);
		lfd[k] = open(ldev, O_RDWR);
		if (lfd[k] < 0) { perror("open loop"); return -1; }
		int ffd = open(img, O_RDWR);
		if (ffd < 0 || ioctl(lfd[k], LOOP_SET_FD_CMD, ffd) < 0) {
			perror("LOOP_SET_FD"); return -1;
		}
		close(ffd);
	}

	snprintf(mdp, sizeof mdp, "/dev/md%d", v->minor);
	ensure_node(mdp, S_IFBLK | 0600, 9, v->minor);
	int mfd = open(mdp, O_RDWR);
	if (mfd < 0) { perror("open md"); return -1; }

	/* Set the superblock version only; raid_disks == 0 so the first
	 * ADD_NEW_DISK imports a device and RUN_ARRAY goes through analyze_sbs(). */
	mdu_array_info_t ai;
	memset(&ai, 0, sizeof ai);
	ai.major_version = 0;
	ai.minor_version = 91;
	ai.patch_version = 0;
	if (ioctl(mfd, SET_ARRAY_INFO_CMD, &ai) < 0)
		printf("SET_ARRAY_INFO: errno=%d (%s)\n", errno, strerror(errno));

	for (int k = 0; k < ndev; k++) {
		mdu_disk_info_t di;
		memset(&di, 0, sizeof di);
		di.number = k;
		di.major = 7;
		di.minor = loopn[k];
		di.raid_disk = k;
		di.state = 0;
		if (ioctl(mfd, ADD_NEW_DISK_CMD, &di) < 0)
			printf("ADD_NEW_DISK[%d]: errno=%d (%s)\n", k, errno, strerror(errno));
	}

	mdu_param_t p;
	memset(&p, 0, sizeof p);
	int r = ioctl(mfd, RUN_ARRAY_CMD, &p);
	printf("RUN_ARRAY: ret=%d errno=%d (%s)\n", r, r < 0 ? errno : 0,
	       r < 0 ? strerror(errno) : "ok");
	err_ret = r;

	if (r == 0 && v->spare) {
		/* Let md's own spare/recovery handling settle, then show the state. */
		sleep(3);
		dump_attr(v->minor, "sync_action");
		dump_attr(v->minor, "raid_disks");
		snprintf(sysp, sizeof sysp, "/sys/block/md%d/md/dev-loop%d/slot",
			 v->minor, loopn[v->nd]);
		dump_path(sysp, "spare slot");
		snprintf(sysp, sizeof sysp, "/sys/block/md%d/md/dev-loop%d/state",
			 v->minor, loopn[v->nd]);
		dump_path(sysp, "spare state");
		/* Write slot=nd to the spare's sysfs slot (activates the spare at raid_disk nd). */
		snprintf(sysp, sizeof sysp, "/sys/block/md%d/md/dev-loop%d/slot",
			 v->minor, loopn[v->nd]);
		int sf = open(sysp, O_WRONLY);
		if (sf < 0) {
			printf("open slot %s: errno=%d (%s)\n", sysp, errno, strerror(errno));
		} else {
			char s[16];
			int n = snprintf(s, sizeof s, "%d\n", v->nd);
			ssize_t w2 = write(sf, s, n);
			printf("write slot=%d: ret=%zd errno=%d (%s)\n", v->nd, w2,
			       w2 < 0 ? errno : 0, w2 < 0 ? strerror(errno) : "ok");
			close(sf);
		}
		fflush(stdout);
	}

	if (r == 0 && v->io) {
		static char buf[65536], rd[65536];
		for (size_t i = 0; i < sizeof buf; i++)
			buf[i] = (char)(i * 7 + 3);
		ssize_t wr = pwrite(mfd, buf, sizeof buf, 0);
		printf("pwrite md: %zd\n", wr);
		fsync(mfd);
		ssize_t rr = pread(mfd, rd, sizeof rd, 0);
		printf("pread md: %zd, cmp=%d\n", rr,
		       rr == (ssize_t)sizeof rd ? memcmp(buf, rd, sizeof rd) : -1);
		snprintf(sysp, sizeof sysp, "/sys/block/md%d/md/new_level", v->minor);
		int sf = open(sysp, O_RDONLY);
		if (sf >= 0) {
			char s[32] = {0};
			read(sf, s, sizeof s - 1);
			printf("sysfs new_level: %s", s);
			close(sf);
		}
		snprintf(sysp, sizeof sysp, "/sys/block/md%d/md/level", v->minor);
		sf = open(sysp, O_RDONLY);
		if (sf >= 0) {
			char s[32] = {0};
			read(sf, s, sizeof s - 1);
			printf("sysfs level: %s", s);
			close(sf);
		}
	}

	ioctl(mfd, STOP_ARRAY_CMD, 0);
	close(mfd);
	for (int k = 0; k < ndev; k++) {
		ioctl(lfd[k], LOOP_CLR_FD_CMD, 0);
		close(lfd[k]);
	}
	printf("== done: %s (ret=%d)\n", v->name, err_ret);
	fflush(stdout);
	return err_ret;
}

int main(void)
{
	mkdir("/tmp", 01777);
	ensure_node("/dev/loop-control", S_IFCHR | 0600, 10, 237);
	int lc = open("/dev/loop-control", O_RDWR);
	if (lc < 0) { perror("open loop-control"); return 1; }

	for (size_t i = 0; i < sizeof vars / sizeof vars[0]; i++)
		run_var(&vars[i], lc);

	close(lc);
	puts("silverfox-md: all variants done");
	return 0;
}

[-- Attachment #3: repro-raid1.c --]
[-- Type: text/plain, Size: 9200 bytes --]

// Stale delta_disks > 0 with reshape_position == MaxSector in a v1.2 superblock,
// then a spare is hot-added through md sysfs 'slot' at slot == raid_disks.
// Syscalls only: /dev/ram0..2 as members, ioctl assembly, sysfs writes, I/O.
#define _GNU_SOURCE
#define MD_MAJOR 9
#include <errno.h>
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include <sys/types.h>
#include <time.h>
#include <unistd.h>
#include <linux/raid/md_u.h>

#define MD_DISK_ACTIVE 1
#define MD_DISK_SYNC 2
#define SB_MAGIC 0xa92b4efcu
#define FEAT_RESHAPE_ACTIVE 4u
#define MAXS 0xffffffffffffffffULL
// Stale delta_disks in the sb: slot_store allows slots < raid_disks + delta = 5.
// conf->mirrors is raid_disks * 2 = 4 entries, so slot 4 is one past the end.
#define DELTA 3
#define SLOT "4"

static void p16(uint8_t *b, int off, uint16_t v) { memcpy(b + off, &v, 2); }
static void p32(uint8_t *b, int off, uint32_t v) { memcpy(b + off, &v, 4); }
static void p64(uint8_t *b, int off, uint64_t v) { memcpy(b + off, &v, 8); }

// calc_sb_1_csum(): sum of 32-bit words over 256 + 2*max_dev bytes, sb_csum zeroed.
static uint32_t sb_csum(uint8_t *sb, int max_dev) {
	int size = 256 + max_dev * 2;
	uint8_t *p = sb;
	uint64_t s = 0;
	uint32_t w;
	uint16_t h;

	p32(sb, 216, 0);
	for (; size >= 4; size -= 4, p += 4) {
		memcpy(&w, p, 4);
		s += w;
	}
	if (size > 0) {
		memcpy(&h, p, 2);
		s += h;
	}
	return (uint32_t)((s & 0xffffffffULL) + (s >> 32));
}

// v1.2 superblock at byte 4096 (sb_start = 8 sectors). Role table: ram0=0, ram1=1, ram2=spare.
static void make_sb(uint8_t *sb, uint32_t dev_number, int32_t delta_disks) {
	memset(sb, 0, 512);
	p32(sb, 0, SB_MAGIC);
	p32(sb, 4, 1);				// major_version
	p32(sb, 8, FEAT_RESHAPE_ACTIVE);	// feature_map: reshape fields claimed valid
	memset(sb + 16, 0x5a, 16);		// set_uuid
	memcpy(sb + 32, "rca-stale-delta", 15);
	p64(sb, 64, 1700000000ULL);		// ctime
	p32(sb, 72, 1);				// level raid1
	p32(sb, 76, 0);				// layout
	p64(sb, 80, 4096);			// size (sectors)
	p32(sb, 88, 128);			// chunksize
	p32(sb, 92, 2);				// raid_disks
	p32(sb, 96, 0);				// bitmap_offset
	p32(sb, 100, 1);			// new_level
	p64(sb, 104, MAXS);			// reshape_position = MaxSector (no reshape)
	p32(sb, 112, (uint32_t)delta_disks);	// delta_disks: stale, not checked
	p32(sb, 116, 0);			// new_layout
	p32(sb, 120, 128);			// new_chunk
	p32(sb, 124, 0);			// new_offset
	p64(sb, 128, 2048);			// data_offset
	p64(sb, 136, 4096);			// data_size
	p64(sb, 144, 8);			// super_offset
	p64(sb, 152, 0);			// recovery_offset
	p32(sb, 160, dev_number);
	p32(sb, 164, 0);
	memset(sb + 168, 0xa0 + (int)dev_number, 16);
	p64(sb, 192, 1700000000ULL);		// utime
	p64(sb, 200, 1);			// events
	p64(sb, 208, MAXS);			// resync_offset
	p32(sb, 220, 3);			// max_dev
	p32(sb, 224, 512);			// logical_block_size
	p16(sb, 256, 0);			// dev_roles[0] = active 0
	p16(sb, 258, 1);			// dev_roles[1] = active 1
	p16(sb, 260, 0xffff);			// dev_roles[2] = spare
	p32(sb, 216, sb_csum(sb, 3));
}

static void step(const char *name, int rc) {
	printf("STEP %s: %s\n", name, rc < 0 ? strerror(-rc) : "ok");
	fflush(stdout);
}

static int wr_file(const char *path, const char *val) {
	int fd = open(path, O_WRONLY);
	if (fd < 0)
		return -errno;
	ssize_t n = write(fd, val, strlen(val));
	int e = n < 0 ? errno : 0;
	close(fd);
	return n < 0 ? -e : 0;
}

static void show(const char *label, const char *path) {
	char buf[128] = "";
	int fd = open(path, O_RDONLY);
	if (fd >= 0) {
		ssize_t n = read(fd, buf, sizeof(buf) - 1);
		if (n > 0) {
			buf[n] = 0;
			for (ssize_t i = n - 1; i >= 0 && (buf[i] == '\n'); i--)
				buf[i] = 0;
		}
		close(fd);
	}
	printf("STATE %s = [%s]\n", label, buf);
	fflush(stdout);
}

static void wait_idle(const char *sync_path) {
	for (int i = 0; i < 300; i++) {
		char buf[32] = "";
		int fd = open(sync_path, O_RDONLY);
		if (fd < 0)
			return;
		ssize_t n = read(fd, buf, sizeof(buf) - 1);
		close(fd);
		if (n > 0 && !strncmp(buf, "idle", 4))
			return;
		struct timespec ts = { 0, 200 * 1000 * 1000 };
		nanosleep(&ts, NULL);
	}
	printf("STATE wait_idle timed out\n");
}

int main(void) {
	const char *rams[3] = { "/dev/ram0", "/dev/ram1", "/dev/ram2" };
	const char *md = "/sys/block/md0/md";
	char p[256];
	uint8_t sb[512];
	int i, fd, mdfd, rc;

	for (i = 0; i < 3; i++) {
		mknod(rams[i], S_IFBLK | 0600, makedev(1, i));
		fd = open(rams[i], O_RDWR);
		if (fd < 0) {
			printf("STEP open %s: %s\n", rams[i], strerror(errno));
			return 0;
		}
		make_sb(sb, i, DELTA);
		if (pwrite(fd, sb, 512, 4096) != 512)
			printf("STEP pwrite %s failed\n", rams[i]);
		close(fd);
	}
	step("sb written (delta_disks=3, reshape_position=MaxSector)", 0);

	mknod("/dev/md0", S_IFBLK | 0600, makedev(MD_MAJOR, 0));
	mdfd = open("/dev/md0", O_RDWR);
	if (mdfd < 0) {
		step("open /dev/md0", -errno);
		return 0;
	}

	mdu_array_info_t ai;
	memset(&ai, 0, sizeof(ai));
	ai.major_version = 1;
	ai.minor_version = 2;
	ai.ctime = 1700000000;
	ai.level = 1;
	// raid_disks = 0: super_1_validate() only copies the sb reshape fields
	// (delta_disks, reshape_position) into mddev when mddev->raid_disks == 0.
	ai.nr_disks = 0;
	ai.raid_disks = 0;
	ai.utime = 1700000000;
	ai.chunk_size = 65536;
	rc = ioctl(mdfd, SET_ARRAY_INFO, &ai);
	step("SET_ARRAY_INFO", rc < 0 ? -errno : 0);

	for (i = 0; i < 3; i++) {
		mdu_disk_info_t di;
		memset(&di, 0, sizeof(di));
		di.number = i;
		di.major = 1;
		di.minor = i;
		di.raid_disk = i < 2 ? i : -1;
		di.state = i < 2 ? ((1 << MD_DISK_ACTIVE) | (1 << MD_DISK_SYNC)) : 0;
		rc = ioctl(mdfd, ADD_NEW_DISK, &di);
		snprintf(p, sizeof(p), "ADD_NEW_DISK ram%d", i);
		step(p, rc < 0 ? -errno : 0);
	}

	mdu_param_t prm;
	memset(&prm, 0, sizeof(prm));
	rc = ioctl(mdfd, RUN_ARRAY, &prm);
	step("RUN_ARRAY", rc < 0 ? -errno : 0);
	if (rc < 0)
		goto out;

	show("raid_disks", "/sys/block/md0/md/raid_disks");
	show("level", "/sys/block/md0/md/level");
	show("reshape_position", "/sys/block/md0/md/reshape_position");
	show("array_state", "/sys/block/md0/md/array_state");
	show("sync_action", "/sys/block/md0/md/sync_action");
	show("degraded", "/sys/block/md0/md/degraded");
	show("dev-ram2/slot", "/sys/block/md0/md/dev-ram2/slot");
	// RUN starts a resync because the sb is not clean; slot store returns EBUSY until it ends.
	wait_idle("/sys/block/md0/md/sync_action");
	show("sync_action after wait", "/sys/block/md0/md/sync_action");

	// Hot-add the spare at slot == raid_disks (2). Allowed because 2 < raid_disks + delta_disks = 3.
	rc = wr_file("/sys/block/md0/md/dev-ram2/slot", SLOT);
	step("slot store dev-ram2 = " SLOT, rc);
	show("dev-ram2/slot after", "/sys/block/md0/md/dev-ram2/slot");
	wait_idle("/sys/block/md0/md/sync_action");
	show("degraded after add", "/sys/block/md0/md/degraded");
	show("dev-ram2/recovery_start", "/sys/block/md0/md/dev-ram2/recovery_start");

	// Drive recovery onto the slot-2 member (mirrors[2], not In_sync), then wait.
	rc = wr_file("/sys/block/md0/md/sync_action", "recover");
	step("sync_action recover", rc);
	wait_idle("/sys/block/md0/md/sync_action");
	show("sync_action after recover", "/sys/block/md0/md/sync_action");
	show("degraded after recover", "/sys/block/md0/md/degraded");
	show("dev-ram2/recovery_start after", "/sys/block/md0/md/dev-ram2/recovery_start");

	// Fail a normal member so reads and writes depend on the slot-2 device.
	rc = wr_file("/sys/block/md0/md/dev-ram0/state", "faulty");
	step("dev-ram0 faulty", rc);
	wait_idle("/sys/block/md0/md/sync_action");
	{
		static uint8_t back2[1 << 20];
		int io2 = open("/dev/md0", O_RDONLY);
		if (io2 >= 0) {
			ssize_t r2 = pread(io2, back2, sizeof(back2), 0);
			printf("STATE io2 read=%zd first=%02x\n", r2, back2[0]);
			fflush(stdout);
			close(io2);
		}
	}

	// Exercise the new member: write data through the array, read it back.
	{
		static uint8_t pat[1 << 20], back[1 << 20];
		int io = open("/dev/md0", O_RDWR);
		memset(pat, 0x3c, sizeof(pat));
		if (io < 0) {
			step("open md0 io", -errno);
		} else {
			ssize_t w = pwrite(io, pat, sizeof(pat), 0);
			fsync(io);
			ssize_t r = pread(io, back, sizeof(back), 0);
			printf("STATE io write=%zd read=%zd cmp=%d\n", w, r, memcmp(pat, back, sizeof(pat)));
			fflush(stdout);
			close(io);
		}
	}

	rc = wr_file("/sys/block/md0/md/sync_action", "check");
	step("sync_action check", rc);
	wait_idle("/sys/block/md0/md/sync_action");
	show("mismatch_cnt", "/sys/block/md0/md/mismatch_cnt");

	rc = wr_file("/sys/block/md0/md/dev-ram2/state", "faulty");
	step("dev-ram2 faulty", rc);
	wait_idle("/sys/block/md0/md/sync_action");
	show("dev-ram2/slot after faulty", "/sys/block/md0/md/dev-ram2/slot");

	rc = wr_file("/sys/block/md0/md/dev-ram2/state", "remove");
	step("dev-ram2 remove", rc);
	show("dev-ram2/slot after remove", "/sys/block/md0/md/dev-ram2/slot");

	rc = wr_file("/sys/block/md0/md/dev-ram2/slot", "2");
	step("slot store dev-ram2 = 2 again", rc);
	wait_idle("/sys/block/md0/md/sync_action");
	show("array_state end", "/sys/block/md0/md/array_state");

out:
	rc = ioctl(mdfd, STOP_ARRAY, NULL);
	step("STOP_ARRAY", rc < 0 ? -errno : 0);
	close(mdfd);
	return 0;
}

[-- Attachment #4: config.gz --]
[-- Type: application/gzip, Size: 37687 bytes --]

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-17  5:10 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-11-17  5:10 [BUG] md: slot_store() lets raid1/raid10 index past conf->mirrors with a stale delta_disks from the superblock Kyuwon Cho

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®