Skip to contentSkip to conversation

net: Check the argument for listen(2)

PATCH Superseded Changli Gao 4 participants 6 messages Jun 28, 2013

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 net: Check the argument for listen(2)

As we use u16 to save the value of the argument for listen(2), we'd better check if the value is larger than SINT_MAX other than cut it down silently on error.

---
 net/ipv4/af_inet.c |    3 +++
 1 file changed, 3 insertions(+)
net/ipv4/af_inet.c +3 -0
index b4d0be2..35aaf00 100644
@@ -198,6 +198,9 @@ int inet_listen(struct socket *sock, int backlog)
 	unsigned char old_state;
 	int err;
 
+	if (backlog >= (1 << 16))
+		return EINVAL;
+
 	lock_sock(sk);
 
 	err = -EINVAL;
Signature · 1 line
1.7.9.5
#1 lore raw
Quoted 12 lines · Changli Gao

As we use u16 to save the value of the argument for listen(2), we'd better check if the value is larger than SINT_MAX other than cut it down silently on error.

---
 net/ipv4/af_inet.c |    3 +++
 1 file changed, 3 insertions(+)
net/ipv4/af_inet.c +0 -0
index b4d0be2..35aaf00 100644

net/ipv4/af_inet.c int inet_listen(struct socket *sock, int backlog) · line 206

Quoted 9 lines
net/ipv4/af_inet.c +3 -0
@@ -198,6 +198,9 @@ int inet_listen(struct socket *sock, int backlog)
 	unsigned char old_state;
 	int err;
 
+	if (backlog >= (1 << 16))
+		return EINVAL;
+
 	lock_sock(sk);
 
 	err = -EINVAL;

This seems pretty buggy to me.

1) EINVAL is not -EINVAL

2) This might break existing applications.

#5 lore raw
In reply to Changli Gao
Quoted 12 lines · On Sat, 29 Jun 2013 00:14:36 +0800 Changli Gao <[email protected]> wrote:

As we use u16 to save the value of the argument for listen(2), we'd better check if the value is larger than SINT_MAX other than cut it down silently on error.

---
 net/ipv4/af_inet.c |    3 +++
 1 file changed, 3 insertions(+)
net/ipv4/af_inet.c +0 -0
index b4d0be2..35aaf00 100644

net/ipv4/af_inet.c int inet_listen(struct socket *sock, int backlog) · line 206

Quoted 9 lines
net/ipv4/af_inet.c +3 -0
@@ -198,6 +198,9 @@ int inet_listen(struct socket *sock, int backlog)
 	unsigned char old_state;
 	int err;
 
+	if (backlog >= (1 << 16))
+		return EINVAL;
+
 	lock_sock(sk);
 
 	err = -EINVAL;

Is this just a theoretical problem or is there an example? Since this is a user API, you should check related standards, it maybe that the kernel should just silently truncate. Returning errors to legacy applications makes people really angry.

If you decide that returning an error is the correct behaviour then the return value should be negative in order to follow kernel practice and other parts of same code.

Rather than open coding >= (1<<16) might be better to > USHRT_MAX.

Lastly, a test like this deserves a comment like:

       /* since sk_max_ack_backlog is ushort */
#2 lore raw
In reply to Eric Dumazet
Quoted 12 lines · Eric Dumazet
Quoted 11 lines · Changli Gao
net/ipv4/af_inet.c +3 -0
@@ -198,6 +198,9 @@ int inet_listen(struct socket *sock, int backlog)
 	unsigned char old_state;
 	int err;
 
+	if (backlog >= (1 << 16))
+		return EINVAL;
+
 	lock_sock(sk);
 
Quoted 7 lines

net/ipv4/af_inet.c int inet_listen(struct socket *sock, int backlog) · line 206

Quoted 1 line
net/ipv4/af_inet.c +0 -0
 	err = -EINVAL;

This seems pretty buggy to me.

1) EINVAL is not -EINVAL

2) This might break existing applications.

Yes, we should clamp the value to netdev_max_backlog.

Greetings,

Hannes

#3 lore raw
Quoted 17 lines · Hannes Frederic Sowa
Quoted 16 lines · Eric Dumazet

net/ipv4/af_inet.c int inet_listen(struct socket *sock, int backlog) · line 206

Quoted 12 lines · Changli Gao
net/ipv4/af_inet.c +3 -0
@@ -198,6 +198,9 @@ int inet_listen(struct socket *sock, int backlog)
 	unsigned char old_state;
 	int err;
 
+	if (backlog >= (1 << 16))
+		return EINVAL;
+
 	lock_sock(sk);
 
 	err = -EINVAL;

This seems pretty buggy to me.

Quoted 5 lines
Quoted 3 lines

1) EINVAL is not -EINVAL

2) This might break existing applications.

Yes, we should clamp the value to netdev_max_backlog.

Nonsense, somaxconn would make more sense.

Sorry,

Hannes

#4 lore raw
Quoted 2 lines · Hannes Frederic Sowa

Nonsense, somaxconn would make more sense.

If we keep u16, we could limit the sysctl (somaxconn, tcp_max_syn_backlog) limits to 65535