Received: by 2002:a25:7ec1:0:0:0:0:0 with SMTP id z184csp3679163ybc; Thu, 21 Nov 2019 12:00:34 -0800 (PST) X-Google-Smtp-Source: APXvYqz9iqQaCUHjPEkTnnCAIIz36WbM5FnBBSyTM7QZDdVe3tiHD0MiMkwYeMO9rMktDLWwVHCg X-Received: by 2002:a17:906:709:: with SMTP id y9mr15435906ejb.321.1574366433895; Thu, 21 Nov 2019 12:00:33 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1574366433; cv=none; d=google.com; s=arc-20160816; b=bp+jjv5po2mc5LEYSWGIkXbiZj+KffrcLDXW1CRcm3MUjDsfpGpiV8EASy1DjgeEPC tFOvOZFAXntqbRGQO8AzZRQgTLvdtZ/jIT9/FoxZP4aEudvycEcI7KdEpqjSA5t+Ct57 2KwmcSc3OsbkTUyO0qmlRTray7yNT46G2kzKxvCvL7AH11zWx33dUDRbDBf4dfI5GVe3 hB1gbSYR8fKaikxr83Lr5+xa004Ru/OU8knpDO19Nyy91wdPfg/IUuNFIgu+QhM26WBh iUy9Y2Ne2ZuSoJD+R3W296VWIl+OtLAMt2GM0Yr5Rfn3uoGs6SOgR+8lTN8VeO0sZ2kh ZZog== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:dkim-signature; bh=NrzxHrY8HSPigVP1ddvjUfQFh9GexeJHE6yrEfyJGn8=; b=HNQ1wkOY+pVS2QXhy2+K9HX+AJuhedeYwD9sGBcp+ECu1utIO6xQ8Glx+KEUMXDeqG kHUL0kb6X0XLAPWVU/ENtk36Ck9+bJ7E4NLdGX70TEc7/4AhfrveqRnxS0hAIbMrwCnV poxqUOmajRo2d8v+tgyEF34+yJ23/16Z0OP4Iwe8SYvJVBOMh6qgNQaeIviq9RvPYxaH uDOWWjHUf/fSHUj1eFn2haPaSjbebPwpbDkefOKV05zbIQnCuOPyMc5c7O+D76u41qWh KYRFuK2cW0iHaK7sW5wv92yGNi6fWsUYO+g7W3+RlEFDYINVp0bqZClO6GL/6xomAjJh i4RA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=Kd3uHXA1; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a13si26691edr.154.2019.11.21.12.00.09; Thu, 21 Nov 2019 12:00:33 -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; dkim=pass header.i=@google.com header.s=20161025 header.b=Kd3uHXA1; 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=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727016AbfKUT6d (ORCPT + 99 others); Thu, 21 Nov 2019 14:58:33 -0500 Received: from mail-qt1-f195.google.com ([209.85.160.195]:38250 "EHLO mail-qt1-f195.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726546AbfKUT6d (ORCPT ); Thu, 21 Nov 2019 14:58:33 -0500 Received: by mail-qt1-f195.google.com with SMTP id 14so5096597qtf.5 for ; Thu, 21 Nov 2019 11:58:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=NrzxHrY8HSPigVP1ddvjUfQFh9GexeJHE6yrEfyJGn8=; b=Kd3uHXA1P6DuCOhB2GxNrroyfRPcjST5rj7knlJlrVz7Lrr4YsfVvSNtbCJMOzDkm9 v/Y0j8z3u2abnQEXY0H/eS8gg8o59m/He8HAtGj9aKsogiT9HudHc7VPDU2uoLVegRab sua5+XIGxHCZJbnaUyGj+m0xq4sdO/dgPH5ql42Q3eTE9zhaycI8jmcThgYH39KDXrzD 1s8L4VxDYCCJ8XxaPf86AG60AUSWCfVYt225dJygovcnw48m1vkJtxVDMaj17SMkzVOU 6motdGawpCw2ZkIZ70iqZsTbQBr8FOa9spMlRL+BJl5lsiRXNBDSQxevq5bBE2xPOBET 0W4g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=NrzxHrY8HSPigVP1ddvjUfQFh9GexeJHE6yrEfyJGn8=; b=BNbkkZDopC9zWCwtf5hFCxA76tZjH2wBHCxksR/mNDWzZh0boq8G1/1YVS4dGDvl5A cYRpY1N3p/jVWM+SVJy2gF73gAZazGUyOiFia+WEPB/Dvmj9McNtT2nJDFg7gYDcH/Lp pNZ7KCuQxrYy9T9UmI+xKP2/EoCcevK0AwEPyFVg6uonCEGT2Nn9b1MqlTcKGzjZflgh gSWjLHSZDdUiNXbQBJiFu4xFTSbDI35bTVnOWNq2he43/hChiumtAdPcQdmfQoIZx392 NO2y14VfG0I6cNvdWMQXGAxEN+akNf5UlF4qGK7YpQGCsTj8oIrnTbdzliSGJQc9cmSy u4SQ== X-Gm-Message-State: APjAAAXXkHq1fwq13L+jFHSQpd9g9wyVIIQngv2h++EbeUxDOJTVSmXA F7IpFRdExFJ95z8kamZ9ThNqQ8EcpB6OkSHYmsVvTA== X-Received: by 2002:aed:24af:: with SMTP id t44mr10377791qtc.57.1574366311591; Thu, 21 Nov 2019 11:58:31 -0800 (PST) MIME-Version: 1.0 References: <20191112065302.7015-1-walter-zh.wu@mediatek.com> <040479c3-6f96-91c6-1b1a-9f3e947dac06@virtuozzo.com> In-Reply-To: <040479c3-6f96-91c6-1b1a-9f3e947dac06@virtuozzo.com> From: Dmitry Vyukov Date: Thu, 21 Nov 2019 20:58:19 +0100 Message-ID: Subject: Re: [PATCH v4 1/2] kasan: detect negative size in memory operation function To: Andrey Ryabinin Cc: Walter Wu , Alexander Potapenko , Matthias Brugger , kasan-dev , Linux-MM , LKML , Linux ARM , wsd_upstream , linux-mediatek@lists.infradead.org Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. 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.