Skip to content
This repository was archived by the owner on Oct 31, 2024. It is now read-only.

Commit f72e3d8

Browse files
Xie Hegregkh
authored andcommitted
net: lapbether: Prevent racing when checking whether the netif is running
[ Upstream commit 5acd0cf ] 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> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 7cc0ba6 commit f72e3d8

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

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);
@@ -98,8 +100,9 @@ static int lapbeth_rcv(struct sk_buff *skb, struct net_device *dev, struct packe
98100
rcu_read_lock();
99101
lapbeth = lapbeth_get_x25_dev(dev);
100102
if (!lapbeth)
101-
goto drop_unlock;
102-
if (!netif_running(lapbeth->axdev))
103+
goto drop_unlock_rcu;
104+
spin_lock_bh(&lapbeth->up_lock);
105+
if (!lapbeth->up)
103106
goto drop_unlock;
104107

105108
len = skb->data[0] + skb->data[1] * 256;
@@ -114,11 +117,14 @@ static int lapbeth_rcv(struct sk_buff *skb, struct net_device *dev, struct packe
114117
goto drop_unlock;
115118
}
116119
out:
120+
spin_unlock_bh(&lapbeth->up_lock);
117121
rcu_read_unlock();
118122
return 0;
119123
drop_unlock:
120124
kfree_skb(skb);
121125
goto out;
126+
drop_unlock_rcu:
127+
rcu_read_unlock();
122128
drop:
123129
kfree_skb(skb);
124130
return 0;
@@ -148,13 +154,11 @@ static int lapbeth_data_indication(struct net_device *dev, struct sk_buff *skb)
148154
static netdev_tx_t lapbeth_xmit(struct sk_buff *skb,
149155
struct net_device *dev)
150156
{
157+
struct lapbethdev *lapbeth = netdev_priv(dev);
151158
int err;
152159

153-
/*
154-
* Just to be *really* sure not to send anything if the interface
155-
* is down, the ethernet device may have gone.
156-
*/
157-
if (!netif_running(dev))
160+
spin_lock_bh(&lapbeth->up_lock);
161+
if (!lapbeth->up)
158162
goto drop;
159163

160164
/* There should be a pseudo header of 1 byte added by upper layers.
@@ -185,6 +189,7 @@ static netdev_tx_t lapbeth_xmit(struct sk_buff *skb,
185189
goto drop;
186190
}
187191
out:
192+
spin_unlock_bh(&lapbeth->up_lock);
188193
return NETDEV_TX_OK;
189194
drop:
190195
kfree_skb(skb);
@@ -276,20 +281,30 @@ static const struct lapb_register_struct lapbeth_callbacks = {
276281
*/
277282
static int lapbeth_open(struct net_device *dev)
278283
{
284+
struct lapbethdev *lapbeth = netdev_priv(dev);
279285
int err;
280286

281287
if ((err = lapb_register(dev, &lapbeth_callbacks)) != LAPB_OK) {
282288
pr_err("lapb_register error: %d\n", err);
283289
return -ENODEV;
284290
}
285291

292+
spin_lock_bh(&lapbeth->up_lock);
293+
lapbeth->up = true;
294+
spin_unlock_bh(&lapbeth->up_lock);
295+
286296
return 0;
287297
}
288298

289299
static int lapbeth_close(struct net_device *dev)
290300
{
301+
struct lapbethdev *lapbeth = netdev_priv(dev);
291302
int err;
292303

304+
spin_lock_bh(&lapbeth->up_lock);
305+
lapbeth->up = false;
306+
spin_unlock_bh(&lapbeth->up_lock);
307+
293308
if ((err = lapb_unregister(dev)) != LAPB_OK)
294309
pr_err("lapb_unregister error: %d\n", err);
295310

@@ -347,6 +362,9 @@ static int lapbeth_new_device(struct net_device *dev)
347362
dev_hold(dev);
348363
lapbeth->ethdev = dev;
349364

365+
lapbeth->up = false;
366+
spin_lock_init(&lapbeth->up_lock);
367+
350368
rc = -EIO;
351369
if (register_netdevice(ndev))
352370
goto fail;

0 commit comments

Comments
 (0)