Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp466547pxb; Wed, 11 Nov 2020 08:05:19 -0800 (PST) X-Google-Smtp-Source: ABdhPJyiZpjZmpZ6V690pswGTukexpdR6iiZ530frd8exq2pNXDmH4gAiQn7jIFcOmg+c1/oaUft X-Received: by 2002:a17:906:284b:: with SMTP id s11mr26982902ejc.326.1605110718891; Wed, 11 Nov 2020 08:05:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605110718; cv=none; d=google.com; s=arc-20160816; b=mYIGJVDio+OhpoMfFtqeTfnD7o+vU13U4CN7VGaIe6ZMucaIBf1s8cmgqu1EzhZQQm EBBzZnRbHXSe1aFBYeMwvSNMkZ4vn0N1eD030gSfUw21zzmRXIbCYki7tuvKAJYDeCBR axiSU4cBnq1hvUnKQFlD/lrW7r2XVVY5lQiUiEhmbwjF6ptwzjxuewz+f4bKEQ+hnFIY 3Gjqrs327od8QyxUC0sj49pUQkSIIveOllq4SDvwb+OtCMsFQb8VUbEJvM31iznVoj0x ZpC8IDjIkn1T9WyIzSL3zZQbmgjgydLmVnLB/IcsrmfFOu93Qb6khAujh2+O3rrB+47s SEHw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:user-agent:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=WHzN/ImHcDVytApQWwIk+Zg/aDG+W+p4VjYjJJGfXUc=; b=sii95eMnHZ7lmYtGrHIpL1qr3WNKUrO24iwT3/X6VKxl+BuaJ/ZC9xsGy1GGU+Cd0X jgMi01WOU6DuOXhyMheSD+H1E84uzbqJZ9R5RcB5q9WyDaeb56aBBRL0V1ZO4r6iif/G A28RAEK1GaXm4ym8HcFbaYXo/rIlEmGwKVD+Ff9cGaN40PDuM7XhkZEoTil+aoS52KN1 AwJk4G+rkA3mJ8CitJUYNmYF509T7umbShUDyYwtNoqnIA2xYzS0M5r4CysKw7urMNGJ 3nd+LPdsvVWfTa9YDcjYrDSKqpa1JS9CwsGy1ENsQM4R6QZNqa0/IqwJT5lb2wdRDJG7 hDEA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=B7OCx4lM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y9si1769646ede.591.2020.11.11.08.04.52; Wed, 11 Nov 2020 08:05:18 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=B7OCx4lM; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726837AbgKKQDV (ORCPT + 99 others); Wed, 11 Nov 2020 11:03:21 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49054 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726274AbgKKQDU (ORCPT ); Wed, 11 Nov 2020 11:03:20 -0500 Received: from mail-wm1-x341.google.com (mail-wm1-x341.google.com [IPv6:2a00:1450:4864:20::341]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5789C0613D1 for ; Wed, 11 Nov 2020 08:03:19 -0800 (PST) Received: by mail-wm1-x341.google.com with SMTP id c9so2706938wml.5 for ; Wed, 11 Nov 2020 08:03:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=WHzN/ImHcDVytApQWwIk+Zg/aDG+W+p4VjYjJJGfXUc=; b=B7OCx4lMIuveEwOOakJkaa0CuQvmorU69sfI71CsoCrUWOR+e/4SjXulRTfJ8eCWR3 ItvR3e4GJ3/Qdr0TTLhH3vBB62w2mwuBTrirrOKJ8qdBbLxcepDia3j+zHEzFEOUCfls iICvtLeRNSD2qO89QijOb6JmCjyuWAheDsMZZyyk9Y4003PwHmkQtb5FQeDcamQiqWSK FcaRT0u8J6T6Oi2CsnZBL6yyNovgJXds4uJVf17WFRhm8poXDoQCKY3oksfu4i5WAC3G AiZ0hQS8wG6L0HyKmRgstQte3gHugoI1ozP+n+Yjrnl83Be9bnP7+xOSmXTgLVofVDt5 DfNg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=WHzN/ImHcDVytApQWwIk+Zg/aDG+W+p4VjYjJJGfXUc=; b=KgrTe5xkxAdEXcoZY5U80ZRkMkE/jYZrLuGFMFXHKqb9AcSAfaQDLHWvABk38iWxtr P7eaYZjanLUTC6hLjLkJF6Lw8lRQG0GTXuWB/AldS+ma3wzqhx85fP8dzMC9OJ28EqRA XkjW7Uy3sb+uYF0kk9Fxt9d6xVC7VjYtUJn0SKzUteOVXsshyMF+y77HNwU5lx65IKpa j+yjVfxrP85NT+TyKaNB2gbfwyOSpQrLZd2ofRKRQCO9lIQRIzJ2wVitEb86JtqPqlSF 3YAKEdJWp/PcmGbcNqrWKOiJu2x7gn0/6iLgbRB2KavqcYbboCOmi0T3fy4dmQa6VyiJ n9Mw== X-Gm-Message-State: AOAM531lbgq854BCzlJH/VeinyzGvUBDq2aynURCRd8XaWNYFCb+Yzq1 qEiXAlJuijkI+33aOj+XJFDHsA== X-Received: by 2002:a7b:c3c7:: with SMTP id t7mr5197094wmj.114.1605110597972; Wed, 11 Nov 2020 08:03:17 -0800 (PST) Received: from elver.google.com ([2a00:79e0:15:13:f693:9fff:fef4:2449]) by smtp.gmail.com with ESMTPSA id g4sm1236758wrp.0.2020.11.11.08.03.16 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 11 Nov 2020 08:03:17 -0800 (PST) Date: Wed, 11 Nov 2020 17:03:11 +0100 From: Marco Elver To: Andrey Konovalov Cc: Dmitry Vyukov , Alexander Potapenko , Catalin Marinas , Will Deacon , Vincenzo Frascino , Evgenii Stepanov , Andrey Ryabinin , Branislav Rankov , Kevin Brodsky , Andrew Morton , kasan-dev@googlegroups.com, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v2 20/20] kasan: update documentation Message-ID: <20201111160311.GB517454@elver.google.com> References: <6103d1aaacea96a94ca4632f78bcd801e4fbc9c4.1605046662.git.andreyknvl@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <6103d1aaacea96a94ca4632f78bcd801e4fbc9c4.1605046662.git.andreyknvl@google.com> User-Agent: Mutt/1.14.6 (2020-07-11) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Nov 10, 2020 at 11:20PM +0100, Andrey Konovalov wrote: > 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. > > Signed-off-by: Andrey Konovalov > --- > Documentation/dev-tools/kasan.rst | 180 +++++++++++++++++++----------- > 1 file changed, 113 insertions(+), 67 deletions(-) > > diff --git a/Documentation/dev-tools/kasan.rst b/Documentation/dev-tools/kasan.rst > index 422f8ee1bb17..f2da2b09e5c7 100644 > --- a/Documentation/dev-tools/kasan.rst > +++ b/Documentation/dev-tools/kasan.rst > @@ -6,6 +6,7 @@ Overview > > KernelAddressSANitizer (KASAN) is a dynamic memory error detector designed to s/memory error/memory safety error/ to be precise and consistent with various other docs and literature we have, if you deem it appropriate to change in this patch. > 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 are compiler instrumentation types. > 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,16 +131,20 @@ freed (in case of a use-after-free bug report). Next comes a description of > 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. > @@ -157,6 +152,55 @@ 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). I think ReST automatically creates a link if you write it as ... (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 ``kasam.mode`` boot parameter allows to choose one of three main modes: s/kasam/kasan/ > +- ``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``) This is indented with tabs instead of spaces. > + > +If ``kasan.mode parameter`` is not provided, it defaults to ``full`` when s/``kasan.mode parameter``/``kasan.mode`` parameter/ ? > +``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 > @@ -194,7 +238,10 @@ function calls GCC directly inserts the code to check the shadow memory. > This option significantly enlarges kernel but it gives x1.1-x2 performance > boost over outline instrumented kernel. > > -Generic KASAN prints up to 2 call_rcu() call stacks in reports, the last one > +Generic KASAN is the only mode that delays the reuse of freed object via > +quarantine (see mm/kasan/quarantine.c for implementation). > + > +Generic KASAN prints up to two call_rcu() call stacks in reports, the last one > and the second to last. > > Software tag-based KASAN > @@ -302,15 +349,15 @@ therefore be wasteful. Furthermore, to ensure that different mappings > 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 in to the vmap infrastructure to lazily clean up unused shadow s/in to/into/ > 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. > @@ -321,24 +368,31 @@ architectures that do not have a fixed module region. > 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. > > -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: > +2. Tests that are currently incompatible with Kunit. Enabled with s/Kunit/KUnit/ > +``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. > > -pass:: > +Each KUNIT-compatible KASAN test prints a KASAN report if an error is detected. s/KUNIT/KUnit/ like elsewhere. > +Then the test prints its number and status. > + > +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 > @@ -346,46 +400,38 @@ or, if a KASAN report was expected, but not found:: > 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. s/Kunit/KUnit/ > + > +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 architecure that supports KASAN. These and any other KUnit > -tests enabled will run and print the results at boot as a late-init > -call. > +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. > > -(3) Using kunit_tool > -~~~~~~~~~~~~~~~~~~~~~ > +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 `_ for more up-to-date > -information on kunit_tool. > +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 `_ > +for more up-to-date information on ``kunit_tool``. > > .. _KUnit: https://www.kernel.org/doc/html/latest/dev-tools/kunit/index.html > - > -``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 interpretted to pass only if the error was the one expected > -by the test. > -- > 2.29.2.222.g5d2a92d10f8-goog >