Skip to contentSkip to conversation

ipv4: add ICMP socket kind

RFC In review Vasiliy Kulikov 4 participants 6 messages Dec 21, 2010 – Dec 22, 2010

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

RFC 1/1 ipv4: add ICMP socket kind

Hi,

This patch adds IPPROTO_ICMP socket kind. It makes it possible to send ICMP_ECHO messages and receive corresponding ICMP_ECHOREPLY messages without any special privileges. In other words, the patch makes it possible to implement setuid-less /bin/ping.

A new ping socket is created with

socket(PF_INET, SOCK_DGRAM, IPPROTO_ICMP)

Message identifiers (octets 4-5 of ICMP header) are interpreted as local ports. Addresses are stored in struct sockaddr_in. No port numbers are reserved for privileged processes, port 0 is reserved for API ("let the kernel pick a free number"). There is no notion of remote ports, remote port numbers provided by the user (e.g. in connect()) are ignored.

Data sent and received include ICMP headers. This is deliberate to:
1) Avoid the need to transport headers values like sequence numbers by other means
2) Make it easier to port existing programs using raw sockets.

ICMP headers given to send() are checked and sanitized. The type must be ICMP_ECHO and the code must be zero (future extensions might relax this, see below). The id is set to the number (local port) of the socket, the checksum is always recomputed.

ICMP reply packets received from the network are demultiplexed according to their id's and returned by recv() without any modifications. IP header information and ICMP errors of those packets may be obtained via ancillary data (IP_RECVTTL, IP_RETOPTS, and IP_RECVERR). ICMP source quenches and redirects are reported as fake errors via the error queue (IP_RECVERR); the next hop address for redirects is saved to ee_info (in network order).

The existing code might be (in the unlikely case anyone needs it) extended rather easily to handle other similar pairs of ICMP messages (Timestamp/Reply, Information Request/Reply, Address Mask Request/Reply etc.).

Userspace ping util & patch for it:
ftp://mirrors.kernel.org/openwall/Owl/current/sources/Owl/packages/iputils/iputils-ss020927.tar.gz ftp://ftp.intelib.org/pub/segoon/iputils-ss020927-pingsock.diff

Similar functionality is implemented in Mac OS X:
http://www.manpagez.com/man/4/icmp/

TODO: implement ICMPv6 sockets.

All ping options (-b, -p, -Q, -R, -s, -t, -T, -M, -I), are tested with the patch.

Tested with 2K "ping -i0.2" running on Core i7 (8 cores), Load average = 80, and with 20 simultanious "ping -c1", each for half an hour.

Initially this functionality was written by Pavel Kankovsky for linux 2.4.32, but unfortunately it was never made public.

All comments are appreciated, especially about:

1) locking (get_port, lookup, unhash) - IMO ping sockets are too rarely used to optimise it.
2) necessity of ICMP_TIMESTAMP, ICMP_INFO_REQUEST, ICMP_ADDRESS - does anybody use it nowadays?
3) whether it's better to stay in net/ipv4/ping.c or move to net/ipv4/icmp.c

--

include/net/ping.h new file +68 -0
new file mode 100644
index 0000000..96ba9e8
@@ -0,0 +1,68 @@
+/*
+ * INET		An implementation of the TCP/IP protocol suite for the LINUX
+ *		operating system.  INET is implemented using the  BSD Socket
+ *		interface as the means of communication with the user level.
+ *
+ *		Definitions for the "ping" module.
+ *
+ *		This program is free software; you can redistribute it and/or
+ *		modify it under the terms of the GNU General Public License
+ *		as published by the Free Software Foundation; either version
+ *		2 of the License, or (at your option) any later version.
+ */
+#ifndef _PING_H
+#define _PING_H
+
+#include <net/netns/hash.h>
+
+#ifdef CONFIG_IP_PING_DEBUG
+#define ping_debug(fmt, x...) printk(KERN_INFO fmt, ## x)
+#else
+#define ping_debug(fmt, x...) do {} while (0)
+#endif
+
+/* PING_HTABLE_SIZE must be power of 2 */
+#define PING_HTABLE_SIZE 	64
+#define PING_HTABLE_MASK 	(PING_HTABLE_SIZE-1)
+
+#define ping_portaddr_for_each_entry(__sk, node, list) \
+	hlist_nulls_for_each_entry(__sk, node, list, sk_nulls_node)
+
+#define MAX_PING_IDENT 	65536
+
+
+struct ping_hslot {
+	struct hlist_nulls_head	head;
+} __attribute__((aligned(2 * sizeof(long))));
+
+struct ping_table {
+	struct ping_hslot	hash[PING_HTABLE_SIZE];
+	rwlock_t		lock;
+};
+
+struct ping_iter_state {
+	struct seq_net_private  p;
+	int			bucket;
+};
+
+extern struct proto ping_prot;
+
+
+#ifdef CONFIG_IP_PING
+#define icmp_echoreply ping_rcv
+#else
+#define icmp_echoreply icmp_discard
+#endif
+
+extern void ping_rcv(struct sk_buff *);
+extern void ping_err(struct sk_buff *, u32 info);
+
+#ifdef CONFIG_PROC_FS
+extern int __init ping_proc_init(void);
+extern void ping_proc_exit(void);
+#endif
+
+void __init ping_init(void);
+
+
+#endif /* _PING_H */
net/ipv4/Kconfig +21 -0
index 9e95d7f..5cb13a3 100644
@@ -14,6 +14,27 @@ config IP_MULTICAST
 	  <file:Documentation/networking/multicast.txt>. For most people, it's
 	  safe to say N.
 
