Skip to content

Commit 501c8f5

Browse files
committed
Merge branch 'ax25-fixes'
Duoming Zhou says: ==================== ax25: fix NPD and UAF bugs when detaching ax25 device There are NPD and UAF bugs when detaching ax25 device, we use lock and refcount to mitigate these bugs. ==================== Signed-off-by: David S. Miller <davem@davemloft.net>
2 parents 928d6fe + d01ffb9 commit 501c8f5

File tree

4 files changed

+31
-5
lines changed

4 files changed

+31
-5
lines changed

include/net/ax25.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ typedef struct ax25_dev {
239239
#if defined(CONFIG_AX25_DAMA_SLAVE) || defined(CONFIG_AX25_DAMA_MASTER)
240240
ax25_dama_info dama;
241241
#endif
242+
refcount_t refcount;
242243
} ax25_dev;
243244

244245
typedef struct ax25_cb {
@@ -293,6 +294,15 @@ static __inline__ void ax25_cb_put(ax25_cb *ax25)
293294
}
294295
}
295296

297+
#define ax25_dev_hold(__ax25_dev) \
298+
refcount_inc(&((__ax25_dev)->refcount))
299+
300+
static __inline__ void ax25_dev_put(ax25_dev *ax25_dev)
301+
{
302+
if (refcount_dec_and_test(&ax25_dev->refcount)) {
303+
kfree(ax25_dev);
304+
}
305+
}
296306
static inline __be16 ax25_type_trans(struct sk_buff *skb, struct net_device *dev)
297307
{
298308
skb->dev = dev;

net/ax25/af_ax25.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ static void ax25_kill_by_device(struct net_device *dev)
7777
{
7878
ax25_dev *ax25_dev;
7979
ax25_cb *s;
80+
struct sock *sk;
8081

8182
if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL)
8283
return;
@@ -85,13 +86,16 @@ static void ax25_kill_by_device(struct net_device *dev)
8586
again:
8687
ax25_for_each(s, &ax25_list) {
8788
if (s->ax25_dev == ax25_dev) {
89+
sk = s->sk;
90+
sock_hold(sk);
8891
spin_unlock_bh(&ax25_list_lock);
89-
lock_sock(s->sk);
92+
lock_sock(sk);
9093
s->ax25_dev = NULL;
91-
release_sock(s->sk);
94+
ax25_dev_put(ax25_dev);
95+
release_sock(sk);
9296
ax25_disconnect(s, ENETUNREACH);
9397
spin_lock_bh(&ax25_list_lock);
94-
98+
sock_put(sk);
9599
/* The entry could have been deleted from the
96100
* list meanwhile and thus the next pointer is
97101
* no longer valid. Play it safe and restart
@@ -436,6 +440,7 @@ static int ax25_ctl_ioctl(const unsigned int cmd, void __user *arg)
436440
}
437441

438442
out_put:
443+
ax25_dev_put(ax25_dev);
439444
ax25_cb_put(ax25);
440445
return ret;
441446

net/ax25/ax25_dev.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ ax25_dev *ax25_addr_ax25dev(ax25_address *addr)
3737
for (ax25_dev = ax25_dev_list; ax25_dev != NULL; ax25_dev = ax25_dev->next)
3838
if (ax25cmp(addr, (const ax25_address *)ax25_dev->dev->dev_addr) == 0) {
3939
res = ax25_dev;
40+
ax25_dev_hold(ax25_dev);
4041
}
4142
spin_unlock_bh(&ax25_dev_lock);
4243

@@ -56,6 +57,7 @@ void ax25_dev_device_up(struct net_device *dev)
5657
return;
5758
}
5859

60+
refcount_set(&ax25_dev->refcount, 1);
5961
dev->ax25_ptr = ax25_dev;
6062
ax25_dev->dev = dev;
6163
dev_hold_track(dev, &ax25_dev->dev_tracker, GFP_ATOMIC);
@@ -83,6 +85,7 @@ void ax25_dev_device_up(struct net_device *dev)
8385
spin_lock_bh(&ax25_dev_lock);
8486
ax25_dev->next = ax25_dev_list;
8587
ax25_dev_list = ax25_dev;
88+
ax25_dev_hold(ax25_dev);
8689
spin_unlock_bh(&ax25_dev_lock);
8790

8891
ax25_register_dev_sysctl(ax25_dev);
@@ -112,27 +115,30 @@ void ax25_dev_device_down(struct net_device *dev)
112115

113116
if ((s = ax25_dev_list) == ax25_dev) {
114117
ax25_dev_list = s->next;
118+
ax25_dev_put(ax25_dev);
115119
spin_unlock_bh(&ax25_dev_lock);
116120
dev->ax25_ptr = NULL;
117121
dev_put_track(dev, &ax25_dev->dev_tracker);
118-
kfree(ax25_dev);
122+
ax25_dev_put(ax25_dev);
119123
return;
120124
}
121125

122126
while (s != NULL && s->next != NULL) {
123127
if (s->next == ax25_dev) {
124128
s->next = ax25_dev->next;
129+
ax25_dev_put(ax25_dev);
125130
spin_unlock_bh(&ax25_dev_lock);
126131
dev->ax25_ptr = NULL;
127132
dev_put_track(dev, &ax25_dev->dev_tracker);
128-
kfree(ax25_dev);
133+
ax25_dev_put(ax25_dev);
129134
return;
130135
}
131136

132137
s = s->next;
133138
}
134139
spin_unlock_bh(&ax25_dev_lock);
135140
dev->ax25_ptr = NULL;
141+
ax25_dev_put(ax25_dev);
136142
}
137143

138144
int ax25_fwd_ioctl(unsigned int cmd, struct ax25_fwd_struct *fwd)
@@ -149,6 +155,7 @@ int ax25_fwd_ioctl(unsigned int cmd, struct ax25_fwd_struct *fwd)
149155
if (ax25_dev->forward != NULL)
150156
return -EINVAL;
151157
ax25_dev->forward = fwd_dev->dev;
158+
ax25_dev_put(fwd_dev);
152159
break;
153160

154161
case SIOCAX25DELFWD:
@@ -161,6 +168,7 @@ int ax25_fwd_ioctl(unsigned int cmd, struct ax25_fwd_struct *fwd)
161168
return -EINVAL;
162169
}
163170

171+
ax25_dev_put(ax25_dev);
164172
return 0;
165173
}
166174

net/ax25/ax25_route.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ static int __must_check ax25_rt_add(struct ax25_routes_struct *route)
116116
ax25_rt->dev = ax25_dev->dev;
117117
ax25_rt->digipeat = NULL;
118118
ax25_rt->ip_mode = ' ';
119+
ax25_dev_put(ax25_dev);
119120
if (route->digi_count != 0) {
120121
if ((ax25_rt->digipeat = kmalloc(sizeof(ax25_digi), GFP_ATOMIC)) == NULL) {
121122
write_unlock_bh(&ax25_route_lock);
@@ -172,6 +173,7 @@ static int ax25_rt_del(struct ax25_routes_struct *route)
172173
}
173174
}
174175
}
176+
ax25_dev_put(ax25_dev);
175177
write_unlock_bh(&ax25_route_lock);
176178

177179
return 0;
@@ -214,6 +216,7 @@ static int ax25_rt_opt(struct ax25_route_opt_struct *rt_option)
214216
}
215217

216218
out:
219+
ax25_dev_put(ax25_dev);
217220
write_unlock_bh(&ax25_route_lock);
218221
return err;
219222
}

0 commit comments

Comments
 (0)