2021-12-30 02:56:47

by Zizhuang Deng

[permalink] [raw]
Subject: [PATCH] lib/mpi: add the return value check of kcalloc

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

Signed-off-by: Zizhuang Deng <[email protected]>
---
lib/mpi/mpi-mod.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/lib/mpi/mpi-mod.c b/lib/mpi/mpi-mod.c
index 47bc59edd4ff..6fd6900f0798 100644
--- a/lib/mpi/mpi-mod.c
+++ b/lib/mpi/mpi-mod.c
@@ -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;
--
2.25.1



2021-12-30 06:42:00

by Tianjia Zhang

[permalink] [raw]
Subject: Re: [PATCH] lib/mpi: add the return value check of kcalloc

Hi Zizhuang,

On 12/30/21 10:51 AM, Zizhuang Deng wrote:
> Add the return value check of kcalloc to avoid potential
> NULL ptr dereference.
>
> Signed-off-by: Zizhuang Deng <[email protected]>
> ---
> lib/mpi/mpi-mod.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/lib/mpi/mpi-mod.c b/lib/mpi/mpi-mod.c
> index 47bc59edd4ff..6fd6900f0798 100644
> --- a/lib/mpi/mpi-mod.c
> +++ b/lib/mpi/mpi-mod.c
> @@ -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.

> + 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

2021-12-30 06:45:52

by Tianjia Zhang

[permalink] [raw]
Subject: Re: [PATCH] lib/mpi: add the return value check of kcalloc



On 12/30/21 10:51 AM, Zizhuang Deng wrote:
> Add the return value check of kcalloc to avoid potential
> NULL ptr dereference.
>
> Signed-off-by: Zizhuang Deng <[email protected]>
> ---
> lib/mpi/mpi-mod.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/lib/mpi/mpi-mod.c b/lib/mpi/mpi-mod.c
> index 47bc59edd4ff..6fd6900f0798 100644
> --- a/lib/mpi/mpi-mod.c
> +++ b/lib/mpi/mpi-mod.c
> @@ -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.

> if (copy) {
> ctx->m = mpi_copy(m);
> ctx->m_copied = 1;