All of lore.kernel.org
 help / color / mirror / Atom feed
From: Johan Hovold <johan@kernel.org>
To: Jiri Slaby <jslaby@suse.cz>
Cc: Gen Zhang <blackgod016574@gmail.com>, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] tty_io: Fix a missing-check bug in drivers/tty/tty_io.c
Date: Wed, 22 May 2019 12:29:00 +0200	[thread overview]
Message-ID: <20190522102900.GC2200@localhost> (raw)
In-Reply-To: <3a3db304-9725-6a90-65ac-dff09ef31aae@suse.cz>

On Wed, May 22, 2019 at 10:15:56AM +0200, Jiri Slaby wrote:
> On 22. 05. 19, 10:06, Gen Zhang wrote:
> > On Wed, May 22, 2019 at 06:25:36AM +0200, Jiri Slaby wrote:
> >> On 22. 05. 19, 3:40, Gen Zhang wrote:
> >>> In alloc_tty_struct(), tty->dev is assigned by tty_get_device(). And it
> >>> calls class_find_device(). And class_find_device() may return NULL.
> >>> And tty->dev is dereferenced in the following codes. When 
> >>> tty_get_device() returns NULL, dereferencing this tty->dev null pointer
> >>> may cause the kernel go wrong. Thus we should check tty->dev.

Where do you see that the kernel is dereferencing tty->dev without
checking for NULL first? If you can find that, then that would indeed be
a bug that needs fixing.

> >>> Further, if tty_get_device() returns NULL, we should free tty and 
> >>> return NULL.
> >>>
> >>> Signed-off-by: Gen Zhang <blackgod016574@gmail.com>
> >>>
> >>> ---
> >>> diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c
> >>> index 033ac7e..1444b59 100644
> >>> --- a/drivers/tty/tty_io.c
> >>> +++ b/drivers/tty/tty_io.c
> >>> @@ -3008,6 +3008,10 @@ struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx)
> >>>  	tty->index = idx;
> >>>  	tty_line_name(driver, idx, tty->name);
> >>>  	tty->dev = tty_get_device(tty);
> >>> +	if (!tty->dev) {
> >>> +		kfree(tty);
> >>> +		return NULL;
> >>> +	}
> >>
> >> This is incorrect, you introduced an ldisc reference leak.
> > Thanks for your reply, Jiri!
> > And what do you mean by an ldisc reference leak? I did't get the reason
> > of introducing it.
> 
> Look at the top of alloc_tty_struct: there is tty_ldisc_init. If
> tty_get_device fails here, you have to call tty_ldisc_deinit. Better,
> you should add a failure-handling tail to this function and "goto" there.
> 
> >> And can this happen at all?
> > I think tty_get_device() may happen to return NULL. Because it calls 
> > class_find_device() and there's a chance class_find_device() returns
> > NULL.
> 
> Sure, but can class_find_device return NULL in this tty case here?

Yes, it can and will and that's fine, not all ttys have a struct device
(e.g. ptys). 

This patch is broken and should be dropped.

Johan

  reply	other threads:[~2019-05-22 10:29 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-22  1:40 [PATCH] tty_io: Fix a missing-check bug in drivers/tty/tty_io.c Gen Zhang
2019-05-22  4:25 ` Jiri Slaby
2019-05-22  8:06   ` Gen Zhang
2019-05-22  8:15     ` Jiri Slaby
2019-05-22 10:29       ` Johan Hovold [this message]
2019-05-22 10:32         ` Jiri Slaby
2019-05-22 11:13         ` Gen Zhang
2019-05-22 11:19           ` Johan Hovold
2019-05-22 11:24             ` Gen Zhang
2019-05-22 11:18       ` Gen Zhang

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=20190522102900.GC2200@localhost \
    --to=johan@kernel.org \
    --cc=blackgod016574@gmail.com \
    --cc=jslaby@suse.cz \
    --cc=linux-kernel@vger.kernel.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.