[PATCH] fmc: fix to avoid NULL pointer dereference
From: Aditya Pakki
Date: Tue Mar 19 2019 - 15:29:09 EST
kmemdup can fail to allocate memory and result in a NULL pointer
dereference. The patches avoids such a scenario.
Signed-off-by: Aditya Pakki <pakki001@xxxxxxx>
---
drivers/fmc/fmc-fakedev.c | 8 ++++++++
1 file changed, 8 insertions(+)
diff --git a/drivers/fmc/fmc-fakedev.c b/drivers/fmc/fmc-fakedev.c
index 941d0930969a..d634c901b4f3 100644
--- a/drivers/fmc/fmc-fakedev.c
+++ b/drivers/fmc/fmc-fakedev.c
@@ -282,6 +282,8 @@ static struct ff_dev *ff_dev_create(void)
for (i = 0; i < ff_nr_dev; i++) {
fmc = kmemdup(&ff_template_fmc, sizeof(ff_template_fmc),
GFP_KERNEL);
+ if (!fmc)
+ goto rollback;
fmc->hwdev = &ff->dev;
fmc->carrier_data = ff;
fmc->nr_slots = ff_nr_dev;
@@ -294,6 +296,12 @@ static struct ff_dev *ff_dev_create(void)
ff_template_fmc.device_id++;
}
return ff;
+
+rollback:
+ for (i--; i >= 0; i--)
+ kfree(ff->fmc[i]);
+ kfree(ff);
+ return ERR_PTR(-ENOMEM);
}
/* init and exit */
--
2.17.1