Skip to contentSkip to conversation

iio: adc: pac1934: validate ACPI _DSM return type and bounds

PATCH Superseded Tristan Madani 3 participants 4 messages Oct 30 – Oct 31

Subsystems (2)

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v5 1/1 iio: adc: pac1934: validate ACPI _DSM return type and bounds

From: Tristan Madani <[email protected]>

pac1934_acpi_parse_channel_config() calls acpi_evaluate_dsm() four times to read channel configuration but does not validate the returned object type or array bounds:

1. No check that the returned object is ACPI_TYPE_PACKAGE. If

     firmware returns a different type, accessing rez->package.count
     and rez->package.elements on a non-package object produces
     unpredictable values.

2. The loop bounds use rez->package.count directly without capping

     to PAC1934_MAX_NUM_CHANNELS (4).  A package with more elements
     than expected causes writes past the fixed-size labels[],
     shunts[], and active_channels[] arrays.

3. The bipolar and sample rate DSM calls access elements[0] without verifying package.count >= 1.

Use acpi_evaluate_dsm_typed() with ACPI_TYPE_PACKAGE to validate the return type at evaluation time. Cap loop iterations to PAC1934_MAX_NUM_CHANNELS, and verify package count >= 1 before accessing elements[0].

---
Changes in v5:
- Keep existing declaration order, only add new 'count' variable, per Andy Shevchenko
- Replace (count & ~1u) with min_t() cap and i + 1 < count loop bound to avoid unsigned literal question, per Andy Shevchenko

Changes in v4:
- Reorder variable declarations to reverse xmas tree, per Andy Shevchenko
- Drop casts from min() parameters, per Andy Shevchenko

Changes in v3:
- Use acpi_evaluate_dsm_typed() instead of manual type checks, per Andy Shevchenko
- Use (count & ~1u) for the name+mohm loop bound to ensure pairs are complete, per Andy Shevchenko
- Drop Fixes: and Cc: stable tags since this is hardening rather than a fix for a specific regression, per Jonathan Cameron

 drivers/iio/adc/pac1934.c | 33 +++++++++++++++++++++++++++------
 1 file changed, 27 insertions(+), 6 deletions(-)
