Skip to content

kasan: switch tag-based modes to stack ring from per-object metadata

[email protected] linux-mm lkml 3 versions – Applied

Status from this message: [merged mm-stable] kasan-add-another-use-after-free-test.patch removed from -mm tree

Get series with b4
b4 am [email protected]

Versions newest first · linked by author and title

  1. v3 Latest34 patches 5 repliesApplied 34/34
  2. v233 patches 7 repliesSuperseded
  3. v132 patches 19 repliesSuperseded

Changes in v3 from the cover letter

- Addressed Marco's comments, see the last 3 patches for list of changes.

Patches in v3 All 34 applied

#Subject and filesSizeRepliesApplied
1/34kasan: check KASAN_NO_FREE_META in __kasan_metadata_sizemm/kasan/common.c+3 -20 repliesApplied
2/34kasan: rename kasan_set_*_info to kasan_save_*_infomm/kasan/common.c mm/kasan/generic.c mm/kasan/kasan.h +1 more+7 -70 repliesApplied
3/34kasan: move is_kmalloc check out of save_alloc_infomm/kasan/common.c+5 -100 repliesApplied
4/34kasan: split save_alloc_info implementationsmm/kasan/common.c mm/kasan/generic.c mm/kasan/kasan.h +1 more+21 -110 repliesApplied
5/34kasan: drop CONFIG_KASAN_TAGS_IDENTIFYlib/Kconfig.kasan mm/kasan/kasan.h mm/kasan/report_tags.c +1 more+3 -660 repliesApplied
6/34kasan: introduce kasan_print_aux_stacksmm/kasan/kasan.h mm/kasan/report.c mm/kasan/report_generic.c+27 -140 repliesApplied
7/34kasan: introduce kasan_get_alloc_trackmm/kasan/generic.c mm/kasan/kasan.h mm/kasan/report.c +1 more+33 -70 repliesApplied
8/34kasan: introduce kasan_init_object_metamm/kasan/common.c mm/kasan/generic.c mm/kasan/kasan.h +1 more+23 -70 repliesApplied
9/34kasan: clear metadata functions for tag-based modesmm/kasan/tags.c+2 -310 repliesApplied
10/34kasan: move kasan_get_*_meta to generic.cmm/kasan/common.c mm/kasan/generic.c mm/kasan/kasan.h+24 -260 repliesApplied
11/34kasan: introduce kasan_requires_metamm/kasan/common.c mm/kasan/kasan.h mm/kasan/tags.c+34 -160 repliesApplied
12/34kasan: introduce kasan_init_cache_metamm/kasan/common.c mm/kasan/generic.c mm/kasan/kasan.h+83 -780 repliesApplied
13/34kasan: drop CONFIG_KASAN_GENERIC check from kasan_init_cache_metamm/kasan/generic.c+0 -60 repliesApplied
14/34kasan: only define kasan_metadata_size for Generic modeinclude/linux/kasan.h mm/kasan/common.c mm/kasan/generic.c+19 -200 repliesApplied
15/34kasan: only define kasan_never_merge for Generic modeinclude/linux/kasan.h mm/kasan/common.c mm/kasan/generic.c+14 -200 repliesApplied
16/34kasan: only define metadata offsets for Generic modeinclude/linux/kasan.h+2 -00 repliesApplied
17/34kasan: only define metadata structs for Generic modemm/kasan/kasan.h+5 -70 repliesApplied
18/34kasan: only define kasan_cache_create for Generic modeinclude/linux/kasan.h include/linux/slab.h mm/kasan/common.c +1 more+23 -300 repliesApplied
19/34kasan: pass tagged pointers to kasan_save_alloc/free_infomm/kasan/common.c mm/kasan/generic.c mm/kasan/kasan.h +1 more+5 -90 repliesApplied
20/34kasan: move kasan_get_alloc/free_track definitionsmm/kasan/generic.c mm/kasan/report_generic.c mm/kasan/report_tags.c +1 more+33 -330 repliesApplied
21/34kasan: cosmetic changes in report.cmm/kasan/report.c+4 -70 repliesApplied
22/34kasan: use virt_addr_valid in kasan_addr_to_page/slabmm/kasan/report.c+2 -20 repliesApplied
23/34kasan: use kasan_addr_to_slab in print_address_descriptionmm/kasan/common.c mm/kasan/report.c+9 -90 repliesApplied
24/34kasan: make kasan_addr_to_page staticmm/kasan/kasan.h mm/kasan/report.c+2 -30 repliesApplied
25/34kasan: simplify print_reportmm/kasan/report.c+5 -60 repliesApplied
26/34kasan: introduce complete_report_infomm/kasan/kasan.h mm/kasan/report.c+19 -30 repliesApplied
27/34kasan: fill in cache and object in complete_report_infomm/kasan/kasan.h mm/kasan/report.c+15 -80 repliesApplied
28/34kasan: rework function arguments in report.cmm/kasan/report.c+11 -120 repliesApplied
29/34kasan: introduce kasan_complete_mode_report_infomm/kasan/kasan.h mm/kasan/report.c mm/kasan/report_generic.c +1 more+51 -570 repliesApplied
30/34kasan: implement stack ring for tag-based modesmm/kasan/kasan.h mm/kasan/report_tags.c mm/kasan/tags.c+142 -00 repliesApplied
31/34kasan: support kasan.stacktrace for SW_TAGSDocumentation/dev-tools/kasan.rst mm/kasan/hw_tags.c mm/kasan/kasan.h +2 more+81 -570 repliesApplied
32/34kasan: dynamically allocate stack ring entriesDocumentation/dev-tools/kasan.rst mm/kasan/kasan.h mm/kasan/report_tags.c +1 more+31 -70 repliesApplied
33/34kasan: better identify bug types for tag-based modesmm/kasan/report_tags.c+21 -40 repliesApplied
34/34kasan: add another use-after-free testlib/test_kasan.c+24 -00 repliesApplied

