Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp7343147ybi; Mon, 8 Jul 2019 19:55:45 -0700 (PDT) X-Google-Smtp-Source: APXvYqzRivTDxwt4Hcc5dNVwAtnuSIdgEozlwa133oynN9URd8EuQRkOegoP7Q74AbwRPKKdfIzb X-Received: by 2002:a17:902:9041:: with SMTP id w1mr29554890plz.132.1562640944904; Mon, 08 Jul 2019 19:55:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1562640944; cv=none; d=google.com; s=arc-20160816; b=Tm7Hud8lfy9LOOwj423U87vMrr34bJCnkSSk8CVymomTdTrPPRhxIAR3oDwFUgntVn m0XNSS6i8DL5fEDpRtsLwk9LQ3mfw/ywT1kpdblDzgkB/73K29UhorvavlcW4bBunk7l Jyq3i1Uhu1dI/MgQKyMp2fhSCEsfop8urKJhfpi0p6bcY0DYqJQ32pSrUMU+8U/VxBfk mwHA9GuZx4SoKvxK1SEY6JVeg+VEj4Lpkt02j17PC0Sn87izSwpH58KfMmtLDJ4mfXaX ZvzpESEtRf1dFSHUeI4VrhsFMq+XKtl/f/+fXxx8hLZrGWM74oY/D6h5qyFTyf26Gjr2 MTnw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:content-transfer-encoding :references:in-reply-to:date:cc:to:from:subject:message-id; bh=1YXVxdVlbjP61OVZVlRbACV4q7st4JmnvP5JYUmOe1Q=; b=hxKRNP2iybwOEwsc2DWXzzOkoWobtJ2MMa1T00ZBnBj12ZnkwTMpDhM19gBUIK/a+1 K8PnYdmzAmh4W6GN2mdxbeuwl+zyYCGFxKsi0l7w0Hi48EAy73YsevoA3Z1GuPjobU7L 6nMpkziLjymqMhDeN5G/lwh5LYedbHOiRjwPdmD0RslbGEwoWkYh5YE64Iv3KDZxwLYR nn/8HdKjXOdQoWhn2CA8wah84Y5wxL8Ul7UVwbGDeu617PoGUoEoIBq2vOSoscOJPp4e T/vATiautOIpbUNJS9tUDEp/iw6AmLcOVYU60NRt8rdxs0x0i+LbjhrrVuXKusqHy+9C wrAg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id d25si9373985pge.301.2019.07.08.19.55.29; Mon, 08 Jul 2019 19:55:44 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727417AbfGICyH (ORCPT + 99 others); Mon, 8 Jul 2019 22:54:07 -0400 Received: from mailgw02.mediatek.com ([210.61.82.184]:44662 "EHLO mailgw02.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1727391AbfGICyG (ORCPT ); Mon, 8 Jul 2019 22:54:06 -0400 X-UUID: e54a938dbb3841b2821206c79fe148af-20190709 X-UUID: e54a938dbb3841b2821206c79fe148af-20190709 Received: from mtkcas07.mediatek.inc [(172.21.101.84)] by mailgw02.mediatek.com (envelope-from ) (mhqrelay.mediatek.com ESMTP with TLS) with ESMTP id 1677781939; Tue, 09 Jul 2019 10:53:54 +0800 Received: from MTKCAS06.mediatek.inc (172.21.101.30) by mtkmbs08n1.mediatek.inc (172.21.101.55) with Microsoft SMTP Server (TLS) id 15.0.1395.4; Tue, 9 Jul 2019 10:53:52 +0800 Received: from [172.21.84.99] (172.21.84.99) by MTKCAS06.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1395.4 via Frontend Transport; Tue, 9 Jul 2019 10:53:52 +0800 Message-ID: <1562640832.9077.32.camel@mtksdccf07> Subject: Re: [PATCH v3] kasan: add memory corruption identification for software tag-based mode From: Walter Wu To: Andrey Ryabinin , Dmitry Vyukov CC: Alexander Potapenko , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , "Matthias Brugger" , Martin Schwidefsky , Arnd Bergmann , Vasily Gorbik , Andrey Konovalov , "Jason A . Donenfeld" , Miles Chen , kasan-dev , LKML , Linux-MM , Linux ARM , , wsd_upstream Date: Tue, 9 Jul 2019 10:53:52 +0800 In-Reply-To: References: <20190613081357.1360-1-walter-zh.wu@mediatek.com> <1560447999.15814.15.camel@mtksdccf07> <1560479520.15814.34.camel@mtksdccf07> <1560744017.15814.49.camel@mtksdccf07> <1560774735.15814.54.camel@mtksdccf07> <1561974995.18866.1.camel@mtksdccf07> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.2.3-0ubuntu6 Content-Transfer-Encoding: 7bit MIME-Version: 1.0 X-MTK: N Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 2019-07-08 at 19:33 +0300, Andrey Ryabinin wrote: > > On 7/5/19 4:34 PM, Dmitry Vyukov wrote: > > On Mon, Jul 1, 2019 at 11:56 AM Walter Wu wrote: > >>>>>>>>> This patch adds memory corruption identification at bug report for > >>>>>>>>> software tag-based mode, the report show whether it is "use-after-free" > >>>>>>>>> or "out-of-bound" error instead of "invalid-access" error.This will make > >>>>>>>>> it easier for programmers to see the memory corruption problem. > >>>>>>>>> > >>>>>>>>> Now we extend the quarantine to support both generic and tag-based kasan. > >>>>>>>>> For tag-based kasan, the quarantine stores only freed object information > >>>>>>>>> to check if an object is freed recently. When tag-based kasan reports an > >>>>>>>>> error, we can check if the tagged addr is in the quarantine and make a > >>>>>>>>> good guess if the object is more like "use-after-free" or "out-of-bound". > >>>>>>>>> > >>>>>>>> > >>>>>>>> > >>>>>>>> We already have all the information and don't need the quarantine to make such guess. > >>>>>>>> Basically if shadow of the first byte of object has the same tag as tag in pointer than it's out-of-bounds, > >>>>>>>> otherwise it's use-after-free. > >>>>>>>> > >>>>>>>> In pseudo-code it's something like this: > >>>>>>>> > >>>>>>>> u8 object_tag = *(u8 *)kasan_mem_to_shadow(nearest_object(cacche, page, access_addr)); > >>>>>>>> > >>>>>>>> if (access_addr_tag == object_tag && object_tag != KASAN_TAG_INVALID) > >>>>>>>> // out-of-bounds > >>>>>>>> else > >>>>>>>> // use-after-free > >>>>>>> > >>>>>>> Thanks your explanation. > >>>>>>> I see, we can use it to decide corruption type. > >>>>>>> But some use-after-free issues, it may not have accurate free-backtrace. > >>>>>>> Unfortunately in that situation, free-backtrace is the most important. > >>>>>>> please see below example > >>>>>>> > >>>>>>> In generic KASAN, it gets accurate free-backrace(ptr1). > >>>>>>> In tag-based KASAN, it gets wrong free-backtrace(ptr2). It will make > >>>>>>> programmer misjudge, so they may not believe tag-based KASAN. > >>>>>>> So We provide this patch, we hope tag-based KASAN bug report is the same > >>>>>>> accurate with generic KASAN. > >>>>>>> > >>>>>>> --- > >>>>>>> ptr1 = kmalloc(size, GFP_KERNEL); > >>>>>>> ptr1_free(ptr1); > >>>>>>> > >>>>>>> ptr2 = kmalloc(size, GFP_KERNEL); > >>>>>>> ptr2_free(ptr2); > >>>>>>> > >>>>>>> ptr1[size] = 'x'; //corruption here > >>>>>>> > >>>>>>> > >>>>>>> static noinline void ptr1_free(char* ptr) > >>>>>>> { > >>>>>>> kfree(ptr); > >>>>>>> } > >>>>>>> static noinline void ptr2_free(char* ptr) > >>>>>>> { > >>>>>>> kfree(ptr); > >>>>>>> } > >>>>>>> --- > >>>>>>> > >>>>>> We think of another question about deciding by that shadow of the first > >>>>>> byte. > >>>>>> In tag-based KASAN, it is immediately released after calling kfree(), so > >>>>>> the slub is easy to be used by another pointer, then it will change > >>>>>> shadow memory to the tag of new pointer, it will not be the > >>>>>> KASAN_TAG_INVALID, so there are many false negative cases, especially in > >>>>>> small size allocation. > >>>>>> > >>>>>> Our patch is to solve those problems. so please consider it, thanks. > >>>>>> > >>>>> Hi, Andrey and Dmitry, > >>>>> > >>>>> I am sorry to bother you. > >>>>> Would you tell me what you think about this patch? > >>>>> We want to use tag-based KASAN, so we hope its bug report is clear and > >>>>> correct as generic KASAN. > >>>>> > >>>>> Thanks your review. > >>>>> Walter > >>>> > >>>> Hi Walter, > >>>> > >>>> I will probably be busy till the next week. Sorry for delays. > >>> > >>> It's ok. Thanks your kindly help. > >>> I hope I can contribute to tag-based KASAN. It is a very important tool > >>> for us. > >> > >> Hi, Dmitry, > >> > >> Would you have free time to discuss this patch together? > >> Thanks. > > > > Sorry for delays. I am overwhelm by some urgent work. I afraid to > > promise any dates because the next week I am on a conference, then > > again a backlog and an intern starting... > > > > Andrey, do you still have concerns re this patch? This change allows > > to print the free stack. > > I 'm not sure that quarantine is a best way to do that. Quarantine is made to delay freeing, but we don't that here. > If we want to remember more free stacks wouldn't be easier simply to remember more stacks in object itself? > Same for previously used tags for better use-after-free identification. > Hi Andrey, We ever tried to use object itself to determine use-after-free identification, but tag-based KASAN immediately released the pointer after call kfree(), the original object will be used by another pointer, if we use object itself to determine use-after-free issue, then it has many false negative cases. so we create a lite quarantine(ring buffers) to record recent free stacks in order to avoid those false negative situations. We hope to have one solution to cover all cases and be accurate. Our patch is configurable feature option, it can provide some programmers to easy see the tag-based KASAN report. > > We also have a quarantine for hwasan in user-space. Though it works a > > bit differently then the normal asan quarantine. We keep a per-thread > > fixed-size ring-buffer of recent allocations: > > https://github.com/llvm-mirror/compiler-rt/blob/master/lib/hwasan/hwasan_report.cpp#L274-L284 > > and scan these ring buffers during reports. > > Thanks your information, it looks like the same idea with our patch.