+config IP_PING
+	bool "IP: ping socket"
+	depends on EXPERIMENTAL
+	help
+	  This option introduces a new kind of sockets - "ping sockets".
+
+	  A ping socket makes it possible to send ICMP Echo messages and receive
+	  corresponding ICMP Echo Reply messages without any special privileges.
+	  In other words, it makes is possible to implement setuid-less /bin/ping.
+
+	  A new ping socket is created with socket(PF_INET, SOCK_DGRAM, PROT_ICMP).
+
+config IP_PING_DEBUG
+	bool "IP: ping socket debug output"
+	depends on IP_PING
+	default n
+	help
+	  Enable the inclusion of debug code in the ICMP ping sockets.
+	  Be aware that doing this will impact performance.
+	  If unsure say N.
+
 config IP_ADVANCED_ROUTER
 	bool "IP: advanced router"
 	---help---
net/ipv4/Makefile +1 -0
index 4978d22..3a37479 100644
@@ -19,6 +19,7 @@ obj-$(CONFIG_IP_FIB_TRIE) += fib_trie.o
 obj-$(CONFIG_PROC_FS) += proc.o
 obj-$(CONFIG_IP_MULTIPLE_TABLES) += fib_rules.o
 obj-$(CONFIG_IP_MROUTE) += ipmr.o
+obj-$(CONFIG_IP_PING) += ping.o
 obj-$(CONFIG_NET_IPIP) += ipip.o
 obj-$(CONFIG_NET_IPGRE_DEMUX) += gre.o
 obj-$(CONFIG_NET_IPGRE) += ip_gre.o
net/ipv4/af_inet.c +36 -0
index f581f77..bbe5eb3 100644
@@ -105,6 +105,7 @@
 #include <net/tcp.h>
 #include <net/udp.h>
 #include <net/udplite.h>
+#include <net/ping.h>
 #include <linux/skbuff.h>
 #include <net/sock.h>
 #include <net/raw.h>
@@ -992,6 +993,16 @@ static struct inet_protosw inetsw_array[] =
 		.flags =      INET_PROTOSW_PERMANENT,
        },
 
