Received: by 2002:a25:8b12:0:0:0:0:0 with SMTP id i18csp2115459ybl; Thu, 29 Aug 2019 03:52:43 -0700 (PDT) X-Google-Smtp-Source: APXvYqw5XHYWI+89mV2TrEYdAOUs4BgjKx6pOPL4kKgAz/oF2Wdcp1q4H7y408b+z8WgKdcXqmdU X-Received: by 2002:a63:ed50:: with SMTP id m16mr7617092pgk.209.1567075963532; Thu, 29 Aug 2019 03:52:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1567075963; cv=none; d=google.com; s=arc-20160816; b=y+6dkm/lmG0anCOmbzyaVyj+Hb8DurHS1ikFGRWdwQtNTlMjzjZgR9KqNH1X6APlYR KVjJ6bdpnBkpJ5KcbdGQpdzxU7O/3/G7yDRaQQq7OR4d6IEwp+MVw3sUsK7m8L+tCfr2 4v8PZWgm18RYzNh+Yw6TGk/SSpKWWyLNB11onFpC6ouUhXGzaNEcePaFOdoYQNj/BT9p R+icAm7NMaSgshjc2dcmSrqRup33TRLLdQmNGXeJpCODJ4RbnKjyweL91MIr69mD4PiD YgG+WrxE731NnvaOtcL35My+b6GqT1tOWw35sb/3OaeGtU769oDQ4g+crjB6Xy5WyPJT piEQ== 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 :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=DvSnVsaiA6wIW3dm63Y/9fCs2jqvJXro1qGDgMD7ggA=; b=hj2E0RPVSjHqobInQRsH3v6HxQG+g64CqaBuYcMQwwmPmDgdyq4V31KS3EU5bUd1ye 64Kh3B2GVCE0FVSLEPzf187FkCTr+gzU+DhRjNuq782IRDPcqgTGEEuCBTpqfunkC0lA uNR1S+6i4jep2ViyjTnEwIofEqzt/ayJGyS3FCySh/IomHVQxgXziIL7g0fSTlDiaJYD J6pff3S0qEu9hU8QVtnQz8Gh6WW3U62ewddVXiogjoSxvXq5Q4lXlRfJHdzC8bmtz6Tv CN+VLFnGjXA6/o/zlHd7reEDMXGBH1wT3kphq3qDJ1pV6wqroC9AxBR2ISK5N0HcM0/8 UR+g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=uJ8ChBRp; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id o9si2308287pfp.158.2019.08.29.03.52.28; Thu, 29 Aug 2019 03:52:43 -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=uJ8ChBRp; 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; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728540AbfH2KvN (ORCPT + 99 others); Thu, 29 Aug 2019 06:51:13 -0400 Received: from mail.kernel.org ([198.145.29.99]:58434 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728321AbfH2Kuz (ORCPT ); Thu, 29 Aug 2019 06:50:55 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 5629C2173E; Thu, 29 Aug 2019 10:50:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1567075853; bh=xOJKPB5a2rimu1th/X6a4Ux1/F/fwoCpS7d7eu1p124=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uJ8ChBRpPLpUrm4dnP+WLg1nyxYyJjGXGZjMJfQ5T5I1GeaonjZNlPljYKZE9GuqQ oMb99zxBOCcvC9il42AM0HhyuibDHOH6wZOdXEreYKSFFYgFLtZCiY8aYVKKxPx3VL X/25vCNAMTKnG/dflOzcLEun1MAlnykZSDD0MATQ= From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Oleg Nesterov , Kefeng Wang , Andrea Arcangeli , Peter Xu , Mike Rapoport , Jann Horn , Jason Gunthorpe , Michal Hocko , Tetsuo Handa , Andrew Morton , Linus Torvalds , Sasha Levin , linux-fsdevel@vger.kernel.org Subject: [PATCH AUTOSEL 4.14 08/14] userfaultfd_release: always remove uffd flags and clear vm_userfaultfd_ctx Date: Thu, 29 Aug 2019 06:50:37 -0400 Message-Id: <20190829105043.2508-8-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190829105043.2508-1-sashal@kernel.org> References: <20190829105043.2508-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore 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 [ Upstream commit 46d0b24c5ee10a15dfb25e20642f5a5ed59c5003 ] 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: Sasha Levin --- fs/userfaultfd.c | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 7a908d6832582..a609d480606da 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -854,6 +854,7 @@ static int userfaultfd_release(struct inode *inode, struct file *file) /* 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 inode *inode, struct file *file) * 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 inode *inode, struct file *file) 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: -- 2.20.1