Received: by 2002:a17:90b:8d0:0:0:0:0 with SMTP id ds16csp4879188pjb; Mon, 27 Jul 2020 07:27:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJybpXMTMM9NSP9ZevFeAzZ0JfJTvHZujYuclEHbEZ+G4ejAi3WPRdLh0gtZnNyU5HqhMNYS X-Received: by 2002:a17:906:7fc8:: with SMTP id r8mr22301126ejs.412.1595860032460; Mon, 27 Jul 2020 07:27:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1595860032; cv=none; d=google.com; s=arc-20160816; b=CmMmu/PjfpU6q9r1eldVixU42kh6PsobSsJ3oCh4GZ7ycsrJhpDeQYw8+oOUg30OOU 1HhEPh5/DinQuOdJW1mUCRAcTPYv/RtUKyNUN7bKxdXn3a4+1Be+MfW3hslpm8c2klCf A3P+NrTcxu187RoMuETTb6oqdwxSaw+nbMLW1F3InHl4VHSiUjAxaRVBzdAA1ZqjP0H6 IN8L1F8wxc2VOAhcEeCxb45zLb/MQ11SE63yizD2oKG2x9rXx3ZwFed3XbIHLFZjiGqn RvRhmQtsRRYP5MyVEj1hBpeL0iEUBxiEs3i3Sw9/U84lFLc3JVfI4lsZq1272QKaX3rk rg6A== 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=PJ721pg1T0rgsHVF6gkazFuj01+2MQ3z+X76mEe7kEM=; b=uewS33wM0t5UuVHhhb0cBpOuIouRt5QkIWFqI8p5tmp0OBM9TyBouCoNPhLddAp/rw BO5iCBAwvj6XxUQdYgkN+fCMsefmgAEDsVPDS2uic/OgJUHiiXMX4rFbEk+R25yeY/eA PJkf1YWoknLMJcbocXFDMfAfzjpnT3t/YzTmeLSBPtnomUIr9+ZdfOADM9ckpD/Hs+ng EEWZrrEz4ptJMCECI7xR7Ig1SxCFHjI6buy/Bkw7YvvMUkUC7BuQ42h39CtgUYPi87uk a8VrW8plxsHTDNpXlmGN9Dpwm6SROO/rYZ1ulBJF1vW3qacou2id0d1/IBzO4m0pBHQQ WyYw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=qTLYdjFi; 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 o21si5368233ejc.533.2020.07.27.07.26.49; Mon, 27 Jul 2020 07:27:12 -0700 (PDT) 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=@kernel.org header.s=default header.b=qTLYdjFi; 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 S1731828AbgG0OZ3 (ORCPT + 99 others); Mon, 27 Jul 2020 10:25:29 -0400 Received: from mail.kernel.org ([198.145.29.99]:55374 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732431AbgG0OZ0 (ORCPT ); Mon, 27 Jul 2020 10:25: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 55881207FC; Mon, 27 Jul 2020 14:25:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1595859925; bh=9Qcp3FSel9atNIpCQ+TYVISgopa85/qqx+u7nEV1sU8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=qTLYdjFio/BIuezEJhypbMIpXFhTLwoGs8J0qi7nXlkZggqRyOFZRsnNrcuLuqIzf gXDCJ49MoHkKG6nTNFRFAkBatnj3kPBD3P3X2UnZMIrauHO3ClbiRi25J0OkX0kG/7 PAPBnC1FS/2jm6zdrG30zF9nQYFycD+tg8Zbbq1A= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jann Horn , "Kirill A. Shutemov" , Andrew Morton , Yang Shi , Vlastimil Babka , Oleg Nesterov , Matthew Wilcox , Linus Torvalds Subject: [PATCH 5.7 159/179] mm/mmap.c: close race between munmap() and expand_upwards()/downwards() Date: Mon, 27 Jul 2020 16:05:34 +0200 Message-Id: <20200727134940.418084921@linuxfoundation.org> X-Mailer: git-send-email 2.27.0 In-Reply-To: <20200727134932.659499757@linuxfoundation.org> References: <20200727134932.659499757@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: Kirill A. Shutemov commit 246c320a8cfe0b11d81a4af38fa9985ef0cc9a4c upstream. VMA with VM_GROWSDOWN or VM_GROWSUP flag set can change their size under mmap_read_lock(). It can lead to race with __do_munmap(): Thread A Thread B __do_munmap() detach_vmas_to_be_unmapped() mmap_write_downgrade() expand_downwards() vma->vm_start = address; // The VMA now overlaps with // VMAs detached by the Thread A // page fault populates expanded part // of the VMA unmap_region() // Zaps pagetables partly // populated by Thread B Similar race exists for expand_upwards(). The fix is to avoid downgrading mmap_lock in __do_munmap() if detached VMAs are next to VM_GROWSDOWN or VM_GROWSUP VMA. [akpm@linux-foundation.org: s/mmap_sem/mmap_lock/ in comment] Fixes: dd2283f2605e ("mm: mmap: zap pages with read mmap_sem in munmap") Reported-by: Jann Horn Signed-off-by: Kirill A. Shutemov Signed-off-by: Andrew Morton Reviewed-by: Yang Shi Acked-by: Vlastimil Babka Cc: Oleg Nesterov Cc: Matthew Wilcox Cc: [4.20+] Link: http://lkml.kernel.org/r/20200709105309.42495-1-kirill.shutemov@linux.intel.com Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- mm/mmap.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2620,7 +2620,7 @@ static void unmap_region(struct mm_struc * Create a list of vma's touched by the unmap, removing them from the mm's * vma list as we go.. */ -static void +static bool detach_vmas_to_be_unmapped(struct mm_struct *mm, struct vm_area_struct *vma, struct vm_area_struct *prev, unsigned long end) { @@ -2645,6 +2645,17 @@ detach_vmas_to_be_unmapped(struct mm_str /* Kill the cache */ vmacache_invalidate(mm); + + /* + * Do not downgrade mmap_lock if we are next to VM_GROWSDOWN or + * VM_GROWSUP VMA. Such VMAs can change their size under + * down_read(mmap_lock) and collide with the VMA we are about to unmap. + */ + if (vma && (vma->vm_flags & VM_GROWSDOWN)) + return false; + if (prev && (prev->vm_flags & VM_GROWSUP)) + return false; + return true; } /* @@ -2825,7 +2836,8 @@ int __do_munmap(struct mm_struct *mm, un } /* Detach vmas from rbtree */ - detach_vmas_to_be_unmapped(mm, vma, prev, end); + if (!detach_vmas_to_be_unmapped(mm, vma, prev, end)) + downgrade = false; if (downgrade) downgrade_write(&mm->mmap_sem);