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=-26.3 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_GIT,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 EDC8FC433E9 for ; Mon, 1 Feb 2021 19:47:06 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id BBEA564EA8 for ; Mon, 1 Feb 2021 19:47:06 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232508AbhBATrA (ORCPT ); Mon, 1 Feb 2021 14:47:00 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:40310 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230118AbhBATpY (ORCPT ); Mon, 1 Feb 2021 14:45:24 -0500 Received: from mail-qk1-x749.google.com (mail-qk1-x749.google.com [IPv6:2607:f8b0:4864:20::749]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8CCFDC061793 for ; Mon, 1 Feb 2021 11:43:59 -0800 (PST) Received: by mail-qk1-x749.google.com with SMTP id 70so14118718qkh.4 for ; Mon, 01 Feb 2021 11:43:59 -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=wTS7rGEWy/lmgEwVXVBE+5vzcvue/e9GmeYKwLI3/GI=; b=NM/EWWXfIO5X0CDVwGsXDCTqh8MrojXHR19eXM6kgDQheLb3UgxElTkZafjlwvJVMh hRX5SjP8nw46ZrfNSlgGwyvk51E5mCcJjGRICuKoVrBb2YwSa9i4QDptcJtNxcTGLJrj CeY1HpjU4xlSLRVgBGfO5+GLBbY3Lp0oNfGToUpZKWmAgO6jg6PmK+eLPiDFfbniYtjj 0oFFptb75/P6D059sJVp3xp5O4JXmkbYqtDH8y1wuU6wR8rq4N02//J4aewnw8CWdPfK zE529RUGZuEtVrAuesBdVfQ/DsQqo1DTN6iIqW3q4M7STnfb18Ldteve0sFZw56OEXzt nmdw== 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=wTS7rGEWy/lmgEwVXVBE+5vzcvue/e9GmeYKwLI3/GI=; b=bI6NMPmfqKwBLNevS3+tqFSM4GnhXzceMbj/g2TyQ+n8GJBPlZev+e6KCJh/gxGRqd zPtKgb01cYg4Zb/k3b7jBYK6NBwCfHq+2Kn0eQpsUzKsblVEcVZVlfYcVzB35bJsGyCg Fwawa30oYohvnfF1cQlngqPzspfu0RJyXQBeUa5uqTXBTv2fCl4hpKVfehDFfPfuCgf5 Ly9HjhRdSE/jGXVW3lM3iL6c//QJU1pVVm3NMl0SXBfSdmCa3h51WYxOUD0xv0/RGgF4 dZ7B6VNuqvdDPrg1ipiMweYewMr2y2oScqgZr2NFxwWiAdkC9guaQD6oCVkzt5doY9Cb YFfA== X-Gm-Message-State: AOAM533Iju5/+cMF04S0vzaVVXAQkXDPlOLS6uhcOnzBUQFUiu06xCE3 vDGqCOd4kDnDLLr9aKi5BtBzAi3FRnQ7++07 X-Google-Smtp-Source: ABdhPJzxW58GoVDg9DXe3VlIIu6Zz+FBlV301B0hIass2XkT/B7p+K717pXSiFgUHOf4CH1xC9QxDtyYCzLn2SR8 Sender: "andreyknvl via sendgmr" X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:a05:6214:1110:: with SMTP id e16mr16696575qvs.62.1612208638752; Mon, 01 Feb 2021 11:43:58 -0800 (PST) Date: Mon, 1 Feb 2021 20:43:32 +0100 In-Reply-To: Message-Id: <431c6cfa0ac8fb2b33d7ab561a64aa84c844d1a0.1612208222.git.andreyknvl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.30.0.365.g02bc693789-goog Subject: [PATCH 08/12] kasan, mm: optimize krealloc poisoning From: Andrey Konovalov To: Catalin Marinas , Vincenzo Frascino , Dmitry Vyukov , Alexander Potapenko , Marco Elver Cc: 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, Andrey Konovalov Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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; + /* 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=-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 16E0DC433DB for ; Mon, 1 Feb 2021 19:44:03 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 9E25C64DBA for ; Mon, 1 Feb 2021 19:44:02 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 9E25C64DBA 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 903DB6B008A; Mon, 1 Feb 2021 14:44:00 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 7F36D6B008C; Mon, 1 Feb 2021 14:44:00 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 61A916B0092; Mon, 1 Feb 2021 14:44:00 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0111.hostedemail.com [216.40.44.111]) by kanga.kvack.org (Postfix) with ESMTP id 4A01B6B008A for ; Mon, 1 Feb 2021 14:44:00 -0500 (EST) Received: from smtpin04.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay05.hostedemail.com (Postfix) with ESMTP id 0FA7A181AEF1A for ; Mon, 1 Feb 2021 19:44:00 +0000 (UTC) X-FDA: 77770724640.04.bears68_5006aeb275c4 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin04.hostedemail.com (Postfix) with ESMTP id E3B4C8007DFB for ; Mon, 1 Feb 2021 19:43:59 +0000 (UTC) X-HE-Tag: bears68_5006aeb275c4 X-Filterd-Recvd-Size: 6534 Received: from mail-qk1-f202.google.com (mail-qk1-f202.google.com [209.85.222.202]) by imf09.hostedemail.com (Postfix) with ESMTP for ; Mon, 1 Feb 2021 19:43:59 +0000 (UTC) Received: by mail-qk1-f202.google.com with SMTP id a75so14097237qkg.16 for ; Mon, 01 Feb 2021 11:43:59 -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=wTS7rGEWy/lmgEwVXVBE+5vzcvue/e9GmeYKwLI3/GI=; b=NM/EWWXfIO5X0CDVwGsXDCTqh8MrojXHR19eXM6kgDQheLb3UgxElTkZafjlwvJVMh hRX5SjP8nw46ZrfNSlgGwyvk51E5mCcJjGRICuKoVrBb2YwSa9i4QDptcJtNxcTGLJrj CeY1HpjU4xlSLRVgBGfO5+GLBbY3Lp0oNfGToUpZKWmAgO6jg6PmK+eLPiDFfbniYtjj 0oFFptb75/P6D059sJVp3xp5O4JXmkbYqtDH8y1wuU6wR8rq4N02//J4aewnw8CWdPfK zE529RUGZuEtVrAuesBdVfQ/DsQqo1DTN6iIqW3q4M7STnfb18Ldteve0sFZw56OEXzt nmdw== 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=wTS7rGEWy/lmgEwVXVBE+5vzcvue/e9GmeYKwLI3/GI=; b=nRxzV5GLchrfnt6WAqjJg5aTy5jZ1zJQ+JIKalapA2bC8P1UwoOOSF1Xdz/ese/m8M FvaP6sUGyywalb3JhUJE6gFCNo5Wfi5qj6EQ68rewanIdRdnp6mL1fvd/rYE10h9N3i5 S4QCdeuS4C8SPDJBKXWrYP2FLAhBuscsCSIoRlJ4BDjtN61dMzxayV+kP26GCH2xkZUh kcTJhLTwv/roCwcGA1yrRNnOOmw4Z7eFcqEKZdgISqFN+kAis6DPpb00gOFlqY7Js+qh DfoL/yV/L+j6P9voF1Eus9U6t6yCiiUVYISfzs16H9i1HhlgWMh1YpWgH9tibB3Yx3Kx h5Yw== X-Gm-Message-State: AOAM530r9IXzapq0ur/7GVjbj+qheicO4faxM4ggse3Av/Wo5G0V9UVh Zi9vQqJCDMb3Ytu8bDw78+f0NRWv6EXVqsvM X-Google-Smtp-Source: ABdhPJzxW58GoVDg9DXe3VlIIu6Zz+FBlV301B0hIass2XkT/B7p+K717pXSiFgUHOf4CH1xC9QxDtyYCzLn2SR8 X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:a05:6214:1110:: with SMTP id e16mr16696575qvs.62.1612208638752; Mon, 01 Feb 2021 11:43:58 -0800 (PST) Date: Mon, 1 Feb 2021 20:43:32 +0100 In-Reply-To: Message-Id: <431c6cfa0ac8fb2b33d7ab561a64aa84c844d1a0.1612208222.git.andreyknvl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.30.0.365.g02bc693789-goog Subject: [PATCH 08/12] kasan, mm: optimize krealloc poisoning From: Andrey Konovalov To: Catalin Marinas , Vincenzo Frascino , Dmitry Vyukov , Alexander Potapenko , Marco Elver Cc: 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, Andrey Konovalov Content-Type: text/plain; charset="UTF-8" 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 --- 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; + /* 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=-17.1 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_GIT 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 8CA97C433DB for ; Mon, 1 Feb 2021 19:46:40 +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 036A464E3C for ; Mon, 1 Feb 2021 19:46:39 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 036A464E3C 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:To:From:Subject:References:Mime-Version:Message-Id: In-Reply-To:Date:Reply-To:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Owner; bh=7AMtUWa/BLz2OkfkN6Esy7QDmet9Z2Rje4xkPp65Xqc=; b=SV+lCXvSd4XSQHTF6FWFOeuQ+ ZGR/dP+8QhrA6V7GqpqyirUoYI/oM7LDuVMD8cwMfx6A7K/vL/VUa4Nyl+QZzxNJrQit/vtokbNd4 6tGyQV7u6vXZYgNGObyNOFRPgaw41+AflS/8ei4kxv/kZhjLvCm3PBXFVvq/X1uEDNPiFWfanUo2X 08f6CODq8vKM98qS9C9Z/pvv4lKUeuQDfAJpvQXLO60eCdzsMFeeS1tEZ85SNJnewzDuKRBn7NmNl jTIKXg6cfYl0xIxVE5VxVNHsnJQJ/Mj7N0vAtvYGsHkGd/2e+S0CzPBuN2iokXTHdG35YVPHlpWYL NPvIOTdZw==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1l6f7x-0002Nm-OV; Mon, 01 Feb 2021 19:44:49 +0000 Received: from mail-qt1-x84a.google.com ([2607:f8b0:4864:20::84a]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1l6f7C-0001xV-Ol for linux-arm-kernel@lists.infradead.org; Mon, 01 Feb 2021 19:44:04 +0000 Received: by mail-qt1-x84a.google.com with SMTP id d10so11428102qtx.8 for ; Mon, 01 Feb 2021 11:44:00 -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=wTS7rGEWy/lmgEwVXVBE+5vzcvue/e9GmeYKwLI3/GI=; b=NM/EWWXfIO5X0CDVwGsXDCTqh8MrojXHR19eXM6kgDQheLb3UgxElTkZafjlwvJVMh hRX5SjP8nw46ZrfNSlgGwyvk51E5mCcJjGRICuKoVrBb2YwSa9i4QDptcJtNxcTGLJrj CeY1HpjU4xlSLRVgBGfO5+GLBbY3Lp0oNfGToUpZKWmAgO6jg6PmK+eLPiDFfbniYtjj 0oFFptb75/P6D059sJVp3xp5O4JXmkbYqtDH8y1wuU6wR8rq4N02//J4aewnw8CWdPfK zE529RUGZuEtVrAuesBdVfQ/DsQqo1DTN6iIqW3q4M7STnfb18Ldteve0sFZw56OEXzt nmdw== 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=wTS7rGEWy/lmgEwVXVBE+5vzcvue/e9GmeYKwLI3/GI=; b=jUjCGlnk2IvgHw1n5mP+jTbEq34vHZLjmGEvLg24CcSFKIp5dMLO5Ln8+ZjzMdvyWm o0ZHo58G0Ui05LdoOIeM+egGguGGYGzDXiFoO5t/Sy+sW42EksgKW4mH1P14Rv0pDMPj I0bf3ZMPNn9f7xAe7kS7X5KJ4SEtVuF5EBv2Q2ez0sqdTLRQaAWWPGdhcICJ9ViUXwtC MbxQfyPK/1DeJ82EA0g7JnWIiCVgq9JvpDsWxzCUSZ1wzojA6+kvN2REw1VbnHf9hIRL ZpwUDJlw3YEBYXF0GpakjF9ghBiBG4HCdj/jOOVHf41UhF6yfaFtHBqFiP5mEbryLgy9 JlbA== X-Gm-Message-State: AOAM5337pSaHsGIfqvY84hxrGBg+kA2RTlUZAMS/gOh7HBlWc+LIbUKn SSn9QEcJRcS2eQHdMGCBiFsNTTGlXOkx/yNE X-Google-Smtp-Source: ABdhPJzxW58GoVDg9DXe3VlIIu6Zz+FBlV301B0hIass2XkT/B7p+K717pXSiFgUHOf4CH1xC9QxDtyYCzLn2SR8 X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:7220:84ff:fe09:7e9d]) (user=andreyknvl job=sendgmr) by 2002:a05:6214:1110:: with SMTP id e16mr16696575qvs.62.1612208638752; Mon, 01 Feb 2021 11:43:58 -0800 (PST) Date: Mon, 1 Feb 2021 20:43:32 +0100 In-Reply-To: Message-Id: <431c6cfa0ac8fb2b33d7ab561a64aa84c844d1a0.1612208222.git.andreyknvl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.30.0.365.g02bc693789-goog Subject: [PATCH 08/12] kasan, mm: optimize krealloc poisoning From: Andrey Konovalov To: Catalin Marinas , Vincenzo Frascino , Dmitry Vyukov , Alexander Potapenko , Marco Elver X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20210201_144402_891781_7C77D0AD X-CRM114-Status: GOOD ( 20.51 ) 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: Branislav Rankov , Andrey Konovalov , Kevin Brodsky , Will Deacon , linux-kernel@vger.kernel.org, kasan-dev@googlegroups.com, linux-mm@kvack.org, linux-arm-kernel@lists.infradead.org, Andrey Ryabinin , Andrew Morton , Peter Collingbourne , Evgenii Stepanov 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 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; + /* 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