Skip to contentSkip to conversation

cxl/edac: Fail ECS threshold read for reserved encodings

PATCH Applied Yili Zhang 2 participants 2 messages Nov 18 CXL

lore
Share & tools
lore

Yili Zhang

author
#0 lore raw

PATCH v5 1/1 cxl/edac: Fail ECS threshold read for reserved encodings

cxl_get_ecs_threshold() extracts a 3-bit index (0-7) from the device-supplied Error Check Scrub (ECS) config word and uses it to index ecs_supp_threshold[], which only has 6 elements. A device reporting index 6 or 7 causes a read beyond the end of the array, and the driver reports the resulting value to userspace through the EDAC 'threshold' sysfs attribute.

The threshold count field also has reserved encodings 0-2, which currently read as 0 from the sparse lookup table. That behavior is not intentional; it is just an artifact of the array's implicit zero-fill. The ABI documentation only lists 256, 1024 and 4096 as supported values, and the write side (cxl_set_ecs_threshold()) rejects everything else, so a reserved encoding can only come from the device reporting it. Rather than documenting 0 as well, make the read fail for all reserved encodings and only ever report the documented values.

None of these encodings can be written through the sysfs write path, and no device is known to report them, so this is hardening against a device returning values not covered by the spec rather than a fix for known hardware.

Convert the cxl_get_ecs_*() helpers to return an int and write the attribute value through an output parameter (u32 * to match the macro), so that errors propagate through the shared CXL_ECS_GET_ATTR() macro to the sysfs read.

---
v5: Drop "adjacent .rodata" and "small info leak" from the commit message, since what sits beyond the array depends on compiler and linker layout (Alison Schofield), and add her Reviewed-by. The diff is unchanged, so Ben's Reviewed-by carries over from v4.

v4: Spell out Error Check Scrub (ECS) on first use (Ben Cheatham) and drop the Suggested-by tag (Alison Schofield). The diff is unchanged, so the Reviewed-by is carried over from v3.

v3: Per review feedback, drop the Fixes tag and reword the description to make clear this is hardening against a device reporting values not covered by the spec rather than a fix (Jonathan Cameron).

v2: Per review feedback, fail the read with -EINVAL for all reserved encodings instead of returning 0, converting the cxl_get_ecs_*() getters to an int return with an output parameter so the error reaches the sysfs read (Alison Schofield).

v1: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v4: https://lore.kernel.org/all/[email protected]/

 drivers/cxl/core/edac.c | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)
drivers/cxl/core/edac.c +17 -9
index b321971fef58..fd6612dde7f1 100644
@@ -624,21 +624,31 @@ static int cxl_mem_ecs_set_attrbs(struct device *dev,
 			       0, NULL);
 }
 
-static u8 cxl_get_ecs_log_entry_type(u8 log_cap, u16 config)
+static int cxl_get_ecs_log_entry_type(u8 log_cap, u16 config, u32 *val)
 {
-	return FIELD_GET(CXL_ECS_LOG_ENTRY_TYPE_MASK, log_cap);
+	*val = FIELD_GET(CXL_ECS_LOG_ENTRY_TYPE_MASK, log_cap);
+
+	return 0;
 }
 
-static u16 cxl_get_ecs_threshold(u8 log_cap, u16 config)
+static int cxl_get_ecs_threshold(u8 log_cap, u16 config, u32 *val)
 {
 	u8 index = FIELD_GET(CXL_ECS_THRESHOLD_COUNT_MASK, config);
 
-	return ecs_supp_threshold[index];
+	if (index >= ARRAY_SIZE(ecs_supp_threshold) ||
+	    !ecs_supp_threshold[index])
+		return -EINVAL;
+
+	*val = ecs_supp_threshold[index];
+
+	return 0;
 }
 
-static u8 cxl_get_ecs_count_mode(u8 log_cap, u16 config)
+static int cxl_get_ecs_count_mode(u8 log_cap, u16 config, u32 *val)
 {
-	return FIELD_GET(CXL_ECS_COUNT_MODE_MASK, config);
+	*val = FIELD_GET(CXL_ECS_COUNT_MODE_MASK, config);
+
+	return 0;
 }
 
 #define CXL_ECS_GET_ATTR(attrb)						    \
@@ -655,9 +665,7 @@ static u8 cxl_get_ecs_count_mode(u8 log_cap, u16 config)
 		if (ret)						    \
 			return ret;					    \
 									    \
-		*val = cxl_get_ecs_##attrb(log_cap, config);		    \
-									    \
-		return 0;						    \
+		return cxl_get_ecs_##attrb(log_cap, config, val);	    \
 	}
 
 CXL_ECS_GET_ATTR(log_entry_type)
Signature · 1 line
2.27.0
maintainer Applied
#1 lore raw
Quoted 22 lines · Yili Zhang

