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=-24.8 required=3.0 tests=BAYES_00,DKIMWL_WL_MED, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED,USER_AGENT_SANE_1,USER_IN_DEF_DKIM_WL autolearn=ham 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 02972C4332B for ; Wed, 3 Feb 2021 14:35:10 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 5D00264F6C for ; Wed, 3 Feb 2021 14:35:09 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5D00264F6C Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 784266B0070; Wed, 3 Feb 2021 09:35:08 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 70E036B0071; Wed, 3 Feb 2021 09:35:08 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5D43C6B0072; Wed, 3 Feb 2021 09:35:08 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0192.hostedemail.com [216.40.44.192]) by kanga.kvack.org (Postfix) with ESMTP id 433CA6B0070 for ; Wed, 3 Feb 2021 09:35:08 -0500 (EST) Received: from smtpin21.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 02155181AEF1E for ; Wed, 3 Feb 2021 14:35:08 +0000 (UTC) X-FDA: 77777203896.21.wood02_2d0ab76275d4 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin21.hostedemail.com (Postfix) with ESMTP id C814B180442CD for ; Wed, 3 Feb 2021 14:35:07 +0000 (UTC) X-HE-Tag: wood02_2d0ab76275d4 X-Filterd-Recvd-Size: 7276 Received: from mail-wr1-f48.google.com (mail-wr1-f48.google.com [209.85.221.48]) by imf50.hostedemail.com (Postfix) with ESMTP for ; Wed, 3 Feb 2021 14:35:07 +0000 (UTC) Received: by mail-wr1-f48.google.com with SMTP id c12so24591671wrc.7 for ; Wed, 03 Feb 2021 06:35:07 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=E4K/9sxU94RKYKEloT1K6toG7ncb4LzmnvyruO4aAFU=; b=piNvFA2h0b/jEgsGNJfp5Ste6KGL4xIbYpBUsJrmhbuzWllOy1R1Zl1PQZm6OH7YFX cfBNWWpqHrsoHJxUS4kZMloAWcpgCOOdYz1f+qkKIK5b18tcvGoQcyA2V3wdy3O0nNya lEoEpSdIH/fjBUiy2H47dSm5LW3q3t1nL23YOvlAY5gYlhcoo4BC4VyC/cQtlGmL9jOw vRN7ZZKr2rMzV99lDWED5j46gLsPaPK0Cqzd/pGm3bUT1xQ+xsLX0NozxLmA7+ab9hul V/7VlKk8zp2ckqiLv180Y3ip1eVtpehprooIKfEK0ZopBdHF5Eh+8IWmtmuFCq+BM0MZ beRA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=E4K/9sxU94RKYKEloT1K6toG7ncb4LzmnvyruO4aAFU=; b=bWyoHws86X97SafW42RTFgSfPvfDRl5mU0qO353XG/8LeQlOAyeUuOoAilEJ0l4QmX P9Cp3dLPKseE2fNHO80XngkQAomKEozkbOfFLSbbLPnKvpgF0u5F6B/PlZ8cPyapB/UM iDM2UwOWZNN+tGtvnyl1PoGzq4Km/qL/ehC+WePJgy5bxWYsg9o3XZd0u4XscwlbHc99 Y/WSu6aISnzQ/E8tkhaYVLFeTdja3kPJXIL8brse6z2JOeMwUOeJsE3QsCXj0Ml0qnK4 o3H4dFbBIaVy9HciRn3Xpacxb8jvcKJvS2co4O33MmrDFqDjaPIRxAs9frNk0+AmRK9W KXRw== X-Gm-Message-State: AOAM532o0iDaVG8T012yFNIM6Vl0ICOCQilNvFrJTwE5IGE/kKr9qsYt bUks2gFqg0jJVeeN+q35JuYcNg== X-Google-Smtp-Source: ABdhPJy862T62VL0M2xpW43TfDXl6YsKV5dJiXETZMsf+h43kEedM6c9mRz/qkbM2gqer3LepLyHvA== X-Received: by 2002:a5d:6b45:: with SMTP id x5mr3763180wrw.415.1612362905963; Wed, 03 Feb 2021 06:35:05 -0800 (PST) Received: from elver.google.com ([2a00:79e0:15:13:b1de:c7d:30ce:1840]) by smtp.gmail.com with ESMTPSA id i15sm2672462wmq.26.2021.02.03.06.35.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Feb 2021 06:35:05 -0800 (PST) Date: Wed, 3 Feb 2021 15:34:59 +0100 From: Marco Elver To: Andrey Konovalov Cc: Catalin Marinas , Vincenzo Frascino , Dmitry Vyukov , Alexander Potapenko , Andrew Morton , Will Deacon , Andrey Ryabinin , Peter Collingbourne , Evgenii Stepanov , Branislav Rankov , Kevin Brodsky , kasan-dev@googlegroups.com, linux-arm-kernel@lists.infradead.org, linux-mm@kvack.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH 08/12] kasan, mm: optimize krealloc poisoning Message-ID: References: <431c6cfa0ac8fb2b33d7ab561a64aa84c844d1a0.1612208222.git.andreyknvl@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <431c6cfa0ac8fb2b33d7ab561a64aa84c844d1a0.1612208222.git.andreyknvl@google.com> User-Agent: Mutt/2.0.2 (2020-11-20) 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 Mon, Feb 01, 2021 at 08:43PM +0100, Andrey Konovalov wrote: > Currently, krealloc() always calls ksize(), which unpoisons the whole > object including the redzone. This is inefficient, as kasan_krealloc() > repoisons the redzone for objects that fit into the same buffer. > > This patch changes krealloc() instrumentation to use uninstrumented > __ksize() that doesn't unpoison the memory. Instead, kasan_kreallos() > is changed to unpoison the memory excluding the redzone. > > For objects that don't fit into the old allocation, this patch disables > KASAN accessibility checks when copying memory into a new object instead > of unpoisoning it. > > Signed-off-by: Andrey Konovalov > --- > mm/kasan/common.c | 12 ++++++++++-- > mm/slab_common.c | 20 ++++++++++++++------ > 2 files changed, 24 insertions(+), 8 deletions(-) > > diff --git a/mm/kasan/common.c b/mm/kasan/common.c > index 9c64a00bbf9c..a51d6ea580b0 100644 > --- a/mm/kasan/common.c > +++ b/mm/kasan/common.c > @@ -476,7 +476,7 @@ static void *____kasan_kmalloc(struct kmem_cache *cache, const void *object, > > /* > * The object has already been unpoisoned by kasan_slab_alloc() for > - * kmalloc() or by ksize() for krealloc(). > + * kmalloc() or by kasan_krealloc() for krealloc(). > */ > > /* > @@ -526,7 +526,7 @@ void * __must_check __kasan_kmalloc_large(const void *ptr, size_t size, > > /* > * The object has already been unpoisoned by kasan_alloc_pages() for > - * alloc_pages() or by ksize() for krealloc(). > + * alloc_pages() or by kasan_krealloc() for krealloc(). > */ > > /* > @@ -554,8 +554,16 @@ void * __must_check __kasan_krealloc(const void *object, size_t size, gfp_t flag > if (unlikely(object == ZERO_SIZE_PTR)) > return (void *)object; > > + /* > + * Unpoison the object's data. > + * Part of it might already have been unpoisoned, but it's unknown > + * how big that part is. > + */ > + kasan_unpoison(object, size); > + > page = virt_to_head_page(object); > > + /* Piggy-back on kmalloc() instrumentation to poison the redzone. */ > if (unlikely(!PageSlab(page))) > return __kasan_kmalloc_large(object, size, flags); > else > diff --git a/mm/slab_common.c b/mm/slab_common.c > index dad70239b54c..821f657d38b5 100644 > --- a/mm/slab_common.c > +++ b/mm/slab_common.c > @@ -1140,19 +1140,27 @@ static __always_inline void *__do_krealloc(const void *p, size_t new_size, > void *ret; > size_t ks; > > - if (likely(!ZERO_OR_NULL_PTR(p)) && !kasan_check_byte(p)) > - return NULL; > - > - ks = ksize(p); > + /* Don't use instrumented ksize to allow precise KASAN poisoning. */ > + if (likely(!ZERO_OR_NULL_PTR(p))) { > + if (!kasan_check_byte(p)) > + return NULL; > + ks = __ksize(p); > + } else > + ks = 0; > This unfortunately broke KFENCE: https://syzkaller.appspot.com/bug?extid=e444e1006d07feef0ef3 + various other false positives. We need to use ksize() here, as __ksize() is unaware of KFENCE. Or somehow add the same check here that ksize() uses to get the real object size. > + /* If the object still fits, repoison it precisely. */ > if (ks >= new_size) { > p = kasan_krealloc((void *)p, new_size, flags); > return (void *)p; > } > > ret = kmalloc_track_caller(new_size, flags); > - if (ret && p) > - memcpy(ret, p, ks); > + if (ret && p) { > + /* Disable KASAN checks as the object's redzone is accessed. */ > + kasan_disable_current(); > + memcpy(ret, kasan_reset_tag(p), ks); > + kasan_enable_current(); > + } > > return ret; > } > -- > 2.30.0.365.g02bc693789-goog >