Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp3410758pxx; Mon, 2 Nov 2020 08:10:49 -0800 (PST) X-Google-Smtp-Source: ABdhPJxQLxJeEq0vxiVWqQ2vmMeFrv50yeA5ukdiEpzqcVJyFA7Ctx4lPZD2Q6fExDdLUUkasJN7 X-Received: by 2002:a17:906:14d:: with SMTP id 13mr15761923ejh.516.1604333449525; Mon, 02 Nov 2020 08:10:49 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1604333449; cv=none; d=google.com; s=arc-20160816; b=PpEb5p+ovQoqNTAD8YN25Y8Vn6N+so3MaWCwbKHp1SiaSD9epZQNu1MLD0tKz3f1xl HKqvraEU4mn9KNXIVmcwbsLwdOO/rsHl8aYzVkoXZjMc8RHAW6tnNX5UhTGoCpOvuotf H9AtVYv1Vr3rvoORDCf6xgUj2dUrfQVbplm8+hdRrgJpIx8+JvuFWMM98Bynca+80Cbx tFXXYpcosOK+8hT0iDLhdizB3VUfdv/LrYppNohsJpoEiNvSn8KZh5IBeTZpU6f2oz3e rviZgB62Czeyxz3Mw6HIILhZsLOI0L5eVqYSIRko3kEeDdIxEb83YWfkGZjG7SnqhpN6 PG6A== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:from:subject:references:mime-version :message-id:in-reply-to:date:sender:dkim-signature; bh=m61njPxPjXbU7KpGf2VpyrS3MAG/f/HeOixCOkKzSfs=; b=0rot/BhLtk/sWjmL7pi9jF9AZ1+WWZoGBSx87oODpHYuwTdxatXc3Uwv4VvADgpG4t tzOyQOKTMDS8B7CJcy8WyU2aaCULMBJlDc3TiNTiaxq3i3754hMfcr4Kk5EPkZnYU/kX MOU8IIgeWPfbcd43Z6tEs7K4/wD5tqNA1Cxe8VOQXjHTBSkXELq4GIsVIsaPhjxX9BrB f2f6zXsp+m+zq8qreYUQP2iHj8alOphJeVxL9295bK0JWaHCsOyio7ilHBp1soI59shY nW1j7fQ1fj5tUhHkyQBbLDjI8LVFSGEKpvyCDcMf2ldGkVX5G9Xgzkni8yPnAGgGLy0P Tdhw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=SPOwEKxi; 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 j23si7107416ejc.46.2020.11.02.08.10.26; Mon, 02 Nov 2020 08:10:49 -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=SPOwEKxi; 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 S1727248AbgKBQGI (ORCPT + 99 others); Mon, 2 Nov 2020 11:06:08 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35722 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727221AbgKBQGE (ORCPT ); Mon, 2 Nov 2020 11:06:04 -0500 Received: from mail-qt1-x84a.google.com (mail-qt1-x84a.google.com [IPv6:2607:f8b0:4864:20::84a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 241BDC061A04 for ; Mon, 2 Nov 2020 08:06:04 -0800 (PST) Received: by mail-qt1-x84a.google.com with SMTP id 22so4799758qtp.9 for ; Mon, 02 Nov 2020 08:06:04 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=m61njPxPjXbU7KpGf2VpyrS3MAG/f/HeOixCOkKzSfs=; b=SPOwEKxi7i6YmqgIt94F/fOGljSOVbnFEBci14y2Va3FRfm3+e4oHujubT3nxiG9Jh zAZO6JBhCO2bXxgev4Sd6LOxNPyqAMS9DOfJh5VMzRdOGMaDpkf/n8mCSP+DvZepBAP/ eBh7VQgne8yPvefBu0cog23Bg49woqgXArXApRCZb+Cx/OdJBbopaAXoktn9SG6uvV6J DA3KfuyTpRPu4iyJ+cUOXGsjHBhgmiTUijAiwuPqgULvUPcG6806coik2gpwol6BoBYU Q+C6M47acQ5HrP9KtfhJSt0+WjJXGqGYskOtj9QMSvLRmq+jUKK1WRF9LpKKOnGUYR7l tNRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=m61njPxPjXbU7KpGf2VpyrS3MAG/f/HeOixCOkKzSfs=; b=n0okLJTxtPRLlmKWqzbX3VLNvX9jpbsfuQUavmqiyNnb2rCCzoPOygzWqAsUJTYRV/ SSXMV6XewFOWgNGGWq8tI8wFWl0gqZULkIKfLmsCgrBQ5GQn6XDalp1A20tI9BSo5xfL 60zOw5e+r99glKfmuttMaRcrHMRpEW1ApExc1nZJKNvKEi0RYmFj9wTE+9fgNeZTCy+Z yD/rdRq6nvydt7pTsmnrXDdkFAkI5C8+MyXi5ZNyx/ro4azyMuG8qI+6aNva7U8cyxZ2 YcyY5QQCyVvrVx4bS/R31Uv5zdG9HHbf5e4CNfkSEyEQH2u3jIKnG533gqwpTHZEphM/ gziw== X-Gm-Message-State: AOAM533gg7T6wX5v+yX2IjFpR15ojRjP9BWs7H+pxTOpoYvljNGh/F0w fuCuO+QntsAxfQB7Otu2TKAWt5FK+CTFOoJG Sender: "andreyknvl via sendgmr" X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:a0c:b525:: with SMTP id d37mr22911256qve.31.1604333163190; Mon, 02 Nov 2020 08:06:03 -0800 (PST) Date: Mon, 2 Nov 2020 17:04:20 +0100 In-Reply-To: Message-Id: <2948a9756e2659c5a5e9e94ad7519a9b9c88ed85.1604333009.git.andreyknvl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.29.1.341.ge80a0c044ae-goog Subject: [PATCH v7 40/41] kasan: add documentation for hardware tag-based mode From: Andrey Konovalov To: Catalin Marinas , Will Deacon Cc: Vincenzo Frascino , kasan-dev@googlegroups.com, Dmitry Vyukov , Andrey Ryabinin , Alexander Potapenko , Marco Elver , Evgenii Stepanov , Elena Petrova , Branislav Rankov , Kevin Brodsky , 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" 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 Signed-off-by: Vincenzo Frascino Reviewed-by: Marco Elver --- Change-Id: Ib46cb444cfdee44054628940a82f5139e10d0258 --- Documentation/dev-tools/kasan.rst | 78 ++++++++++++++++++++++--------- 1 file changed, 57 insertions(+), 21 deletions(-) diff --git a/Documentation/dev-tools/kasan.rst b/Documentation/dev-tools/kasan.rst index b6db715830f9..5bfafecfc033 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. Any supported Clang version is compatible, 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. 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,24 @@ 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 +Software tag-based KASAN requires software memory tagging support in the form +of HWASan-like compiler instrumentation (see HWASan documentation for details). + +Software tag-based KASAN is currently only implemented for arm64 architecture. + +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 memory). -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. +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. + 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 +226,34 @@ 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 through +pointers with 0xFF pointer tag aren't checked). The value 0xFE is currently +reserved to tag freed memory regions. + +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 currently only implemented for arm64 architecture +and 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 through +pointers with 0xFF pointer tag aren't checked). The value 0xFE is currently +reserved to tag freed memory regions. + +Hardware tag-based KASAN currently only supports tagging of slab memory. What memory accesses are sanitised by KASAN? -------------------------------------------- -- 2.29.1.341.ge80a0c044ae-goog