Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp6264813imu; Mon, 21 Jan 2019 06:04:59 -0800 (PST) X-Google-Smtp-Source: ALg8bN5/JarmmVM8uSf1GpV+7qO0c/ozX8bEQ+9Gyvt5RXFx12E/P5TTQzHl7nR8khk9BenilgTz X-Received: by 2002:a17:902:714c:: with SMTP id u12mr29982570plm.234.1548079499292; Mon, 21 Jan 2019 06:04:59 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1548079499; cv=none; d=google.com; s=arc-20160816; b=AtHdwFOKc4ekpJz0hhSmll7vi1m17n1ubfZHTH4SIjnVxHU/LWiG6lqsGw2ageOnMO 1gRd+nCEyaGa8FdDxcrTtWvHiiU89Yj032g9fws/GkC0WJHNXVQkPe9A3dimIbviK1Cc Fh4z7YHq8WxAR4etba7tC0FjTbpo735DnCZgEl5Ll7af3RcCPULCPdzvHzHlT6mIjz5Z UsFgXkbG2Rgku5fgzL/yXNmdFnr1eOaBydfXdClY3/t/cB+0v8bkGBhtohtD1ZPaUGh3 t7pTReGu2MiJFu+arLsx2JClGqqed8Po25aduhh5dAOqm/DTDCVLWbSQi5Qpa9W3OazF YzXQ== 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=cLjVUA6cc55yrfuEVCN+baf1YUz6aciYR7ZjcNJEEuA=; b=upwR8dECYXuyuEEOJJHvzZx2QQOGvu6xnpCk2ZSdSrXRlw70ad1IFzR8noZthJa1od dvpKq311LjHqAWj2nyaOIv45i4wMc1sM45GtN+4qX+Zg0FX6/PedOfdDBwsVuA0l6SZZ d/vqlAFMNsSl67iJJ4QiuGVRHujNax1VPCstyot63vMBV0DsK9gtGPBJaMAT/Obaz6x6 J/biesu1KCxGJZMedpDlfWhGswVVJNEj9L2xkE+NvpKCFcIiRsFL5Q+GTuBiNcTrTNXi Pg69HvSeileUr6uLUSCvgvGUDuGwBmUuQAvkc9hmat3AJvTN8r3WkKBGJtNfQjGwIHXK u0cg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=iUfGye5g; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id b12si12395209pls.32.2019.01.21.06.04.43; Mon, 21 Jan 2019 06:04:59 -0800 (PST) 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=iUfGye5g; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1732615AbfAUOBS (ORCPT + 99 others); Mon, 21 Jan 2019 09:01:18 -0500 Received: from mail.kernel.org ([198.145.29.99]:48244 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732601AbfAUOBQ (ORCPT ); Mon, 21 Jan 2019 09:01:16 -0500 Received: from localhost (5356596B.cm-6-7b.dynamic.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 DBC6B2089F; Mon, 21 Jan 2019 14:01:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1548079275; bh=gXJy0W/V8m5YRot+d3sV1ikwj/2FxtJ/NSdPgG0e1q8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=iUfGye5gt0DVzWP04ueJc+gGssdAR90Q2dIIGSwCUDLTh/1kep9MeOHCMkV0vrqnq CM+7GYKFPgG7iXxyAVykcwb9tEFMUSS5ACzZeKOjFY7wnXiz9/KNAf3aQzLGN7bw1s ZuejFb8gLEv3FRsU3jHd7J/drjc1wYcXPYCubuHI= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Hans Verkuil , syzbot+be93025dd45dccd8923c@syzkaller.appspotmail.com, Hans Verkuil , Mauro Carvalho Chehab Subject: [PATCH 4.19 67/99] media: vb2: vb2_mmap: move lock up Date: Mon, 21 Jan 2019 14:48:59 +0100 Message-Id: <20190121134916.517213609@linuxfoundation.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20190121134913.924726465@linuxfoundation.org> References: <20190121134913.924726465@linuxfoundation.org> User-Agent: quilt/0.65 X-stable: review X-Patchwork-Hint: ignore 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 4.19-stable review patch. If anyone has any objections, please let me know. ------------------ From: Hans Verkuil commit cd26d1c4d1bc947b56ae404998ae2276df7b39b7 upstream. If a filehandle is dup()ped, then it is possible to close it from one fd and call mmap from the other. This creates a race condition in vb2_mmap where it is using queue data that __vb2_queue_free (called from close()) is in the process of releasing. By moving up the mutex_lock(mmap_lock) in vb2_mmap this race is avoided since __vb2_queue_free is called with the same mutex locked. So vb2_mmap now reads consistent buffer data. Signed-off-by: Hans Verkuil Reported-by: syzbot+be93025dd45dccd8923c@syzkaller.appspotmail.com Signed-off-by: Hans Verkuil Signed-off-by: Mauro Carvalho Chehab Signed-off-by: Greg Kroah-Hartman --- drivers/media/common/videobuf2/videobuf2-core.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) --- a/drivers/media/common/videobuf2/videobuf2-core.c +++ b/drivers/media/common/videobuf2/videobuf2-core.c @@ -1933,9 +1933,13 @@ int vb2_mmap(struct vb2_queue *q, struct return -EINVAL; } } + + mutex_lock(&q->mmap_lock); + if (vb2_fileio_is_active(q)) { dprintk(1, "mmap: file io in progress\n"); - return -EBUSY; + ret = -EBUSY; + goto unlock; } /* @@ -1943,7 +1947,7 @@ int vb2_mmap(struct vb2_queue *q, struct */ ret = __find_plane_by_offset(q, off, &buffer, &plane); if (ret) - return ret; + goto unlock; vb = q->bufs[buffer]; @@ -1959,8 +1963,9 @@ int vb2_mmap(struct vb2_queue *q, struct return -EINVAL; } - mutex_lock(&q->mmap_lock); ret = call_memop(vb, mmap, vb->planes[plane].mem_priv, vma); + +unlock: mutex_unlock(&q->mmap_lock); if (ret) return ret;