Commit bba3d2da authored by Alexandre Belloni's avatar Alexandre Belloni

rtc: nvmem: fix possible use after free

In cas of probe failure, devres may free the memory allocated for
rtc->nvram before devm_rtc_release_device() is called. This leads to
rtc_nvram_unregister using it after being freed which may lead to a crash.

This has been shown to happen after commit 461e557b ("rtc: nvmem: use
devm_nvmem_register()")
Reported-by: default avatarkernel test robot <lkp@intel.com>
Signed-off-by: default avatarAlexandre Belloni <alexandre.belloni@bootlin.com>
parent e01b5781
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include <linux/types.h> #include <linux/types.h>
#include <linux/nvmem-consumer.h> #include <linux/nvmem-consumer.h>
#include <linux/rtc.h> #include <linux/rtc.h>
#include <linux/slab.h>
#include <linux/sysfs.h> #include <linux/sysfs.h>
/* /*
...@@ -45,9 +46,7 @@ static int rtc_nvram_register(struct rtc_device *rtc, ...@@ -45,9 +46,7 @@ static int rtc_nvram_register(struct rtc_device *rtc,
{ {
int err; int err;
rtc->nvram = devm_kzalloc(rtc->dev.parent, rtc->nvram = kzalloc(sizeof(struct bin_attribute), GFP_KERNEL);
sizeof(struct bin_attribute),
GFP_KERNEL);
if (!rtc->nvram) if (!rtc->nvram)
return -ENOMEM; return -ENOMEM;
...@@ -64,7 +63,7 @@ static int rtc_nvram_register(struct rtc_device *rtc, ...@@ -64,7 +63,7 @@ static int rtc_nvram_register(struct rtc_device *rtc,
err = sysfs_create_bin_file(&rtc->dev.parent->kobj, err = sysfs_create_bin_file(&rtc->dev.parent->kobj,
rtc->nvram); rtc->nvram);
if (err) { if (err) {
devm_kfree(rtc->dev.parent, rtc->nvram); kfree(rtc->nvram);
rtc->nvram = NULL; rtc->nvram = NULL;
} }
...@@ -74,6 +73,8 @@ static int rtc_nvram_register(struct rtc_device *rtc, ...@@ -74,6 +73,8 @@ static int rtc_nvram_register(struct rtc_device *rtc,
static void rtc_nvram_unregister(struct rtc_device *rtc) static void rtc_nvram_unregister(struct rtc_device *rtc)
{ {
sysfs_remove_bin_file(&rtc->dev.parent->kobj, rtc->nvram); sysfs_remove_bin_file(&rtc->dev.parent->kobj, rtc->nvram);
kfree(rtc->nvram);
rtc->nvram = NULL;
} }
/* /*
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment