Skip to contentSkip to conversation

ipv6: add missing /proc/sys/net/ipv6/ping_group_range

PATCH In review Cong Wang 2 participants 4 messages Jun 8, 2013 – Jun 10, 2013

Subsystems (2)
lore
Share & tools
lore

Cong Wang

author
#0 lore raw

PATCH 1/1 ipv6: add missing /proc/sys/net/ipv6/ping_group_range

From: Cong Wang <[email protected]>

commit 6d0bfe22611602f366 (net: ipv6: Add IPv6 support to the ping socket.) adds IPv6 ping socket support, but forgot to create /proc/sys/net/ipv6/ping_group_range, therefore it wrongly shares /proc/sys/net/ipv4/ping_group_range with IPv4.

This patch adds the missing /proc/sys/net/ipv6/ping_group_range interface.

Cc Lorenzo Colitti <[email protected]>
Cc David S. Miller <[email protected]>
---
 include/net/netns/ipv6.h   |    1 +
 include/net/ping.h         |    2 +-
 net/ipv4/ping.c            |    5 +--
 net/ipv6/ping.c            |   29 +++++++++++++++++-
 net/ipv6/sysctl_net_ipv6.c |   70 ++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 102 insertions(+), 5 deletions(-)
include/net/netns/ipv6.h +1 -0
index 005e2c2..ede6ffd 100644
@@ -28,6 +28,7 @@ struct netns_sysctl_ipv6 {
 	int ip6_rt_mtu_expires;
 	int ip6_rt_min_advmss;
 	int icmpv6_time;
+	kgid_t ping_group_range[2];
 };
 
 struct netns_ipv6 {
include/net/ping.h +1 -1
index 5db0224..3201acc 100644
@@ -69,7 +69,6 @@ int  ping_get_port(struct sock *sk, unsigned short ident);
 void ping_hash(struct sock *sk);
 void ping_unhash(struct sock *sk);
 
-int  ping_init_sock(struct sock *sk);
 void ping_close(struct sock *sk, long timeout);
 int  ping_bind(struct sock *sk, struct sockaddr *uaddr, int addr_len);
 void ping_err(struct sk_buff *skb, int offset, u32 info);
@@ -110,5 +109,6 @@ extern void ping_proc_exit(void);
 void __init ping_init(void);
 int  __init pingv6_init(void);
 void pingv6_exit(void);
+void inet6_get_ping_group_range(kgid_t *data, kgid_t *low, kgid_t *high);
 
 #endif /* _PING_H */
net/ipv4/ping.c +2 -3
index 1f1b2dd..73c6482 100644
@@ -245,7 +245,7 @@ static void inet_get_ping_group_range_net(struct net *net, kgid_t *low,
 }
 
 
-int ping_init_sock(struct sock *sk)
+static int ping_v4_init_sock(struct sock *sk)
 {
 	struct net *net = sock_net(sk);
 	kgid_t group = current_egid();
@@ -270,7 +270,6 @@ int ping_init_sock(struct sock *sk)
 
 	return -EACCES;
 }
-EXPORT_SYMBOL_GPL(ping_init_sock);
 
 void ping_close(struct sock *sk, long timeout)
 {
@@ -964,7 +963,7 @@ EXPORT_SYMBOL_GPL(ping_rcv);
 struct proto ping_prot = {
 	.name =		"PING",
 	.owner =	THIS_MODULE,
-	.init =		ping_init_sock,
+	.init =		ping_v4_init_sock,
 	.close =	ping_close,
 	.connect =	ip4_datagram_connect,
 	.disconnect =	udp_disconnect,
net/ipv6/ping.c +28 -1
index a431103..e6c1953 100644
@@ -26,10 +26,37 @@
 #include <net/transp_v6.h>
 #include <net/ping.h>
 
+static int ping_v6_init_sock(struct sock *sk)
+{
+	struct net *net = sock_net(sk);
+	kgid_t group = current_egid();
+	struct group_info *group_info = get_current_groups();
+	int i, j, count = group_info->ngroups;
+	kgid_t low, high;
+
+	inet6_get_ping_group_range(net->ipv6.sysctl.ping_group_range,
+				   &low, &high);
+	if (gid_lte(low, group) && gid_lte(group, high))
+		return 0;
+
+	for (i = 0; i < group_info->nblocks; i++) {
+		int cp_count = min_t(int, NGROUPS_PER_BLOCK, count);
+		for (j = 0; j < cp_count; j++) {
+			kgid_t gid = group_info->blocks[i][j];
+			if (gid_lte(low, gid) && gid_lte(gid, high))
+				return 0;
+		}
+
+		count -= cp_count;
+	}
+
+	return -EACCES;
+}
+
 struct proto pingv6_prot = {
 	.name =		"PINGv6",
 	.owner =	THIS_MODULE,
-	.init =		ping_init_sock,
+	.init =		ping_v6_init_sock,
 	.close =	ping_close,
 	.connect =	ip6_datagram_connect,
 	.disconnect =	udp_disconnect,
net/ipv6/sysctl_net_ipv6.c +70 -0

88 lines not on this page · raw message

author
#1 lore raw
Quoted 7 lines · Cong Wang

From: Cong Wang <[email protected]>

commit 6d0bfe22611602f366 (net: ipv6: Add IPv6 support to the ping socket.) adds IPv6 ping socket support, but forgot to create /proc/sys/net/ipv6/ping_group_range, therefore it wrongly shares /proc/sys/net/ipv4/ping_group_range with IPv4.

Hmm, this might not be a problem, I just noticed there are other sysctl's (for example ip_early_demux) shared by IPv4 and IPv6.

#2 lore raw
In reply to Cong Wang
Quoted 4 lines · Cong Wang

commit 6d0bfe22611602f366 (net: ipv6: Add IPv6 support to the ping socket.) adds IPv6 ping socket support, but forgot to create /proc/sys/net/ipv6/ping_group_range, therefore it wrongly shares /proc/sys/net/ipv4/ping_group_range with IPv4.

As you said, I'm not sure that sharing the sysctl file with IPv4 is a problem. Is it useful to support different permissions for IPv4 ping versus IPv6 ping? I would imagine that if an admin wants certain groups to be able to run ping, those groups would be the same for IPv4 ping and IPv6 ping. But I'm not the authority here. Maybe David has an opinion?

That said - if we do want to do this, I think you should find a way to do it with less code duplication. For example, your newly-added ping_v6_init_sock is almost line-for-line identical with the current ping_init_sock. This adds maintenance burden, because any future fix needs to modify both functions, and creates the opportunity for the two functions to diverge in the future, leading to different behaviour and/or bugs in the future. It should be trivial to refactor the common code out into one function that is called by both the IPv4 and the IPv6 init functions.

There is similar code duplication in your new get_ping_group range and in other places as well.

author
#3 lore raw
Quoted 6 lines · Lorenzo Colitti
Quoted 4 lines · Cong Wang

commit 6d0bfe22611602f366 (net: ipv6: Add IPv6 support to the ping socket.) adds IPv6 ping socket support, but forgot to create /proc/sys/net/ipv6/ping_group_range, therefore it wrongly shares /proc/sys/net/ipv4/ping_group_range with IPv4.

Quoted 7 lines

As you said, I'm not sure that sharing the sysctl file with IPv4 is a problem. Is it useful to support different permissions for IPv4 ping versus IPv6 ping? I would imagine that if an admin wants certain groups to be able to run ping, those groups would be the same for IPv4 ping and IPv6 ping. But I'm not the authority here. Maybe David has an opinion?

I think we can just ignore the patch for now, we can add it if someone requests for it in future.