Skip to content

Commit 5acd0cf

Browse files
Xie Hedavem330
authored andcommitted
net: lapbether: Prevent racing when checking whether the netif is running
There are two "netif_running" checks in this driver. One is in "lapbeth_xmit" and the other is in "lapbeth_rcv". They serve to make sure that the LAPB APIs called in these functions are called before "lapb_unregister" is called by the "ndo_stop" function. However, these "netif_running" checks are unreliable, because it's possible that immediately after "netif_running" returns true, "ndo_stop" is called (which causes "lapb_unregister" to be called). This patch adds locking to make sure "lapbeth_xmit" and "lapbeth_rcv" can reliably check and ensure the netif is running while doing their work. Fixes: 1da177e ("Linux-2.6.12-rc2") Signed-off-by: Xie He <xie.he.0141@gmail.com> Acked-by: Martin Schiller <ms@dev.tdt.de> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 3f9c066 commit 5acd0cf

1 file changed

Lines changed: 25 additions & 7 deletions

File tree

‎drivers/net/wan/lapbether.c‎

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ struct lapbethdev {
5151
struct list_head node;
5252
struct net_device *ethdev; /* link to ethernet device */
5353
struct net_device *axdev; /* lapbeth device (lapb#) */
54+
bool up;
55+
spinlock_t up_lock; /* Protects "up" */
5456
};
5557

5658
static LIST_HEAD(lapbeth_devices);
@@ -101,8 +103,9 @@ static int lapbeth_rcv(struct sk_buff *skb, struct net_device *dev, struct packe
101103
rcu_read_lock();
102104
lapbeth = lapbeth_get_x25_dev(dev);
103105
if (!lapbeth)
104-
goto drop_unlock;
105-
if (!netif_running(lapbeth->axdev))
106+
goto drop_unlock_rcu;
107+
spin_lock_bh(&lapbeth->up_lock);
108+
if (!lapbeth->up)
106109
goto drop_unlock;
107110

108111
len = skb->data[0] + skb->data[1] * 256;
@@ -117,11 +120,14 @@ static int lapbeth_rcv(struct sk_buff *skb, struct net_device *dev, struct packe
117120
goto drop_unlock;
118121
}
119122
out:
123+
spin_unlock_bh(&lapbeth->up_lock);
120124
rcu_read_unlock();
121125
return 0;
122126
drop_unlock:
123127
kfree_skb(skb);
124128
goto out;
129+
drop_unlock_rcu:
130+
rcu_read_unlock();
125131
drop:
126132
kfree_skb(skb);
127133
return 0;
@@ -151,13 +157,11 @@ static int lapbeth_data_indication(struct net_device *dev, struct sk_buff *skb)
151157
static netdev_tx_t lapbeth_xmit(struct sk_buff *skb,
152158
struct net_device *dev)
153159
{
160+
struct lapbethdev *lapbeth = netdev_priv(dev);
154161
int err;
155162

156-
/*
157-
* Just to be *really* sure not to send anything if the interface
158-
* is down, the ethernet device may have gone.
159-
*/
160-
if (!netif_running(dev))
163+
spin_lock_bh(&lapbeth->up_lock);
164+
if (!lapbeth->up)
161165
goto drop;
162166

163167
/* There should be a pseudo header of 1 byte added by upper layers.
@@ -194,6 +198,7 @@ static netdev_tx_t lapbeth_xmit(struct sk_buff *skb,
194198
goto drop;
195199
}
196200
out:
201+
spin_unlock_bh(&lapbeth->up_lock);
197202
return NETDEV_TX_OK;
198203
drop:
199204
kfree_skb(skb);
@@ -285,20 +290,30 @@ static const struct lapb_register_struct lapbeth_callbacks = {
285290
*/
286291
static int lapbeth_open(struct net_device *dev)
287292
{
293+
struct lapbethdev *lapbeth = netdev_priv(dev);
288294
int err;
289295

290296
if ((err = lapb_register(dev, &lapbeth_callbacks)) != LAPB_OK) {
291297
pr_err("lapb_register error: %d\n", err);
292298
return -ENODEV;
293299
}
294300

301+
spin_lock_bh(&lapbeth->up_lock);
302+
lapbeth->up = true;
303+
spin_unlock_bh(&lapbeth->up_lock);
304+
295305
return 0;
296306
}
297307

298308
static int lapbeth_close(struct net_device *dev)
299309
{
310+
struct lapbethdev *lapbeth = netdev_priv(dev);
300311
int err;
301312

313+
spin_lock_bh(&lapbeth->up_lock);
314+
lapbeth->up = false;
315+
spin_unlock_bh(&lapbeth->up_lock);
316+
302317
if ((err = lapb_unregister(dev)) != LAPB_OK)
303318
pr_err("lapb_unregister error: %d\n", err);
304319

@@ -356,6 +371,9 @@ static int lapbeth_new_device(struct net_device *dev)
356371
dev_hold(dev);
357372
lapbeth->ethdev = dev;
358373

374+
lapbeth->up = false;
375+
spin_lock_init(&lapbeth->up_lock);
376+
359377
rc = -EIO;
360378
if (register_netdevice(ndev))
361379
goto fail;

0 commit comments

Comments
 (0)