Received: by 2002:a05:6a10:8c0a:0:0:0:0 with SMTP id go10csp3973003pxb; Tue, 2 Mar 2021 03:32:03 -0800 (PST) X-Google-Smtp-Source: ABdhPJyIDZBL4gXcEOekzcTT2QSaKbfMTF3Lk7Azu+ZCb8Us8sPXtTrcacIvk+4kq5kO0qa5I+77 X-Received: by 2002:a17:906:7e42:: with SMTP id z2mr20427540ejr.177.1614684723190; Tue, 02 Mar 2021 03:32:03 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1614684723; cv=none; d=google.com; s=arc-20160816; b=Xj8NGy5Zu75l09U0dWCrsiDOJ1E0kc9Wwyw+2NMX9Fw8ScSHWBMnzg0lYXU+pf9MDa CiyzkCCObDZGdl2Tk7MCwgtXXkeXQoCABHwW0b3J3XiHp5QKkqNrtPvGGuk6YnsEUTc4 kJaI4HcXZeptsfu30HmHESJ23Cb4ywCoPjk+zajukoZ14gUpMUXdSu4A/UA7pbzuzDLi rpLrAgZNN5eRG+eyngFtkNbvZv4gh8GqX1Mx2uqcKiVE/823j+oDk8LcxC0YouZ21SDf uSAM0Nk+auEwTtPoT8xxCNnF1YMXF3XQjXjl4wGjzJjaE4ud6/Lrx7B3jm2IlGUupfpu CVXQ== 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=MMssCDdEjsPURuLvg43paEqiJQhhFDB7z7snzPiDbAULjFlXlfjjgs9SbV9wMKs/Yh 9GftW2ce+W+OaTLmk3n9G3Z7PTdwi41qgOtYv4VL6hvxfZXBnNeQ5QFkrUvaNfy9VGsd iVmG+2zhA1lHfPGDOfjOItfydaIpftOhIbF+K5CrGecQz60ZWs9Inro+j8TjxPtR/iko EsxN9N80IkFwp5eTFf1YcDvyAXAdJLvkOvX5T3Zx8N3bzXIUxTE7vOomFZcGJQv812TF qFzCXyLmG3wNJXhIYD6V2r0Kmza6yK3VlsZZnVCDxoxpViIrYwkCNPzydrn5bM9TBV7I u9JA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=y9DMZYyj; 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 m7si4370254edd.486.2021.03.02.03.31.38; Tue, 02 Mar 2021 03:32:03 -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=y9DMZYyj; 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 S1380813AbhCBBzq (ORCPT + 99 others); Mon, 1 Mar 2021 20:55:46 -0500 Received: from mail.kernel.org ([198.145.29.99]:48598 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S241940AbhCAT35 (ORCPT ); Mon, 1 Mar 2021 14:29:57 -0500 Received: by mail.kernel.org (Postfix) with ESMTPSA id 0363F65224; Mon, 1 Mar 2021 17:23:12 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1614619393; bh=DNsNJhRVm6UC89UhRP1anUjHNomOEFNusblbO2r/SnI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=y9DMZYyjzSjbKb5G7f7Y1l/nxNdCxkaE2J1p4GuOKYhLfXBpNhEn9P5FRRUWKNeTp TUCze00v9bXu+QsowhCG9c3U6ruwI/FEO8pQQqJ+ft66db3y2flcbHM4Yg7eMoplV/ YAOZb+mylIAgvAV0EBmCCccKSzD+z7McrU9vcsf4= 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 5.10 439/663] Take mmap lock in cacheflush syscall Date: Mon, 1 Mar 2021 17:11:27 +0100 Message-Id: <20210301161203.616326263@linuxfoundation.org> X-Mailer: git-send-email 2.30.1 In-Reply-To: <20210301161141.760350206@linuxfoundation.org> References: <20210301161141.760350206@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