Skip to content

Commit a428271

Browse files
ralfbaechleDavid S. Miller
authored andcommitted
[AX.25]: Fix unchecked ax25_linkfail_register uses
ax25_linkfail_register uses kmalloc and the callers were ignoring the error value. Rewrite to let the caller deal with the allocation. This allows the use of static allocation of kmalloc use entirely. Signed-off-by: Ralf Baechle <ralf@linux-mips.org> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 58bc574 commit a428271

4 files changed

Lines changed: 29 additions & 52 deletions

File tree

‎include/net/ax25.h‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,8 +342,14 @@ struct ax25_protocol {
342342

343343
extern void ax25_register_pid(struct ax25_protocol *ap);
344344
extern void ax25_protocol_release(unsigned int);
345-
extern int __must_check ax25_linkfail_register(void (*)(ax25_cb *, int));
346-
extern void ax25_linkfail_release(void (*)(ax25_cb *, int));
345+
346+
struct ax25_linkfail {
347+
struct hlist_node lf_node;
348+
void (*func)(ax25_cb *, int);
349+
};
350+
351+
extern void ax25_linkfail_register(struct ax25_linkfail *lf);
352+
extern void ax25_linkfail_release(struct ax25_linkfail *lf);
347353
extern int __must_check ax25_listen_register(ax25_address *,
348354
struct net_device *);
349355
extern void ax25_listen_release(ax25_address *, struct net_device *);

‎net/ax25/ax25_iface.c‎

Lines changed: 9 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,7 @@
3232
static struct ax25_protocol *protocol_list;
3333
static DEFINE_RWLOCK(protocol_list_lock);
3434

35-
static struct linkfail_struct {
36-
struct linkfail_struct *next;
37-
void (*func)(ax25_cb *, int);
38-
} *linkfail_list = NULL;
35+
static HLIST_HEAD(ax25_linkfail_list);
3936
static DEFINE_SPINLOCK(linkfail_lock);
4037

4138
static struct listen_struct {
@@ -93,54 +90,19 @@ void ax25_protocol_release(unsigned int pid)
9390

9491
EXPORT_SYMBOL(ax25_protocol_release);
9592

96-
int ax25_linkfail_register(void (*func)(ax25_cb *, int))
93+
void ax25_linkfail_register(struct ax25_linkfail *lf)
9794
{
98-
struct linkfail_struct *linkfail;
99-
100-
if ((linkfail = kmalloc(sizeof(*linkfail), GFP_ATOMIC)) == NULL)
101-
return 0;
102-
103-
linkfail->func = func;
104-
10595
spin_lock_bh(&linkfail_lock);
106-
linkfail->next = linkfail_list;
107-
linkfail_list = linkfail;
96+
hlist_add_head(&lf->lf_node, &ax25_linkfail_list);
10897
spin_unlock_bh(&linkfail_lock);
109-
110-
return 1;
11198
}
11299

113100
EXPORT_SYMBOL(ax25_linkfail_register);
114101

115-
void ax25_linkfail_release(void (*func)(ax25_cb *, int))
102+
void ax25_linkfail_release(struct ax25_linkfail *lf)
116103
{
117-
struct linkfail_struct *s, *linkfail;
118-
119104
spin_lock_bh(&linkfail_lock);
120-
linkfail = linkfail_list;
121-
if (linkfail == NULL) {
122-
spin_unlock_bh(&linkfail_lock);
123-
return;
124-
}
125-
126-
if (linkfail->func == func) {
127-
linkfail_list = linkfail->next;
128-
spin_unlock_bh(&linkfail_lock);
129-
kfree(linkfail);
130-
return;
131-
}
132-
133-
while (linkfail != NULL && linkfail->next != NULL) {
134-
if (linkfail->next->func == func) {
135-
s = linkfail->next;
136-
linkfail->next = linkfail->next->next;
137-
spin_unlock_bh(&linkfail_lock);
138-
kfree(s);
139-
return;
140-
}
141-
142-
linkfail = linkfail->next;
143-
}
105+
hlist_del_init(&lf->lf_node);
144106
spin_unlock_bh(&linkfail_lock);
145107
}
146108

@@ -237,11 +199,12 @@ int ax25_listen_mine(ax25_address *callsign, struct net_device *dev)
237199

238200
void ax25_link_failed(ax25_cb *ax25, int reason)
239201
{
240-
struct linkfail_struct *linkfail;
202+
struct ax25_linkfail *lf;
203+
struct hlist_node *node;
241204

242205
spin_lock_bh(&linkfail_lock);
243-
for (linkfail = linkfail_list; linkfail != NULL; linkfail = linkfail->next)
244-
(linkfail->func)(ax25, reason);
206+
hlist_for_each_entry(lf, node, &ax25_linkfail_list, lf_node)
207+
lf->func(ax25, reason);
245208
spin_unlock_bh(&linkfail_lock);
246209
}
247210

‎net/netrom/af_netrom.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,6 +1382,10 @@ static struct ax25_protocol nr_pid = {
13821382
.func = nr_route_frame
13831383
};
13841384

1385+
static struct ax25_linkfail nr_linkfail_notifier = {
1386+
.func = nr_link_failed,
1387+
};
1388+
13851389
static int __init nr_proto_init(void)
13861390
{
13871391
int i;
@@ -1430,7 +1434,7 @@ static int __init nr_proto_init(void)
14301434
register_netdevice_notifier(&nr_dev_notifier);
14311435

14321436
ax25_register_pid(&nr_pid);
1433-
ax25_linkfail_register(nr_link_failed);
1437+
ax25_linkfail_register(&nr_linkfail_notifier);
14341438

14351439
#ifdef CONFIG_SYSCTL
14361440
nr_register_sysctl();
@@ -1479,7 +1483,7 @@ static void __exit nr_exit(void)
14791483
nr_unregister_sysctl();
14801484
#endif
14811485

1482-
ax25_linkfail_release(nr_link_failed);
1486+
ax25_linkfail_release(&nr_linkfail_notifier);
14831487
ax25_protocol_release(AX25_P_NETROM);
14841488

14851489
unregister_netdevice_notifier(&nr_dev_notifier);

‎net/rose/af_rose.c‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,6 +1487,10 @@ static struct ax25_protocol rose_pid = {
14871487
.func = rose_route_frame
14881488
};
14891489

1490+
static struct ax25_linkfail rose_linkfail_notifier = {
1491+
.func = rose_link_failed
1492+
};
1493+
14901494
static int __init rose_proto_init(void)
14911495
{
14921496
int i;
@@ -1537,7 +1541,7 @@ static int __init rose_proto_init(void)
15371541
register_netdevice_notifier(&rose_dev_notifier);
15381542

15391543
ax25_register_pid(&rose_pid);
1540-
ax25_linkfail_register(rose_link_failed);
1544+
ax25_linkfail_register(&rose_linkfail_notifier);
15411545

15421546
#ifdef CONFIG_SYSCTL
15431547
rose_register_sysctl();
@@ -1585,7 +1589,7 @@ static void __exit rose_exit(void)
15851589
rose_rt_free();
15861590

15871591
ax25_protocol_release(AX25_P_ROSE);
1588-
ax25_linkfail_release(rose_link_failed);
1592+
ax25_linkfail_release(&rose_linkfail_notifier);
15891593

15901594
if (ax25cmp(&rose_callsign, &null_ax25_address) != 0)
15911595
ax25_listen_release(&rose_callsign, NULL);

0 commit comments

Comments
 (0)