Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp3179580imw; Mon, 11 Jul 2022 03:40:57 -0700 (PDT) X-Google-Smtp-Source: AGRyM1voeAN+2ShAFgrJpNozTCjlPJLEYRmHJ3ONRrdYB6PmpU9TDwKPZmo53LgViIQ57huaq0hT X-Received: by 2002:a17:906:9b8b:b0:72b:13f6:8957 with SMTP id dd11-20020a1709069b8b00b0072b13f68957mr17930059ejc.239.1657536056854; Mon, 11 Jul 2022 03:40:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657536056; cv=none; d=google.com; s=arc-20160816; b=FokjVH0KKzaa6XDel1Y3gKuNpaO4VF5mlBrVeTniGFQIb6YeVIyksTuvzsxqhZpkVS JutXLu65NQJksLIGTBt786lp8qnL6VWIXyEtP5QOrijEnMyjhTCY5q/UqlgbEf69mAhq WyUNd1oxTBvsajiJ2ul/agM96f4OQBNulBz5SxfcQqHcVK1iYdOFaJJHTQTyrRdFUcJ3 SYbyDDr7MWTGW9w9Si0O8oH7jjnk8rFstJgRkJorasdFJRgUeCar9kbg4pRtI7ZNNFDz P6SR2bgx7pJumDCIPG5eDRYUHWKTeVlzDjTDlZXzp81v7ls7RYCfiaiqdTTfCQwngOSE d7Bw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=obBnl8xI69Yep2lBZnxiros4xYduJ8t8aGgQSwDLZ10=; b=DXV3zc9W8j23sgUh1QUgxqUJoUf/GAz9v97tByEiH9C0/IiZ16PCTsku/zTOBbfZLv OWDiKXiB4/UbvrJXA8ShZq2uZ1FXA5SiWsHyuvuq1xP8jDQ0/s7PtpQW29q7iv5V0neM aPn9L1wRjrmiQE4UZtLvijNEZ666ERRdedI50VKqx5zXLYsDNu0lq6a4j5Of0+ZtwvFF uK2/FEjXgQAy4C6DNSNNh0xn/1rzmio5fMKgrimQ3q5/8OYM9D9Euc4xx46nzvJbA+i3 0JeGCiNK8LjbHfk7mqneYffl0uGfu5mZFJUAxL4r2q5xAF6U1OTju4zgWLsP3u5X6WmD dnZQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=U1yCfZg5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l22-20020a1709060e1600b007269f720fb5si8459792eji.530.2022.07.11.03.40.32; Mon, 11 Jul 2022 03:40:56 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=U1yCfZg5; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231419AbiGKJS7 (ORCPT + 99 others); Mon, 11 Jul 2022 05:18:59 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43384 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231576AbiGKJSO (ORCPT ); Mon, 11 Jul 2022 05:18:14 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 406544C627; Mon, 11 Jul 2022 02:11:44 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id BDC2AB80D2C; Mon, 11 Jul 2022 09:11:42 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 33788C34115; Mon, 11 Jul 2022 09:11:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1657530701; bh=kH/LMdSo7S0gTaIOclHQb1ujmGBuyLz3OSUUA7GJ9CU=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=U1yCfZg5uLfpAou7QVS7/PWjc+L0cPQ4/WPHGHHO22xlP9KgV31leeHtVkn3iN6y3 IE79A0VMfHnx7XQUePWk0ULiRVjFwuLWK4Kw8zVDpR3R61nSUpIGeNt7IQ6aYS0uqs PUB6EaCo1CV2O0AsI/lvdKTUXPPSqnw2VGbVT3ME= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jann Horn , Christoph Lameter , David Rientjes , Muchun Song , Hyeonggon Yoo <42.hyeyoo@gmail.com>, Vlastimil Babka Subject: [PATCH 5.10 01/55] mm/slub: add missing TID updates on slab deactivation Date: Mon, 11 Jul 2022 11:06:49 +0200 Message-Id: <20220711090541.810064977@linuxfoundation.org> X-Mailer: git-send-email 2.37.0 In-Reply-To: <20220711090541.764895984@linuxfoundation.org> References: <20220711090541.764895984@linuxfoundation.org> User-Agent: quilt/0.66 X-stable: review X-Patchwork-Hint: ignore MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jann Horn commit eeaa345e128515135ccb864c04482180c08e3259 upstream. The fastpath in slab_alloc_node() assumes that c->slab is stable as long as the TID stays the same. However, two places in __slab_alloc() currently don't update the TID when deactivating the CPU slab. If multiple operations race the right way, this could lead to an object getting lost; or, in an even more unlikely situation, it could even lead to an object being freed onto the wrong slab's freelist, messing up the `inuse` counter and eventually causing a page to be freed to the page allocator while it still contains slab objects. (I haven't actually tested these cases though, this is just based on looking at the code. Writing testcases for this stuff seems like it'd be a pain...) The race leading to state inconsistency is (all operations on the same CPU and kmem_cache): - task A: begin do_slab_free(): - read TID - read pcpu freelist (==NULL) - check `slab == c->slab` (true) - [PREEMPT A->B] - task B: begin slab_alloc_node(): - fastpath fails (`c->freelist` is NULL) - enter __slab_alloc() - slub_get_cpu_ptr() (disables preemption) - enter ___slab_alloc() - take local_lock_irqsave() - read c->freelist as NULL - get_freelist() returns NULL - write `c->slab = NULL` - drop local_unlock_irqrestore() - goto new_slab - slub_percpu_partial() is NULL - get_partial() returns NULL - slub_put_cpu_ptr() (enables preemption) - [PREEMPT B->A] - task A: finish do_slab_free(): - this_cpu_cmpxchg_double() succeeds() - [CORRUPT STATE: c->slab==NULL, c->freelist!=NULL] >From there, the object on c->freelist will get lost if task B is allowed to continue from here: It will proceed to the retry_load_slab label, set c->slab, then jump to load_freelist, which clobbers c->freelist. But if we instead continue as follows, we get worse corruption: - task A: run __slab_free() on object from other struct slab: - CPU_PARTIAL_FREE case (slab was on no list, is now on pcpu partial) - task A: run slab_alloc_node() with NUMA node constraint: - fastpath fails (c->slab is NULL) - call __slab_alloc() - slub_get_cpu_ptr() (disables preemption) - enter ___slab_alloc() - c->slab is NULL: goto new_slab - slub_percpu_partial() is non-NULL - set c->slab to slub_percpu_partial(c) - [CORRUPT STATE: c->slab points to slab-1, c->freelist has objects from slab-2] - goto redo - node_match() fails - goto deactivate_slab - existing c->freelist is passed into deactivate_slab() - inuse count of slab-1 is decremented to account for object from slab-2 At this point, the inuse count of slab-1 is 1 lower than it should be. This means that if we free all allocated objects in slab-1 except for one, SLUB will think that slab-1 is completely unused, and may free its page, leading to use-after-free. Fixes: c17dda40a6a4e ("slub: Separate out kmem_cache_cpu processing from deactivate_slab") Fixes: 03e404af26dc2 ("slub: fast release on full slab") Cc: stable@vger.kernel.org Signed-off-by: Jann Horn Acked-by: Christoph Lameter Acked-by: David Rientjes Reviewed-by: Muchun Song Tested-by: Hyeonggon Yoo <42.hyeyoo@gmail.com> Signed-off-by: Vlastimil Babka Link: https://lore.kernel.org/r/20220608182205.2945720-1-jannh@google.com Signed-off-by: Greg Kroah-Hartman --- mm/slub.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) --- a/mm/slub.c +++ b/mm/slub.c @@ -2297,6 +2297,7 @@ redo: c->page = NULL; c->freelist = NULL; + c->tid = next_tid(c->tid); } /* @@ -2430,8 +2431,6 @@ static inline void flush_slab(struct kme { stat(s, CPUSLAB_FLUSH); deactivate_slab(s, c->page, c->freelist, c); - - c->tid = next_tid(c->tid); } /* @@ -2717,6 +2716,7 @@ redo: if (!freelist) { c->page = NULL; + c->tid = next_tid(c->tid); stat(s, DEACTIVATE_BYPASS); goto new_slab; }