Skip to contentSkip to conversation

cxl: Sashiko bug fixes

PATCH Superseded Richard Cheng 2 participants 10 messages Jul 8 CXL

v3 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v3 cover cxl: Sashiko bug fixes

Five independent, pre-existing bugs in the CXL core, reported by sashiko.

Patch 1: Get/Set Feature derive each mailbox command's offset from the starting offset plus the amount of data already transferred, then store it in a 16-bit field. A large offset/count supplied through fwctl can cause a later offset to exceed the representable feature extent and be truncated by cpu_to_le16(), targeting the wrong feature data. Reject invalid ranges up front.

Change cxl_get_feature() to return ssize_t so invalid input and mailbox failures are reported as negative errno instead of being conflated with a zero-byte result. Update all EDAC callers for the signed return contract while preserving the existing fwctl RPC response behavior.

Patch 2: cxl_get_poison_unmapped() aborted its whole partition sweep on the first fully-mapped partition, silently skipping unmapped poison in all later partitions. Skip that partition instead.

Patch 3: the same function tolerated the -EFAULT a RAM partition returns for Get Poison List but left it in rc, so a benign fault on the last scanned partition surfaced as a spurious read failure. Clear rc, as poison_by_decoder() already does.

Patch 4: the same function also ignored the ctx->offset handoff from poison_by_decoder() and derived its scan start from the highest DPA allocation, so the DPA of allocated-but-uncommitted decoders was never scanned by either phase. Resume the sweep at ctx->offset.

Patch 5: cxl_get_poison_by_memdev() overwrote rc on each partition query, so an earlier partition's failure was masked by a later success and unscanned poison was reported as a clean list. Stop on any error not tolerated as a RAM -EFAULT.

Changes since v2 [1]:
- Patch 1: change cxl_get_feature() to return ssize_t and propagate negative errno through all callers while preserving fwctl behavior (Dave)
- No code changes to patches 2-5

[1]:
https://lore.kernel.org/linux-cxl/[email protected]/

Richard Cheng (5):
cxl/features: Reject feature offset that overflows 16-bit field cxl/region: Scan all partitions for unmapped poison cxl/region: Don't leak tolerated RAM -EFAULT from unmapped poison scan cxl/region: Start unmapped poison scan at the committed decoder
boundary
cxl/memdev: Don't overwrite the error from an earlier partition poison
query

 drivers/cxl/core/core.h     |  2 +-
 drivers/cxl/core/edac.c     | 20 +++++++++++++++-----
 drivers/cxl/core/features.c | 22 +++++++++++++++-------
 drivers/cxl/core/memdev.c   |  2 ++
 drivers/cxl/core/region.c   | 13 ++++++-------
 5 files changed, 39 insertions(+), 20 deletions(-)

base-commit: dc59e4fea9d83f03bad6bddf3fa2e52491777482

Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 1/5 cxl/features: Reject feature offset that overflows 16-bit field

cxl_get_feature() and cxl_set_feature() build each mailbox command's offset from the starting offset plus the amount of data already transferred, then store it in a 16-bit field. An user-controlled fwctl offset and transfer size can exceed the feature extent, allowing a later offset to be truncated by cpu_to_le16() and target the wrong feature data.

Reject requests whose transfer size exceeeds the remaining 16-bit feature range. Express the check as "size > U16_MAX - offset" so the validation itself can't wrap on 32-bit systems.

Change cxl_get_feature() to return ssize_t so invalid input and mailbox failures are reported as negative errno rather than being conflated with a zero-byte result. Update the EDAC callers to handle negative results. Keep fwctl behvaior unchanged by translating helper failures to the same header-only RPC response carrying the CXL mailbox return code.

Fixes 5e5ac21f629d ("cxl/mbox: Add GET_FEATURE mailbox command")
Fixes 14d502cc2718 ("cxl/mbox: Add SET_FEATURE mailbox command")

---
Changelog:

v2->v3:
- Change cxl_get_feature() to return ssize_t and report negative errno.
- Update all EDAC callers for the signed return contract.
- Preserve the existing fwctl RPC response behavior on helper failure.

v1->v2:
- refactor the guard to "size > U16_MAX - offset", the addition is performed in size_t, so on 32-bit arch a large user-supplied size
wrpas the sum and bypasses the check. The substraction form can't misbehave since offset is a u16, making U16_MAX - offset always well-defined.

---
 drivers/cxl/core/core.h     |  2 +-
 drivers/cxl/core/edac.c     | 20 +++++++++++++++-----
 drivers/cxl/core/features.c | 22 +++++++++++++++-------
 3 files changed, 31 insertions(+), 13 deletions(-)
