Skip to content

Commit d01ffb9

Browse files
stonezdmdavem330
authored andcommitted
ax25: add refcount in ax25_dev to avoid UAF bugs
If we dereference ax25_dev after we call kfree(ax25_dev) in ax25_dev_device_down(), it will lead to concurrency UAF bugs. There are eight syscall functions suffer from UAF bugs, include ax25_bind(), ax25_release(), ax25_connect(), ax25_ioctl(), ax25_getname(), ax25_sendmsg(), ax25_getsockopt() and ax25_info_show(). One of the concurrency UAF can be shown as below: (USE) | (FREE) | ax25_device_event | ax25_dev_device_down ax25_bind | ... ... | kfree(ax25_dev) ax25_fillin_cb() | ... ax25_fillin_cb_from_dev() | ... | The root cause of UAF bugs is that kfree(ax25_dev) in ax25_dev_device_down() is not protected by any locks. When ax25_dev, which there are still pointers point to, is released, the concurrency UAF bug will happen. This patch introduces refcount into ax25_dev in order to guarantee that there are no pointers point to it when ax25_dev is released. Signed-off-by: Duoming Zhou <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 4e0f718 commit d01ffb9

File tree

4 files changed

+25
-2
lines changed

4 files changed

+25
-2
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ static void ax25_kill_by_device(struct net_device *dev)
9191
spin_unlock_bh(&ax25_list_lock);
9292
lock_sock(sk);
9393
s->ax25_dev = NULL;
94+
ax25_dev_put(ax25_dev);
9495
release_sock(sk);
9596
ax25_disconnect(s, ENETUNREACH);
9697
spin_lock_bh(&ax25_list_lock);
@@ -439,6 +440,7 @@ static int ax25_ctl_ioctl(const unsigned int cmd, void __user *arg)
439440
}
440441

441442
out_put:
443+
ax25_dev_put(ax25_dev);
442444
ax25_cb_put(ax25);
443445
return ret;
444446

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)