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=-0.7 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY 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 ACBE3C31E45 for ; Thu, 13 Jun 2019 17:46:50 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 8F5D12175B for ; Thu, 13 Jun 2019 17:46:50 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727210AbfFMRqt (ORCPT ); Thu, 13 Jun 2019 13:46:49 -0400 Received: from mailgw01.mediatek.com ([210.61.82.183]:11945 "EHLO mailgw01.mediatek.com" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1725852AbfFMRqt (ORCPT ); Thu, 13 Jun 2019 13:46:49 -0400 X-UUID: 1bc9fbe28e904bcf97072f05f18c0602-20190614 X-UUID: 1bc9fbe28e904bcf97072f05f18c0602-20190614 Received: from mtkcas06.mediatek.inc [(172.21.101.30)] by mailgw01.mediatek.com (envelope-from ) (mhqrelay.mediatek.com ESMTP with TLS) with ESMTP id 795620865; Fri, 14 Jun 2019 01:46:41 +0800 Received: from mtkcas08.mediatek.inc (172.21.101.126) by mtkmbs08n2.mediatek.inc (172.21.101.56) with Microsoft SMTP Server (TLS) id 15.0.1395.4; Fri, 14 Jun 2019 01:46:39 +0800 Received: from [172.21.84.99] (172.21.84.99) by mtkcas08.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1395.4 via Frontend Transport; Fri, 14 Jun 2019 01:46:39 +0800 Message-ID: <1560447999.15814.15.camel@mtksdccf07> Subject: Re: [PATCH v3] kasan: add memory corruption identification for software tag-based mode From: Walter Wu To: Andrey Ryabinin CC: Alexander Potapenko , Dmitry Vyukov , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Matthias Brugger , "Martin Schwidefsky" , Arnd Bergmann , "Vasily Gorbik" , Andrey Konovalov , "Jason A . Donenfeld" , Miles Chen , , , , , , Date: Fri, 14 Jun 2019 01:46:39 +0800 In-Reply-To: References: <20190613081357.1360-1-walter-zh.wu@mediatek.com> Content-Type: text/plain; charset="UTF-8" X-Mailer: Evolution 3.2.3-0ubuntu6 Content-Transfer-Encoding: 7bit MIME-Version: 1.0 X-TM-SNTS-SMTP: 73E573334F60FA682523A92B528AE4E79ADB9EB515874F36690B616CC9E25A672000:8 X-MTK: N Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Thu, 2019-06-13 at 15:27 +0300, Andrey Ryabinin wrote: > > On 6/13/19 11:13 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); } --- From mboxrd@z Thu Jan 1 00:00:00 1970 From: Walter Wu Subject: Re: [PATCH v3] kasan: add memory corruption identification for software tag-based mode Date: Fri, 14 Jun 2019 01:46:39 +0800 Message-ID: <1560447999.15814.15.camel@mtksdccf07> References: <20190613081357.1360-1-walter-zh.wu@mediatek.com> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 7bit Return-path: In-Reply-To: Sender: linux-kernel-owner@vger.kernel.org To: Andrey Ryabinin Cc: Alexander Potapenko , Dmitry Vyukov , 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@googlegroups.com, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, linux-mediatek@lists.infradead.org, wsd_upstream@mediatek.com List-Id: linux-mediatek@lists.infradead.org On Thu, 2019-06-13 at 15:27 +0300, Andrey Ryabinin wrote: > > On 6/13/19 11:13 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); } --- 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=-0.7 required=3.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,SPF_HELO_NONE, SPF_PASS,UNPARSEABLE_RELAY,URIBL_BLOCKED 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 0E08CC31E45 for ; Thu, 13 Jun 2019 17:47:04 +0000 (UTC) Received: from bombadil.infradead.org (bombadil.infradead.org [198.137.202.133]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id DA59A217D9 for ; Thu, 13 Jun 2019 17:47:03 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (2048-bit key) header.d=lists.infradead.org header.i=@lists.infradead.org header.b="nAv1e5LM" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org DA59A217D9 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=mediatek.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=bombadil.20170209; h=Sender: Content-Transfer-Encoding:Content-Type:Cc:List-Subscribe:List-Help:List-Post: List-Archive:List-Unsubscribe:List-Id:MIME-Version:References:In-Reply-To: Date:To:From:Subject:Message-ID:Reply-To:Content-ID:Content-Description: Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Owner; bh=zXhcLKIcHXI0VYEFTTVY8UKjaRa2UWdoqjangzRS0e0=; b=nAv1e5LMJHLPW0 pv0DGZQmLgW3kfn8sehdro8p+VlkEnqbIgA6ioSl0Q4NiXVl06M9FAcuh16Lu9vMyxOk5YDerYe+t yQBhZNKxMHwoID3OIA7y10koV6rfeGzY6OpjeG4tZWbBeFPxtPqwY+hqjPnmEqdV/STqctmhqbZOo Hxt3TudFWNgALbVlxwrPOUMpWuyywQsMwso0DRrl8AkHwMb8N+VeJo8C8mXHJnNWImVlgD2Md+u1P 90KIh0K8Ye9BFrNYpQ6SyJSyDcc7NOQAgzDkWEM45MZ5AwSYSJT+S1Brh3K7PJJB82lSgOgV5/Qbl QSk7ZhxCZYOzsTxkdZww==; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.92 #3 (Red Hat Linux)) id 1hbToU-0004NQ-KS; Thu, 13 Jun 2019 17:47:02 +0000 Received: from mailgw01.mediatek.com ([216.200.240.184]) by bombadil.infradead.org with esmtps (Exim 4.92 #3 (Red Hat Linux)) id 1hbToR-0004Lu-JR; Thu, 13 Jun 2019 17:47:00 +0000 X-UUID: 5161a3908e914fadaee305eea5777c55-20190613 X-UUID: 5161a3908e914fadaee305eea5777c55-20190613 Received: from mtkcas66.mediatek.inc [(172.29.193.44)] by mailgw01.mediatek.com (envelope-from ) (musrelay.mediatek.com ESMTP with TLS) with ESMTP id 342591265; Thu, 13 Jun 2019 09:46:55 -0800 Received: from mtkmbs08n2.mediatek.inc (172.21.101.56) by MTKMBS62DR.mediatek.inc (172.29.94.18) with Microsoft SMTP Server (TLS) id 15.0.1395.4; Thu, 13 Jun 2019 10:46:54 -0700 Received: from mtkcas08.mediatek.inc (172.21.101.126) by mtkmbs08n2.mediatek.inc (172.21.101.56) with Microsoft SMTP Server (TLS) id 15.0.1395.4; Fri, 14 Jun 2019 01:46:39 +0800 Received: from [172.21.84.99] (172.21.84.99) by mtkcas08.mediatek.inc (172.21.101.73) with Microsoft SMTP Server id 15.0.1395.4 via Frontend Transport; Fri, 14 Jun 2019 01:46:39 +0800 Message-ID: <1560447999.15814.15.camel@mtksdccf07> Subject: Re: [PATCH v3] kasan: add memory corruption identification for software tag-based mode From: Walter Wu To: Andrey Ryabinin Date: Fri, 14 Jun 2019 01:46:39 +0800 In-Reply-To: References: <20190613081357.1360-1-walter-zh.wu@mediatek.com> X-Mailer: Evolution 3.2.3-0ubuntu6 MIME-Version: 1.0 X-TM-SNTS-SMTP: 73E573334F60FA682523A92B528AE4E79ADB9EB515874F36690B616CC9E25A672000:8 X-MTK: N X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20190613_104659_644257_F0418732 X-CRM114-Status: GOOD ( 14.85 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: wsd_upstream@mediatek.com, "Jason A . Donenfeld" , Vasily Gorbik , Arnd Bergmann , linux-mm@kvack.org, Andrey Konovalov , linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, Pekka Enberg , Martin Schwidefsky , Miles Chen , Alexander Potapenko , linux-arm-kernel@lists.infradead.org, David Rientjes , Matthias Brugger , linux-mediatek@lists.infradead.org, Christoph Lameter , Joonsoo Kim , Dmitry Vyukov Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+infradead-linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Thu, 2019-06-13 at 15:27 +0300, Andrey Ryabinin wrote: > > On 6/13/19 11:13 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); } --- _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel