inet: ping: misc changes
Subsystems (2)
v2 cover inet: ping: misc changes
First and third patches improve security a bit.
Second patch (ping_hash removal) is a cleanup.
Eric Dumazet (3):
inet: ping: check sock_net() in ping_get_port() and ping_lookup()
inet: ping: remove ping_hash()
inet: ping: make ping_port_rover per netns
include/net/netns/ipv4.h | 1 + net/ipv4/ping.c | 34 +++++++++++++++------------------- net/ipv6/ping.c | 1 - 3 files changed, 16 insertions(+), 20 deletions(-)
Signature · 1 line
2.51.0.268.g9569e192d0-goog
PATCH v2 1/3 inet: ping: check sock_net() in ping_get_port() and ping_lookup()
We need to check socket netns before considering them in ping_get_port(). Otherwise, one malicious netns could 'consume' all ports.
Add corresponding check in ping_lookup().
---
v2: added change to ping_lookup().
v1: https://lore.kernel.org/netdev/CANn89iKF+DFQQyNJoYA2U-wf4QDPOUG2yNOd8fSu45hQ+TxJ5Q@mail.gmail.com/T/#u
net/ipv4/ping.c | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-)
net/ipv4/ping.c +10 -4
@@ -77,6 +77,7 @@ static inline struct hlist_head *ping_hashslot(struct ping_table *table,int ping_get_port (struct sock *sk,unsigned short ident) { +struct net *net =sock_net (sk);struct inet_sock *isk, *isk2;struct hlist_head *hlist;struct sock *sk2 =NULL ;
@@ -90,9 +91,10 @@ int ping_get_port(struct sock *sk, unsigned short ident)for (i =0 ; i < (1L <<16 ); i++, result++) {if (!result) result++;/* avoid zero */ - hlist =ping_hashslot (&ping_table,sock_net (sk), - result); + hlist =ping_hashslot (&ping_table, net, result);sk_for_each (sk2, hlist) { +if (!net_eq (sock_net (sk2), net)) +continue ; isk2 =inet_sk (sk2);if (isk2->inet_num == result)
@@ -108,8 +110,10 @@ int ping_get_port(struct sock *sk, unsigned short ident)if (i >= (1L <<16 )) goto fail; }else { - hlist =ping_hashslot (&ping_table,sock_net (sk), ident); + hlist =ping_hashslot (&ping_table, net, ident);sk_for_each (sk2, hlist) { +if (!net_eq (sock_net (sk2), net)) +continue ; isk2 =inet_sk (sk2);/* BUG? Why is this reuse and not reuseaddr? ping.c
@@ -129,7 +133,7 @@ int ping_get_port(struct sock *sk, unsigned short ident)pr_debug ("was not hashed\n" );sk_add_node_rcu (sk, hlist);sock_set_flag (sk,SOCK_RCU_FREE ); -sock_prot_inuse_add (sock_net (sk), sk->sk_prot,1 ); +sock_prot_inuse_add (net, sk->sk_prot,1 ); }spin_unlock (&ping_table.lock);return 0 ;
@@ -188,6 +192,8 @@ static struct sock *ping_lookup(struct net *net, struct sk_buff *skb, u16 ident) }sk_for_each_rcu (sk, hslot) { +if (!net_eq (sock_net (sk), net)) +continue ; isk =inet_sk (sk);pr_debug ("iterate\n" );
Signature · 1 line
2.51.0.268.g9569e192d0-goog
PATCH v2 2/3 inet: ping: remove ping_hash()
In reply to Eric DumazetThere is no point in keeping ping_hash().
--- net/ipv4/ping.c | 10 ---------- net/ipv6/ping.c | 1 - 2 files changed, 11 deletions(-)
net/ipv4/ping.c +0 -10
@@ -67,7 +67,6 @@ static inline u32 ping_hashfn(const struct net *net, u32 num, u32 mask)pr_debug ("hash(%u) = %u\n" , num, res);return res; } -EXPORT_SYMBOL_GPL (ping_hash);static inline struct hlist_head *ping_hashslot (struct ping_table *table,struct net *net,unsigned int num)
@@ -144,14 +143,6 @@ int ping_get_port(struct sock *sk, unsigned short ident) }EXPORT_SYMBOL_GPL (ping_get_port); -int ping_hash (struct sock *sk) -{ -pr_debug ("ping_hash(sk->port=%u)\n" ,inet_sk (sk)->inet_num); -BUG ();/* "Please do not press this button again." */ - -return 0 ; -} -void ping_unhash (struct sock *sk) {struct inet_sock *isk =inet_sk (sk);
@@ -1008,7 +999,6 @@ struct proto ping_prot = { .bind = ping_bind, .backlog_rcv = ping_queue_rcv_skb, .release_cb = ip4_datagram_release_cb, - .hash = ping_hash, .unhash = ping_unhash, .get_port = ping_get_port, .put_port = ping_unhash,
net/ipv6/ping.c +0 -1
@@ -208,7 +208,6 @@ struct proto pingv6_prot = { .recvmsg = ping_recvmsg, .bind = ping_bind, .backlog_rcv = ping_queue_rcv_skb, - .hash = ping_hash, .unhash = ping_unhash, .get_port = ping_get_port, .put_port = ping_unhash,
Signature · 1 line
2.51.0.268.g9569e192d0-goog
Re: [PATCH v2 net-next 2/3] inet: ping: remove ping_hash()
Quoted 14 lines · Eric Dumazet
There is no point in keeping
ping_hash().Signed-off-by Eric Dumazet <[email protected]>Reviewed-by David Ahern <[email protected]>--- net/ipv4/ping.c | 10 ---------- net/ipv6/ping.c | 1 - 2 files changed, 11 deletions(-)net/ipv4/ping.c +0
-0
net/ipv4/ping.c static inline u32 ping_hashfn(const struct net *net, u32 num, u32 mask) · line 70
Quoted 4 lines
net/ipv4/ping.c +0
-1@@ -67,7 +67,6 @@ static inline u32 ping_hashfn(const struct net *net, u32 num, u32 mask)pr_debug ("hash(%u) = %u\n" , num, res);return res; } -EXPORT_SYMBOL_GPL (ping_hash);
The declaration should also be removed
include/net/ping.h:58:void ping_unhash(struct sock *sk);
Quoted 38 lines
net/ipv4/ping.c +0
-9static inline struct hlist_head *ping_hashslot (struct ping_table *table,struct net *net,unsigned int num)@@ -144,14 +143,6 @@ int ping_get_port(struct sock *sk, unsigned short ident) }EXPORT_SYMBOL_GPL (ping_get_port); -int ping_hash (struct sock *sk) -{ -pr_debug ("ping_hash(sk->port=%u)\n" ,inet_sk (sk)->inet_num); -BUG ();/* "Please do not press this button again." */ - -return 0 ; -} -void ping_unhash (struct sock *sk) {struct inet_sock *isk =inet_sk (sk);@@ -1008,7 +999,6 @@ struct proto ping_prot = { .bind = ping_bind, .backlog_rcv = ping_queue_rcv_skb, .release_cb = ip4_datagram_release_cb, - .hash = ping_hash, .unhash = ping_unhash, .get_port = ping_get_port, .put_port = ping_unhash,net/ipv6/ping.c +0
-1@@ -208,7 +208,6 @@ struct proto pingv6_prot = { .recvmsg = ping_recvmsg, .bind = ping_bind, .backlog_rcv = ping_queue_rcv_skb, - .hash = ping_hash, .unhash = ping_unhash, .get_port = ping_get_port, .put_port = ping_unhash,
Re: [PATCH v2 net-next 2/3] inet: ping: remove ping_hash()
Quoted 23 lines · Yue Haibing
Quoted 14 lines · Eric Dumazet
There is no point in keeping
ping_hash().Signed-off-by Eric Dumazet <[email protected]>Reviewed-by David Ahern <[email protected]>--- net/ipv4/ping.c | 10 ---------- net/ipv6/ping.c | 1 - 2 files changed, 11 deletions(-)net/ipv4/ping.c +0
-0net/ipv4/ping.c static inline u32 ping_hashfn(const struct net *net, u32 num, u32 mask) · line 70
Quoted 4 lines
net/ipv4/ping.c +0
-1@@ -67,7 +67,6 @@ static inline u32 ping_hashfn(const struct net *net, u32 num, u32 mask)pr_debug ("hash(%u) = %u\n" , num, res);return res; } -EXPORT_SYMBOL_GPL (ping_hash);The declaration should also be removed
Quoted 1 line
include/net/ping.h:58:void
ping_unhash(struct sock *sk);
Right, but you probably meant
include/net/ping.h:57:int ping_hash(struct sock *sk);
Re: [PATCH v2 net-next 2/3] inet: ping: remove ping_hash()
Quoted 24 lines · Eric Dumazet
Quoted 23 lines · Yue Haibing
Quoted 14 lines · Eric Dumazet
There is no point in keeping
ping_hash().Signed-off-by Eric Dumazet <[email protected]>Reviewed-by David Ahern <[email protected]>--- net/ipv4/ping.c | 10 ---------- net/ipv6/ping.c | 1 - 2 files changed, 11 deletions(-)net/ipv4/ping.c +0
-0net/ipv4/ping.c static inline u32 ping_hashfn(const struct net *net, u32 num, u32 mask) · line 70
Quoted 4 lines
net/ipv4/ping.c +0
-1@@ -67,7 +67,6 @@ static inline u32 ping_hashfn(const struct net *net, u32 num, u32 mask)pr_debug ("hash(%u) = %u\n" , num, res);return res; } -EXPORT_SYMBOL_GPL (ping_hash);The declaration should also be removed
Quoted 3 lines
Quoted 1 line
include/net/ping.h:58:void
ping_unhash(struct sock *sk);Right, but you probably meant
Sure,I have a copy_paste mistake.
Quoted 2 lines
include/net/ping.h:57:int
ping_hash(struct sock *sk);
PATCH v2 3/3 inet: ping: make ping_port_rover per netns
In reply to Eric DumazetProvide isolation between netns for ping idents.
Randomize initial ping_port_rover value at netns creation.
--- include/net/netns/ipv4.h | 1 + net/ipv4/ping.c | 10 +++++----- 2 files changed, 6 insertions(+), 5 deletions(-)
include/net/netns/ipv4.h +1 -0
@@ -251,6 +251,7 @@ struct netns_ipv4 {int sysctl_igmp_qrv;struct ping_group_range ping_group_range; +u16 ping_port_rover;atomic_t dev_addr_genid;
net/ipv4/ping.c +5 -5
@@ -58,8 +58,6 @@ static struct ping_table ping_table;struct pingv6_ops pingv6_ops;EXPORT_SYMBOL_GPL (pingv6_ops); -static u16 ping_port_rover; -static inline u32 ping_hashfn (const struct net *net,u32 num,u32 mask) {u32 res = (num +net_hash_mix (net)) & mask;
@@ -84,12 +82,12 @@ int ping_get_port(struct sock *sk, unsigned short ident) isk =inet_sk (sk);spin_lock (&ping_table.lock);if (ident ==0 ) { +u16 result = net->ipv4.ping_port_rover +1 ;u32 i; -u16 result = ping_port_rover +1 ;for (i =0 ; i < (1L <<16 ); i++, result++) {if (!result) - result++;/* avoid zero */ +continue ;/* avoid zero */ hlist =ping_hashslot (&ping_table, net, result);sk_for_each (sk2, hlist) {if (!net_eq (sock_net (sk2), net))
@@ -101,7 +99,7 @@ int ping_get_port(struct sock *sk, unsigned short ident) }/* found */ - ping_port_rover = ident = result; + net->ipv4.ping_port_rover = ident = result;break ; next_port: ;
@@ -1146,6 +1144,8 @@ static int __net_init ping_v4_proc_init_net(struct net *net)if (!proc_create_net ("icmp" ,0444 , net->proc_net, &ping_v4_seq_ops,sizeof (struct ping_iter_state )))return -ENOMEM ; + + net->ipv4.ping_port_rover =get_random_u16 ();return 0 ; }
Signature · 1 line
2.51.0.268.g9569e192d0-goog