All of lore.kernel.org
 help / color / mirror / Atom feed
From: Stanislaw Gruszka <sgruszka@redhat.com>
To: Alan Cox <alan@lxorguk.ukuu.org.uk>
Cc: Stanislav Kozina <skozina@redhat.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	linux-serial@vger.kernel.org
Subject: Re: [PATCH V2] [tty] Fix possible race in n_tty_read()
Date: Mon, 13 Aug 2012 17:26:55 +0200	[thread overview]
Message-ID: <20120813152654.GA5091@redhat.com> (raw)
In-Reply-To: <20120809111620.GA3516@redhat.com>

On Thu, Aug 09, 2012 at 01:16:20PM +0200, Stanislaw Gruszka wrote:
> On Wed, Aug 08, 2012 at 04:27:25PM +0100, Alan Cox wrote:
> > On Wed, 08 Aug 2012 16:28:47 +0200
> > Stanislav Kozina <skozina@redhat.com> wrote:
> > 
> > > Fix possible panic caused by unlocked access to tty->read_cnt in 
> > > while-loop condition in n_tty_read().
> > 
> > Should this also be removing the BUG_ON check you noted in the other
> > email was not valid now ?
> 
> You talk about 
> http://marc.info/?l=linux-serial&m=134318985920881&w=2
> 
> Is possible that we can call n_tty_read() after n_tty_close() ? How oterwise
> tty->read_buf could become NULL?
> 
> If I understand correctly Stanislav's patch solve below race condtion:
> 
> CPU0					CPU1
> n_tty_read:				reset_buffer_flags:
> 
> while (nr && tty->read_cnt) {
> 
>   					spin_lock_irqsave(&tty->read_lock, flags);
> 					tty->read_head = tty->read_tail = tty->read_cnt = 0;
>   					spin_lock_irqsave(&tty->read_lock, flags);
> 
>   spin_lock_irqsave(&tty->read_lock, flags);
>   
>   tty->read_cnt--;
> 
>   spin_lock_irqsave(&tty->read_lock, flags);
> 
>   /* Now tty->read_cnt is negative */
> 
> }
> 
> what itself could have varsious nasty consequences, i.e. ininite
> loop. Is also possible that negative tty->read_cnt would result in
> tty->read_buf == NULL ? If so, I'm not quite understand that.

I looked a bit more at this. Excluding memory corruption which could
zero tty struct, the only possibility to nullify tty->read_buf is
call to n_tty_close(). So NULL pointer dereference on n_tty_read,
in "while (nr && tty->read_cnt)" loop can only be caused by calling
n_tty_close(), while performing n_tty_read().

Stanislav patch solve that problem because we do not touch tty->read_buf
any longer once tty->read_cnt become 0, and because n_tty_close() clear
tty->read_cnt (by calling n_tty_flush_buffer() -> reset_buffer_flags()).

However looks like main problem persist, we should never do
n_tty_read() after/during n_tty_close() and before n_tty_open(). That
must be an issue in upper layer i.e. tty_io and tty_ldisc, which should
give guarantee about ->close(), ->read(), ->open() ordering. I'm going
to look at that more closely.

Stanislaw

  reply	other threads:[~2012-08-13 15:27 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-06-25 15:41 Patch for panic in n_tty_read() Stanislav Kozina
2012-06-26 14:21 ` Alan Cox
2012-07-20 12:18   ` Stanislav Kozina
2012-07-20 15:11     ` Alan Cox
2012-07-27 12:05       ` Stanislav Kozina
2012-07-27 12:50         ` Alan Cox
2012-07-30 11:58           ` Stanislav Kozina
2012-08-08  7:58       ` Stanislav Kozina
2012-08-08  9:00         ` Alan Cox
2012-08-08 12:09           ` Stanislav Kozina
2012-08-08 12:26             ` Alan Cox
2012-08-08 14:32               ` Stanislav Kozina
2012-08-08 14:28             ` [PATCH V2] [tty] Fix possible race " Stanislav Kozina
2012-08-08 15:27               ` Alan Cox
2012-08-09 11:16                 ` Stanislaw Gruszka
2012-08-13 15:26                   ` Stanislaw Gruszka [this message]
2012-08-14 11:15                     ` Stanislav Kozina
2012-08-09 11:24                 ` Stanislav Kozina
2012-08-09 12:35                   ` Alan Cox
2012-08-10 10:52                     ` Stanislav Kozina
2012-08-10 10:51                   ` [PATCH] Remove BUG_ON from n_tty_read() Stanislav Kozina
2012-08-10 12:29                     ` Stanislaw Gruszka
2012-08-10 14:53                       ` Stanislav Kozina
2012-08-10 14:38                     ` [PATCH V2] " Stanislav Kozina
2012-08-16  7:52                       ` Stanislav Kozina

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=20120813152654.GA5091@redhat.com \
    --to=sgruszka@redhat.com \
    --cc=alan@lxorguk.ukuu.org.uk \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=skozina@redhat.com \
    /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.