Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp3171667ybc; Thu, 21 Nov 2019 04:29:38 -0800 (PST) X-Google-Smtp-Source: APXvYqyIsKyH/F5Qar8+kYpIAnJjqZ5xvbao7EPQHjwUH+0RVgdhgsnc+t1hSiBzup0GFZazKOWa X-Received: by 2002:a17:906:5448:: with SMTP id d8mr13165471ejp.79.1574339378827; Thu, 21 Nov 2019 04:29:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574339378; cv=none; d=google.com; s=arc-20160816; b=CFKsak8uRbFe2Oxt9WT5jz5Tef/kyFAt79aP5jKfdLCoRGiw1TGIzuBu6sNmxy4Wry f6Y/a0z5gmCyzcMajjZH+jjGBqmWTejMGjm+teUPgfkVtJ/bZceHX3EwqYOyzSLul2ZE zwyQZbQzp77UW5vic3l9xd0S7pSrUoRDAHfsA84iShNk0Lw4ZACLuoiPYiLI19IHw4Xt qh2iPYYJxgWq2Ed/Z136YtGg2N0uiwVDGs1Yqzg5QRDhJhKvF5MyoCSGzZOrpzlqRnS1 CTc0v5U/eJKedCk1uwordPNHUS7Lkn0roSbPMV9bMQz198NYfNJFbu8Q7pb+ReY48cL5 8jAQ== 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=YRH4RjUqX9yiI6hGKAh9OwDvvxLkSoCIt8I1YqnzAko=; b=f6cyNakmwcJLgOJLQpx7y5i//XEe9UIpUd7EI84DgttW1HbJI7wt6NMXxQy6CLbvll 5BdkVRTQMX7hp+WrdGYXyTNZsoRVaOsyB1DWHuV0OZ57TgXlXEbf0BFJq6oUtCO0chlN 4fD0a/G0LNZ+Mp9fZ0qvUJDhR4jIRhtZNrCirUicblYqCJ+yFUb2K6F+AlfkCemEIK+8 AAno/P29M63lShjZXBHyOxp8y+QB43e4cjkism3F0FowgAejXG8n/jrH3BUCmo47Uqs1 v0KpbrsdpDbIsO8AolkicLlhOO6Lr5J5WrJ1AxGfjPJAxOF3xFjCegz1Hfh/it3oxcQz sQ5w== 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 mj25si1639663ejb.198.2019.11.21.04.29.12; Thu, 21 Nov 2019 04:29:38 -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 S1726500AbfKUM1R (ORCPT + 99 others); Thu, 21 Nov 2019 07:27:17 -0500 Received: from relay.sw.ru ([185.231.240.75]:49600 "EHLO relay.sw.ru" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726197AbfKUM1R (ORCPT ); Thu, 21 Nov 2019 07:27:17 -0500 Received: from dhcp-172-16-25-5.sw.ru ([172.16.25.5]) by relay.sw.ru with esmtp (Exim 4.92.3) (envelope-from ) id 1iXlXu-00015y-97; Thu, 21 Nov 2019 15:26:50 +0300 Subject: Re: [PATCH v4 1/2] kasan: detect negative size in memory operation function To: Walter Wu , Alexander Potapenko , Dmitry Vyukov , Matthias Brugger Cc: kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-kernel@vger.kernel.org, linux-arm-kernel@lists.infradead.org, wsd_upstream , linux-mediatek@lists.infradead.org References: <20191112065302.7015-1-walter-zh.wu@mediatek.com> From: Andrey Ryabinin Message-ID: <040479c3-6f96-91c6-1b1a-9f3e947dac06@virtuozzo.com> Date: Thu, 21 Nov 2019 15:26: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: <20191112065302.7015-1-walter-zh.wu@mediatek.com> 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/12/19 9:53 AM, Walter Wu 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().