drivers/iio/adc/pac1934.c +27 -6
index de59dc27c4405..cdb730217d46c 100644
@@ -1091,6 +1091,7 @@ static int pac1934_acpi_parse_channel_config(struct i2c_client *client,
 	union acpi_object *rez;
 	struct device *dev = &client->dev;
 	unsigned short bi_dir_mask;
+	unsigned int count;
 	int idx, i;
 	guid_t guid;
 
@@ -1098,11 +1099,15 @@ static int pac1934_acpi_parse_channel_config(struct i2c_client *client,
 
 	guid_parse(PAC1934_DSM_UUID, &guid);
 
-	rez = acpi_evaluate_dsm(handle, &guid, 0, PAC1934_ACPI_GET_NAMES_AND_MOHMS_VALS, NULL);
+	rez = acpi_evaluate_dsm_typed(handle, &guid, 0,
+				      PAC1934_ACPI_GET_NAMES_AND_MOHMS_VALS,
+				      NULL, ACPI_TYPE_PACKAGE);
 	if (!rez)
 		return -EINVAL;
 
-	for (i = 0; i < rez->package.count; i += 2) {
+	count = min_t(unsigned int, rez->package.count,
+		      PAC1934_MAX_NUM_CHANNELS * 2);
+	for (i = 0; i + 1 < count; i += 2) {
 		idx = i / 2;
 		info->labels[idx] =
 			devm_kmemdup(dev, rez->package.elements[i].string.pointer,
@@ -1119,7 +1124,9 @@ static int pac1934_acpi_parse_channel_config(struct i2c_client *client,
 
 	ACPI_FREE(rez);
 
-	rez = acpi_evaluate_dsm(handle, &guid, 1, PAC1934_ACPI_GET_UOHMS_VALS, NULL);
+	rez = acpi_evaluate_dsm_typed(handle, &guid, 1,
+				      PAC1934_ACPI_GET_UOHMS_VALS,
+				      NULL, ACPI_TYPE_PACKAGE);
 	if (!rez) {
 		/*
 		 * initializing with default values
@@ -1130,7 +1137,9 @@ static int pac1934_acpi_parse_channel_config(struct i2c_client *client,
 		return 0;
 	}
 
-	for (i = 0; i < rez->package.count; i++) {
+	count = min_t(unsigned int, rez->package.count,
+		      PAC1934_MAX_NUM_CHANNELS);
+	for (i = 0; i < count; i++) {
 		idx = i;
 		info->shunts[idx] = rez->package.elements[i].integer.value;
 		info->active_channels[idx] = (info->shunts[idx] != 0);
@@ -1138,9 +1147,15 @@ static int pac1934_acpi_parse_channel_config(struct i2c_client *client,
 
 	ACPI_FREE(rez);
 
-	rez = acpi_evaluate_dsm(handle, &guid, 1, PAC1934_ACPI_GET_BIPOLAR_SETTINGS, NULL);
+	rez = acpi_evaluate_dsm_typed(handle, &guid, 1,
+				      PAC1934_ACPI_GET_BIPOLAR_SETTINGS,
+				      NULL, ACPI_TYPE_PACKAGE);
 	if (!rez)
 		return -EINVAL;
+	if (rez->package.count < 1) {
+		ACPI_FREE(rez);
+		return -EINVAL;
+	}
 
 	bi_dir_mask = rez->package.elements[0].integer.value;
 	info->bi_dir[0] = ((bi_dir_mask & (1 << 3)) | (bi_dir_mask & (1 << 7))) != 0;
@@ -1150,9 +1165,15 @@ static int pac1934_acpi_parse_channel_config(struct i2c_client *client,
 
 	ACPI_FREE(rez);
 
-	rez = acpi_evaluate_dsm(handle, &guid, 1, PAC1934_ACPI_GET_SAMP, NULL);
+	rez = acpi_evaluate_dsm_typed(handle, &guid, 1,
+				      PAC1934_ACPI_GET_SAMP,
+				      NULL, ACPI_TYPE_PACKAGE);
 	if (!rez)
 		return -EINVAL;
+	if (rez->package.count < 1) {
+		ACPI_FREE(rez);
+		return -EINVAL;
+	}
 
 	info->sample_rate_value = rez->package.elements[0].integer.value;
 
Signature · 1 line
2.47.3
maintainer Reviewed-by on v5 1/1
#1 lore raw
Quoted 49 lines · Tristan Madani

EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe

From: Tristan Madani <[email protected]>

pac1934_acpi_parse_channel_config() calls acpi_evaluate_dsm() four times to read channel configuration but does not validate the returned
object type or array bounds:

  1. No check that the returned object is ACPI_TYPE_PACKAGE.  If firmware returns a different type, accessing rez->package.count and rez->package.elements on a non-package object produces unpredictable values.

  2. The loop bounds use rez->package.count directly without capping to PAC1934_MAX_NUM_CHANNELS (4).  A package with more elements than expected causes writes past the fixed-size labels[], shunts[], and active_channels[] arrays.

  3. The bipolar and sample rate DSM calls access elements[0] without verifying package.count >= 1.

Use acpi_evaluate_dsm_typed() with ACPI_TYPE_PACKAGE to validate the return type at evaluation time.  Cap loop iterations to PAC1934_MAX_NUM_CHANNELS, and verify package count >= 1 before accessing elements[0].

---
Changes in v5:
- Keep existing declaration order, only add new 'count' variable, per Andy Shevchenko
- Replace (count & ~1u) with min_t() cap and i + 1 < count loop bound to avoid unsigned literal question, per Andy Shevchenko

Changes in v4:
- Reorder variable declarations to reverse xmas tree, per Andy Shevchenko
- Drop casts from min() parameters, per Andy Shevchenko

Changes in v3:
- Use acpi_evaluate_dsm_typed() instead of manual type checks, per Andy Shevchenko
- Use (count & ~1u) for the name+mohm loop bound to ensure pairs are complete, per Andy Shevchenko
- Drop Fixes: and Cc: stable tags since this is hardening rather than a fix for a specific regression, per Jonathan Cameron drivers/iio/adc/pac1934.c | 33 +++++++++++++++++++++++++++------

Quoted 2 lines
 1 file changed, 27 insertions(+), 6 deletions(-)
#2 lore raw
Quoted 3 lines · On Fri, Oct 30, 2026 at 04:32:51PM +0000, [email protected] wrote:

On Fri, 2026-10-30 at 12:35 +0000, Tristan Madani wrote:

Thanks for the review, but we don't allow min_t() without very good justifications. This code doesn't need that macro to be used. The author of the code should first get a bit familiar with types and integer promotions in C programming language to understand what's going on here.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#3 lore raw
In reply to Tristan Madani
Quoted 21 lines · Tristan Madani

pac1934_acpi_parse_channel_config() calls acpi_evaluate_dsm() four times to read channel configuration but does not validate the returned object type or array bounds:

1. No check that the returned object is ACPI_TYPE_PACKAGE. If

     firmware returns a different type, accessing rez->package.count
     and rez->package.elements on a non-package object produces
     unpredictable values.

2. The loop bounds use rez->package.count directly without capping

     to PAC1934_MAX_NUM_CHANNELS (4).  A package with more elements
     than expected causes writes past the fixed-size labels[],
     shunts[], and active_channels[] arrays.

3. The bipolar and sample rate DSM calls access elements[0] without verifying package.count >= 1.

Use acpi_evaluate_dsm_typed() with ACPI_TYPE_PACKAGE to validate the return type at evaluation time. Cap loop iterations to PAC1934_MAX_NUM_CHANNELS, and verify package count >= 1 before accessing elements[0].

...

drivers/iio/adc/pac1934.c static int pac1934_acpi_parse_channel_config(struct i2c_client *client, · line 1109

Quoted 2 lines
drivers/iio/adc/pac1934.c +2 -0
+	count = min_t(unsigned int, rez->package.count,
+		      PAC1934_MAX_NUM_CHANNELS * 2);

We have already discussed that, that min_t() has not to be in use. Ditto for the rest of the similar cases.

I would recommend you to slow down, drop AI assistance and check the code (variable types, available macros and their usages) first. Don't send a new version until you understand what AI is trying to do and what we want to have at the end.

Signature · 2 lines
With Best Regards,
Andy Shevchenko