nvme: Fix discard buffer overrun
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.
--- drivers/nvme/host/core.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
drivers/nvme/host/core.c +5 -3
@@ -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
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@@ -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++; }
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_segmentsreturn 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.
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_segmentsreturn 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
Jens Axboe
maintainerPATCH 1/1 nvme: Fix discard buffer overrun
In reply to jianchao.wangQuoted 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_segmentsreturn 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
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_segmentsreturn 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
Jens Axboe
maintainerPATCH 1/1 nvme: Fix discard buffer overrun
In reply to Keith BuschQuoted 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@@ -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++; }
Signature · 1 line
Jens Axboe