Skip to content

Commit c275c5c

Browse files
pccwilldeacon
authored andcommitted
kasan: disable freed user page poisoning with HW tags
Poisoning freed pages protects against kernel use-after-free. The likelihood of such a bug involving kernel pages is significantly higher than that for user pages. At the same time, poisoning freed pages can impose a significant performance cost, which cannot always be justified for user pages given the lower probability of finding a bug. Therefore, disable freed user page poisoning when using HW tags. We identify "user" pages via the flag set GFP_HIGHUSER_MOVABLE, which indicates a strong likelihood of not being directly accessible to the kernel. Signed-off-by: Peter Collingbourne <pcc@google.com> Reviewed-by: Andrey Konovalov <andreyknvl@gmail.com> Link: https://linux-review.googlesource.com/id/I716846e2de8ef179f44e835770df7e6307be96c9 Link: https://lore.kernel.org/r/20210602235230.3928842-5-pcc@google.com Signed-off-by: Will Deacon <will@kernel.org>
1 parent 013bb59 commit c275c5c

5 files changed

Lines changed: 37 additions & 9 deletions

File tree

include/linux/gfp.h

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,9 @@ struct vm_area_struct;
5454
#define ___GFP_THISNODE 0x200000u
5555
#define ___GFP_ACCOUNT 0x400000u
5656
#define ___GFP_ZEROTAGS 0x800000u
57+
#define ___GFP_SKIP_KASAN_POISON 0x1000000u
5758
#ifdef CONFIG_LOCKDEP
58-
#define ___GFP_NOLOCKDEP 0x1000000u
59+
#define ___GFP_NOLOCKDEP 0x2000000u
5960
#else
6061
#define ___GFP_NOLOCKDEP 0
6162
#endif
@@ -233,17 +234,22 @@ struct vm_area_struct;
233234
*
234235
* %__GFP_ZEROTAGS returns a page with zeroed memory tags on success, if
235236
* __GFP_ZERO is set.
237+
*
238+
* %__GFP_SKIP_KASAN_POISON returns a page which does not need to be poisoned
239+
* on deallocation. Typically used for userspace pages. Currently only has an
240+
* effect in HW tags mode.
236241
*/
237242
#define __GFP_NOWARN ((__force gfp_t)___GFP_NOWARN)
238243
#define __GFP_COMP ((__force gfp_t)___GFP_COMP)
239244
#define __GFP_ZERO ((__force gfp_t)___GFP_ZERO)
240245
#define __GFP_ZEROTAGS ((__force gfp_t)___GFP_ZEROTAGS)
246+
#define __GFP_SKIP_KASAN_POISON ((__force gfp_t)___GFP_SKIP_KASAN_POISON)
241247

242248
/* Disable lockdep for GFP context tracking */
243249
#define __GFP_NOLOCKDEP ((__force gfp_t)___GFP_NOLOCKDEP)
244250

245251
/* Room for N __GFP_FOO bits */
246-
#define __GFP_BITS_SHIFT (24 + IS_ENABLED(CONFIG_LOCKDEP))
252+
#define __GFP_BITS_SHIFT (25 + IS_ENABLED(CONFIG_LOCKDEP))
247253
#define __GFP_BITS_MASK ((__force gfp_t)((1 << __GFP_BITS_SHIFT) - 1))
248254

