Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3931395pxj; Mon, 21 Jun 2021 09:37:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw1QaSGJAOByu/c5765v89KsRrurWHH4eH6XxE0b3DAsttyUNFOOK9tvddEUweTDSVvjB2l X-Received: by 2002:a17:907:387:: with SMTP id ss7mr2535486ejb.60.1624293459315; Mon, 21 Jun 2021 09:37:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624293459; cv=none; d=google.com; s=arc-20160816; b=aIQjJS/lXnKEVUnw0REtGqBBdQRQ5p3qaEfjA0u4r94muqp1P3PtoPh5C1o69is3/4 i5v1GZka7VMIyncMjyS3CmwtgEe5XCI8mKDlyqKhODYVIG0GAZmhdHPiHB+++d6RzmZZ 037BpUTS/SHD0O6NSH/MVRZUauykEzBbB2sa2zVSRotXqI/t9zcupH2sExnH5oI4oHfr hrI/KuQBSDn+gTF0iwcpOsIjVMdSsB8SrK6ptxvl4yrYBHEK4eJ6SWoXdkKG29NxGp6y EzCZGA/r1oEGylO+G701Ymv5/JRPvw1aPB1l96pG6fvvaC42dGOZL0vYigVJn43nouuQ hJfw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=P7ErVcoSqHakan0BPQOQmoj7bnzLF5Yacdv3UbNGOQk=; b=yp/SBCEoriT2TknG56LFyGPsXI8dlA55helOwwsTu1FK5f04eIn2w6ZDBfz9WvbkN+ NCedTdQYvDRRfQP2E9Gd9mY0AUcPLLwusSOZULjJZ5QBwBbqF231H7KjwlfPzwJD7559 rprMg5DXg0a1Tc/jphS9N96o1YzBc4NoJbJW4EwLuHutrPX8/3gA06GJC1yq62TE296B T3wuhS4FXkx5lf72aeIFhfQEsdIGjmjX7RGBkiNJjpw5y8ICIor51Z50ZkWGUlTiHf2d mmsIk6UynqKkrxrWTwYZmNuxplv6ffsoM6PeO2WD/f9OhPeSRImv+QSsWB0DQ5uRohou lvpQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=gWK2o97E; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id 21si11138695ejc.413.2021.06.21.09.37.16; Mon, 21 Jun 2021 09:37:39 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=gWK2o97E; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232331AbhFUQif (ORCPT + 99 others); Mon, 21 Jun 2021 12:38:35 -0400 Received: from mail.kernel.org ([198.145.29.99]:52804 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232625AbhFUQeP (ORCPT ); Mon, 21 Jun 2021 12:34:15 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 10886613E8; Mon, 21 Jun 2021 16:26:51 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1624292811; bh=BJvZ7lmORa1Y+vQrFYqAEdzyWH10bmrilziAzKaU0Uc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=gWK2o97ENWmYQrUa9ul0OtqDBTwGUo++GJXRJ0hmR5/S9hMV+rpbP87OB/vnx7wPM /3QLj5LiahNMuzrFMYr2SWRwZzubxQOtSwexcnfhYII8K6v8tSlcEBTFlDIjCPTj7V t2Q6h6xFv4Q0X65k44rkUJhOvmUQbx6Q0KVSvl/s= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Kees Cook , Marco Elver , "Lin, Zhenpeng" , Vlastimil Babka , Christoph Lameter , David Rientjes , Joonsoo Kim , Pekka Enberg , Roman Gushchin , Andrew Morton , Linus Torvalds Subject: [PATCH 5.10 139/146] mm/slub: actually fix freelist pointer vs redzoning Date: Mon, 21 Jun 2021 18:16:09 +0200 Message-Id: <20210621154920.443635487@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210621154911.244649123@linuxfoundation.org> References: <20210621154911.244649123@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Kees Cook commit e41a49fadbc80b60b48d3c095d9e2ee7ef7c9a8e upstream. It turns out that SLUB redzoning ("slub_debug=Z") checks from s->object_size rather than from s->inuse (which is normally bumped to make room for the freelist pointer), so a cache created with an object size less than 24 would have the freelist pointer written beyond s->object_size, causing the redzone to be corrupted by the freelist pointer. This was very visible with "slub_debug=ZF": BUG test (Tainted: G B ): Right Redzone overwritten ----------------------------------------------------------------------------- INFO: 0xffff957ead1c05de-0xffff957ead1c05df @offset=1502. First byte 0x1a instead of 0xbb INFO: Slab 0xffffef3950b47000 objects=170 used=170 fp=0x0000000000000000 flags=0x8000000000000200 INFO: Object 0xffff957ead1c05d8 @offset=1496 fp=0xffff957ead1c0620 Redzone (____ptrval____): bb bb bb bb bb bb bb bb ........ Object (____ptrval____): 00 00 00 00 00 f6 f4 a5 ........ Redzone (____ptrval____): 40 1d e8 1a aa @.... Padding (____ptrval____): 00 00 00 00 00 00 00 00 ........ Adjust the offset to stay within s->object_size. (Note that no caches of in this size range are known to exist in the kernel currently.) Link: https://lkml.kernel.org/r/20210608183955.280836-4-keescook@chromium.org Link: https://lore.kernel.org/linux-mm/20200807160627.GA1420741@elver.google.com/ Link: https://lore.kernel.org/lkml/0f7dd7b2-7496-5e2d-9488-2ec9f8e90441@suse.cz/Fixes: 89b83f282d8b (slub: avoid redzone when choosing freepointer location) Link: https://lore.kernel.org/lkml/CANpmjNOwZ5VpKQn+SYWovTkFB4VsT-RPwyENBmaK0dLcpqStkA@mail.gmail.com Signed-off-by: Kees Cook Reported-by: Marco Elver Reported-by: "Lin, Zhenpeng" Tested-by: Marco Elver Acked-by: Vlastimil Babka Cc: Christoph Lameter Cc: David Rientjes Cc: Joonsoo Kim Cc: Pekka Enberg Cc: Roman Gushchin Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- mm/slub.c | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) --- a/mm/slub.c +++ b/mm/slub.c @@ -3639,7 +3639,6 @@ static int calculate_sizes(struct kmem_c { slab_flags_t flags = s->flags; unsigned int size = s->object_size; - unsigned int freepointer_area; unsigned int order; /* @@ -3648,13 +3647,6 @@ static int calculate_sizes(struct kmem_c * the possible location of the free pointer. */ size = ALIGN(size, sizeof(void *)); - /* - * This is the area of the object where a freepointer can be - * safely written. If redzoning adds more to the inuse size, we - * can't use that portion for writing the freepointer, so - * s->offset must be limited within this for the general case. - */ - freepointer_area = size; #ifdef CONFIG_SLUB_DEBUG /* @@ -3680,7 +3672,7 @@ static int calculate_sizes(struct kmem_c /* * With that we have determined the number of bytes in actual use - * by the object. This is the potential offset to the free pointer. + * by the object and redzoning. */ s->inuse = size; @@ -3703,13 +3695,13 @@ static int calculate_sizes(struct kmem_c */ s->offset = size; size += sizeof(void *); - } else if (freepointer_area > sizeof(void *)) { + } else { /* * Store freelist pointer near middle of object to keep * it away from the edges of the object to avoid small * sized over/underflows from neighboring allocations. */ - s->offset = ALIGN(freepointer_area / 2, sizeof(void *)); + s->offset = ALIGN_DOWN(s->object_size / 2, sizeof(void *)); } #ifdef CONFIG_SLUB_DEBUG