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=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 60621C433DB for ; Wed, 3 Feb 2021 14:37:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 1F85C64F7C for ; Wed, 3 Feb 2021 14:37:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233231AbhBCOhE (ORCPT ); Wed, 3 Feb 2021 09:37:04 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:56502 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233061AbhBCOfr (ORCPT ); Wed, 3 Feb 2021 09:35:47 -0500 Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 59A22C061573 for ; Wed, 3 Feb 2021 06:35:07 -0800 (PST) Received: by mail-wr1-x42f.google.com with SMTP id l12so24595406wry.2 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=lhy9U2STl2eNDsVGpFiwKkMD6tiXHiIt+xvxAB15ax43QDATKjRMQFdGS1CRygudJo M/QYb791vOTvu8SGjSIFfJ3QRwHXS/+mPEQzejFieYF1LU9Kp3jsPDTANEsOoT39aBZ3 xECrqfT0+KAKKzc1RplBlzK6n3W/T1JMUFB9fv4Ws3PMx7jFNLhwutjlDIT7nwvFMVwm fRZ+A2TnwVoxjKIYECAOoSXXROPm/oktNQsAxIWtTx2k7b7VFfWx7gGagRmmrqLvECYV 8+31+SuM15bts8Y814PERqEYH+YuRJa8YerP5264Fa6unwvuIdVHPyosEfjDwzINgImM EANg== X-Gm-Message-State: AOAM530NtxUYHqi61v8JEwWMOamKbzbORXhQIy6dQYxS+OiffU+GV2qa d7/XoQfoDJEXvBqR6/bqp5JrIw== 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) Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 > 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 0DCB2C433DB for ; Wed, 3 Feb 2021 14:36:20 +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 A3FAD64DB1 for ; Wed, 3 Feb 2021 14:36:19 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org A3FAD64DB1 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=7rbXzebpsZQeMr2oVpqR8do/HEYew0/vlti6sLV0eV0=; b=yPArbcK+wXowv2/e6/GMxhhvC yn3laCyN3Rd+p1FqlX0PwKAcwg6OVufGPZ6BIyjCIBMpQfIuZGNdpU71cPM3BPnWdPjIBiA3zPgs4 IX22gyRf/hQ7YRnE5kMplsLk79qzD7dAilRMh6VW8ghzNYbJkJoDoWOy5BojIepaCnBwknzureKX2 VMjUzjNZwAw+68NSCGdqluFiZU06AYP143mK9sMvovgb708PO2zvIwLHJz1yR1Mo7eRaBEVU+hp8R 2VM9qwIzU/8iKAhpnmBimDvUMKYcLZEYY6EK3YpOpSzpo6h0G9Zv67q11/MjxhGWya4xKulAh84P4 mzI9JH38A==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1l7JFO-0005D9-A0; Wed, 03 Feb 2021 14:35:10 +0000 Received: from mail-wr1-x42e.google.com ([2a00:1450:4864:20::42e]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1l7JFL-0005CX-GW for linux-arm-kernel@lists.infradead.org; Wed, 03 Feb 2021 14:35:08 +0000 Received: by mail-wr1-x42e.google.com with SMTP id m13so24597514wro.12 for ; Wed, 03 Feb 2021 06:35:06 -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=h7Ggk7p3rSDYo6uCN+WKpEeyAaJGc/XSE9SHJY2Z+tsULIPLlbbPQHybcTIKc8A2F6 RYVIcc3Udj5J9VhOc+EA+6ywLfqekGTcOfauMdru0vyIkEb92rmXpELf/Zf1J9+Kqm84 6LTbla2THLhiVTpPkSXvntr8Dt/X1gtzFIMGs1GpDUt0h2ZjXM9KODrd+E+cVX4MpWLZ ieZiW7FaYcY2pteiYufUELV+jtXlRxWIAR5XMBtYO1OggLC6Jk7Y5j03H/boURQhzoZw ShWvAkmnZGjJnBrRLGRWW6iXr82ubej+xyjx2iY2+iRPie+JDM5Rzb3zkzGuwM2dGXlH 26Iw== X-Gm-Message-State: AOAM532GbxhjsO3/vHNomVbv+Q/xAankLnBqaMk/i8t9Q18WOdyaPcPr q2jSKrNQxsz9izFRgKXWBVln4Q== 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 Subject: Re: [PATCH 08/12] kasan, mm: optimize krealloc poisoning Message-ID: References: <431c6cfa0ac8fb2b33d7ab561a64aa84c844d1a0.1612208222.git.andreyknvl@google.com> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <431c6cfa0ac8fb2b33d7ab561a64aa84c844d1a0.1612208222.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-20210203_093507_588459_9EC8BBE9 X-CRM114-Status: GOOD ( 31.38 ) 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 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 > _______________________________________________ linux-arm-kernel mailing list linux-arm-kernel@lists.infradead.org http://lists.infradead.org/mailman/listinfo/linux-arm-kernel