drivers/cxl/core/core.h +1 -1
index 07555ae63859..09dd1c1722dd 100644
@@ -218,7 +218,7 @@ int cxl_port_get_possible_dports(struct cxl_port *port);
 #ifdef CONFIG_CXL_FEATURES
 struct cxl_feat_entry *
 cxl_feature_info(struct cxl_features_state *cxlfs, const uuid_t *uuid);
-size_t cxl_get_feature(struct cxl_mailbox *cxl_mbox, const uuid_t *feat_uuid,
+ssize_t cxl_get_feature(struct cxl_mailbox *cxl_mbox, const uuid_t *feat_uuid,
 		       enum cxl_get_feat_selection selection,
 		       void *feat_out, size_t feat_out_size, u16 offset,
 		       u16 *return_code);
drivers/cxl/core/edac.c +15 -5
index b321971fef58..f1df4b5cfe5b 100644
@@ -78,7 +78,7 @@ static int cxl_mem_scrub_get_attrbs(struct cxl_mailbox *cxl_mbox, u8 *cap,
 				    u16 *cycle, u8 *flags, u8 *min_cycle)
 {
 	size_t rd_data_size = sizeof(struct cxl_scrub_rd_attrbs);
-	size_t data_size;
+	ssize_t data_size;
 	struct cxl_scrub_rd_attrbs *rd_attrbs __free(kfree) =
 		kzalloc(rd_data_size, GFP_KERNEL);
 	if (!rd_attrbs)
@@ -87,6 +87,8 @@ static int cxl_mem_scrub_get_attrbs(struct cxl_mailbox *cxl_mbox, u8 *cap,
 	data_size = cxl_get_feature(cxl_mbox, &CXL_FEAT_PATROL_SCRUB_UUID,
 				    CXL_GET_FEAT_SEL_CURRENT_VALUE, rd_attrbs,
 				    rd_data_size, 0, NULL);
+	if (data_size < 0)
+		return data_size;
 	if (!data_size)
 		return -EIO;
 
@@ -551,7 +553,7 @@ static int cxl_mem_ecs_get_attrbs(struct device *dev,
 	struct cxl_mailbox *cxl_mbox = &cxlmd->cxlds->cxl_mbox;
 	struct cxl_ecs_fru_rd_attrbs *fru_rd_attrbs;
 	size_t rd_data_size;
-	size_t data_size;
+	ssize_t data_size;
 
 	rd_data_size = cxl_ecs_ctx->get_feat_size;
 
@@ -563,6 +565,8 @@ static int cxl_mem_ecs_get_attrbs(struct device *dev,
 	data_size = cxl_get_feature(cxl_mbox, &CXL_FEAT_ECS_UUID,
 				    CXL_GET_FEAT_SEL_CURRENT_VALUE, rd_attrbs,
 				    rd_data_size, 0, NULL);
+	if (data_size < 0)
+		return data_size;
 	if (!data_size)
 		return -EIO;
 
@@ -583,7 +587,7 @@ static int cxl_mem_ecs_set_attrbs(struct device *dev,
 	struct cxl_ecs_fru_wr_attrbs *fru_wr_attrbs;
 	size_t rd_data_size, wr_data_size;
 	u16 num_media_frus, count;
-	size_t data_size;
+	ssize_t data_size;
 
 	num_media_frus = cxl_ecs_ctx->num_media_frus;
 	rd_data_size = cxl_ecs_ctx->get_feat_size;
@@ -596,6 +600,8 @@ static int cxl_mem_ecs_set_attrbs(struct device *dev,
 	data_size = cxl_get_feature(cxl_mbox, &CXL_FEAT_ECS_UUID,
 				    CXL_GET_FEAT_SEL_CURRENT_VALUE, rd_attrbs,
 				    rd_data_size, 0, NULL);
+	if (data_size < 0)
+		return data_size;
 	if (!data_size)
 		return -EIO;
 
@@ -1264,7 +1270,7 @@ cxl_mem_sparing_get_attrbs(struct cxl_mem_sparing_context *cxl_sparing_ctx)
 	struct cxl_memdev *cxlmd = cxl_sparing_ctx->cxlmd;
 	struct cxl_mailbox *cxl_mbox = &cxlmd->cxlds->cxl_mbox;
 	u16 restriction_flags;
-	size_t data_size;
+	ssize_t data_size;
 	u16 return_code;
 	struct cxl_memdev_sparing_rd_attrbs *rd_attrbs __free(kfree) =
 		kzalloc(rd_data_size, GFP_KERNEL);
@@ -1274,6 +1280,8 @@ cxl_mem_sparing_get_attrbs(struct cxl_mem_sparing_context *cxl_sparing_ctx)
 	data_size = cxl_get_feature(cxl_mbox, &cxl_sparing_ctx->repair_uuid,
 				    CXL_GET_FEAT_SEL_CURRENT_VALUE, rd_attrbs,
 				    rd_data_size, 0, &return_code);
+	if (data_size < 0)
+		return data_size;
 	if (!data_size)
 		return -EIO;
 
@@ -1750,7 +1758,7 @@ static int cxl_mem_ppr_get_attrbs(struct cxl_ppr_context *cxl_ppr_ctx)
 	struct cxl_memdev *cxlmd = cxl_ppr_ctx->cxlmd;
 	struct cxl_mailbox *cxl_mbox = &cxlmd->cxlds->cxl_mbox;
 	u16 restriction_flags;
-	size_t data_size;
+	ssize_t data_size;
 	u16 return_code;
 
 	struct cxl_memdev_ppr_rd_attrbs *rd_attrbs __free(kfree) =
@@ -1761,6 +1769,8 @@ static int cxl_mem_ppr_get_attrbs(struct cxl_ppr_context *cxl_ppr_ctx)
 	data_size = cxl_get_feature(cxl_mbox, &cxl_ppr_ctx->repair_uuid,
 				    CXL_GET_FEAT_SEL_CURRENT_VALUE, rd_attrbs,
 				    rd_data_size, 0, &return_code);
+	if (data_size < 0)
+		return data_size;
 	if (!data_size)
 		return -EIO;
 
drivers/cxl/core/features.c +15 -7
index 85185af46b72..aaa408445fd4 100644
@@ -220,7 +220,7 @@ int devm_cxl_setup_features(struct cxl_dev_state *cxlds)
 }
 EXPORT_SYMBOL_NS_GPL(devm_cxl_setup_features, "CXL");
 
-size_t cxl_get_feature(struct cxl_mailbox *cxl_mbox, const uuid_t *feat_uuid,
+ssize_t cxl_get_feature(struct cxl_mailbox *cxl_mbox, const uuid_t *feat_uuid,
 		       enum cxl_get_feat_selection selection,
 		       void *feat_out, size_t feat_out_size, u16 offset,
 		       u16 *return_code)
@@ -235,7 +235,10 @@ size_t cxl_get_feature(struct cxl_mailbox *cxl_mbox, const uuid_t *feat_uuid,
 		*return_code = CXL_MBOX_CMD_RC_INPUT;
 
 	if (!feat_out || !feat_out_size)
-		return 0;
+		return -EINVAL;
+
+	if (feat_out_size > U16_MAX - offset)
+		return -EINVAL;
 
 	size_out = min(feat_out_size, cxl_mbox->payload_size);
 	uuid_copy(&pi.uuid, feat_uuid);
@@ -258,7 +261,7 @@ size_t cxl_get_feature(struct cxl_mailbox *cxl_mbox, const uuid_t *feat_uuid,
 		if (rc < 0 || !mbox_cmd.size_out) {
 			if (return_code)
 				*return_code = mbox_cmd.return_code;
-			return 0;
+			return rc < 0 ? rc : -EIO;
 		}
 		data_rcvd_size += mbox_cmd.size_out;
 	} while (data_rcvd_size < feat_out_size);
@@ -287,6 +290,9 @@ int cxl_set_feature(struct cxl_mailbox *cxl_mbox,
 	if (return_code)
 		*return_code = CXL_MBOX_CMD_RC_INPUT;
 
+	if (feat_data_size > U16_MAX - offset)
+		return -EINVAL;
+
 	struct cxl_mbox_set_feat_in *pi __free(kfree) =
 			kzalloc(cxl_mbox->payload_size, GFP_KERNEL);
 	if (!pi)
@@ -460,6 +466,7 @@ static void *cxlctl_get_feature(struct cxl_features_state *cxlfs,
 	const struct cxl_mbox_get_feat_in *feat_in;
 	u16 offset, count, return_code;
 	size_t out_size = *out_len;
+	ssize_t data_size;
 
 	if (rpc_in->op_size != sizeof(*feat_in))
 		return ERR_PTR(-EINVAL);
@@ -476,16 +483,17 @@ static void *cxlctl_get_feature(struct cxl_features_state *cxlfs,
 	if (!rpc_out)
 		return ERR_PTR(-ENOMEM);
 
-	out_size = cxl_get_feature(cxl_mbox, &feat_in->uuid,
-				   feat_in->selection, rpc_out->payload,
-				   count, offset, &return_code);
+	data_size = cxl_get_feature(cxl_mbox, &feat_in->uuid,
+				    feat_in->selection, rpc_out->payload,
+				    count, offset, &return_code);
 	*out_len = sizeof(struct fwctl_rpc_cxl_out);
-	if (!out_size) {
+	if (data_size <= 0) {
 		rpc_out->size = 0;
 		rpc_out->retval = return_code;
 		return no_free_ptr(rpc_out);
 	}
 
+	out_size = data_size;
 	rpc_out->size = out_size;
 	rpc_out->retval = CXL_MBOX_CMD_RC_SUCCESS;
 	*out_len += out_size;
Signature · 1 line
2.43.0
#4 lore raw

PATCH v3 3/5 cxl/region: Don't leak tolerated RAM -EFAULT from unmapped poison scan

In reply to Richard Cheng

cxl_get_poison_unmapped() tolerates the -EFAULT a RAM partition returns for Get Poison List by skipping that partition, but left rc holding the error. If the tolerated RAM fault was the last poison query before the loop ended, the function returned a spurious -EFAULT and the poison-list read failed even though enumeration succeeded. Reset rc to 0 when tolerating the fault, matching poison_by_decoder().

Fixes be5cbd0840275 ("cxl: Kill enum cxl_decoder_mode")

---
Changelog:

v2->v3:
- No code changes.

v1->v2:
- No code changes.

---
 drivers/cxl/core/region.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
drivers/cxl/core/region.c +3 -1
index be246fb09c99..52ba8e9e4288 100644
@@ -2933,8 +2933,10 @@ static int cxl_get_poison_unmapped(struct cxl_memdev *cxlmd,
 		if (!length)
 			continue;
 		rc = cxl_mem_get_poison(cxlmd, offset, length, NULL);
-		if (rc == -EFAULT && cxlds->part[i].mode == CXL_PARTMODE_RAM)
+		if (rc == -EFAULT && cxlds->part[i].mode == CXL_PARTMODE_RAM) {
+			rc = 0;
 			continue;
+		}
 		if (rc)
 			break;
 	}
Signature · 1 line
2.43.0
#3 lore raw

PATCH v3 2/5 cxl/region: Scan all partitions for unmapped poison

In reply to Richard Cheng

cxl_get_poison_unmapped() sweeps the unmapped tail of each partition from ctx->part onward. A fully-mapped partition has no unmapped tail, it's a normal per-partition state, but the loop treated it with break, aborting the whole sweep and silently skipping unmapped poison in all later partition. Use continue so a fully-mapped partition is skipped and later partitions are still scanned.

Fixes be5cbd0840275 ("cxl: Kill enum cxl_decoder_mode")

---
Changelog:

v2->v3:
- No code changes.

v1->v2:
- Tweak commit message

---
 drivers/cxl/core/region.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/cxl/core/region.c +1 -1
index 1e211542b6b6..be246fb09c99 100644
@@ -2931,7 +2931,7 @@ static int cxl_get_poison_unmapped(struct cxl_memdev *cxlmd,
 			offset = res->start;
 		length = res->end - offset + 1;
 		if (!length)
-			break;
+			continue;
 		rc = cxl_mem_get_poison(cxlmd, offset, length, NULL);
 		if (rc == -EFAULT && cxlds->part[i].mode == CXL_PARTMODE_RAM)
 			continue;
Signature · 1 line
2.43.0
#6 lore raw

PATCH v3 4/5 cxl/region: Start unmapped poison scan at the committed decoder boundary

In reply to Richard Cheng

poison_by_decoder() stops at the last committed decoder and records the handoff in ctx->offset, but cxl_get_poison_unmapped() ignores it and starts after the highest DPA allocation instead. Allocation exist for uncommitted decoders too, so their DPA is skipped by both phases and poison there is never reported. Resume the scan at ctx->offset, and scan later partitions in full, restoring the pre-rewrite behavior.

Fixes be5cbd084027 ("cxl: Kill enum cxl_decoder_mode")

---
Changelog:

v2->v3:
- No code changes.

v1->v2:
- New added patch ( sashiko's report )

---
 drivers/cxl/core/region.c | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)
drivers/cxl/core/region.c +2 -5
index 52ba8e9e4288..ba77416055f4 100644
@@ -2910,7 +2910,6 @@ static int cxl_get_poison_unmapped(struct cxl_memdev *cxlmd,
 {
 	struct cxl_dev_state *cxlds = cxlmd->cxlds;
 	const struct resource *res;
-	struct resource *p, *last;
 	u64 offset, length;
 	int rc = 0;
 
@@ -2923,10 +2922,8 @@ static int cxl_get_poison_unmapped(struct cxl_memdev *cxlmd,
 	 */
 	for (int i = ctx->part; i < cxlds->nr_partitions; i++) {
 		res = &cxlds->part[i].res;
-		for (p = res->child, last = NULL; p; p = p->sibling)
-			last = p;
-		if (last)
-			offset = last->end + 1;
+		if (i == ctx->part)
+			offset = ctx->offset;
 		else
 			offset = res->start;
 		length = res->end - offset + 1;
Signature · 1 line
2.43.0
#8 lore raw

PATCH v3 5/5 cxl/memdev: Don't overwrite the error from an earlier partition poison query

In reply to Richard Cheng

cxl_get_poison_by_memdev() queries Get Poison List per partition but never checks the result inside the loop, so a later partition's success overwrites an earlier partition's failure and the whole scan reports success while that partition's poison went unlisted. Before the loop conversion the PMEM query returned early on error. Stop the loop on any error not already tolerated as a RAM -EFAULT.

Fixes be5cbd084027 ("cxl: Kill enum cxl_decoder_mode")

---
Changelog:

v2->v3:
- No code changes.

v1->v2:
- New added patch ( sashiko-bot's report )

---
 drivers/cxl/core/memdev.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/cxl/core/memdev.c +2 -0
index 33a3d2e7b13a..8718964b9c5e 100644
@@ -231,6 +231,8 @@ static int cxl_get_poison_by_memdev(struct cxl_memdev *cxlmd)
 		 */
 		if (rc == -EFAULT && cxlds->part[i].mode == CXL_PARTMODE_RAM)
 			rc = 0;
+		if (rc)
+			break;
 	}
 	return rc;
 }
Signature · 1 line
2.43.0
#5 lore raw

Re: [PATCH v3 3/5] cxl/region: Don't leak tolerated RAM -EFAULT from unmapped poison scan

In reply to Richard Cheng
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:

Pre-existing issues:
- [High] Unbounded infinite loop in cxl_mem_get_poison() if the device returns 0 records but leaves FLAG_MORE set.
- [Medium] cxl_get_poison_by_memdev() does not abort on failure, silently overwriting earlier errors if subsequent partitions succeed. --

--- Patch [3]: [PATCH v3 3/5] cxl/region: Don't leak tolerated RAM -EFAULT from unmapped poison scan --- commit 9b48fd52ca70c063c7f4e5fc8516877eba1fee39
Author: Richard Cheng <[email protected]>

cxl/region: Don't leak tolerated RAM -EFAULT from unmapped poison scan

cxl_get_poison_unmapped() tolerates the -EFAULT a RAM partition returns for Get Poison List by skipping that partition, but left rc holding the error. Reset rc to 0 when tolerating the fault, matching poison_by_decoder().

Quoted 6 lines
drivers/cxl/core/region.c +0 -0
index be246fb09c999..52ba8e9e42885 100644
@@ -2933,8 +2933,10 @@ static int cxl_get_poison_unmapped(struct cxl_memdev *cxlmd,
 		if (!length)

drivers/cxl/core/region.c static int cxl_get_poison_unmapped(struct cxl_memdev *cxlmd, · line 2942

Quoted 10 lines
drivers/cxl/core/region.c +3 -1
 			continue;
 		rc = cxl_mem_get_poison(cxlmd, offset, length, NULL);
-		if (rc == -EFAULT && cxlds->part[i].mode == CXL_PARTMODE_RAM)
+		if (rc == -EFAULT && cxlds->part[i].mode == CXL_PARTMODE_RAM) {
+			rc = 0;
 			continue;
+		}
 		if (rc)
 			break;
 	}

While reviewing this change, I noticed a couple of related pre-existing issues in other poison handling functions:

drivers/cxl/core/memdev.c:cxl_get_poison_by_memdev()

Quoted 9 lines
		rc = cxl_mem_get_poison(cxlmd, offset, length, NULL);
		/*
		 * Invalid Physical Address is not an error for
		 * volatile addresses. Device support is optional.
		 */
		if (rc == -EFAULT && cxlds->part[i].mode == CXL_PARTMODE_RAM)
			rc = 0;
	}
	return rc;

[Severity: Medium]
This is a pre-existing issue, but does cxl_get_poison_by_memdev() overwrite previous error codes?

If an early partition fails (for example, returning -EIO) but a subsequent partition succeeds, the loop lacks an if (rc) break; check. The rc variable is overwritten with 0, masking the earlier failure from userspace.

drivers/cxl/core/mbox.c:cxl_mem_get_poison()

Quoted 7 lines
		nr_records = nr_records + le16_to_cpu(po->count);
		if (nr_records >= mds->poison.max_errors) {
			dev_dbg(&cxlmd->dev, "Max Error Records reached: %d\n",
				nr_records);
			break;
		}
	} while (po->flags & CXL_POISON_FLAG_MORE);

[Severity: High]
This is a pre-existing issue, but can this loop in cxl_mem_get_poison() spin infinitely?

If a hardware device returns a payload with po->count == 0 and the CXL_POISON_FLAG_MORE flag set, nr_records won't increment. The safety bound nr_records >= mds->poison.max_errors is never reached, which could cause a kernel hang while holding mds->poison.mutex.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
#9 lore raw

Re: [PATCH v3 5/5] cxl/memdev: Don't overwrite the error from an earlier partition poison query

In reply to Richard Cheng
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Failure to advance the offset in Get Poison List loop causes redundant mailbox queries and missing poison records. --

--- Patch [5]: [PATCH v3 5/5] cxl/memdev: Don't overwrite the error from an earlier partition poison query --- commit d12b3a1ef61c4d80195127d0b11aa93c505ec62e
Author: Richard Cheng <[email protected]>

cxl/memdev: Don't overwrite the error from an earlier partition poison query

This commit fixes an issue in cxl_get_poison_by_memdev() where errors from querying poison lists for earlier partitions were overwritten by subsequent partition successes. It adds a check to break the loop early on any error that is not explicitly tolerated as a RAM -EFAULT.

drivers/cxl/core/memdev.c static int cxl_get_poison_by_memdev(struct cxl_memdev *cxlmd) · line 235

Quoted 10 lines
drivers/cxl/core/memdev.c +2 -0
index 33a3d2e7b13af..8718964b9c5e8 100644
@@ -231,6 +231,8 @@ static int cxl_get_poison_by_memdev(struct cxl_memdev *cxlmd)
 		 */
 		if (rc == -EFAULT && cxlds->part[i].mode == CXL_PARTMODE_RAM)
 			rc = 0;
+		if (rc)
+			break;

[Severity: High]
This isn't a bug introduced by this patch, but I notice an issue with the Get Poison List loop inside cxl_mem_get_poison(), which is called just above this block.

When the device returns CXL_POISON_FLAG_MORE, the do-while loop in drivers/cxl/core/mbox.c:cxl_mem_get_poison() does not advance pi.offset:

	pi.offset = cpu_to_le64(offset);
	pi.length = cpu_to_le64(len / CXL_POISON_LEN_MULT);

	do {
		struct cxl_mbox_cmd mbox_cmd = (struct cxl_mbox_cmd){
			.opcode = CXL_MBOX_OP_GET_POISON,
			.size_in = sizeof(pi),
			.payload_in = &pi,
			[ ... ]
		};

		rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd);
		if (rc)
			break;

		[ ... ]

	} while (po->flags & CXL_POISON_FLAG_MORE);

Does this cause redundant mailbox queries using the exact same offset?

If the device has more poison records than can fit in a single response, would this result in fetching the same records repeatedly and missing the subsequent ones?

Quoted 3 lines
drivers/cxl/core/memdev.c +0 -0
 	}
 	return rc;
 }
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
#7 lore raw

Re: [PATCH v3 4/5] cxl/region: Start unmapped poison scan at the committed decoder boundary

In reply to Richard Cheng
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider:

Pre-existing issues:
- [High] Pre-existing Out-of-Bounds array read in poison_by_decoder() when a decoder fails to map to a partition. --

--- Patch [4]: [PATCH v3 4/5] cxl/region: Start unmapped poison scan at the committed decoder boundary --- commit 3fc5ee5ec0b9d8fae7576a61bcdfd58c1e6ca1c5
Author: Richard Cheng <[email protected]>

cxl/region: Start unmapped poison scan at the committed decoder boundary

poison_by_decoder() stops at the last committed decoder and records the handoff in ctx->offset, but cxl_get_poison_unmapped() ignores it and starts after the highest DPA allocation instead. Allocation exist for uncommitted decoders too, so their DPA is skipped by both phases and poison there is never reported. Resume the scan at ctx->offset, and scan later partitions in full, restoring the pre-rewrite behavior.

Quoted 17 lines
drivers/cxl/core/region.c +2 -4
index 52ba8e9e42885..ba77416055f4a 100644
@@ -2923,10 +2922,8 @@ static int cxl_get_poison_unmapped(struct cxl_memdev *cxlmd,
 	 */
 	for (int i = ctx->part; i < cxlds->nr_partitions; i++) {
 		res = &cxlds->part[i].res;
-		for (p = res->child, last = NULL; p; p = p->sibling)
-			last = p;
-		if (last)
-			offset = last->end + 1;
+		if (i == ctx->part)
+			offset = ctx->offset;
 		else
 			offset = res->start;
 		length = res->end - offset + 1;

[ ... ]

Quoted 10 lines

static int poison_by_decoder(struct device *dev, void *arg) {

drivers/cxl/core/region.c +0 -0
 	struct cxl_poison_context *ctx = arg;
 	struct cxl_endpoint_decoder *cxled;
 	enum cxl_partition_mode mode;
 	struct cxl_dev_state *cxlds;
 	struct cxl_memdev *cxlmd;
 	u64 offset, length;
 	int rc = 0;
 

drivers/cxl/core/region.c

Quoted 10 lines
drivers/cxl/core/region.c +0 -0
 	if (!is_endpoint_decoder(dev))
 		return rc;
 
 	cxled = to_cxl_endpoint_decoder(dev);
 	if (!cxled->dpa_res)
 		return rc;
 
 	cxlmd = cxled_to_memdev(cxled);
 	cxlds = cxlmd->cxlds;
 	mode = cxlds->part[cxled->part].mode;

[Severity: High]
This is a pre-existing issue, but is it possible for cxled->part to be -1 here, causing an out-of-bounds read?

If a CXL decoder's DPA allocation is malformed or spans across partition boundaries during initial enumeration, __cxl_dpa_reserve() can leave cxled->part as -1 while still setting cxled->dpa_res.

Since this function only checks if cxled->dpa_res is non-NULL before the array access, could a privileged user initiating a poison scan trigger a read before the cxlds->part array? Other areas in the subsystem appear to check if cxled->part < 0 prior to access.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
#2 lore raw

Re: [PATCH v3 1/5] cxl/features: Reject feature offset that overflows 16-bit field

In reply to Richard Cheng
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

New issues:
- [Medium] The newly introduced feature transfer size validation incorrectly rejects valid reads/writes, causing an off-by-one bounds check error.

Pre-existing issues:
- [High] Missing bounds check on user-provided out_len and count leads to arbitrary heap buffer overflow and potential ZERO_SIZE_PTR dereference in cxlctl_get_feature and cxlctl_set_feature.
- [High] Missing bounds check on user-supplied in_len and op_size leads to out-of-bounds heap reads and potential kernel crashes in cxlctl_set_feature.
- [High] Hardware-induced heap buffer overflow in cxl_get_feature() loop due to incorrect static size_out tracking.
- [Medium] Internal mailbox command failures mask their error codes and return SUCCESS to the fwctl user.
--

--- Patch [1]: [PATCH v3 1/5] cxl/features: Reject feature offset that overflows 16-bit field --- commit c16baccf7c99ea7335aa40b18d19b96e88ef46aa
Author: Richard Cheng <[email protected]>

cxl/features: Reject feature offset that overflows 16-bit field

cxl_get_feature() and cxl_set_feature() build each mailbox command's offset from the starting offset plus the amount of data already transferred, then store it in a 16-bit field. An user-controlled fwctl offset and transfer size can exceed the feature extent, allowing a later offset to be truncated by cpu_to_le16() and target the wrong feature data.

drivers/cxl/core/features.c size_t cxl_get_feature(struct cxl_mailbox *cxl_mbox, const uuid_t *feat_uuid, · line 241

Quoted 13 lines
drivers/cxl/core/features.c +4 -1
index 85185af46b72d..aaa408445fd40 100644
@@ -235,7 +235,10 @@ size_t cxl_get_feature(struct cxl_mailbox *cxl_mbox, const uuid_t *feat_uuid,
 		*return_code = CXL_MBOX_CMD_RC_INPUT;
 
 	if (!feat_out || !feat_out_size)
-		return 0;
+		return -EINVAL;
+
+	if (feat_out_size > U16_MAX - offset)
+		return -EINVAL;

[Severity: Medium]
Does this logic contain an off-by-one error?

If a user requests exactly 65536 bytes at offset 0, feat_out_size would be 65536. Since U16_MAX is 65535, 65536 > 65535 - 0 evaluates to true, which incorrectly rejects a valid read.

[ ... ]

drivers/cxl/core/features.c size_t cxl_get_feature(struct cxl_mailbox *cxl_mbox, const uuid_t *feat_uuid, · line 263

Quoted 4 lines
drivers/cxl/core/features.c +0 -0
@@ -258,7 +261,7 @@ size_t cxl_get_feature(struct cxl_mailbox *cxl_mbox, const uuid_t *feat_uuid,
 		if (rc < 0 || !mbox_cmd.size_out) {
 			if (return_code)
 				*return_code = mbox_cmd.return_code;

[Severity: High]
This isn't a bug introduced by this patch, but could this lead to a hardware-induced heap buffer overflow?

Looking at the loop just before this check:

    mbox_cmd = (struct cxl_mbox_cmd) {
        ...
        .size_out = size_out,
        .payload_out = feat_out + data_rcvd_size,
        .min_out = data_to_rd_size,
    };
    rc = cxl_internal_send_cmd(cxl_mbox, &mbox_cmd);

The command struct tells the mailbox transport that it has size_out bytes of space remaining in payload_out, even though the actual remaining space requested for this iteration is data_to_rd_size. If a buggy or malicious hardware device ignores min_out and returns up to size_out bytes, could the transport blindly overwrite memory past the end of feat_out?

[Severity: Medium]
This is also a pre-existing issue, but does this mask internal mailbox command failures?

When cxl_internal_send_cmd() fails before getting a response (e.g., due to a timeout or busy state), mbox_cmd.return_code retains its 0-initialized value. Does setting *return_code to 0 end up propagating a SUCCESS response to the fwctl user, even though the command failed?

Quoted 5 lines
drivers/cxl/core/features.c +1 -1
-			return 0;
+			return rc < 0 ? rc : -EIO;
 		}
 		data_rcvd_size += mbox_cmd.size_out;
 	} while (data_rcvd_size < feat_out_size);

[ ... ]

drivers/cxl/core/features.c int cxl_set_feature(struct cxl_mailbox *cxl_mbox, · line 294

Quoted 6 lines
drivers/cxl/core/features.c +2 -0
@@ -287,6 +290,9 @@ int cxl_set_feature(struct cxl_mailbox *cxl_mbox,
 	if (return_code)
 		*return_code = CXL_MBOX_CMD_RC_INPUT;
 
+	if (feat_data_size > U16_MAX - offset)
+		return -EINVAL;

[Severity: Medium]
Does this have the same off-by-one bounds check error as cxl_get_feature() when writing a full 64KB extent?

[ ... ]

drivers/cxl/core/features.c static void *cxlctl_get_feature(struct cxl_features_state *cxlfs, · line 481

Quoted 7 lines
drivers/cxl/core/features.c +0 -3
@@ -476,16 +483,17 @@ static void *cxlctl_get_feature(struct cxl_features_state *cxlfs,
 	if (!rpc_out)
 		return ERR_PTR(-ENOMEM);
 
-	out_size = cxl_get_feature(cxl_mbox, &feat_in->uuid,
-				   feat_in->selection, rpc_out->payload,
-				   count, offset, &return_code);

[Severity: High]
This isn't a bug introduced by this patch, but does this lack a bounds check against count?

The rpc_out buffer is allocated just above using the user-provided out_size:

struct fwctl_rpc_cxl_out *rpc_out __free(kvfree) =
kvzalloc(out_size, GFP_KERNEL);

However, cxl_get_feature() will write up to count bytes into rpc_out->payload. If a user calls the FWCTL_RPC ioctl with a small out_len but a large count, could this lead to an arbitrary heap buffer overflow?

Additionally, if out_size is 0, kvzalloc() returns ZERO_SIZE_PTR. Would the later assignment of rpc_out->size = 0 or data_size trigger a kernel panic?

Similar missing bounds checks on user-supplied in_len and op_size appear to exist in cxlctl_set_feature() and cxlctl_fw_rpc(), which could lead to out-of-bounds heap reads or ZERO_SIZE_PTR dereferences.

Quoted 10 lines
drivers/cxl/core/features.c +4 -1
+	data_size = cxl_get_feature(cxl_mbox, &feat_in->uuid,
+				    feat_in->selection, rpc_out->payload,
+				    count, offset, &return_code);
 	*out_len = sizeof(struct fwctl_rpc_cxl_out);
-	if (!out_size) {
+	if (data_size <= 0) {
 		rpc_out->size = 0;
 		rpc_out->retval = return_code;
 		return no_free_ptr(rpc_out);
 	}
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1