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.8 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,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 8A7C7C43463 for ; Fri, 18 Sep 2020 02:06:29 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 335A6239D2 for ; Fri, 18 Sep 2020 02:06:29 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (1024-bit key) header.d=kernel.org header.i=@kernel.org header.b="0sAJYZhZ" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 335A6239D2 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=kernel.org Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id 9FEA56B0078; Thu, 17 Sep 2020 22:06:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 9862A6B007B; Thu, 17 Sep 2020 22:06:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 828288E0001; Thu, 17 Sep 2020 22:06:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0130.hostedemail.com [216.40.44.130]) by kanga.kvack.org (Postfix) with ESMTP id 66E606B0078 for ; Thu, 17 Sep 2020 22:06:28 -0400 (EDT) Received: from smtpin23.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay03.hostedemail.com (Postfix) with ESMTP id 327E98249980 for ; Fri, 18 Sep 2020 02:06:28 +0000 (UTC) X-FDA: 77274542856.23.cows14_3a0373627127 Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin23.hostedemail.com (Postfix) with ESMTP id 1012C37604 for ; Fri, 18 Sep 2020 02:06:28 +0000 (UTC) X-HE-Tag: cows14_3a0373627127 X-Filterd-Recvd-Size: 7132 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by imf14.hostedemail.com (Postfix) with ESMTP for ; Fri, 18 Sep 2020 02:06:27 +0000 (UTC) Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id DE19B2399A; Fri, 18 Sep 2020 02:06:24 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1600394786; bh=nRDPaV9puZECvmpAJ1pxKIa9voaWUssG0EnYs8bxZUg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=0sAJYZhZp7+mMFghRr+i8QMj1eJyRSGGYrSN0CRpKS/c9r2ETY/1kuY+0UiuJ0X3a 0UGxJoYe6eHBV9tOrcAtKhuHxGh4gy8USvWnVra4EkU46207cMA0JRprJZfBmFKvkd t7ax9GvuJCp/odfYhKYDpuQnwHZGaN314lvbg7bc= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Waiman Long , Andrew Morton , Matthew Wilcox , Kees Cook , Rafael Aquini , Christoph Lameter , Vitaly Nikolenko , Silvio Cesare , Pekka Enberg , David Rientjes , Joonsoo Kim , Markus Elfring , Changbin Du , Linus Torvalds , Sasha Levin , linux-mm@kvack.org Subject: [PATCH AUTOSEL 5.4 257/330] mm/slub: fix incorrect interpretation of s->offset Date: Thu, 17 Sep 2020 21:59:57 -0400 Message-Id: <20200918020110.2063155-257-sashal@kernel.org> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20200918020110.2063155-1-sashal@kernel.org> References: <20200918020110.2063155-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore 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: From: Waiman Long [ Upstream commit cbfc35a48609ceac978791e3ab9dde0c01f8cb20 ] 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. =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D BUG dentry(666:pmcd.service) (Tainted: G B): Freepointer corrupt -----------------------------------------------------------------------= ------ To fix it, use the check "s->offset =3D=3D 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 Signed-off-by: Andrew Morton Reviewed-by: Matthew Wilcox (Oracle) Reviewed-by: Kees Cook Acked-by: Rafael Aquini Cc: Christoph Lameter Cc: Vitaly Nikolenko Cc: Silvio Cesare Cc: Pekka Enberg Cc: David Rientjes Cc: Joonsoo Kim Cc: Markus Elfring Cc: Changbin Du Link: http://lkml.kernel.org/r/20200429135328.26976-1-longman@redhat.com Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- mm/slub.c | 45 ++++++++++++++++++++++++++++++--------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/mm/slub.c b/mm/slub.c index 822ba07245291..d69934eac9e94 100644 --- a/mm/slub.c +++ b/mm/slub.c @@ -533,15 +533,32 @@ static void print_section(char *level, char *text, = u8 *addr, metadata_access_disable(); } =20 +/* + * See comment in calculate_sizes(). + */ +static inline bool freeptr_outside_object(struct kmem_cache *s) +{ + return s->offset >=3D 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; =20 - if (s->offset) - p =3D object + s->offset + sizeof(void *); - else - p =3D object + s->inuse; + p =3D object + get_info_end(s); =20 return p + alloc; } @@ -682,10 +699,7 @@ static void print_trailer(struct kmem_cache *s, stru= ct page *page, u8 *p) print_section(KERN_ERR, "Redzone ", p + s->object_size, s->inuse - s->object_size); =20 - if (s->offset) - off =3D s->offset + sizeof(void *); - else - off =3D s->inuse; + off =3D get_info_end(s); =20 if (s->flags & SLAB_STORE_USER) off +=3D 2 * sizeof(struct track); @@ -776,7 +790,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) @@ -810,11 +824,7 @@ static int check_bytes_and_report(struct kmem_cache = *s, struct page *page, =20 static int check_pad_bytes(struct kmem_cache *s, struct page *page, u8 *= p) { - unsigned long off =3D s->inuse; /* The end of info */ - - if (s->offset) - /* Freepointer is placed after the object. */ - off +=3D sizeof(void *); + unsigned long off =3D get_info_end(s); /* The end of info */ =20 if (s->flags & SLAB_STORE_USER) /* We also have user information there */ @@ -900,7 +910,7 @@ static int check_object(struct kmem_cache *s, struct = page *page, check_pad_bytes(s, page, p); } =20 - if (!s->offset && val =3D=3D SLUB_RED_ACTIVE) + if (!freeptr_outside_object(s) && val =3D=3D SLUB_RED_ACTIVE) /* * Object and freepointer overlap. Cannot check * freepointer while object is allocated. @@ -3585,6 +3595,11 @@ static int calculate_sizes(struct kmem_cache *s, i= nt 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 >=3D 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 =3D size; size +=3D sizeof(void *); --=20 2.25.1