2022-04-29 15:31:56

by Miaohe Lin

[permalink] [raw]
Subject: [PATCH 2/9] mm/z3fold: fix possible null pointer dereferencing

alloc_slots could fail to allocate memory under heavy memory pressure. So
we should check zhdr->slots against NULL to avoid future null pointer
dereferencing.

Fixes: fc5488651c7d ("z3fold: simplify freeing slots")
Signed-off-by: Miaohe Lin <[email protected]>
---
mm/z3fold.c | 12 +++++++++++-
1 file changed, 11 insertions(+), 1 deletion(-)

diff --git a/mm/z3fold.c b/mm/z3fold.c
index c2260f5a5885..5d8c21f2bc59 100644
--- a/mm/z3fold.c
+++ b/mm/z3fold.c
@@ -940,9 +940,19 @@ static inline struct z3fold_header *__z3fold_alloc(struct z3fold_pool *pool,
}
}

- if (zhdr && !zhdr->slots)
+ if (zhdr && !zhdr->slots) {
zhdr->slots = alloc_slots(pool, GFP_ATOMIC);
+ if (!zhdr->slots)
+ goto out_fail;
+ }
return zhdr;
+
+out_fail:
+ if (!kref_put(&zhdr->refcount, release_z3fold_page_locked)) {
+ add_to_unbuddied(pool, zhdr);
+ z3fold_page_unlock(zhdr);
+ }
+ return NULL;
}

/*
--
2.23.0


2022-05-19 12:54:00

by Vitaly Wool

[permalink] [raw]
Subject: Re: [PATCH 2/9] mm/z3fold: fix possible null pointer dereferencing

On Fri, Apr 29, 2022 at 8:40 AM Miaohe Lin <[email protected]> wrote:
>
> alloc_slots could fail to allocate memory under heavy memory pressure. So
> we should check zhdr->slots against NULL to avoid future null pointer
> dereferencing.
>
> Fixes: fc5488651c7d ("z3fold: simplify freeing slots")
> Signed-off-by: Miaohe Lin <[email protected]>
> ---
> mm/z3fold.c | 12 +++++++++++-
> 1 file changed, 11 insertions(+), 1 deletion(-)
>
> diff --git a/mm/z3fold.c b/mm/z3fold.c
> index c2260f5a5885..5d8c21f2bc59 100644
> --- a/mm/z3fold.c
> +++ b/mm/z3fold.c
> @@ -940,9 +940,19 @@ static inline struct z3fold_header *__z3fold_alloc(struct z3fold_pool *pool,
> }
> }
>
> - if (zhdr && !zhdr->slots)
> + if (zhdr && !zhdr->slots) {
> zhdr->slots = alloc_slots(pool, GFP_ATOMIC);
> + if (!zhdr->slots)
> + goto out_fail;
> + }
> return zhdr;
> +
> +out_fail:
> + if (!kref_put(&zhdr->refcount, release_z3fold_page_locked)) {
> + add_to_unbuddied(pool, zhdr);
> + z3fold_page_unlock(zhdr);
> + }
> + return NULL;
> }

Reviewed-by: Vitaly Wool <[email protected]>
> /*
> --
> 2.23.0
>