Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1304557ybt; Thu, 9 Jul 2020 03:53:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJy10RjSfHNmld1/PEK+HkV8yvKz/ztvWATUyA/rGauOs3/TENp0Ep5ZRfCvWoxLROFGyhab X-Received: by 2002:a17:906:c10f:: with SMTP id do15mr58515628ejc.249.1594292029699; Thu, 09 Jul 2020 03:53:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1594292029; cv=none; d=google.com; s=arc-20160816; b=T0BfuSLyxb6yQ44v4d8YHz3KKxnhbEU1a1A4ocPYxrQfeOubzczrgbxWXWj4a6Bi8c yiwqXyOesa4BVUS6hfyH5ms9kdM3mSqoieX+OwuoLmR1WJMX7+h9kSe9y8P8F4Oy+iuU kqMy9HmKFoSLzBXM+FUnOULvO7ty7adNlo47toKlar0Knaa2SUJRCwHabTvEGgfFEJLg SpvQhtwKkriLUg4hJzmOI/zmGzYSMfcwzK6KvL5VcCcQLKxMrOFydVOQwN0Fyvazq+f1 gmlc51HJ2bNRhLMFV+rUb4vWmYbILsmgm2ELERia3dlwWrFOjn7z2DsSD5/41ws0RnP9 QDIA== 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 :message-id:date:subject:cc:to:from:ironport-sdr:ironport-sdr; bh=2YcdYwqKOVugyVfouudtL4kTJIQ6g70tR2tVAbzc8ZI=; b=WXCzvuHNLsrysgbuenmTQHQ+Pst0dPY8K1l2ygeBMHLIR6Dt+g68kMY0WX71IIAOv6 MHPKJYUWlxZIOWRfQeWR5EYX/nJ/5s/GSlkyBhId5ryd6QN9sLXa74uqdReaAksp90cp +Y7RRi3+vrJYWJCV7Lzmt4ZWtYUFKbQ+QGgGZ29Q4g+CT4FhytXVrKpelJt2CrFHz/Wu mU4i5Pw7Iegy1cVGzSG1GTpuSj1qU+EXWoxva7SL8gDu+ieKn8M8V1Hwu+6pyewfRDQJ Q2HHA/cWavEkfMOjVy6AiuSvmEPdB36Hc0RphamR7S70pneMezew41sMjDVBNcu51Esw wYIw== ARC-Authentication-Results: i=1; mx.google.com; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id yj19si1561648ejb.410.2020.07.09.03.53.27; Thu, 09 Jul 2020 03:53:49 -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; 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726920AbgGIKxS (ORCPT + 99 others); Thu, 9 Jul 2020 06:53:18 -0400 Received: from mga12.intel.com ([192.55.52.136]:25233 "EHLO mga12.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726513AbgGIKxS (ORCPT ); Thu, 9 Jul 2020 06:53:18 -0400 IronPort-SDR: 5bAOzo8vPO0qiBySoGeyAmQc8XplNZo/kY3UwKt35RYS3ppVxSc0OH3KJzWSDiK4rPMsyiMwdw dBVkqq9dSqjw== X-IronPort-AV: E=McAfee;i="6000,8403,9676"; a="127557441" X-IronPort-AV: E=Sophos;i="5.75,331,1589266800"; d="scan'208";a="127557441" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by fmsmga106.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 09 Jul 2020 03:53:17 -0700 IronPort-SDR: P4hWY/sye1VYuLTzDCkFtKV52yM7tKk5/vXcJpES3ptC2WS01pmaVpqjBuZ+rp+5rbNRAgTcIZ 5oWMsWZ+jzpw== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.75,331,1589266800"; d="scan'208";a="323208484" Received: from black.fi.intel.com ([10.237.72.28]) by FMSMGA003.fm.intel.com with ESMTP; 09 Jul 2020 03:53:15 -0700 Received: by black.fi.intel.com (Postfix, from userid 1000) id 9D3E01EA; Thu, 9 Jul 2020 13:53:14 +0300 (EEST) From: "Kirill A. Shutemov" To: Andrew Morton Cc: linux-mm@kvack.org, linux-kernel@vger.kernel.org, "Kirill A. Shutemov" , Jann Horn , stable@vger.kernel.org, Yang Shi , Vlastimil Babka , Oleg Nesterov , Matthew Wilcox Subject: [PATCH] mm: Close race between munmap() and expand_upwards()/downwards() Date: Thu, 9 Jul 2020 13:53:09 +0300 Message-Id: <20200709105309.42495-1-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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. Signed-off-by: Kirill A. Shutemov Reported-by: Jann Horn Fixes: dd2283f2605e ("mm: mmap: zap pages with read mmap_sem in munmap") Cc: # 4.20 Cc: Yang Shi Cc: Vlastimil Babka Cc: Oleg Nesterov Cc: Matthew Wilcox --- mm/mmap.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/mm/mmap.c b/mm/mmap.c index 59a4682ebf3f..71df4b36b42a 100644 --- a/mm/mmap.c +++ b/mm/mmap.c @@ -2620,7 +2620,7 @@ static void unmap_region(struct mm_struct *mm, * 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_struct *mm, struct vm_area_struct *vma, /* Kill the cache */ vmacache_invalidate(mm); + + /* + * Do not downgrade mmap_sem if we are next to VM_GROWSDOWN or + * VM_GROWSUP VMA. Such VMAs can change their size under + * down_read(mmap_sem) 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, unsigned long start, size_t len, } /* 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) mmap_write_downgrade(mm); -- 2.26.2