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=-15.8 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_ADSP_CUSTOM_MED,DKIM_SIGNED,DKIM_VALID,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,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 BA24AC433E6 for ; Mon, 8 Feb 2021 10:49:34 +0000 (UTC) Received: from merlin.infradead.org (merlin.infradead.org [205.233.59.134]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 5A65064E76 for ; Mon, 8 Feb 2021 10:49:34 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 5A65064E76 Authentication-Results: mail.kernel.org; dmarc=fail (p=reject dis=none) header.from=google.com Authentication-Results: mail.kernel.org; spf=none smtp.mailfrom=linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.infradead.org; s=merlin.20170209; h=Sender:Content-Transfer-Encoding: Content-Type:Cc:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:In-Reply-To:MIME-Version:References:Message-ID: Subject:To:From:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=r/Iax0LkGdYX4FlSOH6n/p3en98KCfUqeThofHEvjOQ=; b=XxLxq9nuI840+jG2rnTBg+/EK Z8nn3DWwr35wXw7eTnt6rLLfW5mj76XQTSxA6xyJj26cHeiDbQGKAnEz5PlRK2Y8n1KDuERWRuEVw rYeIyKQNelqqSU9tfyIq94LbMMcm3qxiQKHD3et0OhDMHII0EKAIhxcjLajHPWvCRdbHjJJ5XFePY 7iIKQoH44kysmywdg6GL326Z5w4eTNJDA7h1dMigA7MFR9YmHaAxxWPvfjmHPN58aqUuVWHxA4/yI XPq1AnAzDmMr50H+xMZOCfXzd1n9g1kXAKVehdw03knlMjQSPlDxHub5VsYZHEkfy4Vs8qNEYJhNA zUX1kvCvg==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1l945W-0000pK-Vu; Mon, 08 Feb 2021 10:48:15 +0000 Received: from mail-wm1-x333.google.com ([2a00:1450:4864:20::333]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1l945U-0000nz-1z for linux-arm-kernel@lists.infradead.org; Mon, 08 Feb 2021 10:48:13 +0000 Received: by mail-wm1-x333.google.com with SMTP id m1so12782564wml.2 for ; Mon, 08 Feb 2021 02:48:08 -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=lgGm4PB5m4Er0st2+IAu2ADnhULDfoFDJ2Jsf0woe9E=; b=QhK5ZJAGnEkI3A3Z2DNmhtocdr8uInviUMncIed8Yw60g+v4JjhGra+7ogE9iuJekm 23cf96w5JdaoDSZJtcnr3Ivr5yjGk4yPDPLTa/mCaykYOimrYCsUGpsYs7AvB8KuTzP3 /K7jSYSJTaXywmR/kzbjN+gmJyZmHu4zrD+hD9tUBSAfNJd5TEm0PbeCDBNIdKg+P63D jgnXCRdJj93uJ/WNNj8MMQT7CU8pwoM6zd01N3mhkQNfpaCwWtAxyErclXw5gr6UjvlF h/U9EZLMThz7Ijl6rfpflzgsWxyf3rsvsE9s1oiNsyQa9AnK9iH3Sjynsw4dOHyG1NIj CPJw== 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=lgGm4PB5m4Er0st2+IAu2ADnhULDfoFDJ2Jsf0woe9E=; b=jvvw2Nvbbwz9YP0w31e/Tg7ktdQ0F9UIAHNXxl5C2rwWR5wQ34CX1a9JefvHQ1F5rw lGOGwXT2lGTNJjJpQmGZLAsMPChB1vzeFml452E1N4q/k1ZNzrOl+vM95rcb4MemXvwG 2vwU4C8pdfP/aLB8WJuhpI8tCgLyuoQl4rRqXl8hUz4C+kT+dob7FWJ1ZLEKKa/niYCw tzd99VOhXM76wwi5EkaI0rmYTdsl+qKDYqFdmGgt2y1eFcf/rEbUtBqBz9vTa/bBjbE1 9XSlknx/p5Hn7JflPznWl8kduoBsnz/iqo7FWplVPUj0gQidVEQhrpepHhoxUjdkfXTX EXXg== X-Gm-Message-State: AOAM530ifSYxHfr92KVk3cGkFkHwVaSSYXwVYFtLd4SyNMGw10LQJC8q 0BUyATctelOCjRPNe/a1EfcXwA== X-Google-Smtp-Source: ABdhPJxJ1xVrDFdzjEST4rocD/QkhguzJ9ybMCZvQBotr7Rh2p3RxFYnATJDwH4oDHDebADgcA4NLg== X-Received: by 2002:a1c:7905:: with SMTP id l5mr11809452wme.171.1612781287598; Mon, 08 Feb 2021 02:48:07 -0800 (PST) Received: from elver.google.com ([2a00:79e0:15:13:4037:8827:dcff:a9da]) by smtp.gmail.com with ESMTPSA id u142sm20991623wmu.3.2021.02.08.02.48.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 08 Feb 2021 02:48:06 -0800 (PST) Date: Mon, 8 Feb 2021 11:48:01 +0100 From: Marco Elver To: Andrey Konovalov Subject: Re: [PATCH v3 mm 08/13] kasan, mm: optimize krealloc poisoning Message-ID: References: <9bef90327c9cb109d736c40115684fd32f49e6b0.1612546384.git.andreyknvl@google.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <9bef90327c9cb109d736c40115684fd32f49e6b0.1612546384.git.andreyknvl@google.com> User-Agent: Mutt/2.0.2 (2020-11-20) X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210208_054812_199275_42631A70 X-CRM114-Status: GOOD ( 31.70 ) X-BeenThere: linux-arm-kernel@lists.infradead.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: linux-arm-kernel@lists.infradead.org, Branislav Rankov , Catalin Marinas , Kevin Brodsky , Will Deacon , linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, linux-mm@kvack.org, Alexander Potapenko , Evgenii Stepanov , Andrey Ryabinin , Andrew Morton , Vincenzo Frascino , Peter Collingbourne , Dmitry Vyukov Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org On Fri, Feb 05, 2021 at 06:34PM +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 Reviewed-by: Marco Elver Clarification below. > --- > 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 7ea643f7e69c..a8a67dca5e55 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..60a2f49df6ce 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; Just checking: Check byte returns true if the object is not tracked by KASAN, right? I.e. if it's a KFENCE object, kasan_check_byte() always returns true. > + ks = kfence_ksize(p) ?: __ksize(p); > + } else > + ks = 0; > > + /* 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 > _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel