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, 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 9401DC433DB for ; Mon, 8 Feb 2021 10:48:11 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 0DBD364EBF for ; Mon, 8 Feb 2021 10:48:10 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0DBD364EBF 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 5BDEA6B0006; Mon, 8 Feb 2021 05:48:10 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 51FA16B006C; Mon, 8 Feb 2021 05:48:10 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 3C0FC6B006E; Mon, 8 Feb 2021 05:48:10 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0079.hostedemail.com [216.40.44.79]) by kanga.kvack.org (Postfix) with ESMTP id 217C86B0006 for ; Mon, 8 Feb 2021 05:48:10 -0500 (EST) Received: from smtpin24.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay02.hostedemail.com (Postfix) with ESMTP id CE2833629 for ; Mon, 8 Feb 2021 10:48:09 +0000 (UTC) X-FDA: 77794775898.24.lift38_17169f4275fe Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin24.hostedemail.com (Postfix) with ESMTP id A1B201A4A5 for ; Mon, 8 Feb 2021 10:48:09 +0000 (UTC) X-HE-Tag: lift38_17169f4275fe X-Filterd-Recvd-Size: 7252 Received: from mail-wm1-f47.google.com (mail-wm1-f47.google.com [209.85.128.47]) by imf22.hostedemail.com (Postfix) with ESMTP for ; Mon, 8 Feb 2021 10:48:09 +0000 (UTC) Received: by mail-wm1-f47.google.com with SMTP id j21so10079394wmj.0 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=WF0ILaYp8rJknzdDrpIxLosAKaU9zdneWcfBdI3E1qu28yyx7evNE52S9p1iTIPzx3 APWWNDSC8AX0F+X6w60VkUFHgpGJ3CKjwd8POD31hn+InVa3oFKPUgKyywrsdxwpr3we T+xUFP4T2PTS2+GHWUBGyltac5HDqP6ua9DuIA0qUL8s98tm5LuICSCZznLtvIhnFc/j DBQCHlUTtQ2iCF2euTWkNloSxo/55TyI5xD/9hMt8gGN1xgVbZdMgyWg9j/5zbm+nJPR T6jDSdnCKNLzIy0YWi2/7sohCfvV3QW+mAdo4r5XGrMqz8Nl+dEib7Kk2P3N74EfqvFZ FPnA== X-Gm-Message-State: AOAM532exnfMMaw4rWjlgMriMq16D6BZ6ocQQiRLYkkImVPUcUyjq7qo e/svljPZudDhWKp14uDLxi5Sug== 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 Cc: Andrew Morton , Catalin Marinas , Vincenzo Frascino , Dmitry Vyukov , Alexander Potapenko , 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 v3 mm 08/13] kasan, mm: optimize krealloc poisoning Message-ID: References: <9bef90327c9cb109d736c40115684fd32f49e6b0.1612546384.git.andreyknvl@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <9bef90327c9cb109d736c40115684fd32f49e6b0.1612546384.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 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 >