Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp3551000pxb; Mon, 1 Mar 2021 13:05:19 -0800 (PST) X-Google-Smtp-Source: ABdhPJy9vtHZViuaqLH8gGjTWfjr0G+AXYxka7Xu0GDaWPW2IcIt+8vV8W6pRDW5FLxxtDHUPYca X-Received: by 2002:a05:6402:3122:: with SMTP id dd2mr18010450edb.253.1614632719760; Mon, 01 Mar 2021 13:05:19 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614632719; cv=none; d=google.com; s=arc-20160816; b=euu/RwWO9+tkpTBVC0+XEqD8R5a8wVcCGZRiE4jKcsMPrQRnI20RI2uCnzcC5Mq5Ai gbmbwXUH/3OtHu0NMQkLZe0F0ahArBVzO3QVouQjefBye33TL3QWisBUXt0EIv4BDARQ XaRR24dQJK6tlBhYWKPAtF31EWo2PCppDHcF6jwVo/AgQf1U2hGTxpc8U1DtDLJPhH6w w+NLyszWtmmpbhJcAohMFIISQnSGXDD1qrbiOB5ZNQ7Qhzz3A4/velc+IeOJGqwnUahA qEw0i0HyZXAEMkmyAxTzeiX2Zr9Loa1fFK3y80ep3uiMr03GiaHByIqtJ8+hGBaDEmQP k3aw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=HtM/it6Am2ZqHX2vX4iy79aJyuw4+tDWLj1pNkMyuFE=; b=qPiKGktEMh6A2a0vnyLCKF91+saLBFP5ZyhT/pzZn3d3TN1VVFAckc4k4hoFAacnHU MKdB9hYG9FMZJmaRD9yW3V3PiLO8qx7dlCKWzdpcU7oVNbpkD5XwIYGgQw/0hMpFCjBk IXUg1kfr/vbQbyrSCm24Bxvr1XS/ffVlELVw2eNp/52VL1WJK6QeagvUJHd35tmspCZS /ZbgHbw9bccqUr7NnFLht5dn0T3wfK+GI9gIQQsPIFZaGe7W7jaH/3lxaDxdHwa8VyVo 6tDuW50ZVa11eRJ9OToWFkEmwhGYYahkG9/5+JeSWPpeSduRVCbnpEvQcY2MVqE2uco8 sA6w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=FQNpTuF2; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id a42si13534513edf.287.2021.03.01.13.04.56; Mon, 01 Mar 2021 13:05:19 -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=@linuxfoundation.org header.s=korg header.b=FQNpTuF2; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S242323AbhCAVCH (ORCPT + 99 others); Mon, 1 Mar 2021 16:02:07 -0500 Received: from mail.kernel.org ([198.145.29.99]:48350 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237464AbhCARLY (ORCPT ); Mon, 1 Mar 2021 12:11:24 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id E804664EDD; Mon, 1 Mar 2021 16:43:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1614616993; bh=DNsNJhRVm6UC89UhRP1anUjHNomOEFNusblbO2r/SnI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=FQNpTuF2k23oQn2JlYkp7zti8+26s1EegGmpWJg4VByUAIL08KhHOihXwZLzfupRN Dnts1/ky0XYjnZ1vjn7fwgz//OxlK3hKAYCal1H0kiRj2sX9/pTt7zHoBCFL0FyhyP PwFtO286O2XiDHgnowWkFwc1EBWVktdke9mNyulg= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Jann Horn , Ley Foon Tan , Sasha Levin Subject: [PATCH 4.19 166/247] Take mmap lock in cacheflush syscall Date: Mon, 1 Mar 2021 17:13:06 +0100 Message-Id: <20210301161039.789807476@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210301161031.684018251@linuxfoundation.org> References: <20210301161031.684018251@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Jann Horn [ Upstream commit c26958cb5a0d9053d1358258827638773f3d36ed ] We need to take the mmap lock around find_vma() and subsequent use of the VMA. Otherwise, we can race with concurrent operations like munmap(), which can lead to use-after-free accesses to freed VMAs. Fixes: 1000197d8013 ("nios2: System calls handling") Signed-off-by: Jann Horn Signed-off-by: Ley Foon Tan Signed-off-by: Sasha Levin --- arch/nios2/kernel/sys_nios2.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/arch/nios2/kernel/sys_nios2.c b/arch/nios2/kernel/sys_nios2.c index cd390ec4f88bf..b1ca856999521 100644 --- a/arch/nios2/kernel/sys_nios2.c +++ b/arch/nios2/kernel/sys_nios2.c @@ -22,6 +22,7 @@ asmlinkage int sys_cacheflush(unsigned long addr, unsigned long len, unsigned int op) { struct vm_area_struct *vma; + struct mm_struct *mm = current->mm; if (len == 0) return 0; @@ -34,16 +35,22 @@ asmlinkage int sys_cacheflush(unsigned long addr, unsigned long len, if (addr + len < addr) return -EFAULT; + if (mmap_read_lock_killable(mm)) + return -EINTR; + /* * Verify that the specified address region actually belongs * to this process. */ - vma = find_vma(current->mm, addr); - if (vma == NULL || addr < vma->vm_start || addr + len > vma->vm_end) + vma = find_vma(mm, addr); + if (vma == NULL || addr < vma->vm_start || addr + len > vma->vm_end) { + mmap_read_unlock(mm); return -EFAULT; + } flush_cache_range(vma, addr, addr + len); + mmap_read_unlock(mm); return 0; } -- 2.27.0