From patchwork Fri Feb 5 17:34:42 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 12070561 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=-16.5 required=3.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED,DKIM_INVALID,DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_CR_TRAILER,INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS, USER_AGENT_GIT 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 3BF4EC433E0 for ; Fri, 5 Feb 2021 17:35:13 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id C185D64FBF for ; Fri, 5 Feb 2021 17:35:12 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org C185D64FBF 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 E75D08D0002; Fri, 5 Feb 2021 12:35:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id E23C76B0085; Fri, 5 Feb 2021 12:35:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D3D338D0002; Fri, 5 Feb 2021 12:35:11 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0100.hostedemail.com [216.40.44.100]) by kanga.kvack.org (Postfix) with ESMTP id BAD0B6B0083 for ; Fri, 5 Feb 2021 12:35:11 -0500 (EST) Received: from smtpin12.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay01.hostedemail.com (Postfix) with ESMTP id 6FE9A180AD822 for ; Fri, 5 Feb 2021 17:35:11 +0000 (UTC) X-FDA: 77784915222.12.kite80_141448b275e6 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin12.hostedemail.com (Postfix) with ESMTP id 4CEFC18025E99 for ; Fri, 5 Feb 2021 17:35:11 +0000 (UTC) X-HE-Tag: kite80_141448b275e6 X-Filterd-Recvd-Size: 6549 Received: from mail-qv1-f73.google.com (mail-qv1-f73.google.com [209.85.219.73]) by imf06.hostedemail.com (Postfix) with ESMTP for ; Fri, 5 Feb 2021 17:35:10 +0000 (UTC) Received: by mail-qv1-f73.google.com with SMTP id d26so5528011qve.7 for ; Fri, 05 Feb 2021 09:35:10 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=ISHwxx5EnSKzUDUjZ1ysUZFyHBfF802klWxWhYA/428=; b=RwM46b1OK05tP9oh3ZvOyyqP5DeUCm5WEgXs9WIi2QDZ10b7a1JK5ykxxwcHrHcz9b eDMl73sT/lR0Hw6RbGi7n8FmxAtam+z6WvhhjYDN0jIge9F1av+HlKfQ0YVNHLJwTL4a wYltM3xRPcjR+cLRrDGpWcNlDeVQ0jGBPh5FGQNKahJiLulk3hX2Cp1yjBVRjR+/+5Jb tLWvCNv5ECt+IWcvAMdNWQlPComv7q0GUtli6S3pwLuKz9J3Ab96/KplUlWGVJ5DCNPP OUmhAtwjQXJraW/q+HNZTthQdlqCfh8OL9LVIJ6y66/8ZaTjbezpHKnepMfZm+KLEc5v HBiQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=ISHwxx5EnSKzUDUjZ1ysUZFyHBfF802klWxWhYA/428=; b=b0zC7hFJGtMhp4p4dND5tnN/r47zWzuQfn+733HFOgTetmLi1dkkz/H5cMrRe4E8Yq sEKTwHuXUh0Ft2qo/n3SlpZL+Fw5rtl3Z88cyFjmy2ESA74QdfduWjmkaWc1fi4cCOFu tFuxwQBNZc3VYNpApKkTvMkWGKA39d/pa16mvU94iKdhTtmqWAMTlMt2BWbpM+kOKuF1 MwK76yxpprjgAMz+A1eroHdzWVUZU82CNJHwsfwT0tOwxPouqq7XWoRKrXBN3IhOKySM FzqIvwASSKv1iCPiobPsLB7tJDOJe2cVISIED26TO5xcLOsIZwBxXriWbKvzAXQOaxNs f0cw== X-Gm-Message-State: AOAM5316o+4xxfo3SPlkkZjj/fXz96LkxYtdOvCNb1PEQvarEW4rqhyE ikf31Yg53XyWwE/tQU28bKLmjtu2LwXsvxTA X-Google-Smtp-Source: ABdhPJzeuGXvKwDVMKQZvPztFjBxnrWar5viffIlpldRL8/ioMWlXHOlQwfTr/fGJyK6XQY22n8CnOgwdlEcN5sq X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:edb8:b79c:2e20:e531]) (user=andreyknvl job=sendgmr) by 2002:a0c:c687:: with SMTP id d7mr5237396qvj.17.1612546510164; Fri, 05 Feb 2021 09:35:10 -0800 (PST) Date: Fri, 5 Feb 2021 18:34:42 +0100 In-Reply-To: Message-Id: <9bef90327c9cb109d736c40115684fd32f49e6b0.1612546384.git.andreyknvl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.30.0.365.g02bc693789-goog Subject: [PATCH v3 mm 08/13] kasan, mm: optimize krealloc poisoning From: Andrey Konovalov To: Andrew Morton , Catalin Marinas , Vincenzo Frascino , Dmitry Vyukov , Alexander Potapenko , Marco Elver Cc: 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, Andrey Konovalov 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: 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 --- 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; + 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; }