From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 78CE82D0C97 for ; Fri, 19 Jun 2026 19:40:52 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=193.142.43.55 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781898055; cv=none; b=XE6z5wHpFEepPOM0tbZs4i+BIQwo06hAsxPoueaRVCJIvUxDau7g/HzbtGuagTGOW9HWpZgCobbqr9wblbCxh/pbQ0ILhOrVhbCL3aBZ1SXSmBzGpaWw2c+OZ0kcPXleOBYNf03ltV6+f9ny+ETNWCrDnksMj6VJFOHcwPWcrEw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781898055; c=relaxed/simple; bh=w1sZ3qgW2wc6R3q9INTdb1/e7R7raxHPWHD+nzFv3Ls=; h=From:To:Cc:Subject:In-Reply-To:References:Date:Message-ID: MIME-Version:Content-Type; b=CNCLUr/HfRjvTi53yuImXdk3GPdxAN8AknMNCcyG0dS9CmHFlTEIBB1+b/vi5E2GkCjhsGoaSBdeRdmBQ/rWEvySxlQ0fa5GFhzMQw5rR7qBzWhf3st0eNBgS3OiPPfwJfKjx+Pq8K6VJpp/+jtC0AmHXgMLbXO0g2lO27lZFBY= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de; spf=pass smtp.mailfrom=linutronix.de; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=W7TfPp4B; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b=lOXbHu9D; arc=none smtp.client-ip=193.142.43.55 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linutronix.de Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linutronix.de Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="W7TfPp4B"; dkim=permerror (0-bit key) header.d=linutronix.de header.i=@linutronix.de header.b="lOXbHu9D" From: Thomas Gleixner DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1781898050; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=hEB4zEmbr6RkR3vR/o7M7ojevJ4g0wZTiOWTPCaXb0s=; b=W7TfPp4BCJD1C6ocpzBIK5BZTApl3dEAahqwIqNV6CjvsROlPDrzSI+N2Xp6ywuNXqEkDa sgqIyvgeWztwjaOA76muUG7HbabqzWC+kadwydMNSsZ2+YKkTvKESu1duUS8HWTEletzYw O3zmuPMtBbkB5BjGb74+2Rv/tJGo4ZipcvnXn1A8T9NCmB/opVgipmFw1Dsy8ZNuSECnYl 8vGgdoKlximXVJrxVnSxofpd1vSlZpaySnbBVJ2oicISti2XKQ5eYklbBTlPhOVzD5flMD y2j2A+pRk1+1fvamU59x10jBgSYvb0oxJBLyCDtD8zPTweT/7d43qT9LweblMw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1781898050; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=hEB4zEmbr6RkR3vR/o7M7ojevJ4g0wZTiOWTPCaXb0s=; b=lOXbHu9DZabWBsfBJok5rgFm2rJkxK6dpcTGL16Sd8diaAn06VR4vISCLSgNsT/EYoib6s 5eAQN2hDZr0KnSBw== To: Rik van Riel , linux-kernel@vger.kernel.org Cc: kernel-team@meta.com, Rik van Riel , Ingo Molnar , Peter Zijlstra , Juri Lelli , Vincent Guittot , Mathieu Desnoyers , Dietmar Eggemann , Steven Rostedt , Ben Segall , Mel Gorman , Valentin Schneider , K Prateek Nayak Subject: Re: [PATCH v3] sched/mmcid: fix OOB clear_bit when CID is MM_CID_UNSET in fixup path In-Reply-To: <20260616203818.1516263-1-riel@surriel.com> References: <20260616203818.1516263-1-riel@surriel.com> Date: Fri, 19 Jun 2026 21:40:49 +0200 Message-ID: <87zf0qb9oe.ffs@fw13> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain On Tue, Jun 16 2026 at 16:38, Rik van Riel wrote: > In mm_cid_fixup_cpus_to_tasks(), when rq->curr has the target mm and > mm_cid.active is set, the CID is checked with cid_in_transit() before > setting the transition bit. In per-CPU mode a newly forked or exec'd > task can be running with mm_cid.cid == MM_CID_UNSET because CIDs are > assigned lazily on schedule-in. With cid_in_transit() the guard passes > for MM_CID_UNSET (no transit bit), converts it to MM_CID_UNSET | > MM_CID_TRANSIT and stores it back; later mm_cid_schedout() feeds this > to clear_bit() with MM_CID_UNSET as the bit number, triggering an > out-of-bounds write. > > Symptoms: this is genuine memory corruption, but a bounded out-of-bounds > write, not an arbitrary one. MM_CID_UNSET is the fixed sentinel BIT(31), > so once the bad value reaches mm_cid_schedout() the cid_from_transit_cid() > strip leaves MM_CID_UNSET, which fails the "cid < max_cids" convergence > test and falls into mm_drop_cid() -> clear_bit(MM_CID_UNSET, > mm_cidmask(mm)). The cid bitmap is embedded in the mm_struct slab object > (after cpu_bitmap and mm_cpus_allowed) and is only num_possible_cpus() > bits wide, so clearing bit 31 is a deterministic OOB bit-clear at a > fixed offset of 2^31 / 8 == 256 MiB past the bitmap base. The address is > not attacker-influenced (fixed sentinel -> fixed offset) and the op only > clears a single bit; what sits 256 MiB further along the direct map is > whatever kernel object happens to live there, so this corrupts one bit of > unpredictable kernel memory -- it is not an arbitrary-address or > arbitrary-value write. > > It triggers only in per-CPU CID mode, when a CPU is running an active > task of the target mm whose cid is still MM_CID_UNSET -- the > fork()/execve() window before that task's next schedule-in assigns it a > real CID -- and a per-CPU -> per-task fixup walks over it (the mode > fallback driven by a thread exit, sched_mm_cid_exit(), or by the deferred > max_cids recompute in mm_cid_work_fn()). > > In practice syzkaller surfaced it as a KASAN use-after-free reported in > __schedule -> mm_cid_switch_to, where the offending clear_bit() is inlined > via mm_cid_schedout() -> mm_drop_cid(). > > Guard the transition-bit assignment against MM_CID_UNSET, in addition to > the existing cid_in_transit() check, so the bit is only set on a genuine > task-owned CID. A CPU-owned (MM_CID_ONCPU) CID of a running active task > is handled by the cid_on_cpu(pcp->cid) branch above and never reaches > this path, so excluding MM_CID_UNSET (and the already-transitioning case) > is sufficient. Duh. Now that you explained it it's obvious. Thanks for tracking this nasty down!