From patchwork Fri Feb 5 15:39:06 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrey Konovalov X-Patchwork-Id: 12070355 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 60F1EC433DB for ; Fri, 5 Feb 2021 15:41:31 +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 EB47865093 for ; Fri, 5 Feb 2021 15:41:30 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org EB47865093 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=xqBMiEuVcgUu/PQiGlfRYY6pvYtxBSo9cjVkUWsCzQ4=; b=fkf7Z4/IjFV1kMLIhJ09PILiq +zUP2P5eDxFyhROXp5jdIW+Fc2Flty8t+/bEpECbf1DF8KFG3ISN+2DHTyduLy9bLYtAucx2iOtlq gXPcF81FjwgIG7Iw+N6XUmzGLh3Z+y4fDTuhMmtaMIz0NEh6o8HiFImZNTbFUtXhkJzL7Qhr6drjM osPj2l4npddu8+AwzNiDhC74cmMRiSKID0JJbj/Vc4cmnqzszghGVtW+9HIM80JBxDh3IUMQFY4gX xmPI8PC4TqKTpvh3hjOCyxk0E6C2ZMfKL7uEQIIv3by9/YNRuIgeJMgMtAQL6hcUON8STRtO0f1J+ IkcGb51qQ==; Received: from localhost ([::1] helo=merlin.infradead.org) by merlin.infradead.org with esmtp (Exim 4.92.3 #3 (Red Hat Linux)) id 1l83DP-00082w-DG; Fri, 05 Feb 2021 15:40:11 +0000 Received: from mail-wm1-x349.google.com ([2a00:1450:4864:20::349]) by merlin.infradead.org with esmtps (Exim 4.92.3 #3 (Red Hat Linux)) id 1l83Cn-0007lu-57 for linux-arm-kernel@lists.infradead.org; Fri, 05 Feb 2021 15:39:34 +0000 Received: by mail-wm1-x349.google.com with SMTP id y9so3108685wmj.7 for ; Fri, 05 Feb 2021 07:39:30 -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=kg30/g/LrsxD86gPJMTES5VW5Hgvjnk4vM4kmv21+Z4=; b=LCEFnlDaJcG0Q41NRa+Ch0e8o0QaxKPH4VLE5PeiB/2TOutaBSjhOVNhtlIuqskKNL phpumV6RzdrEdBMlzF0JJ/UV8Gi5lGDAW74e0LAzZYz+t1c3Y9oGFxEzdOri6TmYTr5U na1Uut/9hwzYcfOIuhkjF9fJjKOrh+9Tqp8O7hNSKLlOFpHYyFP5w0AVgMSjecylbZlj pFazgfiM/L2igEvCu3/YSfXsjME4M0Weph+1nBqRsxnRS5ZoqKSs/FwCT7YXTazQ1F47 i3VS6u9Ncn4Er7IZkPDPnp8tQRJvJXTxGiJK3gP2p+0V3VZSUOiVr8uO6j1rSNg8VTTG ++5A== 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=kg30/g/LrsxD86gPJMTES5VW5Hgvjnk4vM4kmv21+Z4=; b=eWTpYQm/XFvrxtsNAVqmwsYz3pvipred5bsR5Le7f2Xc6pJoAoYKdeQ7iH0ChbOcFd e3m2LGRbMRaXIVVolFlTg3z3ZbAtQ7ClqAgFau0R5Q/1P4QcuU6TBku8bzRCuIROaNjd wGapWJCgSlfFLIUfs+VeD2BrkmYzUv7yb5QuAFfdTfR4hbrY3t0bRBpIukuQovbIWmjb h/3HKGqsvswcV1tsES8oNz4qsxPNO/F1nXvMtJ+adqlUWdr5Yr9svmZ7qA7T7adDbeXF E+RAcnZwpHVxhCwDzxw9HmwFZHEFtj2esDgg9TSoZMQ2f32t+TUDuG33c3nNHT5TPUIe MgCg== X-Gm-Message-State: AOAM5323YerTUWse3VpUEsvop2N7TPRcwHEOfF9eAXwg8EW550HN21P7 oreccQMrZrmkSMoVzsNnjResEB3gxLUDgooJ X-Google-Smtp-Source: ABdhPJwwKKgQGlKIo0fdvx1Js8AFJ3EgmJEbKeq2cWI3dxu4cKp8SA/tbBXB1FczDscVCzjNnOVVknB1qI6ECJfd X-Received: from andreyknvl3.muc.corp.google.com ([2a00:79e0:15:13:edb8:b79c:2e20:e531]) (user=andreyknvl job=sendgmr) by 2002:a7b:c2aa:: with SMTP id c10mr3901926wmk.101.1612539569588; Fri, 05 Feb 2021 07:39:29 -0800 (PST) Date: Fri, 5 Feb 2021 16:39:06 +0100 In-Reply-To: Message-Id: <492dcd5030419c5421a3762457c0ff1a7c91e628.1612538932.git.andreyknvl@google.com> Mime-Version: 1.0 References: X-Mailer: git-send-email 2.30.0.365.g02bc693789-goog Subject: [PATCH v2 05/12] kasan: unify large kfree checks From: Andrey Konovalov To: Andrew Morton , 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-20210205_103933_262305_CDF306FA X-CRM114-Status: GOOD ( 16.21 ) 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 , Peter Collingbourne , Evgenii Stepanov Sender: "linux-arm-kernel" Errors-To: linux-arm-kernel-bounces+linux-arm-kernel=archiver.kernel.org@lists.infradead.org Unify checks in kasan_kfree_large() and in kasan_slab_free_mempool() for large allocations as it's done for small kfree() allocations. With this change, kasan_slab_free_mempool() starts checking that the first byte of the memory that's being freed is accessible. Reviewed-by: Marco Elver Signed-off-by: Andrey Konovalov --- include/linux/kasan.h | 16 ++++++++-------- mm/kasan/common.c | 36 ++++++++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index 2d5de4092185..d53ea3c047bc 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -200,6 +200,13 @@ static __always_inline bool kasan_slab_free(struct kmem_cache *s, void *object) return false; } +void __kasan_kfree_large(void *ptr, unsigned long ip); +static __always_inline void kasan_kfree_large(void *ptr) +{ + if (kasan_enabled()) + __kasan_kfree_large(ptr, _RET_IP_); +} + void __kasan_slab_free_mempool(void *ptr, unsigned long ip); static __always_inline void kasan_slab_free_mempool(void *ptr) { @@ -247,13 +254,6 @@ static __always_inline void * __must_check kasan_krealloc(const void *object, return (void *)object; } -void __kasan_kfree_large(void *ptr, unsigned long ip); -static __always_inline void kasan_kfree_large(void *ptr) -{ - if (kasan_enabled()) - __kasan_kfree_large(ptr, _RET_IP_); -} - /* * Unlike kasan_check_read/write(), kasan_check_byte() is performed even for * the hardware tag-based mode that doesn't rely on compiler instrumentation. @@ -302,6 +302,7 @@ static inline bool kasan_slab_free(struct kmem_cache *s, void *object) { return false; } +static inline void kasan_kfree_large(void *ptr) {} static inline void kasan_slab_free_mempool(void *ptr) {} static inline void *kasan_slab_alloc(struct kmem_cache *s, void *object, gfp_t flags) @@ -322,7 +323,6 @@ static inline void *kasan_krealloc(const void *object, size_t new_size, { return (void *)object; } -static inline void kasan_kfree_large(void *ptr) {} static inline bool kasan_check_byte(const void *address) { return true; diff --git a/mm/kasan/common.c b/mm/kasan/common.c index da24b144d46c..7ea643f7e69c 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -364,6 +364,31 @@ bool __kasan_slab_free(struct kmem_cache *cache, void *object, unsigned long ip) return ____kasan_slab_free(cache, object, ip, true); } +static bool ____kasan_kfree_large(void *ptr, unsigned long ip) +{ + if (ptr != page_address(virt_to_head_page(ptr))) { + kasan_report_invalid_free(ptr, ip); + return true; + } + + if (!kasan_byte_accessible(ptr)) { + kasan_report_invalid_free(ptr, ip); + return true; + } + + /* + * The object will be poisoned by kasan_free_pages() or + * kasan_slab_free_mempool(). + */ + + return false; +} + +void __kasan_kfree_large(void *ptr, unsigned long ip) +{ + ____kasan_kfree_large(ptr, ip); +} + void __kasan_slab_free_mempool(void *ptr, unsigned long ip) { struct page *page; @@ -377,10 +402,8 @@ void __kasan_slab_free_mempool(void *ptr, unsigned long ip) * KMALLOC_MAX_SIZE, and kmalloc falls back onto page_alloc. */ if (unlikely(!PageSlab(page))) { - if (ptr != page_address(page)) { - kasan_report_invalid_free(ptr, ip); + if (____kasan_kfree_large(ptr, ip)) return; - } kasan_poison(ptr, page_size(page), KASAN_FREE_PAGE); } else { ____kasan_slab_free(page->slab_cache, ptr, ip, false); @@ -539,13 +562,6 @@ void * __must_check __kasan_krealloc(const void *object, size_t size, gfp_t flag return ____kasan_kmalloc(page->slab_cache, object, size, flags); } -void __kasan_kfree_large(void *ptr, unsigned long ip) -{ - if (ptr != page_address(virt_to_head_page(ptr))) - kasan_report_invalid_free(ptr, ip); - /* The object will be poisoned by kasan_free_pages(). */ -} - bool __kasan_check_byte(const void *address, unsigned long ip) { if (!kasan_byte_accessible(address)) {