Skip to content

Commit

Permalink
kasan: reset page tags properly with sampling
Browse files Browse the repository at this point in the history
The implementation of page_alloc poisoning sampling assumed that
tag_clear_highpage resets page tags for __GFP_ZEROTAGS allocations. 
However, this is no longer the case since commit 70c248a ("mm: kasan:
Skip unpoisoning of user pages").

This leads to kernel crashes when MTE-enabled userspace mappings are used
with Hardware Tag-Based KASAN enabled.

Reset page tags for __GFP_ZEROTAGS allocations in post_alloc_hook().

Also clarify and fix related comments.

[[email protected]: update comment]
 Link: https://lkml.kernel.org/r/5dbd866714b4839069e2d8469ac45b60953db290.1674592780.git.andreyknvl@google.com
Link: https://lkml.kernel.org/r/24ea20c1b19c2b4b56cf9f5b354915f8dbccfc77.1674592496.git.andreyknvl@google.com
Fixes: 44383ce ("kasan: allow sampling page_alloc allocations for HW_TAGS")
Signed-off-by: Andrey Konovalov <[email protected]>
Reported-by: Peter Collingbourne <[email protected]>
Tested-by: Peter Collingbourne <[email protected]>
Cc: Alexander Potapenko <[email protected]>
Cc: Andrey Ryabinin <[email protected]>
Cc: Dmitry Vyukov <[email protected]>
Cc: Marco Elver <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
  • Loading branch information
xairy authored and akpm00 committed Feb 3, 2023
1 parent 2e126aa commit 420ef68
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions mm/page_alloc.c
Original file line number Diff line number Diff line change
Expand Up @@ -2476,7 +2476,7 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
bool init = !want_init_on_free() && want_init_on_alloc(gfp_flags) &&
!should_skip_init(gfp_flags);
bool zero_tags = init && (gfp_flags & __GFP_ZEROTAGS);
bool reset_tags = !zero_tags;
bool reset_tags = true;
int i;

set_page_private(page, 0);
Expand All @@ -2503,7 +2503,7 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
* (which happens only when memory should be initialized as well).
*/
if (zero_tags) {
/* Initialize both memory and tags. */
/* Initialize both memory and memory tags. */
for (i = 0; i != 1 << order; ++i)
tag_clear_highpage(page + i);

Expand All @@ -2521,14 +2521,15 @@ inline void post_alloc_hook(struct page *page, unsigned int order,
} else {
/*
* KASAN decided to exclude this allocation from being
* poisoned due to sampling. Skip poisoning as well.
* (un)poisoned due to sampling. Make KASAN skip
* poisoning when the allocation is freed.
*/
SetPageSkipKASanPoison(page);
}
}
/*
* If memory tags have not been set, reset the page tags to ensure
* page_address() dereferencing does not fault.
* If memory tags have not been set by KASAN, reset the page tags to
* ensure page_address() dereferencing does not fault.
*/
if (reset_tags) {
for (i = 0; i != 1 << order; ++i)
Expand Down

0 comments on commit 420ef68

Please sign in to comment.