packets: Always register packet sk in the same order
Subsystems (4)
PATCH 1/1 packets: Always register packet sk in the same order
When using fanouts with AF_PACKET, the demux functions such as fanout_demux_cpu will return an index in the fanout socket array, which corresponds to the selected socket.
The ordering of this array depends on the order the sockets were added to a given fanout group, so for FANOUT_CPU this means sockets are bound to cpus in the order they are configured, which is OK.
However, when stopping then restarting the interface these sockets are bound to, the sockets are reassigned to the fanout group in the reverse order, due to the fact that they were inserted at the head of the interface's AF_PACKET socket list.
This means that traffic that was directed to the first socket in the fanout group is now directed to the last one after an interface restart.
In the case of FANOUT_CPU, traffic from CPU0 will be directed to the socket that used to receive traffic from the last CPU after an interface restart.
This commit introduces a helper to add a socket at the tail of a list, then uses it to register AF_PACKET sockets.
Note that this changes the order in which sockets are listed in /proc and with sock_diag.
--- include/net/sock.h | 6 ++++++ net/packet/af_packet.c | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-)
include/net/sock.h +6 -0
@@ -710,6 +710,12 @@ static inline void sk_add_node_rcu(struct sock *sk, struct hlist_head *list)hlist_add_head_rcu (&sk->sk_node, list); } +static inline void sk_add_node_tail_rcu (struct sock *sk,struct hlist_head *list) +{ +sock_hold (sk); +hlist_add_tail_rcu (&sk->sk_node, list); +} +static inline void __sk_nulls_add_node_rcu (struct sock *sk,struct hlist_nulls_head *list) {hlist_nulls_add_head_rcu (&sk->sk_nulls_node, list);
net/packet/af_packet.c +1 -1
@@ -3243,7 +3243,7 @@ static int packet_create(struct net *net, struct socket *sock, int protocol, }mutex_lock (&net->packet.sklist_lock); -sk_add_node_rcu (sk, &net->packet.sklist); +sk_add_node_tail_rcu (sk, &net->packet.sklist);mutex_unlock (&net->packet.sklist_lock);preempt_disable ();
Signature · 1 line
2.20.1
Quoted 21 lines · Maxime Chevallier
When using fanouts with
AF_PACKET, the demux functions such asfanout_demux_cpuwill return an index in the fanout socket array, which corresponds to the selected socket.The ordering of this array depends on the order the sockets were added to a given fanout group, so for
FANOUT_CPUthis means sockets are bound to cpus in the order they are configured, which is OK.However, when stopping then restarting the interface these sockets are bound to, the sockets are reassigned to the fanout group in the reverse order, due to the fact that they were inserted at the head of the interface's
AF_PACKETsocket list.This means that traffic that was directed to the first socket in the fanout group is now directed to the last one after an interface restart.
In the case of
FANOUT_CPU, traffic from CPU0 will be directed to the socket that used to receive traffic from the last CPU after an interface restart.
Quoted 8 lines
This commit introduces a helper to add a socket at the tail of a list, then uses it to register
AF_PACKETsockets.Note that this changes the order in which sockets are listed in /proc and with
sock_diag.Fixes dc99f600698d ("packet: Add fanout support")Signed-off-by Maxime Chevallier <[email protected]>
Note that another consequence of this patch is that insertion on packet create is now O(N) with the number of active packet sockets, due to sklist being an hlist.
David Miller
maintainerFrom: Willem de Bruijn <[email protected]>
Date: Sat, 16 Mar 2019 14:09:33 -0400
Quoted 3 lines
Note that another consequence of this patch is that insertion on packet create is now O(N) with the number of active packet sockets, due to sklist being an hlist.
Exploitable...
Willem de Bruijn
maintainerQuoted 9 lines · David Miller
From: Willem de Bruijn <[email protected]>
Date: Sat, 16 Mar 2019 14:09:33 -0400Quoted 3 lines
Note that another consequence of this patch is that insertion on packet create is now O(N) with the number of active packet sockets, due to sklist being an hlist.
Exploitable...
With root in userns? The running time is limited by open file rlimit. This pattern is already used in a sk_add_node_rcu in some way, so important to be sure.
In practice I see no significant wall clock time difference when inserting to a fairly standard default limit of 16K. Regardless of insertion order, running time is dominated by cleanup on process exit (synchronize_net barriers?). At higher rlimit it does become problematic.
The packet socket sklist is not easily converted from hlist to a regular list, due to the use of seq_hlist_next_rcu in packet_seq_ops. There is no equivalent seq_list_next_rcu. One option might be instead to leave insertion order as is, but traverse the list in reverse in packet_notifier on NETDEV_DOWN. That would require an sk_for_each_reverse_rcu and hlist_for_each_entry_reverse_rcu. These do not exist, but since an hlist_pprev_rcu does exist, it is probably feasible. Though not a trivial change.
Another more narrow option may be to work around the ordering in fanout itself, e.g., record in the socket the initially assigned location in the fanout array and try to reclaim this spot on re-insertion.
David Miller
maintainerFrom: David Miller <[email protected]>
Date: Sat, 16 Mar 2019 18:21:19 -0700 (PDT)
Quoted 8 lines
From: Willem de Bruijn <[email protected]>
Date: Sat, 16 Mar 2019 14:09:33 -0400Quoted 3 lines
Note that another consequence of this patch is that insertion on packet create is now O(N) with the number of active packet sockets, due to sklist being an hlist.
Exploitable...
After reading Willem's explanation of how the scope of such a problem is constrained, I am going to apply this and queue it up for -stable.
Maybe in the long term we can do the hlist reverse RCU thing.