* [PATCH] hso: fix refcnt leak in recent patch.
@ 2015-04-13 23:36 NeilBrown
[not found] ` <20150414093634.4fe24ba3-wvvUuzkyo1EYVZTmpyfIwg@public.gmane.org>
0 siblings, 1 reply; 5+ messages in thread
From: NeilBrown @ 2015-04-13 23:36 UTC (permalink / raw)
To: Olivier Sobrie, David S. Miller, Jan Dumon
Cc: linux-usb, netdev, linux-kernel, GTA04 owners
[-- Attachment #1: Type: text/plain, Size: 1107 bytes --]
Prior to
commit 29bd3bc1194c624ce863cab2a7da9bc1f0c3b47b
hso: fix crash when device disappears while serial port is open
hso_serial_open would always kref_get(&serial->parent->ref) before
returning zero.
Since that commit, it only calls kref_get when returning 0 if
serial->port.count was zero.
This results in calls to
kref_put(&serial->parent->ref, hso_serial_ref_free);
after hso_serial_ref_free has been called, which dereferences a freed
pointer.
This patch adds the missing kref_get().
Fixes: commit 29bd3bc1194c624ce863cab2a7da9bc1f0c3b47b
Cc: stable@vger.kernel.org (v4.0)
Cc: Olivier Sobrie <olivier@sobrie.be>
Signed-off-by: NeilBrown <neilb@suse.de>
diff --git a/drivers/net/usb/hso.c b/drivers/net/usb/hso.c
index 75befc1bd816..6848fc903340 100644
--- a/drivers/net/usb/hso.c
+++ b/drivers/net/usb/hso.c
@@ -1299,6 +1299,7 @@ static int hso_serial_open(struct tty_struct *tty, struct file *filp)
}
} else {
D1("Port was already open");
+ kref_get(&serial->parent->ref);
}
usb_autopm_put_interface(serial->parent->interface);
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 811 bytes --]
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] hso: fix refcnt leak in recent patch.
[not found] ` <20150414093634.4fe24ba3-wvvUuzkyo1EYVZTmpyfIwg@public.gmane.org>
@ 2015-04-14 1:03 ` NeilBrown
[not found] ` <20150414110303.66edcfee-wvvUuzkyo1EYVZTmpyfIwg@public.gmane.org>
2015-04-16 13:21 ` Olivier Sobrie
0 siblings, 2 replies; 5+ messages in thread
From: NeilBrown @ 2015-04-14 1:03 UTC (permalink / raw)
To: Olivier Sobrie, David S. Miller, Jan Dumon
Cc: linux-usb-u79uwXL29TY76Z2rM5mHXA, netdev-u79uwXL29TY76Z2rM5mHXA,
linux-kernel-u79uwXL29TY76Z2rM5mHXA, GTA04 owners
[-- Attachment #1: Type: text/plain, Size: 3379 bytes --]
On Tue, 14 Apr 2015 09:36:34 +1000 NeilBrown <neilb-l3A5Bk7waGM@public.gmane.org> wrote:
>
>
> Prior to
> commit 29bd3bc1194c624ce863cab2a7da9bc1f0c3b47b
> hso: fix crash when device disappears while serial port is open
>
> hso_serial_open would always kref_get(&serial->parent->ref) before
> returning zero.
> Since that commit, it only calls kref_get when returning 0 if
> serial->port.count was zero.
>
> This results in calls to
> kref_put(&serial->parent->ref, hso_serial_ref_free);
>
> after hso_serial_ref_free has been called, which dereferences a freed
> pointer.
>
> This patch adds the missing kref_get().
>
> Fixes: commit 29bd3bc1194c624ce863cab2a7da9bc1f0c3b47b
> Cc: stable-u79uwXL29TY76Z2rM5mHXA@public.gmane.org (v4.0)
> Cc: Olivier Sobrie <olivier-Ui3EtX6WB9GzQB+pC5nmwQ@public.gmane.org>
> Signed-off-by: NeilBrown <neilb-l3A5Bk7waGM@public.gmane.org>
>
> diff --git a/drivers/net/usb/hso.c b/drivers/net/usb/hso.c
> index 75befc1bd816..6848fc903340 100644
> --- a/drivers/net/usb/hso.c
> +++ b/drivers/net/usb/hso.c
> @@ -1299,6 +1299,7 @@ static int hso_serial_open(struct tty_struct *tty, struct file *filp)
> }
> } else {
> D1("Port was already open");
> + kref_get(&serial->parent->ref);
> }
>
> usb_autopm_put_interface(serial->parent->interface);
Sorry - that was wrong.
I'm getting crashes which strongly suggest the kref_put is being called extra
times, but I misunderstood the code and was hasty.
Maybe this instead?
Thanks,
NeilBrown
From: NeilBrown <neil-+NVA1uvv1dVBDLzU/O5InQ@public.gmane.org>
Date: Tue, 14 Apr 2015 09:33:03 +1000
Subject: [PATCH] hso: fix refcnt leak in recent patch.
Prior to
commit 29bd3bc1194c624ce863cab2a7da9bc1f0c3b47b
hso: fix crash when device disappears while serial port is open
a kref_get on serial->parent->ref would be taken on each open,
and it would be kref_put on each close.
Now the kref_put happens when the tty_struct is finally put (via
the 'cleanup') providing tty->driver_data has been set.
So the kref_get must be called exact once when tty->driver_data is
set.
With the current code, if the first open fails the kref_get() is never
called, but the kref_put() is called, leaving to a crash.
So change the kref_get call to happen exactly when ->driver_data is
changed from NULL to non-NULL.
Fixes: commit 29bd3bc1194c624ce863cab2a7da9bc1f0c3b47b
Cc: stable-u79uwXL29TY76Z2rM5mHXA@public.gmane.org (v4.0)
Cc: Olivier Sobrie <olivier-Ui3EtX6WB9GzQB+pC5nmwQ@public.gmane.org>
Signed-off-by: NeilBrown <neil-+NVA1uvv1dVBDLzU/O5InQ@public.gmane.org>
diff --git a/drivers/net/usb/hso.c b/drivers/net/usb/hso.c
index 75befc1bd816..17fd3820263a 100644
--- a/drivers/net/usb/hso.c
+++ b/drivers/net/usb/hso.c
@@ -1278,6 +1278,8 @@ static int hso_serial_open(struct tty_struct *tty, struct file *filp)
D1("Opening %d", serial->minor);
/* setup */
+ if (tty->driver_data == NULL)
+ kref_get(&serial->parent->ref);
tty->driver_data = serial;
tty_port_tty_set(&serial->port, tty);
@@ -1294,8 +1296,6 @@ static int hso_serial_open(struct tty_struct *tty, struct file *filp)
if (result) {
hso_stop_serial_device(serial->parent);
serial->port.count--;
- } else {
- kref_get(&serial->parent->ref);
}
} else {
D1("Port was already open");
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 811 bytes --]
^ permalink raw reply related [flat|nested] 5+ messages in thread
* Re: [PATCH] hso: fix refcnt leak in recent patch.
[not found] ` <20150414110303.66edcfee-wvvUuzkyo1EYVZTmpyfIwg@public.gmane.org>
@ 2015-04-14 6:50 ` Olivier Sobrie
2015-04-14 7:35 ` NeilBrown
0 siblings, 1 reply; 5+ messages in thread
From: Olivier Sobrie @ 2015-04-14 6:50 UTC (permalink / raw)
To: NeilBrown
Cc: David S. Miller, Jan Dumon, linux-usb-u79uwXL29TY76Z2rM5mHXA,
netdev-u79uwXL29TY76Z2rM5mHXA,
linux-kernel-u79uwXL29TY76Z2rM5mHXA, GTA04 owners
Hello Neil,
On Tue, Apr 14, 2015 at 11:03:03AM +1000, NeilBrown wrote:
> On Tue, 14 Apr 2015 09:36:34 +1000 NeilBrown <neilb-l3A5Bk7waGM@public.gmane.org> wrote:
>
> >
> >
> > Prior to
> > commit 29bd3bc1194c624ce863cab2a7da9bc1f0c3b47b
> > hso: fix crash when device disappears while serial port is open
> >
> > hso_serial_open would always kref_get(&serial->parent->ref) before
> > returning zero.
> > Since that commit, it only calls kref_get when returning 0 if
> > serial->port.count was zero.
> >
> > This results in calls to
> > kref_put(&serial->parent->ref, hso_serial_ref_free);
> >
> > after hso_serial_ref_free has been called, which dereferences a freed
> > pointer.
> >
> > This patch adds the missing kref_get().
> >
> > Fixes: commit 29bd3bc1194c624ce863cab2a7da9bc1f0c3b47b
> > Cc: stable-u79uwXL29TY76Z2rM5mHXA@public.gmane.org (v4.0)
> > Cc: Olivier Sobrie <olivier-Ui3EtX6WB9GzQB+pC5nmwQ@public.gmane.org>
> > Signed-off-by: NeilBrown <neilb-l3A5Bk7waGM@public.gmane.org>
> >
> > diff --git a/drivers/net/usb/hso.c b/drivers/net/usb/hso.c
> > index 75befc1bd816..6848fc903340 100644
> > --- a/drivers/net/usb/hso.c
> > +++ b/drivers/net/usb/hso.c
> > @@ -1299,6 +1299,7 @@ static int hso_serial_open(struct tty_struct *tty, struct file *filp)
> > }
> > } else {
> > D1("Port was already open");
> > + kref_get(&serial->parent->ref);
> > }
> >
> > usb_autopm_put_interface(serial->parent->interface);
>
>
> Sorry - that was wrong.
> I'm getting crashes which strongly suggest the kref_put is being called extra
> times, but I misunderstood the code and was hasty.
>
> Maybe this instead?
Indeed, if I undestand correctly the code in tty_io.c, cleanup() method
is also called when the open fails while kref_get is only done if the
open succeeds. Sorry for that mess.
I assume you get that crash when hso_start_serial_device() returns
an error?
At first sight, the patch below looks good to me.
I'll test it in the next days.
Thank you,
Olivier
>
> Thanks,
> NeilBrown
>
> From: NeilBrown <neil-+NVA1uvv1dVBDLzU/O5InQ@public.gmane.org>
> Date: Tue, 14 Apr 2015 09:33:03 +1000
> Subject: [PATCH] hso: fix refcnt leak in recent patch.
>
> Prior to
> commit 29bd3bc1194c624ce863cab2a7da9bc1f0c3b47b
> hso: fix crash when device disappears while serial port is open
>
> a kref_get on serial->parent->ref would be taken on each open,
> and it would be kref_put on each close.
>
> Now the kref_put happens when the tty_struct is finally put (via
> the 'cleanup') providing tty->driver_data has been set.
> So the kref_get must be called exact once when tty->driver_data is
> set.
>
> With the current code, if the first open fails the kref_get() is never
> called, but the kref_put() is called, leaving to a crash.
>
> So change the kref_get call to happen exactly when ->driver_data is
> changed from NULL to non-NULL.
>
> Fixes: commit 29bd3bc1194c624ce863cab2a7da9bc1f0c3b47b
> Cc: stable-u79uwXL29TY76Z2rM5mHXA@public.gmane.org (v4.0)
> Cc: Olivier Sobrie <olivier-Ui3EtX6WB9GzQB+pC5nmwQ@public.gmane.org>
> Signed-off-by: NeilBrown <neil-+NVA1uvv1dVBDLzU/O5InQ@public.gmane.org>
>
> diff --git a/drivers/net/usb/hso.c b/drivers/net/usb/hso.c
> index 75befc1bd816..17fd3820263a 100644
> --- a/drivers/net/usb/hso.c
> +++ b/drivers/net/usb/hso.c
> @@ -1278,6 +1278,8 @@ static int hso_serial_open(struct tty_struct *tty, struct file *filp)
> D1("Opening %d", serial->minor);
>
> /* setup */
> + if (tty->driver_data == NULL)
> + kref_get(&serial->parent->ref);
> tty->driver_data = serial;
> tty_port_tty_set(&serial->port, tty);
>
> @@ -1294,8 +1296,6 @@ static int hso_serial_open(struct tty_struct *tty, struct file *filp)
> if (result) {
> hso_stop_serial_device(serial->parent);
> serial->port.count--;
> - } else {
> - kref_get(&serial->parent->ref);
> }
> } else {
> D1("Port was already open");
--
To unsubscribe from this list: send the line "unsubscribe linux-usb" in
the body of a message to majordomo-u79uwXL29TY76Z2rM5mHXA@public.gmane.org
More majordomo info at http://vger.kernel.org/majordomo-info.html
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] hso: fix refcnt leak in recent patch.
2015-04-14 6:50 ` Olivier Sobrie
@ 2015-04-14 7:35 ` NeilBrown
0 siblings, 0 replies; 5+ messages in thread
From: NeilBrown @ 2015-04-14 7:35 UTC (permalink / raw)
To: Olivier Sobrie
Cc: David S. Miller, Jan Dumon, linux-usb, netdev, linux-kernel,
GTA04 owners
[-- Attachment #1: Type: text/plain, Size: 4452 bytes --]
On Tue, 14 Apr 2015 08:50:01 +0200 Olivier Sobrie <olivier@sobrie.be> wrote:
> Hello Neil,
>
> On Tue, Apr 14, 2015 at 11:03:03AM +1000, NeilBrown wrote:
> > On Tue, 14 Apr 2015 09:36:34 +1000 NeilBrown <neilb@suse.de> wrote:
> >
> > >
> > >
> > > Prior to
> > > commit 29bd3bc1194c624ce863cab2a7da9bc1f0c3b47b
> > > hso: fix crash when device disappears while serial port is open
> > >
> > > hso_serial_open would always kref_get(&serial->parent->ref) before
> > > returning zero.
> > > Since that commit, it only calls kref_get when returning 0 if
> > > serial->port.count was zero.
> > >
> > > This results in calls to
> > > kref_put(&serial->parent->ref, hso_serial_ref_free);
> > >
> > > after hso_serial_ref_free has been called, which dereferences a freed
> > > pointer.
> > >
> > > This patch adds the missing kref_get().
> > >
> > > Fixes: commit 29bd3bc1194c624ce863cab2a7da9bc1f0c3b47b
> > > Cc: stable@vger.kernel.org (v4.0)
> > > Cc: Olivier Sobrie <olivier@sobrie.be>
> > > Signed-off-by: NeilBrown <neilb@suse.de>
> > >
> > > diff --git a/drivers/net/usb/hso.c b/drivers/net/usb/hso.c
> > > index 75befc1bd816..6848fc903340 100644
> > > --- a/drivers/net/usb/hso.c
> > > +++ b/drivers/net/usb/hso.c
> > > @@ -1299,6 +1299,7 @@ static int hso_serial_open(struct tty_struct *tty, struct file *filp)
> > > }
> > > } else {
> > > D1("Port was already open");
> > > + kref_get(&serial->parent->ref);
> > > }
> > >
> > > usb_autopm_put_interface(serial->parent->interface);
> >
> >
> > Sorry - that was wrong.
> > I'm getting crashes which strongly suggest the kref_put is being called extra
> > times, but I misunderstood the code and was hasty.
> >
> > Maybe this instead?
>
> Indeed, if I undestand correctly the code in tty_io.c, cleanup() method
> is also called when the open fails while kref_get is only done if the
> open succeeds. Sorry for that mess.
> I assume you get that crash when hso_start_serial_device() returns
> an error?
Yes, that's correct.
>
> At first sight, the patch below looks good to me.
> I'll test it in the next days.
Thanks.
NeilBrown
>
> Thank you,
>
> Olivier
>
> >
> > Thanks,
> > NeilBrown
> >
> > From: NeilBrown <neil@brown.name>
> > Date: Tue, 14 Apr 2015 09:33:03 +1000
> > Subject: [PATCH] hso: fix refcnt leak in recent patch.
> >
> > Prior to
> > commit 29bd3bc1194c624ce863cab2a7da9bc1f0c3b47b
> > hso: fix crash when device disappears while serial port is open
> >
> > a kref_get on serial->parent->ref would be taken on each open,
> > and it would be kref_put on each close.
> >
> > Now the kref_put happens when the tty_struct is finally put (via
> > the 'cleanup') providing tty->driver_data has been set.
> > So the kref_get must be called exact once when tty->driver_data is
> > set.
> >
> > With the current code, if the first open fails the kref_get() is never
> > called, but the kref_put() is called, leaving to a crash.
> >
> > So change the kref_get call to happen exactly when ->driver_data is
> > changed from NULL to non-NULL.
> >
> > Fixes: commit 29bd3bc1194c624ce863cab2a7da9bc1f0c3b47b
> > Cc: stable@vger.kernel.org (v4.0)
> > Cc: Olivier Sobrie <olivier@sobrie.be>
> > Signed-off-by: NeilBrown <neil@brown.name>
> >
> > diff --git a/drivers/net/usb/hso.c b/drivers/net/usb/hso.c
> > index 75befc1bd816..17fd3820263a 100644
> > --- a/drivers/net/usb/hso.c
> > +++ b/drivers/net/usb/hso.c
> > @@ -1278,6 +1278,8 @@ static int hso_serial_open(struct tty_struct *tty, struct file *filp)
> > D1("Opening %d", serial->minor);
> >
> > /* setup */
> > + if (tty->driver_data == NULL)
> > + kref_get(&serial->parent->ref);
> > tty->driver_data = serial;
> > tty_port_tty_set(&serial->port, tty);
> >
> > @@ -1294,8 +1296,6 @@ static int hso_serial_open(struct tty_struct *tty, struct file *filp)
> > if (result) {
> > hso_stop_serial_device(serial->parent);
> > serial->port.count--;
> > - } else {
> > - kref_get(&serial->parent->ref);
> > }
> > } else {
> > D1("Port was already open");
>
>
>
> --
> To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at http://vger.kernel.org/majordomo-info.html
> Please read the FAQ at http://www.tux.org/lkml/
[-- Attachment #2: OpenPGP digital signature --]
[-- Type: application/pgp-signature, Size: 811 bytes --]
^ permalink raw reply [flat|nested] 5+ messages in thread
* Re: [PATCH] hso: fix refcnt leak in recent patch.
2015-04-14 1:03 ` NeilBrown
[not found] ` <20150414110303.66edcfee-wvvUuzkyo1EYVZTmpyfIwg@public.gmane.org>
@ 2015-04-16 13:21 ` Olivier Sobrie
1 sibling, 0 replies; 5+ messages in thread
From: Olivier Sobrie @ 2015-04-16 13:21 UTC (permalink / raw)
To: NeilBrown
Cc: David S. Miller, Jan Dumon, linux-usb, netdev, linux-kernel,
GTA04 owners
On Tue, Apr 14, 2015 at 11:03:03AM +1000, NeilBrown wrote:
> On Tue, 14 Apr 2015 09:36:34 +1000 NeilBrown <neilb@suse.de> wrote:
>
> >
> >
> > Prior to
> > commit 29bd3bc1194c624ce863cab2a7da9bc1f0c3b47b
> > hso: fix crash when device disappears while serial port is open
> >
> > hso_serial_open would always kref_get(&serial->parent->ref) before
> > returning zero.
> > Since that commit, it only calls kref_get when returning 0 if
> > serial->port.count was zero.
> >
> > This results in calls to
> > kref_put(&serial->parent->ref, hso_serial_ref_free);
> >
> > after hso_serial_ref_free has been called, which dereferences a freed
> > pointer.
> >
> > This patch adds the missing kref_get().
> >
> > Fixes: commit 29bd3bc1194c624ce863cab2a7da9bc1f0c3b47b
> > Cc: stable@vger.kernel.org (v4.0)
> > Cc: Olivier Sobrie <olivier@sobrie.be>
> > Signed-off-by: NeilBrown <neilb@suse.de>
> >
> > diff --git a/drivers/net/usb/hso.c b/drivers/net/usb/hso.c
> > index 75befc1bd816..6848fc903340 100644
> > --- a/drivers/net/usb/hso.c
> > +++ b/drivers/net/usb/hso.c
> > @@ -1299,6 +1299,7 @@ static int hso_serial_open(struct tty_struct *tty, struct file *filp)
> > }
> > } else {
> > D1("Port was already open");
> > + kref_get(&serial->parent->ref);
> > }
> >
> > usb_autopm_put_interface(serial->parent->interface);
>
>
> Sorry - that was wrong.
> I'm getting crashes which strongly suggest the kref_put is being called extra
> times, but I misunderstood the code and was hasty.
>
> Maybe this instead?
I tested the patch and it looks fine :-)
Thank you,
Olivier
>
> Thanks,
> NeilBrown
>
> From: NeilBrown <neil@brown.name>
> Date: Tue, 14 Apr 2015 09:33:03 +1000
> Subject: [PATCH] hso: fix refcnt leak in recent patch.
>
> Prior to
> commit 29bd3bc1194c624ce863cab2a7da9bc1f0c3b47b
> hso: fix crash when device disappears while serial port is open
>
> a kref_get on serial->parent->ref would be taken on each open,
> and it would be kref_put on each close.
>
> Now the kref_put happens when the tty_struct is finally put (via
> the 'cleanup') providing tty->driver_data has been set.
> So the kref_get must be called exact once when tty->driver_data is
> set.
>
> With the current code, if the first open fails the kref_get() is never
> called, but the kref_put() is called, leaving to a crash.
>
> So change the kref_get call to happen exactly when ->driver_data is
> changed from NULL to non-NULL.
>
> Fixes: commit 29bd3bc1194c624ce863cab2a7da9bc1f0c3b47b
> Cc: stable@vger.kernel.org (v4.0)
> Cc: Olivier Sobrie <olivier@sobrie.be>
Tested-by: Olivier Sobrie <olivier@sobrie.be>
> Signed-off-by: NeilBrown <neil@brown.name>
>
> diff --git a/drivers/net/usb/hso.c b/drivers/net/usb/hso.c
> index 75befc1bd816..17fd3820263a 100644
> --- a/drivers/net/usb/hso.c
> +++ b/drivers/net/usb/hso.c
> @@ -1278,6 +1278,8 @@ static int hso_serial_open(struct tty_struct *tty, struct file *filp)
> D1("Opening %d", serial->minor);
>
> /* setup */
> + if (tty->driver_data == NULL)
> + kref_get(&serial->parent->ref);
> tty->driver_data = serial;
> tty_port_tty_set(&serial->port, tty);
>
> @@ -1294,8 +1296,6 @@ static int hso_serial_open(struct tty_struct *tty, struct file *filp)
> if (result) {
> hso_stop_serial_device(serial->parent);
> serial->port.count--;
> - } else {
> - kref_get(&serial->parent->ref);
> }
> } else {
> D1("Port was already open");
--
Olivier
^ permalink raw reply [flat|nested] 5+ messages in thread
end of thread, other threads:[~2015-04-16 13:21 UTC | newest]
Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-04-13 23:36 [PATCH] hso: fix refcnt leak in recent patch NeilBrown
[not found] ` <20150414093634.4fe24ba3-wvvUuzkyo1EYVZTmpyfIwg@public.gmane.org>
2015-04-14 1:03 ` NeilBrown
[not found] ` <20150414110303.66edcfee-wvvUuzkyo1EYVZTmpyfIwg@public.gmane.org>
2015-04-14 6:50 ` Olivier Sobrie
2015-04-14 7:35 ` NeilBrown
2015-04-16 13:21 ` Olivier Sobrie
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).