Received: by 2002:a05:7412:8521:b0:e2:908c:2ebd with SMTP id t33csp2375475rdf; Mon, 6 Nov 2023 12:12:25 -0800 (PST) X-Google-Smtp-Source: AGHT+IF4aTIANH549G3rw+v+QzvlR+cFYP1Buq07HZ4HG60joCIX/I7CiwJhMeUmkipkKuggkBKF X-Received: by 2002:a17:90b:2541:b0:280:7cd2:429 with SMTP id nw1-20020a17090b254100b002807cd20429mr746541pjb.18.1699301544991; Mon, 06 Nov 2023 12:12:24 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1699301544; cv=none; d=google.com; s=arc-20160816; b=Jb4KYV1BCu73vz9NaxJL7gd32ZKBIT8ySf11r+NfyTB0nTUCg6sUlm55n7Hfur7NZV xw9zGDc1ybwY8wbPwclD2wSHLRSaaAsHJoWJ8CGw/KxBEv8AnW45A9OJH6mlthqAZNor 3B76I+NvF6XionTrSlObOhxzecv8D5BPexEYf9admoPnyMCQxjmXUUbp9HBMsXltuX4M V2wj6pZ5JWSJRvwLL8fRsl514DFoxoJv7Q26W6e9Yf47dw/YzLHpFszMO0Jp8XF97Peq 39PfvXWAu5DwSES80TC1bzEEBdLSODa6iaHpNgYatZHqXafPXHV/TC0caF7SwK/r+N0C y/mQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=wOW8bM5LUt0/83cpSlqfBj0DKQkCrWZjISfEAI+z8F8=; fh=n5KN85tQTomq0Sa/nFFC9xbnc77mxkBzF8HoolO/QOw=; b=lqVezQGs+TjABoU7I/Zw+aqiEdQ+v+h07JQSVfURd+/26cFKfkMBWkOyjoUhcLVTxH sOCBXz9gsTUd0wyZ4L9oAfEijTs4JkUVwQOY9ENGqVHuNc4ODnQVagUwiZHlT/EvJV0m UO4uIuUhC8nmosSNzLBUeH9Bx3TpYyHQuwCPBSCropS5cHLScFkyY498DKyjY4TN4Bl6 u3aJ2quEB1E/F+iQDtwvmw9E6Ugt1trbl+Mtltu0dbjf/hRmycpRo23RRvKdiN/AftSg Rd+bYtBJ74AC42qPOACW68VMdd1/SBJweuky+FjMVS5RfuUld9Q94lJf6aHPu8dM9K0m 3Bwg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=whzKutAZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Return-Path: Received: from agentk.vger.email (agentk.vger.email. [23.128.96.32]) by mx.google.com with ESMTPS id n13-20020a654ccd000000b00578e4c1581bsi393036pgt.866.2023.11.06.12.12.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 06 Nov 2023 12:12:24 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) client-ip=23.128.96.32; Authentication-Results: mx.google.com; dkim=pass header.i=@linux.dev header.s=key1 header.b=whzKutAZ; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.32 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linux.dev Received: from out1.vger.email (depot.vger.email [IPv6:2620:137:e000::3:0]) by agentk.vger.email (Postfix) with ESMTP id DBE0180BB3FA; Mon, 6 Nov 2023 12:12:15 -0800 (PST) X-Virus-Status: Clean X-Virus-Scanned: clamav-milter 0.103.10 at agentk.vger.email Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232959AbjKFULu (ORCPT + 99 others); Mon, 6 Nov 2023 15:11:50 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41218 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233009AbjKFULp (ORCPT ); Mon, 6 Nov 2023 15:11:45 -0500 Received: from out-180.mta1.migadu.com (out-180.mta1.migadu.com [IPv6:2001:41d0:203:375::b4]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 94D8210C2 for ; Mon, 6 Nov 2023 12:11:40 -0800 (PST) X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1699301498; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=wOW8bM5LUt0/83cpSlqfBj0DKQkCrWZjISfEAI+z8F8=; b=whzKutAZQjowRwHa43pazOIwGIr0bQTB71FOqN2VXFxSHeCtofhsbtvlLPo7irVUIhiyw6 pancxW7Jzm82f5iCk4fHMKj/CtH9ujxzi66CbOwDe/5jAaY5Ge9UJvVHW0GX5ta8Tzklfq sce+wX6Wcualha3H+6sfIg6SEBbZJSo= From: andrey.konovalov@linux.dev To: Marco Elver , Alexander Potapenko Cc: Andrey Konovalov , Dmitry Vyukov , Andrey Ryabinin , kasan-dev@googlegroups.com, Evgenii Stepanov , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH RFC 06/20] kasan: introduce kasan_mempool_poison_pages Date: Mon, 6 Nov 2023 21:10:15 +0100 Message-Id: <3a377ec3223f9c98f3ac471845e084abe7fd6fe0.1699297309.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Migadu-Flow: FLOW_OUT X-Spam-Status: No, score=-0.9 required=5.0 tests=DKIM_SIGNED,DKIM_VALID, DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on agentk.vger.email Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-Greylist: Sender passed SPF test, not delayed by milter-greylist-4.6.4 (agentk.vger.email [0.0.0.0]); Mon, 06 Nov 2023 12:12:16 -0800 (PST) From: Andrey Konovalov Introduce and document a kasan_mempool_poison_pages hook to be used by the mempool code instead of kasan_poison_pages. Compated to kasan_poison_pages, the new hook: 1. For the tag-based modes, skips checking and poisoning allocations that were not tagged due to sampling. 2. Checks for double-free and invalid-free bugs. In the future, kasan_poison_pages can also be updated to handle #2, but this is out-of-scope of this series. Signed-off-by: Andrey Konovalov --- include/linux/kasan.h | 27 +++++++++++++++++++++++++++ mm/kasan/common.c | 23 +++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index c5fe303bc1c2..de2a695ad34d 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -212,6 +212,29 @@ static __always_inline void * __must_check kasan_krealloc(const void *object, return (void *)object; } +bool __kasan_mempool_poison_pages(struct page *page, unsigned int order, + unsigned long ip); +/** + * kasan_mempool_poison_pages - Check and poison a mempool page allocation. + * @page: Pointer to the page allocation. + * @order: Order of the allocation. + * + * This function is intended for kernel subsystems that cache page allocations + * to reuse them instead of freeing them back to page_alloc (e.g. mempool). + * + * This function is similar to kasan_mempool_poison_object() but operates on + * page allocations. + * + * Return: true if the allocation can be safely reused; false otherwise. + */ +static __always_inline bool kasan_mempool_poison_pages(struct page *page, + unsigned int order) +{ + if (kasan_enabled()) + return __kasan_mempool_poison_pages(page, order, _RET_IP_); + return true; +} + bool __kasan_mempool_poison_object(void *ptr, unsigned long ip); /** * kasan_mempool_poison_object - Check and poison a mempool slab allocation. @@ -326,6 +349,10 @@ static inline void *kasan_krealloc(const void *object, size_t new_size, { return (void *)object; } +static inline bool kasan_mempool_poison_pages(struct page *page, unsigned int order) +{ + return true; +} static inline bool kasan_mempool_poison_object(void *ptr) { return true; diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 033c860afe51..9ccc78b20cf2 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -416,6 +416,29 @@ void * __must_check __kasan_krealloc(const void *object, size_t size, gfp_t flag return ____kasan_kmalloc(slab->slab_cache, object, size, flags); } +bool __kasan_mempool_poison_pages(struct page *page, unsigned int order, + unsigned long ip) +{ + unsigned long *ptr; + + if (unlikely(PageHighMem(page))) + return true; + + /* Bail out if allocation was excluded due to sampling. */ + if (!IS_ENABLED(CONFIG_KASAN_GENERIC) && + page_kasan_tag(page) == KASAN_TAG_KERNEL) + return true; + + ptr = page_address(page); + + if (check_page_allocation(ptr, ip)) + return false; + + kasan_poison(ptr, PAGE_SIZE << order, KASAN_PAGE_FREE, false); + + return true; +} + bool __kasan_mempool_poison_object(void *ptr, unsigned long ip) { struct folio *folio; -- 2.25.1