Re: [syzbot] [kernel?] KCSAN: data-race in __fput / __tty_hangup (4)

From: Al Viro
Date: Tue Apr 25 2023 - 12:04:01 EST


On Tue, Apr 25, 2023 at 11:47:20PM +0900, Tetsuo Handa wrote:
> @@ -743,7 +733,8 @@ void tty_vhangup_session(struct tty_struct *tty)
> */
> int tty_hung_up_p(struct file *filp)
> {
> - return (filp && filp->f_op == &hung_up_tty_fops);
> + /* Accept race with __tty_hangup(). */
> + return filp && data_race(((struct tty_file_private *) filp->private_data)->hung);
> }
> EXPORT_SYMBOL(tty_hung_up_p);

Umm... Are you sure we never call that for non-TTY files? Seeing that
it's exported and all such... For internal uses (tty_read(), etc.) the
check for file being NULL is pointless; for general-purpose primitive
we probably want to check ->f_op as well...

> @@ -2159,11 +2154,6 @@ static int tty_open(struct inode *inode, struct file *filp)
> return retval;
>
> schedule();
> - /*
> - * Need to reset f_op in case a hangup happened.
> - */
> - if (tty_hung_up_p(filp))
> - filp->f_op = &tty_fops;

That needs a comment in commit message - hangup indication doesn't
need to be reset here, since tty_release() has freed the entire thing
that used to hang off the ->private_data.

> I don't know what changes are required for tty_open() and tty_show_fdinfo().
> I assume that making no change for tty_show_fdinfo() is harmless.
> But how does "hung_up_tty_fops does not call tty_open()" affects here?

It doesn't - file->f_op->open() is only called once, and only on fresh
struct file.