Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp1954349lqa; Tue, 30 Apr 2024 04:35:16 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUZaaZgQU+xy2iCkHyV7DPT28qztLorbRHovJB8Ey0B38n8nmx6y1Bx3mPCgA9bQ9C3ustyQU7Fro9Z0R5WBbh1NlmCEBaSmuNWpcUQOA== X-Google-Smtp-Source: AGHT+IHvgJNkrpp7rKlabMnEOa6cuezwlXpJ/ON2JiLIa4u9P3pCYnAGFQ5YaGrEfHKYnXqeCwak X-Received: by 2002:ad4:5d64:0:b0:6a0:b9aa:3160 with SMTP id fn4-20020ad45d64000000b006a0b9aa3160mr2822855qvb.18.1714476916000; Tue, 30 Apr 2024 04:35:16 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714476915; cv=pass; d=google.com; s=arc-20160816; b=iYY9QTQ00Sx8CMd5Y0N0DU44OBw8DiggtZwBdq/qF8X+yE9EL+4n3EPeZkf0OWkxny UfJRmYqOBCd6Va4y0rB6NpAiKj+5GjpCq9zIPyU/CJ+ymPl8p4Dep/uzP+EA/fqDdCDO ukHLkqn56jG4Yyjm/3Rr+uzG47ZH0SsgXiVowpF/bUmHjuAC84d+sOnVi5crfdL1mfYm vPD+jsdjktDhKwmI2P3+Wn5gyL/VMbLr7mx1KUBtLKeDu2o5gD3/m72lid557XEXcSNA b1VFjHPRvpjgK46JTtge9AJxgGHdgO3ECCcrYZLDhyFKl9k53A8YOWCv9yzdqM5fEDka fYmw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-disposition:mime-version:list-unsubscribe:list-subscribe :list-id:precedence:message-id:subject:cc:to:from:date :dkim-signature; bh=M0gCyZUgKj7iV3h/gwYac96jecnR+4EjQmwFBZXj584=; fh=QBtVPbUYxXDnjv/++1YgcxRGQ2o0P9/2aqvsX2FjdMs=; b=U1tS2FNL4+iyp1HK9UKnE4cFdqNHwRUxVeiMJ1W38U1yL/1lVvghOIs3l/Ln1xq5yS s82c0JIn0FAUy1v8Ajy/2rN4ro48U4rxLjJHMBHW9aujo4GLuTLvBc8995K9MNyt3y48 ExtJr2Ch9Wap44q/b9ED+EPV+oMftsUGc3UrJcwhCmNcSO0gxDTuzo+z909QwdC642YE O3qUjpSSPcsjxZqVwVbk/fit2D5GmSlqdL+aluOZqv0yYuAYV8r3lC6Ezi1Jy69XJJCN rjYxpJqMTBQDCu0bjycQMQg2k6DuDziwqwY8A9OXZBa8wPjLey7nkbaVGHCaMKMqLw2y HrMA==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@clip-os.org header.s=gm1 header.b=nM0URJ6x; arc=pass (i=1 spf=pass spfdomain=clip-os.org dkim=pass dkdomain=clip-os.org dmarc=pass fromdomain=clip-os.org); spf=pass (google.com: domain of linux-kernel+bounces-163849-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-163849-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=clip-os.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id jv14-20020a05621429ee00b006a0cb4012absi5112617qvb.71.2024.04.30.04.35.15 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Apr 2024 04:35:15 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-163849-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@clip-os.org header.s=gm1 header.b=nM0URJ6x; arc=pass (i=1 spf=pass spfdomain=clip-os.org dkim=pass dkdomain=clip-os.org dmarc=pass fromdomain=clip-os.org); spf=pass (google.com: domain of linux-kernel+bounces-163849-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-163849-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=clip-os.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 624E51C22242 for ; Tue, 30 Apr 2024 11:35:15 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 6E63312D76A; Tue, 30 Apr 2024 11:35:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=clip-os.org header.i=@clip-os.org header.b="nM0URJ6x" Received: from relay3-d.mail.gandi.net (relay3-d.mail.gandi.net [217.70.183.195]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id BB06612C47A for ; Tue, 30 Apr 2024 11:35:09 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.195 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714476912; cv=none; b=tmRCH57LEnZqU6gSme6bb8Eetzr/5oRWL9KTv59Fyot3uwRZwhHtWSqwE7MtSMXcWoHMF4vFUyJ81DoI3jEWqymW/xK6YsrPrnimVfQ4t7Kzo2NjmD21LkecTrWRs6W1NCUIiyIIS8xBRL/xFxUPzkNdBh1W7THlyik5Q+Akhs4= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714476912; c=relaxed/simple; bh=Ro5C4SltTDmGHIqKcwc8eY9OizlrYbgNyQfowKhQ2Gg=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type: Content-Disposition; b=GRFgcn2fpMXZvWq+pz+o9/hxxpbiOXBWCWgnHAwiAVQNXfRHHgimU4s5mJdIibuA96u4cVNh2JDErvVYW2bhVTThN1Z/twjrTmQNBKV4BwKC6IH+y4bT2uCsBWQ6HfmlnJeQ/Y5xk3Y/mL0+TJPI3ZRNSgFUFK3t0FmpJ4nL1ko= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=clip-os.org; spf=pass smtp.mailfrom=clip-os.org; dkim=pass (2048-bit key) header.d=clip-os.org header.i=@clip-os.org header.b=nM0URJ6x; arc=none smtp.client-ip=217.70.183.195 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=clip-os.org Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=clip-os.org Received: by mail.gandi.net (Postfix) with ESMTPSA id 560CF60007; Tue, 30 Apr 2024 11:35:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=clip-os.org; s=gm1; t=1714476902; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type; bh=M0gCyZUgKj7iV3h/gwYac96jecnR+4EjQmwFBZXj584=; b=nM0URJ6xiUOjlSg9J/l+2jP270ghHlvF+mpUuOm4n3R6s7J4d88Qx6Bg1Tx6+nyYwYexaP 5g7NiwAQ1S/X4G5VFVYSZHIr9Ky1615t2aUuYAGOulK9Qq3Uc8gp+fSzsYHyqOVZmHeMuZ 69eiP8stHv8cRpBpzTwjyU7Q5k6zQ1KwtnpnHuTjzzGe7TvF49shxJVREcZRoSjTbT6/N5 KBR459cNEASDd2k+pLdFaEod9GWNS+Y0HjS9qPpA/labyVLo2e8TAn9SQEFtJQcDkorLX5 m87rGXS34s+kA/NLRGbMlBwJcOAJzvTX8FsU4K4yfUGZ8mgTLEzJBMRYOjv+tA== Date: Tue, 30 Apr 2024 13:34:59 +0200 From: Nicolas Bouchinet To: linux-mm@kvack.org, linux-kernel@vger.kernel.org Cc: Chengming Zhou , Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , Vlastimil Babka , Roman Gushchin , Hyeonggon Yoo <42.hyeyoo@gmail.com> Subject: [PATCH v3] slub: Fixes freepointer encoding for single free Message-ID: Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-GND-Sasl: nicolas.bouchinet@clip-os.org From: Nicolas Bouchinet Commit 284f17ac13fe ("mm/slub: handle bulk and single object freeing separately") splits single and bulk object freeing in two functions slab_free() and slab_free_bulk() which leads slab_free() to call slab_free_hook() directly instead of slab_free_freelist_hook(). If `init_on_free` is set, slab_free_hook() zeroes the object. Afterward, if `slub_debug=F` and `CONFIG_SLAB_FREELIST_HARDENED` are set, the do_slab_free() slowpath executes freelist consistency checks and try to decode a zeroed freepointer which leads to a "Freepointer corrupt" detection in check_object(). During bulk free, slab_free_freelist_hook() isn't affected as it always sets it objects freepointer using set_freepointer() to maintain its reconstructed freelist after `init_on_free`. For single free, object's freepointer thus needs to be avoided when stored outside the object if `init_on_free` is set. The freepointer left as is, check_object() may later detect an invalid pointer value due to objects overflow. To reproduce, set `slub_debug=FU init_on_free=1 log_level=7` on the command line of a kernel build with `CONFIG_SLAB_FREELIST_HARDENED=y`. dmesg sample log: [ 10.708715] ============================================================================= [ 10.710323] BUG kmalloc-rnd-05-32 (Tainted: G B T ): Freepointer corrupt [ 10.712695] ----------------------------------------------------------------------------- [ 10.712695] [ 10.712695] Slab 0xffffd8bdc400d580 objects=32 used=4 fp=0xffff9d9a80356f80 flags=0x200000000000a00(workingset|slab|node=0|zone=2) [ 10.716698] Object 0xffff9d9a80356600 @offset=1536 fp=0x7ee4f480ce0ecd7c [ 10.716698] [ 10.716698] Bytes b4 ffff9d9a803565f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ [ 10.720703] Object ffff9d9a80356600: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ [ 10.720703] Object ffff9d9a80356610: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ [ 10.724696] Padding ffff9d9a8035666c: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ [ 10.724696] Padding ffff9d9a8035667c: 00 00 00 00 .... [ 10.724696] FIX kmalloc-rnd-05-32: Object at 0xffff9d9a80356600 not freed Co-developed-by: Chengming Zhou Signed-off-by: Nicolas Bouchinet --- Changes since v2: https://lore.kernel.org/all/ZjCxZfD1d36zfq-R@archlinux/ * Reword commit message in order to clarify the patch approach as suggested by Vlastimil Babka Changes since v1: https://lore.kernel.org/all/Zij_fGjRS_rK-65r@archlinux/ * Jump above out of object freepointer if init_on_free is set instead of initializing it with set_freepointer() as suggested by Vlastimil Babka. * Adapt maybe_wipe_obj_freeptr() to avoid wiping out of object on alloc freepointer as suggested by Chengming Zhou. * Reword commit message. --- mm/slub.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index 3aa12b9b323d..173c340ec1d3 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -2102,15 +2102,20 @@ bool slab_free_hook(struct kmem_cache *s, void *x, bool init) * * The initialization memset's clear the object and the metadata, * but don't touch the SLAB redzone. + * + * The object's freepointer is also avoided if stored outside the + * object. */ if (unlikely(init)) { int rsize; + unsigned int inuse; + inuse = get_info_end(s); if (!kasan_has_integrated_init()) memset(kasan_reset_tag(x), 0, s->object_size); rsize = (s->flags & SLAB_RED_ZONE) ? s->red_left_pad : 0; - memset((char *)kasan_reset_tag(x) + s->inuse, 0, - s->size - s->inuse - rsize); + memset((char *)kasan_reset_tag(x) + inuse, 0, + s->size - inuse - rsize); } /* KASAN might put x into memory quarantine, delaying its reuse. */ return !kasan_slab_free(s, x, init); @@ -3789,7 +3794,7 @@ static void *__slab_alloc_node(struct kmem_cache *s, static __always_inline void maybe_wipe_obj_freeptr(struct kmem_cache *s, void *obj) { - if (unlikely(slab_want_init_on_free(s)) && obj) + if (unlikely(slab_want_init_on_free(s)) && obj && !freeptr_outside_object(s)) memset((void *)((char *)kasan_reset_tag(obj) + s->offset), 0, sizeof(void *)); } -- 2.44.0