Skip to contentSkip to conversation

net: reserve ports for applications using fixed port numbers

RFC PATCH Superseded Amerigo Wang 2 participants 5 messages Feb 4, 2010 – Feb 8, 2010

Subsystems (8)

v2 of 10 Superseded Latest is v10 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH v2 1/1 net: reserve ports for applications using fixed port numbers

V2:
update the documentation
update the changelog
fix the checking code in udp

This patch introduces /proc/sys/net/ipv4/ip_local_reserved_ports, it can be used like ip_local_port_range, but this is used to reserve ports for third-party applications which use fixed port numbers within ip_local_port_range.

This only affects the applications which call socket functions like bind(2) with port number 0, or connect() etc., to prevent the kernel getting the ports within the specified range for them. For applications which use fixed port number, it will have no effects.

Any comments are welcome.

Cc Octavian Purdila <[email protected]>
Cc David Miller <[email protected]>
Cc Neil Horman <[email protected]>
Cc Eric Dumazet <[email protected]>

---

Documentation/networking/ip-sysctl.txt +8 -0
index 006b39d..0795ac3 100644
@@ -564,6 +564,14 @@ ip_local_port_range - 2 INTEGERS
 	(i.e. by default) range 1024-4999 is enough to issue up to
 	2000 connections per second to systems supporting timestamps.
 
+ip_local_reserved_ports - 2 INTEGERS
+	Specify the port range which is reserved for known third-party
+	applications, in case the kernel picks those ports for other
+	applications, e.g. when calling connect() or bind() with port
+	number 0. The range shall not go beyond the range specifed in
+	ip_local_port_range. "0 0" means no ports are reserved.
+	Default: 0 0
+
 ip_nonlocal_bind - BOOLEAN
 	If set, allows processes to bind() to non-local IP addresses,
 	which can be quite useful - but may break some applications.
drivers/infiniband/core/cma.c +6 -1
index cc9b594..8248fc6 100644
@@ -1979,6 +1979,8 @@ retry:
 	/* FIXME: add proper port randomization per like inet_csk_get_port */
 	do {
 		ret = idr_get_new_above(ps, bind_list, next_port, &port);
+		if (inet_is_reserved_local_port(port))
+			ret = -EAGAIN;
 	} while ((ret == -EAGAIN) && idr_pre_get(ps, GFP_KERNEL));
 
 	if (ret)
@@ -2997,10 +2999,13 @@ static int __init cma_init(void)
 {
 	int ret, low, high, remaining;
 
-	get_random_bytes(&next_port, sizeof next_port);
 	inet_get_local_port_range(&low, &high);
+again:
+	get_random_bytes(&next_port, sizeof next_port);
 	remaining = (high - low) + 1;
 	next_port = ((unsigned int) next_port % remaining) + low;
+	if (inet_is_reserved_local_port(next_port))
+		goto again;
 
 	cma_wq = create_singlethread_workqueue("rdma_cm");
 	if (!cma_wq)
include/net/ip.h +3 -1
index fb63371..f70acad 100644
@@ -181,8 +181,10 @@ extern void snmp_mib_free(void *ptr[2]);
 extern struct local_ports {
 	seqlock_t	lock;
 	int		range[2];
-} sysctl_local_ports;
+} sysctl_local_ports, sysctl_local_reserved_ports;
 extern void inet_get_local_port_range(int *low, int *high);
+extern void inet_get_local_reserved_ports(int *from, int *to);
+extern int inet_is_reserved_local_port(int port);
 
 extern int sysctl_ip_default_ttl;
 extern int sysctl_ip_nonlocal_bind;
net/ipv4/inet_connection_sock.c +29 -0
index ee16475..ee13e48 100644
@@ -37,6 +37,11 @@ struct local_ports sysctl_local_ports __read_mostly = {
 	.range = { 32768, 61000 },
 };
 
