* [PATCH] net: caif: serial: fix TX UAF on ser->tty
@ 2026-02-12 4:22 Shuangpeng Bai
2026-02-12 12:13 ` Paolo Abeni
0 siblings, 1 reply; 2+ messages in thread
From: Shuangpeng Bai @ 2026-02-12 4:22 UTC (permalink / raw)
To: netdev
Cc: andrew+netdev, davem, edumazet, kuba, pabeni, linux-kernel,
Shuangpeng Bai, Shuangpeng Bai
KASAN reported a slab-use-after-free in tty_write_room() reachable from
caif_serial's TX path. The TX handler dereferences ser->tty while
ldisc_close() can drop the driver's tty reference. Since ser->tty was
not cleared and accesses were not synchronized, the TX path could race
with tty teardown and dereference a stale ser->tty pointer.
Fix it by serializing accesses to ser->tty with a dedicated lock. The TX
path grabs a tty kref under the lock and drops it after the TX attempt,
while ldisc_close() clears ser->tty under the same lock before putting
the old tty reference. This prevents the TX path from observing a freed
tty object via ser->tty.
Reported-by: Shuangpeng Bai <baisp@psu.edu>
Closes: https://groups.google.com/g/syzkaller/c/usNe0oKtoXw/m/x8qUc3yUAQAJ
Signed-off-by: Shuangpeng Bai <shuangpeng.kernel@gmail.com>
---
drivers/net/caif/caif_serial.c | 22 ++++++++++++++++++++--
1 file changed, 20 insertions(+), 2 deletions(-)
diff --git a/drivers/net/caif/caif_serial.c b/drivers/net/caif/caif_serial.c
index c398ac42eae9..fd1213685a89 100644
--- a/drivers/net/caif/caif_serial.c
+++ b/drivers/net/caif/caif_serial.c
@@ -68,6 +68,7 @@ struct ser_device {
struct net_device *dev;
struct sk_buff_head head;
struct tty_struct *tty;
+ spinlock_t tty_lock; /* protects ser->tty */
bool tx_started;
unsigned long state;
#ifdef CONFIG_DEBUG_FS
@@ -197,12 +198,21 @@ static int handle_tx(struct ser_device *ser)
struct sk_buff *skb;
int tty_wr, len, room;
+ spin_lock(&ser->tty_lock);
tty = ser->tty;
+ tty_kref_get(tty);
+ spin_unlock(&ser->tty_lock);
+
+ if (!tty)
+ return 0;
+
ser->tx_started = true;
/* Enter critical section */
- if (test_and_set_bit(CAIF_SENDING, &ser->state))
+ if (test_and_set_bit(CAIF_SENDING, &ser->state)) {
+ tty_kref_put(tty);
return 0;
+ }
/* skb_peek is safe because handle_tx is called after skb_queue_tail */
while ((skb = skb_peek(&ser->head)) != NULL) {
@@ -245,9 +255,11 @@ static int handle_tx(struct ser_device *ser)
ser->common.flowctrl != NULL)
ser->common.flowctrl(ser->dev, ON);
clear_bit(CAIF_SENDING, &ser->state);
+ tty_kref_put(tty);
return 0;
error:
clear_bit(CAIF_SENDING, &ser->state);
+ tty_kref_put(tty);
return tty_wr;
}
@@ -327,6 +339,7 @@ static int ldisc_open(struct tty_struct *tty)
return -ENOMEM;
ser = netdev_priv(dev);
+ spin_lock_init(&ser->tty_lock);
ser->tty = tty_kref_get(tty);
ser->dev = dev;
debugfs_init(ser, tty);
@@ -354,8 +367,13 @@ static int ldisc_open(struct tty_struct *tty)
static void ldisc_close(struct tty_struct *tty)
{
struct ser_device *ser = tty->disc_data;
+ struct tty_struct *old;
- tty_kref_put(ser->tty);
+ spin_lock(&ser->tty_lock);
+ old = ser->tty;
+ ser->tty = NULL;
+ spin_unlock(&ser->tty_lock);
+ tty_kref_put(old);
spin_lock(&ser_lock);
list_move(&ser->node, &ser_release_list);
--
2.34.1
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] net: caif: serial: fix TX UAF on ser->tty
2026-02-12 4:22 [PATCH] net: caif: serial: fix TX UAF on ser->tty Shuangpeng Bai
@ 2026-02-12 12:13 ` Paolo Abeni
0 siblings, 0 replies; 2+ messages in thread
From: Paolo Abeni @ 2026-02-12 12:13 UTC (permalink / raw)
To: Shuangpeng Bai, netdev
Cc: andrew+netdev, davem, edumazet, kuba, linux-kernel,
Shuangpeng Bai
On 2/12/26 5:22 AM, Shuangpeng Bai wrote:
> KASAN reported a slab-use-after-free in tty_write_room() reachable from
> caif_serial's TX path. The TX handler dereferences ser->tty while
> ldisc_close() can drop the driver's tty reference. Since ser->tty was
> not cleared and accesses were not synchronized, the TX path could race
> with tty teardown and dereference a stale ser->tty pointer.
>
> Fix it by serializing accesses to ser->tty with a dedicated lock. The TX
> path grabs a tty kref under the lock and drops it after the TX attempt,
> while ldisc_close() clears ser->tty under the same lock before putting
> the old tty reference. This prevents the TX path from observing a freed
> tty object via ser->tty.
>
> Reported-by: Shuangpeng Bai <baisp@psu.edu>
> Closes: https://groups.google.com/g/syzkaller/c/usNe0oKtoXw/m/x8qUc3yUAQAJ
>
Please, no empty lines in the tag area.
You must include a fixes tag and specify the target tree in the subj prefix
Does not apply cleanly to net nor net-next
Please read carefully the process documentation under:
Documentation/process/
and especially Documentation/process/maintainer-netdev.rst
before resubmitting
/P
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2026-02-12 12:13 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-02-12 4:22 [PATCH] net: caif: serial: fix TX UAF on ser->tty Shuangpeng Bai
2026-02-12 12:13 ` Paolo Abeni
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox