From: James Bottomley <James.Bottomley-d9PhHud1JfjCXq6kfMZ53/egYHeGw8Jk@public.gmane.org>
To: Jason Gunthorpe
<jgunthorpe-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
Cc: linux-security-module-u79uwXL29TY76Z2rM5mHXA@public.gmane.org,
tpmdd-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org,
open list <linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org>
Subject: Re: [PATCH RFC v2 5/5] tpm2: expose resource manager via a device link /dev/tpms<n>
Date: Fri, 13 Jan 2017 12:02:36 -0800 [thread overview]
Message-ID: <1484337756.2527.48.camel@HansenPartnership.com> (raw)
In-Reply-To: <20170113194730.GA32214-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
On Fri, 2017-01-13 at 12:47 -0700, Jason Gunthorpe wrote:
> On Fri, Jan 13, 2017 at 11:20:47AM -0800, James Bottomley wrote:
> > On Thu, 2017-01-12 at 11:39 -0700, Jason Gunthorpe wrote:
> > > On Thu, Jan 12, 2017 at 07:46:08PM +0200, Jarkko Sakkinen wrote:
> > >
> > > > struct tpm_chip {
> > > > - struct device dev;
> > > > - struct cdev cdev;
> > > > + struct device dev, devrm;
> > >
> > > Hum.. devrm adds a new kref but doesn't do anything with the
> > > release
> > > function, so that is going to use after free, ie here:
> > >
> > > > put_device(&chip->dev);
> > > > + put_device(&chip->devrm);
> > > > return ERR_PTR(rc);
> > >
> > > And other places.
> > >
> > > One solution is to get_device(chip->dev) after
> > > device_initialize(dev->rm) and add a devrm->dev.release function
> > > to
> > > do put_device(chip->dev)
> >
> > Actually, no, the devrm is a completely lifetime managed device as
> > part
> > of the chip structure. once you've done a device_del on it, it can
> > be
> > kfreed because it's no longer visible to anything else.
>
> No, that isn't enough. Anything else could have obtained a kref on
> devrm outside of the sphere the device_del manages.
>
> For instance, the cdev does exactly that, via this:
>
> > chip->cdev.kobj.parent = &chip->dev.kobj;
> > + chip->cdevrm.kobj.parent = &chip->devrm.kobj;
>
> In the worst case the kref the cdev grabs is not released until after
> tpm_chip_unregister() returns.
chip_unregister doesn't tear down either device. It's the final
release of the chip->dev that does that. chip->devrm is simply a
subordinate in that process, which is why it doesn't need to be
separately managed. We have to be careful to call cdev_del() before
device_del on devrm, but we do that, so we're guaranteed no visible
references by the time the chip->dev release is called.
> Having a kref that doesn't work is just asking for trouble, please
> make it work properly.
Actually, as shown above, these krefs are managed ... However, they're
not actually what holds the tpm module in place. The try_module_get on
open via the owner field does that. So, by the time tpm_exit() is
called we know there are no devrm references simply because we manage
the cdevrm entity as well.
Now there is a related problem that the owner is actually the *wrong*
module: it holds the tpm module in place not the actual driver module,
so I can happily attach tcsd to the TPM device then rmmod tpm_tis,
which causes some interesting issues. I can fix this, but it's not a
problem of the current patch.
James
------------------------------------------------------------------------------
Developer Access Program for Intel Xeon Phi Processors
Access to Intel Xeon Phi processor-based developer platforms.
With one year of Intel Parallel Studio XE.
Training and support from Colfax.
Order your platform today. http://sdm.link/xeonphi
WARNING: multiple messages have this Message-ID (diff)
From: James Bottomley <James.Bottomley@HansenPartnership.com>
To: Jason Gunthorpe <jgunthorpe@obsidianresearch.com>
Cc: Jarkko Sakkinen <jarkko.sakkinen@linux.intel.com>,
open list <linux-kernel@vger.kernel.org>,
linux-security-module@vger.kernel.org,
tpmdd-devel@lists.sourceforge.net
Subject: Re: [tpmdd-devel] [PATCH RFC v2 5/5] tpm2: expose resource manager via a device link /dev/tpms<n>
Date: Fri, 13 Jan 2017 12:02:36 -0800 [thread overview]
Message-ID: <1484337756.2527.48.camel@HansenPartnership.com> (raw)
In-Reply-To: <20170113194730.GA32214@obsidianresearch.com>
On Fri, 2017-01-13 at 12:47 -0700, Jason Gunthorpe wrote:
> On Fri, Jan 13, 2017 at 11:20:47AM -0800, James Bottomley wrote:
> > On Thu, 2017-01-12 at 11:39 -0700, Jason Gunthorpe wrote:
> > > On Thu, Jan 12, 2017 at 07:46:08PM +0200, Jarkko Sakkinen wrote:
> > >
> > > > struct tpm_chip {
> > > > - struct device dev;
> > > > - struct cdev cdev;
> > > > + struct device dev, devrm;
> > >
> > > Hum.. devrm adds a new kref but doesn't do anything with the
> > > release
> > > function, so that is going to use after free, ie here:
> > >
> > > > put_device(&chip->dev);
> > > > + put_device(&chip->devrm);
> > > > return ERR_PTR(rc);
> > >
> > > And other places.
> > >
> > > One solution is to get_device(chip->dev) after
> > > device_initialize(dev->rm) and add a devrm->dev.release function
> > > to
> > > do put_device(chip->dev)
> >
> > Actually, no, the devrm is a completely lifetime managed device as
> > part
> > of the chip structure. once you've done a device_del on it, it can
> > be
> > kfreed because it's no longer visible to anything else.
>
> No, that isn't enough. Anything else could have obtained a kref on
> devrm outside of the sphere the device_del manages.
>
> For instance, the cdev does exactly that, via this:
>
> > chip->cdev.kobj.parent = &chip->dev.kobj;
> > + chip->cdevrm.kobj.parent = &chip->devrm.kobj;
>
> In the worst case the kref the cdev grabs is not released until after
> tpm_chip_unregister() returns.
chip_unregister doesn't tear down either device. It's the final
release of the chip->dev that does that. chip->devrm is simply a
subordinate in that process, which is why it doesn't need to be
separately managed. We have to be careful to call cdev_del() before
device_del on devrm, but we do that, so we're guaranteed no visible
references by the time the chip->dev release is called.
> Having a kref that doesn't work is just asking for trouble, please
> make it work properly.
Actually, as shown above, these krefs are managed ... However, they're
not actually what holds the tpm module in place. The try_module_get on
open via the owner field does that. So, by the time tpm_exit() is
called we know there are no devrm references simply because we manage
the cdevrm entity as well.
Now there is a related problem that the owner is actually the *wrong*
module: it holds the tpm module in place not the actual driver module,
so I can happily attach tcsd to the TPM device then rmmod tpm_tis,
which causes some interesting issues. I can fix this, but it's not a
problem of the current patch.
James
next prev parent reply other threads:[~2017-01-13 20:02 UTC|newest]
Thread overview: 51+ messages / expand[flat|nested] mbox.gz Atom feed top
2017-01-12 17:46 [PATCH RFC v2 0/5] RFC: in-kernel resource manager Jarkko Sakkinen
2017-01-12 17:46 ` Jarkko Sakkinen
[not found] ` <20170112174612.9314-1-jarkko.sakkinen-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>
2017-01-12 17:46 ` [PATCH RFC v2 1/5] tpm: validate TPM 2.0 commands Jarkko Sakkinen
2017-01-12 17:46 ` Jarkko Sakkinen
2017-01-12 20:34 ` Jarkko Sakkinen
2017-01-12 17:46 ` [PATCH RFC v2 2/5] tpm: export tpm2_flush_context_cmd Jarkko Sakkinen
2017-01-12 17:46 ` Jarkko Sakkinen
2017-01-12 17:46 ` [PATCH RFC v2 3/5] tpm: infrastructure for TPM spaces Jarkko Sakkinen
2017-01-12 17:46 ` Jarkko Sakkinen
2017-01-12 20:38 ` [tpmdd-devel] " James Bottomley
2017-01-13 16:28 ` Jarkko Sakkinen
2017-01-14 17:53 ` Ken Goldman
2017-01-16 9:52 ` Jarkko Sakkinen
2017-01-16 9:52 ` [tpmdd-devel] " Jarkko Sakkinen
[not found] ` <20170112174612.9314-4-jarkko.sakkinen-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>
2017-01-12 18:38 ` James Bottomley
2017-01-12 18:38 ` [tpmdd-devel] " James Bottomley
[not found] ` <1484246310.2417.15.camel-d9PhHud1JfjCXq6kfMZ53/egYHeGw8Jk@public.gmane.org>
2017-01-12 20:31 ` Jarkko Sakkinen
2017-01-12 20:31 ` [tpmdd-devel] " Jarkko Sakkinen
2017-01-12 20:50 ` Jarkko Sakkinen
2017-01-12 20:50 ` Jarkko Sakkinen
2017-01-13 1:17 ` [tpmdd-devel] " James Bottomley
2017-01-13 16:31 ` Jarkko Sakkinen
2017-01-16 9:09 ` Jarkko Sakkinen
2017-01-16 14:24 ` James Bottomley
2017-01-16 14:48 ` Jarkko Sakkinen
2017-01-16 14:58 ` James Bottomley
2017-01-16 16:52 ` Jarkko Sakkinen
2017-01-12 17:46 ` [PATCH RFC v2 4/5] tpm: split out tpm-dev.c into tpm-dev.c and tpm-common-dev.c Jarkko Sakkinen
2017-01-12 17:46 ` Jarkko Sakkinen
2017-01-13 19:18 ` [tpmdd-devel] " James Bottomley
2017-01-12 17:46 ` [PATCH RFC v2 5/5] tpm2: expose resource manager via a device link /dev/tpms<n> Jarkko Sakkinen
2017-01-12 17:46 ` Jarkko Sakkinen
[not found] ` <20170112174612.9314-6-jarkko.sakkinen-VuQAYsv1563Yd54FQh9/CA@public.gmane.org>
2017-01-12 18:39 ` Jason Gunthorpe
2017-01-12 18:39 ` Jason Gunthorpe
2017-01-13 19:20 ` [tpmdd-devel] " James Bottomley
[not found] ` <1484335247.2527.28.camel-d9PhHud1JfjCXq6kfMZ53/egYHeGw8Jk@public.gmane.org>
2017-01-13 19:47 ` Jason Gunthorpe
2017-01-13 19:47 ` [tpmdd-devel] " Jason Gunthorpe
[not found] ` <20170113194730.GA32214-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2017-01-13 20:02 ` James Bottomley [this message]
2017-01-13 20:02 ` James Bottomley
2017-01-13 21:23 ` Jason Gunthorpe
2017-01-14 1:10 ` James Bottomley
2017-01-16 16:54 ` Jason Gunthorpe
2017-01-12 19:46 ` James Bottomley
2017-01-12 19:46 ` [tpmdd-devel] " James Bottomley
2017-01-12 20:56 ` Jarkko Sakkinen
2017-01-13 17:25 ` Jason Gunthorpe
[not found] ` <20170113172537.GA31125-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org>
2017-01-13 17:40 ` James Bottomley
2017-01-13 17:40 ` [tpmdd-devel] " James Bottomley
2017-01-13 18:01 ` Jason Gunthorpe
2017-01-13 18:11 ` James Bottomley
2017-01-16 9:45 ` Jarkko Sakkinen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1484337756.2527.48.camel@HansenPartnership.com \
--to=james.bottomley-d9phhud1jfjcxq6kfmz53/egyhegw8jk@public.gmane.org \
--cc=jgunthorpe-ePGOBjL8dl3ta4EC/59zMFaTQe2KTcn/@public.gmane.org \
--cc=linux-kernel-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=linux-security-module-u79uwXL29TY76Z2rM5mHXA@public.gmane.org \
--cc=tpmdd-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f@public.gmane.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.