2019-01-25 17:39:16

by Matthew Wilcox

[permalink] [raw]
Subject: [PATCH] mm: Prevent mapping slab pages to userspace

It's never appropriate to map a page allocated by SLAB into userspace.
A buggy device driver might try this, or an attacker might be able to
find a way to make it happen.

Signed-off-by: Matthew Wilcox <[email protected]>
---
mm/memory.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/mm/memory.c b/mm/memory.c
index e11ca9dd823f..ce8c90b752be 100644
--- a/mm/memory.c
+++ b/mm/memory.c
@@ -1451,7 +1451,7 @@ static int insert_page(struct vm_area_struct *vma, unsigned long addr,
spinlock_t *ptl;

retval = -EINVAL;
- if (PageAnon(page))
+ if (PageAnon(page) || PageSlab(page))
goto out;
retval = -ENOMEM;
flush_dcache_page(page);
--
2.20.1



2019-01-25 18:45:54

by Kees Cook

[permalink] [raw]
Subject: Re: [PATCH] mm: Prevent mapping slab pages to userspace

On Sat, Jan 26, 2019 at 6:38 AM Matthew Wilcox <[email protected]> wrote:
>
> It's never appropriate to map a page allocated by SLAB into userspace.
> A buggy device driver might try this, or an attacker might be able to
> find a way to make it happen.
>
> Signed-off-by: Matthew Wilcox <[email protected]>
> ---
> mm/memory.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/mm/memory.c b/mm/memory.c
> index e11ca9dd823f..ce8c90b752be 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -1451,7 +1451,7 @@ static int insert_page(struct vm_area_struct *vma, unsigned long addr,
> spinlock_t *ptl;
>
> retval = -EINVAL;
> - if (PageAnon(page))
> + if (PageAnon(page) || PageSlab(page))

Are there other types that should not get mapped? (Or better yet, is
there a whitelist of those that are okay to be mapped?)

Either way, this sounds good. :)

Reviewed-by: Kees Cook <[email protected]>

-Kees

> goto out;
> retval = -ENOMEM;
> flush_dcache_page(page);
> --
> 2.20.1
>


--
Kees Cook

2019-01-25 19:30:42

by Matthew Wilcox

[permalink] [raw]
Subject: Re: [PATCH] mm: Prevent mapping slab pages to userspace

On Sat, Jan 26, 2019 at 07:44:40AM +1300, Kees Cook wrote:
> > - if (PageAnon(page))
> > + if (PageAnon(page) || PageSlab(page))
>
> Are there other types that should not get mapped? (Or better yet, is
> there a whitelist of those that are okay to be mapped?)

Funny you should ask; I think the next patch in this series looks like this:

- if (PageAnon(page) || PageSlab(page))
+ if (PageAnon(page) || PageSlab(page) || page_has_type(page))

but let's see if there's something I've overlooked with this patch.

2019-01-28 18:21:37

by Andrew Morton

[permalink] [raw]
Subject: Re: [PATCH] mm: Prevent mapping slab pages to userspace

On Fri, 25 Jan 2019 09:38:27 -0800 Matthew Wilcox <[email protected]> wrote:

> It's never appropriate to map a page allocated by SLAB into userspace.
> A buggy device driver might try this, or an attacker might be able to
> find a way to make it happen.

It wouldn't surprise me if someone somewhere is doing this. Rather
than mysteriously breaking their code, how about we emit a warning and
still permit it to proceed, for a while?



2019-01-28 19:02:21

by Kees Cook

[permalink] [raw]
Subject: Re: [PATCH] mm: Prevent mapping slab pages to userspace

On Tue, Jan 29, 2019 at 7:21 AM Andrew Morton <[email protected]> wrote:
>
> On Fri, 25 Jan 2019 09:38:27 -0800 Matthew Wilcox <[email protected]> wrote:
>
> > It's never appropriate to map a page allocated by SLAB into userspace.
> > A buggy device driver might try this, or an attacker might be able to
> > find a way to make it happen.
>
> It wouldn't surprise me if someone somewhere is doing this. Rather
> than mysteriously breaking their code, how about we emit a warning and
> still permit it to proceed, for a while?

It seems like a fatal condition to me? There's nothing to check that
such a page wouldn't get freed by the slab while still mapped to
userspace, right?

But I'll take warning over not checking. :)

--
Kees Cook

Subject: Re: [PATCH] mm: Prevent mapping slab pages to userspace

On Tue, 29 Jan 2019, Kees Cook wrote:

> It seems like a fatal condition to me? There's nothing to check that
> such a page wouldn't get freed by the slab while still mapped to
> userspace, right?

Lets just fail the code. Currently this may work with SLUB. But SLAB and
SLOB overlay fields with mapcount. So you would have a corrupted page
struct if you mapped a slab page to user space.

2019-01-31 00:38:20

by Michael Ellerman

[permalink] [raw]
Subject: Re: [PATCH] mm: Prevent mapping slab pages to userspace

Matthew Wilcox <[email protected]> writes:

> It's never appropriate to map a page allocated by SLAB into userspace.
> A buggy device driver might try this, or an attacker might be able to
> find a way to make it happen.
>
> Signed-off-by: Matthew Wilcox <[email protected]>
> ---
> mm/memory.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/mm/memory.c b/mm/memory.c
> index e11ca9dd823f..ce8c90b752be 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -1451,7 +1451,7 @@ static int insert_page(struct vm_area_struct *vma, unsigned long addr,
> spinlock_t *ptl;
>
> retval = -EINVAL;
> - if (PageAnon(page))
> + if (PageAnon(page) || PageSlab(page))
> goto out;
> retval = -ENOMEM;
> flush_dcache_page(page);


Thanks for turning this into an actual patch.

cheers

2019-01-31 06:06:05

by Pekka Enberg

[permalink] [raw]
Subject: Re: [PATCH] mm: Prevent mapping slab pages to userspace

On 25/01/2019 19.38, Matthew Wilcox wrote:
> It's never appropriate to map a page allocated by SLAB into userspace.
> A buggy device driver might try this, or an attacker might be able to
> find a way to make it happen.
>
> Signed-off-by: Matthew Wilcox <[email protected]>

Acked-by: Pekka Enberg <[email protected]>

A WARN_ON_ONCE() would be nice here to let those buggy drivers know that
they will no longer work.

> ---
> mm/memory.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/mm/memory.c b/mm/memory.c
> index e11ca9dd823f..ce8c90b752be 100644
> --- a/mm/memory.c
> +++ b/mm/memory.c
> @@ -1451,7 +1451,7 @@ static int insert_page(struct vm_area_struct *vma, unsigned long addr,
> spinlock_t *ptl;
>
> retval = -EINVAL;
> - if (PageAnon(page))
> + if (PageAnon(page) || PageSlab(page))
> goto out;
> retval = -ENOMEM;
> flush_dcache_page(page);
>