From patchwork Mon Nov 6 20:10:18 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: 13447423 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 D9F66C4332F for ; Mon, 6 Nov 2023 20:11:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 377526B0295; Mon, 6 Nov 2023 15:11:44 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 327026B0296; Mon, 6 Nov 2023 15:11:44 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1A0EA6B0297; Mon, 6 Nov 2023 15:11:44 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 00FB36B0295 for ; Mon, 6 Nov 2023 15:11:43 -0500 (EST) Received: from smtpin02.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id CC580C08C7 for ; Mon, 6 Nov 2023 20:11:43 +0000 (UTC) X-FDA: 81428624886.02.1C2F2E1 Received: from out-185.mta1.migadu.com (out-185.mta1.migadu.com [95.215.58.185]) by imf27.hostedemail.com (Postfix) with ESMTP id DBFE24001C for ; Mon, 6 Nov 2023 20:11:41 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=uCxP18Kq; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf27.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.185 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1699301502; 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=22d9hHWXVi8zazHV61QMl0VMY5WvMEtggwgAvi4OURE=; b=UbFpyEzDRMwQXRbAXvKv/Q25OQ6h5X96oLj6o8V0VCOrnkMZWPdk4C4TdFVSwZkJBM6rnx Frne1lcLBMOsoSuwwS7gPHwoKqCTvHOMbRolX4FWsxYtfYGK0/84eGL5jWpNC2dZW0OBZ3 QY7npa+JLSRF2wKPpj9/SY2Atqc+x4Q= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=uCxP18Kq; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf27.hostedemail.com: domain of andrey.konovalov@linux.dev designates 95.215.58.185 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1699301502; a=rsa-sha256; cv=none; b=QQrvs5ORUpe1UjCB70C8PdWtQq2IJ3fCX92BhRKDty2qro9rByN9WxRNzkXGNiRQoePkyH 6HnUHrx776mUAPrFRlfLll34hVlFcH2kS2mfRuJF9OQqH0RVQ0nF/M6ZU/uEVvLu7QCa0d afZvHtpfcjljfgcylgWsT5Yhg82oqYc= 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=1699301500; 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=22d9hHWXVi8zazHV61QMl0VMY5WvMEtggwgAvi4OURE=; b=uCxP18KqioH8DesdZGPrQhB9iAWKsetIiyp+iZWAll3HfZniU6A5CfSfVvg/YBXwUWgOgp g7au6m059IPietJorP9ETACRyTK+wluf8gUh/M/fKwcPWO9JGxfNf9EdEiOA8l56yCB+on SoSVmK/iZ+6LRI96vDxmC3Xr0mWe7BI= 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 09/20] kasan: save free stack traces for slab mempools Date: Mon, 6 Nov 2023 21:10:18 +0100 Message-Id: <52d80b4c1d42b10b6c2c3cba57b628a88cb58e03.1699297309.git.andreyknvl@google.com> In-Reply-To: References: MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspam-User: X-Rspamd-Server: rspam12 X-Rspamd-Queue-Id: DBFE24001C X-Stat-Signature: ftwta6poagugi9n78jebho7qowskffb7 X-HE-Tag: 1699301501-515194 X-HE-Meta: U2FsdGVkX1+NU6SUytIbAVSA9MZET5FCgPwuXp6J6qSt7v9Rr9DZgrT8w0DF1wfT7aa+ZGP43MS8f7dyBUmPrRHBFt/578B1hTHtZ2CW5Xgys3PWS/7voJJqTK0oxn1XNp8+Khs5U3THBobwvNSPYqBNl+ry1qhCRlD5kLoCgg0Mvb1WvMkzBGcxSOYNNOKoPQHqYaqADEvpUUY4U3DsiOCbrmOB/7IPk4sQwzHOK9q3wEoQAC/+C2s/irSdEesJwYsWceXJ8QeLofTfAbFWqb4Brz8bfvhNLONOazjVSoOaUD7M6Xs06irytwsOjjS33msjOBg0eyUYmrEjK3cD1v5segwRcoT+6Zuaqo2aY8KXEpuUgHcv6IAzqC9IPPnL+Yse89lOfJgZ6qE/Y2OSVfelq0K8hqa6XGbPTzoB6iHIcO4olV1O7Hxv0okxZlMS7qRGihLGWYNvjYWLTzd7Ts4XwzrvP3v8j3zUg/WHodACaWcs+pgMDwRgXl1JC/li5pJZCVEF8syK2zUA6uCyvWV+eeoz7IDO5nv3Yfx1wt+Uz9ESzSGZ6L6uMeVUESxSuJSaEoevw9ueG/UyPaCshFzpNCj5c6D4XCx7otQA4yoww9z9xbPpOfbjfHbf4j7uDzf8ruxHz6rkmpYKuRV73Y5ofaO3d8CZBS0DwMc/yR5McZBJk3MZDG3Vxl0vd+LMrP2q/EZ0S5uuCEJGZcSO1J38ANX+/BdqRHlYz++y3Jh/1TofBtNJcBnStD44dwJYeVVKhEAcQ61wuYzqWziMibR51ayqlHoS7mn086KEk+Lcs6+fI8xO3sNMFUKRUC2Ai1YXAioe8Ab2h0q2fdG4DR246cNXE4f/q4TCxHLskXj2EA0Kvs2H/z+NxaXQvZG+EbW6rN93bPplZ8JXHpWNK/dY27M4bwLTA7nCAkFNE983pGAvejwH9Mwl3NxbVGSQvDqiQ/DNbJxH5IMe2Ru nxQ9N+xi LZaIv8ZSAfD0otBeDyc+uxyEpnuqf6o72Qce42VqXmNoaNcEhxF77N01f5kfeeM4QP/q+3jaH7xSsriqDwNAoaiSvNsDrpyMf0gbajcujiTcRLpM2LyyRJ/i4cg487zVyXDyV1hwIh2QME8w2Zc4yYKl18VcsslpJJCQrvoch6wFndWs= 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 Make kasan_mempool_poison_object save free stack traces for slab and kmalloc mempools when the object is freed into the mempool. Also simplify and rename ____kasan_slab_free to poison_slab_object and do a few other reability changes. Signed-off-by: Andrey Konovalov --- include/linux/kasan.h | 5 +++-- mm/kasan/common.c | 20 +++++++++----------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/include/linux/kasan.h b/include/linux/kasan.h index f8ebde384bd7..e636a00e26ba 100644 --- a/include/linux/kasan.h +++ b/include/linux/kasan.h @@ -268,8 +268,9 @@ bool __kasan_mempool_poison_object(void *ptr, unsigned long ip); * to reuse them instead of freeing them back to the slab allocator (e.g. * mempool). * - * This function poisons a slab allocation without initializing its memory and - * without putting it into the quarantine (for the Generic mode). + * This function poisons a slab allocation and saves a free stack trace for it + * without initializing the allocation's memory and 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. The caller can use the return value of this function diff --git a/mm/kasan/common.c b/mm/kasan/common.c index 7c28d0a5af2c..683d0dad32f2 100644 --- a/mm/kasan/common.c +++ b/mm/kasan/common.c @@ -197,8 +197,8 @@ void * __must_check __kasan_init_slab_obj(struct kmem_cache *cache, return (void *)object; } -static inline bool ____kasan_slab_free(struct kmem_cache *cache, void *object, - unsigned long ip, bool quarantine, bool init) +static inline bool poison_slab_object(struct kmem_cache *cache, void *object, + unsigned long ip, bool init) { void *tagged_object; @@ -211,13 +211,12 @@ static inline bool ____kasan_slab_free(struct kmem_cache *cache, void *object, if (is_kfence_address(object)) return false; - if (unlikely(nearest_obj(cache, virt_to_slab(object), object) != - object)) { + if (unlikely(nearest_obj(cache, virt_to_slab(object), object) != object)) { kasan_report_invalid_free(tagged_object, ip, KASAN_REPORT_INVALID_FREE); return true; } - /* RCU slabs could be legally used after free within the RCU period */ + /* RCU slabs could be legally used after free within the RCU period. */ if (unlikely(cache->flags & SLAB_TYPESAFE_BY_RCU)) return false; @@ -229,19 +228,18 @@ static inline bool ____kasan_slab_free(struct kmem_cache *cache, void *object, kasan_poison(object, round_up(cache->object_size, KASAN_GRANULE_SIZE), KASAN_SLAB_FREE, init); - if ((IS_ENABLED(CONFIG_KASAN_GENERIC) && !quarantine)) - return false; - if (kasan_stack_collection_enabled()) kasan_save_free_info(cache, tagged_object); - return kasan_quarantine_put(cache, object); + return false; } bool __kasan_slab_free(struct kmem_cache *cache, void *object, unsigned long ip, bool init) { - return ____kasan_slab_free(cache, object, ip, true, init); + bool buggy_object = poison_slab_object(cache, object, ip, init); + + return buggy_object ? true : kasan_quarantine_put(cache, object); } static inline bool check_page_allocation(void *ptr, unsigned long ip) @@ -462,7 +460,7 @@ bool __kasan_mempool_poison_object(void *ptr, unsigned long ip) } slab = folio_slab(folio); - return !____kasan_slab_free(slab->slab_cache, ptr, ip, false, false); + return !poison_slab_object(slab->slab_cache, ptr, ip, false); } void __kasan_mempool_unpoison_object(void *ptr, size_t size, unsigned long ip)