2018-11-06 08:46:54

by Xulin Sun

[permalink] [raw]
Subject: [PATCH] rtc: pcf2127: fix a kmemleak caused in pcf2127_i2c_gather_write

pcf2127_i2c_gather_write() allocates memory as local variable
for i2c_master_send(), after finishing the master transfer,
the allocated memory should be freed. The kmemleak is reported:

unreferenced object 0xffff80231e7dba80 (size 64):
comm "hwclock", pid 27762, jiffies 4296880075 (age 356.944s)
hex dump (first 32 bytes):
03 00 12 03 19 02 11 13 00 80 98 18 00 00 ff ff ................
00 50 00 00 00 00 00 00 02 00 00 00 00 00 00 00 .P..............
backtrace:
[<ffff000008221398>] create_object+0xf8/0x278
[<ffff000008a96264>] kmemleak_alloc+0x74/0xa0
[<ffff00000821070c>] __kmalloc+0x1ac/0x348
[<ffff0000087ed1dc>] pcf2127_i2c_gather_write+0x54/0xf8
[<ffff0000085fd9d4>] _regmap_raw_write+0x464/0x850
[<ffff0000085fe3f4>] regmap_bulk_write+0x1a4/0x348
[<ffff0000087ed32c>] pcf2127_rtc_set_time+0xac/0xe8
[<ffff0000087eaad8>] rtc_set_time+0x80/0x138
[<ffff0000087ebfb0>] rtc_dev_ioctl+0x398/0x610
[<ffff00000823f2c0>] do_vfs_ioctl+0xb0/0x848
[<ffff00000823fae4>] SyS_ioctl+0x8c/0xa8
[<ffff000008083ac0>] el0_svc_naked+0x34/0x38
[<ffffffffffffffff>] 0xffffffffffffffff

Signed-off-by: Xulin Sun <[email protected]>
---
drivers/rtc/rtc-pcf2127.c | 3 +++
1 file changed, 3 insertions(+)

diff --git a/drivers/rtc/rtc-pcf2127.c b/drivers/rtc/rtc-pcf2127.c
index e83be18..598e56e 100644
--- a/drivers/rtc/rtc-pcf2127.c
+++ b/drivers/rtc/rtc-pcf2127.c
@@ -248,6 +248,9 @@ static int pcf2127_i2c_gather_write(void *context,
memcpy(buf + 1, val, val_size);

ret = i2c_master_send(client, buf, val_size + 1);
+
+ kfree(buf);
+
if (ret != val_size + 1)
return ret < 0 ? ret : -EIO;

--
2.7.4



2018-11-07 16:26:27

by Alexandre Belloni

[permalink] [raw]
Subject: Re: [PATCH] rtc: pcf2127: fix a kmemleak caused in pcf2127_i2c_gather_write

On 06/11/2018 16:42:19+0800, Xulin Sun wrote:
> pcf2127_i2c_gather_write() allocates memory as local variable
> for i2c_master_send(), after finishing the master transfer,
> the allocated memory should be freed. The kmemleak is reported:
>
> unreferenced object 0xffff80231e7dba80 (size 64):
> comm "hwclock", pid 27762, jiffies 4296880075 (age 356.944s)
> hex dump (first 32 bytes):
> 03 00 12 03 19 02 11 13 00 80 98 18 00 00 ff ff ................
> 00 50 00 00 00 00 00 00 02 00 00 00 00 00 00 00 .P..............
> backtrace:
> [<ffff000008221398>] create_object+0xf8/0x278
> [<ffff000008a96264>] kmemleak_alloc+0x74/0xa0
> [<ffff00000821070c>] __kmalloc+0x1ac/0x348
> [<ffff0000087ed1dc>] pcf2127_i2c_gather_write+0x54/0xf8
> [<ffff0000085fd9d4>] _regmap_raw_write+0x464/0x850
> [<ffff0000085fe3f4>] regmap_bulk_write+0x1a4/0x348
> [<ffff0000087ed32c>] pcf2127_rtc_set_time+0xac/0xe8
> [<ffff0000087eaad8>] rtc_set_time+0x80/0x138
> [<ffff0000087ebfb0>] rtc_dev_ioctl+0x398/0x610
> [<ffff00000823f2c0>] do_vfs_ioctl+0xb0/0x848
> [<ffff00000823fae4>] SyS_ioctl+0x8c/0xa8
> [<ffff000008083ac0>] el0_svc_naked+0x34/0x38
> [<ffffffffffffffff>] 0xffffffffffffffff
>
> Signed-off-by: Xulin Sun <[email protected]>
> ---
> drivers/rtc/rtc-pcf2127.c | 3 +++
> 1 file changed, 3 insertions(+)
>

Applied, thanks.

--
Alexandre Belloni, Bootlin
Embedded Linux and Kernel engineering
https://bootlin.com