From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp-out1.suse.de (smtp-out1.suse.de [195.135.223.130]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9DFD2192D8A for ; Mon, 28 Jul 2025 21:42:50 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=195.135.223.130 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753738972; cv=none; b=d9Jhv4pq5G+h4iEWt185mB1LcAj9wsbcqk6LZoAqRrrXCYAgQYo0ykyEXHUNz6mDYhJRkCusc6865A453N8GaTxWR1hVI/2A2dlvJIfHl2BUzy1HEVHxwBBhdN3x8g/sd4HVPyNBBjnNy2Bn9hmRcSNExxjoEhvOXEfUprJHefo= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1753738972; c=relaxed/simple; bh=wYCY8RjdFV5iq7MmZrHeJvy3x1u6kkQjRxL8tgZ3+9M=; h=Message-ID:Date:MIME-Version:Subject:To:Cc:References:From: In-Reply-To:Content-Type; b=fLajK3Guu3WfjebBjeWvXqaIdYAFneJmjuRWj00ykwsi8XpsR6HYl4diz3OVNFs0r1hEpidqwTjRjjMZBSJXsWSNIemTgiBGfDkJvmBioOd9Q+EzK17VPTBGGA944V0NwAbpKj8ATIr4Kw2olCSDLVu/vrWiHgv7uq3uM89Lir4= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz; spf=pass smtp.mailfrom=suse.cz; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b=yKp8n9Lu; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b=IcXzhZDj; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b=O42lQOT5; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b=c8b42eqb; arc=none smtp.client-ip=195.135.223.130 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=suse.cz Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=suse.cz Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b="yKp8n9Lu"; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b="IcXzhZDj"; dkim=pass (1024-bit key) header.d=suse.cz header.i=@suse.cz header.b="O42lQOT5"; dkim=permerror (0-bit key) header.d=suse.cz header.i=@suse.cz header.b="c8b42eqb" Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id A390A21289; Mon, 28 Jul 2025 21:42:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1753738969; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:autocrypt:autocrypt; bh=6kYLrjhTCwgiO/q5O0ScJ+Y3bmPYFFc08jN4aiaBZLs=; b=yKp8n9Lu65/pFksaSm0Cx5OX9tRIbQshueFX/jBmJnWOnJz1KOQ6dA3jE60KWsxiPrrraL NPKnEXkx164CnsgVF+kvYiTazEu7XGe1MGT8v6n2PrKSTE4gAwHJFnD2AYTnRN/qVv14do dyMR2UHQGl8Qwa15SYZZlaMkYDCKM0M= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1753738969; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:autocrypt:autocrypt; bh=6kYLrjhTCwgiO/q5O0ScJ+Y3bmPYFFc08jN4aiaBZLs=; b=IcXzhZDjDjR6g6ERlgdlz8yyDsvYYF9adQm2odt44Pb09nOIDG6sX5WPcTvaeiK/jZmn9l WyuDQZIIFdr06NCg== Authentication-Results: smtp-out1.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_rsa; t=1753738967; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:autocrypt:autocrypt; bh=6kYLrjhTCwgiO/q5O0ScJ+Y3bmPYFFc08jN4aiaBZLs=; b=O42lQOT5z03gbYYF/pi0808TLVL+MGfIctZ21WDWl/zots9kHSl1ZQ+NSoBf5c2gTNDa8o acpyEDJvep9KDGl5AgG58nekVnoHoKrwaEnBY/eTBTYV+2JQvF56VNXV0lXN1SVZe5A39s wqS8RHJmOiJ9dWldp6FO/KomvMhuw0k= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.cz; s=susede2_ed25519; t=1753738967; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:autocrypt:autocrypt; bh=6kYLrjhTCwgiO/q5O0ScJ+Y3bmPYFFc08jN4aiaBZLs=; b=c8b42eqb36caReC8Qza4Fx22d8mdh6OPCg53YYswZ1aidvfB5VYbDifalXaLEAKeZL4BgE /8E4S7bsrJo678Dw== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 8CFFF1368A; Mon, 28 Jul 2025 21:42:47 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id sWUNItfuh2gKDwAAD6G6ig (envelope-from ); Mon, 28 Jul 2025 21:42:47 +0000 Message-ID: <39fbeb2c-ef42-4898-b0f8-9340a6e9988e@suse.cz> Date: Mon, 28 Jul 2025 23:42:47 +0200 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v2 1/1] mm: fix a UAF when vma->mm is freed after vma->vm_refcnt got dropped Content-Language: en-US To: Suren Baghdasaryan , akpm@linux-foundation.org Cc: jannh@google.com, Liam.Howlett@oracle.com, lorenzo.stoakes@oracle.com, pfalcato@suse.de, linux-mm@kvack.org, linux-kernel@vger.kernel.org, stable@vger.kernel.org References: <20250728175355.2282375-1-surenb@google.com> From: Vlastimil Babka Autocrypt: addr=vbabka@suse.cz; keydata= xsFNBFZdmxYBEADsw/SiUSjB0dM+vSh95UkgcHjzEVBlby/Fg+g42O7LAEkCYXi/vvq31JTB KxRWDHX0R2tgpFDXHnzZcQywawu8eSq0LxzxFNYMvtB7sV1pxYwej2qx9B75qW2plBs+7+YB 87tMFA+u+L4Z5xAzIimfLD5EKC56kJ1CsXlM8S/LHcmdD9Ctkn3trYDNnat0eoAcfPIP2OZ+ 9oe9IF/R28zmh0ifLXyJQQz5ofdj4bPf8ecEW0rhcqHfTD8k4yK0xxt3xW+6Exqp9n9bydiy tcSAw/TahjW6yrA+6JhSBv1v2tIm+itQc073zjSX8OFL51qQVzRFr7H2UQG33lw2QrvHRXqD Ot7ViKam7v0Ho9wEWiQOOZlHItOOXFphWb2yq3nzrKe45oWoSgkxKb97MVsQ+q2SYjJRBBH4 8qKhphADYxkIP6yut/eaj9ImvRUZZRi0DTc8xfnvHGTjKbJzC2xpFcY0DQbZzuwsIZ8OPJCc LM4S7mT25NE5kUTG/TKQCk922v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reply-To: <20250728175355.2282375-1-surenb@google.com> Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Level: X-Spamd-Result: default: False [-4.30 / 50.00]; BAYES_HAM(-3.00)[100.00%]; NEURAL_HAM_LONG(-1.00)[-1.000]; NEURAL_HAM_SHORT(-0.20)[-0.998]; MIME_GOOD(-0.10)[text/plain]; RCVD_VIA_SMTP_AUTH(0.00)[]; FUZZY_RATELIMITED(0.00)[rspamd.com]; ARC_NA(0.00)[]; MIME_TRACE(0.00)[0:+]; RCPT_COUNT_SEVEN(0.00)[9]; MID_RHS_MATCH_FROM(0.00)[]; RCVD_TLS_ALL(0.00)[]; DKIM_SIGNED(0.00)[suse.cz:s=susede2_rsa,suse.cz:s=susede2_ed25519]; FROM_HAS_DN(0.00)[]; TO_DN_SOME(0.00)[]; FROM_EQ_ENVFROM(0.00)[]; TO_MATCH_ENVRCPT_ALL(0.00)[]; RCVD_COUNT_TWO(0.00)[2]; DBL_BLOCKED_OPENRESOLVER(0.00)[suse.cz:email,suse.cz:mid] X-Spam-Flag: NO X-Spam-Score: -4.30 On 7/28/25 19:53, Suren Baghdasaryan wrote: > By inducing delays in the right places, Jann Horn created a reproducer > for a hard to hit UAF issue that became possible after VMAs were allowed > to be recycled by adding SLAB_TYPESAFE_BY_RCU to their cache. > > Race description is borrowed from Jann's discovery report: > lock_vma_under_rcu() looks up a VMA locklessly with mas_walk() under > rcu_read_lock(). At that point, the VMA may be concurrently freed, and > it can be recycled by another process. vma_start_read() then > increments the vma->vm_refcnt (if it is in an acceptable range), and > if this succeeds, vma_start_read() can return a recycled VMA. > > In this scenario where the VMA has been recycled, lock_vma_under_rcu() > will then detect the mismatching ->vm_mm pointer and drop the VMA > through vma_end_read(), which calls vma_refcount_put(). > vma_refcount_put() drops the refcount and then calls rcuwait_wake_up() > using a copy of vma->vm_mm. This is wrong: It implicitly assumes that > the caller is keeping the VMA's mm alive, but in this scenario the caller > has no relation to the VMA's mm, so the rcuwait_wake_up() can cause UAF. > > The diagram depicting the race: > T1 T2 T3 > == == == > lock_vma_under_rcu > mas_walk > > mmap > > vma_start_read > __refcount_inc_not_zero_limited_acquire > munmap > __vma_enter_locked > refcount_add_not_zero > vma_end_read > vma_refcount_put > __refcount_dec_and_test > rcuwait_wait_event > > rcuwait_wake_up [UAF] > > Note that rcuwait_wait_event() in T3 does not block because refcount > was already dropped by T1. At this point T3 can exit and free the mm > causing UAF in T1. > To avoid this we move vma->vm_mm verification into vma_start_read() and > grab vma->vm_mm to stabilize it before vma_refcount_put() operation. > > Fixes: 3104138517fc ("mm: make vma cache SLAB_TYPESAFE_BY_RCU") > Reported-by: Jann Horn > Closes: https://lore.kernel.org/all/CAG48ez0-deFbVH=E3jbkWx=X3uVbd8nWeo6kbJPQ0KoUD+m2tA@mail.gmail.com/ > Signed-off-by: Suren Baghdasaryan > Cc: Reviewed-by: Vlastimil Babka > --- > Changes since v1 [1] > - Made a copy of vma->mm before using it in vma_start_read(), > per Vlastimil Babka > > Notes: > - Applies cleanly over mm-unstable. > - Should be applied to 6.15 and 6.16 but these branches do not > have lock_next_vma() function, so the change in lock_next_vma() should be > skipped when applying to those branches. > > [1] https://lore.kernel.org/all/20250728170950.2216966-1-surenb@google.com/ > > include/linux/mmap_lock.h | 23 +++++++++++++++++++++++ > mm/mmap_lock.c | 10 +++------- > 2 files changed, 26 insertions(+), 7 deletions(-) > > diff --git a/include/linux/mmap_lock.h b/include/linux/mmap_lock.h > index 1f4f44951abe..da34afa2f8ef 100644 > --- a/include/linux/mmap_lock.h > +++ b/include/linux/mmap_lock.h > @@ -12,6 +12,7 @@ extern int rcuwait_wake_up(struct rcuwait *w); > #include > #include > #include > +#include > > #define MMAP_LOCK_INITIALIZER(name) \ > .mmap_lock = __RWSEM_INITIALIZER((name).mmap_lock), > @@ -183,6 +184,28 @@ static inline struct vm_area_struct *vma_start_read(struct mm_struct *mm, > } > > rwsem_acquire_read(&vma->vmlock_dep_map, 0, 1, _RET_IP_); > + > + /* > + * If vma got attached to another mm from under us, that mm is not > + * stable and can be freed in the narrow window after vma->vm_refcnt > + * is dropped and before rcuwait_wake_up(mm) is called. Grab it before > + * releasing vma->vm_refcnt. > + */ > + if (unlikely(vma->vm_mm != mm)) { > + /* Use a copy of vm_mm in case vma is freed after we drop vm_refcnt */ > + struct mm_struct *other_mm = vma->vm_mm; > + /* > + * __mmdrop() is a heavy operation and we don't need RCU > + * protection here. Release RCU lock during these operations. > + */ > + rcu_read_unlock(); > + mmgrab(other_mm); > + vma_refcount_put(vma); > + mmdrop(other_mm); > + rcu_read_lock(); > + return NULL; > + } > + > /* > * Overflow of vm_lock_seq/mm_lock_seq might produce false locked result. > * False unlocked result is impossible because we modify and check > diff --git a/mm/mmap_lock.c b/mm/mmap_lock.c > index 729fb7d0dd59..aa3bc42ecde0 100644 > --- a/mm/mmap_lock.c > +++ b/mm/mmap_lock.c > @@ -164,8 +164,7 @@ struct vm_area_struct *lock_vma_under_rcu(struct mm_struct *mm, > */ > > /* Check if the vma we locked is the right one. */ > - if (unlikely(vma->vm_mm != mm || > - address < vma->vm_start || address >= vma->vm_end)) > + if (unlikely(address < vma->vm_start || address >= vma->vm_end)) > goto inval_end_read; > > rcu_read_unlock(); > @@ -236,11 +235,8 @@ struct vm_area_struct *lock_next_vma(struct mm_struct *mm, > goto fallback; > } > > - /* > - * Verify the vma we locked belongs to the same address space and it's > - * not behind of the last search position. > - */ > - if (unlikely(vma->vm_mm != mm || from_addr >= vma->vm_end)) > + /* Verify the vma is not behind of the last search position. */ > + if (unlikely(from_addr >= vma->vm_end)) > goto fallback_unlock; > > /* > > base-commit: c617a4dd7102e691fa0fb2bc4f6b369e37d7f509