tty: pty: preserve open slaves after rejected locked open
Subsystems (2)
PATCH 1/1 tty: pty: preserve open slaves after rejected locked open
Opening a locked PTY slave fails with EIO, but the failure path sets TTY_IO_ERROR on the shared slave tty. Previously opened slave files then fail I/O even though their master is still open.
tty_open() releases a file after the slave open callback fails. Merely skipping TTY_IO_ERROR on the rejected open is insufficient: pty_close() can mistake the failed file for the last slave and close the master. Counting tty references is also insufficient because a real last close can race the failed open and its release.
Track which slave files opened successfully. Ignore failed files during close, and mark the master peer closed only when the last successful slave file closes. A selftest checks locked opens with zero, one, and two existing slave files, plus ordinary last-slave close behavior.
---
The pre-fix EIO transition was reproduced on an Android 5.10.240 device. The new selftest cross-compiles for arm64 with -Werror. I have not booted a kernel with this patch yet.
drivers/tty/pty.c | 32 ++- drivers/tty/tty_io.c | 1 + include/linux/tty.h | 1 + .../testing/selftests/filesystems/.gitignore | 1 + tools/testing/selftests/filesystems/Makefile | 2 +- .../selftests/filesystems/pty_locked_open.c | 187 ++++++++++++++++++ 6 files changed, 220 insertions(+), 4 deletions(-) create mode 100644 tools/testing/selftests/filesystems/pty_locked_open.c
drivers/tty/pty.c +29 -3
@@ -46,12 +46,29 @@ static DEFINE_MUTEX(devpts_mutex);static void pty_close (struct tty_struct *tty,struct file *filp) { +struct tty_file_private *priv = filp->private_data; +struct tty_file_private *other; +bool another_open = false; +if (tty->driver->subtype ==PTY_TYPE_MASTER )WARN_ON (tty->count >1 );else { -if (tty_io_error (tty)) +/* tty_release() also calls close after an unsuccessful open. */ +if (!priv->pty_opened) +return ; +/* tty->count includes opens that have not succeeded yet. */ +spin_lock (&tty->files_lock); + priv->pty_opened = false; +list_for_each_entry (other, &tty->tty_files, list) { +if (other->pty_opened) { + another_open = true; +break ; + } + } +spin_unlock (&tty->files_lock); +if (another_open)return ; -if (tty->count >2 ) +if (tty_io_error (tty))return ; }set_bit (TTY_IO_ERROR , &tty->flags);
@@ -223,14 +240,23 @@ static int pty_open(struct tty_struct *tty, struct file *filp)if (test_bit (TTY_OTHER_CLOSED , &tty->flags)) goto out; +/* A rejected open must not disrupt already-open slave descriptors. */ if (test_bit (TTY_PTY_LOCK , &tty->link->flags)) - goto out; +return -EIO ;if (tty->driver->subtype ==PTY_TYPE_SLAVE && tty->link->count !=1 ) goto out;clear_bit (TTY_IO_ERROR , &tty->flags);clear_bit (TTY_OTHER_CLOSED , &tty->link->flags);set_bit (TTY_THROTTLED , &tty->flags); +/* Record a successful open before another slave can close. */ +if (tty->driver->subtype ==PTY_TYPE_SLAVE ) { +struct tty_file_private *priv = filp->private_data; + +spin_lock (&tty->files_lock); + priv->pty_opened = true; +spin_unlock (&tty->files_lock); + }return 0 ; out:
drivers/tty/tty_io.c +1 -0
@@ -186,6 +186,7 @@ int tty_alloc_file(struct file *file) priv =kmalloc_obj (*priv);if (!priv)return -ENOMEM ; + priv->pty_opened = false; file->private_data = priv;
include/linux/tty.h +1 -0
@@ -247,6 +247,7 @@ struct tty_file_private {struct tty_struct *tty;struct file *file;struct list_head list; +bool pty_opened; };/**
tools/testing/selftests/filesystems/.gitignore +1 -0
@@ -6,4 +6,5 @@ file_stressor anon_inode_test kernfs_test idmapped_tmpfile +pty_locked_open ustat_test
tools/testing/selftests/filesystems/Makefile +1 -1
@@ -2,7 +2,7 @@ CFLAGS += $(KHDR_INCLUDES) TEST_GEN_PROGS := devpts_pts file_stressor anon_inode_test kernfs_test fclog ustat_test -TEST_GEN_PROGS += idmapped_tmpfile +TEST_GEN_PROGS += idmapped_tmpfile pty_locked_open TEST_GEN_PROGS_EXTENDED := dnotify_test include ../lib.mk
tools/testing/selftests/filesystems/pty_locked_open.c new file +187 -0
187 lines not on this page · raw message
Signature · 1 line
2.50.1
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider: - [High] tty: NULL pointer dereference in pty_open and pty_close - [Medium] tty: logic error tearing down…
Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] tty: NULL pointer dereference in pty_open and pty_close
- [Medium] tty: logic error tearing down active kernel PTY clients
--
commit f8d6871959bd3ab3965cd6aaf18087ecb7ef411f
Author: Akira Patafio <[email protected]>
tty: pty: preserve open slaves after rejected locked open
This commit tracks which PTY slave files were successfully opened to avoid spurious IO errors or premature master closures during locked slave opens. A selftest is also added to verify this behavior.
drivers/tty/pty.c static DEFINE_MUTEX(devpts_mutex); · line 49
Quoted 9 lines
drivers/tty/pty.c +1
-0@@ -46,12 +46,29 @@ static DEFINE_MUTEX(devpts_mutex);static void pty_close (struct tty_struct *tty,struct file *filp) { +struct tty_file_private *priv = filp->private_data;
[Severity: High]
Can this unconditionally dereference a NULL pointer? In-kernel TTY clients such as speakup or serdev can manually invoke the driver's close callback using tty->ops->close(tty, NULL).
If an in-kernel client opens a PTY, calling pty_close() with filp == NULL will cause a kernel panic here.
Quoted 9 lines
drivers/tty/pty.c +5
-1+struct tty_file_private *other; +bool another_open = false; +if (tty->driver->subtype ==PTY_TYPE_MASTER )WARN_ON (tty->count >1 );else { -if (tty_io_error (tty)) +/* tty_release() also calls close after an unsuccessful open. */ +if (!priv->pty_opened)
drivers/tty/pty.c static DEFINE_MUTEX(devpts_mutex); · line 67
Quoted 10 lines
drivers/tty/pty.c +10
-0+return ; +/* tty->count includes opens that have not succeeded yet. */ +spin_lock (&tty->files_lock); + priv->pty_opened = false; +list_for_each_entry (other, &tty->tty_files, list) { +if (other->pty_opened) { + another_open = true; +break ; + } + }
[Severity: Medium]
Will this loop correctly account for in-kernel PTY clients?
If a client like speakup has the PTY open, its open is not backed by a struct file and is absent from tty->tty_files.
drivers/tty/pty.c static DEFINE_MUTEX(devpts_mutex); · line 70
Quoted 3 lines
drivers/tty/pty.c +2
-0+spin_unlock (&tty->files_lock); +if (another_open)return ;
If an in-kernel client is the only remaining open, another_open will evaluate to false here. Could this prematurely tear down the master connection despite the kernel client still actively using the slave?
[ ... ]
Quoted 7 lines
drivers/tty/pty.c +1
-1@@ -223,14 +240,23 @@ static int pty_open(struct tty_struct *tty, struct file *filp)if (test_bit (TTY_OTHER_CLOSED , &tty->flags)) goto out; +/* A rejected open must not disrupt already-open slave descriptors. */ if (test_bit (TTY_PTY_LOCK , &tty->link->flags)) - goto out;
drivers/tty/pty.c static int pty_open(struct tty_struct *tty, struct file *filp) · line 254
Quoted 10 lines
drivers/tty/pty.c +4
-0+return -EIO ;if (tty->driver->subtype ==PTY_TYPE_SLAVE && tty->link->count !=1 ) goto out;clear_bit (TTY_IO_ERROR , &tty->flags);clear_bit (TTY_OTHER_CLOSED , &tty->link->flags);set_bit (TTY_THROTTLED , &tty->flags); +/* Record a successful open before another slave can close. */ +if (tty->driver->subtype ==PTY_TYPE_SLAVE ) { +struct tty_file_private *priv = filp->private_data;
[Severity: High]
Can filp be NULL here?
Similar to pty_close(), in-kernel clients can call tty->ops->open(tty, NULL). This could cause a kernel panic due to a NULL pointer dereference.
Quoted 6 lines
drivers/tty/pty.c +5
-0+ +spin_lock (&tty->files_lock); + priv->pty_opened = true; +spin_unlock (&tty->files_lock); + }return 0 ;
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Greg KH
maintainerQuoted 15 lines · Akira Patafio
Opening a locked PTY slave fails with EIO, but the failure path sets
TTY_IO_ERRORon the shared slave tty. Previously opened slave files then fail I/O even though their master is still open.
tty_open()releases a file after the slave open callback fails. Merely skippingTTY_IO_ERRORon the rejected open is insufficient:pty_close()can mistake the failed file for the last slave and close the master. Counting tty references is also insufficient because a real last close can race the failed open and its release.Track which slave files opened successfully. Ignore failed files during close, and mark the master peer closed only when the last successful slave file closes. A selftest checks locked opens with zero, one, and two existing slave files, plus ordinary last-slave close behavior.
Quoted 6 lines
Fixes 699390354da6 ("pty: Ignore slave pty close() if never successfully opened")Signed-off-by Akira Patafio <[email protected]>---
The pre-fix EIO transition was reproduced on an Android 5.10.240 device. The new selftest cross-compiles for arm64 with -Werror. I have not booted a kernel with this patch yet.
If you haven't even tested or tried it yet, why should we?
{sigh}
Please test your work before sending it to us.
thanks,
greg k-h
Greg KH
maintainerQuoted 10 lines · Akira Patafio
Opening a locked PTY slave fails with EIO, but the failure path sets
TTY_IO_ERRORon the shared slave tty. Previously opened slave files then fail I/O even though their master is still open.
tty_open()releases a file after the slave open callback fails. Merely skippingTTY_IO_ERRORon the rejected open is insufficient:pty_close()can mistake the failed file for the last slave and close the master. Counting tty references is also insufficient because a real last close can race the failed open and its release.
Quoted 7 lines
Track which slave files opened successfully. Ignore failed files during close, and mark the master peer closed only when the last successful slave file closes. A selftest checks locked opens with zero, one, and two existing slave files, plus ordinary last-slave close behavior.
Fixes 699390354da6 ("pty: Ignore slave pty close() if never successfully opened")Signed-off-by Akira Patafio <[email protected]>
Also, did you forget an Assisted-by: tag here?
thanks,
greg k-h
PATCH v2 1/1 tty: pty: preserve open slaves after rejected locked open
In reply to Akira PatafioOpening a locked PTY slave fails with EIO, but the failure path sets TTY_IO_ERROR on the shared slave tty. Previously opened slave files then fail I/O even though their master is still open.
tty_open() releases a file after the slave open callback fails. Merely skipping TTY_IO_ERROR on the rejected open is insufficient: pty_close() can mistake the failed file for the last slave and close the master. Counting tty references is also insufficient because a real last close can race the failed open and its release.
Track which slave files opened successfully. Ignore failed files during close, and mark the master peer closed only when the last successful slave file closes. In-kernel callers may pass NULL instead of a file; retain the count-based close behavior for those callers. A selftest checks locked opens with zero, one, and two existing slave files, plus ordinary last-slave close behavior.
---
Changes in v2:
- Preserve the existing NULL-file callback behavior for in-kernel TTY users.
- Add the Assisted-by tag requested in review.
Tests:
- The selftest failed on an unpatched Linux 6.12.69 kernel at the pre-existing slave write with errno EIO (TAP fail:1).
- Built the patched 7.3.0-rc5 x86_64 kernel with legacy PTYs and Speakup enabled, without compiler warnings. Booted it in QEMU/HVF and ran the selftest in the guest: TAP pass:1 fail:0, exit status 0.
drivers/tty/pty.c | 34 +++- drivers/tty/tty_io.c | 1 + include/linux/tty.h | 1 + .../testing/selftests/filesystems/.gitignore | 1 + tools/testing/selftests/filesystems/Makefile | 2 +- .../selftests/filesystems/pty_locked_open.c | 187 ++++++++++++++++++ 6 files changed, 222 insertions(+), 4 deletions(-) create mode 100644 tools/testing/selftests/filesystems/pty_locked_open.c
drivers/tty/pty.c +31 -3
@@ -46,12 +46,31 @@ static DEFINE_MUTEX(devpts_mutex);static void pty_close (struct tty_struct *tty,struct file *filp) { +struct tty_file_private *priv = filp ? filp->private_data :NULL ; +struct tty_file_private *other; +bool another_open = false; +if (tty->driver->subtype ==PTY_TYPE_MASTER )WARN_ON (tty->count >1 );else { -if (tty_io_error (tty)) +/* tty_release() also calls close after an unsuccessful open. */ +if (priv && !priv->pty_opened) +return ; +/* In-kernel users have no per-file open state. */ +if (!priv && tty->count >2 )return ; -if (tty->count >2 ) +/* tty->count includes opens that have not succeeded yet. */ +spin_lock (&tty->files_lock); +if (priv) + priv->pty_opened = false; +list_for_each_entry (other, &tty->tty_files, list) { +if (other->pty_opened) { + another_open = true; +break ; + } + } +spin_unlock (&tty->files_lock); +if (another_open ||tty_io_error (tty))return ; }set_bit (TTY_IO_ERROR , &tty->flags);
@@ -223,14 +242,23 @@ static int pty_open(struct tty_struct *tty, struct file *filp)if (test_bit (TTY_OTHER_CLOSED , &tty->flags)) goto out; +/* A rejected open must not disrupt already-open slave descriptors. */ if (test_bit (TTY_PTY_LOCK , &tty->link->flags)) - goto out; +return -EIO ;if (tty->driver->subtype ==PTY_TYPE_SLAVE && tty->link->count !=1 ) goto out;clear_bit (TTY_IO_ERROR , &tty->flags);clear_bit (TTY_OTHER_CLOSED , &tty->link->flags);set_bit (TTY_THROTTLED , &tty->flags); +/* Record a successful open before another slave can close. */ +if (filp && tty->driver->subtype ==PTY_TYPE_SLAVE ) { +struct tty_file_private *priv = filp->private_data; + +spin_lock (&tty->files_lock); + priv->pty_opened = true; +spin_unlock (&tty->files_lock); + }return 0 ; out:
drivers/tty/tty_io.c +1 -0
@@ -186,6 +186,7 @@ int tty_alloc_file(struct file *file) priv =kmalloc_obj (*priv);if (!priv)return -ENOMEM ; + priv->pty_opened = false; file->private_data = priv;
include/linux/tty.h +1 -0
@@ -247,6 +247,7 @@ struct tty_file_private {struct tty_struct *tty;struct file *file;struct list_head list; +bool pty_opened; };/**
tools/testing/selftests/filesystems/.gitignore +1 -0
@@ -6,4 +6,5 @@ file_stressor anon_inode_test kernfs_test idmapped_tmpfile +pty_locked_open ustat_test
tools/testing/selftests/filesystems/Makefile +1 -1
@@ -2,7 +2,7 @@ CFLAGS += $(KHDR_INCLUDES) TEST_GEN_PROGS := devpts_pts file_stressor anon_inode_test kernfs_test fclog ustat_test -TEST_GEN_PROGS += idmapped_tmpfile +TEST_GEN_PROGS += idmapped_tmpfile pty_locked_open TEST_GEN_PROGS_EXTENDED := dnotify_test include ../lib.mk
tools/testing/selftests/filesystems/pty_locked_open.c new file +187 -0
187 lines not on this page · raw message
Signature · 1 line
2.50.1
[email protected]
AI reviewSashiko has reviewed this patch and found no issues. It looks great!
Quoted 17 lines
Opening a locked PTY slave fails with EIO, but the failure path sets
TTY_IO_ERRORon the shared slave tty. Previously opened slave files then fail I/O even though their master is still open.
tty_open()releases a file after the slave open callback fails. Merely skippingTTY_IO_ERRORon the rejected open is insufficient:pty_close()can mistake the failed file for the last slave and close the master. Counting tty references is also insufficient because a real last close can race the failed open and its release.Track which slave files opened successfully. Ignore failed files during close, and mark the master peer closed only when the last successful slave file closes. In-kernel callers may pass NULL instead of a file; retain the count-based close behavior for those callers. A selftest checks locked opens with zero, one, and two existing slave files, plus ordinary last-slave close behavior.
Quoted 3 lines
Fixes 699390354da6 ("pty: Ignore slave pty close() if never successfully opened")Assisted-by LLMSigned-off-by Akira Patafio <[email protected]>
Sashiko has reviewed this patch and found no issues. It looks great!
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Greg KH
maintainerQuoted 3 lines · Akira Patafio
Opening a locked PTY slave fails with EIO, but the failure path sets
TTY_IO_ERRORon the shared slave tty. Previously opened slave files then fail I/O even though their master is still open.
Ok, in digging a lot, I think this is "as intended". You MUST call unlockpt() in userspace to unlock a locked pty, and if you don't do that, this all fails as you have seen.
Where in the POSIX spec say that if you attempt to open a locked PTY, that it's allowed to keep working? You are in control of the pty, so it's not like this is some other user doing this lock/unlock stuff, right? What do other Unixes do if you attempt to do this type of thing?
And what about the pty tests in the LTP test suite, did you run them to verify that your change didn't actually break anything there?
Quoted 86 lines
tty_open()releases a file after the slave open callback fails. Merely skippingTTY_IO_ERRORon the rejected open is insufficient:pty_close()can mistake the failed file for the last slave and close the master. Counting tty references is also insufficient because a real last close can race the failed open and its release.Track which slave files opened successfully. Ignore failed files during close, and mark the master peer closed only when the last successful slave file closes. In-kernel callers may pass NULL instead of a file; retain the count-based close behavior for those callers. A selftest checks locked opens with zero, one, and two existing slave files, plus ordinary last-slave close behavior.
Fixes 699390354da6 ("pty: Ignore slave pty close() if never successfully opened")Assisted-by LLMSigned-off-by Akira Patafio <[email protected]>---
Changes in v2:
- Preserve the existing NULL-file callback behavior for in-kernel TTY users.
- Add the Assisted-by tag requested in review.Tests:
- The selftest failed on an unpatched Linux 6.12.69 kernel at the pre-existing slave write with errno EIO (TAP fail:1).
- Built the patched 7.3.0-rc5x86_64kernel with legacy PTYs and Speakup enabled, without compiler warnings. Booted it in QEMU/HVF and ran the selftest in the guest: TAP pass:1 fail:0, exit status 0.drivers/tty/pty.c | 34 +++- drivers/tty/tty_io.c | 1 + include/linux/tty.h | 1 + .../testing/selftests/filesystems/.gitignore | 1 + tools/testing/selftests/filesystems/Makefile | 2 +- .../selftests/filesystems/pty_locked_open.c | 187 ++++++++++++++++++ 6 files changed, 222 insertions(+), 4 deletions(-) create mode 100644 tools/testing/selftests/filesystems/pty_locked_open.cdrivers/tty/pty.c +23
-3@@ -46,12 +46,31 @@ static DEFINE_MUTEX(devpts_mutex);static void pty_close (struct tty_struct *tty,struct file *filp) { +struct tty_file_private *priv = filp ? filp->private_data :NULL ; +struct tty_file_private *other; +bool another_open = false; +if (tty->driver->subtype ==PTY_TYPE_MASTER )WARN_ON (tty->count >1 );else { -if (tty_io_error (tty)) +/* tty_release() also calls close after an unsuccessful open. */ +if (priv && !priv->pty_opened) +return ; +/* In-kernel users have no per-file open state. */ +if (!priv && tty->count >2 )return ; -if (tty->count >2 ) +/* tty->count includes opens that have not succeeded yet. */ +spin_lock (&tty->files_lock); +if (priv) + priv->pty_opened = false; +list_for_each_entry (other, &tty->tty_files, list) { +if (other->pty_opened) { + another_open = true; +break ; + } + } +spin_unlock (&tty->files_lock); +if (another_open ||tty_io_error (tty))return ; }set_bit (TTY_IO_ERROR , &tty->flags);@@ -223,14 +242,23 @@ static int pty_open(struct tty_struct *tty, struct file *filp)if (test_bit (TTY_OTHER_CLOSED , &tty->flags)) goto out; +/* A rejected open must not disrupt already-open slave descriptors. */ if (test_bit (TTY_PTY_LOCK , &tty->link->flags)) - goto out; +return -EIO ;if (tty->driver->subtype ==PTY_TYPE_SLAVE && tty->link->count !=1 ) goto out;
drivers/tty/pty.c static int pty_open(struct tty_struct *tty, struct file *filp) · line 260
Quoted 10 lines
drivers/tty/pty.c +7
-0clear_bit (TTY_IO_ERROR , &tty->flags);clear_bit (TTY_OTHER_CLOSED , &tty->link->flags);set_bit (TTY_THROTTLED , &tty->flags); +/* Record a successful open before another slave can close. */ +if (filp && tty->driver->subtype ==PTY_TYPE_SLAVE ) { +struct tty_file_private *priv = filp->private_data; + +spin_lock (&tty->files_lock); + priv->pty_opened = true; +spin_unlock (&tty->files_lock);
It's kind of obvious that this is LLM-generated code as it loves to add random new booleans and doesn't know about properly locking logic in the kernel.
Think about why you are attempting to use a lock here, and yet the rest of the function didn't use a lock at all, doesn't that feel "odd" to you?
Be _VERY_ careful when using a LLM to create a patch like this...
thanks,
greg k-h