Skip to contentSkip to conversation

lib/mpi: add the return value check of kcalloc

PATCH In review Zizhuang Deng 2 participants 3 messages Dec 30, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib/mpi: add the return value check of kcalloc

Add the return value check of kcalloc to avoid potential NULL ptr dereference.

---
 lib/mpi/mpi-mod.c | 4 ++++
 1 file changed, 4 insertions(+)
lib/mpi/mpi-mod.c +4 -0
index 47bc59edd4ff..6fd6900f0798 100644
@@ -41,6 +41,10 @@ mpi_barrett_t mpi_barrett_init(MPI m, int copy)
 	mpi_normalize(m);
 	ctx = kcalloc(1, sizeof(*ctx), GFP_KERNEL);
 
+	if (!ctx) {
+		return NULL;
+	}
+
 	if (copy) {
 		ctx->m = mpi_copy(m);
 		ctx->m_copied = 1;
Signature · 1 line
2.25.1
#1 lore raw

Hi Zizhuang,

Quoted 13 lines · Zizhuang Deng

Add the return value check of kcalloc to avoid potential NULL ptr dereference.

---
  lib/mpi/mpi-mod.c | 4 ++++
  1 file changed, 4 insertions(+)
lib/mpi/mpi-mod.c +0 -0
index 47bc59edd4ff..6fd6900f0798 100644

lib/mpi/mpi-mod.c mpi_barrett_t mpi_barrett_init(MPI m, int copy) · line 43

Quoted 3 lines
lib/mpi/mpi-mod.c +0 -0
@@ -41,6 +41,10 @@ mpi_barrett_t mpi_barrett_init(MPI m, int copy)
  	mpi_normalize(m);
  	ctx = kcalloc(1, sizeof(*ctx), GFP_KERNEL);
 

Remove this empty line.

lib/mpi/mpi-mod.c mpi_barrett_t mpi_barrett_init(MPI m, int copy) · line 50

Quoted 7 lines
lib/mpi/mpi-mod.c +4 -0
+	if (!ctx) {
+		return NULL;
+	}
+
  	if (copy) {
  		ctx->m = mpi_copy(m);
  		ctx->m_copied = 1;

Thanks for pointing it out. Please send a copy to linux-crypto and Herbert Xu.

Best regards,
Tianjia

#2 lore raw
In reply to Zizhuang Deng
Quoted 13 lines · Zizhuang Deng

Add the return value check of kcalloc to avoid potential NULL ptr dereference.

---
  lib/mpi/mpi-mod.c | 4 ++++
  1 file changed, 4 insertions(+)
lib/mpi/mpi-mod.c +0 -0
index 47bc59edd4ff..6fd6900f0798 100644

lib/mpi/mpi-mod.c mpi_barrett_t mpi_barrett_init(MPI m, int copy) · line 47

Quoted 7 lines
lib/mpi/mpi-mod.c +4 -0
@@ -41,6 +41,10 @@ mpi_barrett_t mpi_barrett_init(MPI m, int copy)
  	mpi_normalize(m);
  	ctx = kcalloc(1, sizeof(*ctx), GFP_KERNEL);
 
+	if (!ctx) {
+		return NULL;
+	}
+

Delete the pair of curly braces, and just keep:

	if (!ctx)
		return NULL;

Thanks.

Quoted 3 lines
lib/mpi/mpi-mod.c +0 -0
  	if (copy) {
  		ctx->m = mpi_copy(m);
  		ctx->m_copied = 1;