Skip to contentSkip to conversation

PM / devfreq: Fix potential NULL pointer dereference in governor_store

PATCH In review Gustavo A. R. Silva 3 participants 5 messages Dec 6, 2017 – Dec 7, 2017 Device frequency (devfreq)

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 PM / devfreq: Fix potential NULL pointer dereference in governor_store

df->governor is being dereferenced before it is null checked, hence there is a potential null pointer dereference.

Notice that df->governor is being null checked at line 1004:
if (df->governor) {, which implies it might be null.

Fix this by null checking df->governor before dereferencing it.

Addresses-Coverity-ID: 1401988 ("Dereference before null check")

Fixes bcf23c79c4e4 ("PM / devfreq: Fix available_governor sysfs")
---
 drivers/devfreq/devfreq.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/devfreq/devfreq.c +2 -1
index 78fb496..14fe76b 100644
@@ -996,7 +996,8 @@ static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
 	if (df->governor == governor) {
 		ret = 0;
 		goto out;
-	} else if (df->governor->immutable || governor->immutable) {
+	} else if ((df->governor && df->governor->immutable) ||
+					governor->immutable) {
 		ret = -EINVAL;
 		goto out;
 	}
Signature · 1 line
2.7.4
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 21 lines · Gustavo A. R. Silva

df->governor is being dereferenced before it is null checked, hence there is a potential null pointer dereference.

Notice that df->governor is being null checked at line 1004:
if (df->governor) {, which implies it might be null.

Fix this by null checking df->governor before dereferencing it.

Addresses-Coverity-ID: 1401988 ("Dereference before null check")

Fixes bcf23c79c4e4 ("PM / devfreq: Fix available_governor sysfs")
---
 drivers/devfreq/devfreq.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/devfreq/devfreq.c +0 -0
index 78fb496..14fe76b 100644

drivers/devfreq/devfreq.c static ssize_t governor_store(struct device *dev, struct device_attribute *attr, · line 1003

Quoted 9 lines
drivers/devfreq/devfreq.c +2 -1
@@ -996,7 +996,8 @@ static ssize_t governor_store(struct device *dev, struct device_attribute *attr,
 	if (df->governor == governor) {
 		ret = 0;
 		goto out;
-	} else if (df->governor->immutable || governor->immutable) {
+	} else if ((df->governor && df->governor->immutable) ||
+					governor->immutable) {
 		ret = -EINVAL;
 		goto out;
 	}

Actually, df->governor would be never NULL because devfreq_add_device() initializes the ->governor always. But, governor_store() doesn't know it.

So, looks good to me.

Signature · 3 lines
Best Regards,
Chanwoo Choi
Samsung Electronics
#2 lore raw

Hi Chanwoo,

Quoting Chanwoo Choi <[email protected]>:

Quoted 27 lines · Gustavo A. R. Silva
Quoted 26 lines

df->governor is being dereferenced before it is null checked, hence there is a potential null pointer dereference.

Notice that df->governor is being null checked at line 1004:
if (df->governor) {, which implies it might be null.

Fix this by null checking df->governor before dereferencing it.

Addresses-Coverity-ID: 1401988 ("Dereference before null check")

Fixes bcf23c79c4e4 ("PM / devfreq: Fix available_governor sysfs")
---
 drivers/devfreq/devfreq.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/devfreq/devfreq.c +1 -1
index 78fb496..14fe76b 100644
@@ -996,7 +996,8 @@ static ssize_t governor_store(struct device  
 *dev, struct device_attribute *attr,
 	if (df->governor == governor) {
 		ret = 0;
 		goto out;
-	} else if (df->governor->immutable || governor->immutable) {
+	} else if ((df->governor && df->governor->immutable) ||
Quoted 9 lines
Quoted 5 lines
drivers/devfreq/devfreq.c +1 -0
+					governor->immutable) {
 		ret = -EINVAL;
 		goto out;

}

Actually, df->governor would be never NULL because devfreq_add_device() initializes the ->governor always. But, governor_store() doesn't know it.

I got it.

Quoted 3 lines

So, looks good to me.

Thank you

--
Gustavo A. R. Silva

maintainer Acked-by on v1 1/1
#3 lore raw
In reply to Gustavo A. R. Silva
Quoted 11 lines

df->governor is being dereferenced before it is null checked, hence there is a potential null pointer dereference.

Notice that df->governor is being null checked at line 1004:
if (df->governor) {, which implies it might be null.

Fix this by null checking df->governor before dereferencing it.

Addresses-Coverity-ID: 1401988 ("Dereference before null check")

Fixes bcf23c79c4e4 ("PM / devfreq: Fix available_governor sysfs")

Cheers,
MyungJoo

Quoted 6 lines
---
 drivers/devfreq/devfreq.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
drivers/devfreq/devfreq.c +0 -0
index 78fb496..14fe76b 100644
#4 lore raw

Quoting MyungJoo Ham <[email protected]>:

Quoted 6 lines
Quoted 6 lines

df->governor is being dereferenced before it is null checked, hence there is a potential null pointer dereference.

Notice that df->governor is being null checked at line 1004:
if (df->governor) {, which implies it might be null.

Quoted 8 lines
Quoted 5 lines

Fix this by null checking df->governor before dereferencing it.

Addresses-Coverity-ID: 1401988 ("Dereference before null check")

Fixes bcf23c79c4e4 ("PM / devfreq: Fix available_governor sysfs")

Thank you, MyungJoo

--
Gustavo A. R. Silva