Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp3821773ybc; Thu, 21 Nov 2019 14:25:15 -0800 (PST) X-Google-Smtp-Source: APXvYqwteE0cIeIm+RRzPKfag9Tkaih3BPQNgY5pXsGNv6yQTYsKgC9SXWhjGr7w/x2PhJuMui4S X-Received: by 2002:a05:600c:2911:: with SMTP id i17mr12745082wmd.83.1574375115326; Thu, 21 Nov 2019 14:25:15 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574375115; cv=none; d=google.com; s=arc-20160816; b=C2MAdTisLjbjR8WvE13rl4k7IL+lMVrdsgNuAnc5R/PwGBWUmbm5M7BwIA0ScZZoud MDhXd1ryA9JBaTz94biDfv7xcI4Uy30lUmNIiYp74LajIhrQpJdq4MmE4SiTXQ+mvKiQ 1UJ+j9OH/YN+z/YhSAS7dqrHdW+RkAyL5mwGCaqO5YiXieVAdsv+7ZizFSGmAdSwT4d7 50m9e6Iv0W0maK432Dt/VGWwxVPCfuNxNuUNp3hYkEBP2cJ93/aHprv3dGcjk+azl51m gZjY/Yl2RBHs/aTsit4m5a6QnCSqr9eeNSokrsac9dE4OZDNDCj4Ja6yy0IZIMO/IFHe ZI3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=+AB/9Sm9DUswifV1IgH5NCvGEGSb1/O0nPDfQF8SVhc=; b=cqQ84fzUOLDGW3tl50XWm0F/g2v6/EZi3mEJ38txBGIqILW3k8u8yRvCFy6fjV8jrI 5J7ND8Jzr8EuyvFRg6AzUZk9Qwp3sO9JrYgy6G0MyFb2ozp8bb1PhD5gGp5X+1ak01v1 JKbi6s38h6/EjRnqFjjN/0697j1FEzyfkJRkeUzqAs5+cSCuDLrik+e+00ZDk2m4xUNO Q0o94YFIKvOxPNoG3/OdQxomTpa68ChPQdusimYHRESpYOwkklgMlVqnzDcSOCKfp3+5 mWJtPZfnUJyEvRwYnVgMPY5uRvW/fmuRMBx5iqMxmj1AJ0+/Re86cdy0JSGuvgOrpxez hF1g== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=virtuozzo.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id bo21si3207672edb.137.2019.11.21.14.24.51; Thu, 21 Nov 2019 14:25:15 -0800 (PST) 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=virtuozzo.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726861AbfKUWUr (ORCPT + 99 others); Thu, 21 Nov 2019 17:20:47 -0500 Received: from relay.sw.ru ([185.231.240.75]:55856 "EHLO relay.sw.ru" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726329AbfKUWUq (ORCPT ); Thu, 21 Nov 2019 17:20:46 -0500 Received: from [192.168.15.154] by relay.sw.ru with esmtp (Exim 4.92.3) (envelope-from ) id 1iXuoL-0007nb-4j; Fri, 22 Nov 2019 01:20:25 +0300 Subject: Re: [PATCH v4 1/2] kasan: detect negative size in memory operation function To: Dmitry Vyukov Cc: Walter Wu , Alexander Potapenko , Matthias Brugger , kasan-dev , Linux-MM , LKML , Linux ARM , wsd_upstream , linux-mediatek@lists.infradead.org References: <20191112065302.7015-1-walter-zh.wu@mediatek.com> <040479c3-6f96-91c6-1b1a-9f3e947dac06@virtuozzo.com> From: Andrey Ryabinin Message-ID: Date: Fri, 22 Nov 2019 01:18:38 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.2.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On 11/21/19 10:58 PM, Dmitry Vyukov wrote: > On Thu, Nov 21, 2019 at 1:27 PM Andrey Ryabinin wrote: >>> diff --git a/mm/kasan/common.c b/mm/kasan/common.c >>> index 6814d6d6a023..4bfce0af881f 100644 >>> --- a/mm/kasan/common.c >>> +++ b/mm/kasan/common.c >>> @@ -102,7 +102,8 @@ EXPORT_SYMBOL(__kasan_check_write); >>> #undef memset >>> void *memset(void *addr, int c, size_t len) >>> { >>> - check_memory_region((unsigned long)addr, len, true, _RET_IP_); >>> + if (!check_memory_region((unsigned long)addr, len, true, _RET_IP_)) >>> + return NULL; >>> >>> return __memset(addr, c, len); >>> } >>> @@ -110,8 +111,9 @@ void *memset(void *addr, int c, size_t len) >>> #undef memmove >>> void *memmove(void *dest, const void *src, size_t len) >>> { >>> - check_memory_region((unsigned long)src, len, false, _RET_IP_); >>> - check_memory_region((unsigned long)dest, len, true, _RET_IP_); >>> + if (!check_memory_region((unsigned long)src, len, false, _RET_IP_) || >>> + !check_memory_region((unsigned long)dest, len, true, _RET_IP_)) >>> + return NULL; >>> >>> return __memmove(dest, src, len); >>> } >>> @@ -119,8 +121,9 @@ void *memmove(void *dest, const void *src, size_t len) >>> #undef memcpy >>> void *memcpy(void *dest, const void *src, size_t len) >>> { >>> - check_memory_region((unsigned long)src, len, false, _RET_IP_); >>> - check_memory_region((unsigned long)dest, len, true, _RET_IP_); >>> + if (!check_memory_region((unsigned long)src, len, false, _RET_IP_) || >>> + !check_memory_region((unsigned long)dest, len, true, _RET_IP_)) >>> + return NULL; >>> >> >> I realized that we are going a wrong direction here. Entirely skipping mem*() operation on any >> poisoned shadow value might only make things worse. Some bugs just don't have any serious consequences, >> but skipping the mem*() ops entirely might introduce such consequences, which wouldn't happen otherwise. >> >> So let's keep this code as this, no need to check the result of check_memory_region(). > > I suggested it. > > For our production runs it won't matter, we always panic on first report. > If one does not panic, there is no right answer. You say: _some_ bugs > don't have any serious consequences, but skipping the mem*() ops > entirely might introduce such consequences. The opposite is true as > well, right? :) And it's not hard to come up with a scenario where > overwriting memory after free or out of bounds badly corrupts memory. > I don't think we can somehow magically avoid bad consequences in all > cases. > Absolutely right. My point was that if it's bad consequences either way, than there is no point in complicating this code, it doesn't buy us anything. > What I was thinking about is tests. We need tests for this. And we > tried to construct tests specifically so that they don't badly corrupt > memory (e.g. OOB/UAF reads, or writes to unused redzones, etc), so > that it's possible to run all of them to completion reliably. Skipping > the actual memory options allows to write such tests for all possible > scenarios. That's was my motivation. But I see you point now. No objections to the patch in that case.