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 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 2A713C4725D for ; Mon, 22 Jan 2024 04:49:48 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 5339E6B0071; Sun, 21 Jan 2024 23:49:47 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 4E1B46B0072; Sun, 21 Jan 2024 23:49:47 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3A9526B0074; Sun, 21 Jan 2024 23:49:47 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 2901E6B0071 for ; Sun, 21 Jan 2024 23:49:47 -0500 (EST) Received: from smtpin24.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id 9D2CB1C09F2 for ; Mon, 22 Jan 2024 04:49:46 +0000 (UTC) X-FDA: 81705719172.24.E8CD9D6 Received: from mail-ed1-f50.google.com (mail-ed1-f50.google.com [209.85.208.50]) by imf17.hostedemail.com (Postfix) with ESMTP id D7A064000D for ; Mon, 22 Jan 2024 04:49:44 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=k8ADnPa8; spf=pass (imf17.hostedemail.com: domain of dvyukov@google.com designates 209.85.208.50 as permitted sender) smtp.mailfrom=dvyukov@google.com; dmarc=pass (policy=reject) header.from=google.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1705898984; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-type:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=v7lHM4FRzCJbagQaJUuF7i1/f1Gd/G/CT3X864K81zY=; b=t7p/xYdkNYDeLeN5m+A4KqgELdwvxzvcSPpYx0yIiIgD/k1F1JujIdckoWHmCfktaDO3OQ kuMJvks53S1ixRTklvkbOS7qggm0QrUH9j0NWOMSi9gRiszj/Itphqh3Q+eGTDbBqPXGL3 edFJvN2YOz9mx/GUCjUKBMrZ/nuvF5c= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1705898984; a=rsa-sha256; cv=none; b=cIxynGkiyf4+pzdtPEGts4ztn5Vu957+sA/D6uY+p6c347OAAhI+6LB+6LjZPfJ0kBIVzi x/U/kO5Oc1RxqVDSE5b0dyTQFPyGVUwBgeRHEN5LrbgQVj1JcH7NAeZWWsqqXTo5YrBL6p N/KRStvYqkh8yndfOsc5ZuvzolvPTMc= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=google.com header.s=20230601 header.b=k8ADnPa8; spf=pass (imf17.hostedemail.com: domain of dvyukov@google.com designates 209.85.208.50 as permitted sender) smtp.mailfrom=dvyukov@google.com; dmarc=pass (policy=reject) header.from=google.com Received: by mail-ed1-f50.google.com with SMTP id 4fb4d7f45d1cf-55a62fad701so7659a12.1 for ; Sun, 21 Jan 2024 20:49:44 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20230601; t=1705898983; x=1706503783; darn=kvack.org; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:from:to:cc:subject:date:message-id:reply-to; bh=v7lHM4FRzCJbagQaJUuF7i1/f1Gd/G/CT3X864K81zY=; b=k8ADnPa8W4CXde28/z6GkvlzTl6URdt8zbfIuX9+Dcijf7U/L29kyKWxPqOsX21Vfl x0IiEyeUB5dUVEL6LZNTqst6dwccu/q/FkQl8els9DGLhkrOs+rHW9MT9qEkbpAoinx/ KNv71Cv8K5KBgnOwIAtZhBMLquW82JSldkfzRcPiHt6U6S15Cc3QTe9cKDLJsUmGaB8r 5wssbSrchqJgHn3ESDe68SMT9TfnsMbpqLyMCQDMapT22oAOXt1JsAKSd45VmWLH4Pys NgtvsmSpFEL8ke1yYsA8nna23mMj/nmX0MNJY7SOLQqdeUKDgxzvc5UNuYl4nz+/jXZB UO6w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705898983; x=1706503783; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=v7lHM4FRzCJbagQaJUuF7i1/f1Gd/G/CT3X864K81zY=; b=GSHoLKFkftisSm+mN0R0zn5myOI/DavGB0qKT6wSMu+U+/7bZUZPu3WgYUjVeHeaxS +tSs/lKIgG9Cg0iJ8VCcbAOyesYam0+8H9Sx0GvjRTYVCHAk9AFGRF3GkIRtTI6qa6qK zFZhfzD4X4SBKCnvWl1ZX9ovPOW0HTvMXcIhIRAR+8It8rtGMwqzmThU8sHa11KlqP/I VtbdBPoy196MGWjaDlxJk58yvFXW3nbePLEaU/U8j9juvwKxN1bKAEdE13Ek/nMTr0jb NraN+XNOlJOrYvK80Fd+pBWyc6Q+DxEb7+eI5LaqsljphqcM6zSFbgF1x9SDPMY4kxBF FzeA== X-Gm-Message-State: AOJu0YxCQwMa+byjYre9yJS3W7fVJ8OyLkI7ba3VXoB2/yPWej66zYIF o46w1oTNal01KJHz9xziht13suBRru6cqtiyYnHQn8nlQzIh X-Google-Smtp-Source: AGHT+IFH0YCz7B8wGiwUV1TD6BK6CIn1d1gdYhInunXPUPjN2s24rXTe9yo8YSaxLitkpBXWBTXVQx7KNvcvWPFnIQY= X-Received: by 2002:a05:6402:b50:b0:55c:5f2a:6091 with SMTP id bx16-20020a0564020b5000b0055c5f2a6091mr27edb.4.1705898983098; Sun, 21 Jan 2024 20:49:43 -0800 (PST) MIME-Version: 1.0 References: <20240118124109.37324-1-lizhe.67@bytedance.com> In-Reply-To: <20240118124109.37324-1-lizhe.67@bytedance.com> From: Dmitry Vyukov Date: Mon, 22 Jan 2024 05:49:29 +0100 Message-ID: Subject: Re: [RFC 0/2] kasan: introduce mem track feature To: lizhe.67@bytedance.com Cc: ryabinin.a.a@gmail.com, glider@google.com, andreyknvl@gmail.com, vincenzo.frascino@arm.com, akpm@linux-foundation.org, kasan-dev@googlegroups.com, linux-mm@kvack.org, lizefan.x@bytedance.com Content-Type: text/plain; charset="UTF-8" X-Rspamd-Queue-Id: D7A064000D X-Rspam-User: X-Stat-Signature: 7mypk1m73wthhg7ochpb4ijy8j7oqcaa X-Rspamd-Server: rspam03 X-HE-Tag: 1705898984-342486 X-HE-Meta: U2FsdGVkX19oIdopL/L3OHKa3R9Pcr7EOtjdOlGPX56DFQqtih/LNF5qaurAbAk64qBPLmQpFpcfxjc5kMtsKtwfpxBhRKkAfcCMyBKAHVJGc6cUmiU6xTh+pJ+d/9mWiG0d5EYOTxGzcPTU/vEj2QBpbm47xPOO1Ez99gW2VOCibzBn1P4M7QnymwZK2nPasaRCNfwpBI7ay2izVI3579Pp9YRh5M9fN62/W085/r211TWYoWLSfiZw4eT4zRuvrE1J55W1xEpxULS1cZSUIQDAx5CNUJ/L2YSPjBAhiqOVlUON1lKyjq1dS/8W45jtBMsOl9oj7Hq1pKkqVXlqzH5NbhH+/l/Ojk0J/LUEtENi1UGVCTSbm9wb4cUNiQ4gLypB99oIRiygsZhBdcrLt6skXqEHbEtysCzpH11APcMKugEn/EXpEZt2fEiIbo1CwBmZ5FpkJZQ82+dwA6rnotw+WoByb5CSFSgc2+Dra5n9F/z2YJGX4qFT60XbFYkKmgAySEAhMqpLqDhIXQyqxR1Zu1InRdy+B4u2qdy87USisCzDKAU1AbzG8yA4kfm4ptbbYWk+2jEIR6bKs8kgIjiaIaeQsREi0PSkgWQMoYITshYAyLYf+abxOeI3o46sBkA62NPrWFKvCJfOx64agLVk6Td2XxGCQIIwKpZpZYEdjh0EJ6AQnlGUGFi9ZGzvujAOLnrTx6He0IZMXbfUhZm00Qi77JwwtFpn10s+r4mga40BM4u3h3G186v0QjsKJQ0z3D8FBFhlqM3y3LJd51cmc7vjZU+ErD4Tk4RTMWMWxuz3u2d3L1pPk0fEAHdWdY3fy2+GnnnC0+peGa39Siy9YnNj2/Y//47+Y4avgTEl6KI1PFwubiX+EeohK8IiRnrOwjIZGKdG++idwy9RWnUC3lZeV7T8oQLnkJ3OERk8UOtnkNbwUovzUaf3+Yc7pNpbpZozIqzMly9Znbg Ew6D7OAq YCSsMgnV+Ha9mt5LLM73klJsQ1E8k0vvsEO3EoICDlEdh2V+Bu70rJjMEcQCs+qNdWyNV6JIMsLRDa3gy6H6ynHn3n7kNZAIevr9e19NyNwIN5WWf1Wh553SWg+gQmhD12FDWhRPPQ72nUjvfFyKommON7SuuvqR7TI/+AwC7jr884Mvx8tjgV9W3O7GIdoT1EY8pt9Qgapgv+Qm8hOc8Uy8W5mKSdUnMniRN0twQlzmtej8r/41LOtxAT6+TJFwV2EEZdjrhwIfysklLGzcoO8W8epPqQoPKNjFUEYVxS06YNfu3b1YA4P/iq3/k7kboVSmPawO0KiFkg2RDGVamo3Z1gkOf6TqquILpWahYdfjRyEFBCa5VaYDxny7LxQ7WcB6VmdSIAsBWSAE= 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: List-Subscribe: List-Unsubscribe: On Thu, 18 Jan 2024 at 13:41, wrote: > > From: Li Zhe > > 1. Problem > ========== > KASAN is a tools for detecting memory bugs like out-of-bounds and > use-after-free. In Generic KASAN mode, it use shadow memory to record > the accessible information of the memory. After we allocate a memory > from kernel, the shadow memory corresponding to this memory will be > marked as accessible. > In our daily development, memory problems often occur. If a task > accidentally modifies memory that does not belong to itself but has > been allocated, some strange phenomena may occur. This kind of problem > brings a lot of trouble to our development, and unluckily, this kind of > problem cannot be captured by KASAN. This is because as long as the > accessible information in shadow memory shows that the corresponding > memory can be accessed, KASAN considers the memory access to be legal. > > 2. Solution > =========== > We solve this problem by introducing mem track feature base on KASAN > with Generic KASAN mode. In the current kernel implementation, we use > bits 0-2 of each shadow memory byte to store how many bytes in the 8 > byte memory corresponding to the shadow memory byte can be accessed. > When a 8-byte-memory is inaccessible, the highest bit of its > corresponding shadow memory value is 1. Therefore, the key idea is that > we can use the currently unused four bits 3-6 in the shadow memory to > record relevant track information. Which means, we can use one bit to > track 2 bytes of memory. If the track bit of the shadow mem corresponding > to a certain memory is 1, it means that the corresponding 2-byte memory > is tracked. By adding this check logic to KASAN's callback function, we > can use KASAN's ability to capture allocated memory corruption. > > 3. Simple usage > =========== > The first step is to mark the memory as tracked after the allocation is > completed. > The second step is to remove the tracked mark of the memory before the > legal access process and re-mark the memory as tracked after finishing > the legal access process. KASAN already has a notion of memory poisoning/unpoisoning. See kasan_unpoison_range function. We don't export kasan_poison_range, but if you do local debuggng, you can export it locally. > The first patch completes the implementation of the mem track, and the > second patch provides an interface for using this facility, as well as > a testcase for the interface. > > Li Zhe (2): > kasan: introduce mem track feature base on kasan > kasan: add mem track interface and its test cases > > include/linux/kasan.h | 5 + > lib/Kconfig.kasan | 9 + > mm/kasan/generic.c | 437 +++++++++++++++++++++++++++++++++-- > mm/kasan/kasan_test_module.c | 26 +++ > mm/kasan/report_generic.c | 6 + > 5 files changed, 467 insertions(+), 16 deletions(-) > > -- > 2.20.1 >