From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-23.2 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_IN_DEF_DKIM_WL autolearn=unavailable autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 859D6C63777 for ; Wed, 18 Nov 2020 15:43:32 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id C5A0524790 for ; Wed, 18 Nov 2020 15:43:31 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="o2elT4rI" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C5A0524790 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id C49CB6B0070; Wed, 18 Nov 2020 10:43:30 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id BD01D6B0072; Wed, 18 Nov 2020 10:43:30 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id A22596B0073; Wed, 18 Nov 2020 10:43:30 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0061.hostedemail.com [216.40.44.61]) by kanga.kvack.org (Postfix) with ESMTP id 633B56B0070 for ; Wed, 18 Nov 2020 10:43:30 -0500 (EST) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id 050B63638 for ; Wed, 18 Nov 2020 15:43:30 +0000 (UTC) X-FDA: 77497958580.12.lift21_0a0f9302733b Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin12.hostedemail.com (Postfix) with ESMTP id D6B961803890E for ; Wed, 18 Nov 2020 15:43:29 +0000 (UTC) X-HE-Tag: lift21_0a0f9302733b X-Filterd-Recvd-Size: 11349 Received: from mail-qv1-f65.google.com (mail-qv1-f65.google.com [209.85.219.65]) by imf07.hostedemail.com (Postfix) with ESMTP for ; Wed, 18 Nov 2020 15:43:29 +0000 (UTC) Received: by mail-qv1-f65.google.com with SMTP id r12so1181168qvq.13 for ; Wed, 18 Nov 2020 07:43:29 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=ii8uMwZ11exGNZ0QuXQOuB3dAaW5Ak4GBCBwnjIahGo=; b=o2elT4rINyVBV6BS5aB0ifPHqNX9mYDaobsn69M6IaY1usexvUYK9bF6WDGHa47c2w HDuxyZnJdgFGCf45cUPY0MSima5dqX0FOH97IGhewfnyI2G3/6HKsVx4AkHi6WAtXYX1 p9YUAEKIv/LWnOlu+IgcKCGOg6SYSNNoVCJ4BWDPTa4moU/RaVFPSXraHeP3bZyh1V// R1GJwaDbUX2LiwOOWLuDc3Q/F5M82/WgorGO12TnPxti1RVItz6+ZNKcT7kcrmQQMNCF RX0ygEcIF6fGaNWJzvA6uSaj4RLdNmm+TLM/jcc4c0ZAjLsau4tijF9aaHjHl+0b98Io 5lTg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=ii8uMwZ11exGNZ0QuXQOuB3dAaW5Ak4GBCBwnjIahGo=; b=FwwKeE4MQgSt94cum68F4B8VJ3B4kHCZ50EOE79d3hLX2sDkvyYRrc1KGynstcGZ9b MX4bkgS3J6xEGV4qWwMBywDvToWR6E3Q1YT41C/mru3nZNKmmlXfqUAeztQdR9C1Vsxv 3K9r+lldt0OKEq/16MwDkgUxlkFnp0HBjcgGHRPPrU1NIayHmg1rAenyzo85bPErmKI7 +Q0xiuxZnJ3k7mRfosBSneB8cIrssYc9tSqb7T0IWx+MhV6Gx1/nprYyeCixNL2duxod T8JfW67gx9dubqoes0ge4BtPbYg9RqxvH7Nn/ucJ1RpNlJQNlHwNeyEmnpoMgRKs8Pg3 ZcwA== X-Gm-Message-State: AOAM5306rS+3jb322P49x9gJKZrdnJjH1PYm1Y9M/LcBcBW8Mc/ABhDI /kryhVS3rWEFwX0eXN5c/RJoBjlYLWxdW+02DZ1eKw== X-Google-Smtp-Source: ABdhPJxLmAnSOWQI2Qf7LFLVct0WnciQlBPqR78aT5f710dElggYdwcvRDEM1AAuM9lbZrNxAZg3O4Ws6bHAYu910jk= X-Received: by 2002:a0c:9e53:: with SMTP id z19mr5380322qve.23.1605714207999; Wed, 18 Nov 2020 07:43:27 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Alexander Potapenko Date: Wed, 18 Nov 2020 16:43:16 +0100 Message-ID: Subject: Re: [PATCH mm v10 41/42] kasan: add documentation for hardware tag-based mode To: Andrey Konovalov Cc: Andrew Morton , Catalin Marinas , Will Deacon , Vincenzo Frascino , Dmitry Vyukov , Andrey Ryabinin , Marco Elver , Evgenii Stepanov , Branislav Rankov , Kevin Brodsky , kasan-dev , Linux ARM , Linux Memory Management List , LKML Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: On Fri, Nov 13, 2020 at 11:17 PM Andrey Konovalov w= rote: > > Add documentation for hardware tag-based KASAN mode and also add some > clarifications for software tag-based mode. > > Signed-off-by: Andrey Konovalov > Signed-off-by: Vincenzo Frascino > Reviewed-by: Marco Elver Reviewed-by: Alexander Potapenko > --- > Change-Id: Ib46cb444cfdee44054628940a82f5139e10d0258 > --- > Documentation/dev-tools/kasan.rst | 80 +++++++++++++++++++++++-------- > 1 file changed, 59 insertions(+), 21 deletions(-) > > diff --git a/Documentation/dev-tools/kasan.rst b/Documentation/dev-tools/= kasan.rst > index 2d55d788971c..ffbae8ce5748 100644 > --- a/Documentation/dev-tools/kasan.rst > +++ b/Documentation/dev-tools/kasan.rst > @@ -5,12 +5,14 @@ Overview > -------- > > KernelAddressSANitizer (KASAN) is a dynamic memory error detector design= ed to > -find out-of-bound and use-after-free bugs. KASAN has two modes: generic = KASAN > -(similar to userspace ASan) and software tag-based KASAN (similar to use= rspace > -HWASan). > +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). > > -KASAN uses compile-time instrumentation to insert validity checks before= every > -memory access, and therefore requires a compiler version that supports t= hat. > +Software KASAN modes (1 and 2) use compile-time instrumentation to inser= t > +validity checks before every memory access, and therefore require a comp= iler > +version that supports that. > > Generic KASAN is supported in both GCC and Clang. With GCC it requires v= ersion > 8.3.0 or later. Any supported Clang version is compatible, but detection= of > @@ -19,7 +21,7 @@ out-of-bounds accesses for global variables is only sup= ported since Clang 11. > Tag-based KASAN is only supported in Clang. > > Currently generic KASAN is supported for the x86_64, arm, arm64, xtensa,= s390 > -and riscv architectures, and tag-based KASAN is supported only for arm64= . > +and riscv architectures, and tag-based KASAN modes are supported only fo= r arm64. > > Usage > ----- > @@ -28,14 +30,16 @@ To enable KASAN configure kernel with:: > > CONFIG_KASAN =3D y > > -and choose between CONFIG_KASAN_GENERIC (to enable generic KASAN) and > -CONFIG_KASAN_SW_TAGS (to enable software tag-based KASAN). > +and choose between CONFIG_KASAN_GENERIC (to enable generic KASAN), > +CONFIG_KASAN_SW_TAGS (to enable software tag-based KASAN), and > +CONFIG_KASAN_HW_TAGS (to enable hardware tag-based KASAN). > > -You also need to choose between CONFIG_KASAN_OUTLINE and CONFIG_KASAN_IN= LINE. > -Outline and inline are compiler instrumentation types. The former produc= es > -smaller binary while the latter is 1.1 - 2 times faster. > +For software modes, you also need to choose between CONFIG_KASAN_OUTLINE= and > +CONFIG_KASAN_INLINE. Outline and inline are compiler instrumentation typ= es. > +The former produces smaller binary while the latter is 1.1 - 2 times fas= ter. > > -Both KASAN modes work with both SLUB and SLAB memory allocators. > +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. > > To augment reports with last allocation and freeing stack of the physica= l page, > @@ -196,17 +200,24 @@ and the second to last. > Software tag-based KASAN > ~~~~~~~~~~~~~~~~~~~~~~~~ > > -Tag-based KASAN uses the Top Byte Ignore (TBI) feature of modern arm64 C= PUs to > -store a pointer tag in the top byte of kernel pointers. Like generic KAS= AN it > -uses shadow memory to store memory tags associated with each 16-byte mem= ory > +Software tag-based KASAN requires software memory tagging support in the= form > +of HWASan-like compiler instrumentation (see HWASan documentation for de= tails). > + > +Software tag-based KASAN is currently only implemented for arm64 archite= cture. > + > +Software tag-based KASAN uses the Top Byte Ignore (TBI) feature of arm64= CPUs > +to store a pointer tag in the top byte of kernel pointers. Like generic = KASAN > +it uses shadow memory to store memory tags associated with each 16-byte = memory > cell (therefore it dedicates 1/16th of the kernel memory for shadow memo= ry). > > -On each memory allocation tag-based KASAN generates a random tag, tags t= he > -allocated memory with this tag, and embeds this tag into the returned po= inter. > +On each memory allocation software tag-based KASAN generates a random ta= g, tags > +the allocated memory with this tag, and embeds this tag into the returne= d > +pointer. > + > Software tag-based KASAN uses compile-time instrumentation to insert che= cks > before each memory access. These checks make sure that tag of the memory= that > is being accessed is equal to tag of the pointer that is used to access = this > -memory. In case of a tag mismatch tag-based KASAN prints a bug report. > +memory. In case of a tag mismatch software tag-based KASAN prints a bug = report. > > Software tag-based KASAN also has two instrumentation modes (outline, th= at > emits callbacks to check memory accesses; and inline, that performs the = shadow > @@ -215,9 +226,36 @@ simply printed from the function that performs the a= ccess check. With inline > instrumentation a brk instruction is emitted by the compiler, and a dedi= cated > brk handler is used to print bug reports. > > -A potential expansion of this mode is a hardware tag-based mode, which w= ould > -use hardware memory tagging support instead of compiler instrumentation = and > -manual shadow memory manipulation. > +Software tag-based KASAN uses 0xFF as a match-all pointer tag (accesses = through > +pointers with 0xFF pointer tag aren't checked). The value 0xFE is curren= tly > +reserved to tag freed memory regions. > + > +Software tag-based KASAN currently only supports tagging of > +kmem_cache_alloc/kmalloc and page_alloc memory. > + > +Hardware tag-based KASAN > +~~~~~~~~~~~~~~~~~~~~~~~~ > + > +Hardware tag-based KASAN is similar to the software mode in concept, but= uses > +hardware memory tagging support instead of compiler instrumentation and > +shadow memory. > + > +Hardware tag-based KASAN is currently only implemented for arm64 archite= cture > +and based on both arm64 Memory Tagging Extension (MTE) introduced in ARM= v8.5 > +Instruction Set Architecture, and Top Byte Ignore (TBI). > + > +Special arm64 instructions are used to assign memory tags for each alloc= ation. > +Same tags are assigned to pointers to those allocations. On every memory > +access, hardware makes sure that tag of the memory that is being accesse= d is > +equal to tag of the pointer that is used to access this memory. In case = of a > +tag mismatch a fault is generated and a report is printed. > + > +Hardware tag-based KASAN uses 0xFF as a match-all pointer tag (accesses = through > +pointers with 0xFF pointer tag aren't checked). The value 0xFE is curren= tly > +reserved to tag freed memory regions. > + > +Hardware tag-based KASAN currently only supports tagging of > +kmem_cache_alloc/kmalloc and page_alloc memory. > > What memory accesses are sanitised by KASAN? > -------------------------------------------- > -- > 2.29.2.299.gdc1121823c-goog > --=20 Alexander Potapenko Software Engineer Google Germany GmbH Erika-Mann-Stra=C3=9Fe, 33 80636 M=C3=BCnchen Gesch=C3=A4ftsf=C3=BChrer: Paul Manicle, Halimah DeLaine Prado Registergericht und -nummer: Hamburg, HRB 86891 Sitz der Gesellschaft: Hamburg