2018-07-17 07:26:38

by Jiang Biao

[permalink] [raw]
Subject: [PATCH v2 1/2] x86/pti: check the return value of pti_user_pagetable_walk_p4d

pti_user_pagetable_walk_p4d() may return NULL, we should check the
return value to avoid NULL pointer dereference. And add warning
for fail allocation where NULL returned.

Signed-off-by: Jiang Biao <[email protected]>
---
arch/x86/mm/pti.c | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/arch/x86/mm/pti.c b/arch/x86/mm/pti.c
index 4d418e7..7c402e9 100644
--- a/arch/x86/mm/pti.c
+++ b/arch/x86/mm/pti.c
@@ -176,7 +176,7 @@ static p4d_t *pti_user_pagetable_walk_p4d(unsigned long address)

if (pgd_none(*pgd)) {
unsigned long new_p4d_page = __get_free_page(gfp);
- if (!new_p4d_page)
+ if (WARN_ON(!new_p4d_page))
return NULL;

set_pgd(pgd, __pgd(_KERNPG_TABLE | __pa(new_p4d_page)));
@@ -195,8 +195,10 @@ static p4d_t *pti_user_pagetable_walk_p4d(unsigned long address)
static pmd_t *pti_user_pagetable_walk_pmd(unsigned long address)
{
gfp_t gfp = (GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO);
- p4d_t *p4d = pti_user_pagetable_walk_p4d(address);
pud_t *pud;
+ p4d_t *p4d = pti_user_pagetable_walk_p4d(address);
+ if (!p4d)
+ return NULL;

BUILD_BUG_ON(p4d_large(*p4d) != 0);
if (p4d_none(*p4d)) {
@@ -354,6 +356,8 @@ static void __init pti_clone_p4d(unsigned long addr)
pgd_t *kernel_pgd;

user_p4d = pti_user_pagetable_walk_p4d(addr);
+ if (!user_p4d)
+ return;
kernel_pgd = pgd_offset_k(addr);
kernel_p4d = p4d_offset(kernel_pgd, addr);
*user_p4d = *kernel_p4d;
--
2.7.4



2018-07-17 07:26:33

by Jiang Biao

[permalink] [raw]
Subject: [PATCH v2 2/2] x86/pti: check the return value of pti_user_pagetable_walk_pmd

Check the return value of pti_user_pagetable_walk_pmd() to avoid
NULL pointer dereference. And add warning for fail allocation.

Signed-off-by: Jiang Biao <[email protected]>
---
arch/x86/mm/pti.c | 8 +++++---
1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/arch/x86/mm/pti.c b/arch/x86/mm/pti.c
index 7c402e9..3dba6a7 100644
--- a/arch/x86/mm/pti.c
+++ b/arch/x86/mm/pti.c
@@ -203,7 +203,7 @@ static pmd_t *pti_user_pagetable_walk_pmd(unsigned long address)
BUILD_BUG_ON(p4d_large(*p4d) != 0);
if (p4d_none(*p4d)) {
unsigned long new_pud_page = __get_free_page(gfp);
- if (!new_pud_page)
+ if (WARN_ON(!new_pud_page))
return NULL;

set_p4d(p4d, __p4d(_KERNPG_TABLE | __pa(new_pud_page)));
@@ -217,7 +217,7 @@ static pmd_t *pti_user_pagetable_walk_pmd(unsigned long address)
}
if (pud_none(*pud)) {
unsigned long new_pmd_page = __get_free_page(gfp);
- if (!new_pmd_page)
+ if (WARN_ON(!new_pmd_page))
return NULL;

set_pud(pud, __pud(_KERNPG_TABLE | __pa(new_pmd_page)));
@@ -239,8 +239,10 @@ static pmd_t *pti_user_pagetable_walk_pmd(unsigned long address)
static __init pte_t *pti_user_pagetable_walk_pte(unsigned long address)
{
gfp_t gfp = (GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO);
- pmd_t *pmd = pti_user_pagetable_walk_pmd(address);
pte_t *pte;
+ pmd_t *pmd = pti_user_pagetable_walk_pmd(address);
+ if (!pmd)
+ return NULL;

/* We can't do anything sensible if we hit a large mapping. */
if (pmd_large(*pmd)) {
--
2.7.4


2018-07-17 18:04:31

by Dave Hansen

[permalink] [raw]
Subject: Re: [PATCH v2 2/2] x86/pti: check the return value of pti_user_pagetable_walk_pmd

On 07/17/2018 12:23 AM, Jiang Biao wrote:
> Check the return value of pti_user_pagetable_walk_pmd() to avoid
> NULL pointer dereference. And add warning for fail allocation.

For both of these:

Acked-by: Dave Hansen <[email protected]>

It's minor, but if you redo these, I'd appreciate a slightly different
form. Instead of:

> @@ -239,8 +239,10 @@ static pmd_t *pti_user_pagetable_walk_pmd(unsigned long address)
> static __init pte_t *pti_user_pagetable_walk_pte(unsigned long address)
> {
> gfp_t gfp = (GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO);
> - pmd_t *pmd = pti_user_pagetable_walk_pmd(address);
> pte_t *pte;
> + pmd_t *pmd = pti_user_pagetable_walk_pmd(address);
> + if (!pmd)
> + return NULL;

I'd much rather see separation of code -- especially _important_ code
like an allocation -- from local variable definitions. Like this:

gfp_t gfp = (GFP_KERNEL | __GFP_NOTRACK | __GFP_ZERO);
pmd_t *pmd;
pte_t *pte;

pmd = pti_user_pagetable_walk_pmd(address);
if (!pmd)
return NULL;

That clearly separtes the variables from the _code_ and also nicely
pairs the action with the check for that action being successful.