From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-12.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_PATCH,MAILING_LIST_MULTI, SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id D63A8C43467 for ; Thu, 8 Oct 2020 23:34:52 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 236C422250 for ; Thu, 8 Oct 2020 23:34:52 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="k2l02j+q" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 236C422250 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=chromium.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 7FF7A900002; Thu, 8 Oct 2020 19:34:51 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7B04E6B005D; Thu, 8 Oct 2020 19:34:51 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 67925900002; Thu, 8 Oct 2020 19:34:51 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0204.hostedemail.com [216.40.44.204]) by kanga.kvack.org (Postfix) with ESMTP id 3BD766B005C for ; Thu, 8 Oct 2020 19:34:51 -0400 (EDT) Received: from smtpin26.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id B36BD1EE6 for ; Thu, 8 Oct 2020 23:34:50 +0000 (UTC) X-FDA: 77350365540.26.seat90_3c02ae4271dc Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin26.hostedemail.com (Postfix) with ESMTP id 8BB611804B66B for ; Thu, 8 Oct 2020 23:34:50 +0000 (UTC) X-HE-Tag: seat90_3c02ae4271dc X-Filterd-Recvd-Size: 6674 Received: from mail-pl1-f196.google.com (mail-pl1-f196.google.com [209.85.214.196]) by imf25.hostedemail.com (Postfix) with ESMTP for ; Thu, 8 Oct 2020 23:34:49 +0000 (UTC) Received: by mail-pl1-f196.google.com with SMTP id h2so3531657pll.11 for ; Thu, 08 Oct 2020 16:34:49 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=MRZxCY82VT8AbmDDW9cNgiSjhXieUNCL4lvDa6bH9cQ=; b=k2l02j+qVTgPRzl0YEB+iSSs1OzEALMQl31r6XgVZU/yO4vFIlyi/eqIIu/eZ2jIXL oD6Ai7UmXysgGKc6ayu+h5ZHYMxGqlk/cmIoX3T89xR0Ad7c3hY+n5cJC1Kv4L8ZAc3l 5J341dy3JiQZ5XmfItqHSYWmpOXA3bVvHSkpk= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=MRZxCY82VT8AbmDDW9cNgiSjhXieUNCL4lvDa6bH9cQ=; b=KTqCtsmL5cgv2siRHYcdo7RGsMRmaCDDn8Wmak1/0u6kdpBhfHs14tPXYi9IAacdLP M0xCWeY3Co6v/+6WfthXhFWCqKRrWrB2Ja4sY+LJFLYNzAiHq6hCvljKXzAuG48Z7oBO hkBqBdutqUnRJh4yDG6V6EP3W/Mq05A1Oo7bHzXgUmxJ4sOJuT4SYYuk1rVXS7YMWDFh XA5BCK91lNXHKjuwoSa8QuMbdrjVxkLJI2SnTBh8uEjYmiLFR9BgVrsc272hFhkHVjfn KvCo1Lf6pHCBU41YiWirSuoJae7tcImwFZuthq/1EIV2+P+5GUZX8fkUuv9S4Tn/HkBm TM/Q== X-Gm-Message-State: AOAM533722USUKeVkKVRHwTQrAs7DlD67klNT+HoyCADtwQoV4a3+J0+ zKtqDHtq0FwkffUXialxy7ZQ9Q== X-Google-Smtp-Source: ABdhPJyexpe9/8XvCs+4d0A3Z2MTbhqPh7RXZQkKLPSCe9cckWw8dLV1aj2qH4TGZOwGLSFuLsmsAw== X-Received: by 2002:a17:902:8d8e:b029:d3:df0c:60a0 with SMTP id v14-20020a1709028d8eb02900d3df0c60a0mr9331844plo.35.1602200088961; Thu, 08 Oct 2020 16:34:48 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id c21sm8660051pfo.139.2020.10.08.16.34.47 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 08 Oct 2020 16:34:48 -0700 (PDT) From: Kees Cook To: Andrew Morton Cc: Kees Cook , Marco Elver , stable@vger.kernel.org, Pekka Enberg , Waiman Long , Christoph Lameter , David Rientjes , Joonsoo Kim , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH] slub: Actually fix freelist pointer vs redzoning Date: Thu, 8 Oct 2020 16:34:43 -0700 Message-Id: <20201008233443.3335464-1-keescook@chromium.org> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable 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: It turns out that SLUB redzoning ("slub_debug=3DZ") 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 their freelist pointer written beyond s->object_size, causing the redzone to corrupt the freelist pointer. This was very visible with "slub_debug=3DZF": BUG test (Tainted: G B ): Redzone overwritten -------------------------------------------------------------------------= ---- INFO: 0xffff957ead1c05de-0xffff957ead1c05df @offset=3D1502. First byte 0x= 1a instead of 0xbb INFO: Slab 0xffffef3950b47000 objects=3D170 used=3D170 fp=3D0x00000000000= 00000 flags=3D0x8000000000000200 INFO: Object 0xffff957ead1c05d8 @offset=3D1496 fp=3D0xffff957ead1c0620 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 there appear to be no such small-sized caches in the kernel currently.) Reported-by: Marco Elver Link: https://lore.kernel.org/linux-mm/20200807160627.GA1420741@elver.goo= gle.com/ Fixes: 89b83f282d8b (slub: avoid redzone when choosing freepointer locati= on) Cc: stable@vger.kernel.org Signed-off-by: Kees Cook --- mm/slub.c | 17 +++++------------ 1 file changed, 5 insertions(+), 12 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index 68c02b2eecd9..979f5da26992 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -3641,7 +3641,6 @@ static int calculate_sizes(struct kmem_cache *s, in= t forced_order) { slab_flags_t flags =3D s->flags; unsigned int size =3D s->object_size; - unsigned int freepointer_area; unsigned int order; =20 /* @@ -3650,13 +3649,6 @@ static int calculate_sizes(struct kmem_cache *s, i= nt forced_order) * the possible location of the free pointer. */ size =3D 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 =3D size; =20 #ifdef CONFIG_SLUB_DEBUG /* @@ -3682,7 +3674,7 @@ static int calculate_sizes(struct kmem_cache *s, in= t forced_order) =20 /* * 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 =3D size; =20 @@ -3694,7 +3686,8 @@ static int calculate_sizes(struct kmem_cache *s, in= t forced_order) * kmem_cache_free. * * This is the case if we do RCU, have a constructor or - * destructor or are poisoning the objects. + * destructor, are poisoning the objects, or are + * redzoning an object smaller than sizeof(void *). * * The assumption that s->offset >=3D s->inuse means free * pointer is outside of the object is used in the @@ -3703,13 +3696,13 @@ static int calculate_sizes(struct kmem_cache *s, = int forced_order) */ s->offset =3D size; size +=3D 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 =3D ALIGN(freepointer_area / 2, sizeof(void *)); + s->offset =3D ALIGN_DOWN(s->object_size / 2, sizeof(void *)); } =20 #ifdef CONFIG_SLUB_DEBUG --=20 2.25.1