+struct local_ports sysctl_local_reserved_ports __read_mostly = {
+	.lock = SEQLOCK_UNLOCKED,
+	.range = { 0, 0 },
+};
+
 void inet_get_local_port_range(int *low, int *high)
 {
 	unsigned seq;
@@ -49,6 +54,28 @@ void inet_get_local_port_range(int *low, int *high)
 }
 EXPORT_SYMBOL(inet_get_local_port_range);
 
+void inet_get_local_reserved_ports(int *from, int *to)
+{
+	unsigned int seq;
+	do {
+		seq = read_seqbegin(&sysctl_local_reserved_ports.lock);
+
+		*from = sysctl_local_reserved_ports.range[0];
+		*to = sysctl_local_reserved_ports.range[1];
+	} while (read_seqretry(&sysctl_local_reserved_ports.lock, seq));
+}
+
+int inet_is_reserved_local_port(int port)
+{
+	int min, max;
+
+	inet_get_local_reserved_ports(&min, &max);
+	if (min && max)
+		return (port >= min && port <= max);
+	return 0;
+}
+EXPORT_SYMBOL(inet_is_reserved_local_port);
+
 int inet_csk_bind_conflict(const struct sock *sk,
 			   const struct inet_bind_bucket *tb)
 {
@@ -105,6 +132,8 @@ again:
 		inet_get_local_port_range(&low, &high);
 		remaining = (high - low) + 1;
 		smallest_rover = rover = net_random() % remaining + low;
+		if (inet_is_reserved_local_port(rover))
+			goto again;
 
 		smallest_size = -1;
 		do {
net/ipv4/inet_hashtables.c +2 -0
index 2b79377..d3e160a 100644
@@ -456,6 +456,8 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row,
 		local_bh_disable();
 		for (i = 1; i <= remaining; i++) {
 			port = low + (i + offset) % remaining;
+			if (inet_is_reserved_local_port(port))
+				continue;
 			head = &hinfo->bhash[inet_bhashfn(net, port,
 					hinfo->bhash_size)];
 			spin_lock(&head->lock);
net/ipv4/sysctl_net_ipv4.c +53 -0

71 lines not on this page · raw message

net/ipv4/udp.c +5 -0
index f0126fd..4bb825e 100644
@@ -203,6 +203,7 @@ int udp_lib_get_port(struct sock *sk, unsigned short snum,
 
 	if (!snum) {
 		int low, high, remaining;
+		int min, max;
 		unsigned rand;
 		unsigned short first, last;
 		DECLARE_BITMAP(bitmap, PORTS_PER_CHAIN);
@@ -210,6 +211,7 @@ int udp_lib_get_port(struct sock *sk, unsigned short snum,
 		inet_get_local_port_range(&low, &high);
 		remaining = (high - low) + 1;
 
+again:
 		rand = net_random();
 		first = (((u64)rand * remaining) >> 32) + low;
 		/*
@@ -217,6 +219,9 @@ int udp_lib_get_port(struct sock *sk, unsigned short snum,
 		 */
 		rand = (rand | 1) * (udptable->mask + 1);
 		last = first + udptable->mask + 1;
+		inet_get_local_reserved_ports(&min, &max);
+		if (!(first > max || last < min))
+			goto again;
 		do {
 			hslot = udp_hashslot(udptable, net, first);
 			bitmap_zero(bitmap, PORTS_PER_CHAIN);
net/sctp/socket.c +2 -0
index 67fdac9..d685141 100644
@@ -5432,6 +5432,8 @@ static long sctp_get_port_local(struct sock *sk, union sctp_addr *addr)
 			rover++;
 			if ((rover < low) || (rover > high))
 				rover = low;
+			if (inet_is_reserved_local_port(rover))
+				continue;
 			index = sctp_phashfn(rover);
 			head = &sctp_port_hashtable[index];
 			sctp_spin_lock(&head->lock);
#1 lore raw

Hello.

net/ipv4/inet_hashtables.c int __inet_hash_connect(struct inet_timewait_death_row *death_row, · line 463

Quoted 13 lines · Amerigo Wang
net/ipv4/inet_hashtables.c +2 -0
index 2b79377..d3e160a 100644
@@ -456,6 +456,8 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row,
 		local_bh_disable();
 		for (i = 1; i <= remaining; i++) {
 			port = low + (i + offset) % remaining;
+			if (inet_is_reserved_local_port(port))
+				continue;
 			head = &hinfo->bhash[inet_bhashfn(net, port,
 					hinfo->bhash_size)];
 			spin_lock(&head->lock);

I'm planning to add a LSM hook here.

If root user sets min port value less than 1024 to /proc/sys/net/ipv4/ip_local_port_range , a process without CAP_NET_BIND_SERVICE capability can bind to privileged port by "bind() with port == 0" or "connect() without bind()" because the condition is

	err = -EACCES;
	if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
		goto out;

I consider this is a security problem if MAC is enabled. MAC is used for dividing root user's privilege. With MAC, somebody doing some part of root user's jobs may set min port value to less than 1024.

Also, some applications needs fixed local port numbers (e.g. 3128 for Squid, 8080 for Tomcat). The port numbers I want to reserve are more complex than simple min-max range like /proc/sys/net/ipv4/ip_local_reserved_ports .

Therefore, TOMOYO wants to insert a LSM hook (
http://tomoyo.sourceforge.jp/cgi-bin/lxr/source/net/ipv4/udp.c#L235
http://tomoyo.sourceforge.jp/cgi-bin/lxr/source/net/ipv4/inet_connection_sock.c#L114
http://tomoyo.sourceforge.jp/cgi-bin/lxr/source/net/ipv4/inet_hashtables.c#L459
) and allow reserving local ports like

deny_autobind 0-1023
deny_autobind 3128
deny_autobind 8080

so that

applications which need such ports won't be unexpectedly blocked by other application's temporary port usage (i.e. "bind() with port == 0" or "connect() without bind()")

and

MAC guarantees that processes without CAP_NET_BIND_SERVICE can never bind to privileged port

.
--
To unsubscribe from this list: send the line "unsubscribe linux-rdma" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html

author
#2 lore raw
Quoted 48 lines · Tetsuo Handa

Hello.

net/ipv4/inet_hashtables.c int __inet_hash_connect(struct inet_timewait_death_row *death_row, · line 463

Quoted 13 lines · Amerigo Wang
net/ipv4/inet_hashtables.c +2 -0
index 2b79377..d3e160a 100644
@@ -456,6 +456,8 @@ int __inet_hash_connect(struct inet_timewait_death_row *death_row,
 		local_bh_disable();
 		for (i = 1; i <= remaining; i++) {
 			port = low + (i + offset) % remaining;
+			if (inet_is_reserved_local_port(port))
+				continue;
 			head = &hinfo->bhash[inet_bhashfn(net, port,
 					hinfo->bhash_size)];
 			spin_lock(&head->lock);

I'm planning to add a LSM hook here.

If root user sets min port value less than 1024 to /proc/sys/net/ipv4/ip_local_port_range , a process without CAP_NET_BIND_SERVICE capability can bind to privileged port by "bind() with port == 0" or "connect() without bind()" because the condition is

	err = -EACCES;
	if (snum && snum < PROT_SOCK && !capable(CAP_NET_BIND_SERVICE))
		goto out;

I consider this is a security problem if MAC is enabled. MAC is used for dividing root user's privilege. With MAC, somebody doing some part of root user's jobs may set min port value to less than 1024.

Also, some applications needs fixed local port numbers (e.g. 3128 for Squid, 8080 for Tomcat). The port numbers I want to reserve are more complex than simple min-max range like /proc/sys/net/ipv4/ip_local_reserved_ports .

Therefore, TOMOYO wants to insert a LSM hook (
http://tomoyo.sourceforge.jp/cgi-bin/lxr/source/net/ipv4/udp.c#L235
http://tomoyo.sourceforge.jp/cgi-bin/lxr/source/net/ipv4/inet_connection_sock.c#L114
http://tomoyo.sourceforge.jp/cgi-bin/lxr/source/net/ipv4/inet_hashtables.c#L459
) and allow reserving local ports like

deny_autobind 0-1023
deny_autobind 3128
deny_autobind 8080

so that

Quoted 9 lines

applications which need such ports won't be unexpectedly blocked by other application's temporary port usage (i.e. "bind() with port == 0" or "connect() without bind()")

and

MAC guarantees that processes without CAP_NET_BIND_SERVICE can never bind to privileged port

Oh, IIUC, TOMOYO is something like SELinux? So, it is somewhat weird to let users to use TOMOYO to reserve the ports with MAC. For normal users /proc interface seems more friendly.

Thanks.

#3 lore raw

Re: [RFC Patch v2] net: reserve ports for applications using fixedport numbers

Quoted 1 line · Cong Wang

Oh, IIUC, TOMOYO is something like SELinux?

Yes. It is a policy based mandatory access control implementation which is applied to not only non root users but also root user. If MAC is enabled, root user cannot freely modify via sysctl() or /proc/sys interface.

Quoted 2 lines

So, it is somewhat weird to let users to use TOMOYO to reserve the ports with MAC.

To add reserved port

echo deny_autobind 0-1023 | ccs-loadpolicy -e
echo deny_autobind 3128 | ccs-loadpolicy -e
echo deny_autobind 8080 | ccs-loadpolicy -e

and to delete reserved port

echo delete deny_autobind 0-1023 | ccs-loadpolicy -e echo delete deny_autobind 3128 | ccs-loadpolicy -e echo delete deny_autobind 8080 | ccs-loadpolicy -e

That's all. Quite easy.

Quoted 1 line

For normal users /proc interface seems more friendly.

I think /proc/sys/net/ipv4/ip_local_reserved_ports interface wants "struct list_head" for handling multiple sets of min/max pairs. I'm using
http://tomoyo.sourceforge.jp/cgi-bin/lxr/source/security/ccsecurity/autobind.c#L29
for that purpose.

author
#4 lore raw

Re: [RFC Patch v2] net: reserve ports for applications using fixedport numbers

Quoted 16 lines · Tetsuo Handa
Quoted 1 line · Cong Wang

Oh, IIUC, TOMOYO is something like SELinux?

Yes. It is a policy based mandatory access control implementation which is applied to not only non root users but also root user. If MAC is enabled, root user cannot freely modify via sysctl() or /proc/sys interface.

Quoted 2 lines

So, it is somewhat weird to let users to use TOMOYO to reserve the ports with MAC.

To add reserved port

echo deny_autobind 0-1023 | ccs-loadpolicy -e
echo deny_autobind 3128 | ccs-loadpolicy -e
echo deny_autobind 8080 | ccs-loadpolicy -e

Quoted 7 lines

and to delete reserved port

echo delete deny_autobind 0-1023 | ccs-loadpolicy -e echo delete deny_autobind 3128 | ccs-loadpolicy -e echo delete deny_autobind 8080 | ccs-loadpolicy -e

That's all. Quite easy.

Hmm, but you are solving a non-security problem with a security tool, doesn't this look weird? ;-)

Quoted 7 lines
Quoted 2 lines

For normal users /proc interface seems more friendly.

I think /proc/sys/net/ipv4/ip_local_reserved_ports interface wants "struct list_head" for handling multiple sets of min/max pairs. I'm using
http://tomoyo.sourceforge.jp/cgi-bin/lxr/source/security/ccsecurity/autobind.c#L29
for that purpose.

Yes, but I didn't plan to add multiple range support for ip_local_reserved_ports, like ip_local_port_range.

Having that will be better but needs more efforts.

Thanks.