Skip to contentSkip to conversation

power: sequencing: fix NULL-pointer dereference in error path

PATCH Superseded Bartosz Golaszewski 2 participants 5 messages Jul 12, 2024 Power sequencing

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 power: sequencing: fix NULL-pointer dereference in error path

From: Bartosz Golaszewski <[email protected]>

We may call pwrseq_target_free() on a target without the final unit assigned yet. In this case pwrseq_unit_put() will dereference a NULL-pointer. Add a check to the latter function.

Fixes 249ebf3f65f8 ("power: sequencing: implement the pwrseq core")
---
 drivers/power/sequencing/core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/power/sequencing/core.c +2 -1
index 9c32b07a55e7..fe07100e4b33 100644
@@ -119,7 +119,8 @@ static void pwrseq_unit_release(struct kref *ref);
 
 static void pwrseq_unit_put(struct pwrseq_unit *unit)
 {
-	kref_put(&unit->ref, pwrseq_unit_release);
+	if (unit)
+		kref_put(&unit->ref, pwrseq_unit_release);
 }
 
 /**
Signature · 1 line
2.43.0
#1 lore raw
Quoted 19 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

We may call pwrseq_target_free() on a target without the final unit assigned yet. In this case pwrseq_unit_put() will dereference a NULL-pointer. Add a check to the latter function.

Fixes 249ebf3f65f8 ("power: sequencing: implement the pwrseq core")
---
 drivers/power/sequencing/core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/power/sequencing/core.c +0 -0
index 9c32b07a55e7..fe07100e4b33 100644

drivers/power/sequencing/core.c static void pwrseq_unit_release(struct kref *ref); · line 122

Quoted 5 lines
drivers/power/sequencing/core.c +1 -1
@@ -119,7 +119,8 @@ static void pwrseq_unit_release(struct kref *ref);
 
 static void pwrseq_unit_put(struct pwrseq_unit *unit)
 {
-	kref_put(&unit->ref, pwrseq_unit_release);
+	if (unit)

I was wondering where you would put the check. But it needs to be:

	if (!IS_ERR_OR_NULL(unit))

regards,
dan carpenter

Quoted 6 lines
drivers/power/sequencing/core.c +1 -0
+		kref_put(&unit->ref, pwrseq_unit_release);
 }
 
 /**
Signature · 1 line
2.43.0
#2 lore raw
Quoted 29 lines · Dan Carpenter
Quoted 19 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

We may call pwrseq_target_free() on a target without the final unit assigned yet. In this case pwrseq_unit_put() will dereference a NULL-pointer. Add a check to the latter function.

Fixes 249ebf3f65f8 ("power: sequencing: implement the pwrseq core")
---
 drivers/power/sequencing/core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/power/sequencing/core.c +0 -0
index 9c32b07a55e7..fe07100e4b33 100644

drivers/power/sequencing/core.c static void pwrseq_unit_release(struct kref *ref); · line 122

Quoted 5 lines
drivers/power/sequencing/core.c +1 -1
@@ -119,7 +119,8 @@ static void pwrseq_unit_release(struct kref *ref);
 
 static void pwrseq_unit_put(struct pwrseq_unit *unit)
 {
-     kref_put(&unit->ref, pwrseq_unit_release);
+     if (unit)

I was wondering where you would put the check. But it needs to be:

Quoted 5 lines
        if (!IS_ERR_OR_NULL(unit))

regards,
dan carpenter

Am I missing something? pwrseq_unit_new() can only return NULL on error.

Bart

Quoted 6 lines
Quoted 6 lines
drivers/power/sequencing/core.c +1 -1
+             kref_put(&unit->ref, pwrseq_unit_release);
 }
 
 /**
--

2.43.0

#3 lore raw
Quoted 30 lines · Bartosz Golaszewski
Quoted 29 lines · Dan Carpenter
Quoted 19 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

We may call pwrseq_target_free() on a target without the final unit assigned yet. In this case pwrseq_unit_put() will dereference a NULL-pointer. Add a check to the latter function.

Fixes 249ebf3f65f8 ("power: sequencing: implement the pwrseq core")
---
 drivers/power/sequencing/core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/power/sequencing/core.c +0 -0
index 9c32b07a55e7..fe07100e4b33 100644

drivers/power/sequencing/core.c static void pwrseq_unit_release(struct kref *ref); · line 122

Quoted 5 lines
drivers/power/sequencing/core.c +1 -1
@@ -119,7 +119,8 @@ static void pwrseq_unit_release(struct kref *ref);
 
 static void pwrseq_unit_put(struct pwrseq_unit *unit)
 {
-     kref_put(&unit->ref, pwrseq_unit_release);
+     if (unit)

I was wondering where you would put the check. But it needs to be:

Quoted 8 lines
Quoted 5 lines
        if (!IS_ERR_OR_NULL(unit))

regards,
dan carpenter

Am I missing something? pwrseq_unit_new() can only return NULL on error.

It's not pwrseq_unit_new() but pwrseq_unit_setup() that's the issue. The target->unit = pwrseq_unit_setup() assignment in pwrseq_do_setup_targets().

regards,
dan carpenter

#4 lore raw
Quoted 41 lines · Dan Carpenter
Quoted 30 lines · Bartosz Golaszewski
Quoted 29 lines · Dan Carpenter
Quoted 19 lines · Bartosz Golaszewski

From: Bartosz Golaszewski <[email protected]>

We may call pwrseq_target_free() on a target without the final unit assigned yet. In this case pwrseq_unit_put() will dereference a NULL-pointer. Add a check to the latter function.

Fixes 249ebf3f65f8 ("power: sequencing: implement the pwrseq core")
---
 drivers/power/sequencing/core.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/power/sequencing/core.c +0 -0
index 9c32b07a55e7..fe07100e4b33 100644

drivers/power/sequencing/core.c static void pwrseq_unit_release(struct kref *ref); · line 122

Quoted 5 lines
drivers/power/sequencing/core.c +1 -1
@@ -119,7 +119,8 @@ static void pwrseq_unit_release(struct kref *ref);
 
 static void pwrseq_unit_put(struct pwrseq_unit *unit)
 {
-     kref_put(&unit->ref, pwrseq_unit_release);
+     if (unit)

I was wondering where you would put the check. But it needs to be:

Quoted 8 lines
Quoted 5 lines
        if (!IS_ERR_OR_NULL(unit))

regards,
dan carpenter

Am I missing something? pwrseq_unit_new() can only return NULL on error.

Quoted 7 lines

It's not pwrseq_unit_new() but pwrseq_unit_setup() that's the issue. The target->unit = pwrseq_unit_setup() assignment in pwrseq_do_setup_targets().

regards,
dan carpenter

Indeed. Thanks.

Bart