2014-02-24 07:15:16

by Saravana Kannan

[permalink] [raw]
Subject: [PATCH] PM / devfreq: Fix out of bounds access of transition table array

The previous_freq value for a device could be an invalid frequency that
results in a error value being returned from devfreq_get_freq_level().
Check for an error value before using that to index into the transition
table.

Not doing this check will result in memory corruption when previous_freq is
not a valid frequency.

Signed-off-by: Saravana Kannan <[email protected]>
---
drivers/devfreq/devfreq.c | 14 ++++++++++----
1 file changed, 10 insertions(+), 4 deletions(-)

diff --git a/drivers/devfreq/devfreq.c b/drivers/devfreq/devfreq.c
index a0b2f7e..f91ea29 100644
--- a/drivers/devfreq/devfreq.c
+++ b/drivers/devfreq/devfreq.c
@@ -101,14 +101,20 @@ static int devfreq_update_status(struct devfreq *devfreq, unsigned long freq)
cur_time = jiffies;
devfreq->time_in_state[lev] +=
cur_time - devfreq->last_stat_updated;
- if (freq != devfreq->previous_freq) {
- prev_lev = devfreq_get_freq_level(devfreq,
- devfreq->previous_freq);
+ devfreq->last_stat_updated = cur_time;
+
+ if (freq == devfreq->previous_freq)
+ return 0;
+
+ prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
+ if (prev_lev < 0)
+ return 0;
+
+ if (lev != prev_lev) {
devfreq->trans_table[(prev_lev *
devfreq->profile->max_state) + lev]++;
devfreq->total_trans++;
}
- devfreq->last_stat_updated = cur_time;

return 0;
}
--
1.8.2.1

The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
hosted by The Linux Foundation


2014-02-25 21:13:59

by Saravana Kannan

[permalink] [raw]
Subject: Re: [PATCH] PM / devfreq: Fix out of bounds access of transition table array

On 02/23/2014 11:15 PM, Saravana Kannan wrote:
> The previous_freq value for a device could be an invalid frequency that
> results in a error value being returned from devfreq_get_freq_level().
> Check for an error value before using that to index into the transition
> table.
>
> Not doing this check will result in memory corruption when previous_freq is
> not a valid frequency.
>
> Signed-off-by: Saravana Kannan <[email protected]>

MyungJoo/Kyungmin,

Would either of you have some time to respond to this?

Thanks,
Saravana


--
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
hosted by The Linux Foundation

2014-02-26 05:12:16

by MyungJoo Ham

[permalink] [raw]
Subject: Re: Re: [PATCH] PM / devfreq: Fix out of bounds access of transition table array

> On 02/23/2014 11:15 PM, Saravana Kannan wrote:
> > The previous_freq value for a device could be an invalid frequency that
> > results in a error value being returned from devfreq_get_freq_level().
> > Check for an error value before using that to index into the transition
> > table.
> >
> > Not doing this check will result in memory corruption when previous_freq is
> > not a valid frequency.
> >
> > Signed-off-by: Saravana Kannan <[email protected]>
>
> MyungJoo/Kyungmin,
>
> Would either of you have some time to respond to this?
>
> Thanks,
> Saravana

Dear Saravana,

> > + prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
> > + if (prev_lev < 0)
> > + return 0;

If devfreq_get_freq_level returned error, please return that error
to the caller. You are retuning 0 in that case.

Plus, do you think we are going to change profile->freq_table in run-time?
(by accidently? or intentionally?)

Cheers,
MyungJoo.

>
>
> --
> The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
> hosted by The Linux Foundation
>
>
>
>
>
>
>
????{.n?+???????+%?????ݶ??w??{.n?+????{??G?????{ay?ʇڙ?,j??f???h?????????z_??(?階?ݢj"???m??????G????????????&???~???iO???z??v?^?m???? ????????I?

2014-02-27 00:45:17

by Saravana Kannan

[permalink] [raw]
Subject: Re: [PATCH] PM / devfreq: Fix out of bounds access of transition table array

On 02/25/2014 09:12 PM, ?Ը??? wrote:
>> On 02/23/2014 11:15 PM, Saravana Kannan wrote:
>>> The previous_freq value for a device could be an invalid frequency that
>>> results in a error value being returned from devfreq_get_freq_level().
>>> Check for an error value before using that to index into the transition
>>> table.
>>>
>>> Not doing this check will result in memory corruption when previous_freq is
>>> not a valid frequency.
>>>
>>> Signed-off-by: Saravana Kannan <[email protected]>
>>
>> MyungJoo/Kyungmin,
>>
>> Would either of you have some time to respond to this?
>>
>> Thanks,
>> Saravana
>
> Dear Saravana,
>
>>> + prev_lev = devfreq_get_freq_level(devfreq, devfreq->previous_freq);
>>> + if (prev_lev < 0)
>>> + return 0;
>
> If devfreq_get_freq_level returned error, please return that error
> to the caller. You are retuning 0 in that case.
>
> Plus, do you think we are going to change profile->freq_table in run-time?
> (by accidently? or intentionally?)

Hi MyungJoo,

The reason for this fix is that some devices can't reliably detect their
current frequency. So, we set the initial freq to 0 so that the first
request from the default governor actually goes through.

If we randomly pick one of the valid frequencies, then the initial
request could get short circuited by devfreq, cause wrong stats, the
device itself might short circuit it after comparing it to prev freq, etc.

The reason I intentionally ignored the error was to prevent printing an
error at boot every time for such devices. I could do better with more
complicated code, but I couldn't imagine any other case where
previous_freq could actually end up being an invalid value.

Hold on, I just realized that the stats code is completely wrong! Isn't
the current code updating the time_in_state[] of the wrong level? It's
updating the new level's time in state instead of the previous one?

I'll send out a patch that fixes all this with in a couple of days.

Thanks,
Saravana

--
The Qualcomm Innovation Center, Inc. is a member of the Code Aurora Forum,
hosted by The Linux Foundation