git: dbeadd4326a0 - main - linux: Allow connect() with destination port 0

From: Aymeric Wibo <obiwac_at_FreeBSD.org>
Date: Mon, 09 Nov 2026 13:25:35 UTC
The branch main has been updated by obiwac:

URL: https://cgit.FreeBSD.org/src/commit/?id=dbeadd4326a05d6a972e6e319a580944e136f931

commit dbeadd4326a05d6a972e6e319a580944e136f931
Author:     Aymeric Wibo <obiwac@FreeBSD.org>
AuthorDate: 2026-09-25 13:20:19 +0000
Commit:     Aymeric Wibo <obiwac@FreeBSD.org>
CommitDate: 2026-11-09 13:25:21 +0000

    linux: Allow connect() with destination port 0
    
    Linux allows connect() with destination port 0, but FreeBSD returns
    EADDRNOTAVAIL in in_pcbconnect() and in6_pcbladdr(). Some Linux programs
    (e.g. the new Android CLI tool) rely on this for routability checks,
    however.
    
    Add an SV_CONN_PORT0 flag to sv_flags which is set in the Linux sysvecs,
    allowing the port 0 guard in the connect paths to be bypassed.
    
    Reviewed by:    glebius, kib
    Differential Revision:  https://reviews.freebsd.org/D60659
---
 sys/i386/linux/linux_sysvec.c | 4 ++--
 sys/netinet/in_pcb.c          | 3 ++-
 sys/netinet6/in6_pcb.c        | 4 +++-
 sys/sys/sysent.h              | 5 +++--
 4 files changed, 10 insertions(+), 6 deletions(-)

diff --git a/sys/i386/linux/linux_sysvec.c b/sys/i386/linux/linux_sysvec.c
index 5153de5a442d..29de0ca9d2eb 100644
--- a/sys/i386/linux/linux_sysvec.c
+++ b/sys/i386/linux/linux_sysvec.c
@@ -596,8 +596,8 @@ struct sysentvec linux_sysvec = {
 	.sv_setregs	= linux_exec_setregs,
 	.sv_fixlimit	= NULL,
 	.sv_maxssiz	= NULL,
-	.sv_flags	= SV_ABI_LINUX | SV_AOUT | SV_ILP32 |
-	    SV_SIG_DISCIGN | SV_SIG_WAITNDQ | SV_PERSISTENT_CTTY,
+	.sv_flags	= SV_ABI_LINUX | SV_AOUT | SV_ILP32 | SV_SIG_DISCIGN |
+	    SV_SIG_WAITNDQ | SV_PERSISTENT_CTTY | SV_CONN_PORT0,
 	.sv_set_syscall_retval = linux_set_syscall_retval,
 	.sv_fetch_syscall_args = linux_fetch_syscall_args,
 	.sv_syscallnames = linux_syscallnames,
diff --git a/sys/netinet/in_pcb.c b/sys/netinet/in_pcb.c
index d72fb4cce587..5d2ea6ea3f4d 100644
--- a/sys/netinet/in_pcb.c
+++ b/sys/netinet/in_pcb.c
@@ -62,6 +62,7 @@
 #include <sys/priv.h>
 #include <sys/proc.h>
 #include <sys/refcount.h>
+#include <sys/sysent.h>
 #include <sys/jail.h>
 #include <sys/kernel.h>
 #include <sys/sysctl.h>
@@ -1150,7 +1151,7 @@ in_pcbconnect(struct inpcb *inp, struct sockaddr_in *sin, struct ucred *cred)
 	KASSERT(sin->sin_len == sizeof(*sin),
 	    ("%s: invalid address length for %p", __func__, sin));
 
-	if (sin->sin_port == 0)
+	if (sin->sin_port == 0 && !SV_CURPROC_FLAG(SV_CONN_PORT0))
 		return (EADDRNOTAVAIL);
 
 	anonport = (inp->inp_lport == 0);
diff --git a/sys/netinet6/in6_pcb.c b/sys/netinet6/in6_pcb.c
index 05c04be5dd80..754925b177cf 100644
--- a/sys/netinet6/in6_pcb.c
+++ b/sys/netinet6/in6_pcb.c
@@ -87,6 +87,7 @@
 #include <sys/priv.h>
 #include <sys/proc.h>
 #include <sys/jail.h>
+#include <sys/sysent.h>
 
 #include <vm/uma.h>
 
@@ -398,7 +399,8 @@ in6_pcbladdr(struct inpcb *inp, struct sockaddr_in6 *sin6,
 	NET_EPOCH_ASSERT();
 	INP_WLOCK_ASSERT(inp);
 
-	if (sin6->sin6_port == 0)
+	if (sin6->sin6_port == 0 &&
+	    !SV_CURPROC_FLAG(SV_CONN_PORT0))
 		return (EADDRNOTAVAIL);
 
 	if (sin6->sin6_scope_id == 0 && !V_ip6_use_defzone)
diff --git a/sys/sys/sysent.h b/sys/sys/sysent.h
index 05c1074fde70..b250ec1f7a70 100644
--- a/sys/sys/sysent.h
+++ b/sys/sys/sysent.h
@@ -175,7 +175,8 @@ struct sysentvec {
 #define	SV_SIG_DISCIGN	0x00200000	/* Do not discard ignored signals */
 #define	SV_SIG_WAITNDQ	0x00400000	/* Wait does not dequeue SIGCHLD */
 #define	SV_DSO_SIG	0x00800000	/* Signal trampoline packed in dso */
-#define	SV_PERSISTENT_CTTY 0x01000000 /* Keep ctty after the last close */
+#define	SV_PERSISTENT_CTTY 0x01000000	/* Keep ctty after the last close */
+#define	SV_CONN_PORT0	0x02000000	/* Allow connect() with port 0 */
 
 #define	SV_ABI_MASK	0xff
 #define	SV_PROC_FLAG(p, x)	((p)->p_sysent->sv_flags & (x))
@@ -190,7 +191,7 @@ struct sysentvec {
 #define	SV_ABI_FREEBSD_FLAGS	(SV_ABI_FREEBSD | SV_ASLR | SV_SHP | \
     SV_TIMEKEEP | SV_RNG_SEED_VER | SV_SIGSYS)
 #define	SV_ABI_LINUX_FLAGS	(SV_ABI_LINUX | SV_SHP | SV_SIG_DISCIGN | \
-    SV_SIG_WAITNDQ | SV_TIMEKEEP | SV_PERSISTENT_CTTY)
+    SV_SIG_WAITNDQ | SV_TIMEKEEP | SV_PERSISTENT_CTTY | SV_CONN_PORT0)
 
 /* sv_coredump flags */
 #define	SVC_PT_COREDUMP	0x00000001	/* dump requested by ptrace(2) */