Skip to contentSkip to conversation

md: raid10: fix use-after-free of bio

PATCH Maik Hampel 3 participants 4 messages Jul 27, 2007 – Jul 30, 2007 MD RAID

lore
Share & tools
lore
#0 lore raw

In case of read errors raid10d tries to print a nice error message, unfortunately using data from an already put bio.

drivers/md/raid10.c +1 -1
index f730a14..ea1b3e3 100644
@@ -1557,7 +1557,6 @@ static void raid10d(mddev_t *mddev)
 			bio = r10_bio->devs[r10_bio->read_slot].bio;
 			r10_bio->devs[r10_bio->read_slot].bio =
 				mddev->ro ? IO_BLOCKED : NULL;
-			bio_put(bio);
 			mirror = read_balance(conf, r10_bio);
 			if (mirror == -1) {
 				printk(KERN_ALERT "raid10: %s: unrecoverable I/O"
@@ -1567,6 +1566,7 @@ static void raid10d(mddev_t *mddev)
 				raid_end_bio_io(r10_bio);
 			} else {
 				const int do_sync = bio_sync(r10_bio->master_bio);
+				bio_put(bio);
 				rdev = conf->mirrors[mirror].rdev;
 				if (printk_ratelimit())
 					printk(KERN_ERR "raid10: %s: redirecting sector %llu to"
#1 lore raw
Quoted 20 lines · On Fri, 27 Jul 2007 16:46:23 +0200 Maik Hampel <[email protected]> wrote:

In case of read errors raid10d tries to print a nice error message, unfortunately using data from an already put bio.

drivers/md/raid10.c +0 -1
index f730a14..ea1b3e3 100644
@@ -1557,7 +1557,6 @@ static void raid10d(mddev_t *mddev)
 			bio = r10_bio->devs[r10_bio->read_slot].bio;
 			r10_bio->devs[r10_bio->read_slot].bio =
 				mddev->ro ? IO_BLOCKED : NULL;
-			bio_put(bio);
 			mirror = read_balance(conf, r10_bio);
 			if (mirror == -1) {
 				printk(KERN_ALERT "raid10: %s: unrecoverable I/O"

drivers/md/raid10.c static void raid10d(mddev_t *mddev) · line 1572

Quoted 7 lines
drivers/md/raid10.c +1 -0
@@ -1567,6 +1566,7 @@ static void raid10d(mddev_t *mddev)
 				raid_end_bio_io(r10_bio);
 			} else {
 				const int do_sync = bio_sync(r10_bio->master_bio);
+				bio_put(bio);
 				rdev = conf->mirrors[mirror].rdev;
 				if (printk_ratelimit())
 					printk(KERN_ERR "raid10: %s: redirecting sector %llu to"

Surely we just leaked that bio if (mirror == -1)?

better:

drivers/md/raid10.c renamed from drivers/md/raid10.c~md-raid10-fix-use-after-free-of-bio +2 -1
@@ -1534,7 +1534,6 @@ static void raid10d(mddev_t *mddev)
 			bio = r10_bio->devs[r10_bio->read_slot].bio;
 			r10_bio->devs[r10_bio->read_slot].bio =
 				mddev->ro ? IO_BLOCKED : NULL;
-			bio_put(bio);
 			mirror = read_balance(conf, r10_bio);
 			if (mirror == -1) {
 				printk(KERN_ALERT "raid10: %s: unrecoverable I/O"
@@ -1542,8 +1541,10 @@ static void raid10d(mddev_t *mddev)
 				       bdevname(bio->bi_bdev,b),
 				       (unsigned long long)r10_bio->sector);
 				raid_end_bio_io(r10_bio);
+				bio_put(bio);
 			} else {
 				const int do_sync = bio_sync(r10_bio->master_bio);
+				bio_put(bio);
 				rdev = conf->mirrors[mirror].rdev;
 				if (printk_ratelimit())
 					printk(KERN_ERR "raid10: %s: redirecting sector %llu to"

_

#2 lore raw

Am Samstag, den 28.07.2007, 23:55 -0700 schrieb Andrew Morton:

Quoted 45 lines · On Fri, 27 Jul 2007 16:46:23 +0200 Maik Hampel <[email protected]> wrote:
Quoted 21 lines

In case of read errors raid10d tries to print a nice error message, unfortunately using data from an already put bio.

drivers/md/raid10.c +0 -1
index f730a14..ea1b3e3 100644
@@ -1557,7 +1557,6 @@ static void raid10d(mddev_t *mddev)
 			bio = r10_bio->devs[r10_bio->read_slot].bio;
 			r10_bio->devs[r10_bio->read_slot].bio =
 				mddev->ro ? IO_BLOCKED : NULL;
-			bio_put(bio);
 			mirror = read_balance(conf, r10_bio);
 			if (mirror == -1) {
 				printk(KERN_ALERT "raid10: %s: unrecoverable I/O"

drivers/md/raid10.c static void raid10d(mddev_t *mddev) · line 1572

Quoted 7 lines
drivers/md/raid10.c +1 -0
@@ -1567,6 +1566,7 @@ static void raid10d(mddev_t *mddev)
 				raid_end_bio_io(r10_bio);
 			} else {
 				const int do_sync = bio_sync(r10_bio->master_bio);
+				bio_put(bio);
 				rdev = conf->mirrors[mirror].rdev;
 				if (printk_ratelimit())
 					printk(KERN_ERR "raid10: %s: redirecting sector %llu to"

Surely we just leaked that bio if (mirror == -1)?

better:

drivers/md/raid10.c renamed from drivers/md/raid10.c~md-raid10-fix-use-after-free-of-bio +0 -1
@@ -1534,7 +1534,6 @@ static void raid10d(mddev_t *mddev)
 			bio = r10_bio->devs[r10_bio->read_slot].bio;
 			r10_bio->devs[r10_bio->read_slot].bio =
 				mddev->ro ? IO_BLOCKED : NULL;
-			bio_put(bio);
 			mirror = read_balance(conf, r10_bio);
 			if (mirror == -1) {
 				printk(KERN_ALERT "raid10: %s: unrecoverable I/O"

drivers/md/raid10.c static void raid10d(mddev_t *mddev) · line 1544

Quoted 4 lines
drivers/md/raid10.c renamed from drivers/md/raid10.c~md-raid10-fix-use-after-free-of-bio +1 -0
@@ -1542,8 +1541,10 @@ static void raid10d(mddev_t *mddev)
 				       bdevname(bio->bi_bdev,b),
 				       (unsigned long long)r10_bio->sector);
 				raid_end_bio_io(r10_bio);
+				bio_put(bio);

raid_end_bio_io() calls put_all_bios(), which does a bio_put() to corresponding r10_bio->devs[i]. So this looks like redundant code for me.

drivers/md/raid10.c static void raid10d(mddev_t *mddev) · line 1550

Quoted 6 lines
drivers/md/raid10.c renamed from drivers/md/raid10.c~md-raid10-fix-use-after-free-of-bio +1 -0
 			} else {
 				const int do_sync = bio_sync(r10_bio->master_bio);
+				bio_put(bio);
 				rdev = conf->mirrors[mirror].rdev;
 				if (printk_ratelimit())
 					printk(KERN_ERR "raid10: %s: redirecting sector %llu to"

Regards,
Maik Hampel

#3 lore raw
Quoted 15 lines · On Monday July 30, [email protected] wrote:
Quoted 15 lines

Surely we just leaked that bio if (mirror == -1)?

better:

drivers/md/raid10.c renamed from drivers/md/raid10.c~md-raid10-fix-use-after-free-of-bio +0 -1
@@ -1534,7 +1534,6 @@ static void raid10d(mddev_t *mddev)
 			bio = r10_bio->devs[r10_bio->read_slot].bio;
 			r10_bio->devs[r10_bio->read_slot].bio =
 				mddev->ro ? IO_BLOCKED : NULL;
-			bio_put(bio);
 			mirror = read_balance(conf, r10_bio);
 			if (mirror == -1) {
 				printk(KERN_ALERT "raid10: %s: unrecoverable I/O"
Quoted 8 lines

drivers/md/raid10.c static void raid10d(mddev_t *mddev) · line 1544

Quoted 5 lines
drivers/md/raid10.c renamed from drivers/md/raid10.c~md-raid10-fix-use-after-free-of-bio +1 -0
@@ -1542,8 +1541,10 @@ static void raid10d(mddev_t *mddev)
 				       bdevname(bio->bi_bdev,b),
 				       (unsigned long long)r10_bio->sector);
 				raid_end_bio_io(r10_bio);
+				bio_put(bio);

raid_end_bio_io() calls put_all_bios(), which does a bio_put() to corresponding r10_bio->devs[i]. So this looks like redundant code for me.

Yes, but note above that this bio for this slot has been set to 'NULL', so put_all_bios won't find anything to call bio_put on.

Thanks,
NeilBrown

Quoted 10 lines

drivers/md/raid10.c static void raid10d(mddev_t *mddev) · line 1550

Quoted 6 lines
drivers/md/raid10.c renamed from drivers/md/raid10.c~md-raid10-fix-use-after-free-of-bio +1 -0
 			} else {
 				const int do_sync = bio_sync(r10_bio->master_bio);
+				bio_put(bio);
 				rdev = conf->mirrors[mirror].rdev;
 				if (printk_ratelimit())
 					printk(KERN_ERR "raid10: %s: redirecting sector %llu to"

Regards,
Maik Hampel