netipsec: Do not zero the IPv6 UDP checksum after NAT-T decapsulation
When transport mode ESP arrives in UDP and the IKE daemon gave no
original addresses (NAT-OA) for the SA, the default
net.inet.ipsec.natt_cksum_policy has udp_ipsec_adjust_cksum() ignore the
checksum of the inner header: a TCP segment is marked as verified and
the checksum of a UDP datagram is set to zero. This is option c of RFC
3948, section 3.1.2, which was written with IPv4 in mind. UDP over IPv6
has no "no checksum" value, so udp6_input() dropped every such datagram.
With strongSwan, which installs no NAT-OA for these SAs, TCP and ICMPv6
worked over an IPv6 transport mode SA with UDP encapsulation and UDP
did not.
Mark the datagram as verified instead, as is done for TCP. Linux does
the same for both address families. A datagram that the sender gave a
zero checksum is still dropped.
Fixes: 80044c785cb0 ("Add UDP encapsulation of ESP in IPv6")
MFC after: 2 weeks
Sponsored by: Rubicon Communications, LLC ("Netgate")
powerpc: Avoid panicking on alignment errors
PowerPC ISA specifies that an alignment fault can occur when any load or
store accesses cache-inhibited or write-through memory. Since
copyin/copyout don't know what type of memory they will access we can't
always trust that memory is well behaved, or that it's properly aligned.
Avoid a panic by giving the fault handler a chance to deal with it and
propagate the fault back up.
libkvm: Add basic sanity check in _kvm_pt_init
_kvm_pt_init() assumes that the map length passed in is an even multiple
of 8 bytes, and the loop over the map decrements by 8
(sizeof(uint64_t)). Since `res` is unsigned, if the result is not a
multiple of 8 then it wraps where it would otherwise stop, thereby
writing memory until a crash. Address this by just erroring out on
invalid input, since valid cores will never have a short size.
Reviewed by: kib
Sponsored by: Hewlett Packard Enterprise Development LP
Differential Revision: https://reviews.freebsd.org/D62333
netinet: Send to the next hop a pfil hook chose in ip_tryforward()
When an inbound pfil hook chooses the next hop (ipfw fwd),
ip_tryforward() looks up the route to it and keeps the address in dest,
which if_output() gets as the next hop when the route has no gateway.
After the outbound hooks dest is set back to the destination of the
packet. ipfw always installs an outbound hook, so a packet forwarded
to a next hop on a connected network leaves on the right interface but
for the wrong neighbour: the router tries to resolve the destination
of the packet there ("arpresolve: can't allocate llinfo") and the
packet is lost. This worked until 9ac7c6cfed72, before which the
address came from the route lookup.
Set dest from the packet only when the outbound hooks changed the
destination or chose a next hop. To tell, compare with the destination
as it was before these hooks, as ip6_tryforward() does. The comparison
with the destination before the inbound hooks, which is older than
9ac7c6cfed72, also lost the next hop of a packet whose destination an
inbound hook had rewritten, and for such a packet it repeated the check
[14 lines not shown]
netipsec: Allow IPv6 extension headers before UDP encapsulated ESP
udp_ipsec_input() took the UDP header of an IPv6 packet to follow the
IPv6 header directly. With a hop-by-hop, routing or destination options
header in front of it, the SA was still found by its SPI, but the port
check then read the extension header instead of the UDP header, and the
packet was dropped as having no SA. The ESP protocol number was also
written to the next header field of the IPv6 header, not to the one that
led to the UDP header.
Derive the offset of the UDP header from the payload offset that
udp6_append() passes, and find the next header field with
ip6_get_prevhdr(), as ipsec6_input() does since 3d810fa261f1.
udp_ipsec_adjust_cksum() made the same assumption where it recomputes
the checksum of the inner TCP or UDP header
(net.inet.ipsec.natt_cksum_policy set to 1): it summed from the end of
the IPv6 header and stored the result at that offset, inside the
extension header. Use the offset of the transport header that the
[6 lines not shown]
stdio: Fix negated wchar scansets
We were recording the start of the scanset before checking if it began
with a caret. We need to check for a caret first, then record whatever
comes next as the start of the scanset.
Note that scanf() did not have this bug, only wscanf(), but this adds
regression tests for both anyway.
MFC after: 1 week
Reviewed by: emaste
Differential Revision: https://reviews.freebsd.org/D62328
netipsec: Requeue a nested IPsec header on IPv6 input
In transport mode ipsec6_common_input_cb() delivers the packet itself,
on the stack of the crypto callback that called it. With a synchronous
crypto driver that is the stack of the thread that dispatched the
request. Since 3cd5f1b307ad the callback hands a further AH, ESP or
IPComp header to ipsec_common_input() from there, so the transform, its
callback and the next delivery all ran below the first one. Every
nested header added a set of frames and only the size of the packet
limited their number. IPComp carries no authentication and
ipcomp_input() only refuses an IPComp header that is directly followed
by another, so anyone who could reach a host with an IPv6 IPComp SA
could overflow the kernel stack with one packet that hides each IPComp
header behind a destination options header.
Requeue the packet to the IPv6 netisr instead, as the IPv4 callback
does for every transport mode packet, and let ip6_input() hand the
header over from a fresh stack. ip6_input() refuses addresses in the
embedded scope form that its first pass left behind, so put them back
[19 lines not shown]
libthr: fix libc spinlock initialization
Publish thr_extra with a release store only after _thr_umutex_init()
and read it with an acquire load, so that a thread racing the first use
of a libc spinlock cannot lock the umutex before it is initialized.
PR: 301407
Fixes: bddd24cd9c3e ("Replace internal usage of struct umtx with umutex which can ...")
Reviewed by: kib
MFC after: 1 week
nfscl: Set up a custom buf_ops so that VOP_STRATEGY() call is avoided
A crash was reported, where there was a panic because
a NFS client bwrite() call resulted in a call to VOP_STRATEGY()
where the vp is unlocked.
I believe that write-backs done via the NFS client are safe to
do while holding the buffer lock, but not the vnode lock.
To avoid this panic, this patch defines a custom buf_ops
with an NFS specific bufstrategy() function that calls the
NFS strategy code directly instead of through a VOP_STRATEGY()
call.
Reviewed by: kib
MFC after: 2 weeks
Differential Revision: https://reviews.freebsd.org/D62336
nfs_nfsdstate.c: Partial revert of 64857418bb23
Dropping the cached reply that is too large causes
a correctness problem that is too difficult to fix,
so revert this and leave it the way it was.
Although there is an argument that not doing could
cause resource exhaustion, it has not been reported
in the field as a problem.
(cherry picked from commit bc7366a340edb366c37352c636b8360de92aa3a2)
nfsd: Fix handling of RPC messages too large to cache
The NFSv4.1/4.2 session negotiates a limit to the size
of an RPC reply message that can be cached for the session
slot.
This patch fixes the code so that it checks that limit and
does not cache replies larger than the limit, even if the
client requested that it be cached.
This patch is slightly different than what the author
submitted, but retains the check for reply cache size limit.
(cherry picked from commit 64857418bb2379a7d89b6af04cddf92fa48dd28c)
xprt_verbs.c: Use the default fib# for route lookups
Without this patch, fib4_lookup()/fib6_lookup() are
called with the fib# for the current thread.
During a reconnect, this could be any thread, so use
the default fib# instead.
Reported by: llm-postcr
MFC after: 1 month
Fixes: 80bee4d83637 ("nfsclrdma: Add code to support client side NFS over RDMA")
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