diff mbox series

net/socket: clamp negative backlog value to 0 in listen()

Message ID 20240628172836.19213-1-cosiekvfj@o2.pl (mailing list archive)
State Rejected
Delegated to: Netdev Maintainers
Headers show
Series net/socket: clamp negative backlog value to 0 in listen() | expand

Checks

Context Check Description
netdev/series_format warning Single patches do not need cover letters; Target tree name not specified in the subject
netdev/tree_selection success Guessed tree name to be net-next
netdev/ynl success Generated files up to date; no warnings/errors; no diff in generated;
netdev/fixes_present success Fixes tag not required for -next series
netdev/header_inline success No static functions without inline keyword in header files
netdev/build_32bit success Errors and warnings before: 839 this patch: 839
netdev/build_tools success No tools touched, skip
netdev/cc_maintainers success CCed 4 of 4 maintainers
netdev/build_clang success Errors and warnings before: 846 this patch: 846
netdev/verify_signedoff success Signed-off-by tag matches author and committer
netdev/deprecated_api success None detected
netdev/check_selftest success No net selftest shell script
netdev/verify_fixes success No Fixes tag
netdev/build_allmodconfig_warn success Errors and warnings before: 847 this patch: 847
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 9 lines checked
netdev/build_clang_rust success No Rust files in patch. Skipping build
netdev/kdoc success Errors and warnings before: 24 this patch: 24
netdev/source_inline success Was 0 now: 0
netdev/contest fail net-next-2024-06-29--00-00 (tests: 664)

Commit Message

Kacper Piwiński June 28, 2024, 5:28 p.m. UTC
According to manual: https://man7.org/linux/man-pages/man3/listen.3p.html
If listen() is called with a backlog argument value that is less
than 0, the function behaves as if it had been called with a
backlog argument value of 0.

Signed-off-by: Kacper Piwiński <cosiekvfj@o2.pl>
---
 net/socket.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

Comments

Kuniyuki Iwashima June 28, 2024, 9:58 p.m. UTC | #1
From Kacper Piwiński <cosiekvfj@o2.pl>
Date: Fri, 28 Jun 2024 19:28:36 +0200
> According to manual: https://man7.org/linux/man-pages/man3/listen.3p.html
> If listen() is called with a backlog argument value that is less
> than 0, the function behaves as if it had been called with a
> backlog argument value of 0.

This breaks many applications that assume listen(fd, -1) configures
the backlog with the max value allowed in the netns.

The behaviour is useful especially in a container-like env where app
does not have access to procfs.

The man page should be updated instead.


> 
> Signed-off-by: Kacper Piwiński <cosiekvfj@o2.pl>
> ---
>  net/socket.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/net/socket.c b/net/socket.c
> index e416920e9..9567223d7 100644
> --- a/net/socket.c
> +++ b/net/socket.c
> @@ -1873,8 +1873,7 @@ int __sys_listen(int fd, int backlog)
>  	sock = sockfd_lookup_light(fd, &err, &fput_needed);
>  	if (sock) {
>  		somaxconn = READ_ONCE(sock_net(sock->sk)->core.sysctl_somaxconn);
> -		if ((unsigned int)backlog > somaxconn)
> -			backlog = somaxconn;
> +		backlog = clamp(backlog, 0, somaxconn);
>  
>  		err = security_socket_listen(sock, backlog);
>  		if (!err)
> --
diff mbox series

Patch

diff --git a/net/socket.c b/net/socket.c
index e416920e9..9567223d7 100644
--- a/net/socket.c
+++ b/net/socket.c
@@ -1873,8 +1873,7 @@  int __sys_listen(int fd, int backlog)
 	sock = sockfd_lookup_light(fd, &err, &fput_needed);
 	if (sock) {
 		somaxconn = READ_ONCE(sock_net(sock->sk)->core.sysctl_somaxconn);
-		if ((unsigned int)backlog > somaxconn)
-			backlog = somaxconn;
+		backlog = clamp(backlog, 0, somaxconn);
 
 		err = security_socket_listen(sock, backlog);
 		if (!err)