Received: by 2002:a05:6a10:22f:0:0:0:0 with SMTP id 15csp874936pxk; Thu, 17 Sep 2020 20:00:17 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwfxfnGuvWwnAXeAEg1ZbNU/QBXsoUlr2cuk4vZHWAw8qE5iOQYC5FWmYSEbUIZFbzZcGEJ X-Received: by 2002:a17:907:1116:: with SMTP id qu22mr10520800ejb.231.1600398017206; Thu, 17 Sep 2020 20:00:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1600398017; cv=none; d=google.com; s=arc-20160816; b=ELVKGbcedTwDOPY9r/bD+gD7peLVzmNgG0BV1tB2EoQ92n8olKbDv04DOEa6oijJZp +MnNwuXUEzITcRZ+mVOhvOyVaT61pYE08VEjdb7ZaJ1zfJ38LCfNQZxKYAz05Teb2WLQ LNwlPSBel4xWvyv3+Dfz1U03dmt/nUVtRp7Yd/1LtyqL6VVlzUxiKihLE988jo/4Kpdb YS3/BxUdstky8zkLMhW4x5Kamgwg2Ol+alcY8I6QzJ1tiQ4uH3YX/W3TwYSk1fQF35Uj FzlX4H3bFiuwA5nfTbLiTo1vUwI1lU1BZuAr6LCGXhYvv6H4sfLssLxu8mm9z3F1nzaj vCsg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=IkpYd93P1NuNiNQXFDgnlpvNA8GnvGbEAaL/k52awN4=; b=WdI8cjGlQQECHJgvyyac63oClq0noWGYxc1cPeQWexth6JKikkdqxJOtdJptTzV8hP dPKInM8DtNVKPbRYU53nOgxcGMD2V9aYyTiR8Qpbbk8Q4Gl6UgPzmui9o+F3iOosNBOz fF/gFV8eFu1CBsD8Q4UHdxoyA0FIGtpjrZAYZ/floRVSB+NnIgGh4EdK+uuNK9Vpm4Pq Gkn4yBSPjzLeNwqppbb7XQ1TOVlWJ1lVXy8D5FqwTN59t36Hns4aazR2ras+UmP2pI6k c0zidNykSDGY5u/DkUVt/dXrLOBniOCb7UoyAjac+PEmKK/WWVMyFxFYoVAqy9m75owT qJWQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=0sAJYZhZ; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id oo24si1222157ejb.136.2020.09.17.19.59.53; Thu, 17 Sep 2020 20:00:17 -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=@kernel.org header.s=default header.b=0sAJYZhZ; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727044AbgIRC5w (ORCPT + 99 others); Thu, 17 Sep 2020 22:57:52 -0400 Received: from mail.kernel.org ([198.145.29.99]:55812 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727756AbgIRCG1 (ORCPT ); Thu, 17 Sep 2020 22:06:27 -0400 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: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. ============================================================================= BUG dentry(666:pmcd.service) (Tainted: G B): Freepointer corrupt ----------------------------------------------------------------------------- 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 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(); } +/* + * 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; } @@ -682,10 +699,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); @@ -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, 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 */ @@ -900,7 +910,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. @@ -3585,6 +3595,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.25.1