From patchwork Thu Aug 11 04:49:22 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dan Williams X-Patchwork-Id: 9274445 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 28CB56022E for ; Thu, 11 Aug 2016 04:54:53 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1943E284E0 for ; Thu, 11 Aug 2016 04:54:53 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0A926284E5; Thu, 11 Aug 2016 04:54:53 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.9 required=2.0 tests=BAYES_00,RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 070CD284E0 for ; Thu, 11 Aug 2016 04:54:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751932AbcHKEyu (ORCPT ); Thu, 11 Aug 2016 00:54:50 -0400 Received: from mga04.intel.com ([192.55.52.120]:15653 "EHLO mga04.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S932155AbcHKEyt (ORCPT ); Thu, 11 Aug 2016 00:54:49 -0400 Received: from fmsmga001.fm.intel.com ([10.253.24.23]) by fmsmga104.fm.intel.com with ESMTP; 10 Aug 2016 21:51:46 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.28,503,1464678000"; d="scan'208";a="1023467451" Received: from dwillia2-desk3.jf.intel.com (HELO dwillia2-desk3.amr.corp.intel.com) ([10.54.39.14]) by fmsmga001.fm.intel.com with ESMTP; 10 Aug 2016 21:51:45 -0700 Subject: [PATCH] fs/char_dev: fix cdev_put() vs f_op->release() use-after-free From: Dan Williams To: viro@zeniv.linux.org.uk Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-nvdimm@lists.01.org Date: Wed, 10 Aug 2016 21:49:22 -0700 Message-ID: <147089096242.9037.17705831938180576705.stgit@dwillia2-desk3.amr.corp.intel.com> User-Agent: StGit/0.17.1-9-g687f MIME-Version: 1.0 Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP drivers/dax/dax.c implements a character device that supports mmap(). While trying to convert it to use the cdev api a unit test started failing. The test effectively does the following to test that the driver revokes active mappings when the device is unregistered: fd = open("/dev/dax0.0", ...); mmap(..., fd, ...); ; exit(); ...results in this crash: dax dax0.0: dax_dev_vm_close dax dax0.0: dax_dev_release: ffff880338afd298 dax_dev_free: ffff880338afd298 general protection fault: 0000 [#1] SMP [..] Call Trace: [] cdev_put+0x20/0x30 [] __fput+0x1ab/0x1f0 [] ____fput+0xe/0x10 [] task_work_run+0x7e/0xa0 [] do_exit+0x302/0xc10 Where dax_dev_release() is the f_op->release() method, and is implemented to simply drop the final references on our driver objects: struct dax_dev *dax_dev = filp->private_data; struct device *dev = dax_dev->dev; dev_dbg(dax_dev->dev, "%s\n", __func__); put_device(dev); dax_dev_put(dax_dev); The dax_dev object embeds a 'struct cdev' which means f_op->release() may free cdev, so __fput() needs to drop the cdev reference before calling f_op->release(). Cc: Al Viro Signed-off-by: Dan Williams --- fs/file_table.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) -- To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/fs/file_table.c b/fs/file_table.c index ad17e05ebf95..8856b8008248 100644 --- a/fs/file_table.c +++ b/fs/file_table.c @@ -204,13 +204,13 @@ static void __fput(struct file *file) file->f_op->fasync(-1, file, 0); } ima_file_free(file); - if (file->f_op->release) - file->f_op->release(inode, file); - security_file_free(file); if (unlikely(S_ISCHR(inode->i_mode) && inode->i_cdev != NULL && !(file->f_mode & FMODE_PATH))) { cdev_put(inode->i_cdev); } + if (file->f_op->release) + file->f_op->release(inode, file); + security_file_free(file); fops_put(file->f_op); put_pid(file->f_owner.pid); if ((file->f_mode & (FMODE_READ | FMODE_WRITE)) == FMODE_READ)