Re: [PATCH net] phonet: do not autobind a socket to port 0

From: Rémi Denis-Courmont

Date: Fri Nov 20 2026 - 03:18:01 EST




Le 19 novembre 2026 14:21:27 GMT+02:00, Andrey Misiurov <amisiurov@xxxxxxxxxxxxxx> a écrit :
>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.
>
>Fixes: 5b0c911bcdbd ("net: phonet: do not BUG_ON() in pn_socket_autobind() on failed bind")
>Signed-off-by: Andrey Misiurov <amisiurov@xxxxxxxxxxxxxx>

Acked-by: Rémi Denis-Courmont <courmisch@xxxxxxxxx>