From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752162AbcHKFQz (ORCPT ); Thu, 11 Aug 2016 01:16:55 -0400 Received: from zeniv.linux.org.uk ([195.92.253.2]:48750 "EHLO ZenIV.linux.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751839AbcHKFQx (ORCPT ); Thu, 11 Aug 2016 01:16:53 -0400 Date: Thu, 11 Aug 2016 06:16:50 +0100 From: Al Viro To: Dan Williams Cc: linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-nvdimm@ml01.01.org Subject: Re: [PATCH] fs/char_dev: fix cdev_put() vs f_op->release() use-after-free Message-ID: <20160811051650.GS2356@ZenIV.linux.org.uk> References: <147089096242.9037.17705831938180576705.stgit@dwillia2-desk3.amr.corp.intel.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <147089096242.9037.17705831938180576705.stgit@dwillia2-desk3.amr.corp.intel.com> User-Agent: Mutt/1.6.1 (2016-04-27) Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Aug 10, 2016 at 09:49:22PM -0700, Dan Williams wrote: > 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(). NAK. You *can't* free a structure that contains kobj with currently positive refcount. Ever. If you embed a struct kobj into something, you must use the refcount of that kobj (or one of its ancestors) to control the lifetime of containing object. If your dax_dev_put() can trigger freeing of dax_dev despite the still-positive refcount of embedded cdev.kobj, it is fundamentally broken.