l2tp: use rcu_dereference_sk_user_data() in l2tp_udp_encap_recv()
Subsystems (3)
PATCH 1/1 l2tp: use rcu_dereference_sk_user_data() in l2tp_udp_encap_recv()
Canonical way to fetch sk_user_data from an encap_rcv() handler called from UDP stack in rcu protected section is to use rcu_dereference_sk_user_data(), otherwise compiler might read it multiple times.
--- net/l2tp/l2tp_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
net/l2tp/l2tp_core.c +1 -1
@@ -909,7 +909,7 @@ int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb) {struct l2tp_tunnel *tunnel; - tunnel =l2tp_tunnel (sk); + tunnel =rcu_dereference_sk_user_data (sk);if (tunnel ==NULL ) goto pass_up;
Signature · 1 line
2.21.0.593.g511ec345e18-goog
Quoted 4 lines · Eric Dumazet
Canonical way to fetch
sk_user_datafrom anencap_rcv()handler called from UDP stack in rcu protected section is to usercu_dereference_sk_user_data(), otherwise compiler might read it multiple times.
That reminds me the more general problem we have with ->sk_user_data:
https://lore.kernel.org/netdev/[email protected]/
We're not even guarateed that ->sk_user_data points to a struct l2tp_tunnel (some external modules can still probably override it).
There were some locking rules defined for setting ->sk_user_data:
https://lore.kernel.org/netdev/[email protected]/
Converting all users to either avoid using ->sk_user_data or using it under the proper pre-conditions has been on my TODO list for a while... I'll try to revive this effort.
Quoted 1 line
Fixes d00fa9adc528 ("il2tp: fix races with tunnel socket close")
^
Spurious "i". Vi user? :)
Quoted 22 lines
Signed-off-by Eric Dumazet <[email protected]>Cc James Chapman <[email protected]>--- net/l2tp/l2tp_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)net/l2tp/l2tp_core.c +1
-1@@ -909,7 +909,7 @@ int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb) {struct l2tp_tunnel *tunnel; - tunnel =l2tp_tunnel (sk); + tunnel =rcu_dereference_sk_user_data (sk);if (tunnel ==NULL ) goto pass_up;Signature · 1 line
2.21.0.593.g511ec345e18-goog
Quoted 6 lines · Guillaume Nault
Quoted 4 lines · Eric Dumazet
Canonical way to fetch
sk_user_datafrom anencap_rcv()handler called from UDP stack in rcu protected section is to usercu_dereference_sk_user_data(), otherwise compiler might read it multiple times.
Quoted 6 lines
That reminds me the more general problem we have with ->sk_user_data:
https://lore.kernel.org/netdev/[email protected]/We're not even guarateed that ->sk_user_data points to a struct
l2tp_tunnel(some external modules can still probably override it).
RCU rules should prevail.
An RCU grace period must be enforced if the same UDP socket has its sk_user_data changed.
Normally, UDP socket observes an RCU grace period at close/destroy time. (SOCK_RCU_FREE)
If we detect the same UDP socket has not been closed between clearing sk_user_data
and setting it again to a non NULL value, we must insert a synchronize_rcu()
A generic change could look like the following (but we must check that all rcu_assign_sk_user_data() callers can sleep)
include/net/sock.h +0 -0
100644
include/net/sock.h +0 -0
@@ -522,7 +522,6 @@ enum sk_pacing {#define __sk_user_data (sk) ((*((void __rcu **)&(sk)->sk_user_data)))#define rcu_dereference_sk_user_data (sk)
rcu_dereference(__sk_user_data((sk)))
-#define rcu_assign_sk_user_data(sk, ptr)rcu_assign_pointer(__sk_user_data((sk)), ptr)
/*
* SK_CAN_REUSE and SK_NO_REUSE on a socket mean that the socket is OK
@@ -811,6 +810,7 @@ enum sock_flags {
SOCK_FILTER_LOCKED, /* Filter cannot be changed anymore */ SOCK_SELECT_ERR_QUEUE, /* Wake select on error queue */ SOCK_RCU_FREE, /* wait rcu grace period in sk_destruct() */
+ SOCK_USER_DATA_SET,
SOCK_TXTIME,
SOCK_XDP, /* XDP is attached */
SOCK_TSTAMP_NEW, /* Indicates 64 bit timestamps always */
@@ -2582,4 +2582,15 @@ static inline bool sk_dev_equal_l3scope(struct sock *sk, int dif)
return false;
}
+static inline void rcu_assign_sk_user_data(struct sock *sk, void *ptr) +{
+ if (ptr) {
+ might_sleep();
+ if (sock_flag(sk, SOCK_USER_DATA_SET))
+ synchronize_net();
+ } + rcu_assign_pointer(__sk_user_data((sk)), ptr); + if (ptr)
+ sock_set_flag(sk, SOCK_USER_DATA_SET);
+}
#endif /* _SOCK_H */
Quoted 5 lines
There were some locking rules defined for setting ->sk_user_data:
https://lore.kernel.org/netdev/[email protected]/
Converting all users to either avoid using ->sk_user_data or using it under the proper pre-conditions has been on my TODO list for a while... I'll try to revive this effort.
Avoiding using sk_user_data seems not practical.
However making sure it is not blindly overwritten by a buggy module is doable.
Quoted 4 lines
Quoted 2 lines
Fixes d00fa9adc528 ("il2tp: fix races with tunnel socket close")^Spurious "i". Vi user? :)
I am not a vi user ;)
Quoted 23 lines
Quoted 23 lines
Signed-off-by Eric Dumazet <[email protected]>Cc James Chapman <[email protected]>--- net/l2tp/l2tp_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)net/l2tp/l2tp_core.c +1
-2@@ -909,7 +909,7 @@ int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb) {struct l2tp_tunnel *tunnel; - tunnel =l2tp_tunnel (sk); + tunnel =rcu_dereference_sk_user_data (sk);if (tunnel ==NULL ) goto pass_up; --2.21.0.593.g511ec345e18-goog
Quoted 22 lines · Eric Dumazet
Quoted 6 lines · Guillaume Nault
Quoted 4 lines · Eric Dumazet
Canonical way to fetch
sk_user_datafrom anencap_rcv()handler called from UDP stack in rcu protected section is to usercu_dereference_sk_user_data(), otherwise compiler might read it multiple times.Quoted 6 lines
That reminds me the more general problem we have with ->sk_user_data:
https://lore.kernel.org/netdev/[email protected]/We're not even guarateed that ->sk_user_data points to a struct
l2tp_tunnel(some external modules can still probably override it).RCU rules should prevail.
An RCU grace period must be enforced if the same UDP socket has its
sk_user_datachanged.Normally, UDP socket observes an RCU grace period at close/destroy time. (
SOCK_RCU_FREE)
Quoted 7 lines
If we detect the same UDP socket has not been closed between clearing
sk_user_data
and setting it again to a non NULL value, we must insert asynchronize_rcu()A generic change could look like the following (but we must check that all
rcu_assign_sk_user_data()callers can sleep)
It seems that callers of rcu_assign_sk_user_data() can indeed sleep.
But I think we have more problems. Not all code paths treat ->sk_user_data as RCU pointers (IIUC that's why we created the __sk_user_data() macro, instead of just redefining ->sk_user_data as "void __rcu *"). But even if RCU rules were respected for all accesses, we'd need to ensure consistent protection for the update side.
And then, we'd need to make sure that ->sk_user_data is in sync with the encap_rcv() callback (or whatever actually uses the data pointed to). Otherwise a module could treat ->sk_user_data as a struct foo pointer while it actually points to a struct bar.
For example, a quick look at net/sunrpc/svcsock.c seems to indicate that svc_addsock() would accept any (unconnected) UDP socket and pass it to svc_addsock(), which in turn would override ->sk_user_data with a struct svc_sock pointer. If the socket was previously set up by L2TP, then we'd end up with ->sk_user_data pointing to a svc_sock structure, but ->encap_rcv still pointing to l2tp_udp_encap_recv(). That's going to give unexpected results when l2tp_udp_encap_recv() will dereference ->sk_user_data to access (what it believes to be) its tunnel structure.
Quoted 49 lines
include/net/sock.h +0
-0100644
include/net/sock.h +0
-1@@ -522,7 +522,6 @@ enum sk_pacing {#define __sk_user_data (sk) ((*((void __rcu **)&(sk)->sk_user_data)))#define rcu_dereference_sk_user_data (sk)rcu_dereference (__sk_user_data((sk ))) -#define rcu_assign_sk_user_data (sk, ptr)rcu_assign_pointer (__sk_user_data((sk )), ptr)/*
*SK_CAN_REUSEandSK_NO_REUSEon a socket mean that the socket is OKinclude/net/sock.h +12
-0@@ -811,6 +810,7 @@ enum sock_flags {SOCK_FILTER_LOCKED ,/* Filter cannot be changed anymore */ SOCK_SELECT_ERR_QUEUE ,/* Wake select on error queue */ SOCK_RCU_FREE ,/* wait rcu grace period in sk_destruct() */ +SOCK_USER_DATA_SET ,SOCK_TXTIME ,SOCK_XDP ,/* XDP is attached */ SOCK_TSTAMP_NEW, /* Indicates 64 bit timestamps always */@@ -2582,4 +2582,15 @@ static inline bool sk_dev_equal_l3scope(struct sock *sk, int dif) return false; } +static inline void rcu_assign_sk_user_data (struct sock *sk,void *ptr) +{ +if (ptr) { +might_sleep (); +if (sock_flag (sk,SOCK_USER_DATA_SET )) +synchronize_net (); + } +rcu_assign_pointer (__sk_user_data ((sk)), ptr); +if (ptr) +sock_set_flag (sk,SOCK_USER_DATA_SET ); +}#endif /*
_SOCK_H*/Quoted 5 lines
There were some locking rules defined for setting ->sk_user_data:
https://lore.kernel.org/netdev/[email protected]/
Converting all users to either avoid using ->sk_user_data or using it under the proper pre-conditions has been on my TODO list for a while... I'll try to revive this effort.
Quoted 4 lines
Avoiding using
sk_user_dataseems not practical.However making sure it is not blindly overwritten by a buggy module is doable.
At least for those that use rcu_assign_sk_user_data(). And even then, we'd need to ensure callers do properly validate the socket, to avoid overriding ->sk_user_data with a different pointer type than what other users expect.
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
Fixes d00fa9adc528 ("il2tp: fix races with tunnel socket close")^Spurious "i". Vi user? :)
I am not a vi user ;)
So nice to hear :)
Quoted 23 lines
Quoted 23 lines
Quoted 23 lines
Signed-off-by Eric Dumazet <[email protected]>Cc James Chapman <[email protected]>--- net/l2tp/l2tp_core.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)net/l2tp/l2tp_core.c +1
-2@@ -909,7 +909,7 @@ int l2tp_udp_encap_recv(struct sock *sk, struct sk_buff *skb) {struct l2tp_tunnel *tunnel; - tunnel =l2tp_tunnel (sk); + tunnel =rcu_dereference_sk_user_data (sk);if (tunnel ==NULL ) goto pass_up; --2.21.0.593.g511ec345e18-goog
Quoted 7 lines · Guillaume Nault
It seems that callers of
rcu_assign_sk_user_data()can indeed sleep.But I think we have more problems. Not all code paths treat ->sk_user_data as RCU pointers (IIUC that's why we created the
__sk_user_data()macro, instead of just redefining ->sk_user_data as "void__rcu*"). But even if RCU rules were respected for all accesses, we'd need to ensure consistent protection for the update side.
Sure.
Quoted 6 lines
And then, we'd need to make sure that ->sk_user_data is in sync with the
encap_rcv()callback (or whatever actually uses the data pointed to). Otherwise a module could treat ->sk_user_data as a struct foo pointer while it actually points to a struct bar.
Quoted 8 lines
For example, a quick look at
net/sunrpc/svcsock.cseems to indicate thatsvc_addsock()would accept any (unconnected) UDP socket and pass it tosvc_addsock(), which in turn would override ->sk_user_data with a structsvc_sockpointer. If the socket was previously set up by L2TP, then we'd end up with ->sk_user_data pointing to asvc_sockstructure, but ->encap_rcv still pointing tol2tp_udp_encap_recv(). That's going to give unexpected results whenl2tp_udp_encap_recv()will dereference ->sk_user_data to access (what it believes to be) its tunnel structure.
A full audit is needed, and I have started it. If you want to help just send a patch ;)
I have looked at this l2tp code only after fixing another issue in RXRPC, and would have
looked later at SUNRPC.
Quoted 17 lines · Eric Dumazet
Quoted 6 lines · Guillaume Nault
And then, we'd need to make sure that ->sk_user_data is in sync with the
encap_rcv()callback (or whatever actually uses the data pointed to). Otherwise a module could treat ->sk_user_data as a struct foo pointer while it actually points to a struct bar.Quoted 8 lines
For example, a quick look at
net/sunrpc/svcsock.cseems to indicate thatsvc_addsock()would accept any (unconnected) UDP socket and pass it tosvc_addsock(), which in turn would override ->sk_user_data with a structsvc_sockpointer. If the socket was previously set up by L2TP, then we'd end up with ->sk_user_data pointing to asvc_sockstructure, but ->encap_rcv still pointing tol2tp_udp_encap_recv(). That's going to give unexpected results whenl2tp_udp_encap_recv()will dereference ->sk_user_data to access (what it believes to be) its tunnel structure.
Quoted 6 lines
A full audit is needed, and I have started it. If you want to help just send a patch ;)
I have looked at this l2tp code only after fixing another issue in RXRPC, and would have
looked later at SUNRPC.
Hum, sorry, I didn't realise that. I'm really interested in the solutions you can come up with.