Received: by 2002:ad5:4acb:0:0:0:0:0 with SMTP id n11csp3167093imw; Mon, 11 Jul 2022 03:21:49 -0700 (PDT) X-Google-Smtp-Source: AGRyM1tUxRGZSrmek2ouAQFTGK/Hfe3IndnL6M0feOFYO06gMFr7VTb5eePLpwdMSUE2WwkPp6c/ X-Received: by 2002:a17:907:7ea4:b0:72b:6929:4571 with SMTP id qb36-20020a1709077ea400b0072b69294571mr1053455ejc.257.1657534908730; Mon, 11 Jul 2022 03:21:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1657534908; cv=none; d=google.com; s=arc-20160816; b=KutuKu0e5xIUyNwepTbvtsfGEadJpS+LuOog3kyERpnKse1xa+1WtBvUQwclH2f3/a O9oZgNPOnLXoXbXmDUGks2pR542zgDnnktnAlvsh6IE0xqmu8hpvTqMGwxapbMwCES8D ASXRo1qqmTcoIrYo3t+acg5/bXd9wt3PlAzlNCpoKocTBAb8/XKQlofWfHNEiRlOeptI w6x3+137JwxcSxyHKx/he5cJRs2APBiKXk8X8Ri3pCylcVMWykqJEgo/nS81GjoEQPwH e9SZ4HO0zmmbI3opZNAQlshlxJGIMzKXF8UO8ZJkwp4gxCdmeFr8PqIKpBkTO8gDbAsz aomw== 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=kC40ipiBGgIMoohODK/zx5vCx+wCS+Wr7k0xWrFUa3k=; b=YYuLss2BgA6CHpRnmjHItPMJqSgabiPZ0L9DWy4YE+maJp7dRjaQbCYf+p/LQuxrs8 pLlNlfPn/zoy4ZV87L001NwzCx0sbroPMkw6WubfllFwN3aT8Ir2mKe9RmXCSH4spHax AIuTkDxdmcLnXF2krLdct5+fRVYx2rnjACaEM0qn+LM5VMWopo3U1h3aAQZo06gpa+o0 ieItYaYMdpYuTRhtdU5qG8CqDeCVugRSMPIPp3VjajafNlFnmezRhPm0i1LADdzN3VoR m7kqmNOL8f2UIzw4sS4BdDud0eYj1KIcST41Qqzr1Y6NO2qndLaYuc3Cw7ngOWnPfXJQ 10Cw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=GJT5mkXT; 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 gh4-20020a170906e08400b0071200866b78si8745298ejb.689.2022.07.11.03.21.23; Mon, 11 Jul 2022 03:21:48 -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=GJT5mkXT; 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 S231268AbiGKJLW (ORCPT + 99 others); Mon, 11 Jul 2022 05:11:22 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46774 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230127AbiGKJKa (ORCPT ); Mon, 11 Jul 2022 05:10:30 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [139.178.84.217]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D7827B4B4; Mon, 11 Jul 2022 02:08:40 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 57D0E611DF; Mon, 11 Jul 2022 09:08:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 68A48C34115; Mon, 11 Jul 2022 09:08:39 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1657530519; bh=AOZzCYGpTcjFuNIF7fPsACXJ58sxB04TYtQtbnyTpWE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=GJT5mkXTyH55+AncgeUdnRbdz3WhPmnNIbOzCpLljJcaU2WgKQ6tNHISLhIhuAhZ1 rNjsF7Tx0I3KvgxMq0O1y0HLTOHdhukBQ5LkYkTpxwfUyWZ8A8feE8hpjpS8AnYvtH JPTbBs9IkH0CvOnmFGRVGkPXAUg71aPhUTQbZ14Y= 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 4.19 02/31] mm/slub: add missing TID updates on slab deactivation Date: Mon, 11 Jul 2022 11:06:41 +0200 Message-Id: <20220711090537.917739384@linuxfoundation.org> X-Mailer: git-send-email 2.37.0 In-Reply-To: <20220711090537.841305347@linuxfoundation.org> References: <20220711090537.841305347@linuxfoundation.org> User-Agent: quilt/0.66 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 @@ -2162,6 +2162,7 @@ redo: c->page = NULL; c->freelist = NULL; + c->tid = next_tid(c->tid); } /* @@ -2295,8 +2296,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); } /* @@ -2583,6 +2582,7 @@ redo: if (!freelist) { c->page = NULL; + c->tid = next_tid(c->tid); stat(s, DEACTIVATE_BYPASS); goto new_slab; }