hwpmc: call the thread-create hook for Linux and kernel threads
linux_clone_thread() and kthread_add() did not call the
PMC_FN_THR_CREATE hook. Their new threads had no hwpmc thread
descriptor. If the process had a sampling PMC, the first switch-in
of such a thread hit KASSERT(pt != NULL) in pmc_process_csw_in().
An unprivileged user could panic an INVARIANTS kernel with
"pmcstat -P" on a threaded Linux program.
Call the hook in both functions, as thread_create() does.
kern_kthread.c did not include opt_hwpmc_hooks.h, so its hwpmc code
never compiled. Include it, and use newtd in kthread_add1().
Remove the exit log call from kthread_exit(), because thread_exit()
already logs the exit.
Reported by: llm-postcr
Reviewed by: kib, mhorne
Fixes: 6161b98c9935 ("hwpmc: Implement per-thread counters for PMC sampling")
Differential Revision: https://reviews.freebsd.org/D61524
vmm: release the lock when vrtc_snapshot() fails
The error path of the SNAPSHOT_*_OR_LEAVE() macros jumped past
VRTC_UNLOCK() and returned with the vrtc lock held.
Signed-off-by: Wanpeng Qian <wanpengqian at gmail.com>
Reviewed by: bnovkov, markj, jhb
Sponsored by: keelos.dev
Differential Revision: https://reviews.freebsd.org/D60232
vmm: do not let the RTC's periodic interrupt drift
The callout of the periodic interrupt was rearmed from its handler with
a timeout of one period from the time the handler ran. Each period was
longer than programmed by the latency of the callout, so the interrupt
came at a lower rate than the guest asked for. A guest that counts
these interrupts to keep its time runs slow.
Give the callout absolute deadlines one period apart, as vatpit does.
If a deadline has already passed when it is computed, for instance
after the VM was paused, start again from the current time.
Signed-off-by: Wanpeng Qian <wanpengqian at gmail.com>
Reviewed by: jhb
Sponsored by: keelos.dev
Differential Revision: https://reviews.freebsd.org/D60233
ipfw: print the log, not the config, for "nat <name> show log"
Add a test too.
Reviewed by: ae
Fixes: d6164b77f8b7 ("Make ipfw_nat module use IP_FW3 codes.")
Found with: Claude Opus 4.8
MFC after: 2 weeks
Differential Revision: https://reviews.freebsd.org/D61588
ipfw_nat: fix the buffer length check in IP_FW_NAT44_XGETLOG
nat44_get_log() compared the user buffer length against the request
header plus another ipfw_obj_header instead of the request header plus
the log buffer. A buffer that passed this check but was shorter than
the full reply left too little room in the sockopt window, so
ipfw_get_sopt_space() returned NULL and the following memcpy() of the
log wrote through a NULL pointer, panicking the kernel.
The handler was cloned from nat44_get_cfg() in the commit below, under
the convention of that time where ucfg->size held the payload only and
the ipfw_obj_header was added back in the length check. 9f925e8a920e
("Fix IP_FW_NAT44_LIST_NAT size calculation.") later switched
nat44_get_cfg() to count the header in ucfg->size and reduced its check
to "valsize < size", but nat44_get_log() was not updated. It kept the
stale idiom, whose check counts the header twice, and by coincidence of
the struct sizes that never leaves room for the log.
Compare against the full reply length and check the result of
[18 lines not shown]
bhyve: e82545: deassert the interrupt when its last cause is masked
A write to IMC only cleared the bits in the mask. If the device was
asserting INTx for a cause that the write masked, the line stayed up
until the guest cleared the cause in ICR. The hardware deasserts INTx
as soon as no unmasked cause is left, and a guest may rely on that:
Windows masks all causes and unloads its driver when it shuts down, and
then spends its time in the handlers of whatever shares the interrupt.
Do what the comment in the code asked for.
Signed-off-by: Wanpeng Qian <wanpengqian at gmail.com>
Reviewed by: jhb
Sponsored by: keelos.dev
Differential Revision: https://reviews.freebsd.org/D60230
netipsec: Clear the crypto error before dispatching a request again
When a crypto driver unregisters, a request on one of its sessions
completes with EAGAIN. The ESP, AH and IPComp callbacks then move the
SA to the new session and pass the same request to crypto_dispatch()
again, with crp_etype still set to EAGAIN.
crypto_dispatch() requires a request without an error. A kernel with
INVARIANTS panics there with "incoming crp with error". The other
consumers that dispatch a request again after EAGAIN clear the field
first.
The six callbacks prepare the request for the retry with the same
code. Move that code to a new function, ipsec_crypto_retry(), and
clear the error there. ipsec_updateid() has no other callers and
becomes static. The callbacks still dispatch the request themselves,
because the input callbacks have to release the sahtree lock first.
Reviewed by: markj, kib
[4 lines not shown]
mac: Fix a couple of bugs in prison label handling
In mac_get_prison(), mac.m_string points to a user address; the
string is copied in by mac_label_copyin_string(). If an error occurs
before that copyin, we end up freeing a user-controlled pointer.
In mac_set_prison_prepare(), the string is leaked.
Fix both problems. While here, make free_copied_label() a bit more
robust to bugs in callers.
Fixes: bd55cbb50c58 ("kern: add a mac.label jail parameter")
Reviewed by: olce, kevans
Sponsored by: The FreeBSD Foundation
Differential Revision: https://reviews.freebsd.org/D61596
tcp: improve sender side SWS avoidance
When checking for a full sized segment, take all TCP options that will
be included into account. This fixes a bug which can trigger the
"sent too much" KASSERT related to limited transmit.
Reported by: olivier
Reviewed by: rscheff, Peter Lei
MFC after: 3 days
MFC to: stable/15
MFC to: stable/14
Sponsored by: Netflix, Inc.
Differential Revision: https://reviews.freebsd.org/D61605
modstat: check errno before calling feature_present
feature_present will overwrite errno if COMPAT_FREEBSD13 is not present.
Reported by: llm-postcr
Fixes: ef726c645300 ("modstat: allow compat support to fail in tests")
(cherry picked from commit acd270c8c6542b45e404078b28e1a732106aa76c)
modstat: allow compat support to fail in tests
v1 and v2 versions of stuct module_stat are now gated by COMPAT_ ifdefs
so allow the otherwise valid query to fail with EINVAL.
Effort: CHERI upstreaming
Reviewed by: markj
Reported by: llm-postcr
Sponsored by: Innovate UK
Fixes: 1ebf46ecb627 ("modstat: don't copyout while holding a lock")
Differential Revision: https://reviews.freebsd.org/D61460
(cherry picked from commit ef726c645300870b6bdf3573079b1f89f3b45c74)
modstat: don't copyout while holding a lock
If a user arranges for a page fault to never be filled (e.g., via a
FUSE file system), this can hold the shared lock forever.
To make this all work, refactor kern_modstat and freebsd32_modstat to
malloc a temporary buffer and fill it before copying it out rather
than doing a bunch of copies.
Add COMPAT_* ifdefs around compat code. The module_stat_v1 code is
really for a.out binaries in practice and thus a bit questionable.
Reviewed by: markj
Reported by: llm-postcr
Sponsored by: Innovate UK
Fixes: 7f8c2056db1e ("modstat: avoid copying from freed memory")
Differential Revision: https://reviews.freebsd.org/D61432
(cherry picked from commit 1ebf46ecb6278e31d7ae029b0f8af3d64e7bc1f9)
modstat: avoid copying from freed memory
If a module is unloaded after the module is looked up in kern_modstat,
but before one of the copyout's occurs, then the memory being copied
might be freed and reused leading to an information disclosure.
The LLM was particularly concerned about a TOCTOU issue in the length
of the name, but it seems likely that there are issues with all the
copyout's. As such, hold the shared lock throughout.
Reviewed by: markj
Reported by: llm-postcr
MFC after: 3 days
Sponsored by: Innovate UK
Fixes: 9b3851e9e3b1 ("- Lock down the ``module'' structure...")
Differential Revision: https://reviews.freebsd.org/D61398
(cherry picked from commit 7f8c2056db1e39e44c3eda15aa4e53f7fc971cd8)
kern_module.c: add kern_ implementations
In the case of modstat(2) use a cast to access the name member of the
old struct version so the compiler see the right array size. This
addresses a sub-object bounds bug.
Effort: CHERI upstreaming
Sponsored by: DARPA, AFRL, Innovate UK
Pull Request: https://github.com/freebsd/freebsd-src/pull/2284
(cherry picked from commit b96dc2c04fb4f504ad0c752c5d4b074c5bfbf7e0)
netipsec: Release the SA and SP when mb_unmapped_to_ext() fails
ipsec4_perform_request() maps unmapped (M_EXTPG) mbufs before the
software transforms run. mb_unmapped_to_ext() frees the chain when it
fails, and the error return then kept both the SA reference taken by
ipsec4_allocsa() and the SP reference that this function must release.
The SA, its crypto session and the SP were never freed after they were
deleted.
The conversion fails with ENOMEM when an mbuf allocation fails, for
example for sendfile(2) data sent through a software IPv4 SA, and with
EINVAL for every packet of a NIC kTLS connection that matches an IPsec
policy. Take the common error path instead; it skips m_freem() when
the mbuf pointer is NULL.
IPv6 is not affected: ipsec6_perform_request() does not call
mb_unmapped_to_ext().
Reported by: llm-postcr
[3 lines not shown]
tftpd: Keep proper track of option length
When parsing options, keep track of the length of the unparsed portion
and pass that to get_field() instead of the length of the entire header.
While here, switch both make_options() and parse_options() to using
size_t for sizes and lengths.
MFC after: 1 week
Reviewed by: markj
Differential Revision: https://reviews.freebsd.org/D61048
(cherry picked from commit 35cd70490f4b7ec1921db980ae3992192e4cbfbb)
su, login: fix heap over-read in environment export filter
ok_to_export() (su.c) and export() (login.c) read s[n] before
strncmp(s, *pp, n) confirms s is at least n bytes long, where n is
the length of the blocked variable name. A PAM environment entry
shorter than that name over-reads past its heap allocation.
Check strncmp() first; it stops at s's NUL terminator and never
reads past the boundary that guarded the s[n] access.
MFC after: 1 week
Sponsored by: Advanced Micro Devices, Inc.
Signed-off-by: Osvaldo Janeri Filho <ojanerif at amd.com>
Reviewed by: des, markj
Differential Revision: https://reviews.freebsd.org/D61420
(cherry picked from commit e1b5e1f9ddac43ed669a44cfdbba1b394d144c6b)
tftpd: Fix off-by-one in get_field()
Due to a logic error, get_field() would behave as if the passed-in size
was one greater than it actually is. Fix it so it always fails if no
terminating NUL is found within the buffer, instead of succeeding if the
first byte past the end is a NUL.
MFC after: 1 week
Reviewed by: markj
Differential Revision: https://reviews.freebsd.org/D61032
(cherry picked from commit 14541ca2bf3766e659a22c5ea9ed3dc2d5f11fa5)
tftpd: Keep proper track of header length
When parsing a header, keep track of the length of the unparsed portion
and pass that to get_field() instead of the length of the entire header.
Reported by: n0ps1ed
MFC after: 1 week
Reviewed by: markj
Differential Revision: https://reviews.freebsd.org/D60902
(cherry picked from commit 579e428a551a11b43e341297dd0ef9d38c4fcda7)
tftpd: Keep proper track of header length
When parsing a header, keep track of the length of the unparsed portion
and pass that to get_field() instead of the length of the entire header.
Reported by: n0ps1ed
MFC after: 1 week
Reviewed by: markj
Differential Revision: https://reviews.freebsd.org/D60902
(cherry picked from commit 579e428a551a11b43e341297dd0ef9d38c4fcda7)
tftpd: Fix off-by-one in get_field()
Due to a logic error, get_field() would behave as if the passed-in size
was one greater than it actually is. Fix it so it always fails if no
terminating NUL is found within the buffer, instead of succeeding if the
first byte past the end is a NUL.
MFC after: 1 week
Reviewed by: markj
Differential Revision: https://reviews.freebsd.org/D61032
(cherry picked from commit 14541ca2bf3766e659a22c5ea9ed3dc2d5f11fa5)
su, login: fix heap over-read in environment export filter
ok_to_export() (su.c) and export() (login.c) read s[n] before
strncmp(s, *pp, n) confirms s is at least n bytes long, where n is
the length of the blocked variable name. A PAM environment entry
shorter than that name over-reads past its heap allocation.
Check strncmp() first; it stops at s's NUL terminator and never
reads past the boundary that guarded the s[n] access.
MFC after: 1 week
Sponsored by: Advanced Micro Devices, Inc.
Signed-off-by: Osvaldo Janeri Filho <ojanerif at amd.com>
Reviewed by: des, markj
Differential Revision: https://reviews.freebsd.org/D61420
(cherry picked from commit e1b5e1f9ddac43ed669a44cfdbba1b394d144c6b)
tftpd: Keep proper track of option length
When parsing options, keep track of the length of the unparsed portion
and pass that to get_field() instead of the length of the entire header.
While here, switch both make_options() and parse_options() to using
size_t for sizes and lengths.
MFC after: 1 week
Reviewed by: markj
Differential Revision: https://reviews.freebsd.org/D61048
(cherry picked from commit 35cd70490f4b7ec1921db980ae3992192e4cbfbb)
pax: Use copy_file_range
Use copy_file_range(2) instead of our own inefficient and unreliable
attempt at detecting holes. If copy_file_range(2) is not available,
which in theory should be never, we fall back to a simple read(2) /
write(2) loop which makes no attempt to copy holes. This should
speed up pax copy mode and cpio passthrough mode significantly.
This includes refactoring much of the existing I/O code to use the
size_t and ssize_t instead of int where appropriate.
MFC after: 1 week
Sponsored by: Klara, Inc.
Reviewed by: obiwac
Differential Revision: https://reviews.freebsd.org/D58470
pax: Add tests
Add tests exercising pax copy mode, cpio passthrough mode, the ability
to extract pax, cpio, and tar archives, and the ability to parse pax,
cpio, and tar command lines.
MFC after: 1 week
Sponsored by: Klara, Inc.
Reviewed by: obiwac
Differential Revision: https://reviews.freebsd.org/D58469
bsd.sys.mk: suppress some clang warnings for C++ for >= 19
I just tried building with system clang on one of my Linux systems which
happens to be version 19, and these warning suppressions are needed for
that version too: same errors as in the original commit.
Reviewed by: imp
MFC after: 1 week
Differential Revision: https://reviews.freebsd.org/D56874
(cherry picked from commit 004b5935a6e643033526cab623c6005b69ab11aa)
netipsec: Select outbound transmit queues by SA instead of inner flow
Outbound IPsec packets retain the flow ID of the inner traffic. Multi-queue
interfaces use this ID to select a transmit queue (iflib's QIDX()) or a member
port (lagg(4) with use_flowid). Consequently, packets belonging to the same
security association (SA) can be distributed across independently draining
queues.
ESP and AH sequence numbers are assigned before encryption, but the packets
can be transmitted out of order when different queues drain at different
rates. This can cause legitimate packets to fall outside the receiver's
anti-replay window and be dropped as possible replays. RFC 4303, Section
3.4.3, makes 32 packets the smallest window a receiver must support, and
strongSwan installs that minimum by default.
Assign each SA a flow ID derived from a Jenkins hash of its SPI, computed once
when the SA is created, from the SPI in host byte order so that the value is
the same on every architecture. Set this ID on outbound packets in
ipsec{4,6}_perform_request(), with M_HASHTYPE_OPAQUE_HASH, once the SA is
[32 lines not shown]