Skip to content

Commit

Permalink
virtio: console: fix race with port unplug and open/close
Browse files Browse the repository at this point in the history
There's a window between find_port_by_devt() returning a port and us
taking a kref on the port, where the port could get unplugged.  Fix it
by taking the reference in find_port_by_devt() itself.

Problem reported and analyzed by Mateusz Guzik.

CC: <[email protected]>
Reported-by: Mateusz Guzik <[email protected]>
Signed-off-by: Amit Shah <[email protected]>
Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
Amit Shah authored and rustyrussell committed Jul 29, 2013
1 parent 2b4fbf0 commit 057b82b
Showing 1 changed file with 6 additions and 7 deletions.
13 changes: 6 additions & 7 deletions drivers/char/virtio_console.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,9 +272,12 @@ static struct port *find_port_by_devt_in_portdev(struct ports_device *portdev,
unsigned long flags;

spin_lock_irqsave(&portdev->ports_lock, flags);
list_for_each_entry(port, &portdev->ports, list)
if (port->cdev->dev == dev)
list_for_each_entry(port, &portdev->ports, list) {
if (port->cdev->dev == dev) {
kref_get(&port->kref);
goto out;
}
}
port = NULL;
out:
spin_unlock_irqrestore(&portdev->ports_lock, flags);
Expand Down Expand Up @@ -1036,14 +1039,10 @@ static int port_fops_open(struct inode *inode, struct file *filp)
struct port *port;
int ret;

/* We get the port with a kref here */
port = find_port_by_devt(cdev->dev);
filp->private_data = port;

/* Prevent against a port getting hot-unplugged at the same time */
spin_lock_irq(&port->portdev->ports_lock);
kref_get(&port->kref);
spin_unlock_irq(&port->portdev->ports_lock);

/*
* Don't allow opening of console port devices -- that's done
* via /dev/hvc
Expand Down

0 comments on commit 057b82b

Please sign in to comment.