netinet6: Keep the AH flags on the inner packet of a tunnel mode SA
ip6_input() clears M_AUTHIPHDR and M_AUTHIPDGM on every packet. The
inner packet of a tunnel mode AH SA reaches it through netisr after
ipsec[46]_common_input_cb() has stripped the outer header, so it loses
the flags that ah_input_cb() set after verifying the ICV. The inbound
policy check in ipsec_in_reject() then finds no AH flag on a packet
whose policy requires AH and drops it as a policy violation ("inbound
packets violated process security policy" in netstat -sp ipsec6). With
the usual "require" level, every IPv6 packet carried by a policy-based
AH tunnel is dropped, whatever the outer address family. ESP is not
affected, since M_DECRYPTED is not cleared, and neither are IPv4 inner
packets, since ip_input() has never cleared the flags. if_ipsec(4) is
not involved, since it only uses ESP.
net.inet.ipsec.check_policy_history does not help: ipsec_in_reject()
checks the flags in addition to the history tags.
The clearing comes from KAME, whose ah6_input() deliberately did not
[24 lines not shown]
powerpc: keep the vector registers in a core after setcontext
After setcontext() or sigreturn the AltiVec and VSX registers stay in
the PCB until the thread touches them again, and only PCB_VECREGS and
PCB_FPREGS say they're valid. The NT_PPC_VMX and NT_PPC_VSX core notes
are only written when PCB_VEC and PCB_VSX are set, so a core dumped in
that window has no vector registers for the thread. Check the validity
flags instead, like get_mcontext() does.
PCB_FPREGS is also set on CPUs without VSX, so only write the VSX note
when the CPU has it.
Reviewed by: jhibbits
MFC after: 1 week
Differential Revision: https://reviews.freebsd.org/D62315
netipsec: Find the next header field of an IPv6 IPsec header
The transforms write the protocol of the payload over the next header
field that led to the AH, ESP or IPComp header they remove.
ipsec6_input() always gave them the field of the IPv6 header. When
extension headers precede the IPsec header, the field to update is in
the last of them. The packet was still delivered, because the callback
reads the protocol back from the same place, but the chain left in the
mbuf was wrong: the IPv6 header named the payload while the extension
headers were still in front of it. Whatever walks the chain again
misread the packet; a socket with IPV6_RECVHOPOPTS set, for one, no
longer got the hop-by-hop options of a packet that ESP protected.
Look the field up with ip6_get_prevhdr(), as ipsec6_common_input() did
before the projects/ipsec merge.
Reported by: llm-postcr
Reviewed by: kib
Fixes: fcf596178b5f ("Merge projects/ipsec into head/.")
[3 lines not shown]
netinet6: limit size of packet options set via setsockopt(2) to MCLBYTES
While soopt_getm() can indeed allocate a chain of mbufs, the control is
then passed to ip6_pcbopts(), that passes to ip6_setpktopts(). And the
latter refuses to work on a chain and returns EINVAL. This all was true
since KAME import. Thus, the limit set by aec9c8d5a5ee indeed protects
from a local DoS vector, but it doesn't make a practical sense. Anything
between MCLBYTES and IPV6_PKTOPTIONS_MBUF_LIMIT would be refused with
EINVAL and freed.
Reviewed by: pouria
Fixes: aec9c8d5a5ee52dd6d19892138ef78fc78ef2210
PR: 100219
Differential Revision: https://reviews.freebsd.org/D62252
nd6: Skip periodic DAD while SLAAC can regenerate a stable address
A failed periodic DAD retry probes the same address. Counting it as
another RFC 7217 failure lets a persistent duplicate exhaust the
interface's regeneration budget and prevent SLAAC on other prefixes.
Count only the initial DAD failure for each address. When SLAAC can
generate another stable address, disable periodic DAD for the failed
candidate. The final candidate remains eligible for periodic recovery
if the conflict clears.
Reported by: llm-postcr
Fixes: 4d5833adfd19 ("nd6: Periodically retry DAD for duplicated IPv6 addresses")
powerpc: fix single stepping across signal delivery and fork
PT_STEP sets MSR[SE] and trap() clears it again when the trace trap
comes in, but the rest of the kernel treated it as a bit a thread can't
change, which broke stepping in three places.
sendsig() kept it set for the signal handler, so the debugger got a
SIGTRAP on the handler's first instruction. This is what
ptrace__PT_STEP_with_signal in ptrace_test trips over. Clear it for the
handler, like amd64 does with PSL_T.
The context saved for the handler still has it, but set_mcontext()
refused a context whose MSR[SE] no longer matched the frame, so
sigreturn(2) failed and the process exited as soon as the handler
returned. Let a context carry MSR[SE].
cpu_fork() copied it into the child, which nobody traces, so a fork(2)
done while stepping killed the child with SIGTRAP. Clear it there.
[3 lines not shown]
libc: memstream tests: also assert in the wmemstream test
This should have been done in the previous commit, but whoops. While
we're here- there can be garbage after the buffer, so only print the
valid portion.
Fixes: dc34894d410 ("libc: memstream tests: convert [...]")
Sponsored by: Klara, Inc.
pkg: Fix two issues with -r
* Since we pass the -r option and its argument unmodified to the real
pkg, we shouldn't chdir before executing it. It is unnecessary if the
argument is absolute, and harmful if it is relative as a second chdir
will fail.
* Unlike -j, we can't assume that the target environment for -r is
executable; we might be cross-building an image, for instance.
Therefore, we should execute the pkg binary from our own environment,
not the one we just bootstrapped.
Fixes: cad99ff75342 ("pkg: Add -j and -r options")
Sponsored by: Klara, Inc.
Sponsored by: The FreeBSD Foundation
Reviewed by: bapt
Differential Revision: https://reviews.freebsd.org/D62268
ptrace: Fix interactions between remote requests and signal handling
The debugger may have invoked execve in the debugee via PT_SC_REMOTE, in
which case the sigacts structure may have been replaced and freed.
Then, sigprocess() can operate on the freed structure. This can be
exploited to obtain privileges. Further, we could have nested
ptracestop() calls if the debugger requests a blocking syscall while a
signal is pending. It is very hard to reason about the correctness of
such calls.
Make sigprocess() reload the process signal handler state after handing
control to the debugger, this might change due to execve().
Set TDB_BOUNDARY in ptracestop() rather than in the AST handler and
syscallret(). Plumb a flag through to achieve this. This also ensures
that nested ptracestop() calls do not try to handle remote requests.
Add a TDB_INREMOTEREQ flag, use it to ensure that we don't pause or
consume signals while executing a remote request. Factor out the
[11 lines not shown]
pam_xdg: Do not follow symbolic links when removing the runtime directory
When the last session of a user ends, the runtime directory is removed by
root. The user owns the directory and can still have processes (always
without the linger option), so its content can change while it is removed.
remove_dir() took the type of each entry from readdir() and opened the
directories with openat(fd, name, 0). An entry replaced by a symbolic link
between the two was followed and the content of its target was removed
as root.
Open every entry relative to its parent with O_DIRECTORY | O_NOFOLLOW and
unlink what is not a directory at that moment, never what it points to.
Also stay on the filesystem of the runtime directory, do not enter a
directory that belongs to neither the user nor root, and limit the depth
of the recursion. remove_dir() now always consumes the descriptor it is
given, the callers closed it a second time.
Looking for the last session file also opened a user-controlled entry
[16 lines not shown]
linux: Release futexes and clear tids earlier during execve
During execve, other threads in the process are terminated. As a part
of this, they invoke the sv_thread_detach handler, which modifies user
memory. This handler is invoked only after the new process image has
been loaded, in which case they can modify memory in a setuid or setgid
image process.
Fix this by implementing sv_onexec_old, which explicitly runs before the
new image is loaded. This is how native executables release robust
mutexes.
Approved by: so
Security: FreeBSD-SA-26:99.linux
Security: CVE-2026-127757
Reported by: mmacy, kbowling
Reviewed by: kib
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D62207
kern_proc_setrlimit: take the fast path only for curproc
When a thread changes its own process' resource limits,
kern_proc_setrlimit() syncs the thread to the new plimit and releases
two references on the old one, the process' and the thread's, rather
than letting the thread sync itself on its next kernel entry. It
takes this path whenever the target process' copy-on-write generation
is one ahead of the current thread's, but never checks that the
target is the thread's own process.
Another process' limits can be set through the kern.proc.rlimit
sysctl (used by limits -P), Linux prlimit64(2) and the Linux fork
path. On such a remote update the generations can still line up;
lim_cowsync() then syncs and releases the calling thread's reference,
yet both references are dropped from the target's old plimit. A
thread of the target still points at that plimit through td_limit, so
it is freed while live -- a use-after-free.
Guard the fast path with the target being the calling thread's own
[12 lines not shown]
proc: Make the COW generation counter a saturating counter
A sleeping thread can hold on to a COW counter value indefinitely while
a sibling thread forces the process generation counter to wrap around.
This can cause us to free a limit set while it's still in use. This can
be exploited to obtain root privileges.
Fix this by making p_cowgen a saturating counter: once it reaches
UINT_MAX, the value is fixed, and we always take the slow path through
thread_cow_update(). The per-process counter is reset at fork time, and
I suspect it's quite unlikely that a process will hit the limit
organically.
Simplify the optimization in setrlimit which proactively syncs the
thread's limit structure: now that the counter is saturating, we cannot
infer from PROC_COW_CHANGECOUNT(td, p) == 1 that the thread and proc are
in sync.
Approved by: so
[6 lines not shown]
socket: Disallow listen on SS_ISDISCONNECTED sockets
tcp_usr_shutdown() can race with listen(): it calls tcp_output after
marking the socket disconnected. This can lead to corruption of the
socket union.
Simply disallow listen() on disconnected data sockets. This is not a
portable feature and it seems unlikely that anything relies on it.
Remove a recently added regression test which documents the old
behaviour.
Approved by: so
Security: FreeBSD-SA-26:102.socket
Security: CVE-2026-127762
Reported by: Laam Alif at Ibteda Digital Library
Reviewed by: emaste
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D62220
proc: assert td belongs to p in PROC_COW_CHANGECOUNT
PROC_COW_CHANGECOUNT(td, p) evaluates p->p_cowgen - td->td_cowgen, which
is only meaningful when td is a thread of p. Otherwise it subtracts two
unrelated copy-on-write generations, as the kern.proc.rlimit remote
update path did before it was fixed. Assert td->td_proc == p to catch
such a caller on INVARIANTS kernels.
Approved by: so
Security: FreeBSD-SA-26:101.setrlimit
Suggested by: mjg
MFC after: 2 weeks
Assisted-by: Claude Opus 4.8
Reviewed by: olce, mjg
Differential Revision: https://reviews.freebsd.org/D62162
socket: Disallow listen on SS_ISDISCONNECTED sockets
tcp_usr_shutdown() can race with listen(): it calls tcp_output after
marking the socket disconnected. This can lead to corruption of the
socket union.
Simply disallow listen() on disconnected data sockets. This is not a
portable feature and it seems unlikely that anything relies on it.
Remove a recently added regression test which documents the old
behaviour.
Approved by: so
Security: FreeBSD-SA-26:102.socket
Security: CVE-2026-127762
Reported by: Laam Alif at Ibteda Digital Library
Reviewed by: emaste
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D62220
proc: assert td belongs to p in PROC_COW_CHANGECOUNT
PROC_COW_CHANGECOUNT(td, p) evaluates p->p_cowgen - td->td_cowgen, which
is only meaningful when td is a thread of p. Otherwise it subtracts two
unrelated copy-on-write generations, as the kern.proc.rlimit remote
update path did before it was fixed. Assert td->td_proc == p to catch
such a caller on INVARIANTS kernels.
Approved by: so
Security: FreeBSD-SA-26:101.setrlimit
Suggested by: mjg
MFC after: 2 weeks
Assisted-by: Claude Opus 4.8
Reviewed by: olce, mjg
Differential Revision: https://reviews.freebsd.org/D62162
ptrace: Fix interactions between remote requests and signal handling
The debugger may have invoked execve in the debugee via PT_SC_REMOTE, in
which case the sigacts structure may have been replaced and freed.
Then, sigprocess() can operate on the freed structure. This can be
exploited to obtain privileges. Further, we could have nested
ptracestop() calls if the debugger requests a blocking syscall while a
signal is pending. It is very hard to reason about the correctness of
such calls.
Make sigprocess() reload the process signal handler state after handing
control to the debugger, this might change due to execve().
Set TDB_BOUNDARY in ptracestop() rather than in the AST handler and
syscallret(). Plumb a flag through to achieve this. This also ensures
that nested ptracestop() calls do not try to handle remote requests.
Add a TDB_INREMOTEREQ flag, use it to ensure that we don't pause or
consume signals while executing a remote request. Factor out the
[11 lines not shown]
proc: Make the COW generation counter a saturating counter
A sleeping thread can hold on to a COW counter value indefinitely while
a sibling thread forces the process generation counter to wrap around.
This can cause us to free a limit set while it's still in use. This can
be exploited to obtain root privileges.
Fix this by making p_cowgen a saturating counter: once it reaches
UINT_MAX, the value is fixed, and we always take the slow path through
thread_cow_update(). The per-process counter is reset at fork time, and
I suspect it's quite unlikely that a process will hit the limit
organically.
Simplify the optimization in setrlimit which proactively syncs the
thread's limit structure: now that the counter is saturating, we cannot
infer from PROC_COW_CHANGECOUNT(td, p) == 1 that the thread and proc are
in sync.
Approved by: so
[6 lines not shown]
kern_proc_setrlimit: take the fast path only for curproc
When a thread changes its own process' resource limits,
kern_proc_setrlimit() syncs the thread to the new plimit and releases
two references on the old one, the process' and the thread's, rather
than letting the thread sync itself on its next kernel entry. It
takes this path whenever the target process' copy-on-write generation
is one ahead of the current thread's, but never checks that the
target is the thread's own process.
Another process' limits can be set through the kern.proc.rlimit
sysctl (used by limits -P), Linux prlimit64(2) and the Linux fork
path. On such a remote update the generations can still line up;
lim_cowsync() then syncs and releases the calling thread's reference,
yet both references are dropped from the target's old plimit. A
thread of the target still points at that plimit through td_limit, so
it is freed while live -- a use-after-free.
Guard the fast path with the target being the calling thread's own
[12 lines not shown]
unix: fix use-after-free in SCM_RIGHTS GC after MSG_PEEK
uipc_soreceive_dgram() removes a datagram from the receive queue with
STAILQ_REMOVE_HEAD() before pinning it in so_rcv.uxdg_peeked for a
MSG_PEEK receive. STAILQ_REMOVE_HEAD() does not clear the removed
element's link field, and m_stailqpkt is a union with m_nextpkt, so the
pinned datagram keeps pointing at the datagram that used to follow it in
the queue. That datagram is no longer referenced from here and can be
freed independently, for example by unp_disconnect() when the sending
peer closes.
unp_scan_socket() passes so_rcv.uxdg_peeked to unp_scan(), which walks a
record list through m_nextpkt while collecting SCM_RIGHTS garbage. It
therefore followed the stale link into the freed datagram. On an
INVARIANTS or KASAN kernel this is a panic in unp_remove_dead_ref(); on
a release kernel it double-counts the rights in that record and lets the
collector reclaim a socket that is still reachable.
Clear m_nextpkt when the datagram is pinned so the scan stops at the
[17 lines not shown]
arm64: Apply boundary checking to ranges in the ARM64_GUARD_PAGE handler
Otherwise pmap_bti_set() may end up partially operating on a largepage
mapping. In the case of a L2 block mapping, it'll demote the mapping.
Then, a subsequent fault will cause pmap_enter_largepage() to remap the
whole large page, but without a break-before-make sequence, so we could
end up with 4KB TLB entries pointing into the large page. If the
largepage object is subsequently destroyed, those TLB entries won't be
invalidated, and would allow userspace to access freed memory.
Fix this using the vm_map_check_boundary() helper. Don't bother
checking for non-canonical addresses, as pmap_bti_set() handles that.
Approved by: so
Security: FreeBSD-SA-26:95.arm64
Security: CVE-2026-127755
Reviewed by: alc, kib
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D61956
pam_xdg: Do not follow symbolic links when removing the runtime directory
When the last session of a user ends, the runtime directory is removed by
root. The user owns the directory and can still have processes (always
without the linger option), so its content can change while it is removed.
remove_dir() took the type of each entry from readdir() and opened the
directories with openat(fd, name, 0). An entry replaced by a symbolic link
between the two was followed and the content of its target was removed
as root.
Open every entry relative to its parent with O_DIRECTORY | O_NOFOLLOW and
unlink what is not a directory at that moment, never what it points to.
Also stay on the filesystem of the runtime directory, do not enter a
directory that belongs to neither the user nor root, and limit the depth
of the recursion. remove_dir() now always consumes the descriptor it is
given, the callers closed it a second time.
Looking for the last session file also opened a user-controlled entry
[16 lines not shown]
linux: Release futexes and clear tids earlier during execve
During execve, other threads in the process are terminated. As a part
of this, they invoke the sv_thread_detach handler, which modifies user
memory. This handler is invoked only after the new process image has
been loaded, in which case they can modify memory in a setuid or setgid
image process.
Fix this by implementing sv_onexec_old, which explicitly runs before the
new image is loaded. This is how native executables release robust
mutexes.
Approved by: so
Security: FreeBSD-SA-26:99.linux
Security: CVE-2026-127757
Reported by: mmacy, kbowling
Reviewed by: kib
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D62207
socket: Disallow listen on SS_ISDISCONNECTED sockets
tcp_usr_shutdown() can race with listen(): it calls tcp_output after
marking the socket disconnected. This can lead to corruption of the
socket union.
Simply disallow listen() on disconnected data sockets. This is not a
portable feature and it seems unlikely that anything relies on it.
Remove a recently added regression test which documents the old
behaviour.
Approved by: so
Security: FreeBSD-SA-26:102.socket
Security: CVE-2026-127762
Reported by: Laam Alif at Ibteda Digital Library
Reviewed by: emaste, glebius
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D62220
ptrace: Fix interactions between remote requests and signal handling
The debugger may have invoked execve in the debugee via PT_SC_REMOTE, in
which case the sigacts structure may have been replaced and freed.
Then, sigprocess() can operate on the freed structure. This can be
exploited to obtain privileges. Further, we could have nested
ptracestop() calls if the debugger requests a blocking syscall while a
signal is pending. It is very hard to reason about the correctness of
such calls.
Make sigprocess() reload the process signal handler state after handing
control to the debugger, this might change due to execve().
Set TDB_BOUNDARY in ptracestop() rather than in the AST handler and
syscallret(). Plumb a flag through to achieve this. This also ensures
that nested ptracestop() calls do not try to handle remote requests.
Add a TDB_INREMOTEREQ flag, use it to ensure that we don't pause or
consume signals while executing a remote request. Factor out the
[11 lines not shown]
unix: fix use-after-free in SCM_RIGHTS GC after MSG_PEEK
uipc_soreceive_dgram() removes a datagram from the receive queue with
STAILQ_REMOVE_HEAD() before pinning it in so_rcv.uxdg_peeked for a
MSG_PEEK receive. STAILQ_REMOVE_HEAD() does not clear the removed
element's link field, and m_stailqpkt is a union with m_nextpkt, so the
pinned datagram keeps pointing at the datagram that used to follow it in
the queue. That datagram is no longer referenced from here and can be
freed independently, for example by unp_disconnect() when the sending
peer closes.
unp_scan_socket() passes so_rcv.uxdg_peeked to unp_scan(), which walks a
record list through m_nextpkt while collecting SCM_RIGHTS garbage. It
therefore followed the stale link into the freed datagram. On an
INVARIANTS or KASAN kernel this is a panic in unp_remove_dead_ref(); on
a release kernel it double-counts the rights in that record and lets the
collector reclaim a socket that is still reachable.
Clear m_nextpkt when the datagram is pinned so the scan stops at the
[16 lines not shown]
linux: Release futexes and clear tids earlier during execve
During execve, other threads in the process are terminated. As a part
of this, they invoke the sv_thread_detach handler, which modifies user
memory. This handler is invoked only after the new process image has
been loaded, in which case they can modify memory in a setuid or setgid
image process.
Fix this by implementing sv_onexec_old, which explicitly runs before the
new image is loaded. This is how native executables release robust
mutexes.
Approved by: so
Security: FreeBSD-SA-26:99.linux
Security: CVE-2026-127757
Reported by: mmacy, kbowling
Reviewed by: kib
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D62207