Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp2299854ybl; Thu, 19 Dec 2019 11:12:56 -0800 (PST) X-Google-Smtp-Source: APXvYqxmidl6i/yevqqYT49IId0xiSDHIDVJuaIbWHbyjfOORxHcP/4tNh/ZtTXFH9di58sF4Zom X-Received: by 2002:aca:1e11:: with SMTP id m17mr2996415oic.167.1576782776521; Thu, 19 Dec 2019 11:12:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576782776; cv=none; d=google.com; s=arc-20160816; b=qB3TTfnkP5WrQoigncXDkCiJmVTi7GhhL8ETU7PmEIsnKhH8Pwua8NSalR851LOiTa ZzIrVGTAA8eOOu3ZZuSSIC/fFiwX4x09zAOyaq1tWrqicwkwbezQPuTerUh+kF9cJwRD iY5q2plFeGIgtqB4oAWFc2sdDxRfNMhj1WHxnzYdObeWmUGVlxGm+cjhgJl2jJqUUU4p fiIHTewIK5njOrDmbmXAPUGYlX4OHEhr3BKOhh2ya4tfec4BJHH2Z8eZ3r5yBSzwyEtK Nyc/L3GSt5P8lr982w3n+KrbOHrsFkgIDrqASYtaIdG80xiK+oPxxuhydzh5iGkUB+rv nNYw== 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=BIGyASbJUhOFhs9xzNneRRQlAoi8vTHkerEsk1SvXgBZ837qF32S+CQK4Km5qkZoIz SMK3AWaWAMSQ4wvkinXOij9adtuSvRoN98ugJsJrTJ4R+O1DGtAeiOXOQefqIFB8l971 V9icShIytD4Sy3tlh3RzXA2oZZLGvTzLjDRpyd8T4gpQlMgOU/ogrTLcWIDY45+x1uJl OCqqkSm6EccNYw5NKF7+6i0j3wmslQZux2+ZWzz/APRBfsvSzKU62NuEI5aMLHHRAkM5 QKHydA4k7DOBvNeU10S5kM++mMbBJ5XVH/wncEybsdgshtSuvkhjINdLj0XQyZC8p24J Qtrg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=Crz+OkuJ; 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 65si1577356oif.14.2019.12.19.11.12.43; Thu, 19 Dec 2019 11:12:56 -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=Crz+OkuJ; 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 S1728130AbfLSTMC (ORCPT + 99 others); Thu, 19 Dec 2019 14:12:02 -0500 Received: from mail.kernel.org ([198.145.29.99]:55100 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727384AbfLSSh1 (ORCPT ); Thu, 19 Dec 2019 13:37:27 -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 908C820716; Thu, 19 Dec 2019 18:37:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1576780647; bh=NEpL2CzNtWa8pl2T6jNiFSD8t5+OTKp74Cn9n98fZfI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Crz+OkuJno1y/BXpP1vu6s5b3lU5Q+0Y/+em1H8v5y+3cDkFNb+SyNlG1ZmEOKp/7 UYTfp8OUq80tWp5WqkRyrNqWPnKuAmLj5FShc9cdLjbih9aBgWmC0a1qSu2sWo0H+x /EU14kSuuw1XT0sUQASb6y8Qd7HFbyEwoKHaEqkM= 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.4 058/162] CIFS: Fix NULL-pointer dereference in smb2_push_mandatory_locks Date: Thu, 19 Dec 2019 19:32:46 +0100 Message-Id: <20191219183211.406082600@linuxfoundation.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20191219183150.477687052@linuxfoundation.org> References: <20191219183150.477687052@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;