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=-9.9 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, INCLUDES_PATCH,MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_HELO_NONE,SPF_PASS, URIBL_BLOCKED autolearn=unavailable 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 C9EEDC43457 for ; Thu, 15 Oct 2020 03:08:53 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id F3DC722245 for ; Thu, 15 Oct 2020 03:08:52 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=pass (1024-bit key) header.d=chromium.org header.i=@chromium.org header.b="OhI20NXp" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org F3DC722245 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 51DB46B0068; Wed, 14 Oct 2020 23:08:52 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 4A6AC6B0070; Wed, 14 Oct 2020 23:08:52 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 36E146B0071; Wed, 14 Oct 2020 23:08:52 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0217.hostedemail.com [216.40.44.217]) by kanga.kvack.org (Postfix) with ESMTP id 083986B0068 for ; Wed, 14 Oct 2020 23:08:51 -0400 (EDT) Received: from smtpin09.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 8820C8249980 for ; Thu, 15 Oct 2020 03:08:51 +0000 (UTC) X-FDA: 77372677662.09.plant11_0604c6127211 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin09.hostedemail.com (Postfix) with ESMTP id 66918180AD807 for ; Thu, 15 Oct 2020 03:08:51 +0000 (UTC) X-HE-Tag: plant11_0604c6127211 X-Filterd-Recvd-Size: 7492 Received: from mail-pg1-f194.google.com (mail-pg1-f194.google.com [209.85.215.194]) by imf08.hostedemail.com (Postfix) with ESMTP for ; Thu, 15 Oct 2020 03:08:50 +0000 (UTC) Received: by mail-pg1-f194.google.com with SMTP id r10so941520pgb.10 for ; Wed, 14 Oct 2020 20:08:50 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=XKOhTjKsO0EE4YV4BAqrsCKoLcqTC+6y91gTBtyDfzc=; b=OhI20NXp3bRpI+D+HvzZmUsVj2SG0BLogQBtTOgVmB1IGwBBmPo3+zIkQTcfXvrE3c hz1n94A+Dx1w4X8g5XZQW9pRLZWXr3jnJD7godSQvrnAHtHW5GXWABb2zL0O45l4rCcg gEk2IzRuI5wzPIRE/dmbuaGMl9dDRq0KVdEmQ= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=XKOhTjKsO0EE4YV4BAqrsCKoLcqTC+6y91gTBtyDfzc=; b=VbaX7lYQsVZaH2bpj2GNU/B86/v7KoljWT/ej8kvyxi9BgeRhQcihXLc3NvFmkr+cw 2VjqOmr8uWkxaV1EDF2ZyxtB6ukN7KG2GyaS4IUFAME1JaIqxrR9TOyIEW+XmzSzACpJ P+AV1jGVD7M7Ac1ywu8Qo7Tf22axQpZwKVEF57awIyN2VZee/wmG3UruFPmQMLO3qc/8 w+45kCkizRzPP9VG6OSYB0esC+d66iT2O1zybBwMgHrOYtxYngcai/2o36ita3Gfzxlq QciUPy8ShQYMeMiclcHYPAUlmGFB9JWeBLt1+H11cpxpFSOSCleOIaQGWT+z+9c+nI8c yECQ== X-Gm-Message-State: AOAM530M4gQe5+qexbe54p/nZx69PJ1Ei5rU/z3RACOgYmfyDn7sknoW ngw5vn/nZcFFY1L7BNKH9fX5YQ== X-Google-Smtp-Source: ABdhPJym5UDdpMgKQQWmMZLVVyeSA1V2DiZOaQb1m5XeGgB5IjoBb5DxMYDHlklwpsc9fEsDssLb4g== X-Received: by 2002:a62:8c55:0:b029:152:b326:9558 with SMTP id m82-20020a628c550000b0290152b3269558mr2082303pfd.72.1602731329686; Wed, 14 Oct 2020 20:08:49 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id k8sm1036546pgi.39.2020.10.14.20.08.48 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 14 Oct 2020 20:08:48 -0700 (PDT) Date: Wed, 14 Oct 2020 20:08:47 -0700 From: Kees Cook To: Waiman Long Cc: Andrew Morton , Marco Elver , stable@vger.kernel.org, Pekka Enberg , Christoph Lameter , David Rientjes , Joonsoo Kim , linux-kernel@vger.kernel.org, linux-mm@kvack.org Subject: Re: [PATCH] slub: Actually fix freelist pointer vs redzoning Message-ID: <202010142006.BD734954@keescook> References: <20201008233443.3335464-1-keescook@chromium.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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: On Wed, Oct 14, 2020 at 10:36:01PM -0400, Waiman Long wrote: > On 10/8/20 7:34 PM, Kees Cook wrote: > > 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 their freelist pointer written beyond > > s->object_size, causing the redzone to corrupt the freelist pointer. > > This was very visible with "slub_debug=ZF": > > > > BUG test (Tainted: G B ): 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 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.google.com/ > > Fixes: 89b83f282d8b (slub: avoid redzone when choosing freepointer location) > > 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, int forced_order) > > { > > slab_flags_t flags = s->flags; > > unsigned int size = s->object_size; > > - unsigned int freepointer_area; > > unsigned int order; > > /* > > @@ -3650,13 +3649,6 @@ static int calculate_sizes(struct kmem_cache *s, int forced_order) > > * 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 > > /* > > @@ -3682,7 +3674,7 @@ static int calculate_sizes(struct kmem_cache *s, int forced_order) > > /* > > * 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; > > @@ -3694,7 +3686,8 @@ static int calculate_sizes(struct kmem_cache *s, int 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 >= s->inuse means free > > * pointer is outside of the object is used in the > > There is no check to go into this if condition to put free pointer after > object when redzoning an object smaller than sizeof(void *). In that sense, > the additional comment isn't correct. Right -- part of this is why I did my v2 series[1], where I failed to find where that had been enforced, and explicitly added it[2]. If no one objects, I could fold that check into this fix, just for robustness. cache creation is not fast-path, and the size test is a trivial check. > Should we add that check even though we don't have slab objects with such a > small size in the kernel? I'd like to... I will spin a v3. Thanks! [1] https://lore.kernel.org/lkml/20201009195411.4018141-1-keescook@chromium.org/ [2] https://lore.kernel.org/lkml/20201009195411.4018141-3-keescook@chromium.org/ -- Kees Cook