From patchwork Wed Oct 16 15:41:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Feng Tang X-Patchwork-Id: 13838601 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 E1CCFD2A529 for ; Wed, 16 Oct 2024 15:42:08 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 72DA76B0093; Wed, 16 Oct 2024 11:42:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 6DCD66B0095; Wed, 16 Oct 2024 11:42:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 5575A6B0096; Wed, 16 Oct 2024 11:42:08 -0400 (EDT) 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 344086B0093 for ; Wed, 16 Oct 2024 11:42:08 -0400 (EDT) Received: from smtpin07.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id BFE8A406CA for ; Wed, 16 Oct 2024 15:42:01 +0000 (UTC) X-FDA: 82679881116.07.37A7F8E Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.10]) by imf27.hostedemail.com (Postfix) with ESMTP id D86F040002 for ; Wed, 16 Oct 2024 15:41:56 +0000 (UTC) Authentication-Results: imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=PcxNTrAX; spf=pass (imf27.hostedemail.com: domain of feng.tang@intel.com designates 198.175.65.10 as permitted sender) smtp.mailfrom=feng.tang@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1729093252; 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=SAsqbT7F8ZH/bzSMugdy64lPBN/0mAPzdY7m+7h1gzw=; b=kROBWEDiqUiSn1bEm9EksVcwuX1TXrnI5ZDbyl7qeqBP2kyRsoIlaskDTuuuZpGeS/lx/e FgteXZdEywZsxMvz0BXmRamJIKS62DHCRa7Vhok+ICkVFL3pzOh58PvPsO9bkKBrTOvHBA JxxPJB5QdCoQjnNDxexQzkP4ZOguPfI= ARC-Authentication-Results: i=1; imf27.hostedemail.com; dkim=pass header.d=intel.com header.s=Intel header.b=PcxNTrAX; spf=pass (imf27.hostedemail.com: domain of feng.tang@intel.com designates 198.175.65.10 as permitted sender) smtp.mailfrom=feng.tang@intel.com; dmarc=pass (policy=none) header.from=intel.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1729093252; a=rsa-sha256; cv=none; b=z/DdtE4aV+0kR+JsW4PCxTc7195zb2smMvmpbPbnW86l7XU4gAknHZU+5h3SgaZfEdoJ/B UGv+HIzjqtdPHrXZe2E2KssRpBJz8XBX7Z9beIzRqOLaIW2FxzJmDcM1TgXLGWKOMxGSPJ 8U+25y4AQevcUEm8wX7t43Bw2pJDTHI= DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1729093325; x=1760629325; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=SfN+aazMvPNd5suMLtjlSQgXeOWhjVA+dg6w5Bfb7iQ=; b=PcxNTrAXTTSab0pUUj3y5GT7I7A8AFE6pLpp6f0dtJoWHUolLmDhgUW6 KHNdwzZVInOo9Vg2bS9sei8FEq+wTXvb83OUoUQvphESeGpB1Kru8Npl/ UPM0NHgjgH3G863f0f1qBuF7UeLlR+gCAUCnjOP/TWjIQ4hy46XH4kKqV 9ZkYJCmxP6CwTVcUcBM7xsmEp30mEFTX2XOSoHpAD+SpHM+hw2wOSjQqx k6M5xMOteHAxBT1drvNm4/eJMcq1dH3Yuh2u7RroO5sVZAeUOq+QL6OCH vCtU1ZUx/VDWuha/Y1N5yA6A77R1tv1PoUxZBERCkttRx8KbHuiR9ynyN A==; X-CSE-ConnectionGUID: CFpYAv0IR9O330Lgl690Uw== X-CSE-MsgGUID: qvtO0ceKRmOMqbCrSF7O+g== X-IronPort-AV: E=McAfee;i="6700,10204,11222"; a="46021370" X-IronPort-AV: E=Sophos;i="6.11,199,1725346800"; d="scan'208";a="46021370" Received: from fmviesa001.fm.intel.com ([10.60.135.141]) by orvoesa102.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 Oct 2024 08:42:05 -0700 X-CSE-ConnectionGUID: UUGX2kglTD2IGECAAw+E6g== X-CSE-MsgGUID: 51KYtXJaTQ6dVWcB49DHcg== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.11,208,1725346800"; d="scan'208";a="109018920" Received: from feng-clx.sh.intel.com ([10.239.159.50]) by fmviesa001.fm.intel.com with ESMTP; 16 Oct 2024 08:42:01 -0700 From: Feng Tang To: Vlastimil Babka , Andrew Morton , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Andrey Konovalov , Marco Elver , Alexander Potapenko , Dmitry Vyukov , Danilo Krummrich , Narasimhan.V@amd.com Cc: linux-mm@kvack.org, kasan-dev@googlegroups.com, linux-kernel@vger.kernel.org, Feng Tang Subject: [PATCH v3 2/3] mm/slub: Improve redzone check and zeroing for krealloc() Date: Wed, 16 Oct 2024 23:41:51 +0800 Message-Id: <20241016154152.1376492-3-feng.tang@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241016154152.1376492-1-feng.tang@intel.com> References: <20241016154152.1376492-1-feng.tang@intel.com> MIME-Version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sKJNGA1vrBqnjqmSrkDVtaM80i8 R6kDrJQo yRXRWvG5hFCIxGbSKoaWnc0E6MrD4Hi0HsLaaBJKMByknKVGjoYt3YMFmxpMvQLFLZB4raalL/8PQwqxF0/JW9xRTLJTRNiWVA97WCfXOTPFpSBqDkyDsIOFADyz0BfgyfIMqJ6VqLMdAH+GnrOOFZDDnUl1O8kvMshgp1bNNxFkT9EizX2PQPNDWwYt/JYCbJRjhZj7E3rVHKO9p9hZmfq4H6xB7FRlgFq6HhlcVNWzapDqN++4KyuOs6Zx0XnrghTv5 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: For current krealloc(), one problem is its caller doesn't pass the old request size, say the object is 64 bytes kmalloc one, but caller may only requested 48 bytes. Then when krealloc() shrinks or grows in the same object, or allocate a new bigger object, it lacks this 'original size' information to do accurate data preserving or zeroing (when __GFP_ZERO is set). Thus with slub debug redzone and object tracking enabled, parts of the object after krealloc() might contain redzone data instead of zeroes, which is violating the __GFP_ZERO guarantees. Good thing is in this case, kmalloc caches do have this 'orig_size' feature. So solve the problem by utilize 'org_size' to do accurate data zeroing and preserving. [Thanks to syzbot and V, Narasimhan for discovering kfence and big kmalloc related issues in early patch version] Suggested-by: Vlastimil Babka Signed-off-by: Feng Tang Reviewed-by: Hyeonggon Yoo <42.hyeyoo@gmail.com> --- mm/slub.c | 84 +++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 60 insertions(+), 24 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index 1d348899f7a3..958f7af79fad 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -4718,34 +4718,66 @@ static __always_inline __realloc_size(2) void * __do_krealloc(const void *p, size_t new_size, gfp_t flags) { void *ret; - size_t ks; - - /* Check for double-free before calling ksize. */ - if (likely(!ZERO_OR_NULL_PTR(p))) { - if (!kasan_check_byte(p)) - return NULL; - ks = ksize(p); - } else - ks = 0; - - /* If the object still fits, repoison it precisely. */ - if (ks >= new_size) { - /* Zero out spare memory. */ - if (want_init_on_alloc(flags)) { - kasan_disable_current(); + size_t ks = 0; + int orig_size = 0; + struct kmem_cache *s = NULL; + + /* Check for double-free. */ + if (unlikely(ZERO_OR_NULL_PTR(p))) + goto alloc_new; + + if (!kasan_check_byte(p)) + return NULL; + + if (is_kfence_address(p)) { + ks = orig_size = kfence_ksize(p); + } else { + struct folio *folio; + + folio = virt_to_folio(p); + if (unlikely(!folio_test_slab(folio))) { + /* Big kmalloc object */ + WARN_ON(folio_size(folio) <= KMALLOC_MAX_CACHE_SIZE); + WARN_ON(p != folio_address(folio)); + ks = folio_size(folio); + } else { + s = folio_slab(folio)->slab_cache; + orig_size = get_orig_size(s, (void *)p); + ks = s->object_size; + } + } + + /* If the old object doesn't fit, allocate a bigger one */ + if (new_size > ks) + goto alloc_new; + + /* Zero out spare memory. */ + if (want_init_on_alloc(flags)) { + kasan_disable_current(); + if (orig_size && orig_size < new_size) + memset((void *)p + orig_size, 0, new_size - orig_size); + else memset((void *)p + new_size, 0, ks - new_size); - kasan_enable_current(); - } + kasan_enable_current(); + } - p = kasan_krealloc((void *)p, new_size, flags); - return (void *)p; + /* Setup kmalloc redzone when needed */ + if (s && slub_debug_orig_size(s)) { + set_orig_size(s, (void *)p, new_size); + if (s->flags & SLAB_RED_ZONE && new_size < ks) + memset_no_sanitize_memory((void *)p + new_size, + SLUB_RED_ACTIVE, ks - new_size); } + p = kasan_krealloc((void *)p, new_size, flags); + return (void *)p; + +alloc_new: ret = kmalloc_node_track_caller_noprof(new_size, flags, NUMA_NO_NODE, _RET_IP_); if (ret && p) { /* Disable KASAN checks as the object's redzone is accessed. */ kasan_disable_current(); - memcpy(ret, kasan_reset_tag(p), ks); + memcpy(ret, kasan_reset_tag(p), orig_size ?: ks); kasan_enable_current(); } @@ -4766,16 +4798,20 @@ __do_krealloc(const void *p, size_t new_size, gfp_t flags) * memory allocation is flagged with __GFP_ZERO. Otherwise, it is possible that * __GFP_ZERO is not fully honored by this API. * - * This is the case, since krealloc() only knows about the bucket size of an - * allocation (but not the exact size it was allocated with) and hence - * implements the following semantics for shrinking and growing buffers with - * __GFP_ZERO. + * When slub_debug_orig_size() is off, krealloc() only knows about the bucket + * size of an allocation (but not the exact size it was allocated with) and + * hence implements the following semantics for shrinking and growing buffers + * with __GFP_ZERO. * * new bucket * 0 size size * |--------|----------------| * | keep | zero | * + * Otherwise, the original allocation size 'orig_size' could be used to + * precisely clear the requested size, and the new size will also be stored + * as the new 'orig_size'. + * * In any case, the contents of the object pointed to are preserved up to the * lesser of the new and old sizes. *