Received: by 2002:a05:6a10:6744:0:0:0:0 with SMTP id w4csp94766pxu; Wed, 14 Oct 2020 21:31:16 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx1+zJIMfTh/XRO/37OjInxOJsH+tmBLisv1Hm23VhQESwXtDs7Rfnfn+lTSZ2aUa02Gqmh X-Received: by 2002:a50:d796:: with SMTP id w22mr2456158edi.123.1602736276517; Wed, 14 Oct 2020 21:31:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1602736276; cv=none; d=google.com; s=arc-20160816; b=JTdU4nre7y1ocqdSVf2vWtxAAO1ZhPUeF/pozfHFJuF6uT1FgSzhoGD37hzi8uqExj kjzZW3dDgPlzpcjZa0fAL0LJNkiE+DQvH48ebsKk7oEmbaCjm7FKE5t/xg+gg4uQuKsO buxy3bE3r0dYcIJqFAnZX/FVKZMg+2HVm6SntE2HOLXUD2Rms7BlawvVPy8RGOLA1aUD gT3pXBTlY5UxasVDKvnseLFXxF/8vlIlvcwEqi6D2kIF9YAbi0k5L0bKXheBVoQMF95M l0chzm7VEHvIUUTDGEhkyTTgtfjGkC/2x2XpGe6tSBccCsOJ96BzA9IklT4E4mIIqPvN q8Vg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=XKOhTjKsO0EE4YV4BAqrsCKoLcqTC+6y91gTBtyDfzc=; b=wlDTOqn6IQicENvM6221ewy35oqc/xO7UZsp7ddKVUVKN/0/cv32u0085h1tiDUElK xcnSSHZUTiMYT1nzTDxhsvQWEue/UNqAcVEde2jMK/7ZLlASb4Cco36k6Mkm7wSrdVuC Bz16xln1gD65xX1cj0Zlhqshjaz1W/Gr544Hkckd4wAz+MuJOl1X0jMIOSLL0a/G5Imh EhYWBCBxUtKAuguA2V3cAmNNAFvZv90YEridIj9cGrHghgHllFA71674XGQw/qghaZmJ jxSSHEFSlGGUV/3P+B6Gol1T4hLAg0YiI6n8M2i1a/eqX6t1EOiGi3+E0LR48iA4THiB 6zOQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=OhI20NXp; 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=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y12si1056259edo.479.2020.10.14.21.30.54; Wed, 14 Oct 2020 21:31:16 -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=@chromium.org header.s=google header.b=OhI20NXp; 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=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727209AbgJODIw (ORCPT + 99 others); Wed, 14 Oct 2020 23:08:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36552 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1725919AbgJODIw (ORCPT ); Wed, 14 Oct 2020 23:08:52 -0400 Received: from mail-pf1-x441.google.com (mail-pf1-x441.google.com [IPv6:2607:f8b0:4864:20::441]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7BB48C0613D2 for ; Wed, 14 Oct 2020 20:08:50 -0700 (PDT) Received: by mail-pf1-x441.google.com with SMTP id w21so1084747pfc.7 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=cpqJ+ntlxrmJ1v89fmgyFbTs2turdui4eMCPmR9ghEcOzudoSbhiBTMIubLUU7piy0 vGM+FXdEAP6VMtAkqyv5e7xbt/W20HkzHv9UOZkk1SrWX6iWUP3sMwacWCQjb3inDPTt zVpr4Yd8GdbGWxhtO7/6WKHN1pM3HgKWOWR4y5swh3rJRA9EAdUrMBkSYN4VAuxfx27t +Qh2YS0A7b3l/9ZBSgGkI63Qb+vp7e947xTPg4QrtLxcP7kDfLlNOTAIeV4RMNxN6KS5 bLgoRcgOdAKB+KF2ZsPki14gOr8hKmjzsQAc07yWU7rryiFiujzMVqbQHs8+Q7L2z1j+ 7qIg== X-Gm-Message-State: AOAM533u+S0vWedhQHCB+wHDfvwDZ03D9MLB1kb+I8u+3VXI799fvZTH T7zORSjh5oM0IDKHyWB3cFG4Lw== 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: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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