From patchwork Mon Nov 6 20:10:13 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: andrey.konovalov@linux.dev X-Patchwork-Id: 13447419 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id 4CB1DC4167D for ; Mon, 6 Nov 2023 20:10:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 32A246B028C; Mon, 6 Nov 2023 15:10:40 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 2D8236B028F; Mon, 6 Nov 2023 15:10:40 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 12CE86B028E; Mon, 6 Nov 2023 15:10:40 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0013.hostedemail.com [216.40.44.13]) by kanga.kvack.org (Postfix) with ESMTP id 016136B028A for ; Mon, 6 Nov 2023 15:10:39 -0500 (EST) Received: from smtpin10.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id CC1281CB464 for ; Mon, 6 Nov 2023 20:10:39 +0000 (UTC) X-FDA: 81428622198.10.B525C84 Received: from out-174.mta1.migadu.com (out-174.mta1.migadu.com [95.215.58.174]) by imf01.hostedemail.com (Postfix) with ESMTP id 78CF240015 for ; Mon, 6 Nov 2023 20:10:37 +0000 (UTC) Authentication-Results: imf01.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=mSKMvKXc; spf=pass (imf01.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.174 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1699301437; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=Hi9wxEG5hyOM24yVF5pvgyGm+UDfsaiMZW2KqzysWxs=; b=zPzZno7LehiRi2M1rcZWRlV2d6udJpvKcrERnfWH66hyoJTHueVTYzazcqOIbel3o+Z8aw fpiI5+Fe9kF+oakvnQHBIf05hsLdmckuyD1+y+DWvNJZibxKeWA4x0L63EHGxfeSYjeGxW tk23ly5yX4cL4iwXRDLr5Xd6xsCZS1w= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1699301437; a=rsa-sha256; cv=none; b=Udis7VZ1w99vWSl5lyhCkyoG05g28+Y3oTfm16DRiHL647p8RmwwA3b1yig5bnKS7I8z1C OGvXJMpG20R6YR7b9rnhSfBUS8x3ktKHShL42VOMiORBayThmrp2rgqEteZ9+xbvjU0UBp grFae/fjvaMQ48QXzAj/AbfW8s2DyKY= ARC-Authentication-Results: i=1; imf01.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=mSKMvKXc; spf=pass (imf01.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.174 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev; dmarc=pass (policy=none) header.from=linux.dev 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=1699301436; 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=Hi9wxEG5hyOM24yVF5pvgyGm+UDfsaiMZW2KqzysWxs=; b=mSKMvKXcLKjjTcyMkClrC36gOqKSe1PvNKyag40bJ4xtUzbPDJFYH5kFaBHKeVRgaBbWYV GIZXX51aID/GYcIpaL4fQ+N0sAMZRrmnx9/AYcnXe7Vz4JkkXEyYOmwLRJOMKQdsT311zO FHVzZWec6HA/wWLZY3SvOuHIPVsbkv8= 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 04/20] kasan: add return value for kasan_mempool_poison_object Date: Mon, 6 Nov 2023 21:10:13 +0100 Message-Id: <673406599a04fc9ea0111dac01ae9c84f9a01524.1699297309.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Stat-Signature: 19mxggu5khjz9hu3btwkw9anzq7kz6nc X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 78CF240015 X-Rspam-User: X-HE-Tag: 1699301437-264862 X-HE-Meta: U2FsdGVkX1+aXI69RTmgWMPG6qnGw9Sv175Po61zKw2VZxS0vAQ/ZPrRNK4eEXp8HjpDJ52BZose+QxKre3g7fWsKw8drtL4Qm/D43uK6k0hobbEC0RmG4TD2ulg9AijU5dpsGCqmN1C/tAh6lNW1hWKIIiZdcd40SUOmqoZ+UZMcLEV5qThe+9tjSiH3DvpQTq4khD+wK0mqAb3iIkExYbR+t+V0P6rh3XBOdgnDgIIQV0wwMqVONJHHD4X5Iafr7VvZ6+9eGUGJNfrVW9KOY6QmMFeUQiwkZRtr5LE+/I7yq5nBpRjjWoipfubiAZ7ZryDoODBiqjy/Z0BtOQYZ0kzNHq6dwjJ721ff9QX0QTkimv9mOeA1K0yVuRV7mFmHiOMvXVty7Ugu550lBPU5CqBVnaBAdth1n/ud5KphANV34nGSHS1jDkGjIsaBikRfIP0sJ2HcRFVg9i7+HcpiZZZ29k52NBBH8yDk/jVTrQ/WsX0W2dWaSxbNfAslQqfdE5MO7aUmEo7kpW+gi3PXT2nI/XpE4I7M8NwB1BjnwA6g2/+xM5EKrO+3TsCWQGlloYFPTP4Vq8S7pqazgp+2y0bOhzKnyMpRGtyTQEvb4RlwfEZltduoQf+bqb0K/NBxHPEqAg5MpSBUMrHZTfuK+7iUW1NBVcJywHA1/k7DqDsEYeb8CFGU3Bq64L3KEdWH2WJwmv8d/7o5Fwxe4scG4L1wCU7WhdjxQZv1rvaAvpJzBuD4yf1WjlLkT/st1aGFpWqBsJhEtgL1n2aLdUynaUQDAgUwb7T9sZNXJRKUvMqLNirQMqtZ1fi05CTcaJuLBkqd+b+dmhtHMyj1MvVdYDq9Bpe5l3IwLXfyzZMDFPqnk8iMKT0x6Nzg47AWDZXaYu2LvK0P8ibprLBOc1rsUDG3GP7Zd1mRoE0+Uc8QQGhuoB2zP0+grAFjH9opFGeM5lVRZFct69322y0Xs/ iGDemF+w Wd4Lh8W23+ME4zmMVUHSXD0AfDLdCrqtenm/I1J5hGDankRs3Q+rXjuwNEMsJoL4CBa4xa+2THu369+9dhxjdgh/Y46m3rrYKCZCgJytPD21MgvLy3L/CBpkTxYo/MtnR/DxQXtmN4Pvur7qaw9L2Y0PHr7Bxet+cRW4XFQCssh1kE68QhlxfZpxTAnTMEj44ljTi/ZPGzU9EC7zatquxzA+IFg== 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: List-Subscribe: List-Unsubscribe: From: Andrey Konovalov Add a return value for kasan_mempool_poison_object that lets the caller know whether the allocation is affected by a double-free or an invalid-free bug. The caller can use this return value to stop operating on the object. Also introduce a check_page_allocation helper function to improve the code readability. Signed-off-by: Andrey Konovalov --- include/linux/kasan.h | 17 ++++++++++++----- mm/kasan/common.c | 21 ++++++++++----------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index bbf6e2fa4ffd..33387e254caa 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -212,7 +212,7 @@ static __always_inline void * __must_check kasan_krealloc(const void *object, return (void *)object; } -void __kasan_mempool_poison_object(void *ptr, unsigned long ip); +bool __kasan_mempool_poison_object(void *ptr, unsigned long ip); /** * kasan_mempool_poison_object - Check and poison a mempool slab allocation. * @ptr: Pointer to the slab allocation. @@ -225,16 +225,20 @@ void __kasan_mempool_poison_object(void *ptr, unsigned long ip); * without putting it into the quarantine (for the Generic mode). * * This function also performs checks to detect double-free and invalid-free - * bugs and reports them. + * bugs and reports them. The caller can use the return value of this function + * to find out if the allocation is buggy. * * This function operates on all slab allocations including large kmalloc * allocations (the ones returned by kmalloc_large() or by kmalloc() with the * size > KMALLOC_MAX_SIZE). + * + * Return: true if the allocation can be safely reused; false otherwise. */ -static __always_inline void kasan_mempool_poison_object(void *ptr) +static __always_inline bool kasan_mempool_poison_object(void *ptr) { if (kasan_enabled()) - __kasan_mempool_poison_object(ptr, _RET_IP_); + return __kasan_mempool_poison_object(ptr, _RET_IP_); + return true; } /* @@ -293,7 +297,10 @@ static inline void *kasan_krealloc(const void *object, size_t new_size, { return (void *)object; } -static inline void kasan_mempool_poison_object(void *ptr) {} +static inline bool kasan_mempool_poison_object(void *ptr) +{ + return true; +} static inline bool kasan_check_byte(const void *address) { return true; diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 69f4c66f0da3..087f93629132 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -244,7 +244,7 @@ bool __kasan_slab_free(struct kmem_cache *cache, void *object, return ____kasan_slab_free(cache, object, ip, true, init); } -static inline bool ____kasan_kfree_large(void *ptr, unsigned long ip) +static inline bool check_page_allocation(void *ptr, unsigned long ip) { if (!kasan_arch_is_ready()) return false; @@ -259,17 +259,14 @@ static inline bool ____kasan_kfree_large(void *ptr, unsigned long ip) return true; } - /* - * The object will be poisoned by kasan_poison_pages() or - * kasan_mempool_poison_object(). - */ - return false; } void __kasan_kfree_large(void *ptr, unsigned long ip) { - ____kasan_kfree_large(ptr, ip); + check_page_allocation(ptr, ip); + + /* The object will be poisoned by kasan_poison_pages(). */ } void * __must_check __kasan_slab_alloc(struct kmem_cache *cache, @@ -419,7 +416,7 @@ void * __must_check __kasan_krealloc(const void *object, size_t size, gfp_t flag return ____kasan_kmalloc(slab->slab_cache, object, size, flags); } -void __kasan_mempool_poison_object(void *ptr, unsigned long ip) +bool __kasan_mempool_poison_object(void *ptr, unsigned long ip) { struct folio *folio; @@ -432,13 +429,15 @@ void __kasan_mempool_poison_object(void *ptr, unsigned long ip) * KMALLOC_MAX_SIZE, and kmalloc falls back onto page_alloc. */ if (unlikely(!folio_test_slab(folio))) { - if (____kasan_kfree_large(ptr, ip)) - return; + if (check_page_allocation(ptr, ip)) + return false; kasan_poison(ptr, folio_size(folio), KASAN_PAGE_FREE, false); + return true; } else { struct slab *slab = folio_slab(folio); - ____kasan_slab_free(slab->slab_cache, ptr, ip, false, false); + return !____kasan_slab_free(slab->slab_cache, ptr, ip, + false, false); } }