phonet: do not autobind a socket to port 0
Subsystems (3)
PATCH 1/1 phonet: do not autobind a socket to port 0
pn_sock_get_port() walks the local port range to pick a free port for an autobinding socket. net.phonet.local_port_range may start at 0 because the sysctl lower bound is 0, so the very first candidate can be port 0.
For port 0 pn_find_sock_by_sa() falls back to matching by resource and usually returns NULL, so port 0 is accepted. The socket then gets pn->sobject = pn_object(0, 0) == 0 and is hashed into bucket 0. The next lookup that walks bucket 0 -- another autobind, the same socket's next sendmsg, or an incoming packet to an object whose low nibble is 0 -- hits BUG_ON(!pn->sobject) in pn_find_sock_by_sa():
kernel BUG at net/phonet/socket.c:77!
Oops: invalid opcode: 0000 [#1] PREEMPT SMP KASAN CPU: 1 PID: 6883 Comm: phonet_bug RIP: 0010:pn_find_sock_by_sa+0x3a9/0x590 [phonet] Call Trace: pn_sock_get_port+0x1da/0x4b0 [phonet] pn_socket_bind+0x312/0x580 [phonet] pn_socket_autobind+0xaf/0x160 [phonet] pn_socket_sendmsg+0x4b/0xe0 [phonet] __sock_sendmsg+0x1ab/0x200 __sys_sendto+0x1eb/0x300 __x64_sys_sendto+0xe8/0x1d0 do_syscall_64+0x102/0x2d0 entry_SYSCALL_64_after_hwframe+0x76/0x7e
It is deterministic and triggerable by an unprivileged socket. Skip port 0 when searching for a free port; 0 is reserved for the unbound state and must never be handed out.
---
Found during a review of the CVE-2026-53292 backport.
Reproduced on debian13 6.12.88 (CONFIG_PHONET):
set net.phonet.local_port_range to "0 0", then send on an unbound AF_PHONET SOCK_DGRAM socket so it autobinds -> the port-0 BUG above. With this patch the same sequence no longer crashes.
net/phonet/socket.c | 2 ++ 1 file changed, 2 insertions(+)
net/phonet/socket.c +2 -0
@@ -490,6 +490,8 @@ port_cur++;if (port_cur < pmin || port_cur > pmax) port_cur = pmin; +if (!port_cur) +continue ;pn_sockaddr_set_port (&try_sa, port_cur); tmpsk =pn_find_sock_by_sa (net, &try_sa);
Quoted 26 lines · Andrey Misiurov
pn_sock_get_port()walks the local port range to pick a free port for an autobinding socket. net.phonet.local_port_range may start at 0 because the sysctl lower bound is 0, so the very first candidate can be port 0.For port 0
pn_find_sock_by_sa()falls back to matching by resource and usually returns NULL, so port 0 is accepted. The socket then getspn->sobject=pn_object(0, 0)== 0 and is hashed into bucket 0. The next lookup that walks bucket 0 -- another autobind, the same socket's next sendmsg, or an incoming packet to an object whose low nibble is 0 -- hitsBUG_ON(!pn->sobject)inpn_find_sock_by_sa():kernel BUG at
net/phonet/socket.c:77!Oops: invalid opcode: 0000 [#1] PREEMPT SMP KASAN CPU: 1 PID: 6883 Comm: phonet_bug RIP: 0010:pn_find_sock_by_sa+0x3a9/0x590 [phonet] Call Trace: pn_sock_get_port+0x1da/0x4b0 [phonet] pn_socket_bind+0x312/0x580 [phonet] pn_socket_autobind+0xaf/0x160 [phonet] pn_socket_sendmsg+0x4b/0xe0 [phonet] __sock_sendmsg+0x1ab/0x200 __sys_sendto+0x1eb/0x300 __x64_sys_sendto+0xe8/0x1d0 do_syscall_64+0x102/0x2d0 entry_SYSCALL_64_after_hwframe+0x76/0x7e
Quoted 6 lines
It is deterministic and triggerable by an unprivileged socket. Skip port 0 when searching for a free port; 0 is reserved for the unbound state and must never be handed out.
Fixes 5b0c911bcdbd ("net: phonet: do not BUG_ON() in pn_socket_autobind() on failed bind")Signed-off-by Andrey Misiurov <[email protected]>
[email protected]
AI reviewThank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.
Critical: 0 · High: 0 · Medium: 2 · Low: 1
If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).
- [Medium] The Fixes: tag points to the wrong commit.
- [Medium] The commit message says the crash is "deterministic and triggerable by an unprivileged socket".
- [Low] After this patch the sysctl and the allocator disagree about port 0.
--
phonet: do not autobind a socket to port 0
This patch makes pn_sock_get_port() skip port 0 when it searches for a free port during autobind. Without it, a net.phonet.local_port_range that starts at 0 lets a socket be hashed with sobject == 0. The next walk of hash bucket 0 then hits BUG_ON(!pn->sobject) in pn_find_sock_by_sa().
Quoted 3 lines
It is deterministic and triggerable by an unprivileged socket. Skip port 0 when searching for a free port; 0 is reserved for the unbound state and must never be handed out.
[Severity: Medium]
Is the "unprivileged" part accurate? pn_socket_create() refuses to create any AF_PHONET socket without CAP_SYS_ADMIN in the initial user namespace:
net/phonet/af_phonet.c:pn_socket_create() {
...
if (!capable(CAP_SYS_ADMIN))
return -EPERM;
...
}Port 0 is also only a candidate after a range starting at 0 is written to net.phonet.local_port_range. That sysctl is registered only in init_net, with mode 0644:
net/phonet/sysctl.c:phonet_sysctl_init() {
phonet_table_hrd = register_net_sysctl(&init_net, "net/phonet", phonet_table);
...
}The default range is DYNAMIC_PORT_MIN..DYNAMIC_PORT_MAX (0x40..0x7f), which does not include 0.
The incoming packet path in the commit message also needs a socket already hashed with sobject 0, so it needs the same setup.
So the crash seems to need init namespace root to set the range, plus a process with CAP_SYS_ADMIN. This commit message came out of a CVE backport review. Could it state the privilege requirements more precisely?
Quoted 1 line
Fixes 5b0c911bcdbd ("net: phonet: do not BUG_ON() in pn_socket_autobind() on failed bind")
[Severity: Medium]
Is this the right commit for the Fixes tag? 5b0c911bcdbd only changed the -EINVAL handling in pn_socket_autobind():
if (err != -EINVAL || unlikely(!pn_port(pn_sk(sock->sk)->sobject))) return err;
The port 0 crash described here never takes that branch. With local_port_range set to "0 0", the first autobind succeeds:
pn_socket_autobind()
pn_socket_bind()
pn_sock_get_port() port_cur == 0, nothing found in bucket 0
pn->sobject = pn_object(0, 0) == 0
sk->sk_prot->hash() socket hashed into bucket 0returns 0
On the next autobind, this check in pn_socket_bind() still passes, because pn_port(pn->sobject) is 0:
if (sk->sk_state != TCP_CLOSE || pn_port(pn->sobject)) {
err = -EINVAL; /* attempt to rebind */
goto out;
}get_port() then walks bucket 0 again and hits BUG_ON(!pn->sobject) in pn_find_sock_by_sa(). The code before 5b0c911bcdbd takes exactly the same path.
The search loop and the BUG_ON() go back to ba113a94b750 ("Phonet: common socket glue"). The configurable pmin/pmax in pn_sock_get_port() and the local_port_range_min of {0, 0} come from 87ab4e20b445 ("Phonet: proc interface for port range"). Both are from 2008.
Should this be the following instead?
The current tag suggests only kernels with 5b0c911bcdbd are affected. That matters for stable routing.
net/phonet/socket.c int pn_sock_get_port(struct sock *sk, unsigned short sport) · line 494
Quoted 10 lines
net/phonet/socket.c +2
-0@@ -490,6 +490,8 @@ int pn_sock_get_port(struct sock *sk, unsigned short sport) port_cur++;if (port_cur < pmin || port_cur > pmax) port_cur = pmin; +if (!port_cur) +continue ;
[Severity: Low]
This isn't a bug, but the sysctl and the allocator now disagree about port 0. net/phonet/sysctl.c still allows 0 as the lower bound:
static int local_port_range_min[2] = {0, 0};
proc_local_port_range() passes that as .extra1 to proc_dointvec_minmax(). Its only other check is range[1] < range[0], so writing "0 0" still succeeds.
After this change, that range has no usable ports. The loop runs once, skips 0, and pn_sock_get_port() returns -EADDRINUSE. sendmsg, connect and listen on unbound sockets then fail through pn_socket_autobind(), with nothing pointing at the configuration. A range of "0 N" quietly behaves like "1 N".
The commit message says 0 "must never be handed out". Would it make sense to raise local_port_range_min to {1, 1}, so the write is rejected, and keep this skip as a backstop? That would make writes of "0 N" return -EINVAL, so it may be a call for the maintainers.
Quoted 3 lines
net/phonet/socket.c +0
-0pn_sockaddr_set_port (&try_sa, port_cur); tmpsk =pn_find_sock_by_sa (net, &try_sa);
Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261119122127.1708071-1-amisiurov%40cloudlinux.com