* [PATCH v2 net-next 0/3] inet: ping: misc changes
@ 2025-08-28 16:41 Eric Dumazet
2025-08-28 16:41 ` [PATCH v2 net-next 1/3] inet: ping: check sock_net() in ping_get_port() and ping_lookup() Eric Dumazet
` (2 more replies)
0 siblings, 3 replies; 7+ messages in thread
From: Eric Dumazet @ 2025-08-28 16:41 UTC (permalink / raw)
To: David S . Miller, Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, David Ahern, netdev, eric.dumazet, Eric Dumazet
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(-)
--
2.51.0.268.g9569e192d0-goog
^ permalink raw reply [flat|nested] 7+ messages in thread
* [PATCH v2 net-next 1/3] inet: ping: check sock_net() in ping_get_port() and ping_lookup()
2025-08-28 16:41 [PATCH v2 net-next 0/3] inet: ping: misc changes Eric Dumazet
@ 2025-08-28 16:41 ` Eric Dumazet
2025-08-28 16:41 ` [PATCH v2 net-next 2/3] inet: ping: remove ping_hash() Eric Dumazet
2025-08-28 16:41 ` [PATCH v2 net-next 3/3] inet: ping: make ping_port_rover per netns Eric Dumazet
2 siblings, 0 replies; 7+ messages in thread
From: Eric Dumazet @ 2025-08-28 16:41 UTC (permalink / raw)
To: David S . Miller, Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, David Ahern, netdev, eric.dumazet, Eric Dumazet
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().
Fixes: c319b4d76b9e ("net: ipv4: add IPPROTO_ICMP socket kind")
Signed-off-by: Eric Dumazet <edumazet@google.com>
Reviewed-by: David Ahern <dsahern@kernel.org>
---
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(-)
diff --git a/net/ipv4/ping.c b/net/ipv4/ping.c
index f119da68fc301be00719213ad33615b6754e6272..74a0beddfcc41d8ba17792a11a9d027c9d590bac 100644
--- a/net/ipv4/ping.c
+++ b/net/ipv4/ping.c
@@ -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");
--
2.51.0.268.g9569e192d0-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 net-next 2/3] inet: ping: remove ping_hash()
2025-08-28 16:41 [PATCH v2 net-next 0/3] inet: ping: misc changes Eric Dumazet
2025-08-28 16:41 ` [PATCH v2 net-next 1/3] inet: ping: check sock_net() in ping_get_port() and ping_lookup() Eric Dumazet
@ 2025-08-28 16:41 ` Eric Dumazet
2025-08-29 6:47 ` Yue Haibing
2025-08-28 16:41 ` [PATCH v2 net-next 3/3] inet: ping: make ping_port_rover per netns Eric Dumazet
2 siblings, 1 reply; 7+ messages in thread
From: Eric Dumazet @ 2025-08-28 16:41 UTC (permalink / raw)
To: David S . Miller, Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, David Ahern, netdev, eric.dumazet, Eric Dumazet
There is no point in keeping ping_hash().
Signed-off-by: Eric Dumazet <edumazet@google.com>
Reviewed-by: David Ahern <dsahern@kernel.org>
---
net/ipv4/ping.c | 10 ----------
net/ipv6/ping.c | 1 -
2 files changed, 11 deletions(-)
diff --git a/net/ipv4/ping.c b/net/ipv4/ping.c
index 74a0beddfcc41d8ba17792a11a9d027c9d590bac..75e1b0f5c697653e79166fde5f312f46b471344a 100644
--- a/net/ipv4/ping.c
+++ b/net/ipv4/ping.c
@@ -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,
diff --git a/net/ipv6/ping.c b/net/ipv6/ping.c
index 82b0492923d458213ac7a6f9316158af2191e30f..d7a2cdaa26312b44f1fe502d3d40f3e27f961fa8 100644
--- a/net/ipv6/ping.c
+++ b/net/ipv6/ping.c
@@ -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,
--
2.51.0.268.g9569e192d0-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread
* [PATCH v2 net-next 3/3] inet: ping: make ping_port_rover per netns
2025-08-28 16:41 [PATCH v2 net-next 0/3] inet: ping: misc changes Eric Dumazet
2025-08-28 16:41 ` [PATCH v2 net-next 1/3] inet: ping: check sock_net() in ping_get_port() and ping_lookup() Eric Dumazet
2025-08-28 16:41 ` [PATCH v2 net-next 2/3] inet: ping: remove ping_hash() Eric Dumazet
@ 2025-08-28 16:41 ` Eric Dumazet
2 siblings, 0 replies; 7+ messages in thread
From: Eric Dumazet @ 2025-08-28 16:41 UTC (permalink / raw)
To: David S . Miller, Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, David Ahern, netdev, eric.dumazet, Eric Dumazet
Provide isolation between netns for ping idents.
Randomize initial ping_port_rover value at netns creation.
Signed-off-by: Eric Dumazet <edumazet@google.com>
Reviewed-by: David Ahern <dsahern@kernel.org>
---
include/net/netns/ipv4.h | 1 +
net/ipv4/ping.c | 10 +++++-----
2 files changed, 6 insertions(+), 5 deletions(-)
diff --git a/include/net/netns/ipv4.h b/include/net/netns/ipv4.h
index 6373e3f17da84ebc5c11058763932e595f0fd205..54a7d187f62a2e995076e85f1e6b2fd70f84b2c1 100644
--- a/include/net/netns/ipv4.h
+++ b/include/net/netns/ipv4.h
@@ -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;
diff --git a/net/ipv4/ping.c b/net/ipv4/ping.c
index 75e1b0f5c697653e79166fde5f312f46b471344a..98ccd4f9ed657d2bb9c013932d0c678f2b38a746 100644
--- a/net/ipv4/ping.c
+++ b/net/ipv4/ping.c
@@ -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;
}
--
2.51.0.268.g9569e192d0-goog
^ permalink raw reply related [flat|nested] 7+ messages in thread
* Re: [PATCH v2 net-next 2/3] inet: ping: remove ping_hash()
2025-08-28 16:41 ` [PATCH v2 net-next 2/3] inet: ping: remove ping_hash() Eric Dumazet
@ 2025-08-29 6:47 ` Yue Haibing
2025-08-29 6:57 ` Eric Dumazet
0 siblings, 1 reply; 7+ messages in thread
From: Yue Haibing @ 2025-08-29 6:47 UTC (permalink / raw)
To: Eric Dumazet, David S . Miller, Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, David Ahern, netdev, eric.dumazet
On 2025/8/29 0:41, Eric Dumazet wrote:
> There is no point in keeping ping_hash().
>
> Signed-off-by: Eric Dumazet <edumazet@google.com>
> Reviewed-by: David Ahern <dsahern@kernel.org>
> ---
> net/ipv4/ping.c | 10 ----------
> net/ipv6/ping.c | 1 -
> 2 files changed, 11 deletions(-)
>
> diff --git a/net/ipv4/ping.c b/net/ipv4/ping.c
> index 74a0beddfcc41d8ba17792a11a9d027c9d590bac..75e1b0f5c697653e79166fde5f312f46b471344a 100644
> --- a/net/ipv4/ping.c
> +++ b/net/ipv4/ping.c
> @@ -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);
>
> 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,
> diff --git a/net/ipv6/ping.c b/net/ipv6/ping.c
> index 82b0492923d458213ac7a6f9316158af2191e30f..d7a2cdaa26312b44f1fe502d3d40f3e27f961fa8 100644
> --- a/net/ipv6/ping.c
> +++ b/net/ipv6/ping.c
> @@ -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,
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 net-next 2/3] inet: ping: remove ping_hash()
2025-08-29 6:47 ` Yue Haibing
@ 2025-08-29 6:57 ` Eric Dumazet
2025-08-29 7:13 ` Yue Haibing
0 siblings, 1 reply; 7+ messages in thread
From: Eric Dumazet @ 2025-08-29 6:57 UTC (permalink / raw)
To: Yue Haibing
Cc: David S . Miller, Jakub Kicinski, Paolo Abeni, Simon Horman,
David Ahern, netdev, eric.dumazet
On Thu, Aug 28, 2025 at 11:47 PM Yue Haibing <yuehaibing@huawei.com> wrote:
>
> On 2025/8/29 0:41, Eric Dumazet wrote:
> > There is no point in keeping ping_hash().
> >
> > Signed-off-by: Eric Dumazet <edumazet@google.com>
> > Reviewed-by: David Ahern <dsahern@kernel.org>
> > ---
> > net/ipv4/ping.c | 10 ----------
> > net/ipv6/ping.c | 1 -
> > 2 files changed, 11 deletions(-)
> >
> > diff --git a/net/ipv4/ping.c b/net/ipv4/ping.c
> > index 74a0beddfcc41d8ba17792a11a9d027c9d590bac..75e1b0f5c697653e79166fde5f312f46b471344a 100644
> > --- a/net/ipv4/ping.c
> > +++ b/net/ipv4/ping.c
> > @@ -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);
Right, but you probably meant
include/net/ping.h:57:int ping_hash(struct sock *sk);
^ permalink raw reply [flat|nested] 7+ messages in thread
* Re: [PATCH v2 net-next 2/3] inet: ping: remove ping_hash()
2025-08-29 6:57 ` Eric Dumazet
@ 2025-08-29 7:13 ` Yue Haibing
0 siblings, 0 replies; 7+ messages in thread
From: Yue Haibing @ 2025-08-29 7:13 UTC (permalink / raw)
To: Eric Dumazet
Cc: David S . Miller, Jakub Kicinski, Paolo Abeni, Simon Horman,
David Ahern, netdev, eric.dumazet
On 2025/8/29 14:57, Eric Dumazet wrote:
> On Thu, Aug 28, 2025 at 11:47 PM Yue Haibing <yuehaibing@huawei.com> wrote:
>>
>> On 2025/8/29 0:41, Eric Dumazet wrote:
>>> There is no point in keeping ping_hash().
>>>
>>> Signed-off-by: Eric Dumazet <edumazet@google.com>
>>> Reviewed-by: David Ahern <dsahern@kernel.org>
>>> ---
>>> net/ipv4/ping.c | 10 ----------
>>> net/ipv6/ping.c | 1 -
>>> 2 files changed, 11 deletions(-)
>>>
>>> diff --git a/net/ipv4/ping.c b/net/ipv4/ping.c
>>> index 74a0beddfcc41d8ba17792a11a9d027c9d590bac..75e1b0f5c697653e79166fde5f312f46b471344a 100644
>>> --- a/net/ipv4/ping.c
>>> +++ b/net/ipv4/ping.c
>>> @@ -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);
>
> Right, but you probably meant
Sure,I have a copy_paste mistake.
>
> include/net/ping.h:57:int ping_hash(struct sock *sk);
^ permalink raw reply [flat|nested] 7+ messages in thread
end of thread, other threads:[~2025-08-29 7:13 UTC | newest]
Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-28 16:41 [PATCH v2 net-next 0/3] inet: ping: misc changes Eric Dumazet
2025-08-28 16:41 ` [PATCH v2 net-next 1/3] inet: ping: check sock_net() in ping_get_port() and ping_lookup() Eric Dumazet
2025-08-28 16:41 ` [PATCH v2 net-next 2/3] inet: ping: remove ping_hash() Eric Dumazet
2025-08-29 6:47 ` Yue Haibing
2025-08-29 6:57 ` Eric Dumazet
2025-08-29 7:13 ` Yue Haibing
2025-08-28 16:41 ` [PATCH v2 net-next 3/3] inet: ping: make ping_port_rover per netns Eric Dumazet
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.