* [PATCH -next v2] mm/page_alloc: fix a false memory corruption
@ 2019-06-20 20:46 Qian Cai
2019-06-21 1:01 ` Kees Cook
0 siblings, 1 reply; 7+ messages in thread
From: Qian Cai @ 2019-06-20 20:46 UTC (permalink / raw)
To: akpm; +Cc: glider, keescook, linux-mm, linux-kernel, Qian Cai
The linux-next commit "mm: security: introduce init_on_alloc=1 and
init_on_free=1 boot options" [1] introduced a false positive when
init_on_free=1 and page_poison=on, due to the page_poison expects the
pattern 0xaa when allocating pages which were overwritten by
init_on_free=1 with 0.
Fix it by switching the order between kernel_init_free_pages() and
kernel_poison_pages() in free_pages_prepare().
[1] https://patchwork.kernel.org/patch/10999465/
Signed-off-by: Qian Cai <cai@lca.pw>
---
v2: After further debugging, the issue after switching order is likely a
separate issue as clear_page() should not cause issues with future
accesses.
mm/page_alloc.c | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/mm/page_alloc.c b/mm/page_alloc.c
index 54dacf35d200..32bbd30c5f85 100644
--- a/mm/page_alloc.c
+++ b/mm/page_alloc.c
@@ -1172,9 +1172,10 @@ static __always_inline bool free_pages_prepare(struct page *page,
PAGE_SIZE << order);
}
arch_free_page(page, order);
- kernel_poison_pages(page, 1 << order, 0);
if (want_init_on_free())
kernel_init_free_pages(page, 1 << order);
+
+ kernel_poison_pages(page, 1 << order, 0);
if (debug_pagealloc_enabled())
kernel_map_pages(page, 1 << order, 0);
--
1.8.3.1
^ permalink raw reply [flat|nested] 7+ messages in thread* Re: [PATCH -next v2] mm/page_alloc: fix a false memory corruption 2019-06-20 20:46 [PATCH -next v2] mm/page_alloc: fix a false memory corruption Qian Cai @ 2019-06-21 1:01 ` Kees Cook 2019-06-21 10:39 ` Alexander Potapenko 0 siblings, 1 reply; 7+ messages in thread From: Kees Cook @ 2019-06-21 1:01 UTC (permalink / raw) To: Qian Cai; +Cc: akpm, glider, linux-mm, linux-kernel On Thu, Jun 20, 2019 at 04:46:06PM -0400, Qian Cai wrote: > The linux-next commit "mm: security: introduce init_on_alloc=1 and > init_on_free=1 boot options" [1] introduced a false positive when > init_on_free=1 and page_poison=on, due to the page_poison expects the > pattern 0xaa when allocating pages which were overwritten by > init_on_free=1 with 0. > > Fix it by switching the order between kernel_init_free_pages() and > kernel_poison_pages() in free_pages_prepare(). Cool; this seems like the right approach. Alexander, what do you think? Reviewed-by: Kees Cook <keescook@chromium.org> -Kees > > [1] https://patchwork.kernel.org/patch/10999465/ > > Signed-off-by: Qian Cai <cai@lca.pw> > --- > > v2: After further debugging, the issue after switching order is likely a > separate issue as clear_page() should not cause issues with future > accesses. > > mm/page_alloc.c | 3 ++- > 1 file changed, 2 insertions(+), 1 deletion(-) > > diff --git a/mm/page_alloc.c b/mm/page_alloc.c > index 54dacf35d200..32bbd30c5f85 100644 > --- a/mm/page_alloc.c > +++ b/mm/page_alloc.c > @@ -1172,9 +1172,10 @@ static __always_inline bool free_pages_prepare(struct page *page, > PAGE_SIZE << order); > } > arch_free_page(page, order); > - kernel_poison_pages(page, 1 << order, 0); > if (want_init_on_free()) > kernel_init_free_pages(page, 1 << order); > + > + kernel_poison_pages(page, 1 << order, 0); > if (debug_pagealloc_enabled()) > kernel_map_pages(page, 1 << order, 0); > > -- > 1.8.3.1 > -- Kees Cook ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH -next v2] mm/page_alloc: fix a false memory corruption 2019-06-21 1:01 ` Kees Cook @ 2019-06-21 10:39 ` Alexander Potapenko 2019-06-21 12:26 ` Qian Cai 0 siblings, 1 reply; 7+ messages in thread From: Alexander Potapenko @ 2019-06-21 10:39 UTC (permalink / raw) To: Kees Cook; +Cc: Qian Cai, Andrew Morton, Linux Memory Management List, LKML On Fri, Jun 21, 2019 at 3:01 AM Kees Cook <keescook@chromium.org> wrote: > > On Thu, Jun 20, 2019 at 04:46:06PM -0400, Qian Cai wrote: > > The linux-next commit "mm: security: introduce init_on_alloc=1 and > > init_on_free=1 boot options" [1] introduced a false positive when > > init_on_free=1 and page_poison=on, due to the page_poison expects the > > pattern 0xaa when allocating pages which were overwritten by > > init_on_free=1 with 0. > > > > Fix it by switching the order between kernel_init_free_pages() and > > kernel_poison_pages() in free_pages_prepare(). > > Cool; this seems like the right approach. Alexander, what do you think? Can using init_on_free together with page_poison bring any value at all? Isn't it better to decide at boot time which of the two features we're going to enable? > Reviewed-by: Kees Cook <keescook@chromium.org> > > -Kees > > > > > [1] https://patchwork.kernel.org/patch/10999465/ > > > > Signed-off-by: Qian Cai <cai@lca.pw> > > --- > > > > v2: After further debugging, the issue after switching order is likely a > > separate issue as clear_page() should not cause issues with future > > accesses. > > > > mm/page_alloc.c | 3 ++- > > 1 file changed, 2 insertions(+), 1 deletion(-) > > > > diff --git a/mm/page_alloc.c b/mm/page_alloc.c > > index 54dacf35d200..32bbd30c5f85 100644 > > --- a/mm/page_alloc.c > > +++ b/mm/page_alloc.c > > @@ -1172,9 +1172,10 @@ static __always_inline bool free_pages_prepare(struct page *page, > > PAGE_SIZE << order); > > } > > arch_free_page(page, order); > > - kernel_poison_pages(page, 1 << order, 0); > > if (want_init_on_free()) > > kernel_init_free_pages(page, 1 << order); > > + > > + kernel_poison_pages(page, 1 << order, 0); > > if (debug_pagealloc_enabled()) > > kernel_map_pages(page, 1 << order, 0); > > > > -- > > 1.8.3.1 > > > > -- > Kees Cook -- Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH -next v2] mm/page_alloc: fix a false memory corruption 2019-06-21 10:39 ` Alexander Potapenko @ 2019-06-21 12:26 ` Qian Cai 2019-06-21 14:37 ` Alexander Potapenko 0 siblings, 1 reply; 7+ messages in thread From: Qian Cai @ 2019-06-21 12:26 UTC (permalink / raw) To: Alexander Potapenko, Kees Cook Cc: Andrew Morton, Linux Memory Management List, LKML On Fri, 2019-06-21 at 12:39 +0200, Alexander Potapenko wrote: > On Fri, Jun 21, 2019 at 3:01 AM Kees Cook <keescook@chromium.org> wrote: > > > > On Thu, Jun 20, 2019 at 04:46:06PM -0400, Qian Cai wrote: > > > The linux-next commit "mm: security: introduce init_on_alloc=1 and > > > init_on_free=1 boot options" [1] introduced a false positive when > > > init_on_free=1 and page_poison=on, due to the page_poison expects the > > > pattern 0xaa when allocating pages which were overwritten by > > > init_on_free=1 with 0. > > > > > > Fix it by switching the order between kernel_init_free_pages() and > > > kernel_poison_pages() in free_pages_prepare(). > > > > Cool; this seems like the right approach. Alexander, what do you think? > > Can using init_on_free together with page_poison bring any value at all? > Isn't it better to decide at boot time which of the two features we're > going to enable? I think the typical use case is people are using init_on_free=1, and then decide to debug something by enabling page_poison=on. Definitely, don't want init_on_free=1 to disable page_poison as the later has additional checking in the allocation time to make sure that poison pattern set in the free time is still there. > > > Reviewed-by: Kees Cook <keescook@chromium.org> > > > > -Kees > > > > > > > > [1] https://patchwork.kernel.org/patch/10999465/ > > > > > > Signed-off-by: Qian Cai <cai@lca.pw> > > > --- > > > > > > v2: After further debugging, the issue after switching order is likely a > > > separate issue as clear_page() should not cause issues with future > > > accesses. > > > > > > mm/page_alloc.c | 3 ++- > > > 1 file changed, 2 insertions(+), 1 deletion(-) > > > > > > diff --git a/mm/page_alloc.c b/mm/page_alloc.c > > > index 54dacf35d200..32bbd30c5f85 100644 > > > --- a/mm/page_alloc.c > > > +++ b/mm/page_alloc.c > > > @@ -1172,9 +1172,10 @@ static __always_inline bool > > > free_pages_prepare(struct page *page, > > > PAGE_SIZE << order); > > > } > > > arch_free_page(page, order); > > > - kernel_poison_pages(page, 1 << order, 0); > > > if (want_init_on_free()) > > > kernel_init_free_pages(page, 1 << order); > > > + > > > + kernel_poison_pages(page, 1 << order, 0); > > > if (debug_pagealloc_enabled()) > > > kernel_map_pages(page, 1 << order, 0); > > > > > > -- > > > 1.8.3.1 > > > > > > > -- > > Kees Cook > > > ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH -next v2] mm/page_alloc: fix a false memory corruption 2019-06-21 12:26 ` Qian Cai @ 2019-06-21 14:37 ` Alexander Potapenko 2019-06-21 14:56 ` Qian Cai 0 siblings, 1 reply; 7+ messages in thread From: Alexander Potapenko @ 2019-06-21 14:37 UTC (permalink / raw) To: Qian Cai; +Cc: Kees Cook, Andrew Morton, Linux Memory Management List, LKML On Fri, Jun 21, 2019 at 2:26 PM Qian Cai <cai@lca.pw> wrote: > > On Fri, 2019-06-21 at 12:39 +0200, Alexander Potapenko wrote: > > On Fri, Jun 21, 2019 at 3:01 AM Kees Cook <keescook@chromium.org> wrote: > > > > > > On Thu, Jun 20, 2019 at 04:46:06PM -0400, Qian Cai wrote: > > > > The linux-next commit "mm: security: introduce init_on_alloc=1 and > > > > init_on_free=1 boot options" [1] introduced a false positive when > > > > init_on_free=1 and page_poison=on, due to the page_poison expects the > > > > pattern 0xaa when allocating pages which were overwritten by > > > > init_on_free=1 with 0. > > > > > > > > Fix it by switching the order between kernel_init_free_pages() and > > > > kernel_poison_pages() in free_pages_prepare(). > > > > > > Cool; this seems like the right approach. Alexander, what do you think? > > > > Can using init_on_free together with page_poison bring any value at all? > > Isn't it better to decide at boot time which of the two features we're > > going to enable? > > I think the typical use case is people are using init_on_free=1, and then decide > to debug something by enabling page_poison=on. Definitely, don't want > init_on_free=1 to disable page_poison as the later has additional checking in > the allocation time to make sure that poison pattern set in the free time is > still there. In addition to information lifetime reduction the idea of init_on_free is to ensure the newly allocated objects have predictable contents. Therefore it's handy (although not strictly necessary) to keep them zero-initialized regardless of other boot-time flags. Right now free_pages_prezeroed() relies on that, though this can be changed. On the other hand, since page_poison already initializes freed memory, we can probably make want_init_on_free() return false in that case to avoid extra initialization. Side note: if we make it possible to switch betwen 0x00 and 0xAA in init_on_free mode, we can merge it with page_poison, performing the initialization depending on a boot-time flag and doing heavyweight checks under a separate config. > > > > > Reviewed-by: Kees Cook <keescook@chromium.org> > > > > > > -Kees > > > > > > > > > > > [1] https://patchwork.kernel.org/patch/10999465/ > > > > > > > > Signed-off-by: Qian Cai <cai@lca.pw> > > > > --- > > > > > > > > v2: After further debugging, the issue after switching order is likely a > > > > separate issue as clear_page() should not cause issues with future > > > > accesses. > > > > > > > > mm/page_alloc.c | 3 ++- > > > > 1 file changed, 2 insertions(+), 1 deletion(-) > > > > > > > > diff --git a/mm/page_alloc.c b/mm/page_alloc.c > > > > index 54dacf35d200..32bbd30c5f85 100644 > > > > --- a/mm/page_alloc.c > > > > +++ b/mm/page_alloc.c > > > > @@ -1172,9 +1172,10 @@ static __always_inline bool > > > > free_pages_prepare(struct page *page, > > > > PAGE_SIZE << order); > > > > } > > > > arch_free_page(page, order); > > > > - kernel_poison_pages(page, 1 << order, 0); > > > > if (want_init_on_free()) > > > > kernel_init_free_pages(page, 1 << order); > > > > + > > > > + kernel_poison_pages(page, 1 << order, 0); > > > > if (debug_pagealloc_enabled()) > > > > kernel_map_pages(page, 1 << order, 0); > > > > > > > > -- > > > > 1.8.3.1 > > > > > > > > > > -- > > > Kees Cook > > > > > > -- Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH -next v2] mm/page_alloc: fix a false memory corruption 2019-06-21 14:37 ` Alexander Potapenko @ 2019-06-21 14:56 ` Qian Cai 2019-06-21 15:26 ` Alexander Potapenko 0 siblings, 1 reply; 7+ messages in thread From: Qian Cai @ 2019-06-21 14:56 UTC (permalink / raw) To: Alexander Potapenko Cc: Kees Cook, Andrew Morton, Linux Memory Management List, LKML On Fri, 2019-06-21 at 16:37 +0200, Alexander Potapenko wrote: > On Fri, Jun 21, 2019 at 2:26 PM Qian Cai <cai@lca.pw> wrote: > > > > On Fri, 2019-06-21 at 12:39 +0200, Alexander Potapenko wrote: > > > On Fri, Jun 21, 2019 at 3:01 AM Kees Cook <keescook@chromium.org> wrote: > > > > > > > > On Thu, Jun 20, 2019 at 04:46:06PM -0400, Qian Cai wrote: > > > > > The linux-next commit "mm: security: introduce init_on_alloc=1 and > > > > > init_on_free=1 boot options" [1] introduced a false positive when > > > > > init_on_free=1 and page_poison=on, due to the page_poison expects the > > > > > pattern 0xaa when allocating pages which were overwritten by > > > > > init_on_free=1 with 0. > > > > > > > > > > Fix it by switching the order between kernel_init_free_pages() and > > > > > kernel_poison_pages() in free_pages_prepare(). > > > > > > > > Cool; this seems like the right approach. Alexander, what do you think? > > > > > > Can using init_on_free together with page_poison bring any value at all? > > > Isn't it better to decide at boot time which of the two features we're > > > going to enable? > > > > I think the typical use case is people are using init_on_free=1, and then > > decide > > to debug something by enabling page_poison=on. Definitely, don't want > > init_on_free=1 to disable page_poison as the later has additional checking > > in > > the allocation time to make sure that poison pattern set in the free time is > > still there. > > In addition to information lifetime reduction the idea of init_on_free > is to ensure the newly allocated objects have predictable contents. > Therefore it's handy (although not strictly necessary) to keep them > zero-initialized regardless of other boot-time flags. > Right now free_pages_prezeroed() relies on that, though this can be changed. > > On the other hand, since page_poison already initializes freed memory, > we can probably make want_init_on_free() return false in that case to > avoid extra initialization. > > Side note: if we make it possible to switch betwen 0x00 and 0xAA in > init_on_free mode, we can merge it with page_poison, performing the > initialization depending on a boot-time flag and doing heavyweight > checks under a separate config. Yes, that would be great which will reduce code duplication. ^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH -next v2] mm/page_alloc: fix a false memory corruption 2019-06-21 14:56 ` Qian Cai @ 2019-06-21 15:26 ` Alexander Potapenko 0 siblings, 0 replies; 7+ messages in thread From: Alexander Potapenko @ 2019-06-21 15:26 UTC (permalink / raw) To: Qian Cai; +Cc: Kees Cook, Andrew Morton, Linux Memory Management List, LKML On Fri, Jun 21, 2019 at 4:56 PM Qian Cai <cai@lca.pw> wrote: > > On Fri, 2019-06-21 at 16:37 +0200, Alexander Potapenko wrote: > > On Fri, Jun 21, 2019 at 2:26 PM Qian Cai <cai@lca.pw> wrote: > > > > > > On Fri, 2019-06-21 at 12:39 +0200, Alexander Potapenko wrote: > > > > On Fri, Jun 21, 2019 at 3:01 AM Kees Cook <keescook@chromium.org> wrote: > > > > > > > > > > On Thu, Jun 20, 2019 at 04:46:06PM -0400, Qian Cai wrote: > > > > > > The linux-next commit "mm: security: introduce init_on_alloc=1 and > > > > > > init_on_free=1 boot options" [1] introduced a false positive when > > > > > > init_on_free=1 and page_poison=on, due to the page_poison expects the > > > > > > pattern 0xaa when allocating pages which were overwritten by > > > > > > init_on_free=1 with 0. > > > > > > > > > > > > Fix it by switching the order between kernel_init_free_pages() and > > > > > > kernel_poison_pages() in free_pages_prepare(). > > > > > > > > > > Cool; this seems like the right approach. Alexander, what do you think? > > > > > > > > Can using init_on_free together with page_poison bring any value at all? > > > > Isn't it better to decide at boot time which of the two features we're > > > > going to enable? > > > > > > I think the typical use case is people are using init_on_free=1, and then > > > decide > > > to debug something by enabling page_poison=on. Definitely, don't want > > > init_on_free=1 to disable page_poison as the later has additional checking > > > in > > > the allocation time to make sure that poison pattern set in the free time is > > > still there. > > > > In addition to information lifetime reduction the idea of init_on_free > > is to ensure the newly allocated objects have predictable contents. > > Therefore it's handy (although not strictly necessary) to keep them > > zero-initialized regardless of other boot-time flags. > > Right now free_pages_prezeroed() relies on that, though this can be changed. > > > > On the other hand, since page_poison already initializes freed memory, > > we can probably make want_init_on_free() return false in that case to > > avoid extra initialization. > > > > Side note: if we make it possible to switch betwen 0x00 and 0xAA in > > init_on_free mode, we can merge it with page_poison, performing the > > initialization depending on a boot-time flag and doing heavyweight > > checks under a separate config. > > Yes, that would be great which will reduce code duplication. I suggest we disable init_on_alloc/init_on_free under CONFIG_PAGE_POISONING now then and work towards deduplicating this code in further patch series. -- Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Straße, 33 80636 München Geschäftsführer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg ^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2019-06-21 15:26 UTC | newest] Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2019-06-20 20:46 [PATCH -next v2] mm/page_alloc: fix a false memory corruption Qian Cai 2019-06-21 1:01 ` Kees Cook 2019-06-21 10:39 ` Alexander Potapenko 2019-06-21 12:26 ` Qian Cai 2019-06-21 14:37 ` Alexander Potapenko 2019-06-21 14:56 ` Qian Cai 2019-06-21 15:26 ` Alexander Potapenko
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox