Skip to contentSkip to conversation

tty: n_gsm: fix a possible data race in gsmtty_open

PATCH In review Tuo Li 2 participants 2 messages Jul 4, 2024 TTY layer and serial drivers

lore
Share & tools
lore

Tuo Li

author
#0 lore raw

PATCH 1/1 tty: n_gsm: fix a possible data race in gsmtty_open

In the function gsmtty_open(), the counter port->count is updated without holding the lock port->lock, which may lead to a data race.

This possible data race is found by an experimental static analysis tool. This tool analyzes the locking APIs to deduce locking rules about which variable should be protected by which lock. And then the tool checks whether a given variable access violates the deduced locking rules, to detect data races. The above possible data race is reported, when the tool analyzes the source code of Linux 6.2.

Referring to the function tty_port_open() in tty_port.c:
spin_lock_irq(&port->lock);
++port->count;
spin_unlock_irq(&port->lock);

we add a lock/unlock pair to protect the access to port->count, to fix this possible data race.

Fixes 86176ed90545 ("TTY: n_gsm, use tty_port_install")
---
 drivers/tty/n_gsm.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/tty/n_gsm.c +2 -0
index be35f7334ecd..b709f1a6bd2d 100644
@@ -4307,7 +4307,9 @@ static int gsmtty_open(struct tty_struct *tty, struct file *filp)
 	struct gsm_dlci *dlci = tty->driver_data;
 	struct tty_port *port = &dlci->port;
 
+	spin_lock_irq(&port->lock);
 	port->count++;
+	spin_unlock_irq(&port->lock);
 	tty_port_tty_set(port, tty);
 
 	dlci->modem_rx = 0;
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 29 lines · Tuo Li

In the function gsmtty_open(), the counter port->count is updated without holding the lock port->lock, which may lead to a data race.

This possible data race is found by an experimental static analysis tool. This tool analyzes the locking APIs to deduce locking rules about which variable should be protected by which lock. And then the tool checks whether a given variable access violates the deduced locking rules, to detect data races. The above possible data race is reported, when the tool analyzes the source code of Linux 6.2.

Referring to the function tty_port_open() in tty_port.c:
spin_lock_irq(&port->lock);
++port->count;
spin_unlock_irq(&port->lock);

we add a lock/unlock pair to protect the access to port->count, to fix this possible data race.

Fixes 86176ed90545 ("TTY: n_gsm, use tty_port_install")
---
 drivers/tty/n_gsm.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/tty/n_gsm.c +0 -0
index be35f7334ecd..b709f1a6bd2d 100644

drivers/tty/n_gsm.c static int gsmtty_open(struct tty_struct *tty, struct file *filp) · line 4312

Quoted 6 lines
drivers/tty/n_gsm.c +2 -0
@@ -4307,7 +4307,9 @@ static int gsmtty_open(struct tty_struct *tty, struct file *filp)
 	struct gsm_dlci *dlci = tty->driver_data;
 	struct tty_port *port = &dlci->port;
 
+	spin_lock_irq(&port->lock);
 	port->count++;
+	spin_unlock_irq(&port->lock);

Open counts are almost never a good idea, especially like this. Please fix to use a proper reference count instead which will fix this issue.

Also, you mention kernel 6.2, which is VERY old, especially for this driver, please redo this against the latest kernel tree which has lots and lots of fixes for this code in it since that obsolete release.

And finally, as you are using an automated tool, you need to describe it better, see the documentation for how to do so.

thanks,

greg k-h