Reviews trailers per patch

31 of 34 patches have a Reviewed-by or Acked-by of their own; the series has one for all of them.

PatchMarco Elver
SeriesA-b
1/34R-b
2/34R-b
3/34R-b
4/34R-b
5/34R-b
6/34R-b
7/34R-b
8/34R-b
9/34R-b
10/34R-b
11/34R-b
12/34R-b
13/34R-b
14/34R-b
15/34R-b
16/34R-b
17/34R-b
18/34R-b
19/34R-b
20/34R-b
21/34R-b
22/34R-b
23/34R-b
24/34R-b
25/34R-b
26/34R-b
27/34R-b
28/34R-b
29/34R-b
30/34
31/34R-b
32/34
33/34R-b
34/34

Trailers given in replies are filled; those carried in the patches from earlier versions are outlined with dashes.

Read the v3 thread

Cover letter [PATCH mm v3 00/34] kasan: switch tag-based modes to stack ring from per-object metadata

From: Andrey Konovalov <[email protected]>

This series makes the tag-based KASAN modes use a ring buffer for storing stack depot handles for alloc/free stack traces for slab objects instead of per-object metadata. This ring buffer is referred to as the stack ring.

On each alloc/free of a slab object, the tagged address of the object and the current stack trace are recorded in the stack ring.

On each bug report, if the accessed address belongs to a slab object, the stack ring is scanned for matching entries. The newest entries are used to print the alloc/free stack traces in the report: one entry for alloc and one for free.

The advantages of this approach over storing stack trace handles in per-object metadata with the tag-based KASAN modes:

- Allows to find relevant stack traces for use-after-free bugs without using quarantine for freed memory. (Currently, if the object was reallocated multiple times, the report contains the latest alloc/free stack traces, not necessarily the ones relevant to the buggy allocation.)
- Allows to better identify and mark use-after-free bugs, effectively making the CONFIG_KASAN_TAGS_IDENTIFY functionality always-on.
- Has fixed memory overhead.

The disadvantage:

- If the affected object was allocated/freed long before the bug happened and the stack trace events were purged from the stack ring, the report will have no stack traces.

Discussion
==========

The proposed implementation of the stack ring uses a single ring buffer for the whole kernel. This might lead to contention due to atomic accesses to the ring buffer index on multicore systems.