+#ifdef CONFIG_IP_PING
+       {
+		.type =       SOCK_DGRAM,
+		.protocol =   IPPROTO_ICMP,
+		.prot =       &ping_prot,
+		.ops =        &inet_dgram_ops,
+		.no_check =   UDP_CSUM_DEFAULT,
+		.flags =      INET_PROTOSW_REUSE,
+       },
+#endif
 
        {
 	       .type =       SOCK_RAW,
@@ -1520,6 +1531,9 @@ static const struct net_protocol udp_protocol = {
 
 static const struct net_protocol icmp_protocol = {
 	.handler =	icmp_rcv,
+#ifdef CONFIG_IP_PING
+	.err_handler =	ping_err,
+#endif
 	.no_policy =	1,
 	.netns_ok =	1,
 };
@@ -1635,6 +1649,12 @@ static int __init inet_init(void)
 	if (rc)
 		goto out_unregister_udp_proto;
 
+#ifdef CONFIG_IP_PING
+	rc = proto_register(&ping_prot, 1);
+	if (rc)
+		goto out_unregister_raw_proto;
+#endif
+
 	/*
 	 *	Tell SOCKET that we are alive...
 	 */
@@ -1690,6 +1710,10 @@ static int __init inet_init(void)
 	/* Add UDP-Lite (RFC 3828) */
 	udplite4_register();
 
+#ifdef CONFIG_IP_PING
+	ping_init();
+#endif
+
 	/*
 	 *	Set the ICMP layer up
 	 */
@@ -1720,6 +1744,10 @@ static int __init inet_init(void)
 	rc = 0;
 out:
 	return rc;
+#ifdef CONFIG_IP_PING
+out_unregister_raw_proto:
+	proto_unregister(&raw_prot);
+#endif
 out_unregister_udp_proto:
 	proto_unregister(&udp_prot);
 out_unregister_tcp_proto:
@@ -1744,11 +1772,19 @@ static int __init ipv4_proc_init(void)
 		goto out_tcp;
 	if (udp4_proc_init())
 		goto out_udp;
+#ifdef CONFIG_IP_PING
+	if (ping_proc_init())
+		goto out_ping;
+#endif
 	if (ip_misc_proc_init())
 		goto out_misc;
 out:
 	return rc;
 out_misc:
+#ifdef CONFIG_IP_PING
+	ping_proc_exit();
+out_ping:
+#endif
 	udp4_proc_exit();
 out_udp:
 	tcp4_proc_exit();
net/ipv4/icmp.c +13 -1
index e5d1a44..83232e2 100644
@@ -83,6 +83,7 @@
 #include <net/tcp.h>
 #include <net/udp.h>
 #include <net/raw.h>
+#include <net/ping.h>
 #include <linux/skbuff.h>
 #include <net/sock.h>
 #include <linux/errno.h>
@@ -808,6 +809,17 @@ static void icmp_redirect(struct sk_buff *skb)
 			       iph->saddr, skb->dev);
 		break;
 	}
+
+#ifdef CONFIG_IP_PING
+	/* Ping wants to see redirects.
+         * Let's pretend they are errors of sorts... */
+	if (iph->protocol == IPPROTO_ICMP &&
+	    iph->ihl >= 5 &&
+	    pskb_may_pull(skb, (iph->ihl<<2)+8)) {
+		ping_err(skb, icmp_hdr(skb)->un.gateway);
+	}
+#endif
+
 out:
 	return;
 out_err:
