2023-09-08 22:27:39

by Sasha Levin

[permalink] [raw]
Subject: [PATCH AUTOSEL 4.14 2/3] md: raid1: fix potential OOB in raid1_remove_disk()

From: Zhang Shurong <[email protected]>

[ Upstream commit 8b0472b50bcf0f19a5119b00a53b63579c8e1e4d ]

If rddev->raid_disk is greater than mddev->raid_disks, there will be
an out-of-bounds in raid1_remove_disk(). We have already found
similar reports as follows:

1) commit d17f744e883b ("md-raid10: fix KASAN warning")
2) commit 1ebc2cec0b7d ("dm raid: fix KASAN warning in raid5_remove_disk")

Fix this bug by checking whether the "number" variable is
valid.

Signed-off-by: Zhang Shurong <[email protected]>
Reviewed-by: Yu Kuai <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Song Liu <[email protected]>
Signed-off-by: Sasha Levin <[email protected]>
---
drivers/md/raid1.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/drivers/md/raid1.c b/drivers/md/raid1.c
index 28f78199de3ba..3e54b6639e213 100644
--- a/drivers/md/raid1.c
+++ b/drivers/md/raid1.c
@@ -1775,6 +1775,10 @@ static int raid1_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
struct r1conf *conf = mddev->private;
int err = 0;
int number = rdev->raid_disk;
+
+ if (unlikely(number >= conf->raid_disks))
+ goto abort;
+
struct raid1_info *p = conf->mirrors + number;

if (rdev != p->rdev)
--
2.40.1


2023-09-12 16:20:37

by Song Liu

[permalink] [raw]
Subject: Re: [PATCH AUTOSEL 4.14 2/3] md: raid1: fix potential OOB in raid1_remove_disk()

On Mon, Sep 11, 2023 at 2:49 AM Pavel Machek <[email protected]> wrote:
>
> Hi!
>
> > From: Zhang Shurong <[email protected]>
> >
> > [ Upstream commit 8b0472b50bcf0f19a5119b00a53b63579c8e1e4d ]
> >
> > If rddev->raid_disk is greater than mddev->raid_disks, there will be
> > an out-of-bounds in raid1_remove_disk(). We have already found
> > similar reports as follows:
> >
> > 1) commit d17f744e883b ("md-raid10: fix KASAN warning")
> > 2) commit 1ebc2cec0b7d ("dm raid: fix KASAN warning in raid5_remove_disk")
> >
> > Fix this bug by checking whether the "number" variable is
> > valid.
>
> > +++ b/drivers/md/raid1.c
> > @@ -1775,6 +1775,10 @@ static int raid1_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
> > struct r1conf *conf = mddev->private;
> > int err = 0;
> > int number = rdev->raid_disk;
> > +
> > + if (unlikely(number >= conf->raid_disks))
> > + goto abort;
> > +
> > struct raid1_info *p = conf->mirrors + number;
> >
> > if (rdev != p->rdev)
>
> Wow. Mixing declarations and code. I'm pretty sure that's not ok
> according to our coding style, and I'd be actually surprised if all
> our compiler configurations allowed this.
>

We have a fix queued for this:

https://git.kernel.org/pub/scm/linux/kernel/git/song/md.git/commit/?h=md-fixes&id=df203da47f4428bc286fc99318936416253a321c

Thanks,
Song