Received: by 2002:a05:6a10:1a4d:0:0:0:0 with SMTP id nk13csp899030pxb; Tue, 1 Feb 2022 12:44:35 -0800 (PST) X-Google-Smtp-Source: ABdhPJz5/j+d6Hxo531niL6eQlEsfcum3p/mKkk7AG0++KH83A3MSYEu8zTzNMPYm/dhJZ6kP0Sa X-Received: by 2002:a05:6a00:1a53:: with SMTP id h19mr26582306pfv.65.1643748274802; Tue, 01 Feb 2022 12:44:34 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1643748274; cv=none; d=google.com; s=arc-20160816; b=RO6kO3ZWbLqVzW96r8zIdEglePzi0ezhc7lVv/GiGFD0q33mjZqGZcSckbRXNqkmZg 3H8qtvGzv4/7xkZGTmhIq4fkBbJ+0TUll46KwbOtqUvnZBkF/0e0BiOGA72pY7EfZU3K fR1cyHcQh0scby2jVurqIQVW1si0cIWYPQAqyNIuuacpL7BlBwcshwF6k7GbAZb150GZ 531Pa8OAk3YOzJ5VSKymW1BoTtdMpaqsBtmBz9jGykGNLcRKPAwYDHTqpEn5Gq+ro/1i oDvCQdvMjhHiwE4scrg2ogqtfI0cIlyZVNcxnNZcUF2MAjgkZQcGpydAp8iIyg6FaaDp LFtg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:content-disposition:mime-version :references:message-id:subject:cc:to:from:date:dkim-signature; bh=uxNZYP3LGCky/ZKmDFz/8zNFJ/WaFMrh5Ih+cZEMlts=; b=swaAIdc7mfh6fDwfgBFydEpDaZfGOFC+6+/ntdemuHSMDKMnlq+I50qlEiSQzSy1q0 Q41tbf39S5PDKl+XCbA8mTMTrPZrBbqyOkN3Cp6ifj4csJSFVFCWzQdkMzwskudWzL0p MMSzxn6T8pmfdRzGjlCjmgXlveHpsyCy+VbEz2io0DtDx/BXLVBUcP+QqiubiVYeHUmA +NEptG6+WyyEImZvDZNpinqeNxLHTU6+4fSMykOryPytOGCi5Uaz+eYazfHez9xOuUHv KvtgIv/LLoGEb+G+WdFi9vcjwVPVPYioSPNmhpvmjJJkTXhnEDkqbVwZw/6Jb7IJjEw4 D5UQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@infradead.org header.s=casper.20170209 header.b=XZVTYtHj; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i15si18044353pgo.387.2022.02.01.12.44.23; Tue, 01 Feb 2022 12:44:34 -0800 (PST) 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=@infradead.org header.s=casper.20170209 header.b=XZVTYtHj; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1359521AbiAaQfR (ORCPT + 99 others); Mon, 31 Jan 2022 11:35:17 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37918 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1380512AbiAaQfQ (ORCPT ); Mon, 31 Jan 2022 11:35:16 -0500 Received: from casper.infradead.org (casper.infradead.org [IPv6:2001:8b0:10b:1236::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id BA9C0C061714; Mon, 31 Jan 2022 08:35:15 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=In-Reply-To:Content-Type:MIME-Version: References:Message-ID:Subject:Cc:To:From:Date:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description; bh=uxNZYP3LGCky/ZKmDFz/8zNFJ/WaFMrh5Ih+cZEMlts=; b=XZVTYtHjrOi/dz9ct7Y6VN7aL+ igs/tFjbkhhZiIlBOBWn1fOIDa49M5C+aKAyluZFWKsDn4ZLtTqUt/9fLr5QO3lQ3G2CvpZMHD6p9 uA4aDjgsMbmXxHST4Punkm+KumfyRKFStZRe/JHF40DHKG2oCCFtenKt9gXMhAgXlUhWr/mMSZUzo nJq4iw4fI8iipgcT1jmL1sYgkuOhGmLqV0uelUIuAThItlKHUGkhBfVQ6KwoRJlw7AWrO0M6uAdpj oyZq+teZb44xsQ2q5NWYscPCA9k1UC4JUC6GlfyiK8AdR33L18xRB1CMAnIKDNIPj1bP97Jo/NsJ1 7I3bVl/w==; Received: from willy by casper.infradead.org with local (Exim 4.94.2 #2 (Red Hat Linux)) id 1nEZe4-00AAB1-JG; Mon, 31 Jan 2022 16:35:12 +0000 Date: Mon, 31 Jan 2022 16:35:12 +0000 From: Matthew Wilcox To: "Eric W. Biederman" Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, Alexander Viro , Denys Vlasenko , Kees Cook , Jann Horn , Vlastimil Babka , "Liam R . Howlett" Subject: Re: [PATCH] binfmt_elf: Take the mmap lock when walking the VMA list Message-ID: References: <20220131153740.2396974-1-willy@infradead.org> <871r0nriy4.fsf@email.froward.int.ebiederm.org> <877dafq3bw.fsf@email.froward.int.ebiederm.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <877dafq3bw.fsf@email.froward.int.ebiederm.org> Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jan 31, 2022 at 10:26:11AM -0600, Eric W. Biederman wrote: > Matthew Wilcox writes: > > > On Mon, Jan 31, 2022 at 10:03:31AM -0600, Eric W. Biederman wrote: > >> "Matthew Wilcox (Oracle)" writes: > >> > >> > I'm not sure if the VMA list can change under us, but dump_vma_snapshot() > >> > is very careful to take the mmap_lock in write mode. We only need to > >> > take it in read mode here as we do not care if the size of the stack > >> > VMA changes underneath us. > >> > > >> > If it can be changed underneath us, this is a potential use-after-free > >> > for a multithreaded process which is dumping core. > >> > >> The problem is not multi-threaded process so much as processes that > >> share their mm. > > > > I don't understand the difference. I appreciate that another process can > > get read access to an mm through, eg, /proc, but how can another process > > (that isn't a thread of this process) modify the VMAs? > > There are a couple of ways. > > A classic way is a multi-threads process can call vfork, and the > mm_struct is shared with the child until exec is called. While true, I thought the semantics of vfork() were that the parent was suspended. Given that, it can't core dump until the child execs ... right? > A process can do this more deliberately by forking a child using > clone(CLONE_VM) and not including CLONE_THREAD. Supporting this case > is a hold over from before CLONE_THREAD was supported in the kernel and > such processes were used to simulate threads. That is a multithreaded process then! Maybe not in the strict POSIX compliance sense, but the intent is to be a multithreaded process. ie multiple threads of execution, sharing an address space. > It also happens that there are subsystems in the kernel that do things > like kthread_use_mm that can also be modifying the mm during a coredump. Yikes. That's terrifying. It's really legitimate for a kthread to attach to a process and start tearing down VMAs? > > Uhh .. that seems like it needs a lot more understanding of binfmt_elf > > than I currently possess. I'd rather spend my time working on folios > > than learning much more about binfmt_elf. I was just trying to fix an > > assertion failure with the maple tree patches (we now assert that you're > > holding a lock when walking the list of VMAs). > > Fair enough. I will put it on my list of things to address. Thanks. Now that I've disclosed it's a UAF, I hope you're able to get to it soon. Otherwise we should put this band-aid in for now and you can address it properly in the fullness of time.