Received: by 2002:ab2:1689:0:b0:1f7:5705:b850 with SMTP id d9csp1883858lqa; Tue, 30 Apr 2024 01:59:39 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUsYVr183cfhSJWI7yaWaAemNtIsauMo04/TYxWV1pyTPkRW8TjhxPL+KKfm9d4L1mLfCzLfddaZEO8svt5yJyM24cqDIntOHpdR/SDXg== X-Google-Smtp-Source: AGHT+IEfhanRIHUq2I9en7a/JvOsoFSjfakgoPJEsa/d3sAui3mIeRBlJyX3WD0X94Nozh6MZ7MD X-Received: by 2002:a05:6a00:7106:b0:6f3:e6d6:7ff5 with SMTP id lh6-20020a056a00710600b006f3e6d67ff5mr8090075pfb.22.1714467578868; Tue, 30 Apr 2024 01:59:38 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1714467578; cv=pass; d=google.com; s=arc-20160816; b=bkU198bLJ2QMIxCzoP4/nyx5apcFWqrqugBwH+LJZGyJoVm/jMARJHtYazAiHgV3Ri hKbyn3WDYUHaA3PiY6GF6M7jbddcL8lSui3S8rFTLf82QCyYDHi8shp/7rJm9sbi7s6/ 8tgM80uPCptU6tjUA0Px/yWJwvsHUI8VO89ShwZlrskpuSeRY/OV80LSSfre1auaebAd rM1lok44BYw3Cdmw/rkX2nzgxv+vS7pmkGWG8MKEkigXmSiOWsPgyIokTbi9oJavhT9z hVSYyg3L/wWKPQCrzW8KIGegFukc62ARgcJcVNkMyAUFTru4ti6H6AYrseu8AWzgb9EI fVFg== 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=qkps59hvkVp6rMx8vJsbQ8PnYFAMlqf/nMFnhk1Nt+k=; fh=QBtVPbUYxXDnjv/++1YgcxRGQ2o0P9/2aqvsX2FjdMs=; b=TVxw7HECbuilx3MTY1Be9wqxyf50XGujBWjpzGyA7QIn+nO3GIfpGQsuiOOI83I6NI R1vbj82cHZc73fnD3Ler7fLjP18DuQ4ilr4oPK7cySPPK5mps5pTqmh3yufbG/TwwBod Q/IYNkwZaHwjfafsFRdfUHE3eNAwC58p1cA+oNPgxd3yCJYLhcOGDfByhXeO/7gLZjcb 7dXlQcXQqPSIeorjw3xUBmEoMEUA3Mwhl4SdYJgQuQH+AEuY1Xl47gRasTc8nL5JPc54 FIZYOZY1vX/kOGGsWPWveJapvq9G1eDDXiijViVGaWpbKiF9rN356ZsmLb2gNPQzWLvV oR3A==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@clip-os.org header.s=gm1 header.b=goOYACRG; 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-163600-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-163600-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 sy.mirrors.kernel.org (sy.mirrors.kernel.org. [2604:1380:40f1:3f00::1]) by mx.google.com with ESMTPS id h5-20020a655185000000b005fc8d3e6851si17091182pgq.846.2024.04.30.01.59.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Apr 2024 01:59:38 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-163600-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) client-ip=2604:1380:40f1:3f00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@clip-os.org header.s=gm1 header.b=goOYACRG; 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-163600-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:40f1:3f00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-163600-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 sy.mirrors.kernel.org (Postfix) with ESMTPS id 8792AB23BA5 for ; Tue, 30 Apr 2024 08:55:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 09E0719DF53; Tue, 30 Apr 2024 08:53:02 +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="goOYACRG" Received: from relay5-d.mail.gandi.net (relay5-d.mail.gandi.net [217.70.183.197]) (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 91EC6199EA0 for ; Tue, 30 Apr 2024 08:52:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=217.70.183.197 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714467181; cv=none; b=Skf629MKGSLBnva1nm7JjbQHB41DfRC6OLT6oE7WyI71rnMN/vxwp7CvM6C6H5OpwahnnwtVSDM3PTVaRV7JKedmGfJj8IJROTwtl6S1I2tSa6/4JZrpREB1vhLU/0YzV/pvbliUsiifzSR4AUepFsP/S/rRAtkHPu2DM2ID3SI= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1714467181; c=relaxed/simple; bh=g/NYaYHIx0Khsr7fDxNzZiYNm+NgJsDfbc+RU4Udv90=; h=Date:From:To:Cc:Subject:Message-ID:MIME-Version:Content-Type: Content-Disposition; b=hGhe/A0POREa6rsWB8ufDr3Q+47sGS9MbHe6Xhc65WZisqW1gn58jwtFx0vWz6BxWKEY1pQH81ZB/1L34bdsHR+D1fwXvr1oohX+HhnuzFxPLoF46bUfaAkHZGCOW9KfkjsfUeDmOV8aNZMmrnTLZWCK7dE8C1/S7j4mHr5HqMc= 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=goOYACRG; arc=none smtp.client-ip=217.70.183.197 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 A162C1C000B; Tue, 30 Apr 2024 08:52:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=clip-os.org; s=gm1; t=1714467176; 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=qkps59hvkVp6rMx8vJsbQ8PnYFAMlqf/nMFnhk1Nt+k=; b=goOYACRG2rE3wyyILMJ2ekMgFNwGMULcPGn05Fy3NCLO+MS3MP5IpxZwN+QT3JFNx+gLv5 tW1QoamnbDOIb3xTCjmayZmQwlzhbmkolKJAlWdjSAkeVVXDZunyyMsgSW/M5Sy2XTTCXt selVHev8dABkm+fLUCJiSulglv0t/qyv9AFeSJdfseo07cBnPEhNzYZYE9AVCRl02CxBm6 S1znt+ZFMImhHLLp/OEh4CXu82UHZrWci6Zh9fGUApYPaEFpb8cSq6qVsy7AMNYHuKqz28 DsibqbMtMiay1pJmneum+VKqWiA3GsDzmr2SBx/Ky3ZrET/lLISKtbaJ0u6Ouw== Date: Tue, 30 Apr 2024 10:52:53 +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 v2] 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(). Object's freepointer thus needs to be avoided when stored outside the object if init_on_free is set. 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-authored-by: Chengming Zhou Signed-off-by: Nicolas Bouchinet --- 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