From patchwork Wed Nov 22 23:12:02 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: 13465550 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 634BAC27C40 for ; Wed, 22 Nov 2023 23:12:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AC1A56B028E; Wed, 22 Nov 2023 18:12:12 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id A48F86B0296; Wed, 22 Nov 2023 18:12:12 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 8E9D66B029C; Wed, 22 Nov 2023 18:12:12 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id 766616B028E for ; Wed, 22 Nov 2023 18:12:12 -0500 (EST) Received: from smtpin13.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id 4B50B12037C for ; Wed, 22 Nov 2023 23:12:12 +0000 (UTC) X-FDA: 81487140504.13.70A8C6C Received: from out-177.mta0.migadu.com (out-177.mta0.migadu.com [91.218.175.177]) by imf19.hostedemail.com (Postfix) with ESMTP id 48D091A001B for ; Wed, 22 Nov 2023 23:12:10 +0000 (UTC) Authentication-Results: imf19.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=EbjD+Kjt; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf19.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.177 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=1700694730; 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:references:dkim-signature; bh=uNmdlwYGwAMCb5jza2UGKjePd3PtCVqVMGoJzVDye+g=; b=YNds9PzU66d8ILv2Z9YhBcSOHewHU9bnrGW8i7oEtfAm1fmuYvlaZFtRF+EGXpJluDzcvh O8tYl4DBh3b5Uf5GL0aRbcGLp0mCU+Xn5+3Gg5vxr+LkBWMDS3PYRpXvLiQ6UHDmvT8CYP ZjN1I06NpBgl6mTy0KNTo8sdG1WdnjY= ARC-Authentication-Results: i=1; imf19.hostedemail.com; dkim=pass header.d=linux.dev header.s=key1 header.b=EbjD+Kjt; dmarc=pass (policy=none) header.from=linux.dev; spf=pass (imf19.hostedemail.com: domain of andrey.konovalov@linux.dev designates 91.218.175.177 as permitted sender) smtp.mailfrom=andrey.konovalov@linux.dev ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1700694730; a=rsa-sha256; cv=none; b=XI9POzJDcKW6XgWmm6tQVOKPFzsCN4GUb9F2GvhztNFwZckHIBcrDtf9rDieibHwIHDxRL WPV1+EJxnTF+TEo/ONePyM0o9qjLjICDcC3mwQZp7PgWzQM3BWcgMN1m+9EZ6Gf0GpbMSj cWm01DnZja3VNFRoxxh5lbPHMABDE20= 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=1700694727; 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; bh=uNmdlwYGwAMCb5jza2UGKjePd3PtCVqVMGoJzVDye+g=; b=EbjD+KjtUReYpy3VEP2zh1PWDPdocfuDXn//2UykrrShxpEvbz8Lt2fOjSTIqAaqQQSP7V CYH/tF+RfK9UBYjQWzumEnZ/lRQqdOcxNIy6bxBqR8zc55i8kVhf3qeEOvvYkfiviv7XtT J/+WXzYfqAyKcZ3HAi6QMawrfjd1sEU= From: andrey.konovalov@linux.dev To: Andrew Morton Cc: Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Vlastimil Babka , kasan-dev@googlegroups.com, Evgenii Stepanov , Oscar Salvador , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Feng Tang , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Andrey Konovalov Subject: [PATCH mm] slub, kasan: improve interaction of KASAN and slub_debug poisoning Date: Thu, 23 Nov 2023 00:12:02 +0100 Message-Id: <20231122231202.121277-1-andrey.konovalov@linux.dev> MIME-Version: 1.0 X-Migadu-Flow: FLOW_OUT X-Rspamd-Queue-Id: 48D091A001B X-Rspam-User: X-Rspamd-Server: rspam02 X-Stat-Signature: 76eramu7gcxasot5ag37ukhe3ubwu4bq X-HE-Tag: 1700694730-347429 X-HE-Meta: U2FsdGVkX1/A2hJT1NsT42AnTPSwM+KNxWUWGK7R0xCNTxXWlWj+I9t+hYPh14Q+ct+JNQ0lU8VlBMk7dk4y9yzI2z9RDJ7vnfe+RJMkpdTxeIiKq08hXvjxqw39TinljdiZgjO2WroFHN0DasDVJbLM6zHRbuvMiD+R3aOMgcEasPmS6BQQriEreCIqx44rWN5SDiZCUrJwJdy2h+RIaogliIgEYJL8asA4W6wzhAXNld7ZX3UGN/EhCacdCaouTO18HemeHX8aPDTqo5IBFKRyd1CWcSYrQ+iGGtTCTv5FCfn+LxtpP6qrR9tAUwyrciduSO+Ir5LKH2u7VFgu6S6nSHa/2LfzcZoaf1aWY3u51+XWVwLcJ4unMENGLnMGx4GsS8HbFPIf1VZHb1/FBsfnZ9hpIEWBQVuTkR+6QKzq9zUdYB+tVPg08Ss9nGiK9KkRkGGzrSjbhUhb+OIunNyjIb9aqzbvL0pKYw0sTRmPofwXQy3Nt4qqZm6JztMr/V5nzCARgbZ3MSU23+DEpOQjHYIplGBoxELEhexayjWwsgoa0O4UB6U0wYSR7xHSVaBTAVINTvf4Gt8tjpHVr3dVdfBeTR+50mlrrbESa+AvZIoq6yJTItnpAHRm/bKyIkXbuW2ObkeZ6y5o2JqB/0+1WVT4Nd6P35MqfUDd9RBDPidid+WZUsg+Iz/tkG7bcVLvFuMDDqMPcLkbRn9DrczAcc9dUao3L5s0LFTI9fZyQrpAku3nBKIGXfofbC8dnsrOHhQB6sH13/WXpHOOAf71T8y0M5RwLl+4/Vm/xCAJ68ksXKmV5ZjiL6NMPC/I28pjch03TxuXoYCiCo3DeDj0UacH+/XIGHzvkYyEX0+QU6SsllcW6XNKMk9XL5wUv/vVg5SwVK4fkX3r/qFz8IS7ZuErmk72QMmrA5N8a0tELOzOPoJpJR8HusWObdMGSGrKs+aaFYnQcEPqu1g NJoBtAH8 htyXnaxL7XGLi+j9ucjMD4vwfltvmuOewwe7Qimo2MCREObEV1pz7mrxBD3ApqDrEAoFlpy88oQurC3lgHjWM9L3RzgbiyxAYY0QVJZ9sK3dgTwj+0oH5gN/dlkvplBpsRBd+Iz6GSCGiwmwVZqPPEavdOk4YVS0MW9wG2DzqkdDZDX4bGVa+2GYKDij0AOUKac4l1NqbBFf0LdaRyL5xuzfQFM+n4X1F25Vw 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 When both KASAN and slub_debug are enabled, when a free object is being prepared in setup_object, slub_debug poisons the object data before KASAN initializes its per-object metadata. Right now, in setup_object, KASAN only initializes the alloc metadata, which is always stored outside of the object. slub_debug is aware of this and it skips poisoning and checking that memory area. However, with the following patch in this series, KASAN also starts initializing its free medata in setup_object. As this metadata might be stored within the object, this initialization might overwrite the slub_debug poisoning. This leads to slub_debug reports. Thus, skip checking slub_debug poisoning of the object data area that overlaps with the in-object KASAN free metadata. Also make slub_debug poisoning of tail kmalloc redzones more precise when KASAN is enabled: slub_debug can still poison and check the tail kmalloc allocation area that comes after the KASAN free metadata. Signed-off-by: Andrey Konovalov Acked-by: Vlastimil Babka --- Andrew, please put this patch right before "kasan: use stack_depot_put for Generic mode". --- mm/slub.c | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index 63d281dfacdb..782bd8a6bd34 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -870,20 +870,20 @@ static inline void set_orig_size(struct kmem_cache *s, void *object, unsigned int orig_size) { void *p = kasan_reset_tag(object); + unsigned int kasan_meta_size; if (!slub_debug_orig_size(s)) return; -#ifdef CONFIG_KASAN_GENERIC /* - * KASAN could save its free meta data in object's data area at - * offset 0, if the size is larger than 'orig_size', it will - * overlap the data redzone in [orig_size+1, object_size], and - * the check should be skipped. + * KASAN can save its free meta data inside of the object at offset 0. + * If this meta data size is larger than 'orig_size', it will overlap + * the data redzone in [orig_size+1, object_size]. Thus, we adjust + * 'orig_size' to be as at least as big as KASAN's meta data. */ - if (kasan_metadata_size(s, true) > orig_size) - orig_size = s->object_size; -#endif + kasan_meta_size = kasan_metadata_size(s, true); + if (kasan_meta_size > orig_size) + orig_size = kasan_meta_size; p += get_info_end(s); p += sizeof(struct track) * 2; @@ -1192,7 +1192,7 @@ static int check_object(struct kmem_cache *s, struct slab *slab, { u8 *p = object; u8 *endobject = object + s->object_size; - unsigned int orig_size; + unsigned int orig_size, kasan_meta_size; if (s->flags & SLAB_RED_ZONE) { if (!check_bytes_and_report(s, slab, object, "Left Redzone", @@ -1222,12 +1222,23 @@ static int check_object(struct kmem_cache *s, struct slab *slab, } if (s->flags & SLAB_POISON) { - if (val != SLUB_RED_ACTIVE && (s->flags & __OBJECT_POISON) && - (!check_bytes_and_report(s, slab, p, "Poison", p, - POISON_FREE, s->object_size - 1) || - !check_bytes_and_report(s, slab, p, "End Poison", - p + s->object_size - 1, POISON_END, 1))) - return 0; + if (val != SLUB_RED_ACTIVE && (s->flags & __OBJECT_POISON)) { + /* + * KASAN can save its free meta data inside of the + * object at offset 0. Thus, skip checking the part of + * the redzone that overlaps with the meta data. + */ + kasan_meta_size = kasan_metadata_size(s, true); + if (kasan_meta_size < s->object_size - 1 && + !check_bytes_and_report(s, slab, p, "Poison", + p + kasan_meta_size, POISON_FREE, + s->object_size - kasan_meta_size - 1)) + return 0; + if (kasan_meta_size < s->object_size && + !check_bytes_and_report(s, slab, p, "End Poison", + p + s->object_size - 1, POISON_END, 1)) + return 0; + } /* * check_pad_bytes cleans up on its own. */