At this point, it is unknown whether the performance impact from this contention would be significant compared to the slowdown introduced by collecting stack traces due to the planned changes to the latter part, see the section below.

For now, the proposed implementation is deemed to be good enough, but this might need to be revisited once the stack collection becomes faster.

A considered alternative is to keep a separate ring buffer for each CPU and then iterate over all of them when printing a bug report. This approach requires somehow figuring out which of the stack rings has the freshest stack traces for an object if multiple stack rings have them.

Further plans
=============

This series is a part of an effort to make KASAN stack trace collection suitable for production. This requires stack trace collection to be fast and memory-bounded.

The planned steps are:

1. Speed up stack trace collection (potentially, by using SCS; patches on-hold until steps #2 and #3 are completed).
2. Keep stack trace handles in the stack ring (this series).
3. Add a memory-bounded mode to stack depot or provide an alternative memory-bounded stack storage.
4. Potentially, implement stack trace collection sampling to minimize the performance impact.

Thanks!

---

Changes v2->v3:
- Addressed Marco's comments, see the last 3 patches for list of changes.

Changes v1->v2:
- Rework synchronization in the stack ring implementation.
- Dynamically allocate stack ring based on the kasan.stack_ring_size command-line parameter.
- Multiple less significant changes, see the notes in patches for details.

Andrey Konovalov (34):
kasan: check KASAN_NO_FREE_META in __kasan_metadata_size
kasan: rename kasan_set_*_info to kasan_save_*_info
kasan: move is_kmalloc check out of save_alloc_info
kasan: split save_alloc_info implementations
kasan: drop CONFIG_KASAN_TAGS_IDENTIFY
kasan: introduce kasan_print_aux_stacks
kasan: introduce kasan_get_alloc_track
kasan: introduce kasan_init_object_meta
kasan: clear metadata functions for tag-based modes
kasan: move kasan_get_*_meta to generic.c
kasan: introduce kasan_requires_meta
kasan: introduce kasan_init_cache_meta
kasan: drop CONFIG_KASAN_GENERIC check from kasan_init_cache_meta
kasan: only define kasan_metadata_size for Generic mode
kasan: only define kasan_never_merge for Generic mode
kasan: only define metadata offsets for Generic mode
kasan: only define metadata structs for Generic mode
kasan: only define kasan_cache_create for Generic mode
kasan: pass tagged pointers to kasan_save_alloc/free_info
kasan: move kasan_get_alloc/free_track definitions
kasan: cosmetic changes in report.c
kasan: use virt_addr_valid in kasan_addr_to_page/slab
kasan: use kasan_addr_to_slab in print_address_description
kasan: make kasan_addr_to_page static
kasan: simplify print_report
kasan: introduce complete_report_info
kasan: fill in cache and object in complete_report_info
kasan: rework function arguments in report.c
kasan: introduce kasan_complete_mode_report_info
kasan: implement stack ring for tag-based modes
kasan: support kasan.stacktrace for SW_TAGS
kasan: dynamically allocate stack ring entries
kasan: better identify bug types for tag-based modes
kasan: add another use-after-free test

 Documentation/dev-tools/kasan.rst |  17 ++-
 include/linux/kasan.h             |  55 ++++------
 include/linux/slab.h              |   2 +-
 lib/Kconfig.kasan                 |   8 --
 lib/test_kasan.c                  |  24 ++++
 mm/kasan/common.c                 | 175 +++---------------------------
 mm/kasan/generic.c                | 154 ++++++++++++++++++++++++--
 mm/kasan/hw_tags.c                |  39 +------
 mm/kasan/kasan.h                  | 171 ++++++++++++++++++++---------
 mm/kasan/report.c                 | 117 +++++++++-----------
 mm/kasan/report_generic.c         |  45 +++++++-
 mm/kasan/report_tags.c            | 123 ++++++++++++++++-----
 mm/kasan/sw_tags.c                |   5 +-
 mm/kasan/tags.c                   | 141 +++++++++++++++++++-----
 14 files changed, 642 insertions(+), 434 deletions(-)
Signature · 1 line
2.25.1