cxl_get_ecs_threshold() extracts a 3-bit index (0-7) from the device-supplied Error Check Scrub (ECS) config word and uses it to index ecs_supp_threshold[], which only has 6 elements. A device reporting index 6 or 7 causes a read beyond the end of the array, and the driver reports the resulting value to userspace through the EDAC 'threshold' sysfs attribute.

The threshold count field also has reserved encodings 0-2, which currently read as 0 from the sparse lookup table. That behavior is not intentional; it is just an artifact of the array's implicit zero-fill. The ABI documentation only lists 256, 1024 and 4096 as supported values, and the write side (cxl_set_ecs_threshold()) rejects everything else, so a reserved encoding can only come from the device reporting it. Rather than documenting 0 as well, make the read fail for all reserved encodings and only ever report the documented values.

None of these encodings can be written through the sysfs write path, and no device is known to report them, so this is hardening against a device returning values not covered by the spec rather than a fix for known hardware.

Quoted 8 lines

Convert the cxl_get_ecs_*() helpers to return an int and write the attribute value through an output parameter (u32 * to match the macro), so that errors propagate through the shared CXL_ECS_GET_ATTR() macro to the sysfs read.

Applied to cxl/next:
a15688352fe7

Quoted 81 lines

---
v5: Drop "adjacent .rodata" and "small info leak" from the commit message, since what sits beyond the array depends on compiler and linker layout (Alison Schofield), and add her Reviewed-by. The diff is unchanged, so Ben's Reviewed-by carries over from v4.

v4: Spell out Error Check Scrub (ECS) on first use (Ben Cheatham) and drop the Suggested-by tag (Alison Schofield). The diff is unchanged, so the Reviewed-by is carried over from v3.

v3: Per review feedback, drop the Fixes tag and reword the description to make clear this is hardening against a device reporting values not covered by the spec rather than a fix (Jonathan Cameron).

v2: Per review feedback, fail the read with -EINVAL for all reserved encodings instead of returning 0, converting the cxl_get_ecs_*() getters to an int return with an output parameter so the error reaches the sysfs read (Alison Schofield).

v1: https://lore.kernel.org/all/[email protected]/
v2: https://lore.kernel.org/all/[email protected]/
v3: https://lore.kernel.org/all/[email protected]/
v4: https://lore.kernel.org/all/[email protected]/

 drivers/cxl/core/edac.c | 26 +++++++++++++++++---------
 1 file changed, 17 insertions(+), 9 deletions(-)
drivers/cxl/core/edac.c +17 -9
index b321971fef58..fd6612dde7f1 100644
@@ -624,21 +624,31 @@ static int cxl_mem_ecs_set_attrbs(struct device *dev,
 			       0, NULL);
 }
 
-static u8 cxl_get_ecs_log_entry_type(u8 log_cap, u16 config)
+static int cxl_get_ecs_log_entry_type(u8 log_cap, u16 config, u32 *val)
 {
-	return FIELD_GET(CXL_ECS_LOG_ENTRY_TYPE_MASK, log_cap);
+	*val = FIELD_GET(CXL_ECS_LOG_ENTRY_TYPE_MASK, log_cap);
+
+	return 0;
 }
 
-static u16 cxl_get_ecs_threshold(u8 log_cap, u16 config)
+static int cxl_get_ecs_threshold(u8 log_cap, u16 config, u32 *val)
 {
 	u8 index = FIELD_GET(CXL_ECS_THRESHOLD_COUNT_MASK, config);
 
-	return ecs_supp_threshold[index];
+	if (index >= ARRAY_SIZE(ecs_supp_threshold) ||
+	    !ecs_supp_threshold[index])
+		return -EINVAL;
+
+	*val = ecs_supp_threshold[index];
+
+	return 0;
 }
 
-static u8 cxl_get_ecs_count_mode(u8 log_cap, u16 config)
+static int cxl_get_ecs_count_mode(u8 log_cap, u16 config, u32 *val)
 {
-	return FIELD_GET(CXL_ECS_COUNT_MODE_MASK, config);
+	*val = FIELD_GET(CXL_ECS_COUNT_MODE_MASK, config);
+
+	return 0;
 }
 
 #define CXL_ECS_GET_ATTR(attrb)						    \
@@ -655,9 +665,7 @@ static u8 cxl_get_ecs_count_mode(u8 log_cap, u16 config)
 		if (ret)						    \
 			return ret;					    \
 									    \
-		*val = cxl_get_ecs_##attrb(log_cap, config);		    \
-									    \
-		return 0;						    \
+		return cxl_get_ecs_##attrb(log_cap, config, val);	    \
 	}
 
 CXL_ECS_GET_ATTR(log_entry_type)