mm-commits.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Andrew Morton <akpm@linux-foundation.org>
To: akpm@linux-foundation.org, andreyknvl@google.com,
	aryabinin@virtuozzo.com, Branislav.Rankov@arm.com,
	catalin.marinas@arm.com, dvyukov@google.com, elver@google.com,
	eugenis@google.com, glider@google.com, gor@linux.ibm.com,
	kevin.brodsky@arm.com, linux-mm@kvack.org,
	mm-commits@vger.kernel.org, torvalds@linux-foundation.org,
	vincenzo.frascino@arm.com, will.deacon@arm.com
Subject: [patch 60/60] kasan: update documentation
Date: Tue, 22 Dec 2020 12:03:35 -0800	[thread overview]
Message-ID: <20201222200335.RHdnHDjIK%akpm@linux-foundation.org> (raw)
In-Reply-To: <20201222115844.d30aaef7df6f5b120d3e0c3d@linux-foundation.org>

From: Andrey Konovalov <andreyknvl@google.com>
Subject: kasan: update documentation

This change updates KASAN documentation to reflect the addition of boot
parameters and also reworks and clarifies some of the existing sections,
in particular: defines what a memory granule is, mentions quarantine,
makes Kunit section more readable.

Link: https://lkml.kernel.org/r/748daf013e17d925b0fe00c1c3b5dce726dd2430.1606162397.git.andreyknvl@google.com
Link: https://linux-review.googlesource.com/id/Ib1f83e91be273264b25f42b04448ac96b858849f
Signed-off-by: Andrey Konovalov <andreyknvl@google.com>
Reviewed-by: Dmitry Vyukov <dvyukov@google.com>
Reviewed-by: Marco Elver <elver@google.com>
Tested-by: Vincenzo Frascino <vincenzo.frascino@arm.com>
Cc: Alexander Potapenko <glider@google.com>
Cc: Andrey Ryabinin <aryabinin@virtuozzo.com>
Cc: Branislav Rankov <Branislav.Rankov@arm.com>
Cc: Catalin Marinas <catalin.marinas@arm.com>
Cc: Evgenii Stepanov <eugenis@google.com>
Cc: Kevin Brodsky <kevin.brodsky@arm.com>
Cc: Vasily Gorbik <gor@linux.ibm.com>
Cc: Will Deacon <will.deacon@arm.com>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
---

 Documentation/dev-tools/kasan.rst |  188 +++++++++++++++++-----------
 1 file changed, 117 insertions(+), 71 deletions(-)

--- a/Documentation/dev-tools/kasan.rst~kasan-update-documentation
+++ a/Documentation/dev-tools/kasan.rst
@@ -4,8 +4,9 @@ The Kernel Address Sanitizer (KASAN)
 Overview
 --------
 
-KernelAddressSANitizer (KASAN) is a dynamic memory error detector designed to
-find out-of-bound and use-after-free bugs. KASAN has three modes:
+KernelAddressSANitizer (KASAN) is a dynamic memory safety error detector
+designed to find out-of-bound and use-after-free bugs. KASAN has three modes:
+
 1. generic KASAN (similar to userspace ASan),
 2. software tag-based KASAN (similar to userspace HWASan),
 3. hardware tag-based KASAN (based on hardware memory tagging).
@@ -39,23 +40,13 @@ CONFIG_KASAN_INLINE. Outline and inline
 The former produces smaller binary while the latter is 1.1 - 2 times faster.
 
 Both software KASAN modes work with both SLUB and SLAB memory allocators,
-hardware tag-based KASAN currently only support SLUB.
-For better bug detection and nicer reporting, enable CONFIG_STACKTRACE.
+while the hardware tag-based KASAN currently only support SLUB.
+
+For better error reports that include stack traces, enable CONFIG_STACKTRACE.
 
 To augment reports with last allocation and freeing stack of the physical page,
 it is recommended to enable also CONFIG_PAGE_OWNER and boot with page_owner=on.
 
