Skip to contentSkip to conversation

nvme-multipath: avoid size_t underflow in ANA log bounds checks

PATCH In review Martin George 2 participants 2 messages Oct 22 NVMe

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvme-multipath: avoid size_t underflow in ANA log bounds checks

The ANA log bounds checks subtract the required size from ana_log_size in unsigned arithmetic, which can wrap when a rogue or buggy target reports an oversized nnsids. Compare as "ana_log_size - offset < required" instead, so the subtraction can't underflow and bypass the check.

---
 drivers/nvme/host/multipath.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/nvme/host/multipath.c +2 -2
index 87b934b46b85..701015022199 100644
@@ -878,7 +878,7 @@ static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data,
 		u32 nr_nsids;
 		size_t nsid_buf_size;
 
-		if (WARN_ON_ONCE(offset > ctrl->ana_log_size - sizeof(*desc)))
+		if (WARN_ON_ONCE(ctrl->ana_log_size - offset < sizeof(*desc)))
 			return -EINVAL;
 
 		nr_nsids = le32_to_cpu(desc->nnsids);
@@ -894,7 +894,7 @@ static int nvme_parse_ana_log(struct nvme_ctrl *ctrl, void *data,
 			return -EINVAL;
 
 		offset += sizeof(*desc);
-		if (WARN_ON_ONCE(offset > ctrl->ana_log_size - nsid_buf_size))
+		if (WARN_ON_ONCE(ctrl->ana_log_size - offset < nsid_buf_size))
 			return -EINVAL;
 
 		error = cb(ctrl, desc, data);
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 4 lines · Martin George

The ANA log bounds checks subtract the required size from ana_log_size in unsigned arithmetic, which can wrap when a rogue or buggy target reports an oversized nnsids. Compare as "ana_log_size - offset < required" instead, so the subtraction can't underflow and bypass the check.

Looks good, applied to nvme-7.4.