@@ -1068,7 +1080,7 @@ error:
  */
 static const struct icmp_control icmp_pointers[NR_ICMP_TYPES + 1] = {
 	[ICMP_ECHOREPLY] = {
-		.handler = icmp_discard,
+		.handler = icmp_echoreply,
 	},
 	[1] = {
 		.handler = icmp_discard,
net/ipv4/ping.c new file +894 -0

894 lines not on this page · raw message

Signature · 1 line
Vasiliy
#1 lore raw
Quoted 10 lines · Vasiliy Kulikov

Hi,

This patch adds IPPROTO_ICMP socket kind.  It makes it possible to send ICMP_ECHO messages and receive corresponding ICMP_ECHOREPLY messages without any special privileges.  In other words, the patch makes it possible to implement setuid-less /bin/ping.

A new ping socket is created with

   socket(PF_INET, SOCK_DGRAM, IPPROTO_ICMP)

And the default is to allow any uid to do this (modulo LSM)?

If you really have a burning desire to get rid of setuid /bin/ping, why not just do it in userspace via message passing to/from a privileged process, and avoid a lot of code in the kernel? It's much more flexible. You could, for example, limit it to once a second by default, allow only one process doing this per uid, etc.

#2 lore raw
Quoted 6 lines · Colin Walters
Quoted 3 lines · Vasiliy Kulikov

A new ping socket is created with

socket(PF_INET, SOCK_DGRAM, IPPROTO_ICMP)

And the default is to allow any uid to do this (modulo LSM)?

We intend to have this sysctl'able and to have it restricted to a group by default (the sysctl would set the GID) on our Linux distro, Openwall GNU/*/Linux. However, we figured that it'd be tough for us to get this complication accepted into mainstream, so we opted to have the patch posted for comment without it.

Quoted 3 lines

If you really have a burning desire to get rid of setuid /bin/ping, why not just do it in userspace via message passing to/from a privileged process, and avoid a lot of code in the kernel?

Yes, we thought of that, and we don't like this solution. We similarly (but for different reasons) don't like using fscaps to grant CAP_NET_RAW to ping.

We share your concern about the size of net/ipv4/ping.c introduced by this patch, yet this is our current proposal.

Quoted 3 lines

It's much
more flexible. You could, for example, limit it to once a second by default, allow only one process doing this per uid, etc.

We figured that there's little point behind such restrictions. Just how is an ICMP echo request any worse than a UDP packet of the same size? Anyone can send the latter with current kernels.

Additionally, Vasiliy found out that Mac OS X has a similar feature, implemented in a riskier way than what we propose (they do no filtering of incoming ICMP traffic):

http://www.manpagez.com/man/4/icmp/

So there's precedent, and our proposal is better.

Yet, as I have mentioned, we're in fact going to restrict this to a group by default and to have ping SGID - just not to expose the extra kernel code for direct attack by a local user. That's in case there's a vulnerability in the added code.

If a sysctl like this is what others want to have as well, we'd be happy to provide a revision of the patch including that. Then we won't have to maintain it as a custom patch.

Thank you for your criticism.

Alexander Peslyak <solar at openwall.com>
GPG key ID: 5B341F15 fp: B3FB 63F4 D7A3 BCCC 6F6E FC55 A2FC 027C 5B34 1F15
http://www.openwall.com - bringing security into open computing environments

#3 lore raw
Quoted 6 lines · Solar Designer
Quoted 5 lines · Colin Walters
Quoted 3 lines · Vasiliy Kulikov

A new ping socket is created with

 socket(PF_INET, SOCK_DGRAM, IPPROTO_ICMP)

Quoted 7 lines
Quoted 1 line

And the default is to allow any uid to do this (modulo LSM)?

We intend to have this sysctl'able and to have it restricted to a group by default (the sysctl would set the GID) on our Linux distro, Openwall GNU/*/Linux.  However, we figured that it'd be tough for us to get this complication accepted into mainstream, so we opted to have the patch posted for comment without it.

Right, a sysctl was the obvious thing to have.

Quoted 5 lines
Quoted 3 lines

If you really have a burning desire to get rid of setuid /bin/ping, why not just do it in userspace via message passing to/from a privileged process, and avoid a lot of code in the kernel?

Yes, we thought of that, and we don't like this solution.

...because?

Quoted 3 lines

 We similarly
(but for different reasons) don't like using fscaps to grant CAP_NET_RAW to ping.

I am (learning now) about the fscaps drawbacks...

Quoted 2 lines

We share your concern about the size of net/ipv4/ping.c introduced by this patch, yet this is our current proposal.

To be clear I have no personal stake in the size of net/; my concern is more about the set of permissions granted by the default kernel configuration. Both from an OS developer standpoint, and also just so I feel I can continue to explain to someone who's learning about Linux all the interactions between the uid/gid, capabilities, SELinux, etc. If the kernel starts adding extensions to things it historically didn't, that gets even more complicated.

Quoted 3 lines

We figured that there's little point behind such restrictions.  Just how is an ICMP echo request any worse than a UDP packet of the same size? Anyone can send the latter with current kernels.

Clearly if we could go back in time and make some changes to the default Unix security model, one of those would probably be making allocating TCP/UDP sockets a privileged operation in some way. But the ship has sailed there...

Quoted 4 lines

Yet, as I have mentioned, we're in fact going to restrict this to a group by default and to have ping SGID - just not to expose the extra kernel code for direct attack by a local user.  That's in case there's a vulnerability in the added code.

So wait...this whole patch is to remove the setuid bit, but then you're going to go back and add setgid? How is that really compellingly different and better?

#4 lore raw
Quoted 8 lines · Colin Walters
Quoted 5 lines · Solar Designer

We intend to have this sysctl'able and to have it restricted to a group by default (the sysctl would set the GID) on our Linux distro, Openwall GNU/*/Linux.  However, we figured that it'd be tough for us to get this complication accepted into mainstream, so we opted to have the patch posted for comment without it.

Right, a sysctl was the obvious thing to have.

If others don't mind either, we'd be very happy for this to get in. Vasiliy can include it in the next revision of the patch he posts.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

If you really have a burning desire to get rid of setuid /bin/ping, why not just do it in userspace via message passing to/from a privileged process, and avoid a lot of code in the kernel?

Yes, we thought of that, and we don't like this solution.

...because?

The helper daemon process would need to be constantly running, and it would consume somewhat more resources than just a piece of extra kernel code would. Its complexity would be slightly higher. It may get OOM-killed or the like (the cause may be external to it), after which point the functionality will disappear. We use OpenVZ containers heavily, and we don't want to run an instance of such process per container (up to a few hundred per system). We also don't want to introduce a container to host channel for this (would be a hack). We already have the process to kernel interface, which works equally well from containers, so we prefer to just use that.

Quoted 5 lines
Quoted 3 lines

 We similarly
(but for different reasons) don't like using fscaps to grant CAP_NET_RAW to ping.

I am (learning now) about the fscaps drawbacks...

Here's some info on the topic:

http://www.openwall.com/lists/oss-security/2010/11/08/3

You may also want to read the follow-ups ("thread-next"). Unfortunately, I failed to find time to continue that discussion thread myself, although I still hope to.

Quoted 10 lines
Quoted 2 lines

We share your concern about the size of net/ipv4/ping.c introduced by this patch, yet this is our current proposal.

To be clear I have no personal stake in the size of net/; my concern is more about the set of permissions granted by the default kernel configuration. Both from an OS developer standpoint, and also just so I feel I can continue to explain to someone who's learning about Linux all the interactions between the uid/gid, capabilities, SELinux, etc. If the kernel starts adding extensions to things it historically didn't, that gets even more complicated.

Oh, this is a very minor change from this point of view. There was already a related change in 2.3.x that permitted Olaf Kirch's implementation of traceroute to work as non-root on Linux 2.4+ (ability to receive ICMP errors). We only want the extra bits of functionality needed for ping available as well.

Quoted 8 lines
Quoted 3 lines

We figured that there's little point behind such restrictions.  Just how is an ICMP echo request any worse than a UDP packet of the same size? Anyone can send the latter with current kernels.

Clearly if we could go back in time and make some changes to the default Unix security model, one of those would probably be making allocating TCP/UDP sockets a privileged operation in some way. But the ship has sailed there...

I see no problem here. It is OK to have this available to all users by default, but then to allow, say, a privsep child process in a daemon to give up some of the "privileges" of a traditional Unix process - including socket allocation.

Quoted 8 lines
Quoted 4 lines

Yet, as I have mentioned, we're in fact going to restrict this to a group by default and to have ping SGID - just not to expose the extra kernel code for direct attack by a local user.  That's in case there's a vulnerability in the added code.

So wait...this whole patch is to remove the setuid bit, but then you're going to go back and add setgid? How is that really compellingly different and better?

ping would otherwise be either available to root only or SUID root. If there are no other SUID root programs - like there are not in a default install of Owl 3.0 (our passwd, crontab, at work without SUID due to changes we made elsewhere) - then it means that ping's SUIDness would expose the program startup code in ld/libc/kernel to potential attacks. There were numerous relevant vulnerabilities in the past.

SGID would not actually be a problem. On the contrary, it would introduce a second layer of security - protecting the kernel code related to ICMP sockets from direct attacks. If there's a vulnerability in ld/libc/ping that results in a compromise of the group, this merely removes this second layer of security. It does not result in a compromise of the system on its own.

You might want to refer to the following pages for info on how our SUID-less userland is possible (without fscaps):

http://www.openwall.com/Owl/
http://www.openwall.com/tcb/
http://www.openwall.com/presentations/Owl/mgp00013.html
http://www.openwall.com/presentations/Owl/mgp00020.html
http://www.openwall.com/presentations/Owl/mgp00021.html
http://www.openwall.com/presentations/Owl/mgp00022.html
http://www.openwall.com/presentations/Owl/mgp00023.html

ping is the only problem we have not solved yet. On Owl 3.0, we install it mode 700 by default for this reason, and the "control ping public" command makes it mode 4711 (re-introducing the risk). Thus, this ICMP sockets feature is fairly important for us. It's the last missing bit.

ping also appears to be the only program that would benefit from fscaps for real. (In other cases, different solutions are possible and/or the capability set would be root-equivalent.) Rather than have the system depend on fscaps, which is inconvenient (e.g., backup/restore issues), we can address the specific case of ping.

Alexander

#5 lore raw
Quoted 3 lines · Solar Designer

We use OpenVZ containers
heavily, and we don't want to run an instance of such process per container (up to a few hundred per system).

Besides memory usage daemons cannot allocate reliably unique icmp echo identifiers as they don't know anything about already assigned idents.

Thanks,

Signature · 2 lines
Vasiliy Kulikov
http://www.openwall.com - bringing security into open computing environments