Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp1121192ybz; Fri, 1 May 2020 15:06:57 -0700 (PDT) X-Google-Smtp-Source: APiQypKYUBGiJv5FX3Ss1y/5WCZY3SpxBW1NaK+RuzOKNDlrT1+Z40zVBOxGdDn8SVhVgd6P9ADl X-Received: by 2002:a05:6402:684:: with SMTP id f4mr5569627edy.240.1588370817118; Fri, 01 May 2020 15:06:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1588370817; cv=none; d=google.com; s=arc-20160816; b=0zrQf8eaACvv7H0bUE8ul23U2dQyikM5iq+hAipaWiIMh3W8rUgxzmQh/GayETPAhS UWx4qyf+YbLVim0FTM+Dk3AzkrYooTPigmgUgiZqL0ivLBFpuVq6tVSEx0QPkeHsKvNJ 3uyo1zZKqpmEMWR3rumhl9RlACEf0723cQdCTEgSzqpgBYabOwSmOtGyDH0fl5GWB5ag moxdbFlPvxFGY1YrihwOUIe64k2WTG2wb47ssNBmYy2nZhA9HTs0basOHrzn7dyLMdt3 YZmlN7jojyuaJ/TMAE+9vGTTVfZTf3opqzzaZSVsd8iyPoFoV/LIK1kPl8sNnP9XUZoy VJFw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:in-reply-to:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :dkim-signature; bh=T6li7RP0nNmM/rAdvvN6+PcfoPgJq4NN8pqOv5gUJa8=; b=cB7joCcjjAyc2938og/AozLwpdlXKrZCsqAg23fI5UpcD1HRa1ppHQ96+WIBqcZQU9 mhk1C1sIGRC0heD0iMeATp8epooLFZql0p+cLz+WPI34GVuZgEJKcLfhCUNS3N0WtY1r Y0TqQwP0OLegBBcJT2l4AqNePNu9Ac+5WOty3Qtl4IKJHrYG/aE9gKW5+CCi3l+fDrZ7 pI09JAvdpkn9xFVPCNB+sn9Wuip4JmQyuCxykrAsleFK1yfBd/7u9nD+/9KcFldaNknr ZKd0CtVCNF2H9i+mo7hApPLSmyHcBmU9qXCwlktxwpQrWGUjaSOTXbWb0ZKACsL24xbP o/IQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b=Sxto6yV4; 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 g2si2335956edp.611.2020.05.01.15.06.34; Fri, 01 May 2020 15:06:57 -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=Sxto6yV4; 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 S1726574AbgEAWBI (ORCPT + 99 others); Fri, 1 May 2020 18:01:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46440 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726307AbgEAWBH (ORCPT ); Fri, 1 May 2020 18:01:07 -0400 Received: from mail-pj1-x1042.google.com (mail-pj1-x1042.google.com [IPv6:2607:f8b0:4864:20::1042]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4211DC061A0C for ; Fri, 1 May 2020 15:01:07 -0700 (PDT) Received: by mail-pj1-x1042.google.com with SMTP id ms17so449721pjb.0 for ; Fri, 01 May 2020 15:01:07 -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=T6li7RP0nNmM/rAdvvN6+PcfoPgJq4NN8pqOv5gUJa8=; b=Sxto6yV4sfv9B9gDti6Qo1WPgv6DCiJYs9s5aUw6Sj/QCrEyNhCMPaNKevWD1wvn/m fW3djuswak172TroKWRGCyWaWLA2awkaCJ84DV6tT6Nawpzi9bdygmUaUJOdDbxL/h32 fZks3VUbNfKmUpM8Vs62G0GqVaqca8ZjOcAWM= 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=T6li7RP0nNmM/rAdvvN6+PcfoPgJq4NN8pqOv5gUJa8=; b=rjNm+R7i5+iS8Q+hvjJejCH18ql04d7RoGUFFFdktVi6pjiJ9x2c5gurRmwTsS8Fi9 Iv/AIHNLTSJFr9rUzP07otzkoguuy47tDCskwf5fR7cAYBPfQwR+Xr8u649JI2xr2miT Je2k9Mrrhtr8XLqTGUjxXnxIM7DowBDiRKQdawBZB9S47j7RqRRxgdOrhgb+NStssVG0 OrW5aaCFgGX5QY0LAe8KNjJA5LUcbmZLbTLzKF9T89rMEBrX5sAGHHPG6H/zk4pZcxfw gn1iLPubVcO90Mja0fXNZcjbp/Q8nJtzo1Pel1EfoTj3wbNsKwNv3fsKvpPSd43pv0ne mGPw== X-Gm-Message-State: AGi0PuZw2hrZP0TvUsqO7MJIDQBhLLT8JsFUM8pEZBO+G5PwCne3zDgf e1v0llnYrj4xAuzJe+B/mtupGg== X-Received: by 2002:a17:90a:24a7:: with SMTP id i36mr2109358pje.32.1588370466589; Fri, 01 May 2020 15:01:06 -0700 (PDT) Received: from www.outflux.net (smtp.outflux.net. [198.145.64.163]) by smtp.gmail.com with ESMTPSA id s27sm137576pgo.42.2020.05.01.15.01.05 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 May 2020 15:01:05 -0700 (PDT) Date: Fri, 1 May 2020 15:01:04 -0700 From: Kees Cook To: Waiman Long Cc: Christoph Lameter , Pekka Enberg , David Rientjes , Joonsoo Kim , Andrew Morton , linux-mm@kvack.org, linux-kernel@vger.kernel.org, Changbin Du , Matthew Wilcox , Markus Elfring Subject: Re: [PATCH v3] mm/slub: Fix incorrect interpretation of s->offset Message-ID: <202005011459.5C3E13F4@keescook> References: <20200429135328.26976-1-longman@redhat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20200429135328.26976-1-longman@redhat.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Apr 29, 2020 at 09:53:28AM -0400, Waiman Long wrote: > In a couple of places in the slub memory allocator, the code uses > "s->offset" as a check to see if the free pointer is put right after the > object. That check is no longer true with commit 3202fa62fb43 ("slub: > relocate freelist pointer to middle of object"). > > As a result, echoing "1" into the validate sysfs file, e.g. of dentry, > may cause a bunch of "Freepointer corrupt" error reports like the > following to appear with the system in panic afterwards. > > [ 38.579769] ============================================================================= > [ 38.580845] BUG dentry(666:pmcd.service) (Tainted: G B): Freepointer corrupt > [ 38.581948] ----------------------------------------------------------------------------- > > To fix it, use the check "s->offset == s->inuse" in the new helper > function freeptr_outside_object() instead. Also add another helper function > get_info_end() to return the end of info block (inuse + free pointer > if not overlapping with object). > > Fixes: 3202fa62fb43 ("slub: relocate freelist pointer to middle of object") > Signed-off-by: Waiman Long Thanks! Nice catch. :) Reviewed-by: Kees Cook -Kees > --- > mm/slub.c | 45 ++++++++++++++++++++++++++++++--------------- > 1 file changed, 30 insertions(+), 15 deletions(-) > > [v3: Change function name to freeptr_outside_object(), update check & add comment] > > diff --git a/mm/slub.c b/mm/slub.c > index 9bf44955c4f1..b762450fc9f0 100644 > --- a/mm/slub.c > +++ b/mm/slub.c > @@ -551,15 +551,32 @@ static void print_section(char *level, char *text, u8 *addr, > metadata_access_disable(); > } > > +/* > + * See comment in calculate_sizes(). > + */ > +static inline bool freeptr_outside_object(struct kmem_cache *s) > +{ > + return s->offset >= s->inuse; > +} > + > +/* > + * Return offset of the end of info block which is inuse + free pointer if > + * not overlapping with object. > + */ > +static inline unsigned int get_info_end(struct kmem_cache *s) > +{ > + if (freeptr_outside_object(s)) > + return s->inuse + sizeof(void *); > + else > + return s->inuse; > +} > + > static struct track *get_track(struct kmem_cache *s, void *object, > enum track_item alloc) > { > struct track *p; > > - if (s->offset) > - p = object + s->offset + sizeof(void *); > - else > - p = object + s->inuse; > + p = object + get_info_end(s); > > return p + alloc; > } > @@ -686,10 +703,7 @@ static void print_trailer(struct kmem_cache *s, struct page *page, u8 *p) > print_section(KERN_ERR, "Redzone ", p + s->object_size, > s->inuse - s->object_size); > > - if (s->offset) > - off = s->offset + sizeof(void *); > - else > - off = s->inuse; > + off = get_info_end(s); > > if (s->flags & SLAB_STORE_USER) > off += 2 * sizeof(struct track); > @@ -782,7 +796,7 @@ static int check_bytes_and_report(struct kmem_cache *s, struct page *page, > * object address > * Bytes of the object to be managed. > * If the freepointer may overlay the object then the free > - * pointer is the first word of the object. > + * pointer is at the middle of the object. > * > * Poisoning uses 0x6b (POISON_FREE) and the last byte is > * 0xa5 (POISON_END) > @@ -816,11 +830,7 @@ static int check_bytes_and_report(struct kmem_cache *s, struct page *page, > > static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *p) > { > - unsigned long off = s->inuse; /* The end of info */ > - > - if (s->offset) > - /* Freepointer is placed after the object. */ > - off += sizeof(void *); > + unsigned long off = get_info_end(s); /* The end of info */ > > if (s->flags & SLAB_STORE_USER) > /* We also have user information there */ > @@ -907,7 +917,7 @@ static int check_object(struct kmem_cache *s, struct page *page, > check_pad_bytes(s, page, p); > } > > - if (!s->offset && val == SLUB_RED_ACTIVE) > + if (!freeptr_outside_object(s) && val == SLUB_RED_ACTIVE) > /* > * Object and freepointer overlap. Cannot check > * freepointer while object is allocated. > @@ -3587,6 +3597,11 @@ static int calculate_sizes(struct kmem_cache *s, int forced_order) > * > * This is the case if we do RCU, have a constructor or > * destructor or are poisoning the objects. > + * > + * The assumption that s->offset >= s->inuse means free > + * pointer is outside of the object is used in the > + * freeptr_outside_object() function. If that is no > + * longer true, the function needs to be modified. > */ > s->offset = size; > size += sizeof(void *); > -- > 2.18.1 > -- Kees Cook