net: ipv4: add IPPROTO_ICMP socket kind
Subsystems (3)
PATCH 1/1 net: ipv4: add IPPROTO_ICMP socket kind
This patch adds IPPROTO_ICMP socket kind. It makes it possible to send ICMP_ECHO messages and receive the corresponding ICMP_ECHOREPLY messages without any special privileges. In other words, the patch makes it possible to implement setuid-less and CAP_NET_RAW-less /bin/ping. In order not to increase the kernel's attack surface (in case of vulnerabilities in the newly added code), the new functionality is disabled by default, but is enabled at bootup by supporting Linux distributions, optionally with restriction to a group or a group range (see below).
Similar functionality is implemented in Mac OS X:
http://www.manpagez.com/man/4/icmp/
A new ping socket is created with
socket(PF_INET, SOCK_DGRAM, PROT_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 are 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).
socket(2) is restricted to the group range specified in "/proc/sys/net/ipv4/ping_group_range". It is "1 0" by default, meaning that nobody (not even root) may create ping sockets. Setting it to "100 100" would grant permissions to the single group, "0 65535" would enable it for the world.
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:
http://openwall.info/wiki/segoon/ping
A revision of this patch (for RHEL5/OpenVZ kernels) is in use in Owl-current, such as in the 2011/03/12 LiveCD ISOs:
http://mirrors.kernel.org/openwall/Owl/current/iso/
For Openwall GNU/*/Linux it is the last step on the road to the setuid-less distro.
Initially this functionality was written by Pavel Kankovsky (CC'ed him) for linux 2.4.32, but unfortunately it was never made public.
Reference to the previous discussion:
http://lwn.net/Articles/420801/
All ping options (-b, -p, -Q, -R, -s, -t, -T, -M, -I), are tested with the patch.
Changes since RFCv2:
- fixed checksumming bug.
- CAP_NET_RAW may not create icmp sockets anymore.
Changes since RFCv1:
- minor cleanups.
- introduced sysctl'able group range to restrict socket(2).
--- include/net/netns/ipv4.h | 2 + include/net/ping.h | 69 ++++ net/ipv4/Kconfig | 21 + net/ipv4/Makefile | 1 + net/ipv4/af_inet.c | 36 ++ net/ipv4/icmp.c | 14 +- net/ipv4/ping.c | 933 ++++++++++++++++++++++++++++++++++++++++++++ net/ipv4/sysctl_net_ipv4.c | 90 +++++ 8 files changed, 1165 insertions(+), 1 deletions(-) create mode 100644 include/net/ping.h create mode 100644 net/ipv4/ping.c
include/net/netns/ipv4.h +2 -0
@@ -55,6 +55,8 @@ struct netns_ipv4 {int sysctl_rt_cache_rebuild_count;int current_rt_cache_rebuild_count; +unsigned int sysctl_ping_group_range[2 ]; +atomic_t rt_genid;#ifdef CONFIG_IP_MROUTE
include/net/ping.h new file +69 -0
69 lines not on this page · raw message
net/ipv4/Kconfig +21 -0
@@ -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
@@ -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
@@ -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>
@@ -1008,6 +1009,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,
@@ -1528,6 +1539,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 , };
@@ -1643,6 +1657,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... */
@@ -1698,6 +1718,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 */
@@ -1728,6 +1752,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:
@@ -1752,11 +1780,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: +#endifudp4_proc_exit (); out_udp:tcp4_proc_exit ();
net/ipv4/icmp.c +13 -1
@@ -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>
@@ -798,6 +799,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:
@@ -1058,7 +1070,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 +933 -0
933 lines not on this page · raw message
net/ipv4/sysctl_net_ipv4.c +90 -0
128 lines not on this page · raw message
Signature · 1 line
1.7.0.4
David Miller
maintainerFrom: Vasiliy Kulikov <[email protected]>
Date: Fri, 18 Mar 2011 21:00:02 +0300
Quoted 1 line
This patch adds
IPPROTO_ICMPsocket kind.
I kindly asked the other day for people to not submit new feature patches while we are amidst the merge window:
http://marc.info/?l=linux-netdev&m=130023005214910&w=2
Please, if you want to do networking development, pay attention to such important announcements which decreases the burdon upon me and makes everything operate more smoothly.
Thanks.
Quoted 2 lines · David Miller
I kindly asked the other day for people to not submit new feature patches while we are amidst the merge window:
Sorry, I should have grepped the archives before sending the patch (I'm not a subscriber of netdev@). I'll wait for .39-rc1.
Thanks,
Signature · 2 lines
Vasiliy Kulikov http://www.openwall.com - bringing security into open computing environments