Skip to content

Commit aadf31d

Browse files
bwhacksdavem330
authored andcommitted
llc: Fix races between llc2 handler use and (un)registration
When registering the handlers, any state they rely on must be completely initialised first. When unregistering, we must wait until they are definitely no longer running. llc_rcv() must also avoid reading the handler pointers again after checking for NULL. Signed-off-by: Ben Hutchings <ben@decadent.org.uk> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent f4f8720 commit aadf31d

2 files changed

Lines changed: 18 additions & 5 deletions

File tree

‎net/llc/llc_input.c‎

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ static void (*llc_type_handlers[2])(struct llc_sap *sap,
4242
void llc_add_pack(int type, void (*handler)(struct llc_sap *sap,
4343
struct sk_buff *skb))
4444
{
45+
smp_wmb(); /* ensure initialisation is complete before it's called */
4546
if (type == LLC_DEST_SAP || type == LLC_DEST_CONN)
4647
llc_type_handlers[type - 1] = handler;
4748
}
@@ -50,11 +51,19 @@ void llc_remove_pack(int type)
5051
{
5152
if (type == LLC_DEST_SAP || type == LLC_DEST_CONN)
5253
llc_type_handlers[type - 1] = NULL;
54+
synchronize_net();
5355
}
5456

5557
void llc_set_station_handler(void (*handler)(struct sk_buff *skb))
5658
{
59+
/* Ensure initialisation is complete before it's called */
60+
if (handler)
61+
smp_wmb();
62+
5763
llc_station_handler = handler;
64+
65+
if (!handler)
66+
synchronize_net();
5867
}
5968

6069
/**
@@ -150,6 +159,8 @@ int llc_rcv(struct sk_buff *skb, struct net_device *dev,
150159
int dest;
151160
int (*rcv)(struct sk_buff *, struct net_device *,
152161
struct packet_type *, struct net_device *);
162+
void (*sta_handler)(struct sk_buff *skb);
163+
void (*sap_handler)(struct llc_sap *sap, struct sk_buff *skb);
153164

154165
if (!net_eq(dev_net(dev), &init_net))
155166
goto drop;
@@ -182,7 +193,8 @@ int llc_rcv(struct sk_buff *skb, struct net_device *dev,
182193
*/
183194
rcv = rcu_dereference(sap->rcv_func);
184195
dest = llc_pdu_type(skb);
185-
if (unlikely(!dest || !llc_type_handlers[dest - 1])) {
196+
sap_handler = dest ? ACCESS_ONCE(llc_type_handlers[dest - 1]) : NULL;
197+
if (unlikely(!sap_handler)) {
186198
if (rcv)
187199
rcv(skb, dev, pt, orig_dev);
188200
else
@@ -193,7 +205,7 @@ int llc_rcv(struct sk_buff *skb, struct net_device *dev,
193205
if (cskb)
194206
rcv(cskb, dev, pt, orig_dev);
195207
}
196-
llc_type_handlers[dest - 1](sap, skb);
208+
sap_handler(sap, skb);
197209
}
198210
llc_sap_put(sap);
199211
out:
@@ -202,9 +214,10 @@ int llc_rcv(struct sk_buff *skb, struct net_device *dev,
202214
kfree_skb(skb);
203215
goto out;
204216
handle_station:
205-
if (!llc_station_handler)
217+
sta_handler = ACCESS_ONCE(llc_station_handler);
218+
if (!sta_handler)
206219
goto drop;
207-
llc_station_handler(skb);
220+
sta_handler(skb);
208221
goto out;
209222
}
210223

‎net/llc/llc_station.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -696,9 +696,9 @@ void __init llc_station_init(void)
696696
(unsigned long)&llc_main_station);
697697
llc_main_station.ack_timer.expires = jiffies +
698698
sysctl_llc_station_ack_timeout;
699-
llc_set_station_handler(llc_station_rcv);
700699
llc_main_station.maximum_retry = 1;
701700
llc_main_station.state = LLC_STATION_STATE_UP;
701+
llc_set_station_handler(llc_station_rcv);
702702
}
703703

704704
void llc_station_exit(void)

0 commit comments

Comments
 (0)