-To disable instrumentation for specific files or directories, add a line
-similar to the following to the respective kernel Makefile:
-
-- For a single file (e.g. main.o)::
-
-    KASAN_SANITIZE_main.o := n
-
-- For all files in one directory::
-
-    KASAN_SANITIZE := n
-
 Error reports
 ~~~~~~~~~~~~~
 
@@ -140,22 +131,75 @@ freed (in case of a use-after-free bug r
 the accessed slab object and information about the accessed memory page.
 
 In the last section the report shows memory state around the accessed address.
-Reading this part requires some understanding of how KASAN works.
-
-The state of each 8 aligned bytes of memory is encoded in one shadow byte.
-Those 8 bytes can be accessible, partially accessible, freed or be a redzone.
-We use the following encoding for each shadow byte: 0 means that all 8 bytes
-of the corresponding memory region are accessible; number N (1 <= N <= 7) means
-that the first N bytes are accessible, and other (8 - N) bytes are not;
-any negative value indicates that the entire 8-byte word is inaccessible.
-We use different negative values to distinguish between different kinds of
-inaccessible memory like redzones or freed memory (see mm/kasan/kasan.h).
+Internally KASAN tracks memory state separately for each memory granule, which
+is either 8 or 16 aligned bytes depending on KASAN mode. Each number in the
+memory state section of the report shows the state of one of the memory
+granules that surround the accessed address.
+
+For generic KASAN the size of each memory granule is 8. The state of each
+granule is encoded in one shadow byte. Those 8 bytes can be accessible,
+partially accessible, freed or be a part of a redzone. KASAN uses the following
+encoding for each shadow byte: 0 means that all 8 bytes of the corresponding
+memory region are accessible; number N (1 <= N <= 7) means that the first N
+bytes are accessible, and other (8 - N) bytes are not; any negative value
+indicates that the entire 8-byte word is inaccessible. KASAN uses different
+negative values to distinguish between different kinds of inaccessible memory
+like redzones or freed memory (see mm/kasan/kasan.h).
 
 In the report above the arrows point to the shadow byte 03, which means that
 the accessed address is partially accessible.
 
 For tag-based KASAN this last report section shows the memory tags around the
-accessed address (see Implementation details section).
+accessed address (see `Implementation details`_ section).
+
+Boot parameters
+~~~~~~~~~~~~~~~
+
+Hardware tag-based KASAN mode (see the section about different mode below) is
+intended for use in production as a security mitigation. Therefore it supports
+boot parameters that allow to disable KASAN competely or otherwise control
+particular KASAN features.
+
+The things that can be controlled are:
+
+1. Whether KASAN is enabled at all.
+2. Whether KASAN collects and saves alloc/free stacks.
+3. Whether KASAN panics on a detected bug or not.
+
+The ``kasan.mode`` boot parameter allows to choose one of three main modes:
+
+- ``kasan.mode=off`` - KASAN is disabled, no tag checks are performed
+- ``kasan.mode=prod`` - only essential production features are enabled
+- ``kasan.mode=full`` - all KASAN features are enabled
+
+The chosen mode provides default control values for the features mentioned
+above. However it's also possible to override the default values by providing:
+
+- ``kasan.stacktrace=off`` or ``=on`` - enable alloc/free stack collection
+					(default: ``on`` for ``mode=full``,
+					 otherwise ``off``)
+- ``kasan.fault=report`` or ``=panic`` - only print KASAN report or also panic
+					 (default: ``report``)
+
+If ``kasan.mode`` parameter is not provided, it defaults to ``full`` when
+``CONFIG_DEBUG_KERNEL`` is enabled, and to ``prod`` otherwise.
+
+For developers
+~~~~~~~~~~~~~~
+
+Software KASAN modes use compiler instrumentation to insert validity checks.
+Such instrumentation might be incompatible with some part of the kernel, and
+therefore needs to be disabled. To disable instrumentation for specific files
+or directories, add a line similar to the following to the respective kernel
+Makefile:
+
+- For a single file (e.g. main.o)::
+
+    KASAN_SANITIZE_main.o := n
+
+- For all files in one directory::
+
+    KASAN_SANITIZE := n
 
 
 Implementation details
@@ -164,10 +208,10 @@ Implementation details
 Generic KASAN
 ~~~~~~~~~~~~~
 
-From a high level, our approach to memory error detection is similar to that
-of kmemcheck: use shadow memory to record whether each byte of memory is safe
-to access, and use compile-time instrumentation to insert checks of shadow
-memory on each memory access.
+From a high level perspective, KASAN's approach to memory error detection is
+similar to that of kmemcheck: use shadow memory to record whether each byte of
+memory is safe to access, and use compile-time instrumentation to insert checks
+of shadow memory on each memory access.
 
 Generic KASAN dedicates 1/8th of kernel memory to its shadow memory (e.g. 16TB
 to cover 128TB on x86_64) and uses direct mapping with a scale and offset to
@@ -198,6 +242,9 @@ Generic KASAN also reports the last 2 ca
 potentially has access to an object. Call stacks for the following are shown:
 call_rcu() and workqueue queuing.
 
+Generic KASAN is the only mode that delays the reuse of freed object via
+quarantine (see mm/kasan/quarantine.c for implementation).
+
 Software tag-based KASAN
 ~~~~~~~~~~~~~~~~~~~~~~~~
 
@@ -305,15 +352,15 @@ therefore be wasteful. Furthermore, to e
 use different shadow pages, mappings would have to be aligned to
 ``KASAN_GRANULE_SIZE * PAGE_SIZE``.
 
-Instead, we share backing space across multiple mappings. We allocate
+Instead, KASAN shares backing space across multiple mappings. It allocates
 a backing page when a mapping in vmalloc space uses a particular page
 of the shadow region. This page can be shared by other vmalloc
 mappings later on.
 
-We hook in to the vmap infrastructure to lazily clean up unused shadow
+KASAN hooks into the vmap infrastructure to lazily clean up unused shadow
 memory.
 
-To avoid the difficulties around swapping mappings around, we expect
+To avoid the difficulties around swapping mappings around, KASAN expects
 that the part of the shadow region that covers the vmalloc space will
 not be covered by the early shadow page, but will be left
 unmapped. This will require changes in arch-specific code.
@@ -324,24 +371,31 @@ architectures that do not have a fixed m
 CONFIG_KASAN_KUNIT_TEST & CONFIG_TEST_KASAN_MODULE
 --------------------------------------------------
 
-``CONFIG_KASAN_KUNIT_TEST`` utilizes the KUnit Test Framework for testing.
-This means each test focuses on a small unit of functionality and
-there are a few ways these tests can be run.
+KASAN tests consist on two parts:
+
+1. Tests that are integrated with the KUnit Test Framework. Enabled with
+``CONFIG_KASAN_KUNIT_TEST``. These tests can be run and partially verified
+automatically in a few different ways, see the instructions below.
+
+2. Tests that are currently incompatible with KUnit. Enabled with
+``CONFIG_TEST_KASAN_MODULE`` and can only be run as a module. These tests can
+only be verified manually, by loading the kernel module and inspecting the
+kernel log for KASAN reports.
 
-Each test will print the KASAN report if an error is detected and then
-print the number of the test and the status of the test:
+Each KUnit-compatible KASAN test prints a KASAN report if an error is detected.
+Then the test prints its number and status.
 
-pass::
+When a test passes::
 
         ok 28 - kmalloc_double_kzfree
 
-or, if kmalloc failed::
+When a test fails due to a failed ``kmalloc``::
 
         # kmalloc_large_oob_right: ASSERTION FAILED at lib/test_kasan.c:163
         Expected ptr is not null, but is
         not ok 4 - kmalloc_large_oob_right
 
-or, if a KASAN report was expected, but not found::
+When a test fails due to a missing KASAN report::
 
         # kmalloc_double_kzfree: EXPECTATION FAILED at lib/test_kasan.c:629
         Expected kasan_data->report_expected == kasan_data->report_found, but
@@ -349,46 +403,38 @@ or, if a KASAN report was expected, but
         kasan_data->report_found == 0
         not ok 28 - kmalloc_double_kzfree
 
-All test statuses are tracked as they run and an overall status will
-be printed at the end::
+At the end the cumulative status of all KASAN tests is printed. On success::
 
         ok 1 - kasan
 
-or::
+Or, if one of the tests failed::
 
         not ok 1 - kasan
 
-(1) Loadable Module
-~~~~~~~~~~~~~~~~~~~~
+
+There are a few ways to run KUnit-compatible KASAN tests.
+
+1. Loadable module
+~~~~~~~~~~~~~~~~~~
 
 With ``CONFIG_KUNIT`` enabled, ``CONFIG_KASAN_KUNIT_TEST`` can be built as
-a loadable module and run on any architecture that supports KASAN
-using something like insmod or modprobe. The module is called ``test_kasan``.
+a loadable module and run on any architecture that supports KASAN by loading
+the module with insmod or modprobe. The module is called ``test_kasan``.
 
-(2) Built-In
-~~~~~~~~~~~~~
+2. Built-In
+~~~~~~~~~~~
 
 With ``CONFIG_KUNIT`` built-in, ``CONFIG_KASAN_KUNIT_TEST`` can be built-in
-on any architecture that supports KASAN. These and any other KUnit
-tests enabled will run and print the results at boot as a late-init
-call.
-
-(3) Using kunit_tool
-~~~~~~~~~~~~~~~~~~~~~
-
-With ``CONFIG_KUNIT`` and ``CONFIG_KASAN_KUNIT_TEST`` built-in, we can also
-use kunit_tool to see the results of these along with other KUnit
-tests in a more readable way. This will not print the KASAN reports
-of tests that passed. Use `KUnit documentation <https://www.kernel.org/doc/html/latest/dev-tools/kunit/index.html>`_ for more up-to-date
-information on kunit_tool.
+on any architecure that supports KASAN. These and any other KUnit tests enabled
+will run and print the results at boot as a late-init call.
 
-.. _KUnit: https://www.kernel.org/doc/html/latest/dev-tools/kunit/index.html
+3. Using kunit_tool
+~~~~~~~~~~~~~~~~~~~
 
-``CONFIG_TEST_KASAN_MODULE`` is a set of KASAN tests that could not be
-converted to KUnit. These tests can be run only as a module with
-``CONFIG_TEST_KASAN_MODULE`` built as a loadable module and
-``CONFIG_KASAN`` built-in. The type of error expected and the
-function being run is printed before the expression expected to give
-an error. Then the error is printed, if found, and that test
-should be interpreted to pass only if the error was the one expected
-by the test.
+With ``CONFIG_KUNIT`` and ``CONFIG_KASAN_KUNIT_TEST`` built-in, it's also
+possible use ``kunit_tool`` to see the results of these and other KUnit tests
+in a more readable way. This will not print the KASAN reports of the tests that
+passed. Use `KUnit documentation <https://www.kernel.org/doc/html/latest/dev-tools/kunit/index.html>`_
+for more up-to-date information on ``kunit_tool``.
+
+.. _KUnit: https://www.kernel.org/doc/html/latest/dev-tools/kunit/index.html
_

  parent reply	other threads:[~2020-12-22 20:04 UTC|newest]

Thread overview: 62+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-12-22 19:58 incoming Andrew Morton
2020-12-22 20:00 ` [patch 01/60] kasan: drop unnecessary GPL text from comment headers Andrew Morton
2020-12-22 20:00 ` [patch 02/60] kasan: KASAN_VMALLOC depends on KASAN_GENERIC Andrew Morton
2020-12-22 20:00 ` [patch 03/60] kasan: group vmalloc code Andrew Morton
2020-12-22 20:00 ` [patch 04/60] kasan: shadow declarations only for software modes Andrew Morton
2020-12-22 20:00 ` [patch 05/60] kasan: rename (un)poison_shadow to (un)poison_range Andrew Morton
2020-12-22 20:00 ` [patch 06/60] kasan: rename KASAN_SHADOW_* to KASAN_GRANULE_* Andrew Morton
2020-12-22 20:00 ` [patch 07/60] kasan: only build init.c for software modes Andrew Morton
2020-12-22 20:00 ` [patch 08/60] kasan: split out shadow.c from common.c Andrew Morton
2020-12-22 20:00 ` [patch 09/60] kasan: define KASAN_MEMORY_PER_SHADOW_PAGE Andrew Morton
2020-12-22 20:00 ` [patch 10/60] kasan: rename report and tags files Andrew Morton
2020-12-22 20:00 ` [patch 11/60] kasan: don't duplicate config dependencies Andrew Morton
2020-12-22 20:00 ` [patch 12/60] kasan: hide invalid free check implementation Andrew Morton
2020-12-22 20:00 ` [patch 13/60] kasan: decode stack frame only with KASAN_STACK_ENABLE Andrew Morton
2020-12-22 20:00 ` [patch 14/60] kasan, arm64: only init shadow for software modes Andrew Morton
2020-12-22 20:00 ` [patch 15/60] kasan, arm64: only use kasan_depth " Andrew Morton
2020-12-22 20:01 ` [patch 16/60] kasan, arm64: move initialization message Andrew Morton
2020-12-22 20:01 ` [patch 17/60] kasan, arm64: rename kasan_init_tags and mark as __init Andrew Morton
2020-12-22 20:01 ` [patch 18/60] kasan: rename addr_has_shadow to addr_has_metadata Andrew Morton
2020-12-22 20:01 ` [patch 19/60] kasan: rename print_shadow_for_address to print_memory_metadata Andrew Morton
2020-12-22 20:01 ` [patch 20/60] kasan: rename SHADOW layout macros to META Andrew Morton
2020-12-22 20:01 ` [patch 21/60] kasan: separate metadata_fetch_row for each mode Andrew Morton
2020-12-22 20:01 ` [patch 22/60] kasan: introduce CONFIG_KASAN_HW_TAGS Andrew Morton
2020-12-22 20:01 ` [patch 23/60] arm64: enable armv8.5-a asm-arch option Andrew Morton
2020-12-22 20:01 ` [patch 24/60] arm64: mte: add in-kernel MTE helpers Andrew Morton
2020-12-22 20:01 ` [patch 25/60] arm64: mte: reset the page tag in page->flags Andrew Morton
2020-12-22 20:01 ` [patch 26/60] arm64: mte: add in-kernel tag fault handler Andrew Morton
2020-12-22 20:01 ` [patch 27/60] arm64: kasan: allow enabling in-kernel MTE Andrew Morton
2020-12-22 20:01 ` [patch 28/60] arm64: mte: convert gcr_user into an exclude mask Andrew Morton
2020-12-22 20:01 ` [patch 29/60] arm64: mte: switch GCR_EL1 in kernel entry and exit Andrew Morton
2020-12-22 20:01 ` [patch 30/60] kasan, mm: untag page address in free_reserved_area Andrew Morton
2020-12-22 20:01 ` [patch 31/60] arm64: kasan: align allocations for HW_TAGS Andrew Morton
2020-12-22 20:01 ` [patch 32/60] arm64: kasan: add arch layer for memory tagging helpers Andrew Morton
2020-12-22 20:01 ` [patch 33/60] kasan: define KASAN_GRANULE_SIZE for HW_TAGS Andrew Morton
2020-12-22 20:02 ` [patch 34/60] kasan, x86, s390: update undef CONFIG_KASAN Andrew Morton
2020-12-22 20:02 ` [patch 35/60] kasan, arm64: expand CONFIG_KASAN checks Andrew Morton
2020-12-22 20:02 ` [patch 36/60] kasan, arm64: implement HW_TAGS runtime Andrew Morton
2020-12-22 20:02 ` [patch 37/60] kasan, arm64: print report from tag fault handler Andrew Morton
2020-12-22 20:02 ` [patch 38/60] kasan, mm: reset tags when accessing metadata Andrew Morton
2020-12-22 20:02 ` [patch 39/60] kasan, arm64: enable CONFIG_KASAN_HW_TAGS Andrew Morton
2020-12-22 20:02 ` [patch 40/60] kasan: add documentation for hardware tag-based mode Andrew Morton
2020-12-22 20:02 ` [patch 41/60] kselftest/arm64: check GCR_EL1 after context switch Andrew Morton
2020-12-22 20:02 ` [patch 42/60] kasan: simplify quarantine_put call site Andrew Morton
2020-12-22 20:02 ` [patch 43/60] kasan: rename get_alloc/free_info Andrew Morton
2020-12-22 20:02 ` [patch 44/60] kasan: introduce set_alloc_info Andrew Morton
2020-12-22 20:02 ` [patch 45/60] kasan, arm64: unpoison stack only with CONFIG_KASAN_STACK Andrew Morton
2020-12-22 20:02 ` [patch 46/60] kasan: allow VMAP_STACK for HW_TAGS mode Andrew Morton
2020-12-22 20:02 ` [patch 47/60] kasan: remove __kasan_unpoison_stack Andrew Morton
2020-12-22 20:02 ` [patch 48/60] kasan: inline kasan_reset_tag for tag-based modes Andrew Morton
2020-12-22 20:02 ` [patch 49/60] kasan: inline random_tag for HW_TAGS Andrew Morton
2020-12-22 20:02 ` [patch 50/60] kasan: open-code kasan_unpoison_slab Andrew Morton
2020-12-22 20:03 ` [patch 51/60] kasan: inline (un)poison_range and check_invalid_free Andrew Morton
2020-12-22 20:03 ` [patch 52/60] kasan: add and integrate kasan boot parameters Andrew Morton
2020-12-22 20:03 ` [patch 53/60] kasan, mm: check kasan_enabled in annotations Andrew Morton
2020-12-22 20:03 ` [patch 54/60] kasan, mm: rename kasan_poison_kfree Andrew Morton
2020-12-22 20:03 ` [patch 55/60] kasan: don't round_up too much Andrew Morton
2020-12-22 20:03 ` [patch 56/60] kasan: simplify assign_tag and set_tag calls Andrew Morton
2020-12-22 20:03 ` [patch 57/60] kasan: clarify comment in __kasan_kfree_large Andrew Morton
2020-12-22 20:03 ` [patch 58/60] kasan: sanitize objects when metadata doesn't fit Andrew Morton
2020-12-22 20:03 ` [patch 59/60] kasan, mm: allow cache merging with no metadata Andrew Morton
2020-12-22 20:03 ` Andrew Morton [this message]
2020-12-22 21:43 ` incoming Linus Torvalds

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201222200335.RHdnHDjIK%akpm@linux-foundation.org \
    --to=akpm@linux-foundation.org \
    --cc=Branislav.Rankov@arm.com \
    --cc=andreyknvl@google.com \
    --cc=aryabinin@virtuozzo.com \
    --cc=catalin.marinas@arm.com \
    --cc=dvyukov@google.com \
    --cc=elver@google.com \
    --cc=eugenis@google.com \
    --cc=glider@google.com \
    --cc=gor@linux.ibm.com \
    --cc=kevin.brodsky@arm.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=mm-commits@vger.kernel.org \
    --cc=torvalds@linux-foundation.org \
    --cc=vincenzo.frascino@arm.com \
    --cc=will.deacon@arm.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).