Skip to contentSkip to conversation

platform/x86/intel/pmc: Fix mode check in pmc_core_lpm_latch_mode_write()

PATCH New Ian Rogers 1 participant 1 message Nov 19

Subsystems (2)
lore
Share & tools
lore

Ian Rogers

author
#0 lore raw

PATCH 1/1 platform/x86/intel/pmc: Fix mode check in pmc_core_lpm_latch_mode_write()

In pmc_core_lpm_latch_mode_write(), when pmc->num_lpm_modes is 0 (all S0ix substates disabled), pmc_for_each_mode(m, pmc) terminates without assigning m. The subsequent 'if (mode != m || mode < 0)' check then reads uninitialized stack memory for m; under zero stack initialization (m == 0), writing "S0i2.0" (mode == 0) evaluates '0 != 0' as false and latches a disabled substate.

Track whether mode matched an enabled substate with an explicit boolean instead of comparing against m after the loop.

Found via Clang -Wconditional-uninitialized on an x86_64 allyesconfig build. Verified by building drivers/platform/x86/intel/pmc/core.o with LLVM=1 W=1 (-Werror) and -Wconditional-uninitialized, and with a standalone C reproducer under ASan/UBSan; not runtime-tested on Intel PMC hardware with disabled LPM substates.

Fixes 8074a79fad2e ("platform/x86: intel_pmc_core: Add option to set/clear LPM mode")
Fixes e35ee8ee498d ("platform/x86: intel/pmc: Remove unused param idx from pmc_for_each_mode()")
---
 drivers/platform/x86/intel/pmc/core.c | 17 +++++++++++------
 1 file changed, 11 insertions(+), 6 deletions(-)
drivers/platform/x86/intel/pmc/core.c +11 -6
index 47759b2a8012..7e12990d45a2 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 matched = false;
 	unsigned char buf[8];
 	int mode;
 	u32 reg;
@@ -1165,13 +1166,17 @@ static ssize_t pmc_core_lpm_latch_mode_write(struct file *file,
 	 *	'clear'
 	 */
 	mode = sysfs_match_string(pmc_lpm_modes, buf);
+	if (mode >= 0) {
+		/* Check string matches enabled mode */
+		pmc_for_each_mode(m, pmc) {
+			if (mode == m) {
+				matched = true;
+				break;
+			}
+		}
+	}
 
-	/* Check string matches enabled mode */
-	pmc_for_each_mode(m, pmc)
-		if (mode == m)
-			break;
-
-	if (mode != m || mode < 0) {
+	if (!matched) {
 		if (sysfs_streq(buf, "clear"))
 			clear = true;
 		else if (sysfs_streq(buf, "c10"))
Signature · 1 line
2.56.1.708.g77570a6a1d-goog