Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp3208951ybc; Thu, 21 Nov 2019 05:06:25 -0800 (PST) X-Google-Smtp-Source: APXvYqw8DynaatgQX0dMXVveDRbYJyL1QDSj59uzCuCmTZJgkj0J+7B2Dot2bS+aHQF8BNZIUFQn X-Received: by 2002:ac2:5119:: with SMTP id q25mr7472870lfb.175.1574341585690; Thu, 21 Nov 2019 05:06:25 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574341585; cv=none; d=google.com; s=arc-20160816; b=gKtWaYHREnpoZV4TuzjCcPjmGqld2cuVvsb5wskfNcZbzQlfUha2zRUqLUCjCThKuS G5PTuj94J8bnjdZkkGGZ70mEw8PvKrhBBuJqyX0KS1Iv/tW1YObjy54uxcuUksBPvxQG GHc4Tzor61nruPLJ/3O6O0Qz5iM76OxyS9EVrdC6VE7LVCqbTXiZrWmPdrZCgqrs6WcO rvUjHE7AubWTzJfa/JT7o1uu0a04GD/kE6SMTRBQk+SGCpILnGnnr5SITumqOAISD/JM RogOafI2OJGMSg3K1oYvndaVk/a5Nyuvxa5BtYM1T2b80BWI6f0S0Fu+lVkChkFGSbpg nNdA== 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=EBcTiMM5yBnrH7fTMom6myKUpQTRFOmVctAF+o4fa7Y=; b=pmkXVMe7O6LPUeID9G+7Z0u6Tn9zaJAg5NCy4FRPqXiGTpP/HWUyEufzLYJnGXqjJL JlG0EWH9Epko2YQIb0g4WBINAl5HXO2v0YWzhNdw+y1AdSDf2V45d9tzafLiB9LfnBAW 2/lZix/ebP3PAxdS9c+rKXc30m1pN2getc9QJRvbbc7h+8T1dU1ZsMD5aQa+2iLnuYTQ dyF9z0UN00GHthRNfoZNou6anTnNNB+LMiiXo/2cUqbdC8kbYO5t/zIbyO3DrZnIBJwd SHvMtEDovB5zGYosyqASJzLDPJz+hH7GIYlIjMp+/r37CddeRDgANLSW2dxxIVN99IZH 2+Vg== 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 d2si1742076ejr.371.2019.11.21.05.05.59; Thu, 21 Nov 2019 05:06:25 -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 S1726939AbfKUNEA (ORCPT + 99 others); Thu, 21 Nov 2019 08:04:00 -0500 Received: from relay.sw.ru ([185.231.240.75]:52106 "EHLO relay.sw.ru" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726293AbfKUND7 (ORCPT ); Thu, 21 Nov 2019 08:03:59 -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 1iXm7i-0002DQ-S4; Thu, 21 Nov 2019 16:03:50 +0300 Subject: Re: [PATCH v4 1/2] kasan: detect negative size in memory operation function To: Walter Wu Cc: Alexander Potapenko , Dmitry Vyukov , Matthias Brugger , 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> <040479c3-6f96-91c6-1b1a-9f3e947dac06@virtuozzo.com> <1574341376.8338.4.camel@mtksdccf07> From: Andrey Ryabinin Message-ID: <217bd537-e6b7-3acc-b6bb-ac9c5d94da89@virtuozzo.com> Date: Thu, 21 Nov 2019 16:03: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: <1574341376.8338.4.camel@mtksdccf07> 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 4:02 PM, Walter Wu wrote: > On Thu, 2019-11-21 at 15:26 +0300, Andrey Ryabinin wrote: >> >> 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(). >> >> > Ok, we just need to determine whether size is negative number. If yes > then KASAN produce report and continue to execute mem*(). right? > Yes.