diff mbox

PM / devfreq: Fix potential NULL pointer dereference in governor_store

Message ID 20171206202015.GA15636@embeddedor.com (mailing list archive)
State Deferred
Headers show

Commit Message

Gustavo A. R. Silva Dec. 6, 2017, 8:20 p.m. UTC
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")
Signed-off-by: Gustavo A. R. Silva <garsilva@embeddedor.com>
---
 drivers/devfreq/devfreq.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

Comments

Chanwoo Choi Dec. 7, 2017, 1:24 a.m. UTC | #1
On 2017년 12월 07일 05:20, Gustavo A. R. Silva wrote:
> 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")
> Signed-off-by: Gustavo A. R. Silva <garsilva@embeddedor.com>
> ---
>  drivers/devfreq/devfreq.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c
> index 78fb496..14fe76b 100644
> --- a/drivers/devfreq/devfreq.c
> +++ b/drivers/devfreq/devfreq.c
> @@ -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.
Reviewed-by: Chanwoo Choi <cw00.choi@samsung.com>
MyungJoo Ham Dec. 7, 2017, 2:41 a.m. UTC | #2
> 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")
> Signed-off-by: Gustavo A. R. Silva <garsilva@embeddedor.com>

Acked-by: MyungJoo Ham <myungjoo.ham@samsung.com>


Cheers,
MyungJoo

> ---
>  drivers/devfreq/devfreq.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c
> index 78fb496..14fe76b 100644
Gustavo A. R. Silva Dec. 7, 2017, 11:56 p.m. UTC | #3
Hi Chanwoo,

Quoting Chanwoo Choi <cw00.choi@samsung.com>:

> On 2017년 12월 07일 05:20, Gustavo A. R. Silva wrote:
>> 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")
>> Signed-off-by: Gustavo A. R. Silva <garsilva@embeddedor.com>
>> ---
>>  drivers/devfreq/devfreq.c | 3 ++-
>>  1 file changed, 2 insertions(+), 1 deletion(-)
>>
>> diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c
>> index 78fb496..14fe76b 100644
>> --- a/drivers/devfreq/devfreq.c
>> +++ b/drivers/devfreq/devfreq.c
>> @@ -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.
>

I got it.

> So, looks good to me.
> Reviewed-by: Chanwoo Choi <cw00.choi@samsung.com>
>

Thank you

--
Gustavo A. R. Silva
Gustavo A. R. Silva Dec. 7, 2017, 11:57 p.m. UTC | #4
Quoting MyungJoo Ham <myungjoo.ham@samsung.com>:

>> 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")
>> Signed-off-by: Gustavo A. R. Silva <garsilva@embeddedor.com>
>
> Acked-by: MyungJoo Ham <myungjoo.ham@samsung.com>
>

Thank you, MyungJoo

--
Gustavo A. R. Silva
diff mbox

Patch

diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c
index 78fb496..14fe76b 100644
--- a/drivers/devfreq/devfreq.c
+++ b/drivers/devfreq/devfreq.c
@@ -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;
 	}