From c3c8c051df3ee5042dd91593593a8b0e008f4c85 Mon Sep 17 00:00:00 2001 From: Dan Carpenter Date: Fri, 14 May 2021 17:27:15 +0300 Subject: [PATCH] mtd: core: Potential NULL dereference in mtd_otp_size() If kmalloc() fails then it could lead to a NULL dereference. Check and return -ENOMEM on error. Fixes: 4b361cfa8624 ("mtd: core: add OTP nvmem provider support") Signed-off-by: Dan Carpenter Reviewed-by: Michael Walle Signed-off-by: Miquel Raynal Link: https://lore.kernel.org/linux-mtd/YJ6Iw3iNvGycAWV6@mwanda --- drivers/mtd/mtdcore.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c index ffa46ccea0cf..ce514305f8f7 100644 --- a/drivers/mtd/mtdcore.c +++ b/drivers/mtd/mtdcore.c @@ -779,12 +779,16 @@ static void mtd_set_dev_defaults(struct mtd_info *mtd) static ssize_t mtd_otp_size(struct mtd_info *mtd, bool is_user) { - struct otp_info *info = kmalloc(PAGE_SIZE, GFP_KERNEL); + struct otp_info *info; ssize_t size = 0; unsigned int i; size_t retlen; int ret; + info = kmalloc(PAGE_SIZE, GFP_KERNEL); + if (!info) + return -ENOMEM; + if (is_user) ret = mtd_get_user_prot_info(mtd, PAGE_SIZE, &retlen, info); else