Linux Serial subsystem development
 help / color / mirror / Atom feed
From: Karl Mehltretter <kmehltretter@gmail.com>
To: Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Jiri Slaby <jirislaby@kernel.org>
Cc: Karl Mehltretter <kmehltretter@gmail.com>,
	linux-serial@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: [PATCH v4 4/5] tty: clear cdev pointer after cdev_add() failure
Date: Fri, 31 Jul 2026 20:18:43 +0200	[thread overview]
Message-ID: <20260731181844.11330-5-kmehltretter@gmail.com> (raw)
In-Reply-To: <20260731181844.11330-1-kmehltretter@gmail.com>

tty_cdev_add() drops the cdev reference when cdev_add() fails, but
leaves driver->cdevs[index] pointing to freed memory.
tty_unregister_device() later passes that stale pointer to cdev_del(),
causing a use-after-free.

Clear the slot after dropping the reference.

Fixes: c1a752ba2d6b ("tty: don't leak cdev in tty_cdev_add()")
Assisted-by: Claude:claude-fable-5
Signed-off-by: Karl Mehltretter <kmehltretter@gmail.com>
---
 drivers/tty/tty_io.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/tty/tty_io.c b/drivers/tty/tty_io.c
index e742bf9d8631..4889076b975f 100644
--- a/drivers/tty/tty_io.c
+++ b/drivers/tty/tty_io.c
@@ -3167,8 +3167,10 @@ static int tty_cdev_add(struct tty_driver *driver, dev_t dev,
 	driver->cdevs[index]->ops = &tty_fops;
 	driver->cdevs[index]->owner = driver->owner;
 	err = cdev_add(driver->cdevs[index], dev, count);
-	if (err)
+	if (err) {
 		kobject_put(&driver->cdevs[index]->kobj);
+		driver->cdevs[index] = NULL;
+	}
 	return err;
 }
 
-- 
2.53.0

  parent reply	other threads:[~2026-07-31 18:19 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-07-31 18:18 [PATCH v4 0/5] serial: fix console lifetime bugs on failed bind and removal Karl Mehltretter
2026-07-31 18:18 ` [PATCH v4 1/5] serial: core: do fallible allocations before the console can be registered Karl Mehltretter
2026-07-31 18:18 ` [PATCH v4 2/5] serial: core: clear freed pointers on uart_register_driver() failure Karl Mehltretter
2026-07-31 18:18 ` [PATCH v4 3/5] tty: skip cdev_del() when no cdev is registered Karl Mehltretter
2026-07-31 18:18 ` Karl Mehltretter [this message]
2026-07-31 18:18 ` [PATCH v4 5/5] serial: imx: serialize imx_uart_ports[] lifetime Karl Mehltretter
2026-08-03 14:53   ` Greg Kroah-Hartman

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=20260731181844.11330-5-kmehltretter@gmail.com \
    --to=kmehltretter@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jirislaby@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-serial@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