Skip to content

Commit 40b8fe4

Browse files
vyasevichdavem330
authored andcommitted
macvtap: Fix race between device delete and open.
In macvtap device delete and open calls can race and this causes a list curruption of the vlan queue_list. The race intself is triggered by the idr accessors that located the vlan device. The device is stored into and removed from the idr under both an rtnl and a mutex. However, when attempting to locate the device in idr, only a mutex is taken. As a result, once cpu perfoming a delete may take an rtnl and wait for the mutex, while another cput doing an open() will take the idr mutex first to fetch the device pointer and later take an rtnl to add a queue for the device which may have just gotten deleted. With this patch, we now hold the rtnl for the duration of the macvtap_open() call thus making sure that open will not race with delete. CC: Michael S. Tsirkin <mst@redhat.com> CC: Jason Wang <jasowang@redhat.com> Signed-off-by: Vladislav Yasevich <vyasevic@redhat.com> Acked-by: Jason Wang <jasowang@redhat.com> Acked-by: Michael S. Tsirkin <mst@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 2b07f0d commit 40b8fe4

1 file changed

Lines changed: 8 additions & 10 deletions

File tree

‎drivers/net/macvtap.c‎

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -112,17 +112,15 @@ static int macvtap_enable_queue(struct net_device *dev, struct file *file,
112112
return err;
113113
}
114114

115+
/* Requires RTNL */
115116
static int macvtap_set_queue(struct net_device *dev, struct file *file,
116117
struct macvtap_queue *q)
117118
{
118119
struct macvlan_dev *vlan = netdev_priv(dev);
119-
int err = -EBUSY;
120120

121-
rtnl_lock();
122121
if (vlan->numqueues == MAX_MACVTAP_QUEUES)
123-
goto out;
122+
return -EBUSY;
124123

125-
err = 0;
126124
rcu_assign_pointer(q->vlan, vlan);
127125
rcu_assign_pointer(vlan->taps[vlan->numvtaps], q);
128126
sock_hold(&q->sk);
@@ -136,9 +134,7 @@ static int macvtap_set_queue(struct net_device *dev, struct file *file,
136134
vlan->numvtaps++;
137135
vlan->numqueues++;
138136

139-
out:
140-
rtnl_unlock();
141-
return err;
137+
return 0;
142138
}
143139

144140
static int macvtap_disable_queue(struct macvtap_queue *q)
@@ -454,11 +450,12 @@ static void macvtap_sock_destruct(struct sock *sk)
454450
static int macvtap_open(struct inode *inode, struct file *file)
455451
{
456452
struct net *net = current->nsproxy->net_ns;
457-
struct net_device *dev = dev_get_by_macvtap_minor(iminor(inode));
453+
struct net_device *dev;
458454
struct macvtap_queue *q;
459-
int err;
455+
int err = -ENODEV;
460456

461-
err = -ENODEV;
457+
rtnl_lock();
458+
dev = dev_get_by_macvtap_minor(iminor(inode));
462459
if (!dev)
463460
goto out;
464461

@@ -498,6 +495,7 @@ static int macvtap_open(struct inode *inode, struct file *file)
498495
if (dev)
499496
dev_put(dev);
500497

498+
rtnl_unlock();
501499
return err;
502500
}
503501

0 commit comments

Comments
 (0)