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=-5.2 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED, USER_AGENT_SANE_1 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 D93D0C432C3 for ; Thu, 21 Nov 2019 22:20:49 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id A6CE2206CB for ; Thu, 21 Nov 2019 22:20:49 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A6CE2206CB Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=virtuozzo.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 3F8EB6B0396; Thu, 21 Nov 2019 17:20:49 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3AB596B0398; Thu, 21 Nov 2019 17:20:49 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 2BF626B0399; Thu, 21 Nov 2019 17:20:49 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0204.hostedemail.com [216.40.44.204]) by kanga.kvack.org (Postfix) with ESMTP id 127AE6B0396 for ; Thu, 21 Nov 2019 17:20:49 -0500 (EST) Received: from smtpin29.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with SMTP id 9D834180AD81A for ; Thu, 21 Nov 2019 22:20:48 +0000 (UTC) X-FDA: 76181705376.29.hand34_7b69695c72905 X-HE-Tag: hand34_7b69695c72905 X-Filterd-Recvd-Size: 4815 Received: from relay.sw.ru (relay.sw.ru [185.231.240.75]) by imf24.hostedemail.com (Postfix) with ESMTP for ; Thu, 21 Nov 2019 22:20:47 +0000 (UTC) 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 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: 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.