249255
/**
@@ -324,7 +330,8 @@ struct vm_area_struct;
324330
#define GFP_DMA __GFP_DMA
325331
#define GFP_DMA32 __GFP_DMA32
326332
#define GFP_HIGHUSER (GFP_USER | __GFP_HIGHMEM)
327-
#define GFP_HIGHUSER_MOVABLE (GFP_HIGHUSER | __GFP_MOVABLE)
333+
#define GFP_HIGHUSER_MOVABLE (GFP_HIGHUSER | __GFP_MOVABLE | \
334+
__GFP_SKIP_KASAN_POISON)
328335
#define GFP_TRANSHUGE_LIGHT ((GFP_HIGHUSER_MOVABLE | __GFP_COMP | \
329336
__GFP_NOMEMALLOC | __GFP_NOWARN) & ~__GFP_RECLAIM)
330337
#define GFP_TRANSHUGE (GFP_TRANSHUGE_LIGHT | __GFP_DIRECT_RECLAIM)

include/linux/page-flags.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ enum pageflags {
137137
#endif
138138
#ifdef CONFIG_64BIT
139139
PG_arch_2,
140+
#endif
141+
#ifdef CONFIG_KASAN_HW_TAGS
142+
PG_skip_kasan_poison,
140143
#endif
141144
__NR_PAGEFLAGS,
142145

@@ -443,6 +446,12 @@ TESTCLEARFLAG(Young, young, PF_ANY)
443446
PAGEFLAG(Idle, idle, PF_ANY)
444447
#endif
445448

449+
#ifdef CONFIG_KASAN_HW_TAGS
450+
PAGEFLAG(SkipKASanPoison, skip_kasan_poison, PF_HEAD)
451+
#else
452+
PAGEFLAG_FALSE(SkipKASanPoison)
453+
#endif
454+
446455
/*
447456
* PageReported() is used to track reported free pages within the Buddy
448457
* allocator. We can use the non-atomic version of the test and set

include/trace/events/mmflags.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,12 @@
8585
#define IF_HAVE_PG_ARCH_2(flag,string)
8686
#endif
8787

88+
#ifdef CONFIG_KASAN_HW_TAGS
89+
#define IF_HAVE_PG_SKIP_KASAN_POISON(flag,string) ,{1UL << flag, string}
90+
#else
91+
#define IF_HAVE_PG_SKIP_KASAN_POISON(flag,string)
92+
#endif
93+
8894
#define __def_pageflag_names \
8995
{1UL << PG_locked, "locked" }, \
9096
{1UL << PG_waiters, "waiters" }, \
@@ -112,7 +118,8 @@ IF_HAVE_PG_UNCACHED(PG_uncached, "uncached" ) \
112118
IF_HAVE_PG_HWPOISON(PG_hwpoison, "hwpoison" ) \
113119
IF_HAVE_PG_IDLE(PG_young, "young" ) \
114120
IF_HAVE_PG_IDLE(PG_idle, "idle" ) \
115-
IF_HAVE_PG_ARCH_2(PG_arch_2, "arch_2" )
121+
IF_HAVE_PG_ARCH_2(PG_arch_2, "arch_2" ) \
122+
IF_HAVE_PG_SKIP_KASAN_POISON(PG_skip_kasan_poison, "skip_kasan_poison")
116123

117124
#define show_page_flags(flags) \
118125
(flags) ? __print_flags(flags, "|", \

mm/kasan/hw_tags.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,9 @@ void kasan_alloc_pages(struct page *page, unsigned int order, gfp_t flags)
246246
*/
247247
bool init = !want_init_on_free() && want_init_on_alloc(flags);
248248

249+
if (flags & __GFP_SKIP_KASAN_POISON)
250+
SetPageSkipKASanPoison(page);
251+
249252
if (flags & __GFP_ZEROTAGS) {
250253
int i;
251254

mm/page_alloc.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -394,11 +394,12 @@ static DEFINE_STATIC_KEY_TRUE(deferred_pages);
394394
* on-demand allocation and then freed again before the deferred pages
395395
* initialization is done, but this is not likely to happen.
396396
*/
397-
static inline bool should_skip_kasan_poison(fpi_t fpi_flags)
397+
static inline bool should_skip_kasan_poison(struct page *page, fpi_t fpi_flags)
398398
{
399399
return static_branch_unlikely(&deferred_pages) ||
400400
(!IS_ENABLED(CONFIG_KASAN_GENERIC) &&
401-
(fpi_flags & FPI_SKIP_KASAN_POISON));
401+
(fpi_flags & FPI_SKIP_KASAN_POISON)) ||
402+
PageSkipKASanPoison(page);
402403
}
403404

404405
/* Returns true if the struct page for the pfn is uninitialised */
@@ -449,10 +450,11 @@ defer_init(int nid, unsigned long pfn, unsigned long end_pfn)
449450
return false;
450451
}
451452
#else
452-
static inline bool should_skip_kasan_poison(fpi_t fpi_flags)
453+
static inline bool should_skip_kasan_poison(struct page *page, fpi_t fpi_flags)
453454
{
454455
return (!IS_ENABLED(CONFIG_KASAN_GENERIC) &&
455-
(fpi_flags & FPI_SKIP_KASAN_POISON));
456+
(fpi_flags & FPI_SKIP_KASAN_POISON)) ||
457+
PageSkipKASanPoison(page);
456458
}
457459

458460
static inline bool early_page_uninitialised(unsigned long pfn)
@@ -1244,7 +1246,7 @@ static __always_inline bool free_pages_prepare(struct page *page,
12441246
unsigned int order, bool check_free, fpi_t fpi_flags)
12451247
{
12461248
int bad = 0;
1247-
bool skip_kasan_poison = should_skip_kasan_poison(fpi_flags);
1249+
bool skip_kasan_poison = should_skip_kasan_poison(page, fpi_flags);
12481250

12491251
VM_BUG_ON_PAGE(PageTail(page), page);
12501252

0 commit comments

Comments
 (0)