public inbox for linux-serial@vger.kernel.org
 help / color / mirror / Atom feed
From: "Ilpo Järvinen" <ilpo.jarvinen@linux.intel.com>
To: Max Staudt <max@enpas.org>
Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	 Jiri Slaby <jirislaby@kernel.org>,
	Johan Hovold <johan@kernel.org>,
	 linux-serial <linux-serial@vger.kernel.org>,
	 LKML <linux-kernel@vger.kernel.org>,
	stable@vger.kernel.org
Subject: Re: [PATCH v2 2/2] tty: Fix race against tty_open() in tty_register_device_attr()
Date: Mon, 2 Jun 2025 13:31:49 +0300 (EEST)	[thread overview]
Message-ID: <6068387e-7064-0c2b-700a-3817bea1045b@linux.intel.com> (raw)
In-Reply-To: <20250528132816.11433-2-max@enpas.org>

On Wed, 28 May 2025, Max Staudt wrote:

> Since the chardev is now created before the class device is registered,
> an attempt to tty_[k]open() the chardev between these two steps will
> lead to tty->dev being assigned NULL by alloc_tty_struct().
> 
> alloc_tty_struct() is called via tty_init_dev() when the tty is firstly
> opened, and is entered with tty_mutex held, so let's lock the critical
> section in tty_register_device_attr() with the same global mutex.
> This guarantees that tty->dev can be assigned a sane value.
> 
> Fixes: 6a7e6f78c235 ("tty: close race between device register and open")
> Signed-off-by: Max Staudt <max@enpas.org>
> Cc: <stable@vger.kernel.org>
> ---
>  drivers/tty/tty_io.c | 9 ++++++++-
>  1 file changed, 8 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c
> index e922b84524d2..94768509e2d2 100644
> --- a/drivers/tty/tty_io.c
> +++ b/drivers/tty/tty_io.c
> @@ -3258,6 +3258,8 @@ struct device *tty_register_device_attr(struct tty_driver *driver,
>  	else
>  		tty_line_name(driver, index, name);
>  
> +	mutex_lock(&tty_mutex);

Use guard() so you don't need to change the returns and rollback path.

> +
>  	if (!(driver->flags & TTY_DRIVER_DYNAMIC_ALLOC)) {
>  		/*
>  		 * Free any saved termios data so that the termios state is
> @@ -3271,7 +3273,7 @@ struct device *tty_register_device_attr(struct tty_driver *driver,
>  
>  		retval = tty_cdev_add(driver, devt, index, 1);
>  		if (retval)
> -			return ERR_PTR(retval);
> +			goto err_unlock;
>  
>  		cdev_added = true;
>  	}
> @@ -3294,6 +3296,8 @@ struct device *tty_register_device_attr(struct tty_driver *driver,
>  	if (retval)
>  		goto err_put;
>  
> +	mutex_unlock(&tty_mutex);
> +
>  	return dev;
>  
>  err_put:
> @@ -3309,6 +3313,9 @@ struct device *tty_register_device_attr(struct tty_driver *driver,
>  		driver->cdevs[index] = NULL;
>  	}
>  
> +err_unlock:
> +	mutex_unlock(&tty_mutex);
> +
>  	return ERR_PTR(retval);
>  }
>  EXPORT_SYMBOL_GPL(tty_register_device_attr);
> 

-- 
 i.


  reply	other threads:[~2025-06-02 10:31 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-05-28 13:28 [PATCH v2 1/2] tty: Register device *after* creating the cdev for a tty Max Staudt
2025-05-28 13:28 ` [PATCH v2 2/2] tty: Fix race against tty_open() in tty_register_device_attr() Max Staudt
2025-06-02 10:31   ` Ilpo Järvinen [this message]
2025-06-02 13:40     ` Max Staudt
2025-06-03 13:43       ` Jiri Slaby
2025-06-03  8:49   ` kernel test robot

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=6068387e-7064-0c2b-700a-3817bea1045b@linux.intel.com \
    --to=ilpo.jarvinen@linux.intel.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jirislaby@kernel.org \
    --cc=johan@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-serial@vger.kernel.org \
    --cc=max@enpas.org \
    --cc=stable@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox