Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp533016pxa; Fri, 14 Aug 2020 10:32:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxcAEE/dEYhunA0aXt0eXl5mlU5psZSfhbY0ughYEPtEyqC1nH/xFP7YcPfnYJyKqsGLw3n X-Received: by 2002:a17:906:924b:: with SMTP id c11mr3584807ejx.75.1597426374740; Fri, 14 Aug 2020 10:32:54 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1597426374; cv=none; d=google.com; s=arc-20160816; b=wVtKdrjFeq53sj6uS+p54AFDIOF1tFcnHhilbEavDaTkK+/Powd4SDXTOWW9yIQpXR /YhDjdazC7B3mSu4oMl1IyHw+gE0ZQ6w8f1jCWl6mKXZV/z3r1PZAMVr+ShFI6Dt5x/l AmqBsX2+1LtDvQKYGyFYp5/FpFWIMzEThfmPNlUnHjEr5AxrukIyGAy2ztl4VwsMoLcw uD+eW0c/Upqu1NFmDIl/0U+wraW1/iYYM7DGNPKktGZkiZ0nihUqB8bPFj3RodR64Lft BnI/qmKYb9weijSgWMWSvdEvXg5kBL4Mr+0x7lsL1vQfuQNWKscjUGnzTlVAw5qoGEGL 7Icg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:from:subject:references :mime-version:message-id:in-reply-to:date:dkim-signature; bh=ZNAU/quOyDd65Y8FbbPHdMn2fE8Roa22GmWzXnVDZak=; b=jrPqg2t842//++P98gu4FAtVOxZIy8GGxxJmrcAMk2/HaUfUMvUtgTfG48H5qnIYJF UQU6C/OPbU5jZm66yDfRvGaTTEKQoXmZe2An0N+iWWOe67n4cRXy4QU0LAWrhJVzG9Po 4lkuSGWnX3XlHD0zu9t/rmb/k8Bh8devvm82ZKR8i2iEx8SbG8pKccmnqW2weh45YXSJ fiuQjtwaJNEJPA3I+1ccohvKcbTbiXD8KAv2RFBJq6wKYbEmCeHQOR+ryZJL6QD9+QEk 0nqFZtuH3mGMjWCKWflXLzeubIe1fmKfpSN3tUcGUSz8oKlNIxxcTyCVCxo7DgYdd6+h u7pA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=VpPdM4Kj; 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 w27si5475964eja.225.2020.08.14.10.32.31; Fri, 14 Aug 2020 10:32:54 -0700 (PDT) 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=VpPdM4Kj; 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 S1728864AbgHNR3C (ORCPT + 99 others); Fri, 14 Aug 2020 13:29:02 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37656 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728853AbgHNR2r (ORCPT ); Fri, 14 Aug 2020 13:28:47 -0400 Received: from mail-wr1-x449.google.com (mail-wr1-x449.google.com [IPv6:2a00:1450:4864:20::449]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 61D6DC061385 for ; Fri, 14 Aug 2020 10:28:47 -0700 (PDT) Received: by mail-wr1-x449.google.com with SMTP id b8so3618834wrr.2 for ; Fri, 14 Aug 2020 10:28:47 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to :cc; bh=ZNAU/quOyDd65Y8FbbPHdMn2fE8Roa22GmWzXnVDZak=; b=VpPdM4KjSqSmi15WeszefkGFIv0AyTX0aHnAZL6iexBFZD0v86l5iCux6uY1gkgnfs ClcC0AVbghTzetFwaDUUh+9i4JVbBqkuhu4WZ9khBNrrsUUITZMejQr9xUPp8ei9ASJY hzXxWtzTJSqOep3y4/sJXpo1rCCxpmSyQFHS499mnp9fq6JWyWYXZrBn+9atF2VwImqf 5DinyUih8C2eUEjwXAtcahxIh0w0ll0zPTahMEeEY7SmvR59hNooOyoPEOkIS391v3Jx A8HU0xnEe5UoZ8Za37Ia0/M/z9+iYtsR6UANiQw6Sw7vzTXZ591Wf6i7Yc+fdbiCiEq+ TfQw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=ZNAU/quOyDd65Y8FbbPHdMn2fE8Roa22GmWzXnVDZak=; b=XMJnsYvRdzVWFVaJkxZj/LmJQWpNdW8H8T61AQFr+DqSandb4nyUdMm1e4iykn+q16 M742MwLhwtNQWoKk8jK/QHR/KNDlkt3RZi2ruY7arT3tfvVDGtdPIvUf687V0ibAwNDM INUE3Pui6pKIVUk0rDqN3W33FUSYCLiyDXCUqPPjNItdehCPWhMmh/ZoyQ9reZyIN8Xm 9+z3Mw9v00bC17zehKkaKy0iFfsZN20MF5hm7frmOP40+Sv9ca/AvQCUqZ/ZOcZPYPUS vGOUwD/rfb9/PQYO1X/xWPMqmux4LjO3sPp4c6xYqRyipC1Mm0qgx/v+YXpgSo/gFxKF KgoA== X-Gm-Message-State: AOAM531cMBH8S5433WPG49onAPa1n/Y7xNvghiI+d/RaroaYd2bGF0BP xEnvlG+5BAUehH66Gf9T4aqAKBIt/88dSIMZ X-Received: by 2002:a1c:f416:: with SMTP id z22mr3308371wma.62.1597426126049; Fri, 14 Aug 2020 10:28:46 -0700 (PDT) Date: Fri, 14 Aug 2020 19:27:17 +0200 In-Reply-To: Message-Id: <5d0f3c0ee55c58ffa9f58bdea6fa6bf4f6f973a4.1597425745.git.andreyknvl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.28.0.220.ged08abb693-goog Subject: [PATCH 35/35] kasan: add documentation for hardware tag-based mode From: Andrey Konovalov To: Dmitry Vyukov , Vincenzo Frascino , Catalin Marinas , kasan-dev@googlegroups.com Cc: Andrey Ryabinin , Alexander Potapenko , Marco Elver , Evgenii Stepanov , Elena Petrova , Branislav Rankov , Kevin Brodsky , Will Deacon , Andrew Morton , linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Add documentation for hardware tag-based KASAN mode and also add some clarifications for software tag-based mode. Signed-off-by: Andrey Konovalov --- Documentation/dev-tools/kasan.rst | 73 +++++++++++++++++++++---------- 1 file changed, 51 insertions(+), 22 deletions(-) diff --git a/Documentation/dev-tools/kasan.rst b/Documentation/dev-tools/kasan.rst index a3030fc6afe5..aeed89d6eaf5 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 designed 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 userspace -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 that. +Software KASAN modes (1 and 2) use compile-time instrumentation to insert +validity checks before every memory access, and therefore require a compiler +version that supports that. Generic KASAN is supported in both GCC and Clang. With GCC it requires version 8.3.0 or later. With Clang it requires version 7.0.0 or later, but detection of @@ -19,7 +21,7 @@ out-of-bounds accesses for global variables is only supported since Clang 11. Tag-based KASAN is only supported in Clang and requires version 7.0.0 or later. Currently generic KASAN is supported for the x86_64, arm64, xtensa, s390 and -riscv architectures, and tag-based KASAN is supported only for arm64. +riscv architectures, and tag-based KASAN modes are supported only for arm64. Usage ----- @@ -28,14 +30,16 @@ To enable KASAN configure kernel with:: CONFIG_KASAN = 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_INLINE. -Outline and inline are compiler instrumentation types. The former produces -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 types. +The former produces smaller binary while the latter is 1.1 - 2 times faster. -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 physical page, @@ -196,17 +200,20 @@ and the second to last. Software tag-based KASAN ~~~~~~~~~~~~~~~~~~~~~~~~ -Tag-based KASAN uses the Top Byte Ignore (TBI) feature of modern 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 memory). +Software tag-based KASAN uses the Top Byte Ignore (TBI) feature of modern 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 +memory). + +On each memory allocation software tag-based KASAN generates a random tag, tags +the allocated memory with this tag, and embeds this tag into the returned +pointer. -On each memory allocation tag-based KASAN generates a random tag, tags the -allocated memory with this tag, and embeds this tag into the returned pointer. Software tag-based KASAN uses compile-time instrumentation to insert checks 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, that emits callbacks to check memory accesses; and inline, that performs the shadow @@ -215,9 +222,31 @@ simply printed from the function that performs the access check. With inline instrumentation a brk instruction is emitted by the compiler, and a dedicated brk handler is used to print bug reports. -A potential expansion of this mode is a hardware tag-based mode, which would -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 aren't +checked). + +Software tag-based KASAN currently only supports tagging of slab 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 based on both arm64 Memory Tagging Extension (MTE) +introduced in ARMv8.5 Instruction Set Architecture, and Top Byte Ignore (TBI). + +Special arm64 instructions are used to assign memory tags for each allocation. +Same tags are assigned to pointers to those allocations. On every memory +access, hardware makes 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 a fault is generated and a report is printed. + +Hardware tag-based KASAN uses 0xFF as a match-all pointer tag (accesses aren't +checked). + +Hardware tag-based KASAN currently only supports tagging of slab memory. What memory accesses are sanitised by KASAN? -------------------------------------------- -- 2.28.0.220.ged08abb693-goog