Skip to contentSkip to conversation

platform/x86/intel/pmc: validate LPM mode without stale index

PATCH In review Yousef Alhouseen 2 participants 3 messages Jun 30 – Jul 18

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 platform/x86/intel/pmc: validate LPM mode without stale index

pmc_lpm_mode_write() compares the requested mode with the loop variable after pmc_for_each_mode(). If no low-power modes are enabled, the loop never assigns that variable and the comparison reads an uninitialized value.

Track a successful match explicitly, including the empty-mode case.

---
 drivers/platform/x86/intel/pmc/core.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/platform/x86/intel/pmc/core.c +10 -4
index 9f77c0716e59..9833df172d4e 100644
@@ -1147,6 +1147,7 @@ static ssize_t pmc_core_lpm_latch_mode_write(struct file *file,
 	struct pmc_dev *pmcdev = s->private;
 	struct pmc *pmc = pmcdev->pmcs[PMC_IDX_MAIN];
 	bool clear = false, c10 = false;
+	bool mode_enabled = false;
 	unsigned char buf[8];
 	int mode;
 	u32 reg;
@@ -1167,11 +1168,16 @@ static ssize_t pmc_core_lpm_latch_mode_write(struct file *file,
 	mode = sysfs_match_string(pmc_lpm_modes, buf);
 
 	/* Check string matches enabled mode */
-	pmc_for_each_mode(m, pmc)
-		if (mode == m)
-			break;
+	if (mode >= 0) {
+		pmc_for_each_mode(m, pmc) {
+			if (mode == m) {
+				mode_enabled = true;
+				break;
+			}
+		}
+	}
 
-	if (mode != m || mode < 0) {
+	if (!mode_enabled) {
 		if (sysfs_streq(buf, "clear"))
 			clear = true;
 		else if (sysfs_streq(buf, "c10"))
Signature · 1 line
2.54.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 25 lines · Yousef Alhouseen

pmc_lpm_mode_write() compares the requested mode with the loop variable after pmc_for_each_mode(). If no low-power modes are enabled, the loop never assigns that variable and the comparison reads an uninitialized value.

Track a successful match explicitly, including the empty-mode case.

---
 drivers/platform/x86/intel/pmc/core.c | 14 ++++++++++----
 1 file changed, 10 insertions(+), 4 deletions(-)
drivers/platform/x86/intel/pmc/core.c +1 -0
index 9f77c0716e59..9833df172d4e 100644
@@ -1147,6 +1147,7 @@ static ssize_t pmc_core_lpm_latch_mode_write(struct file *file,
 	struct pmc_dev *pmcdev = s->private;
 	struct pmc *pmc = pmcdev->pmcs[PMC_IDX_MAIN];
 	bool clear = false, c10 = false;
+	bool mode_enabled = false;
 	unsigned char buf[8];
 	int mode;
 	u32 reg;

drivers/platform/x86/intel/pmc/core.c static ssize_t pmc_core_lpm_latch_mode_write(struct file *file, · line 1169

Quoted 2 lines
drivers/platform/x86/intel/pmc/core.c +0 -0
@@ -1167,11 +1168,16 @@ static ssize_t pmc_core_lpm_latch_mode_write(struct file *file,
 	mode = sysfs_match_string(pmc_lpm_modes, buf);
 

Since you're now checking mode right after assignment remove this empty line to tie it to the test.

Quoted 21 lines
drivers/platform/x86/intel/pmc/core.c +9 -4
 	/* Check string matches enabled mode */
-	pmc_for_each_mode(m, pmc)
-		if (mode == m)
-			break;
+	if (mode >= 0) {
+		pmc_for_each_mode(m, pmc) {
+			if (mode == m) {
+				mode_enabled = true;
+				break;
+			}
+		}
+	}
 
-	if (mode != m || mode < 0) {
+	if (!mode_enabled) {
 		if (sysfs_streq(buf, "clear"))
 			clear = true;
 		else if (sysfs_streq(buf, "c10"))
Signature · 1 line
2.54.0

With the above change,

Thanks

#2 lore raw

Agreed. I'll remove that blank line in v2 and carry your Reviewed-by.

Thanks,
Yousef

Quoted 59 lines · David Box
Quoted 25 lines · Yousef Alhouseen

pmc_lpm_mode_write() compares the requested mode with the loop variable after pmc_for_each_mode(). If no low-power modes are enabled, the loop never assigns that variable and the comparison reads an uninitialized value.

Track a successful match explicitly, including the empty-mode case.

---
drivers/platform/x86/intel/pmc/core.c | 14 ++++++++++----

1 file changed, 10 insertions(+), 4 deletions(-)
drivers/platform/x86/intel/pmc/core.c +1 -0
index 9f77c0716e59..9833df172d4e 100644
@@ -1147,6 +1147,7 @@ static ssize_t pmc_core_lpm_latch_mode_write(struct file *file,
 struct pmc_dev *pmcdev = s->private;
 struct pmc *pmc = pmcdev->pmcs[PMC_IDX_MAIN];
 bool clear = false, c10 = false;
+ bool mode_enabled = false;
 unsigned char buf[8];
 int mode;
 u32 reg;

drivers/platform/x86/intel/pmc/core.c static ssize_t pmc_core_lpm_latch_mode_write(struct file *file, · line 1169

Quoted 2 lines
drivers/platform/x86/intel/pmc/core.c +0 -0
@@ -1167,11 +1168,16 @@ static ssize_t pmc_core_lpm_latch_mode_write(struct file *file,
 mode = sysfs_match_string(pmc_lpm_modes, buf);
 

Since you're now checking mode right after assignment remove this empty line to tie it to the test.

Quoted 19 lines
drivers/platform/x86/intel/pmc/core.c +9 -5
 /* Check string matches enabled mode */
- pmc_for_each_mode(m, pmc)
- if (mode == m)
- break;
+ if (mode >= 0) {
+ pmc_for_each_mode(m, pmc) {
+ if (mode == m) {
+ mode_enabled = true;
+ break;
+ }
+ }
+ }
 
- if (mode != m || mode < 0) {
+ if (!mode_enabled) {
 if (sysfs_streq(buf, "clear"))
 clear = true;
 else if (sysfs_streq(buf, "c10"))
--
Quoted 2 lines

2.54.0

With the above change,

Thanks