| 0/0 |
2026/05/21 14:10 |
flow |
assessment-security |
8m
Results: map[DenialOfService:true Explanation:Based on the analysis of the kernel bug report and the source code, here is the
assessment of the vulnerability:
### Bug Mechanism
The bug is a local Denial of Service (DoS) caused by holding a read-write
semaphore (`termios_rwsem`) and a mutex (`atomic_read_lock`) across a user-space
memory copy (`copy_to_iter`), which can be intentionally stalled by an
unprivileged user.
1. When a user reads from a TTY (e.g., a pseudo-terminal), `tty_read()` acquires
`ldisc_sem` for read and calls `iterate_tty_read()`.
2. `iterate_tty_read()` repeatedly calls the line discipline's read function
(`n_tty_read()`) to fetch data into a small 64-byte stack buffer (`kernel_buf`).
3. `n_tty_read()` acquires `atomic_read_lock` and `termios_rwsem` (for read). If
there is more data available than what fits in the 64-byte buffer, it sets a
`cookie` and returns to the caller **without releasing `atomic_read_lock` and
`termios_rwsem`** (this is an optimization to avoid lock bouncing).
4. `iterate_tty_read()` then calls `copy_to_iter()` to copy the 64 bytes from
`kernel_buf` to the user-provided buffer.
5. If the user-provided buffer is backed by `userfaultfd` or a FUSE filesystem,
the unprivileged user can intentionally block the page fault indefinitely. While
blocked, the thread continues to hold `termios_rwsem` (read),
`atomic_read_lock`, and `ldisc_sem` (read).
6. The user can then spawn a second thread and call `ioctl(TCSETS)` on the same
TTY. This eventually calls `tty_set_termios()`, which attempts to acquire
`termios_rwsem` for write using `down_write()`.
7. Because `down_write()` puts the task into an **uninterruptible** sleep
(`TASK_UNINTERRUPTIBLE` / D-state), the second thread becomes completely
unkillable (immune to `SIGKILL`).
### Security Impact
* **Exploitable:** No. This is a locking issue that leads to a deadlock/hang,
not a memory corruption vulnerability. It cannot be used for privilege
escalation or arbitrary code execution.
* **Denial Of Service:** Yes. An unprivileged user can repeatedly exploit this
to create thousands of unkillable D-state tasks. This exhausts the system's PID
space, kernel stacks, and memory, eventually locking up the system. Furthermore,
if `kernel.hung_task_panic` is enabled, this will immediately crash the system.
* **Accessible From Unprivileged Processes:** Yes. Any unprivileged user can
create a PTY pair, write data to the master, and read from the slave into a
FUSE/userfaultfd buffer to trigger the hang. No special capabilities are
required.
* **Accessible From User Namespaces:** Yes, for the same reasons as above.
* **VM Guest Trigger:** Yes, an unprivileged user inside a VM guest can trigger
this to DoS the guest kernel.
### Conclusion
This is a local Denial of Service vulnerability accessible to unprivileged
users. The root cause is the anti-pattern of holding kernel locks (specifically
`termios_rwsem` and `atomic_read_lock`) while performing a potentially blocking
copy to user space (`copy_to_iter`). Exploitable:false FilesystemTrigger:false NetworkTrigger:false PeripheralTrigger:false RemoteTrigger:false Unprivileged:true UserNamespace:true VMGuestTrigger:true VMHostTrigger:false]
|
| 1/1 |
2026/05/21 14:10 |
action |
syz-repro-to-c-repro |
0m
Results:
|
| 2/1 |
2026/05/21 14:10 |
action |
kernel-checkouter |
0m
Results: map[KernelSrc:/app/workdir/cache/src/544276298cf27c4e033fe49d3c5d66a675e4b606]
|
| 3/1 |
2026/05/21 14:10 |
action |
kernel-builder |
0m
Results: map[KernelObj:/app/workdir/cache/build/ae6c90caf5f2a97bf384ec8122159c4abd4b733f]
|
| 4/1 |
2026/05/21 14:10 |
action |
codesearch-prepare |
0m
Results: map[Index:codesearch-index]
|
| 5/1 |
2026/05/21 14:11 |
agent |
expert |
7m
Model:
Results: map[DenialOfService:true Exploitable:false FilesystemTrigger:false NetworkTrigger:false PeripheralTrigger:false RemoteTrigger:false Unprivileged:true UserNamespace:true VMGuestTrigger:true VMHostTrigger:false]
Instruction:
You are an experienced Linux kernel security engineer. Your task is to analyze given kernel bug report
and determine its security impact based on the following dimensions.
Use the provided tools to examine the source code, check for capability checks (e.g., capable(), ns_capable()),
and understand the nature of the bug. Analyze the given kernel build and configuration.
You can check the kernel config by grepping ".config" file; you can check kernel cmdline by greeping
".config" file for "CONFIG_CMDLINE=". Assume sysctl parameters have default values.
But analyze for the corresponding production build w/o debugging tools enabled (like KASAN, KMSAN, UBSAN).
Don't make assumptions; verify them with source code access. Try different strategies when analyzing the bug:
- think of ways in which the vulnerable code is unreachable
- or the other way around: try to come up with different ideas of how an unprivileged user can reach the bug
If still unsure err on the side of the bug being non-exploitable/not-accessible.
In the final reply, provide a reasoning for your assessment.
Analysis dimensions:
* Exploitable:
Determine if the bug can result in memory corruption or elevated privileges.
Memory safety issues are almost always exploitable (KASAN or UBSAN reports for use-after-free, out-of-bounds;
refcounting issues, corrupted lists, etc). When kernel is crashing on a completly wild pointer access
(e.g. user-space address, or non-canonical address, but not on NULL or address corresponding to KASAN shadow
for NULL address), including both data accesses and control tranfers, that's also usually implies possibility
of exploitation. Such reports usually say "unable to handle kernel paging request".
Uses of uninitialized values detected by KMSAN may be exploitable b/c attacker frequently can affect uninit
values with spraying techniques. However, for these exploitabability depends on how exactly the uninit value
is used in the code, and what it affects.
Think of what happens after the bug is triggered. Some bugs cause kernel panic and halt execution,
they are harder to exploit. For example, BUG reports halts the kernel. However, WARNING reports don't halt
execution in production builds. Debug bug detection tools (like KASAN, KMSAN, KCSAN, UBSAN) are also not enabled
in production builds, so attacker can freely exploit these bugs w/o being detected by these tools.
If you see an integer overflow, think how the overflowed value used later (if it's used as allocation size,
or an array index). If you see an out-of-bounds read, think if it's followed by an out-of-bounds write as well.
Some KCSAN data-races may be exploitable by skilled attackers as well. Think what data structures got corrupted
as the result of data races and how. However, note that kernel has lots of "benign" data races that don't lead
to any runtime misbehavior at all.
* Denial Of Service:
Determine if the bug can result in denial-of-service. Most bugs can, since they cause system crash,
hangs, deadlocks, or resource leaks. This is mostly applicable to WARNING bugs that won't cause system crash
in production. For these think what will be consequences of the violation of the kernel assumptions flagged
by the WARNING. In some cases the unexpected condition is also properly handled by the normal control flow
(e.g. with "if (WARN_ON(...))"), these won't cause denial-of-service. If the condition is not handled,
then it may or may not cause denial-of-service.
* Accessible From Unprivileged Processes:
Determine if the bug can be reached from a typical (non-root) user process that does NOT have any special capabilities
(like CAP_SYS_ADMIN, CAP_NET_ADMIN, CAP_NET_RAW, CAP_PERFMON) or access to device nodes restricted to root.
Assume that unprivileged_bpf_disabled=1, that is eBPF loading is not accessible. However, cBPF (classical BPF)
is still accessible to non-root processes.
Assume that user namespaces are not accessible, that is, the process cannot get the mentioned capabilities even
within a new user namespace (checked by ns_capable() function in the kernel sources).
* Accessible From User Namespaces:
Determine if the bug can be reached within a user-namespace where the process has all capabilities
(including CAP_SYS_ADMIN, CAP_NET_ADMIN, CAP_NET_RAW, CAP_PERFMON). Such capabilities are checked with ns_capable()
function in the kernel sources.
* VM Guest Trigger:
Determine if the bug can be triggered from the context of a typical KVM guest (e.g., set up by a QEMU VMM).
Consider accesses to standard Linux host paravirtualized features (virtio-blk, virtio-net, etc.),
and handling of VM exits in the KVM code.
* VM Host Trigger in The Confidetial Computing Context:
Determine if the bug can be triggered in a confidential computing guest kernel from the context of a KVM host.
Consider access to standard Linux guest paravirtualized features (virtio-blk, virtio-net, etc.).
* Ethernet Network Trigger:
Determine if the bug can be triggered by processing ingress network Ethernet traffic, either directly (network stack)
or via drivers exposed to network data.
* Other Remote Trigger:
Determine if the bug can be triggered by processing remote traffic other than Ethernet (Wifi, Bluetooth, NFC, etc).
* Peripheral Trigger:
Determine if the bug can be triggered via an untrusted peripheral device that can be physically plugged
into a system, such as a USB device or a niche hardware driver handling external hardware inputs.
This is particularly important for mobile and desktop environments where users can plug in unknown devices.
* Malicious Filesystem Trigger:
Determine if the bug can be triggered by the kernel mounting and parsing a malicious filesystem image.
This is highly critical for Desktop and Mobile environments where external media or downloaded images
might be auto-mounted.
Prefer calling several tools at the same time to save round-trips.
Use set-results tool to provide results of the analysis.
It must be called exactly once before the final reply.
Ignore results of this tool.
Prompt:
The kernel bug report is:
INFO: task syz.2.3733:18112 blocked for more than 143 seconds.
Tainted: G L syzkaller #0
"echo 0 > /proc/sys/kernel/hung_task_timeout_secs" disables this message.
task:syz.2.3733 state:D stack:28120 pid:18112 tgid:18105 ppid:16852 task_flags:0x400140 flags:0x00080002
Call Trace:
<TASK>
context_switch kernel/sched/core.c:5388 [inline]
__schedule+0x1821/0x5740 kernel/sched/core.c:7189
__schedule_loop kernel/sched/core.c:7268 [inline]
schedule+0x164/0x360 kernel/sched/core.c:7283
schedule_preempt_disabled+0x13/0x30 kernel/sched/core.c:7340
rwsem_down_write_slowpath+0x883/0x1080 kernel/locking/rwsem.c:1215
__down_write_common kernel/locking/rwsem.c:1347 [inline]
__down_write kernel/locking/rwsem.c:1356 [inline]
down_write+0x1bc/0x200 kernel/locking/rwsem.c:1626
tty_set_termios+0x147/0x1800 drivers/tty/tty_ioctl.c:333
set_termios+0x566/0x710 drivers/tty/tty_ioctl.c:516
tty_mode_ioctl+0x4a6/0x7d0 drivers/tty/tty_ioctl.c:-1
tty_ioctl+0x9c5/0xde0 drivers/tty/tty_io.c:2801
vfs_ioctl fs/ioctl.c:51 [inline]
__do_sys_ioctl fs/ioctl.c:597 [inline]
__se_sys_ioctl+0xfc/0x170 fs/ioctl.c:583
do_syscall_x64 arch/x86/entry/syscall_64.c:63 [inline]
do_syscall_64+0x15f/0xf80 arch/x86/entry/syscall_64.c:94
entry_SYSCALL_64_after_hwframe+0x77/0x7f
RIP: 0033:0x7f7d5d99ce59
RSP: 002b:00007f7d5e7de028 EFLAGS: 00000246 ORIG_RAX: 0000000000000010
RAX: ffffffffffffffda RBX: 00007f7d5dc15fa0 RCX: 00007f7d5d99ce59
RDX: 00002000000000c0 RSI: 0000000000005402 RDI: 0000000000000003
RBP: 00007f7d5da32d6f R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 0000000000000000
R13: 00007f7d5dc16038 R14: 00007f7d5dc15fa0 R15: 00007ffe9eb7f248
</TASK>
Showing all locks held in the system:
3 locks held by kworker/u8:0/12:
2 locks held by kworker/u8:1/13:
#0: ffff88813fe7c940 ((wq_completion)events_unbound#2){+.+.}-{0:0}, at: process_one_work kernel/workqueue.c:3289 [inline]
#0: ffff88813fe7c940 ((wq_completion)events_unbound#2){+.+.}-{0:0}, at: process_scheduled_works+0xa35/0x1860 kernel/workqueue.c:3397
#1: ffffc90000127c40 ((work_completion)(&sub_info->work)){+.+.}-{0:0}, at: process_one_work kernel/workqueue.c:3290 [inline]
#1: ffffc90000127c40 ((work_completion)(&sub_info->work)){+.+.}-{0:0}, at: process_scheduled_works+0xa70/0x1860 kernel/workqueue.c:3397
1 lock held by khungtaskd/31:
#0: ffffffff8e95cde0 (rcu_read_lock){....}-{1:3}, at: rcu_lock_acquire include/linux/rcupdate.h:300 [inline]
#0: ffffffff8e95cde0 (rcu_read_lock){....}-{1:3}, at: rcu_read_lock include/linux/rcupdate.h:838 [inline]
#0: ffffffff8e95cde0 (rcu_read_lock){....}-{1:3}, at: debug_show_all_locks+0x2e/0x180 kernel/locking/lockdep.c:6775
2 locks held by kworker/u8:2/36:
#0: ffff88813fe7c940 ((wq_completion)events_unbound#2){+.+.}-{0:0}, at: process_one_work kernel/workqueue.c:3289 [inline]
#0: ffff88813fe7c940 ((wq_completion)events_unbound#2){+.+.}-{0:0}, at: process_scheduled_works+0xa35/0x1860 kernel/workqueue.c:3397
#1: ffffc90000ac7c40 ((work_completion)(&sub_info->work)){+.+.}-{0:0}, at: process_one_work kernel/workqueue.c:3290 [inline]
#1: ffffc90000ac7c40 ((work_completion)(&sub_info->work)){+.+.}-{0:0}, at: process_scheduled_works+0xa70/0x1860 kernel/workqueue.c:3397
2 locks held by getty/5383:
#0: ffff8880364390a0 (&tty->ldisc_sem){++++}-{0:0}, at: tty_ldisc_ref_wait+0x25/0x70 drivers/tty/tty_ldisc.c:243
#1: ffffc900032332e8 (&ldata->atomic_read_lock){+.+.}-{4:4}, at: n_tty_read+0x45c/0x13a0 drivers/tty/n_tty.c:2211
3 locks held by kworker/u8:15/6208:
3 locks held by kworker/u8:18/6211:
4 locks held by kworker/u8:19/6212:
6 locks held by syz.6.3282/16454:
2 locks held by syz.2.3733/18112:
#0: ffff888020f010a0 (&tty->ldisc_sem){++++}-{0:0}, at: tty_ldisc_ref_wait+0x25/0x70 drivers/tty/tty_ldisc.c:243
#1: ffff888020f012c8 (&tty->termios_rwsem){++++}-{4:4}, at: tty_set_termios+0x147/0x1800 drivers/tty/tty_ioctl.c:333
3 locks held by kworker/u8:7/18226:
#0: ffff888032307940 ((wq_completion)ipv6_addrconf){+.+.}-{0:0}, at: process_one_work kernel/workqueue.c:3289 [inline]
#0: ffff888032307940 ((wq_completion)ipv6_addrconf){+.+.}-{0:0}, at: process_scheduled_works+0xa35/0x1860 kernel/workqueue.c:3397
#1: ffffc9000737fc40 ((work_completion)(&(&ifa->dad_work)->work)){+.+.}-{0:0}, at: process_one_work kernel/workqueue.c:3290 [inline]
#1: ffffc9000737fc40 ((work_completion)(&(&ifa->dad_work)->work)){+.+.}-{0:0}, at: process_scheduled_works+0xa70/0x1860 kernel/workqueue.c:3397
#2: ffffffff8fdd0680 (rtnl_mutex){+.+.}-{4:4}, at: rtnl_net_lock include/linux/rtnetlink.h:130 [inline]
#2: ffffffff8fdd0680 (rtnl_mutex){+.+.}-{4:4}, at: addrconf_dad_work+0x11e/0x14c0 net/ipv6/addrconf.c:4206
5 locks held by kworker/u8:9/18380:
1 lock held by syz-executor/19455:
2 locks held by syz-executor/19496:
1 lock held by syz.8.4159/19884:
3 locks held by syz.7.4160/19894:
1 lock held by sed/19929:
1 lock held by modprobe/19931:
2 locks held by modprobe/19933:
=============================================
NMI backtrace for cpu 1
CPU: 1 UID: 0 PID: 31 Comm: khungtaskd Tainted: G L syzkaller #0 PREEMPT(full)
Tainted: [L]=SOFTLOCKUP
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 04/18/2026
Call Trace:
<TASK>
dump_stack_lvl+0xe8/0x150 lib/dump_stack.c:120
nmi_cpu_backtrace+0x274/0x2d0 lib/nmi_backtrace.c:113
nmi_trigger_cpumask_backtrace+0x17a/0x300 lib/nmi_backtrace.c:62
trigger_all_cpu_backtrace include/linux/nmi.h:162 [inline]
__sys_info lib/sys_info.c:157 [inline]
sys_info+0x135/0x170 lib/sys_info.c:165
check_hung_uninterruptible_tasks kernel/hung_task.c:353 [inline]
watchdog+0xfd3/0x1030 kernel/hung_task.c:561
kthread+0x389/0x470 kernel/kthread.c:436
ret_from_fork+0x514/0xb70 arch/x86/kernel/process.c:158
ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:245
</TASK>
Sending NMI from CPU 1 to CPUs 0:
NMI backtrace for cpu 0
CPU: 0 UID: 0 PID: 0 Comm: swapper/0 Tainted: G L syzkaller #0 PREEMPT(full)
Tainted: [L]=SOFTLOCKUP
Hardware name: Google Google Compute Engine/Google Compute Engine, BIOS Google 04/18/2026
RIP: 0010:pv_native_safe_halt+0xf/0x20 arch/x86/kernel/paravirt.c:63
Code: cb 7d 02 e9 fe 6f 67 f5 cc cc cc 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 90 f3 0f 1e fa 66 90 0f 00 2d 13 92 20 00 fb f4 <e9> d7 6f 67 f5 cc cc cc cc cc cc cc cc cc cc cc cc 90 90 90 90 90
RSP: 0018:ffffffff8e607dc0 EFLAGS: 00000242
RAX: 0000000002e20a9d RBX: ffffffff819a958a RCX: 0000000080000001
RDX: 0000000000000001 RSI: ffffffff8dfaa874 RDI: ffffffff8c28af60
RBP: ffffffff8e607eb0 R08: ffff8880b86339db R09: 1ffff110170c673b
R10: dffffc0000000000 R11: ffffed10170c673c R12: 0000000000000000
R13: 1ffffffff1cd25d8 R14: 0000000000000000 R15: 1ffffffff1cd25d8
FS: 0000000000000000(0000) GS:ffff88812528a000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 00007fa9d77ea540 CR3: 0000000058363000 CR4: 0000000000350ef0
Call Trace:
<TASK>
arch_safe_halt arch/x86/kernel/process.c:766 [inline]
default_idle+0x9/0x20 arch/x86/kernel/process.c:767
default_idle_call+0x72/0xb0 kernel/sched/idle.c:122
cpuidle_idle_call kernel/sched/idle.c:199 [inline]
do_idle+0x36a/0x5f0 kernel/sched/idle.c:352
cpu_startup_entry+0x43/0x60 kernel/sched/idle.c:451
rest_init+0x2de/0x300 init/main.c:762
start_kernel+0x38a/0x3e0 init/main.c:1220
x86_64_start_reservations+0x24/0x30 arch/x86/kernel/head64.c:310
x86_64_start_kernel+0x143/0x1c0 arch/x86/kernel/head64.c:291
common_startup_64+0x13e/0x147
</TASK>
Reply: Based on the analysis of the kernel bug report and the source code, here is the assessment of the vulnerability:
### Bug Mechanism
The bug is a local Denial of Service (DoS) caused by holding a read-write semaphore (`termios_rwsem`) and a mutex (`atomic_read_lock`) across a user-space memory copy (`copy_to_iter`), which can be intentionally stalled by an unprivileged user.
1. When a user reads from a TTY (e.g., a pseudo-terminal), `tty_read()` acquires `ldisc_sem` for read and calls `iterate_tty_read()`.
2. `iterate_tty_read()` repeatedly calls the line discipline's read function (`n_tty_read()`) to fetch data into a small 64-byte stack buffer (`kernel_buf`).
3. `n_tty_read()` acquires `atomic_read_lock` and `termios_rwsem` (for read). If there is more data available than what fits in the 64-byte buffer, it sets a `cookie` and returns to the caller **without releasing `atomic_read_lock` and `termios_rwsem`** (this is an optimization to avoid lock bouncing).
4. `iterate_tty_read()` then calls `copy_to_iter()` to copy the 64 bytes from `kernel_buf` to the user-provided buffer.
5. If the user-provided buffer is backed by `userfaultfd` or a FUSE filesystem, the unprivileged user can intentionally block the page fault indefinitely. While blocked, the thread continues to hold `termios_rwsem` (read), `atomic_read_lock`, and `ldisc_sem` (read).
6. The user can then spawn a second thread and call `ioctl(TCSETS)` on the same TTY. This eventually calls `tty_set_termios()`, which attempts to acquire `termios_rwsem` for write using `down_write()`.
7. Because `down_write()` puts the task into an **uninterruptible** sleep (`TASK_UNINTERRUPTIBLE` / D-state), the second thread becomes completely unkillable (immune to `SIGKILL`).
### Security Impact
* **Exploitable:** No. This is a locking issue that leads to a deadlock/hang, not a memory corruption vulnerability. It cannot be used for privilege escalation or arbitrary code execution.
* **Denial Of Service:** Yes. An unprivileged user can repeatedly exploit this to create thousands of unkillable D-state tasks. This exhausts the system's PID space, kernel stacks, and memory, eventually locking up the system. Furthermore, if `kernel.hung_task_panic` is enabled, this will immediately crash the system.
* **Accessible From Unprivileged Processes:** Yes. Any unprivileged user can create a PTY pair, write data to the master, and read from the slave into a FUSE/userfaultfd buffer to trigger the hang. No special capabilities are required.
* **Accessible From User Namespaces:** Yes, for the same reasons as above.
* **VM Guest Trigger:** Yes, an unprivileged user inside a VM guest can trigger this to DoS the guest kernel.
### Conclusion
This is a local Denial of Service vulnerability accessible to unprivileged users. The root cause is the anti-pattern of holding kernel locks (specifically `termios_rwsem` and `atomic_read_lock`) while performing a potentially blocking copy to user space (`copy_to_iter`).
|
| 6/2 |
2026/05/21 14:11 |
llm |
expert |
0m
Model:
Tokens:
input: 8484
output: 44
thoughts: 1368
Thoughts: **Analyzing a Hung Task in the TTY Subsystem**
Okay, so I've got a report indicating a task, `syz.2.3733`, has been blocked for quite a while – over 143 seconds, to be exact. The stack trace points directly to `tty_set_termios`, specifically the `down_write(&tty->termios_rwsem)` call. This indicates it's trying to acquire a write lock on the `termios_rwsem` read-write semaphore associated with a TTY device.
My initial thought is to figure out who's holding that semaphore, the resource in contention. The lockdep output for the blocked task, `syz.2.3733`, *itself* confirms that it holds the `tty->termios_rwsem` and is blocked *on* it. It also shows a second lock being held. Wait, what? It appears syz.2.3733 holds the lock and is blocked on itself getting it again? This is strange, as locks are usually acquired and *released* by the same task. The lockdep output doesn't show other tasks holding it.
I'm thinking, is the semaphore being held by a *reader*? `down_write` is a write operation on a read-write semaphore (rwsem), so it's possible a read lock is active and preventing the write. Lockdep *should* track readers, but maybe the reader isn't blocked and therefore not explicitly shown? Or maybe lockdep is not able to determine which operation is blocking the `down_write`. Let's check the locks held by other tasks.
I check all other locks held by other tasks, which are also listed. Nothing there indicates another task is currently holding `tty->termios_rwsem`. However, it does show other tasks holding `ldisc_sem`. Hmm.
Then I get this line: `"Showing all locks held in the system:"` which lists all tasks and their number of locks. Ah, here's where things get interesting! `syz.6.3282` is listed as holding *six* locks, but the output doesn't show what those six locks *are*. The other task also listed, `syz.2.3733`, only lists two locks, which are shown. Seems the output is truncated or otherwise not showing the detailed lock information for `syz.6.3282`. So I have a very strong hunch that there may be a lock held by a read lock on the rwsem that is blocking `syz.2.3733`. Let's check `tty_set_termios` in the TTY driver's source code to see what that operation does.
|
| 7/2 |
2026/05/21 14:11 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/tty/tty_ioctl.c IncludeLines:true Name:tty_set_termios]
Results: map[SourceCode: 309: /**
310: * tty_set_termios - update termios values
311: * @tty: tty to update
312: * @new_termios: desired new value
313: *
314: * Perform updates to the termios values set on this @tty. A master pty's
315: * termios should never be set.
316: *
317: * Locking: &tty_struct->termios_rwsem
318: */
319: int tty_set_termios(struct tty_struct *tty, struct ktermios *new_termios)
320: {
321: struct ktermios old_termios;
322: struct tty_ldisc *ld;
323:
324: WARN_ON(tty->driver->type == TTY_DRIVER_TYPE_PTY &&
325: tty->driver->subtype == PTY_TYPE_MASTER);
326: /*
327: * Perform the actual termios internal changes under lock.
328: */
329:
330:
331: /* FIXME: we need to decide on some locking/ordering semantics
332: for the set_termios notification eventually */
333: down_write(&tty->termios_rwsem);
334: old_termios = tty->termios;
335: tty->termios = *new_termios;
336: unset_locked_termios(tty, &old_termios);
337: /* Reset any ADDRB changes, ADDRB is changed through ->rs485_config() */
338: tty->termios.c_cflag ^= (tty->termios.c_cflag ^ old_termios.c_cflag) & ADDRB;
339:
340: if (tty->ops->set_termios)
341: tty->ops->set_termios(tty, &old_termios);
342: else
343: tty_termios_copy_hw(&tty->termios, &old_termios);
344:
345: ld = tty_ldisc_ref(tty);
346: if (ld != NULL) {
347: if (ld->ops->set_termios)
348: ld->ops->set_termios(tty, &old_termios);
349: tty_ldisc_deref(ld);
350: }
351: up_write(&tty->termios_rwsem);
352: return 0;
353: }
SourceFile:drivers/tty/tty_ioctl.c]
|
| 8/2 |
2026/05/21 14:11 |
llm |
expert |
0m
Model:
Tokens:
input: 11259
output: 42
thoughts: 718
Thoughts: **Analyzing a Blocking Issue in Terminal Handling**
Okay, so I've hit a snag. The task is getting blocked right at `down_write(&tty->termios_rwsem)` on line 333. My initial thought is to check the NMI backtrace, and what do you know, it's showing both CPUs as idle or stuck in watchdog. That's odd. If both CPUs are essentially doing nothing, there can't be an active loop holding the lock.
Okay, let me rephrase that. If *nothing* is actively executing on either CPU, then the lock holder *must* be asleep, yes? So, who's holding `tty->termios_rwsem`? That's the million-dollar question. I can't immediately see the culprit. I'm missing some critical context. Could it possibly be `syz.6.3282/16454`? The lockdep output would be incredibly useful right about now, but it's not here.
Alright, I need to remember what's going on here. I need to refresh myself, are there any known race conditions in this area with `tty->termios_rwsem` and `tty->ldisc_sem`? I should probably refresh my memory on any known issues in this part of the codebase. Oh wait... right... `tty_set_termios` is called from `set_termios`. I need to start tracing back from there. I'll need to go deeper.
|
| 9/2 |
2026/05/21 14:11 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/tty/tty_ioctl.c IncludeLines:true Name:set_termios]
Results: map[SourceCode: 430: /**
431: * set_termios - set termios values for a tty
432: * @tty: terminal device
433: * @arg: user data
434: * @opt: option information
435: *
436: * Helper function to prepare termios data and run necessary other functions
437: * before using tty_set_termios() to do the actual changes.
438: *
439: * Locking: called functions take &tty_struct->ldisc_sem and
440: * &tty_struct->termios_rwsem locks
441: *
442: * Returns: 0 on success, an error otherwise
443: */
444: static int set_termios(struct tty_struct *tty, void __user *arg, int opt)
445: {
446: struct ktermios tmp_termios;
447: struct tty_ldisc *ld;
448: int retval = tty_check_change(tty);
449:
450: if (retval)
451: return retval;
452:
453: down_read(&tty->termios_rwsem);
454: tmp_termios = tty->termios;
455: up_read(&tty->termios_rwsem);
456:
457: if (opt & TERMIOS_TERMIO) {
458: if (user_termio_to_kernel_termios(&tmp_termios,
459: (struct termio __user *)arg))
460: return -EFAULT;
461: #ifdef TCGETS2
462: } else if (opt & TERMIOS_OLD) {
463: if (user_termios_to_kernel_termios_1(&tmp_termios,
464: (struct termios __user *)arg))
465: return -EFAULT;
466: } else {
467: if (user_termios_to_kernel_termios(&tmp_termios,
468: (struct termios2 __user *)arg))
469: return -EFAULT;
470: }
471: #else
472: } else if (user_termios_to_kernel_termios(&tmp_termios,
473: (struct termios __user *)arg))
474: return -EFAULT;
475: #endif
476:
477: /* If old style Bfoo values are used then load c_ispeed/c_ospeed
478: * with the real speed so its unconditionally usable */
479: tmp_termios.c_ispeed = tty_termios_input_baud_rate(&tmp_termios);
480: tmp_termios.c_ospeed = tty_termios_baud_rate(&tmp_termios);
481:
482: if (opt & (TERMIOS_FLUSH|TERMIOS_WAIT)) {
483: retry_write_wait:
484: retval = wait_event_interruptible(tty->write_wait, !tty_chars_in_buffer(tty));
485: if (retval < 0)
486: return retval;
487:
488: if (tty_write_lock(tty, false) < 0)
489: goto retry_write_wait;
490:
491: /* Racing writer? */
492: if (tty_chars_in_buffer(tty)) {
493: tty_write_unlock(tty);
494: goto retry_write_wait;
495: }
496:
497: ld = tty_ldisc_ref(tty);
498: if (ld != NULL) {
499: if ((opt & TERMIOS_FLUSH) && ld->ops->flush_buffer)
500: ld->ops->flush_buffer(tty);
501: tty_ldisc_deref(ld);
502: }
503:
504: if ((opt & TERMIOS_WAIT) && tty->ops->wait_until_sent) {
505: tty->ops->wait_until_sent(tty, 0);
506: if (signal_pending(current)) {
507: tty_write_unlock(tty);
508: return -ERESTARTSYS;
509: }
510: }
511:
512: tty_set_termios(tty, &tmp_termios);
513:
514: tty_write_unlock(tty);
515: } else {
516: tty_set_termios(tty, &tmp_termios);
517: }
518:
519: /* FIXME: Arguably if tmp_termios == tty->termios AND the
520: actual requested termios was not tmp_termios then we may
521: want to return an error as no user requested change has
522: succeeded */
523: return 0;
524: }
SourceFile:drivers/tty/tty_ioctl.c]
|
| 10/2 |
2026/05/21 14:11 |
llm |
expert |
0m
Model:
Tokens:
input: 13837
output: 43
thoughts: 396
Thoughts: **Analyzing a Potential TTY Lock Contention**
Okay, let me break this down. First, `set_termios` calls `tty_set_termios` – that much is clear, line 516. And `tty_set_termios` then calls `down_write` on `tty->termios_rwsem`. No surprises there. But, wait, `set_termios` gets called from `tty_mode_ioctl`. I need to remember that.
Now, let's look at this lockdep output. It's showing `syz.2.3733/18112` is holding `(&tty->ldisc_sem)`. And also, that `(&tty->termios_rwsem)` is held at `tty_set_termios`. Okay...
Crucially, `syz.2.3733/18112` *holds* `tty->ldisc_sem`. But `set_termios` itself *doesn't* hold that semaphore. So, how are we getting a conflict? Could `tty_mode_ioctl` be holding `ldisc_sem`? Not directly. This is where it gets interesting... The function `tty_ldisc_ref_wait` is actually called from `tty_set_termios`?!
Aha! Hang on... Reading the code... `tty_set_termios` calls `tty_ldisc_ref(tty)` at line 345. But, *that* call *doesn't* wait. Which means the `tty_ldisc_ref_wait` that shows up in the stack trace might be indirectly happening as a result of something else happening in that function, or in another, perhaps.
Okay, let me retrace my steps. The line showing `tty_ldisc_ref_wait` is at `drivers/tty/tty_ldisc.c:243`. Right, I should go back and re-examine the `tty_mode_ioctl` function. This *has* to be the source of the issue. I need to understand how the lock is being acquired here.
|
| 11/2 |
2026/05/21 14:11 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/tty/tty_ioctl.c IncludeLines:true Name:tty_mode_ioctl]
Results: map[SourceCode: 755: /**
756: * tty_mode_ioctl - mode related ioctls
757: * @tty: tty for the ioctl
758: * @cmd: command
759: * @arg: ioctl argument
760: *
761: * Perform non-line discipline specific mode control ioctls. This is designed
762: * to be called by line disciplines to ensure they provide consistent mode
763: * setting.
764: */
765: int tty_mode_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
766: {
767: struct tty_struct *real_tty;
768: void __user *p = (void __user *)arg;
769: int ret = 0;
770: struct ktermios kterm;
771:
772: if (tty->driver->type == TTY_DRIVER_TYPE_PTY &&
773: tty->driver->subtype == PTY_TYPE_MASTER)
774: real_tty = tty->link;
775: else
776: real_tty = tty;
777:
778: switch (cmd) {
779: #ifdef TIOCGETP
780: case TIOCGETP:
781: return get_sgttyb(real_tty, (struct sgttyb __user *) arg);
782: case TIOCSETP:
783: case TIOCSETN:
784: return set_sgttyb(real_tty, (struct sgttyb __user *) arg);
785: #endif
786: #ifdef TIOCGETC
787: case TIOCGETC:
788: return get_tchars(real_tty, p);
789: case TIOCSETC:
790: return set_tchars(real_tty, p);
791: #endif
792: #ifdef TIOCGLTC
793: case TIOCGLTC:
794: return get_ltchars(real_tty, p);
795: case TIOCSLTC:
796: return set_ltchars(real_tty, p);
797: #endif
798: case TCSETSF:
799: return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_OLD);
800: case TCSETSW:
801: return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_OLD);
802: case TCSETS:
803: return set_termios(real_tty, p, TERMIOS_OLD);
804: #ifndef TCGETS2
805: case TCGETS:
806: copy_termios(real_tty, &kterm);
807: if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
808: ret = -EFAULT;
809: return ret;
810: #else
811: case TCGETS:
812: copy_termios(real_tty, &kterm);
813: if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
814: ret = -EFAULT;
815: return ret;
816: case TCGETS2:
817: copy_termios(real_tty, &kterm);
818: if (kernel_termios_to_user_termios((struct termios2 __user *)arg, &kterm))
819: ret = -EFAULT;
820: return ret;
821: case TCSETSF2:
822: return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT);
823: case TCSETSW2:
824: return set_termios(real_tty, p, TERMIOS_WAIT);
825: case TCSETS2:
826: return set_termios(real_tty, p, 0);
827: #endif
828: case TCGETA:
829: return get_termio(real_tty, p);
830: case TCSETAF:
831: return set_termios(real_tty, p, TERMIOS_FLUSH | TERMIOS_WAIT | TERMIOS_TERMIO);
832: case TCSETAW:
833: return set_termios(real_tty, p, TERMIOS_WAIT | TERMIOS_TERMIO);
834: case TCSETA:
835: return set_termios(real_tty, p, TERMIOS_TERMIO);
836: #ifndef TCGETS2
837: case TIOCGLCKTRMIOS:
838: copy_termios_locked(real_tty, &kterm);
839: if (kernel_termios_to_user_termios((struct termios __user *)arg, &kterm))
840: ret = -EFAULT;
841: return ret;
842: case TIOCSLCKTRMIOS:
843: if (!checkpoint_restore_ns_capable(&init_user_ns))
844: return -EPERM;
845: copy_termios_locked(real_tty, &kterm);
846: if (user_termios_to_kernel_termios(&kterm,
847: (struct termios __user *) arg))
848: return -EFAULT;
849: down_write(&real_tty->termios_rwsem);
850: real_tty->termios_locked = kterm;
851: up_write(&real_tty->termios_rwsem);
852: return 0;
853: #else
854: case TIOCGLCKTRMIOS:
855: copy_termios_locked(real_tty, &kterm);
856: if (kernel_termios_to_user_termios_1((struct termios __user *)arg, &kterm))
857: ret = -EFAULT;
858: return ret;
859: case TIOCSLCKTRMIOS:
860: if (!checkpoint_restore_ns_capable(&init_user_ns))
861: return -EPERM;
862: copy_termios_locked(real_tty, &kterm);
863: if (user_termios_to_kernel_termios_1(&kterm,
864: (struct termios __user *) arg))
865: return -EFAULT;
866: down_write(&real_tty->termios_rwsem);
867: real_tty->termios_locked = kterm;
868: up_write(&real_tty->termios_rwsem);
869: return ret;
870: #endif
871: #ifdef TCGETX
872: case TCGETX:
873: case TCSETX:
874: case TCSETXW:
875: case TCSETXF:
876: return -ENOTTY;
877: #endif
878: case TIOCGSOFTCAR:
879: copy_termios(real_tty, &kterm);
880: ret = put_user((kterm.c_cflag & CLOCAL) ? 1 : 0,
881: (int __user *)arg);
882: return ret;
883: case TIOCSSOFTCAR:
884: if (get_user(arg, (unsigned int __user *) arg))
885: return -EFAULT;
886: return tty_change_softcar(real_tty, arg);
887: default:
888: return -ENOIOCTLCMD;
889: }
890: }
SourceFile:drivers/tty/tty_ioctl.c]
|
| 12/2 |
2026/05/21 14:11 |
llm |
expert |
0m
Model:
Tokens:
input: 17031
output: 41
thoughts: 591
Thoughts: **Analyzing the tty_mode_ioctl and set_termios Interaction**
Okay, so I'm looking at how `tty_mode_ioctl` interacts with `set_termios`. Right away, it's clear that the former calls the latter. I see a block of code within `set_termios` that seems particularly interesting, especially the parts dealing with `TERMIOS_FLUSH` and `TERMIOS_WAIT`. There's this `retry_write_wait` loop, which is pretty standard for handling potential races and ensuring the tty write buffer is clear before we proceed. The code waits on `tty->write_wait`, grabs a write lock, checks the buffer again, and then, if the buffer is empty, it calls into the line discipline (`ld`) to flush the buffer if `TERMIOS_FLUSH` is set and then waits for all the bytes to be sent on the serial port.
Then the code calls `tty_set_termios` using `tmp_termios`, but I didn't see where `tty_ldisc_ref_wait` was called there, it looks like `tty_ldisc_ref` is called.
Now, that's where things get interesting because I had a lockdep output. It indicates that there's a lock ordering issue with `tty->ldisc_sem` being acquired before `tty->termios_rwsem`. The stack trace shows `tty_ldisc_ref_wait` and `tty_set_termios`, which points to a potential problem. But wait, `tty_set_termios` calls `tty_ldisc_ref`, it doesn't call `tty_ldisc_ref_wait`, so I need to figure out where `tty_ldisc_ref_wait` is being called from. I think I'll need to check the `tty_ioctl` source code to pinpoint exactly where this lock inversion is happening.
|
| 13/2 |
2026/05/21 14:11 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/tty/tty_io.c IncludeLines:true Name:tty_ioctl]
Results: map[SourceCode:2665: /*
2666: * Split this up, as gcc can choke on it otherwise..
2667: */
2668: long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
2669: {
2670: struct tty_struct *tty = file_tty(file);
2671: struct tty_struct *real_tty;
2672: void __user *p = (void __user *)arg;
2673: int retval;
2674: struct tty_ldisc *ld;
2675:
2676: if (tty_paranoia_check(tty, file_inode(file), "tty_ioctl"))
2677: return -EINVAL;
2678:
2679: real_tty = tty_pair_get_tty(tty);
2680:
2681: /*
2682: * Factor out some common prep work
2683: */
2684: switch (cmd) {
2685: case TIOCSETD:
2686: case TIOCSBRK:
2687: case TIOCCBRK:
2688: case TCSBRK:
2689: case TCSBRKP:
2690: retval = tty_check_change(tty);
2691: if (retval)
2692: return retval;
2693: if (cmd != TIOCCBRK) {
2694: tty_wait_until_sent(tty, 0);
2695: if (signal_pending(current))
2696: return -EINTR;
2697: }
2698: break;
2699: }
2700:
2701: /*
2702: * Now do the stuff.
2703: */
2704: switch (cmd) {
2705: case TIOCSTI:
2706: return tiocsti(tty, p);
2707: case TIOCGWINSZ:
2708: return tiocgwinsz(real_tty, p);
2709: case TIOCSWINSZ:
2710: return tiocswinsz(real_tty, p);
2711: case TIOCCONS:
2712: return real_tty != tty ? -EINVAL : tioccons(file);
2713: case TIOCEXCL:
2714: set_bit(TTY_EXCLUSIVE, &tty->flags);
2715: return 0;
2716: case TIOCNXCL:
2717: clear_bit(TTY_EXCLUSIVE, &tty->flags);
2718: return 0;
2719: case TIOCGEXCL:
2720: {
2721: int excl = test_bit(TTY_EXCLUSIVE, &tty->flags);
2722:
2723: return put_user(excl, (int __user *)p);
2724: }
2725: case TIOCGETD:
2726: return tiocgetd(tty, p);
2727: case TIOCSETD:
2728: return tiocsetd(tty, p);
2729: case TIOCVHANGUP:
2730: if (!capable(CAP_SYS_ADMIN))
2731: return -EPERM;
2732: tty_vhangup(tty);
2733: return 0;
2734: case TIOCGDEV:
2735: {
2736: unsigned int ret = new_encode_dev(tty_devnum(real_tty));
2737:
2738: return put_user(ret, (unsigned int __user *)p);
2739: }
2740: /*
2741: * Break handling
2742: */
2743: case TIOCSBRK: /* Turn break on, unconditionally */
2744: if (tty->ops->break_ctl)
2745: return tty->ops->break_ctl(tty, -1);
2746: return 0;
2747: case TIOCCBRK: /* Turn break off, unconditionally */
2748: if (tty->ops->break_ctl)
2749: return tty->ops->break_ctl(tty, 0);
2750: return 0;
2751: case TCSBRK: /* SVID version: non-zero arg --> no break */
2752: /* non-zero arg means wait for all output data
2753: * to be sent (performed above) but don't send break.
2754: * This is used by the tcdrain() termios function.
2755: */
2756: if (!arg)
2757: return send_break(tty, 250);
2758: return 0;
2759: case TCSBRKP: /* support for POSIX tcsendbreak() */
2760: return send_break(tty, arg ? arg*100 : 250);
2761:
2762: case TIOCMGET:
2763: return tty_tiocmget(tty, p);
2764: case TIOCMSET:
2765: case TIOCMBIC:
2766: case TIOCMBIS:
2767: return tty_tiocmset(tty, cmd, p);
2768: case TIOCGICOUNT:
2769: return tty_tiocgicount(tty, p);
2770: case TCFLSH:
2771: switch (arg) {
2772: case TCIFLUSH:
2773: case TCIOFLUSH:
2774: /* flush tty buffer and allow ldisc to process ioctl */
2775: tty_buffer_flush(tty, NULL);
2776: break;
2777: }
2778: break;
2779: case TIOCSSERIAL:
2780: return tty_tiocsserial(tty, p);
2781: case TIOCGSERIAL:
2782: return tty_tiocgserial(tty, p);
2783: case TIOCGPTPEER:
2784: /* Special because the struct file is needed */
2785: return ptm_open_peer(file, tty, (int)arg);
2786: default:
2787: retval = tty_jobctrl_ioctl(tty, real_tty, file, cmd, arg);
2788: if (retval != -ENOIOCTLCMD)
2789: return retval;
2790: }
2791: if (tty->ops->ioctl) {
2792: retval = tty->ops->ioctl(tty, cmd, arg);
2793: if (retval != -ENOIOCTLCMD)
2794: return retval;
2795: }
2796: ld = tty_ldisc_ref_wait(tty);
2797: if (!ld)
2798: return hung_up_tty_ioctl(file, cmd, arg);
2799: retval = -EINVAL;
2800: if (ld->ops->ioctl) {
2801: retval = ld->ops->ioctl(tty, cmd, arg);
2802: if (retval == -ENOIOCTLCMD)
2803: retval = -ENOTTY;
2804: }
2805: tty_ldisc_deref(ld);
2806: return retval;
2807: }
SourceFile:drivers/tty/tty_io.c]
|
| 14/2 |
2026/05/21 14:11 |
llm |
expert |
0m
Model:
Tokens:
input: 20200
output: 22
thoughts: 980
Thoughts: **Analyzing a Potential TTY Deadlock**
Okay, let's break this down. My initial thought is to trace the execution path. The `tty_ioctl` function is the entry point, and I see it calling `tty_ldisc_ref_wait(tty)` at line 2796. Then, it calls the line discipline's `ioctl` function (`ld->ops->ioctl`). If the line discipline's `ioctl` calls `tty_mode_ioctl`, we get a chain: `tty_mode_ioctl` calls `set_termios`, which calls `tty_set_termios`, which in turn calls `down_write(&tty->termios_rwsem)`. This means that if a process calls `ioctl` that can modify the TTY's termios, it will acquire the write lock on `termios_rwsem`. So, `syz.2.3733/18112` likely holds `tty->ldisc_sem` (acquired through `tty_ldisc_ref_wait`) and is trying to acquire `tty->termios_rwsem`.
Now, I spot a potential contender, `getty/5383`. It holds two locks: `tty->ldisc_sem` and `ldata->atomic_read_lock`. Crucially, this seems to be a *different* TTY instance than the one being used by `syz.2.3733/18112`. I know that because of the memory addresses shown for the `tty` structures.
My next step is to figure out who's holding `tty->termios_rwsem` for this other TTY. Could it be `syz.6.3282/16454`? I immediately check for known deadlock scenarios involving `termios_rwsem`, but it is an rwsem, so readers are not always recorded. However, lockdep *does* show readers and writers. Hmm, it looks like it might be blocked on something else, but it's hard to tell.
The output from `syz.6.3282` is truncated. This is a red flag. The truncation means the kernel log was truncated or interleaved, or syzkaller missed some lines. If `syz.6` holds `termios_rwsem` and is blocked on `ldisc_sem`, we have an AB-BA deadlock on our hands!
Let's trace to see if this is true. Can a process acquire `termios_rwsem` and then `ldisc_sem`? I look at `tty_set_termios`. I see the `down_write` on `termios_rwsem` and then call `tty_ldisc_ref`, which means `ldisc_sem` must be acquired. Ah, but wait. `tty_ldisc_ref` uses `trylock` and returns `NULL` if it cannot acquire the lock! This eliminates the possibility of a direct deadlock. This means it *cannot* deadlock in that order.
Now, I have to consider the other code paths that can lead to acquiring those locks and see if the order is reversed elsewhere in the system.
|
| 15/2 |
2026/05/21 14:11 |
tool |
grepper |
0m
Args: map[Expression:down_.*termios_rwsem]
Results: map[Output:drivers/accessibility/speakup/spk_ttyio.c=135=static inline void get_termios(struct tty_struct *tty,
--
drivers/accessibility/speakup/spk_ttyio.c-137-{
drivers/accessibility/speakup/spk_ttyio.c:138: down_read(&tty->termios_rwsem);
drivers/accessibility/speakup/spk_ttyio.c-139- *out_termios = tty->termios;
--
drivers/tty/n_tty.c=1067=static void isig(int sig, struct tty_struct *tty)
--
drivers/tty/n_tty.c-1097- }
drivers/tty/n_tty.c:1098: down_read(&tty->termios_rwsem);
drivers/tty/n_tty.c-1099-}
--
drivers/tty/n_tty.c=2137=static int n_tty_wait_for_input(struct tty_struct *tty, struct file *file,
--
drivers/tty/n_tty.c-2158- *timeout = wait_woken(wait, TASK_INTERRUPTIBLE, *timeout);
drivers/tty/n_tty.c:2159: down_read(&tty->termios_rwsem);
drivers/tty/n_tty.c-2160-
--
drivers/tty/n_tty.c=2184=static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, u8 *kbuf,
--
drivers/tty/n_tty.c-2214-
drivers/tty/n_tty.c:2215: down_read(&tty->termios_rwsem);
drivers/tty/n_tty.c-2216-
--
drivers/tty/n_tty.c-2250- tty_buffer_flush_work(tty->port);
drivers/tty/n_tty.c:2251: down_read(&tty->termios_rwsem);
drivers/tty/n_tty.c-2252- if (!input_available_p(tty, 0)) {
--
drivers/tty/n_tty.c=2335=static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
--
drivers/tty/n_tty.c-2408-
drivers/tty/n_tty.c:2409: down_read(&tty->termios_rwsem);
drivers/tty/n_tty.c-2410- }
--
drivers/tty/serial/serial_core.c=1554=uart_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
--
drivers/tty/serial/serial_core.c-1576- if (cmd == TIOCSRS485)
drivers/tty/serial/serial_core.c:1577: down_write(&tty->termios_rwsem);
drivers/tty/serial/serial_core.c-1578-
--
drivers/tty/tty_io.c=1137=int tty_send_xchar(struct tty_struct *tty, u8 ch)
--
drivers/tty/tty_io.c-1141- if (tty->ops->send_xchar) {
drivers/tty/tty_io.c:1142: down_read(&tty->termios_rwsem);
drivers/tty/tty_io.c-1143- tty->ops->send_xchar(tty, ch);
--
drivers/tty/tty_io.c-1150-
drivers/tty/tty_io.c:1151: down_read(&tty->termios_rwsem);
drivers/tty/tty_io.c-1152- if (was_stopped)
--
drivers/tty/tty_ioctl.c=101=void tty_unthrottle(struct tty_struct *tty)
drivers/tty/tty_ioctl.c-102-{
drivers/tty/tty_ioctl.c:103: down_write(&tty->termios_rwsem);
drivers/tty/tty_ioctl.c-104- if (test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
--
drivers/tty/tty_ioctl.c=319=int tty_set_termios(struct tty_struct *tty, struct ktermios *new_termios)
--
drivers/tty/tty_ioctl.c-332- for the set_termios notification eventually */
drivers/tty/tty_ioctl.c:333: down_write(&tty->termios_rwsem);
drivers/tty/tty_ioctl.c-334- old_termios = tty->termios;
--
drivers/tty/tty_ioctl.c=444=static int set_termios(struct tty_struct *tty, void __user *arg, int opt)
--
drivers/tty/tty_ioctl.c-452-
drivers/tty/tty_ioctl.c:453: down_read(&tty->termios_rwsem);
drivers/tty/tty_ioctl.c-454- tmp_termios = tty->termios;
--
drivers/tty/tty_ioctl.c=526=static void copy_termios(struct tty_struct *tty, struct ktermios *kterm)
drivers/tty/tty_ioctl.c-527-{
drivers/tty/tty_ioctl.c:528: down_read(&tty->termios_rwsem);
drivers/tty/tty_ioctl.c-529- *kterm = tty->termios;
--
drivers/tty/tty_ioctl.c=533=static void copy_termios_locked(struct tty_struct *tty, struct ktermios *kterm)
drivers/tty/tty_ioctl.c-534-{
drivers/tty/tty_ioctl.c:535: down_read(&tty->termios_rwsem);
drivers/tty/tty_ioctl.c-536- *kterm = tty->termios_locked;
--
drivers/tty/tty_ioctl.c=573=static int get_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
--
drivers/tty/tty_ioctl.c-576-
drivers/tty/tty_ioctl.c:577: down_read(&tty->termios_rwsem);
drivers/tty/tty_ioctl.c-578- tmp.sg_ispeed = tty->termios.c_ispeed;
--
drivers/tty/tty_ioctl.c=625=static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
--
drivers/tty/tty_ioctl.c-637-
drivers/tty/tty_ioctl.c:638: down_write(&tty->termios_rwsem);
drivers/tty/tty_ioctl.c-639- termios = tty->termios;
--
drivers/tty/tty_ioctl.c=653=static int get_tchars(struct tty_struct *tty, struct tchars __user *tchars)
--
drivers/tty/tty_ioctl.c-656-
drivers/tty/tty_ioctl.c:657: down_read(&tty->termios_rwsem);
drivers/tty/tty_ioctl.c-658- tmp.t_intrc = tty->termios.c_cc[VINTR];
--
drivers/tty/tty_ioctl.c=668=static int set_tchars(struct tty_struct *tty, struct tchars __user *tchars)
--
drivers/tty/tty_ioctl.c-673- return -EFAULT;
drivers/tty/tty_ioctl.c:674: down_write(&tty->termios_rwsem);
drivers/tty/tty_ioctl.c-675- tty->termios.c_cc[VINTR] = tmp.t_intrc;
--
drivers/tty/tty_ioctl.c=687=static int get_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
--
drivers/tty/tty_ioctl.c-690-
drivers/tty/tty_ioctl.c:691: down_read(&tty->termios_rwsem);
drivers/tty/tty_ioctl.c-692- tmp.t_suspc = tty->termios.c_cc[VSUSP];
--
drivers/tty/tty_ioctl.c=704=static int set_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
--
drivers/tty/tty_ioctl.c-710-
drivers/tty/tty_ioctl.c:711: down_write(&tty->termios_rwsem);
drivers/tty/tty_ioctl.c-712- tty->termios.c_cc[VSUSP] = tmp.t_suspc;
--
drivers/tty/tty_ioctl.c=737=static int tty_change_softcar(struct tty_struct *tty, bool enable)
--
drivers/tty/tty_ioctl.c-742-
drivers/tty/tty_ioctl.c:743: down_write(&tty->termios_rwsem);
drivers/tty/tty_ioctl.c-744- old = tty->termios;
--
drivers/tty/tty_ioctl.c=765=int tty_mode_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
--
drivers/tty/tty_ioctl.c-848- return -EFAULT;
drivers/tty/tty_ioctl.c:849: down_write(&real_tty->termios_rwsem);
drivers/tty/tty_ioctl.c-850- real_tty->termios_locked = kterm;
--
drivers/tty/tty_ioctl.c-865- return -EFAULT;
drivers/tty/tty_ioctl.c:866: down_write(&real_tty->termios_rwsem);
drivers/tty/tty_ioctl.c-867- real_tty->termios_locked = kterm;
--
drivers/tty/tty_ldisc.c=407=static void tty_set_termios_ldisc(struct tty_struct *tty, int disc)
drivers/tty/tty_ldisc.c-408-{
drivers/tty/tty_ldisc.c:409: down_write(&tty->termios_rwsem);
drivers/tty/tty_ldisc.c-410- tty->termios.c_line = disc;
--
drivers/tty/tty_ldisc.c=519=int tty_set_ldisc(struct tty_struct *tty, int disc)
--
drivers/tty/tty_ldisc.c-570- if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc) {
drivers/tty/tty_ldisc.c:571: down_read(&tty->termios_rwsem);
drivers/tty/tty_ldisc.c-572- tty->ops->set_ldisc(tty);
--
drivers/tty/tty_ldisc.c=625=static void tty_reset_termios(struct tty_struct *tty)
drivers/tty/tty_ldisc.c-626-{
drivers/tty/tty_ldisc.c:627: down_write(&tty->termios_rwsem);
drivers/tty/tty_ldisc.c-628- tty->termios = tty->driver->init_termios;
--
drivers/usb/serial/xr_serial.c=846=static int xr_get_rs485_config(struct tty_struct *tty,
--
drivers/usb/serial/xr_serial.c-851-
drivers/usb/serial/xr_serial.c:852: down_read(&tty->termios_rwsem);
drivers/usb/serial/xr_serial.c-853- if (copy_to_user(argp, &data->rs485, sizeof(data->rs485))) {
--
drivers/usb/serial/xr_serial.c=862=static int xr_set_rs485_config(struct tty_struct *tty,
--
drivers/usb/serial/xr_serial.c-872-
drivers/usb/serial/xr_serial.c:873: down_write(&tty->termios_rwsem);
drivers/usb/serial/xr_serial.c-874- data->rs485 = rs485;
--
net/nfc/nci/uart.c=403=void nci_uart_set_config(struct nci_uart *nu, int baudrate, int flow_ctrl)
--
net/nfc/nci/uart.c-409-
net/nfc/nci/uart.c:410: down_read(&nu->tty->termios_rwsem);
net/nfc/nci/uart.c-411- new_termios = nu->tty->termios;
]
|
| 16/2 |
2026/05/21 14:11 |
llm |
expert |
0m
Model:
Tokens:
input: 25250
output: 45
thoughts: 47
Thoughts: Here's my take on the thought process, summarized as you requested:
**Digging into the Line Discipline Setup**
Right, so `tty_set_ldisc` is grabbing `termios_rwsem` for a read. That's good to know, a useful locking observation. Now, I need to understand what it's *doing* with that lock. Let's head over to `drivers/tty/tty_ldisc.c` and take a look at the implementation of `tty_set_ldisc` itself. Knowing the exact logic behind that read-lock acquisition will clarify the concurrency model and potential implications for performance and contention in line discipline changes. I want to see if there are any obvious gotchas or potential areas for optimization related to its usage.
|
| 17/2 |
2026/05/21 14:11 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/tty/tty_ldisc.c IncludeLines:true Name:tty_set_ldisc]
Results: map[SourceCode: 509: /**
510: * tty_set_ldisc - set line discipline
511: * @tty: the terminal to set
512: * @disc: the line discipline number
513: *
514: * Set the discipline of a tty line. Must be called from a process context. The
515: * ldisc change logic has to protect itself against any overlapping ldisc
516: * change (including on the other end of pty pairs), the close of one side of a
517: * tty/pty pair, and eventually hangup.
518: */
519: int tty_set_ldisc(struct tty_struct *tty, int disc)
520: {
521: int retval;
522: struct tty_ldisc *old_ldisc, *new_ldisc;
523:
524: new_ldisc = tty_ldisc_get(tty, disc);
525: if (IS_ERR(new_ldisc))
526: return PTR_ERR(new_ldisc);
527:
528: tty_lock(tty);
529: retval = tty_ldisc_lock(tty, 5 * HZ);
530: if (retval)
531: goto err;
532:
533: if (!tty->ldisc) {
534: retval = -EIO;
535: goto out;
536: }
537:
538: /* Check the no-op case */
539: if (tty->ldisc->ops->num == disc)
540: goto out;
541:
542: if (test_bit(TTY_HUPPED, &tty->flags)) {
543: /* We were raced by hangup */
544: retval = -EIO;
545: goto out;
546: }
547:
548: if (tty->ops->ldisc_ok) {
549: retval = tty->ops->ldisc_ok(tty, disc);
550: if (retval)
551: goto out;
552: }
553:
554: old_ldisc = tty->ldisc;
555:
556: /* Shutdown the old discipline. */
557: tty_ldisc_close(tty, old_ldisc);
558:
559: /* Now set up the new line discipline. */
560: tty->ldisc = new_ldisc;
561: tty_set_termios_ldisc(tty, disc);
562:
563: retval = tty_ldisc_open(tty, new_ldisc);
564: if (retval < 0) {
565: /* Back to the old one or N_TTY if we can't */
566: tty_ldisc_put(new_ldisc);
567: tty_ldisc_restore(tty, old_ldisc);
568: }
569:
570: if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc) {
571: down_read(&tty->termios_rwsem);
572: tty->ops->set_ldisc(tty);
573: up_read(&tty->termios_rwsem);
574: }
575:
576: /*
577: * At this point we hold a reference to the new ldisc and a
578: * reference to the old ldisc, or we hold two references to
579: * the old ldisc (if it was restored as part of error cleanup
580: * above). In either case, releasing a single reference from
581: * the old ldisc is correct.
582: */
583: new_ldisc = old_ldisc;
584: out:
585: tty_ldisc_unlock(tty);
586:
587: /*
588: * Restart the work queue in case no characters kick it off. Safe if
589: * already running
590: */
591: tty_buffer_restart_work(tty->port);
592: err:
593: tty_ldisc_put(new_ldisc); /* drop the extra reference */
594: tty_unlock(tty);
595: return retval;
596: }
SourceFile:drivers/tty/tty_ldisc.c]
|
| 18/2 |
2026/05/21 14:11 |
llm |
expert |
0m
Model:
Tokens:
input: 26829
output: 22
thoughts: 1811
Thoughts: **Analyzing a Potential TTY Deadlock**
Okay, here's the breakdown of what I'm seeing. It looks like we've got a potential deadlock situation brewing within the TTY subsystem. It started with an observation about how `tty_set_ldisc` interacts with the locking mechanisms. Specifically, `tty_set_ldisc` calls `tty_ldisc_lock`, which acquires `tty->ldisc_sem` for write. This is followed by a call to `tty_set_termios_ldisc`, and the crucial point is that it attempts to acquire `tty->termios_rwsem` *after* holding `ldisc_sem` (write).
My initial thought was that `tty_set_termios` was the culprit, since it calls `down_write(&tty->termios_rwsem)` and then `tty_ldisc_ref` which uses a try lock. However, `trylock` prevents a direct deadlock. This is reinforced by the general lock order: `ldisc_sem` acquired for write before `termios_rwsem`. However the lockdep output from `syz.2.3733/18112` indicates the `tty_set_termios` function is calling `down_write(&tty->termios_rwsem)` *after* holding `ldisc_sem`, which is what I suspected.
I then traced the call path within `tty_ioctl` (and the `ld->ops->ioctl` function). So, `tty_ioctl` acquires `ldisc_sem` for read via `tty_ldisc_ref_wait`. Then if the line discipline's `ioctl` calls `tty_mode_ioctl`, or the N_TTY code calls `tty_mode_ioctl` it calls `set_termios`, which in turn calls `tty_set_termios`. This looks like a potentially problematic lock order: first `ldisc_sem` (read), then `termios_rwsem` (write).
Now, the classic deadlock scenario arises when one process holds `ldisc_sem` (in our case, `syz.2.3733/18112`) for read and is attempting to acquire the write lock on `termios_rwsem`. If another process, potentially `tty_set_ldisc`, which needs write access to `ldisc_sem` is blocked waiting on this read lock, it could deadlock. The issue is exacerbated if one process acquires `termios_rwsem` and blocks on `ldisc_sem`. If two tasks have these lock orders in reverse, deadlock!
I examined other code paths that use the `termios_rwsem` and `ldisc_sem` locks, like `n_tty_read` which acquires `termios_rwsem` after `ldisc_sem`, confirming the need to further examine the deadlock report.
My current theory is that `syz.2.3733/18112` is likely the key. It's holding `ldisc_sem` (read) and blocked on writing to `termios_rwsem`. Now I have to determine which process holds `termios_rwsem` and is blocking on `ldisc_sem`. The likely candidate is `tty_set_ldisc`, which attempts to take `ldisc_sem` first, and then the write lock on `termios_rwsem`. If it's blocked on the read lock on `ldisc_sem`, it's not holding `termios_rwsem`. I'm going to have to dig into what's happening to `syz.2.3733/18112`, and figure out where the read lock is coming from, and *which* task holds `termios_rwsem` (write or read) in order to determine the deadlock.
|
| 19/2 |
2026/05/21 14:11 |
tool |
grepper |
0m
Args: map[Expression:down_.*termios_rwsem]
Results: map[Output:drivers/accessibility/speakup/spk_ttyio.c=135=static inline void get_termios(struct tty_struct *tty,
--
drivers/accessibility/speakup/spk_ttyio.c-137-{
drivers/accessibility/speakup/spk_ttyio.c:138: down_read(&tty->termios_rwsem);
drivers/accessibility/speakup/spk_ttyio.c-139- *out_termios = tty->termios;
--
drivers/tty/n_tty.c=1067=static void isig(int sig, struct tty_struct *tty)
--
drivers/tty/n_tty.c-1097- }
drivers/tty/n_tty.c:1098: down_read(&tty->termios_rwsem);
drivers/tty/n_tty.c-1099-}
--
drivers/tty/n_tty.c=2137=static int n_tty_wait_for_input(struct tty_struct *tty, struct file *file,
--
drivers/tty/n_tty.c-2158- *timeout = wait_woken(wait, TASK_INTERRUPTIBLE, *timeout);
drivers/tty/n_tty.c:2159: down_read(&tty->termios_rwsem);
drivers/tty/n_tty.c-2160-
--
drivers/tty/n_tty.c=2184=static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, u8 *kbuf,
--
drivers/tty/n_tty.c-2214-
drivers/tty/n_tty.c:2215: down_read(&tty->termios_rwsem);
drivers/tty/n_tty.c-2216-
--
drivers/tty/n_tty.c-2250- tty_buffer_flush_work(tty->port);
drivers/tty/n_tty.c:2251: down_read(&tty->termios_rwsem);
drivers/tty/n_tty.c-2252- if (!input_available_p(tty, 0)) {
--
drivers/tty/n_tty.c=2335=static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
--
drivers/tty/n_tty.c-2408-
drivers/tty/n_tty.c:2409: down_read(&tty->termios_rwsem);
drivers/tty/n_tty.c-2410- }
--
drivers/tty/serial/serial_core.c=1554=uart_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
--
drivers/tty/serial/serial_core.c-1576- if (cmd == TIOCSRS485)
drivers/tty/serial/serial_core.c:1577: down_write(&tty->termios_rwsem);
drivers/tty/serial/serial_core.c-1578-
--
drivers/tty/tty_io.c=1137=int tty_send_xchar(struct tty_struct *tty, u8 ch)
--
drivers/tty/tty_io.c-1141- if (tty->ops->send_xchar) {
drivers/tty/tty_io.c:1142: down_read(&tty->termios_rwsem);
drivers/tty/tty_io.c-1143- tty->ops->send_xchar(tty, ch);
--
drivers/tty/tty_io.c-1150-
drivers/tty/tty_io.c:1151: down_read(&tty->termios_rwsem);
drivers/tty/tty_io.c-1152- if (was_stopped)
--
drivers/tty/tty_ioctl.c=101=void tty_unthrottle(struct tty_struct *tty)
drivers/tty/tty_ioctl.c-102-{
drivers/tty/tty_ioctl.c:103: down_write(&tty->termios_rwsem);
drivers/tty/tty_ioctl.c-104- if (test_and_clear_bit(TTY_THROTTLED, &tty->flags) &&
--
drivers/tty/tty_ioctl.c=319=int tty_set_termios(struct tty_struct *tty, struct ktermios *new_termios)
--
drivers/tty/tty_ioctl.c-332- for the set_termios notification eventually */
drivers/tty/tty_ioctl.c:333: down_write(&tty->termios_rwsem);
drivers/tty/tty_ioctl.c-334- old_termios = tty->termios;
--
drivers/tty/tty_ioctl.c=444=static int set_termios(struct tty_struct *tty, void __user *arg, int opt)
--
drivers/tty/tty_ioctl.c-452-
drivers/tty/tty_ioctl.c:453: down_read(&tty->termios_rwsem);
drivers/tty/tty_ioctl.c-454- tmp_termios = tty->termios;
--
drivers/tty/tty_ioctl.c=526=static void copy_termios(struct tty_struct *tty, struct ktermios *kterm)
drivers/tty/tty_ioctl.c-527-{
drivers/tty/tty_ioctl.c:528: down_read(&tty->termios_rwsem);
drivers/tty/tty_ioctl.c-529- *kterm = tty->termios;
--
drivers/tty/tty_ioctl.c=533=static void copy_termios_locked(struct tty_struct *tty, struct ktermios *kterm)
drivers/tty/tty_ioctl.c-534-{
drivers/tty/tty_ioctl.c:535: down_read(&tty->termios_rwsem);
drivers/tty/tty_ioctl.c-536- *kterm = tty->termios_locked;
--
drivers/tty/tty_ioctl.c=573=static int get_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
--
drivers/tty/tty_ioctl.c-576-
drivers/tty/tty_ioctl.c:577: down_read(&tty->termios_rwsem);
drivers/tty/tty_ioctl.c-578- tmp.sg_ispeed = tty->termios.c_ispeed;
--
drivers/tty/tty_ioctl.c=625=static int set_sgttyb(struct tty_struct *tty, struct sgttyb __user *sgttyb)
--
drivers/tty/tty_ioctl.c-637-
drivers/tty/tty_ioctl.c:638: down_write(&tty->termios_rwsem);
drivers/tty/tty_ioctl.c-639- termios = tty->termios;
--
drivers/tty/tty_ioctl.c=653=static int get_tchars(struct tty_struct *tty, struct tchars __user *tchars)
--
drivers/tty/tty_ioctl.c-656-
drivers/tty/tty_ioctl.c:657: down_read(&tty->termios_rwsem);
drivers/tty/tty_ioctl.c-658- tmp.t_intrc = tty->termios.c_cc[VINTR];
--
drivers/tty/tty_ioctl.c=668=static int set_tchars(struct tty_struct *tty, struct tchars __user *tchars)
--
drivers/tty/tty_ioctl.c-673- return -EFAULT;
drivers/tty/tty_ioctl.c:674: down_write(&tty->termios_rwsem);
drivers/tty/tty_ioctl.c-675- tty->termios.c_cc[VINTR] = tmp.t_intrc;
--
drivers/tty/tty_ioctl.c=687=static int get_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
--
drivers/tty/tty_ioctl.c-690-
drivers/tty/tty_ioctl.c:691: down_read(&tty->termios_rwsem);
drivers/tty/tty_ioctl.c-692- tmp.t_suspc = tty->termios.c_cc[VSUSP];
--
drivers/tty/tty_ioctl.c=704=static int set_ltchars(struct tty_struct *tty, struct ltchars __user *ltchars)
--
drivers/tty/tty_ioctl.c-710-
drivers/tty/tty_ioctl.c:711: down_write(&tty->termios_rwsem);
drivers/tty/tty_ioctl.c-712- tty->termios.c_cc[VSUSP] = tmp.t_suspc;
--
drivers/tty/tty_ioctl.c=737=static int tty_change_softcar(struct tty_struct *tty, bool enable)
--
drivers/tty/tty_ioctl.c-742-
drivers/tty/tty_ioctl.c:743: down_write(&tty->termios_rwsem);
drivers/tty/tty_ioctl.c-744- old = tty->termios;
--
drivers/tty/tty_ioctl.c=765=int tty_mode_ioctl(struct tty_struct *tty, unsigned int cmd, unsigned long arg)
--
drivers/tty/tty_ioctl.c-848- return -EFAULT;
drivers/tty/tty_ioctl.c:849: down_write(&real_tty->termios_rwsem);
drivers/tty/tty_ioctl.c-850- real_tty->termios_locked = kterm;
--
drivers/tty/tty_ioctl.c-865- return -EFAULT;
drivers/tty/tty_ioctl.c:866: down_write(&real_tty->termios_rwsem);
drivers/tty/tty_ioctl.c-867- real_tty->termios_locked = kterm;
--
drivers/tty/tty_ldisc.c=407=static void tty_set_termios_ldisc(struct tty_struct *tty, int disc)
drivers/tty/tty_ldisc.c-408-{
drivers/tty/tty_ldisc.c:409: down_write(&tty->termios_rwsem);
drivers/tty/tty_ldisc.c-410- tty->termios.c_line = disc;
--
drivers/tty/tty_ldisc.c=519=int tty_set_ldisc(struct tty_struct *tty, int disc)
--
drivers/tty/tty_ldisc.c-570- if (tty->ldisc->ops->num != old_ldisc->ops->num && tty->ops->set_ldisc) {
drivers/tty/tty_ldisc.c:571: down_read(&tty->termios_rwsem);
drivers/tty/tty_ldisc.c-572- tty->ops->set_ldisc(tty);
--
drivers/tty/tty_ldisc.c=625=static void tty_reset_termios(struct tty_struct *tty)
drivers/tty/tty_ldisc.c-626-{
drivers/tty/tty_ldisc.c:627: down_write(&tty->termios_rwsem);
drivers/tty/tty_ldisc.c-628- tty->termios = tty->driver->init_termios;
--
drivers/usb/serial/xr_serial.c=846=static int xr_get_rs485_config(struct tty_struct *tty,
--
drivers/usb/serial/xr_serial.c-851-
drivers/usb/serial/xr_serial.c:852: down_read(&tty->termios_rwsem);
drivers/usb/serial/xr_serial.c-853- if (copy_to_user(argp, &data->rs485, sizeof(data->rs485))) {
--
drivers/usb/serial/xr_serial.c=862=static int xr_set_rs485_config(struct tty_struct *tty,
--
drivers/usb/serial/xr_serial.c-872-
drivers/usb/serial/xr_serial.c:873: down_write(&tty->termios_rwsem);
drivers/usb/serial/xr_serial.c-874- data->rs485 = rs485;
--
net/nfc/nci/uart.c=403=void nci_uart_set_config(struct nci_uart *nu, int baudrate, int flow_ctrl)
--
net/nfc/nci/uart.c-409-
net/nfc/nci/uart.c:410: down_read(&nu->tty->termios_rwsem);
net/nfc/nci/uart.c-411- new_termios = nu->tty->termios;
]
|
| 20/2 |
2026/05/21 14:11 |
llm |
expert |
0m
Model:
Tokens:
input: 32851
output: 41
thoughts: 779
Thoughts: **Deadlock Analysis of `tty_set_termios`**
Okay, so I'm looking at this syzkaller report, specifically `syz.2.3733/18112`, and it's blocked on `down_write(&tty->termios_rwsem)` within `tty_set_termios`. I need to figure out how this deadlock is happening.
First, I trace the calls. I see `tty_set_termios` gets called from `set_termios`, and `set_termios` takes `termios_rwsem` in read mode using `down_read`. Then, after some operations, it calls `tty_set_termios`, which tries to acquire `termios_rwsem` again, but this time for writing using `down_write`.
My immediate thought is, could multiple threads be calling `set_termios` concurrently? Thread A acquires the read lock, does its thing, and then calls `tty_set_termios` which tries for the write lock. Meanwhile, thread B could do the same. There's no lock held across the read and write operations.
I remember `tty_mode_ioctl` calls `set_termios`. The syzkaller report mentions a lock named `ldisc_sem`. I have to figure out if it's involved in this deadlock. `syz.2.3733/18112` *is* holding `ldisc_sem` for read. This is crucial.
If *another* thread holds `termios_rwsem` for read and is blocked on `ldisc_sem` for write, that's a classic deadlock situation. Who is the other thread?
Now I dig into `tty_set_ldisc`. It looks like it acquires `ldisc_sem` (write) *before* acquiring `termios_rwsem` (write). Okay, if thread A calls `tty_set_ldisc`, it grabs `ldisc_sem` for write and *then* tries for `termios_rwsem` for write.
If we have thread B, which is calling `tty_ioctl` -> `tty_ldisc_ref_wait` (which grabs `ldisc_sem` read) -> `set_termios` -> `tty_set_termios` which attempts to acquire `termios_rwsem` write. Bingo! If thread B holds `ldisc_sem` (read) and then wants `termios_rwsem` (write), and Thread A already has it read, and wants `ldisc_sem` write, we get a deadlock!
But wait. No. The order is important. Thread A acquires `ldisc_sem` for write, and it can't be blocked on `termios_rwsem`. So it is not the deadlocked thread. It can't be! So, who holds `termios_rwsem` then?
Could it be a reader? Could thread C be holding `termios_rwsem` read, and blocked on `ldisc_sem` for read? Ah, that's the key. Since `ldisc_sem` is a read-write semaphore, `ldsem_down_read` will *block* if a writer is pending! So if Thread A (or any other thread) tries to write to the ldisc (holding `ldisc_sem` for write), this blocks readers.
So, syz.2.3733/18112 is likely holding `ldisc_sem` for *read*. Another thread holds `termios_rwsem` for *read* (from calling `set_termios`), and tries to grab `ldisc_sem` for *write*, while the original thread tries to grab `termios_rwsem` for *write*. This is the deadlock!
|
| 21/2 |
2026/05/21 14:11 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:lib/ldsem.c IncludeLines:true Name:ldsem_down_read]
Results: map[SourceCode: 334: /*
335: * lock for reading -- returns 1 if successful, 0 if timed out
336: */
337: int __sched ldsem_down_read(struct ld_semaphore *sem, long timeout)
338: {
339: might_sleep();
340: return __ldsem_down_read_nested(sem, 0, timeout);
341: }
SourceFile:drivers/tty/tty_ldsem.c]
|
| 22/2 |
2026/05/21 14:11 |
llm |
expert |
0m
Model:
Tokens:
input: 34653
output: 48
thoughts: 15
Thoughts: Okay, here's my attempt at summarizing that thought process, keeping in mind I'm speaking to a fellow expert:
**Investigating `__ldsem_down_read_nested`**
Alright, time to dive into this one. The mention of `__ldsem_down_read_nested` immediately makes me think, "Okay, a likely nested read semaphore operation." That function name strongly suggests we're dealing with a lock that allows for concurrent reads, with some level of nesting support. We need to figure out exactly what's going on within that context. I'll need to check the surrounding code for how it's being *used* - specifically what kind of scenarios trigger its invocation. Is it a critical section? A reader/writer lock? And how deep are these nested calls allowed to go? My immediate thought is to look for the implementation itself. That’s probably the best starting point.
|
| 23/2 |
2026/05/21 14:11 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/tty/tty_ldsem.c IncludeLines:true Name:__ldsem_down_read_nested]
Results: map[SourceCode: 295: static int __ldsem_down_read_nested(struct ld_semaphore *sem,
296: int subclass, long timeout)
297: {
298: long count;
299:
300: rwsem_acquire_read(&sem->dep_map, subclass, 0, _RET_IP_);
301:
302: count = atomic_long_add_return(LDSEM_READ_BIAS, &sem->count);
303: if (count <= 0) {
304: lock_contended(&sem->dep_map, _RET_IP_);
305: if (!down_read_failed(sem, count, timeout)) {
306: rwsem_release(&sem->dep_map, _RET_IP_);
307: return 0;
308: }
309: }
310: lock_acquired(&sem->dep_map, _RET_IP_);
311: return 1;
312: }
SourceFile:drivers/tty/tty_ldsem.c]
|
| 24/2 |
2026/05/21 14:11 |
llm |
expert |
0m
Model:
Tokens:
input: 35224
output: 22
thoughts: 626
Thoughts: **Analyzing a Potential Deadlock in TTY Driver**
Okay, so I'm looking at a potential deadlock scenario within the TTY driver. Initially, I was thinking, if `count <= 0`, that indicates a writer is present, and since `LDSEM_READ_BIAS` is positive, a negative `count` means a writer is active. This confirms that readers will block if a writer is active.
Now, this points to a classic three-thread deadlock, which can be visualized as:
* **Thread 1 (e.g., `syz.2.3733/18112`):** Calls `tty_ioctl`, acquires `ldisc_sem` for a read operation, then calls `set_termios` which translates to `tty_set_termios` and ends up blocking on `down_write(&tty->termios_rwsem)`. This suggests it is waiting for exclusive access to the `termios` structure.
* **Thread 2 (e.g., `syz.6.3282/16454`):** This thread is crucial because it can be holding `termios_rwsem` for either read or write operations and subsequently attempts to acquire `ldisc_sem` for read access. The assumption here is that it blocks because there's a pending writer (Thread 3).
* **Thread 3 (e.g., `syz.7.4160/19894`):** Calls `tty_set_ldisc`, which tries to acquire `ldisc_sem` for a write operation. It blocks because Thread 1 is already holding it for read access.
The deadlock manifests because each thread is waiting for a resource held by another. Thread 1 has `ldisc_sem` in read mode while it waits for `termios_rwsem` (held by Thread 2). Thread 2 waits for `ldisc_sem` to be available. Thread 3 has `ldisc_sem` in write mode while it waits for `termios_rwsem`. This all depends on the interaction between these two semaphores.
However, I need to verify a crucial detail about Thread 2. It holds `termios_rwsem` and attempts to acquire `ldisc_sem`. The place I need to check if there is an issue is where this happens in the code.
Wait, the function `tty_ldisc_ref` uses `ldsem_down_read_trylock`. *Trylock* doesn't block! This is a significant factor. If there is a writer, `count < 0`, the `trylock` function immediately returns 0!
```c
int __sched ldsem_down_read_trylock(struct ld_semaphore *sem)
{
long count = atomic_long_read(&sem->count);
while (count >= 0) {
if (atomic_long_try_cmpxchg(&sem->count, &count, count + LDSEM_READ_BIAS)) {
rwsem_acquire_read(&sem->dep_map, 0, 1, _RET_IP_);
lock_acquired(&sem->dep_map, _RET_IP_);
return 1;
}
}
return 0;
}
```
So, Thread 2, when calling `tty_ldisc_ref`, cannot actually block on `ldisc_sem` if a writer is present, because of the `trylock` behavior. It will return immediately if it finds a writer, so it will not get stuck. I need to make sure I am not missing any other potential scenarios where `ldisc_sem` could be acquired for read while holding `termios_rwsem`. I need to search for `tty_ldisc_ref_wait` or `ldsem_down_read` within a context where `termios_rwsem` is also held.
|
| 25/2 |
2026/05/21 14:11 |
tool |
grepper |
0m
Args: map[Expression:tty_ldisc_ref_wait]
Results: map[Output:Documentation/driver-api/tty/tty_ldisc.rst=66=but not yet enforced.
--
Documentation/driver-api/tty/tty_ldisc.rst-68-.. kernel-doc:: drivers/tty/tty_ldisc.c
Documentation/driver-api/tty/tty_ldisc.rst:69: :identifiers: tty_ldisc_ref_wait tty_ldisc_ref tty_ldisc_deref
Documentation/driver-api/tty/tty_ldisc.rst-70-
--
drivers/tty/tty_io.c=901=static ssize_t tty_read(struct kiocb *iocb, struct iov_iter *to)
--
drivers/tty/tty_io.c-916- */
drivers/tty/tty_io.c:917: ld = tty_ldisc_ref_wait(tty);
drivers/tty/tty_io.c-918- if (!ld)
--
drivers/tty/tty_io.c=1062=static ssize_t file_tty_write(struct file *file, struct kiocb *iocb, struct iov_iter *from)
--
drivers/tty/tty_io.c-1074- tty_err(tty, "missing write_room method\n");
drivers/tty/tty_io.c:1075: ld = tty_ldisc_ref_wait(tty);
drivers/tty/tty_io.c-1076- if (!ld)
--
drivers/tty/tty_io.c=1326=static int tty_reopen(struct tty_struct *tty)
--
drivers/tty/tty_io.c-1341-
drivers/tty/tty_io.c:1342: ld = tty_ldisc_ref_wait(tty);
drivers/tty/tty_io.c-1343- if (ld) {
--
drivers/tty/tty_io.c=2186=static __poll_t tty_poll(struct file *filp, poll_table *wait)
--
drivers/tty/tty_io.c-2194-
drivers/tty/tty_io.c:2195: ld = tty_ldisc_ref_wait(tty);
drivers/tty/tty_io.c-2196- if (!ld)
--
drivers/tty/tty_io.c=2272=static int tiocsti(struct tty_struct *tty, u8 __user *p)
--
drivers/tty/tty_io.c-2284- tty_audit_tiocsti(tty, ch);
drivers/tty/tty_io.c:2285: ld = tty_ldisc_ref_wait(tty);
drivers/tty/tty_io.c-2286- if (!ld)
--
drivers/tty/tty_io.c=2444=static int tiocgetd(struct tty_struct *tty, int __user *p)
--
drivers/tty/tty_io.c-2448-
drivers/tty/tty_io.c:2449: ld = tty_ldisc_ref_wait(tty);
drivers/tty/tty_io.c-2450- if (!ld)
--
drivers/tty/tty_io.c=2668=long tty_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
--
drivers/tty/tty_io.c-2795- }
drivers/tty/tty_io.c:2796: ld = tty_ldisc_ref_wait(tty);
drivers/tty/tty_io.c-2797- if (!ld)
--
drivers/tty/tty_io.c=2875=static long tty_compat_ioctl(struct file *file, unsigned int cmd,
--
drivers/tty/tty_io.c-2975-
drivers/tty/tty_io.c:2976: ld = tty_ldisc_ref_wait(tty);
drivers/tty/tty_io.c-2977- if (!ld)
--
drivers/tty/tty_ioctl.c=921=int tty_perform_flush(struct tty_struct *tty, unsigned long arg)
--
drivers/tty/tty_ioctl.c-927-
drivers/tty/tty_ioctl.c:928: ld = tty_ldisc_ref_wait(tty);
drivers/tty/tty_ioctl.c-929- retval = __tty_perform_flush(tty, arg);
--
drivers/tty/tty_ldisc.c=215=const struct seq_operations tty_ldiscs_seq_ops = {
--
drivers/tty/tty_ldisc.c-222-/**
drivers/tty/tty_ldisc.c:223: * tty_ldisc_ref_wait - wait for the tty ldisc
drivers/tty/tty_ldisc.c-224- * @tty: tty device
--
drivers/tty/tty_ldisc.c-238- */
drivers/tty/tty_ldisc.c:239:struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *tty)
drivers/tty/tty_ldisc.c-240-{
--
drivers/tty/tty_ldisc.c-248-}
drivers/tty/tty_ldisc.c:249:EXPORT_SYMBOL_GPL(tty_ldisc_ref_wait);
drivers/tty/tty_ldisc.c-250-
--
drivers/tty/tty_ldisc.c=270=EXPORT_SYMBOL_GPL(tty_ldisc_ref);
--
drivers/tty/tty_ldisc.c-275- *
drivers/tty/tty_ldisc.c:276: * Undoes the effect of tty_ldisc_ref() or tty_ldisc_ref_wait(). May be called
drivers/tty/tty_ldisc.c-277- * in IRQ context.
--
drivers/tty/vt/selection.c=391=int paste_selection(struct tty_struct *tty)
--
drivers/tty/vt/selection.c-408-
drivers/tty/vt/selection.c:409: ld = tty_ldisc_ref_wait(tty);
drivers/tty/vt/selection.c-410- if (!ld)
--
include/linux/tty_ldisc.h=279=void tty_ldisc_deref(struct tty_ldisc *);
include/linux/tty_ldisc.h:280:struct tty_ldisc *tty_ldisc_ref_wait(struct tty_struct *);
include/linux/tty_ldisc.h-281-
]
|
| 26/2 |
2026/05/21 14:11 |
llm |
expert |
0m
Model:
Tokens:
input: 38449
output: 23
thoughts: 1381
Thoughts: **Analyzing Potential Deadlock in TTY Line Discipline and Termios Semaphores**
Okay, let's break this down systematically. I'm looking at a potential deadlock scenario involving `tty_ioctl`, the line discipline (`ldisc_sem`), and the `termios_rwsem`. I need to figure out if it's truly possible, or if the locking order prevents it.
First, I see the typical flow: `tty_ioctl` calls `tty_ldisc_ref_wait`, which locks `ldisc_sem` for read. Then, it calls the line discipline's `ioctl` function. If that's the `n_tty_ioctl`, it further calls `tty_mode_ioctl`, which ultimately hits `set_termios`. `set_termios` is where the `termios_rwsem` comes in, starting with a *read* lock and later, in `tty_set_termios`, grabbing a *write* lock.
Initially, I thought two threads concurrently calling `tty_ioctl` and subsequently `set_termios` might cause a problem with the read-then-write sequence on `termios_rwsem`. However, the fact that both threads acquire `termios_rwsem` for *read* first, means that one will acquire it and then release it, before the next call for a *write* lock. This doesn't seem to lead to a deadlock.
Now, my focus shifts to a more complex three-thread scenario. If one thread has `ldisc_sem` (read) and is blocked on `termios_rwsem` (write), while another has `termios_rwsem` (read) and is blocked on `ldisc_sem` (read), *that* would be interesting. The real challenge is determining if this configuration is even *possible* based on the locking patterns.
The critical question now becomes: Is there any situation where a thread *first* grabs `termios_rwsem` (read) and then attempts to acquire `ldisc_sem`?
I need to meticulously review the places that acquire `termios_rwsem` for read. I've gone through the various functions that can acquire that read lock: `n_tty_read`, `n_tty_write`, `set_termios`, `tty_send_xchar`, `copy_termios`, `get_sgttyb`, and `n_tty_wait_for_input`. Importantly, most of these functions are called from locations that *already* hold `ldisc_sem` (read). So, the order would be `ldisc_sem` then `termios_rwsem`, which breaks the potential for a circular dependency in the lock order.
Then, there's `tty_set_termios`, which *writes* to `termios_rwsem` and calls `tty_ldisc_ref`, which *tries* to acquire `ldisc_sem` using `trylock`. The critical piece here is the use of `trylock`. If it fails to acquire `ldisc_sem`, it *doesn't* block. It returns. So, `tty_set_termios` can't create the necessary blocking condition.
Now, the remaining piece of the puzzle: Could `set_termios` be called *without* holding `ldisc_sem`? In my quick analysis of `tty_mode_ioctl` callers, it looks like `tty_mode_ioctl` gets called from `tty_ioctl` (which *holds* `ldisc_sem`) or `n_tty_ioctl` (which is called from `tty_ioctl` i.e. holding the lock). So, `set_termios` isn't called externally, outside `ldisc_sem`.
Alright, I'm confident there's no deadlock here. It's safe.
|
| 27/2 |
2026/05/21 14:11 |
tool |
codesearch-find-references |
0m
Args:
Results: map[References:[map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:n_tty_ioctl_helper SourceFile:drivers/tty/tty_ioctl.c SourceLine:982] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:ppp_asynctty_ioctl SourceFile:drivers/net/ppp/ppp_async.c SourceLine:324] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:ppp_synctty_ioctl SourceFile:drivers/net/ppp/ppp_synctty.c SourceLine:315] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:slcan_ioctl SourceFile:drivers/net/can/slcan/slcan-core.c SourceLine:916] map[ReferenceKind:calls ReferencingEntityKind:function ReferencingEntityName:slip_ioctl SourceFile:drivers/net/slip/slip.c SourceLine:1176]] TruncatedOutput:false]
|
| 28/2 |
2026/05/21 14:11 |
llm |
expert |
0m
Model:
Tokens:
input: 40973
output: 46
thoughts: 699
Thoughts: **Analyzing Potential Deadlocks in `tty_set_termios`**
Okay, so I'm looking at these `ld->ops->ioctl` implementations, right? They're all being called from `tty_ioctl` with `ldisc_sem` held in read mode. I see that `tty_mode_ioctl` is exported, but it's only called from the ldisc ioctl handlers. That means `tty_set_termios` *always* gets called with `ldisc_sem` held for read. This seems like it could create a problem.
Let's imagine: Thread 1 calls `tty_set_termios`. It already has `ldisc_sem` (read) and is now trying to acquire `termios_rwsem` (write). If it blocks on `termios_rwsem` in write mode, who could be holding that? Could it be Thread 2, which also called `tty_set_termios`?
If Thread 2 *did* call `tty_set_termios`, it would have `ldisc_sem` (read) and already hold `termios_rwsem` (write). Then it does this:
```c
ld = tty_ldisc_ref(tty);
if (ld != NULL) {
if (ld->ops->set_termios)
ld->ops->set_termios(tty, &old_termios);
tty_ldisc_deref(ld);
}
```
Now, `tty_ldisc_ref` calls `ldisc_sem` for read. But Thread 2 *already* has it for read! Can a thread recursively acquire a read lock on a rwsem? Yes, `ldsem_down_read_trylock` will succeed if the count is greater than or equal to zero.
Here's the tricky part: what if there's a pending writer? If Thread 3 calls `tty_set_ldisc`, it's blocked on `ldisc_sem` (write) because Threads 1 and 2 have the read lock. So, Thread 3 is a pending writer, and the count will be less than zero. Then, when Thread 2 calls `tty_ldisc_ref(tty)`, it calls `ldsem_down_read_trylock`. Because the count is less than zero (due to Thread 3), the `trylock` FAILS! `tty_ldisc_ref` returns NULL! Therefore, Thread 2 skips the `ld->ops->set_termios` call and then calls `up_write(&tty->termios_rwsem)`. Then it releases `ldisc_sem` (read). And so, after Thread 2 finishes, Thread 1 can get the write lock and there's *no* deadlock!
However, what if Thread 2 *is* blocked inside `ld->ops->set_termios`? If the trylock succeeds and there's no pending writer, Thread 2 calls `ld->ops->set_termios`, and it blocks on something. Let's check `n_tty_set_termios`.
|
| 29/2 |
2026/05/21 14:11 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/tty/n_tty.c IncludeLines:true Name:n_tty_set_termios]
Results: map[SourceCode:1762: /**
1763: * n_tty_set_termios - termios data changed
1764: * @tty: terminal
1765: * @old: previous data
1766: *
1767: * Called by the tty layer when the user changes termios flags so that the line
1768: * discipline can plan ahead. This function cannot sleep and is protected from
1769: * re-entry by the tty layer. The user is guaranteed that this function will
1770: * not be re-entered or in progress when the ldisc is closed.
1771: *
1772: * Locking: Caller holds @tty->termios_rwsem
1773: */
1774: static void n_tty_set_termios(struct tty_struct *tty, const struct ktermios *old)
1775: {
1776: struct n_tty_data *ldata = tty->disc_data;
1777:
1778: if (!old || (old->c_lflag ^ tty->termios.c_lflag) & (ICANON | EXTPROC)) {
1779: bitmap_zero(ldata->read_flags, N_TTY_BUF_SIZE);
1780: ldata->line_start = ldata->read_tail;
1781: if (!L_ICANON(tty) || !read_cnt(ldata)) {
1782: ldata->canon_head = ldata->read_tail;
1783: ldata->push = 0;
1784: } else {
1785: set_bit(MASK(ldata->read_head - 1), ldata->read_flags);
1786: ldata->canon_head = ldata->read_head;
1787: ldata->push = 1;
1788: }
1789: ldata->commit_head = ldata->read_head;
1790: ldata->erasing = 0;
1791: ldata->lnext = 0;
1792: }
1793:
1794: ldata->icanon = (L_ICANON(tty) != 0);
1795:
1796: if (I_ISTRIP(tty) || I_IUCLC(tty) || I_IGNCR(tty) ||
1797: I_ICRNL(tty) || I_INLCR(tty) || L_ICANON(tty) ||
1798: I_IXON(tty) || L_ISIG(tty) || L_ECHO(tty) ||
1799: I_PARMRK(tty)) {
1800: bitmap_zero(ldata->char_map, 256);
1801:
1802: if (I_IGNCR(tty) || I_ICRNL(tty))
1803: set_bit('\r', ldata->char_map);
1804: if (I_INLCR(tty))
1805: set_bit('\n', ldata->char_map);
1806:
1807: if (L_ICANON(tty)) {
1808: set_bit(ERASE_CHAR(tty), ldata->char_map);
1809: set_bit(KILL_CHAR(tty), ldata->char_map);
1810: set_bit(EOF_CHAR(tty), ldata->char_map);
1811: set_bit('\n', ldata->char_map);
1812: set_bit(EOL_CHAR(tty), ldata->char_map);
1813: if (L_IEXTEN(tty)) {
1814: set_bit(WERASE_CHAR(tty), ldata->char_map);
1815: set_bit(LNEXT_CHAR(tty), ldata->char_map);
1816: set_bit(EOL2_CHAR(tty), ldata->char_map);
1817: if (L_ECHO(tty))
1818: set_bit(REPRINT_CHAR(tty),
1819: ldata->char_map);
1820: }
1821: }
1822: if (I_IXON(tty)) {
1823: set_bit(START_CHAR(tty), ldata->char_map);
1824: set_bit(STOP_CHAR(tty), ldata->char_map);
1825: }
1826: if (L_ISIG(tty)) {
1827: set_bit(INTR_CHAR(tty), ldata->char_map);
1828: set_bit(QUIT_CHAR(tty), ldata->char_map);
1829: set_bit(SUSP_CHAR(tty), ldata->char_map);
1830: }
1831: clear_bit(__DISABLED_CHAR, ldata->char_map);
1832: ldata->raw = 0;
1833: ldata->real_raw = 0;
1834: } else {
1835: ldata->raw = 1;
1836: if ((I_IGNBRK(tty) || (!I_BRKINT(tty) && !I_PARMRK(tty))) &&
1837: (I_IGNPAR(tty) || !I_INPCK(tty)) &&
1838: (tty->driver->flags & TTY_DRIVER_REAL_RAW))
1839: ldata->real_raw = 1;
1840: else
1841: ldata->real_raw = 0;
1842: }
1843: /*
1844: * Fix tty hang when I_IXON(tty) is cleared, but the tty
1845: * been stopped by STOP_CHAR(tty) before it.
1846: */
1847: if (!I_IXON(tty) && old && (old->c_iflag & IXON) && !tty->flow.tco_stopped) {
1848: start_tty(tty);
1849: process_echoes(tty);
1850: }
1851:
1852: /* The termios change make the tty ready for I/O */
1853: wake_up_interruptible(&tty->write_wait);
1854: wake_up_interruptible(&tty->read_wait);
1855: }
SourceFile:drivers/tty/n_tty.c]
|
| 30/2 |
2026/05/21 14:12 |
llm |
expert |
0m
Model:
Tokens:
input: 44147
output: 43
thoughts: 112
Thoughts: **Analyzing a Potential Hang in n_tty_read**
Okay, so `n_tty_set_termios` is non-blocking, which rules out an immediate blocking issue with Thread 2. That's a good starting point. But the hung task, `syz.2.3733/18112`, is specifically blocked on `down_write(&tty->termios_rwsem)`. This is where things get interesting. I need to figure out who's holding that semaphore.
Could there be a read lock holder on `termios_rwsem` that is somehow itself blocked? That's a scenario I need to investigate. My next step has to be examining the `n_tty_read` function. I need to trace the locking behavior there, determine if a reader is blocking, and how it might be related to this write lock contention.
|
| 31/2 |
2026/05/21 14:12 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/tty/n_tty.c IncludeLines:true Name:n_tty_read]
Results: map[SourceCode:2164: /**
2165: * n_tty_read - read function for tty
2166: * @tty: tty device
2167: * @file: file object
2168: * @kbuf: kernelspace buffer pointer
2169: * @nr: size of I/O
2170: * @cookie: if non-%NULL, this is a continuation read
2171: * @offset: where to continue reading from (unused in n_tty)
2172: *
2173: * Perform reads for the line discipline. We are guaranteed that the line
2174: * discipline will not be closed under us but we may get multiple parallel
2175: * readers and must handle this ourselves. We may also get a hangup. Always
2176: * called in user context, may sleep.
2177: *
2178: * This code must be sure never to sleep through a hangup.
2179: *
2180: * Locking: n_tty_read()/consumer path:
2181: * claims non-exclusive termios_rwsem;
2182: * publishes read_tail
2183: */
2184: static ssize_t n_tty_read(struct tty_struct *tty, struct file *file, u8 *kbuf,
2185: size_t nr, void **cookie, unsigned long offset)
2186: {
2187: struct n_tty_data *ldata = tty->disc_data;
2188: u8 *kb = kbuf;
2189: DEFINE_WAIT_FUNC(wait, woken_wake_function);
2190: int minimum, time;
2191: ssize_t retval;
2192: long timeout;
2193: bool packet;
2194: size_t old_tail;
2195:
2196: /* Is this a continuation of a read started earlier? */
2197: if (*cookie)
2198: return n_tty_continue_cookie(tty, kbuf, nr, cookie);
2199:
2200: retval = job_control(tty, file);
2201: if (retval < 0)
2202: return retval;
2203:
2204: /*
2205: * Internal serialization of reads.
2206: */
2207: if (file->f_flags & O_NONBLOCK) {
2208: if (!mutex_trylock(&ldata->atomic_read_lock))
2209: return -EAGAIN;
2210: } else {
2211: if (mutex_lock_interruptible(&ldata->atomic_read_lock))
2212: return -ERESTARTSYS;
2213: }
2214:
2215: down_read(&tty->termios_rwsem);
2216:
2217: minimum = time = 0;
2218: timeout = MAX_SCHEDULE_TIMEOUT;
2219: if (!ldata->icanon) {
2220: minimum = MIN_CHAR(tty);
2221: if (minimum) {
2222: time = (HZ / 10) * TIME_CHAR(tty);
2223: } else {
2224: timeout = (HZ / 10) * TIME_CHAR(tty);
2225: minimum = 1;
2226: }
2227: }
2228:
2229: packet = tty->ctrl.packet;
2230: old_tail = ldata->read_tail;
2231:
2232: add_wait_queue(&tty->read_wait, &wait);
2233: while (nr) {
2234: /* First test for status change. */
2235: if (packet && tty->link->ctrl.pktstatus) {
2236: u8 cs;
2237: if (kb != kbuf)
2238: break;
2239: scoped_guard(spinlock_irq, &tty->link->ctrl.lock) {
2240: cs = tty->link->ctrl.pktstatus;
2241: tty->link->ctrl.pktstatus = 0;
2242: }
2243: *kb++ = cs;
2244: nr--;
2245: break;
2246: }
2247:
2248: if (!input_available_p(tty, 0)) {
2249: up_read(&tty->termios_rwsem);
2250: tty_buffer_flush_work(tty->port);
2251: down_read(&tty->termios_rwsem);
2252: if (!input_available_p(tty, 0)) {
2253: int ret = n_tty_wait_for_input(tty, file, &wait,
2254: &timeout);
2255: if (ret <= 0) {
2256: retval = ret;
2257: break;
2258: }
2259: continue;
2260: }
2261: }
2262:
2263: if (ldata->icanon && !L_EXTPROC(tty)) {
2264: if (canon_copy_from_read_buf(tty, &kb, &nr))
2265: goto more_to_be_read;
2266: } else {
2267: /* Deal with packet mode. */
2268: if (packet && kb == kbuf) {
2269: *kb++ = TIOCPKT_DATA;
2270: nr--;
2271: }
2272:
2273: if (copy_from_read_buf(tty, &kb, &nr) && kb - kbuf >= minimum)
2274: goto more_to_be_read;
2275: }
2276:
2277: n_tty_check_unthrottle(tty);
2278:
2279: if (kb - kbuf >= minimum)
2280: break;
2281: if (time)
2282: timeout = time;
2283: }
2284: if (old_tail != ldata->read_tail) {
2285: /*
2286: * Make sure no_room is not read in n_tty_kick_worker()
2287: * before setting ldata->read_tail in copy_from_read_buf().
2288: */
2289: smp_mb();
2290: n_tty_kick_worker(tty);
2291: }
2292: up_read(&tty->termios_rwsem);
2293:
2294: remove_wait_queue(&tty->read_wait, &wait);
2295: mutex_unlock(&ldata->atomic_read_lock);
2296:
2297: if (kb - kbuf)
2298: retval = kb - kbuf;
2299:
2300: return retval;
2301: more_to_be_read:
2302: /*
2303: * There is more to be had and we have nothing more to wait for, so
2304: * let's mark us for retries.
2305: *
2306: * NOTE! We return here with both the termios_sem and atomic_read_lock
2307: * still held, the retries will release them when done.
2308: */
2309: remove_wait_queue(&tty->read_wait, &wait);
2310: *cookie = cookie;
2311:
2312: return kb - kbuf;
2313: }
SourceFile:drivers/tty/n_tty.c]
|
| 32/2 |
2026/05/21 14:12 |
llm |
expert |
1m
Model:
Tokens:
input: 46679
output: 41
thoughts: 418
Thoughts: **Analyzing the Asynchronous Read Path in the N_TTY Driver**
Okay, let's break this down. `n_tty_read`... right, it starts by taking the read semaphore, `tty->termios_rwsem`. Then it checks for available input using `input_available_p`. If nothing is immediately available, it releases the semaphore, calls `tty_buffer_flush_work` (that's important to remember for potential deadlock scenarios), re-acquires the semaphore, and then, if *still* no input, calls `n_tty_wait_for_input`.
Now, that `n_tty_wait_for_input` function is where things get interesting. It *also* releases the `termios_rwsem` semaphore *before* waiting. It uses `wait_woken` with `TASK_INTERRUPTIBLE`. Okay, that's standard for interruptible waits. But the key thing is the release of the semaphore while waiting. I can see why this is done to avoid blocking for too long.
But wait. What happens if, during this wait, more data becomes available, so the flow jumps to the `more_to_be_read` label? In that path, the code *returns* with `termios_rwsem` still held. But that means a potential race condition and blocking issues! The `remove_wait_queue` call is performed and *not* paired with a `up_read()`. It also returns with something called `atomic_read_lock` still held. This looks like a critical section that must be handled very carefully.
So, `n_tty_read` *can* return with the semaphore held, but only under certain conditions relating to the interrupt handling, and likely some sort of retry mechanism. Where does it even return to? Ah, it returns back to `tty_read`, which lives in `drivers/tty/tty_io.c`. Time to follow this thread of thought to that point. I'll need to double-check that this is thread safe, specifically in that interruptible wait path. There may be some hidden implications here concerning re-entry or data races.
|
| 33/2 |
2026/05/21 14:13 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/tty/tty_io.c IncludeLines:true Name:tty_read]
Results: map[SourceCode: 889: /**
890: * tty_read - read method for tty device files
891: * @iocb: kernel I/O control block
892: * @to: destination for the data read
893: *
894: * Perform the read system call function on this terminal device. Checks
895: * for hung up devices before calling the line discipline method.
896: *
897: * Locking:
898: * Locks the line discipline internally while needed. Multiple read calls
899: * may be outstanding in parallel.
900: */
901: static ssize_t tty_read(struct kiocb *iocb, struct iov_iter *to)
902: {
903: struct file *file = iocb->ki_filp;
904: struct inode *inode = file_inode(file);
905: struct tty_struct *tty = file_tty(file);
906: struct tty_ldisc *ld;
907: ssize_t ret;
908:
909: if (tty_paranoia_check(tty, inode, "tty_read"))
910: return -EIO;
911: if (!tty || tty_io_error(tty))
912: return -EIO;
913:
914: /* We want to wait for the line discipline to sort out in this
915: * situation.
916: */
917: ld = tty_ldisc_ref_wait(tty);
918: if (!ld)
919: return hung_up_tty_read(iocb, to);
920: ret = -EIO;
921: if (ld->ops->read)
922: ret = iterate_tty_read(ld, tty, file, to);
923: tty_ldisc_deref(ld);
924:
925: if (ret > 0)
926: tty_update_time(tty, false);
927:
928: return ret;
929: }
SourceFile:drivers/tty/tty_io.c]
|
| 34/2 |
2026/05/21 14:13 |
llm |
expert |
0m
Model:
Tokens:
input: 48236
output: 43
thoughts: 23
Thoughts: Here's my attempt at summarizing the thought process, tailored for an expert audience:
**Analyzing the Call Stack: `iterate_tty_read`**
Okay, so the program flow is currently hitting `iterate_tty_read`. That's the key. I need to understand what's happening at *that* point in the execution, because that function is where the input stream is being handled. This suggests there is a potential for a problem, likely in either how reads are structured or how that incoming data is being handled as a result. Let's dive into the function's implementation and see what happens in that part of the code. This is definitely where I should start digging.
|
| 35/2 |
2026/05/21 14:14 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/tty/tty_io.c IncludeLines:true Name:iterate_tty_read]
Results: map[SourceCode: 824: /*
825: * Iterate on the ldisc ->read() function until we've gotten all
826: * the data the ldisc has for us.
827: *
828: * The "cookie" is something that the ldisc read function can fill
829: * in to let us know that there is more data to be had.
830: *
831: * We promise to continue to call the ldisc until it stops returning
832: * data or clears the cookie. The cookie may be something that the
833: * ldisc maintains state for and needs to free.
834: */
835: static ssize_t iterate_tty_read(struct tty_ldisc *ld, struct tty_struct *tty,
836: struct file *file, struct iov_iter *to)
837: {
838: void *cookie = NULL;
839: unsigned long offset = 0;
840: ssize_t retval = 0;
841: size_t copied, count = iov_iter_count(to);
842: u8 kernel_buf[64];
843:
844: do {
845: ssize_t size = min(count, sizeof(kernel_buf));
846:
847: size = ld->ops->read(tty, file, kernel_buf, size, &cookie, offset);
848: if (!size)
849: break;
850:
851: if (size < 0) {
852: /* Did we have an earlier error (ie -EFAULT)? */
853: if (retval)
854: break;
855: retval = size;
856:
857: /*
858: * -EOVERFLOW means we didn't have enough space
859: * for a whole packet, and we shouldn't return
860: * a partial result.
861: */
862: if (retval == -EOVERFLOW)
863: offset = 0;
864: break;
865: }
866:
867: copied = copy_to_iter(kernel_buf, size, to);
868: offset += copied;
869: count -= copied;
870:
871: /*
872: * If the user copy failed, we still need to do another ->read()
873: * call if we had a cookie to let the ldisc clear up.
874: *
875: * But make sure size is zeroed.
876: */
877: if (unlikely(copied != size)) {
878: count = 0;
879: retval = -EFAULT;
880: }
881: } while (cookie);
882:
883: /* We always clear tty buffer in case they contained passwords */
884: memzero_explicit(kernel_buf, sizeof(kernel_buf));
885: return offset ? offset : retval;
886: }
SourceFile:drivers/tty/tty_io.c]
|
| 36/2 |
2026/05/21 14:14 |
llm |
expert |
0m
Model:
Tokens:
input: 49364
output: 45
thoughts: 114
Thoughts: **Analyzing the TTY Read Loop**
Okay, so `iterate_tty_read` is calling `ld->ops->read` repeatedly, as long as this `cookie` variable isn't NULL. Interesting. That's a classic loop construct, but the condition is what I'm focused on. Then, `n_tty_read` is returning with `termios_rwsem` *held* if and only if it sets the `cookie`. That's a critical synchronization point. The implications are starting to become clear. If `n_tty_read` returns *holding* `termios_rwsem`, `iterate_tty_read` will immediately call it again! It's a re-entrant pattern. And the next function called in the process is going to be `n_tty_continue_cookie`. So I need to dig into `n_tty_continue_cookie` now to fully understand how this cookie-based mechanism works, and how data flows through this loop. I need to see how the cookie is used to signal the loop's continuation or termination. Let's see how that's implemented.
|
| 37/2 |
2026/05/21 14:14 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/tty/n_tty.c IncludeLines:true Name:n_tty_continue_cookie]
Results: map[SourceCode:2103: /*
2104: * We still hold the atomic_read_lock and the termios_rwsem, and can just
2105: * continue to copy data.
2106: */
2107: static ssize_t n_tty_continue_cookie(struct tty_struct *tty, u8 *kbuf,
2108: size_t nr, void **cookie)
2109: {
2110: struct n_tty_data *ldata = tty->disc_data;
2111: u8 *kb = kbuf;
2112:
2113: if (ldata->icanon && !L_EXTPROC(tty)) {
2114: /*
2115: * If we have filled the user buffer, see if we should skip an
2116: * EOF character before releasing the lock and returning done.
2117: */
2118: if (!nr)
2119: canon_skip_eof(ldata);
2120: else if (canon_copy_from_read_buf(tty, &kb, &nr))
2121: return kb - kbuf;
2122: } else {
2123: if (copy_from_read_buf(tty, &kb, &nr))
2124: return kb - kbuf;
2125: }
2126:
2127: /* No more data - release locks and stop retries */
2128: n_tty_kick_worker(tty);
2129: n_tty_check_unthrottle(tty);
2130: up_read(&tty->termios_rwsem);
2131: mutex_unlock(&ldata->atomic_read_lock);
2132: *cookie = NULL;
2133:
2134: return kb - kbuf;
2135: }
SourceFile:drivers/tty/n_tty.c]
|
| 38/2 |
2026/05/21 14:14 |
llm |
expert |
0m
Model:
Tokens:
input: 50295
output: 45
thoughts: 408
Thoughts: **Analyzing a Potential Kernel Bug**
Okay, so I'm looking at this `iterate_tty_read` function, and something's not sitting quite right. Let's break down the logic: it reads data from the tty using `ld->ops->read`, then copies it to the user's buffer using `copy_to_iter`. The interesting part is what happens if `copy_to_iter` fails, like when the user buffer is unmapped. In that case, `copied` won't equal `size`. This will make count to 0.
The code then sets `count = 0` and sets a `-EFAULT` return value. But the really worrying part is that it *still loops* because `cookie` is still set! Now it's calling `ld->ops->read` again, but this time with `size = 0`! That seems... odd.
This ultimately leads to a call to `n_tty_continue_cookie` with `nr = 0`. Within `n_tty_continue_cookie`, there is a call to `canon_skip_eof` if `nr == 0`. It checks if canonical mode is enabled, and the process is not external. And then there is also a call to `copy_from_read_buf`, which looks like the next point of interest.
The `copy_from_read_buf` returns 0 if `nr` is 0. But wait, `copy_from_read_buf` returns a `bool`! Why is that? I need to check the definition of `copy_from_read_buf` to understand its behavior in this situation. It's possible this could lead to an infinite loop, or some other unexpected behavior. This needs further investigation.
|
| 39/2 |
2026/05/21 14:15 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/tty/n_tty.c IncludeLines:true Name:copy_from_read_buf]
Results: map[SourceCode:1918: /**
1919: * copy_from_read_buf - copy read data directly
1920: * @tty: terminal device
1921: * @kbp: data
1922: * @nr: size of data
1923: *
1924: * Helper function to speed up n_tty_read(). It is only called when %ICANON is
1925: * off; it copies characters straight from the tty queue.
1926: *
1927: * Returns: true if it successfully copied data, but there is still more data
1928: * to be had.
1929: *
1930: * Locking:
1931: * * called under the @ldata->atomic_read_lock sem
1932: * * n_tty_read()/consumer path:
1933: * caller holds non-exclusive %termios_rwsem;
1934: * read_tail published
1935: */
1936: static bool copy_from_read_buf(const struct tty_struct *tty, u8 **kbp,
1937: size_t *nr)
1938:
1939: {
1940: struct n_tty_data *ldata = tty->disc_data;
1941: size_t n;
1942: bool is_eof;
1943: size_t head = smp_load_acquire(&ldata->commit_head);
1944: size_t tail = MASK(ldata->read_tail);
1945:
1946: n = min3(head - ldata->read_tail, N_TTY_BUF_SIZE - tail, *nr);
1947: if (!n)
1948: return false;
1949:
1950: u8 *from = read_buf_addr(ldata, tail);
1951: memcpy(*kbp, from, n);
1952: is_eof = n == 1 && *from == EOF_CHAR(tty);
1953: tty_audit_add_data(tty, from, n);
1954: zero_buffer(tty, from, n);
1955: smp_store_release(&ldata->read_tail, ldata->read_tail + n);
1956:
1957: /* Turn single EOF into zero-length read */
1958: if (L_EXTPROC(tty) && ldata->icanon && is_eof &&
1959: head == ldata->read_tail)
1960: return false;
1961:
1962: *kbp += n;
1963: *nr -= n;
1964:
1965: /* If we have more to copy, let the caller know */
1966: return head != ldata->read_tail;
1967: }
SourceFile:drivers/tty/n_tty.c]
|
| 40/2 |
2026/05/21 14:15 |
llm |
expert |
0m
Model:
Tokens:
input: 51944
output: 47
thoughts: 185
Thoughts: **Analyzing the TTY Read Loop**
Okay, let's break this down systematically. If `nr` (number read) is zero, then `n` (another read counter, I presume) is also zero. That means the `copy_from_read_buf` function returns `false`, indicating no data was actually copied. Therefore, we go into the `n_tty_continue_cookie` path.
Now, `n_tty_continue_cookie` is interesting. It essentially says, "No more data, so we're done." It releases the locks, specifically calling `n_tty_kick_worker`, `n_tty_check_unthrottle`, and releasing the read lock on the `termios_rwsem` and the `atomic_read_lock`. It also sets the cookie to null. Then, it returns `0` (the difference between `kb` and `kbuf`, which are, I assume, pointers into kernel buffers).
So, `iterate_tty_read` receives a `size` of 0, because `n_tty_continue_cookie` returned 0. And that means it **breaks** the loop! It does not loop infinitely as I initially considered.
But wait a minute, I need to consider all possible scenarios. What about `canon_copy_from_read_buf`? I need to analyze this separately to make sure that the same logic applies to this path.
|
| 41/2 |
2026/05/21 14:15 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/tty/n_tty.c IncludeLines:true Name:canon_copy_from_read_buf]
Results: map[SourceCode:1969: /**
1970: * canon_copy_from_read_buf - copy read data in canonical mode
1971: * @tty: terminal device
1972: * @kbp: data
1973: * @nr: size of data
1974: *
1975: * Helper function for n_tty_read(). It is only called when %ICANON is on; it
1976: * copies one line of input up to and including the line-delimiting character
1977: * into the result buffer.
1978: *
1979: * Note: When termios is changed from non-canonical to canonical mode and the
1980: * read buffer contains data, n_tty_set_termios() simulates an EOF push (as if
1981: * C-d were input) _without_ the %DISABLED_CHAR in the buffer. This causes data
1982: * already processed as input to be immediately available as input although a
1983: * newline has not been received.
1984: *
1985: * Locking:
1986: * * called under the %atomic_read_lock mutex
1987: * * n_tty_read()/consumer path:
1988: * caller holds non-exclusive %termios_rwsem;
1989: * read_tail published
1990: */
1991: static bool canon_copy_from_read_buf(const struct tty_struct *tty, u8 **kbp,
1992: size_t *nr)
1993: {
1994: struct n_tty_data *ldata = tty->disc_data;
1995: size_t n, size, more, c;
1996: size_t eol;
1997: size_t tail, canon_head;
1998: int found = 0;
1999:
2000: /* N.B. avoid overrun if nr == 0 */
2001: if (!*nr)
2002: return false;
2003:
2004: canon_head = smp_load_acquire(&ldata->canon_head);
2005: n = min(*nr, canon_head - ldata->read_tail);
2006:
2007: tail = MASK(ldata->read_tail);
2008: size = min_t(size_t, tail + n, N_TTY_BUF_SIZE);
2009:
2010: eol = find_next_bit(ldata->read_flags, size, tail);
2011: more = n - (size - tail);
2012: if (eol == N_TTY_BUF_SIZE && more) {
2013: /* scan wrapped without finding set bit */
2014: eol = find_first_bit(ldata->read_flags, more);
2015: found = eol != more;
2016: } else
2017: found = eol != size;
2018:
2019: n = eol - tail;
2020: if (n > N_TTY_BUF_SIZE)
2021: n += N_TTY_BUF_SIZE;
2022: c = n + found;
2023:
2024: if (!found || read_buf(ldata, eol) != __DISABLED_CHAR)
2025: n = c;
2026:
2027: tty_copy(tty, *kbp, tail, n);
2028: *kbp += n;
2029: *nr -= n;
2030:
2031: if (found)
2032: clear_bit(eol, ldata->read_flags);
2033: smp_store_release(&ldata->read_tail, ldata->read_tail + c);
2034:
2035: if (found) {
2036: if (!ldata->push)
2037: ldata->line_start = ldata->read_tail;
2038: else
2039: ldata->push = 0;
2040: tty_audit_push();
2041: return false;
2042: }
2043:
2044: /* No EOL found - do a continuation retry if there is more data */
2045: return ldata->read_tail != canon_head;
2046: }
SourceFile:drivers/tty/n_tty.c]
|
| 42/2 |
2026/05/21 14:15 |
llm |
expert |
1m
Model:
Tokens:
input: 53720
output: 43
thoughts: 4305
Thoughts: **Analysis of a Potential TTY Deadlock Vulnerability**
Okay, here's my breakdown of this. It looks like we've got a classic resource contention scenario playing out in the TTY subsystem. The core issue is the potential for a user-space process to induce an indefinite block during a `read()` operation, which then holds critical locks and prevents subsequent `ioctl` calls (specifically, `TCSETS` and thus `tty_set_termios`) from completing, leading to a hang.
My primary concern revolves around how `copy_to_iter` can block. Specifically, if the buffer being read from has a backing store that's subject to a page fault, and that page fault is handled by, say, a userfaultfd, we have a problem. The faulting process can essentially stall indefinitely in user space while holding `termios_rwsem` (read) and possibly `ldisc_sem` (read).
Here's the sequence I've worked through:
1. A read operation is initiated, potentially involving `n_tty_read` and subsequent calls to `iterate_tty_read` which in turn calls `copy_to_iter`. This call copies data to the user buffer.
2. `copy_to_iter` can trigger a page fault if the data is not present in memory.
3. The crucial point is that `copy_to_iter` is called *with* `termios_rwsem` held for read, and potentially `ldisc_sem` as well.
4. If the page fault blocks (e.g., waiting for a userfaultfd response), this thread will block indefinitely *while* holding the locks.
5. A separate thread (e.g., from an `ioctl(TCSETS)` call) attempts to modify terminal settings (through `tty_set_termios`, which tries to acquire `termios_rwsem` in write mode, and potentially `ldisc_sem` in write mode).
6. The second thread blocks because the first thread holds the lock for read, creating a write dependency.
7. Since `termios_rwsem` is a rwsem, the second thread is now a pending writer blocking the original reader.
8. Any *other* threads attempting to use the locks will also get blocked, freezing the TTY.
The use of userfaultfd is a critical piece of this puzzle because it provides a mechanism for an unprivileged user to create a malicious page fault, essentially causing their read to block indefinitely. Without userfaultfd, the page fault might still block on a local resource (like a lock) which makes it less reliable as a DoS technique. The FUSE filesystem is also a potential source of indefinite blocking.
This effectively creates a local denial-of-service vulnerability. An unprivileged user can create an unkillable task that leaks resources. And because `down_write` is uninterruptible, the hung task cannot be easily killed with SIGKILL (or at all). This can eventually cause a kernel panic if `hung_task_panic` is set or lead to PID/memory exhaustion. The use of `ldisc_sem` just adds to the problem in other concurrent threads.
The `n_tty_read` function explicitly returns with `termios_rwsem` held, and so `copy_to_iter` is called with the lock. This is the heart of the race condition.
I’m particularly focused on the possibility that this is a repeatable DoS, as syzbot reports indicate hung tasks and potential panics. This indicates there is a serious problem which should be addressed.
|
| 43/2 |
2026/05/21 14:16 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/tty/n_tty.c IncludeLines:true Name:n_tty_write]
Results: map[SourceCode:2335: static ssize_t n_tty_write(struct tty_struct *tty, struct file *file,
2336: const u8 *buf, size_t nr)
2337: {
2338: const u8 *b = buf;
2339: DEFINE_WAIT_FUNC(wait, woken_wake_function);
2340: ssize_t num, retval = 0;
2341:
2342: /* Job control check -- must be done at start (POSIX.1 7.1.1.4). */
2343: if (L_TOSTOP(tty) && file->f_op->write_iter != redirected_tty_write) {
2344: retval = tty_check_change(tty);
2345: if (retval)
2346: return retval;
2347: }
2348:
2349: guard(rwsem_read)(&tty->termios_rwsem);
2350:
2351: /* Write out any echoed characters that are still pending */
2352: process_echoes(tty);
2353:
2354: add_wait_queue(&tty->write_wait, &wait);
2355: while (1) {
2356: if (signal_pending(current)) {
2357: retval = -ERESTARTSYS;
2358: break;
2359: }
2360: if (tty_hung_up_p(file) || (tty->link && !tty->link->count)) {
2361: retval = -EIO;
2362: break;
2363: }
2364: if (O_OPOST(tty)) {
2365: while (nr > 0) {
2366: num = process_output_block(tty, b, nr);
2367: if (num < 0) {
2368: if (num == -EAGAIN)
2369: break;
2370: retval = num;
2371: goto break_out;
2372: }
2373: b += num;
2374: nr -= num;
2375: if (nr == 0)
2376: break;
2377: if (process_output(*b, tty) < 0)
2378: break;
2379: b++; nr--;
2380: }
2381: if (tty->ops->flush_chars)
2382: tty->ops->flush_chars(tty);
2383: } else {
2384: struct n_tty_data *ldata = tty->disc_data;
2385:
2386: while (nr > 0) {
2387: scoped_guard(mutex, &ldata->output_lock)
2388: num = tty->ops->write(tty, b, nr);
2389: if (num < 0) {
2390: retval = num;
2391: goto break_out;
2392: }
2393: if (!num)
2394: break;
2395: b += num;
2396: nr -= num;
2397: }
2398: }
2399: if (!nr)
2400: break;
2401: if (tty_io_nonblock(tty, file)) {
2402: retval = -EAGAIN;
2403: break;
2404: }
2405: up_read(&tty->termios_rwsem);
2406:
2407: wait_woken(&wait, TASK_INTERRUPTIBLE, MAX_SCHEDULE_TIMEOUT);
2408:
2409: down_read(&tty->termios_rwsem);
2410: }
2411: break_out:
2412: remove_wait_queue(&tty->write_wait, &wait);
2413: if (nr && tty->fasync)
2414: set_bit(TTY_DO_WRITE_WAKEUP, &tty->flags);
2415:
2416: return (b - buf) ? b - buf : retval;
2417: }
SourceFile:drivers/tty/n_tty.c]
|
| 44/2 |
2026/05/21 14:16 |
llm |
expert |
0m
Model:
Tokens:
input: 60046
output: 39
thoughts: 172
Thoughts: **Analyzing the Concurrency in `n_tty_write`**
Okay, let's break this down. The code snippet shows `n_tty_write` handling the `termios_rwsem` semaphore. My initial thought was that it was holding the read lock on `termios_rwsem` during the wait period. The code explicitly shows, however, that the lock is released with `up_read` *before* `wait_woken` is called, and reacquired with `down_read` *after* the wait. So, `n_tty_write` *does not* hold `termios_rwsem` while it waits for space. That's good, minimizes contention.
But, and here's the crucial point: it *does* hold `termios_rwsem` while calling `tty->ops->write(tty, b, nr)`. Essentially, the `termios_rwsem` read lock protects the terminal's state when a write operation is in progress. The question then becomes whether that write operation, `tty->ops->write`, can itself block. If it can, we're back in a contention scenario, albeit in a different part of the system.
For a pseudo-terminal (PTY) – and that's the common case I'm most familiar with – the `tty->ops->write` function is `pty_write`. Now, `pty_write` has its own synchronization primitives and mechanisms for handling flow control and buffering to the controlling process, right? So, under what conditions does `pty_write` block? Does it hold any locks that could then block the `n_tty_write` process or other processes? I need to refresh my memory on the details of `pty_write` to fully understand the implications. The blocking behavior of that underlying call is the key.
|
| 45/2 |
2026/05/21 14:16 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/tty/pty.c IncludeLines:true Name:pty_write]
Results: map[SourceCode: 109: static ssize_t pty_write(struct tty_struct *tty, const u8 *buf, size_t c)
110: {
111: struct tty_struct *to = tty->link;
112:
113: if (tty->flow.stopped || !c)
114: return 0;
115:
116: return tty_insert_flip_string_and_push_buffer(to->port, buf, c);
117: }
SourceFile:drivers/tty/pty.c]
|
| 46/2 |
2026/05/21 14:16 |
llm |
expert |
1m
Model:
Tokens:
input: 60824
output: 43
thoughts: 6853
Thoughts: **Analysis of Potential Deadlock in TTY Driver**
Okay, so here's the breakdown of what I've been thinking about this TTY driver code. `pty_write` seems straightforward, just pushes data. `uart_write` copies data to a circular buffer, and that also seems non-blocking. The concern is really around the blocking behavior, especially if `process_output_block` somehow stalls. That calls `tty->ops->write`, so that part needs further inspection. I'm focusing on places that could hold locks for extended periods. `n_tty_write` itself doesn't appear to be the culprit as it doesn't block while holding `termios_rwsem`.
Now, `n_tty_read` is more interesting. We know it *can* return with `termios_rwsem` held if it sets a 'cookie', and this is crucial. This then means `iterate_tty_read` is called, which in turn calls `copy_to_iter`. This `copy_to_iter` is the critical point. Because `copy_to_iter` *can* block on a page fault! That's it!
So, the other places: `tty_ioctl` calls `tiocsti`, and that can call `tty_ldisc_ref_wait` and `tty_insert_flip_char`. These don't seem to block. Then `tty_mode_ioctl` calls `set_termios`, and the `wait_event_interruptible` call there doesn't hold the `termios_rwsem`. So, it seems like the *only* place where a thread can indefinitely block while holding `termios_rwsem` is in `copy_to_iter` within `iterate_tty_read`!
Okay, let's step through `tty_read`. `tty_read` acquires `ldisc_sem` (read) via `tty_ldisc_ref_wait`. Then, it calls `iterate_tty_read`. `iterate_tty_read` then calls the line discipline's `read` operation, specifically `n_tty_read`. `n_tty_read` does acquire the `termios_rwsem` (read). If there's data, it copies it into `kernel_buf`, and then `n_tty_read` *doesn't* copy it directly to user space! It copies it into `kbuf`, which is on the stack of `iterate_tty_read`. `kbuf` is limited to 64 bytes.
Now, `n_tty_read` copies to a kernel buffer first. It returns `size`, but does it release `termios_rwsem` on return? If the buffer is full, it breaks out of the loop and RELEASES `termios_rwsem`. However, if there's *more* data to copy than requested (and available in the tty buffer), it returns `true`. If more than 64 bytes are in the tty buffer, and the user has requested more, `copy_from_read_buf` copies 64 bytes, and it returns `true`. It goes to `more_to_be_read`, and it returns 64! And it LEAVES `termios_rwsem` and `atomic_read_lock` HELD. This is the crux.
Then `iterate_tty_read` gets `size = 64` and calls `copy_to_iter` which copies 64 bytes from the `kernel_buf` to the user buffer. Here's where it happens. This `copy_to_iter` *can* trigger a page fault! While it triggers a page fault, `termios_rwsem` and `atomic_read_lock` are still held because `n_tty_read` didn't release them!
So, `iterate_tty_read` calls `copy_to_iter` while it has `termios_rwsem` (read), `atomic_read_lock` and `ldisc_sem` held! It's a clever optimization to avoid dropping locks for every 64-byte chunk, but it means `copy_to_iter` can block indefinitely under the right conditions. This, I can see is a perfect fit for a userfaultfd or FUSE exploit!
Here, is what will happen, and how it can cause a local DoS: If `copy_to_iter` blocks, `termios_rwsem` is held for read. Another thread calls `ioctl(TCSETS)`, which triggers `tty_set_termios` and `down_write(&tty->termios_rwsem)`. This second thread will block indefinitely in `TASK_UNINTERRUPTIBLE`. This is
|
| 47/2 |
2026/05/21 14:18 |
tool |
codesearch-definition-source |
0m
Args: map[ContextFile:drivers/tty/tty_io.c IncludeLines:true Name:iterate_tty_write]
Results: map[SourceCode: 948: /*
949: * Split writes up in sane blocksizes to avoid
950: * denial-of-service type attacks
951: */
952: static ssize_t iterate_tty_write(struct tty_ldisc *ld, struct tty_struct *tty,
953: struct file *file, struct iov_iter *from)
954: {
955: size_t chunk, count = iov_iter_count(from);
956: ssize_t ret, written = 0;
957:
958: ret = tty_write_lock(tty, file->f_flags & O_NDELAY);
959: if (ret < 0)
960: return ret;
961:
962: /*
963: * We chunk up writes into a temporary buffer. This
964: * simplifies low-level drivers immensely, since they
965: * don't have locking issues and user mode accesses.
966: *
967: * But if TTY_NO_WRITE_SPLIT is set, we should use a
968: * big chunk-size..
969: *
970: * The default chunk-size is 2kB, because the NTTY
971: * layer has problems with bigger chunks. It will
972: * claim to be able to handle more characters than
973: * it actually does.
974: */
975: chunk = 2048;
976: if (test_bit(TTY_NO_WRITE_SPLIT, &tty->flags))
977: chunk = 65536;
978: if (count < chunk)
979: chunk = count;
980:
981: /* write_buf/write_cnt is protected by the atomic_write_lock mutex */
982: if (tty->write_cnt < chunk) {
983: u8 *buf_chunk;
984:
985: if (chunk < 1024)
986: chunk = 1024;
987:
988: buf_chunk = kvmalloc(chunk, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
989: if (!buf_chunk) {
990: ret = -ENOMEM;
991: goto out;
992: }
993: kvfree(tty->write_buf);
994: tty->write_cnt = chunk;
995: tty->write_buf = buf_chunk;
996: }
997:
998: /* Do the write .. */
999: for (;;) {
1000: size_t size = min(chunk, count);
1001:
1002: ret = -EFAULT;
1003: if (copy_from_iter(tty->write_buf, size, from) != size)
1004: break;
1005:
1006: ret = ld->ops->write(tty, file, tty->write_buf, size);
1007: if (ret <= 0)
1008: break;
1009:
1010: written += ret;
1011: if (ret > size)
1012: break;
1013:
1014: /* FIXME! Have Al check this! */
1015: if (ret != size)
1016: iov_iter_revert(from, size-ret);
1017:
1018: count -= ret;
1019: if (!count)
1020: break;
1021: ret = -ERESTARTSYS;
1022: if (signal_pending(current))
1023: break;
1024: cond_resched();
1025: }
1026: if (written) {
1027: tty_update_time(tty, true);
1028: ret = written;
1029: }
1030: out:
1031: tty_write_unlock(tty);
1032: return ret;
1033: }
SourceFile:drivers/tty/tty_io.c]
|
| 48/2 |
2026/05/21 14:18 |
llm |
expert |
0m
Model:
Tokens:
input: 69959
output: 66
thoughts: 879
Thoughts: **TTY Locking Analysis: Unkillable Task Vulnerability**
Okay, here's my breakdown of this TTY locking issue. It seems like the core problem lies within the interaction between read/write operations and termios settings.
Initially, I see that `iterate_tty_write` calls `copy_from_iter` *before* acquiring the `termios_rwsem` lock. This is important because it means `copy_from_iter` can block while only holding `atomic_write_lock`, a mutex. If `copy_from_iter` stalls indefinitely, it keeps that mutex locked.
I then looked for other code paths that contend for `atomic_write_lock`, and found that `set_termios`, which is invoked by functions like `ioctl(TCSETSW)` (with `TERMIOS_WAIT`), needs this lock as well.
The vulnerability shows itself because when Thread 1 gets stuck in `copy_from_iter`, it owns `atomic_write_lock`. Thread 2, attempting `ioctl(TCSETSW)`, gets blocked by `tty_write_lock`, waiting for that same mutex. Crucially, this is an interruptible wait, meaning Thread 2 is killable, and doesn't hold critical locks like `termios_rwsem` during the wait. It *does* hold `ldisc_sem` (read) because `tty_ioctl` holds it while calling `set_termios`.
Now, if Thread 3 calls `tty_set_ldisc`, it will block on `ldisc_sem` (write). However, since Thread 2 is killable, this is not a hard lockup! This is important because Thread 1 is also killable.
The true problem surfaces when analyzing `iterate_tty_read`. This function holds `termios_rwsem` (read) and `atomic_read_lock` *across* a call to `copy_to_iter`. Here's the kicker: `tty_set_termios` (used to change terminal settings) uses `down_write(&tty->termios_rwsem)`, which is an *uninterruptible* lock!
Therefore, the key point is that `iterate_tty_read`'s hold on `termios_rwsem` across the potentially blocking `copy_to_iter` creates the potential for `tty_set_termios` to become an unkillable task.
My conclusions are thus:
* **Exploitable: False** (though it creates a denial of service).
* **DenialOfService: True** (due to the unkillable task)
* **Unprivileged: True** (accessible by unprivileged processes)
* **UserNamespace: True** (accessible from user namespaces)
* **VMGuestTrigger: True** (can be triggered from a guest)
* **VMHostTrigger: False** (not triggered from the host)
* **NetworkTrigger: False**
* **RemoteTrigger: False**
* **PeripheralTrigger: False**
* **FilesystemTrigger: False** (while a malicious filesystem *could* trigger the blocking condition, the *root cause* is in the TTY subsystem itself, not the filesystem parsing).
My analysis is solid. The primary vulnerability stems from the uninterruptible nature of `tty_set_termios`'s `termios_rwsem` acquisition when called from `iterate_tty_read` while a read operation can block on `copy_to_iter`.
Now, I can call `set-results` with this information.
|
| 49/2 |
2026/05/21 14:18 |
tool |
set-results |
0m
Args: map[DenialOfService:true Exploitable:false FilesystemTrigger:false NetworkTrigger:false PeripheralTrigger:false RemoteTrigger:false Unprivileged:true UserNamespace:true VMGuestTrigger:true VMHostTrigger:false]
Results: map[DenialOfService:true Exploitable:false FilesystemTrigger:false NetworkTrigger:false PeripheralTrigger:false RemoteTrigger:false Unprivileged:true UserNamespace:true VMGuestTrigger:true VMHostTrigger:false]
|
| 50/2 |
2026/05/21 14:18 |
llm |
expert |
0m
Model:
Tokens:
input: 71738
output: 758
thoughts: 471
Thoughts: **My Analysis of the TTY Deadlock**
Alright, the analysis is done, and the results are clear. Now, I need to articulate this deadlock scenario precisely. Here's how I see it, step-by-step:
It starts in `tty_read()`, where `ldisc_sem` is acquired for a read operation, and then `iterate_tty_read()` is invoked. This in turn calls `ld->ops->read()`, which resolves to `n_tty_read()`. This function is where things start to get interesting.
`n_tty_read()` grabs `atomic_read_lock` and `termios_rwsem` for reading, which is standard. But here's the kicker: if the data available exceeds the small 64-byte `kernel_buf` buffer within `iterate_tty_read()`, `n_tty_read()` sets a "cookie" and returns, *deliberately* leaving those locks – `atomic_read_lock` and `termios_rwsem` – held.
`iterate_tty_read()` then proceeds to call `copy_to_iter()` to transfer that 64 bytes to user space. This is where we run into the root of the issue.
If the user-space buffer being written to is backed by something like `userfaultfd` or a FUSE filesystem, `copy_to_iter()` can block indefinitely, and, crucially, while blocking, the thread *still* holds the `termios_rwsem` (read lock), `atomic_read_lock`, and `ldisc_sem` (read lock).
Meanwhile, another thread, acting independently, issues an `ioctl(TCSETS)` command on the same TTY device. This eventually calls `tty_set_termios()`. Here is where things unravel.
`tty_set_termios()` wants to modify the terminal settings, and therefore attempts to acquire `termios_rwsem` for *write* access using `down_write()`. But this is the catch: Since the first thread holds `termios_rwsem` in read mode (because it is still blocked in `copy_to_iter()`), `down_write()` puts the second thread into an *uninterruptible* sleep state (the dreaded D-state, `TASK_UNINTERRUPTIBLE`). This occurs because it is blocked waiting for a lock held by a different thread.
This creates a deadlock, an unkillable task. This will eventually lead to either a hung task panic, if the kernel is configured to do so, or a local denial of service via resource exhaustion, as the system leaks task structures, kernel stacks, and PID space.
Essentially, this is a classic "holding locks across `copy_to_user`" vulnerability that allows an unprivileged user to trivially cause a local DoS condition, demonstrating a clear security risk.
|
| 51/1 |
2026/05/21 14:19 |
action |
format-explanation |
0m
Results: map[Explanation:Based on the analysis of the kernel bug report and the source code, here is the
assessment of the vulnerability:
### Bug Mechanism
The bug is a local Denial of Service (DoS) caused by holding a read-write
semaphore (`termios_rwsem`) and a mutex (`atomic_read_lock`) across a user-space
memory copy (`copy_to_iter`), which can be intentionally stalled by an
unprivileged user.
1. When a user reads from a TTY (e.g., a pseudo-terminal), `tty_read()` acquires
`ldisc_sem` for read and calls `iterate_tty_read()`.
2. `iterate_tty_read()` repeatedly calls the line discipline's read function
(`n_tty_read()`) to fetch data into a small 64-byte stack buffer (`kernel_buf`).
3. `n_tty_read()` acquires `atomic_read_lock` and `termios_rwsem` (for read). If
there is more data available than what fits in the 64-byte buffer, it sets a
`cookie` and returns to the caller **without releasing `atomic_read_lock` and
`termios_rwsem`** (this is an optimization to avoid lock bouncing).
4. `iterate_tty_read()` then calls `copy_to_iter()` to copy the 64 bytes from
`kernel_buf` to the user-provided buffer.
5. If the user-provided buffer is backed by `userfaultfd` or a FUSE filesystem,
the unprivileged user can intentionally block the page fault indefinitely. While
blocked, the thread continues to hold `termios_rwsem` (read),
`atomic_read_lock`, and `ldisc_sem` (read).
6. The user can then spawn a second thread and call `ioctl(TCSETS)` on the same
TTY. This eventually calls `tty_set_termios()`, which attempts to acquire
`termios_rwsem` for write using `down_write()`.
7. Because `down_write()` puts the task into an **uninterruptible** sleep
(`TASK_UNINTERRUPTIBLE` / D-state), the second thread becomes completely
unkillable (immune to `SIGKILL`).
### Security Impact
* **Exploitable:** No. This is a locking issue that leads to a deadlock/hang,
not a memory corruption vulnerability. It cannot be used for privilege
escalation or arbitrary code execution.
* **Denial Of Service:** Yes. An unprivileged user can repeatedly exploit this
to create thousands of unkillable D-state tasks. This exhausts the system's PID
space, kernel stacks, and memory, eventually locking up the system. Furthermore,
if `kernel.hung_task_panic` is enabled, this will immediately crash the system.
* **Accessible From Unprivileged Processes:** Yes. Any unprivileged user can
create a PTY pair, write data to the master, and read from the slave into a
FUSE/userfaultfd buffer to trigger the hang. No special capabilities are
required.
* **Accessible From User Namespaces:** Yes, for the same reasons as above.
* **VM Guest Trigger:** Yes, an unprivileged user inside a VM guest can trigger
this to DoS the guest kernel.
### Conclusion
This is a local Denial of Service vulnerability accessible to unprivileged
users. The root cause is the anti-pattern of holding kernel locks (specifically
`termios_rwsem` and `atomic_read_lock`) while performing a potentially blocking
copy to user space (`copy_to_iter`).]
|