2021-07-28 10:25:57

by Tuo Li

[permalink] [raw]
Subject: [BUG] power: supply: 88pm860x_battery: possible uninitialized-variable access in measure_vbatt()

Hello,

Our static analysis tool finds a possible uninitialized-variable access
in the 88pm860x_battery driver in Linux 5.14.0-rc3:

In calc_soc():
369:    int ocv;
376:    switch (state) {
380:    case OCV_MODE_SLEEP:
381:        ret = measure_vbatt(info, OCV_MODE_SLEEP, &ocv);

In measure_vbatt(struct pm860x_battery_info *info, int state, int *data)
176:    switch (state) {
184:    case OCV_MODE_SLEEP:
201:        *data = ((*data & 0xff) * 27 * 25) >> 9;

If the variable state is OCV_MODE_SLEEP, the function measure_vbatt() is
called with the argument &ocv,
and the corresponding parameter is data. Thus *data is uninitialized but
it is used at line 201.

I am not quite sure whether this possible uninitialized-variable access
is real and how to fix it if it is real.
Any feedback would be appreciated, thanks!

Reported-by: TOTE Robot <[email protected]>

Best wishes,
Tuo Li


2021-08-05 23:46:07

by Sebastian Reichel

[permalink] [raw]
Subject: Re: [BUG] power: supply: 88pm860x_battery: possible uninitialized-variable access in measure_vbatt()

[adding Jett Zhou to Cc who introduced the driver]

Hi,

On Wed, Jul 28, 2021 at 06:24:12PM +0800, Li Tuo wrote:
> Our static analysis tool finds a possible uninitialized-variable access in
> the 88pm860x_battery driver in Linux 5.14.0-rc3:
>
> In calc_soc():
> 369:??? int ocv;
> 376:??? switch (state) {
> 380:??? case OCV_MODE_SLEEP:
> 381:??????? ret = measure_vbatt(info, OCV_MODE_SLEEP, &ocv);
>
> In measure_vbatt(struct pm860x_battery_info *info, int state, int *data)
> 176:??? switch (state) {
> 184:??? case OCV_MODE_SLEEP:
> 201:??????? *data = ((*data & 0xff) * 27 * 25) >> 9;
>
> If the variable state is OCV_MODE_SLEEP, the function measure_vbatt() is
> called with the argument &ocv, and the corresponding parameter is data.
> Thus *data is uninitialized but it is used at line 201.
>
> I am not quite sure whether this possible uninitialized-variable access is
> real and how to fix it if it is real.
> Any feedback would be appreciated, thanks!
>
> Reported-by: TOTE Robot <[email protected]>

I suppose the code is suppose to look like this:

201:??????? *data = ((ret & 0xff) * 27 * 25) >> 9;

Considering quite some code is spent before to setup ret, which is
never used. I don't have the device (nor datasheets) though. Considering
the driver has only seen trivial cleanups over the last 9 years, maybe
it can just be removed?

-- Sebastian


Attachments:
(No filename) (1.38 kB)
signature.asc (849.00 B)
Download all attachments

2021-08-06 11:31:04

by Tuo Li

[permalink] [raw]
Subject: Re: [BUG] power: supply: 88pm860x_battery: possible uninitialized-variable access in measure_vbatt()

Thanks for your feedback, and any further feedback about this problem
would be appreciated.

Best wishes,
Tuo Li

On 2021/8/6 1:49, Sebastian Reichel wrote:
> [adding Jett Zhou to Cc who introduced the driver]
>
> Hi,
>
> On Wed, Jul 28, 2021 at 06:24:12PM +0800, Li Tuo wrote:
>> Our static analysis tool finds a possible uninitialized-variable access in
>> the 88pm860x_battery driver in Linux 5.14.0-rc3:
>>
>> In calc_soc():
>> 369:??? int ocv;
>> 376:??? switch (state) {
>> 380:??? case OCV_MODE_SLEEP:
>> 381:??????? ret = measure_vbatt(info, OCV_MODE_SLEEP, &ocv);
>>
>> In measure_vbatt(struct pm860x_battery_info *info, int state, int *data)
>> 176:??? switch (state) {
>> 184:??? case OCV_MODE_SLEEP:
>> 201:??????? *data = ((*data & 0xff) * 27 * 25) >> 9;
>>
>> If the variable state is OCV_MODE_SLEEP, the function measure_vbatt() is
>> called with the argument &ocv, and the corresponding parameter is data.
>> Thus *data is uninitialized but it is used at line 201.
>>
>> I am not quite sure whether this possible uninitialized-variable access is
>> real and how to fix it if it is real.
>> Any feedback would be appreciated, thanks!
>>
>> Reported-by: TOTE Robot <[email protected]>
> I suppose the code is suppose to look like this:
>
> 201:??????? *data = ((ret & 0xff) * 27 * 25) >> 9;
>
> Considering quite some code is spent before to setup ret, which is
> never used. I don't have the device (nor datasheets) though. Considering
> the driver has only seen trivial cleanups over the last 9 years, maybe
> it can just be removed?
>
> -- Sebastian