Skip to contentSkip to conversation

nvme: Fix discard buffer overrun

PATCH New Keith Busch 3 participants 7 messages Feb 1, 2018 – Feb 2, 2018 NVMe

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvme: Fix discard buffer overrun

This patch checks the discard range array bounds before setting it in case the driver gets a badly formed request.

Signed-off-by Keith Busch <keith.busch at intel.com>
Cc Jens Axboe <axboe at kernel.dk>
---
 drivers/nvme/host/core.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/nvme/host/core.c +5 -3
index 4bfb4ba6cd14..d02906a6dabe 100644
@@ -518,9 +518,11 @@ static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req,
 		u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector);
 		u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
 
-		range[n].cattr = cpu_to_le32(0);
-		range[n].nlb = cpu_to_le32(nlb);
-		range[n].slba = cpu_to_le64(slba);
+		if (n < segments) {
+			range[n].cattr = cpu_to_le32(0);
+			range[n].nlb = cpu_to_le32(nlb);
+			range[n].slba = cpu_to_le64(slba);
+		}
 		n++;
 	}
 
Signature · 1 line
2.14.3
#1 lore raw

PATCH 1/1 nvme: Fix discard buffer overrun

Hi Keith

Since each discard bio merged into a request is counted as one segment, why not let blk_rq_nr_discard_segments return numbers of bios directly.

Quoted 27 lines · Keith Busch

This patch checks the discard range array bounds before setting it in case the driver gets a badly formed request.

Signed-off-by Keith Busch <keith.busch at intel.com>
Cc Jens Axboe <axboe at kernel.dk>
---
 drivers/nvme/host/core.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/nvme/host/core.c +5 -3
index 4bfb4ba6cd14..d02906a6dabe 100644
@@ -518,9 +518,11 @@ static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req,
 		u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector);
 		u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
 
-		range[n].cattr = cpu_to_le32(0);
-		range[n].nlb = cpu_to_le32(nlb);
-		range[n].slba = cpu_to_le64(slba);
+		if (n < segments) {
+			range[n].cattr = cpu_to_le32(0);
+			range[n].nlb = cpu_to_le32(nlb);
+			range[n].slba = cpu_to_le64(slba);
+		}
 		n++;
 	}
#2 lore raw

PATCH 1/1 nvme: Fix discard buffer overrun

Quoted 4 lines · jianchao.wang

Hi Keith

Since each discard bio merged into a request is counted as one segment, why not let blk_rq_nr_discard_segments return numbers of bios directly.

Right, I'm all for fixing the discard segment count. At the same time, I don't want to depend on it's correctness to not access out-of-bounds array indices.

#3 lore raw

PATCH 1/1 nvme: Fix discard buffer overrun

Hi Keith

Thanks for your kindly response.

Quoted 10 lines · Keith Busch
Quoted 4 lines · jianchao.wang

Hi Keith

Since each discard bio merged into a request is counted as one segment, why not let blk_rq_nr_discard_segments return numbers of bios directly.

Right, I'm all for fixing the discard segment count. At the same time, I don't want to depend on it's correctness to not access out-of-bounds array indices.

Actually, if we use iterating request bio list to return discard segments count, we won't have any out-of-bounds error any more. And things will be simpler. :)

Thanks
Jianchao

maintainer
#4 lore raw

PATCH 1/1 nvme: Fix discard buffer overrun

In reply to jianchao.wang
Quoted 4 lines · jianchao.wang

Hi Keith

Since each discard bio merged into a request is counted as one segment, why not let blk_rq_nr_discard_segments return numbers of bios directly.

That's a good idea, but for now this looks fine. Both this and the core block hole can be fixed, and then someone can hopefully work on making discards behave a bit more nicely after that.

Signature · 1 line
Jens Axboe
#5 lore raw

PATCH 1/1 nvme: Fix discard buffer overrun

Hi Jens

Thanks for your kindly response.

Quoted 10 lines · Jens Axboe
Quoted 4 lines · jianchao.wang

Hi Keith

Since each discard bio merged into a request is counted as one segment, why not let blk_rq_nr_discard_segments return numbers of bios directly.

That's a good idea, but for now this looks fine. Both this and the core block hole can be fixed, and then someone can hopefully work on making discards behave a bit more nicely after that.

Yes, got it.

Thanks
Jianchao

maintainer
#6 lore raw

PATCH 1/1 nvme: Fix discard buffer overrun

In reply to Keith Busch
Quoted 17 lines · Keith Busch

This patch checks the discard range array bounds before setting it in case the driver gets a badly formed request.

Signed-off-by Keith Busch <keith.busch at intel.com>
Cc Jens Axboe <axboe at kernel.dk>
---
 drivers/nvme/host/core.c | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/nvme/host/core.c +0 -0
index 4bfb4ba6cd14..d02906a6dabe 100644
@@ -518,9 +518,11 @@ static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req,
 		u64 slba = nvme_block_nr(ns, bio->bi_iter.bi_sector);
 		u32 nlb = bio->bi_iter.bi_size >> ns->lba_shift;
 

drivers/nvme/host/core.c static blk_status_t nvme_setup_discard(struct nvme_ns *ns, struct request *req, · line 527

Quoted 10 lines
drivers/nvme/host/core.c +5 -3
-		range[n].cattr = cpu_to_le32(0);
-		range[n].nlb = cpu_to_le32(nlb);
-		range[n].slba = cpu_to_le64(slba);
+		if (n < segments) {
+			range[n].cattr = cpu_to_le32(0);
+			range[n].nlb = cpu_to_le32(nlb);
+			range[n].slba = cpu_to_le64(slba);
+		}
 		n++;
 	}
Reviewed-by Jens Axboe <axboe at kernel.dk>
Signature · 1 line
Jens Axboe