Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp5214399ybl; Tue, 27 Aug 2019 00:55:08 -0700 (PDT) X-Google-Smtp-Source: APXvYqz7VVSj/WuetESN3PnsQkAUAzIpkyAp+iDtJ0EPZW7sgpl83mpFTeV+gqeb0AZ3+XwjPMC3 X-Received: by 2002:aa7:8705:: with SMTP id b5mr25092894pfo.205.1566892508601; Tue, 27 Aug 2019 00:55:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1566892508; cv=none; d=google.com; s=arc-20160816; b=e4Xo83cHVRrVyqZLJ3s/y+CHxPa/LDqPp0rFEmatvaXKEucccg6m3s6qzWUxbQNvbU 8jPhYqIPewuMXykfVNp08Z4zSPsPNQOUVy723zOtFHAMUHrmP3MWCjqUliiGPDHM7WJa ZA798QmdL6ecxq1+tb8b4wCqlaMzYeg0DmGRfzjQgbBqlatA0V/3tcMDS9PerhPiTBP+ 3AH1KC5CWj0YsvF9+xq+2Cd8X285AR+Co0KrL90mSGUMMxaJYziO4yguItPIIgun1OFg 2hb27aFX57p/DZQl2a6NNI6OPzt5Ixu7pcaxx5JF7kp3jtDdBm6AmZjOQ3lh2DWH7ai7 kXWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=9lj362DMaY2Vi65z+QghXyqoVGITalZfucxyaor3Dvk=; b=MBIe5b2qkSRDQTAh8jkrvIc7YmJ4MT6204M/e8vqc/FTOZJ2O5vTPQ82d1rGmMYEQK RiMp3qxruV1Gox3CbG0HFVn8KrepMMtHkZJHsVIpA7jjFQS0Ms1+HnAdokYAze6rAmrR BX5XCqMX44cofcRwUcfn1zey2vtiQ/rTD+IJdJ9BiQcXwemzdEYsij3ze47O7DXRNijW DViBi1iDuzUu24nL3feaThUR+LxLjGxJY5ci0FGg/Anv+z3fHX6ELeKSV/RboWA4CYH3 z/GyqzARNq8Gvm+QwHnNOTktSEFPMkLqImfuyvniZ7pOW6AJPCKiUx8tbGHxDY3AEbK1 duuw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=JVqdXVgJ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b2si11560999plz.15.2019.08.27.00.54.53; Tue, 27 Aug 2019 00:55:08 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=JVqdXVgJ; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729788AbfH0Hx2 (ORCPT + 99 others); Tue, 27 Aug 2019 03:53:28 -0400 Received: from mail.kernel.org ([198.145.29.99]:44908 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1729762AbfH0Hx0 (ORCPT ); Tue, 27 Aug 2019 03:53:26 -0400 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 1E4BF206BA; Tue, 27 Aug 2019 07:53:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1566892405; bh=LD81ViPV9Dk2D1S2A6RNmJkyDuqapNAWlR6U5vdwRso=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=JVqdXVgJOGs9uggDq7cwYAI18Igf2289rDH/u3vVb4abkswdPyByxq3zpuJwIz7bQ QBwpHyoRaJQfsuX2PtgAWxHhUszHoTKIyx1rvUZe7IYtz/clx4DQR26c7QsHCJW6gH S+sIBR7x5Th1a11vdTA8+iI9Hwib7BbuhSwwOW5g= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Oleg Nesterov , Kefeng Wang , Andrea Arcangeli , Peter Xu , Mike Rapoport , Jann Horn , Jason Gunthorpe , Michal Hocko , Tetsuo Handa , Andrew Morton , Linus Torvalds Subject: [PATCH 4.14 43/62] userfaultfd_release: always remove uffd flags and clear vm_userfaultfd_ctx Date: Tue, 27 Aug 2019 09:50:48 +0200 Message-Id: <20190827072703.069744154@linuxfoundation.org> X-Mailer: git-send-email 2.23.0 In-Reply-To: <20190827072659.803647352@linuxfoundation.org> References: <20190827072659.803647352@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Oleg Nesterov commit 46d0b24c5ee10a15dfb25e20642f5a5ed59c5003 upstream. userfaultfd_release() should clear vm_flags/vm_userfaultfd_ctx even if mm->core_state != NULL. Otherwise a page fault can see userfaultfd_missing() == T and use an already freed userfaultfd_ctx. Link: http://lkml.kernel.org/r/20190820160237.GB4983@redhat.com Fixes: 04f5866e41fb ("coredump: fix race condition between mmget_not_zero()/get_task_mm() and core dumping") Signed-off-by: Oleg Nesterov Reported-by: Kefeng Wang Reviewed-by: Andrea Arcangeli Tested-by: Kefeng Wang Cc: Peter Xu Cc: Mike Rapoport Cc: Jann Horn Cc: Jason Gunthorpe Cc: Michal Hocko Cc: Tetsuo Handa Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- fs/userfaultfd.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -854,6 +854,7 @@ static int userfaultfd_release(struct in /* len == 0 means wake all */ struct userfaultfd_wake_range range = { .len = 0, }; unsigned long new_flags; + bool still_valid; ACCESS_ONCE(ctx->released) = true; @@ -869,8 +870,7 @@ static int userfaultfd_release(struct in * taking the mmap_sem for writing. */ down_write(&mm->mmap_sem); - if (!mmget_still_valid(mm)) - goto skip_mm; + still_valid = mmget_still_valid(mm); prev = NULL; for (vma = mm->mmap; vma; vma = vma->vm_next) { cond_resched(); @@ -881,19 +881,20 @@ static int userfaultfd_release(struct in continue; } new_flags = vma->vm_flags & ~(VM_UFFD_MISSING | VM_UFFD_WP); - prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end, - new_flags, vma->anon_vma, - vma->vm_file, vma->vm_pgoff, - vma_policy(vma), - NULL_VM_UFFD_CTX); - if (prev) - vma = prev; - else - prev = vma; + if (still_valid) { + prev = vma_merge(mm, prev, vma->vm_start, vma->vm_end, + new_flags, vma->anon_vma, + vma->vm_file, vma->vm_pgoff, + vma_policy(vma), + NULL_VM_UFFD_CTX); + if (prev) + vma = prev; + else + prev = vma; + } vma->vm_flags = new_flags; vma->vm_userfaultfd_ctx = NULL_VM_UFFD_CTX; } -skip_mm: up_write(&mm->mmap_sem); mmput(mm); wakeup: