Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp781385ybl; Wed, 11 Dec 2019 07:25:51 -0800 (PST) X-Google-Smtp-Source: APXvYqxcWZEggOe29aFnw29sWG31PG5tO0c6SRKblmYEjkutakURJUfbe77SYh8Y2MPsKBeQDc+f X-Received: by 2002:a9d:6b17:: with SMTP id g23mr2552676otp.265.1576077950951; Wed, 11 Dec 2019 07:25:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576077950; cv=none; d=google.com; s=arc-20160816; b=vMWsFTE9rFZ+PJOP2uef2sQhqTrlcexka/mqaMCG+s5uq4mm1sDfXfU46ozDCrLMp3 9sS/UEuuZsG/VNa56ZfbvNAAvRazqcfgJ3j974srtZaexd4/LJk27BS3FM1xWHW4siN4 yvPaXVWzZuWPkTj4vwPj9hjYA4eV9Kg3+bCqK0X5twjxGq4D8zKKvsNGI0vEJQQFeRsf f27uDhH3nvzLb5RXAB/DeKfevTMm0xRim4eerOQrOaskm4j49zOP9qu7aWkJYXwfh29g KGbThCn1NvdhfsykWUj4TXPp+VCiiZA7NNd0chkDHIi5qHKN7s1xUSCxMwrZ8GwJAPUY NJGA== 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=eYgUnvt8qe846aRdp36uar6nuSpgrZ4+M54NJUelwvo=; b=Rzm65DeKuHkaUBre+GsnU1clgkfPjcEqrGL59lcMuYCyQ9lgOhCEWJE30UlZ9zGns4 wMQ0iVSqnqhBice7lKb/cbmN7GFD1/L/A78iUQh6KcxRqBekUdO2y3PpYZx0R2oxuV4h 7n7iSm8PddHE+NAKj9vArAWc9NmPuKxhzBFjvly9Wi9oxl6TEW1en6XoKvTCY440MqE8 wko4h/FLq3JU/SGp7euiHeeph1owrp8sKGKhArrg4Vho0ywpnI5nOpOj0wOgM4Vj+GSl dLaQJvbSefrBM0HgUATrM+vhwE5mRJ9c0Km4DrYp8n5iDfzHjqaNh4UaNJrVnQSDpskD fUlA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Ond6MBko; 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 q126si1332077oia.8.2019.12.11.07.25.38; Wed, 11 Dec 2019 07:25:50 -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=Ond6MBko; 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 S1732959AbfLKPZK (ORCPT + 99 others); Wed, 11 Dec 2019 10:25:10 -0500 Received: from mail.kernel.org ([198.145.29.99]:56818 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1732951AbfLKPZH (ORCPT ); Wed, 11 Dec 2019 10:25:07 -0500 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 4F3C92077B; Wed, 11 Dec 2019 15:25:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1576077906; bh=NEpL2CzNtWa8pl2T6jNiFSD8t5+OTKp74Cn9n98fZfI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Ond6MBkoAiZId0d6qkQk2dkkS3ln47yKmG6O1IivJ8RQLDY45tkMICtszLOAZrvKo 7l94KQNrRHuyO9v56ib4epCmYS3UKKRxQRptWF1Sm+JoqmveidUkwhFjEMbeiYwt13 Qns1A1/0X4KJ/dfpjsvyazzBDwzpOjjS9EuStJw8= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Pavel Shilovsky , Aurelien Aptel , Steve French Subject: [PATCH 4.19 216/243] CIFS: Fix NULL-pointer dereference in smb2_push_mandatory_locks Date: Wed, 11 Dec 2019 16:06:18 +0100 Message-Id: <20191211150353.926799856@linuxfoundation.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20191211150339.185439726@linuxfoundation.org> References: <20191211150339.185439726@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: Pavel Shilovsky commit 6f582b273ec23332074d970a7fb25bef835df71f upstream. Currently when the client creates a cifsFileInfo structure for a newly opened file, it allocates a list of byte-range locks with a pointer to the new cfile and attaches this list to the inode's lock list. The latter happens before initializing all other fields, e.g. cfile->tlink. Thus a partially initialized cifsFileInfo structure becomes available to other threads that walk through the inode's lock list. One example of such a thread may be an oplock break worker thread that tries to push all cached byte-range locks. This causes NULL-pointer dereference in smb2_push_mandatory_locks() when accessing cfile->tlink: [598428.945633] BUG: kernel NULL pointer dereference, address: 0000000000000038 ... [598428.945749] Workqueue: cifsoplockd cifs_oplock_break [cifs] [598428.945793] RIP: 0010:smb2_push_mandatory_locks+0xd6/0x5a0 [cifs] ... [598428.945834] Call Trace: [598428.945870] ? cifs_revalidate_mapping+0x45/0x90 [cifs] [598428.945901] cifs_oplock_break+0x13d/0x450 [cifs] [598428.945909] process_one_work+0x1db/0x380 [598428.945914] worker_thread+0x4d/0x400 [598428.945921] kthread+0x104/0x140 [598428.945925] ? process_one_work+0x380/0x380 [598428.945931] ? kthread_park+0x80/0x80 [598428.945937] ret_from_fork+0x35/0x40 Fix this by reordering initialization steps of the cifsFileInfo structure: initialize all the fields first and then add the new byte-range lock list to the inode's lock list. Cc: Stable Signed-off-by: Pavel Shilovsky Reviewed-by: Aurelien Aptel Signed-off-by: Steve French Signed-off-by: Greg Kroah-Hartman --- fs/cifs/file.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) --- a/fs/cifs/file.c +++ b/fs/cifs/file.c @@ -312,9 +312,6 @@ cifs_new_fileinfo(struct cifs_fid *fid, INIT_LIST_HEAD(&fdlocks->locks); fdlocks->cfile = cfile; cfile->llist = fdlocks; - cifs_down_write(&cinode->lock_sem); - list_add(&fdlocks->llist, &cinode->llist); - up_write(&cinode->lock_sem); cfile->count = 1; cfile->pid = current->tgid; @@ -338,6 +335,10 @@ cifs_new_fileinfo(struct cifs_fid *fid, oplock = 0; } + cifs_down_write(&cinode->lock_sem); + list_add(&fdlocks->llist, &cinode->llist); + up_write(&cinode->lock_sem); + spin_lock(&tcon->open_file_lock); if (fid->pending_open->oplock != CIFS_OPLOCK_NO_CHANGE && oplock) oplock = fid->pending_open->oplock;