Commit graph

633 commits

Author SHA1 Message Date
Konstantin Belousov 53186bc143 sigqueue(2): add impl-specific flag __SIGQUEUE_TID
The flag allows the pid argument to designate a thread from the calling
process.  The flag value is carved from the high bit of the signal
number, which slightly changes the ABI of syscall.

Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D44867
2024-04-23 19:51:09 +03:00
Konstantin Belousov 2effad53b4 kern_thr.c/kern_sig.c: remove sys/cdefs.h
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D44867
2024-04-23 19:51:05 +03:00
Jake Freeland 6a4616a529 ktrace: Record signal violations with KTR_CAPFAIL
Report the delivery of signals to processes other than self while
Capsicum violation tracing with CAPFAIL_SIGNAL.

Reviewed by:	markj
Approved by:	markj (mentor)
MFC after:	1 month
Differential Revision:	https://reviews.freebsd.org/D40679
2024-04-07 18:52:51 -05:00
Jake Freeland 05296a0ff6 ktrace: Record syscall violations with KTR_CAPFAIL
Report syscalls that are not allowed in capability mode with
CAPFAIL_SYSCALL.

Reviewed by:	markj
Approved by:	markj (mentor)
MFC after:	1 month
Differential Revision:	https://reviews.freebsd.org/D40678
2024-04-07 18:52:51 -05:00
Konstantin Belousov ed410b78ed EVFILT_SIGNAL: do not use target process pointer on detach
It is enough to know knlist to remove from it, and the list is
autodestroyed on last removal.

PR:	275286
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D42777
2023-11-28 19:29:58 +02:00
Warner Losh 29363fb446 sys: Remove ancient SCCS tags.
Remove ancient SCCS tags from the tree, automated scripting, with two
minor fixup to keep things compiling. All the common forms in the tree
were removed with a perl script.

Sponsored by:		Netflix
2023-11-26 22:23:30 -07:00
Alexander Motin 0a7139485c Replace random sbuf_printf() with cheaper cat/putc. 2023-11-22 17:27:17 -05:00
Konstantin Belousov 5804a16230 nosys(): add kern.signosys tunable/sysctl to control SIGSYS
Reviewed by:	dchagin, markj
Discussed with:	jhb
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D41976
2023-10-03 01:30:53 +03:00
Konstantin Belousov b82b4ae752 sysentvec: add SV_SIGSYS flag
to allow ABIs to indicate that SIGSYS is needed.  Mark all native
FreeBSD ABIs with the flag.

This implicitly marks Linux' ABIs as not delivering SIGSYS on invalid
syscall.

Reviewed by:	dchagin, markj
Discussed with:	jhb
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D41976
2023-10-03 01:30:52 +03:00
Konstantin Belousov 39024a8914 syscalls: fix missing SIGSYS for several ENOSYS errors
In particular, when the syscall number is too large, or when syscall is
dynamic.  For that, add nosys_sysent structure to pass fake sysent to
syscall top code.

Reviewed by:	dchagin, markj
Discussed with:	jhb
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D41976
2023-10-03 01:30:52 +03:00
Warner Losh 685dc743dc sys: Remove $FreeBSD$: one-line .c pattern
Remove /^[\s*]*__FBSDID\("\$FreeBSD\$"\);?\s*\n/
2023-08-16 11:54:36 -06:00
Ed Maste 1a6238d1ea sigexit: add a break in default case
Suggested by:	markj
Fixes:		6edbe5616c ("Provide some more information for...")
Sponsored by:	The FreeBSD Foundation
2023-08-05 19:21:11 -04:00
Ed Maste 6edbe5616c Provide some more information for userland core dumps
Previously the log message indicated only "(core dumped)" if a core was
successfully created, or nothing if it was not. This provides
insufficient information to faciliate debugging. Dtrace is no help as
coredump() is static and we cannot find the return value via fbt.
Expand the log message to include error return value information.

Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D39942
2023-08-05 19:18:35 -04:00
Konstantin Belousov dfe172484d sigtd(): prefer non-stopped thread as a target for signal queue
This should improve signal delivery latency and better expose the
process state to the executing threads.

Reviewed by:	markj
Tested by:	pho
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D41128
2023-07-26 18:12:55 +03:00
Konstantin Belousov aaa924138a Revert "killpg(): close a race with fork(), part 2"
This reverts commits 81a37995c7 and
565a343ae3.

There is still a leakage of the p_killpg_cnt, some but not all sources
of which were identified.

Second, and more important, is that there is a fundamental issue with
blocked signals having KSI_KILLPG flag set.  Queueing of such signal
increments p_killpg_cnt, but it cannot be decremented until the signal
is delivered.  If, for instance, a single-threaded process with blocked
signal receives killpg-kill and executes fork(2), the fork enter check
returns with ERESTART.  And since signal is blocked, the condition
cannot be cleared.

Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D41128
2023-07-26 18:12:55 +03:00
Brooks Davis 437e1e37df kern_sig.c: include sys/jail.h per style(9)
Fixes:		e722820434
Sponsored by:	DARPA
2023-07-25 18:13:17 +01:00
Dmitry Chagin 17cb2ac3df signal: Get rid of gsignal() as it not used anywhere
Reviewed by:		imp, kib
Differential Revision:	https://reviews.freebsd.org/D41007
MFC after:		1 week
2023-07-13 10:41:51 +03:00
Konstantin Belousov 565a343ae3 sigqueue_delete_set_proc(): initialize sq_proc for worklist
This should fix leaks for the p_killpg_cnt counter, because
sigqueue_flush() drops ksi's.

Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
2023-07-11 03:52:16 +03:00
Konstantin Belousov 81a37995c7 killpg(): close a race with fork(), part 2
When we are sending terminating signal to the group, killpg() needs to
guarantee that all group members are to be terminated (it does not need
to ensure that they are terminated on return from killpg()).  The
pg_killsx change eliminates the largest window there, but still, if a
multithreaded process is signalled, the following could happen:
- thread 1 is selected for the signal delivery and gets descheduled
- thread 2 waits for pg_killsx lock, obtains it and forks
- thread 1 continue executing and terminates the process
This scenario allows the child to escape still.

To fix it, count the number of signals sent to the process with
killpg(2), in p_killpg_cnt variable, which is incremented in killpg()
and decremented after signal handler frame is created or in exit1()
after single-threading.  This way we avoid forking if the termination is
due.

Noted and reviewed by:	markj (previous version)
Tested by:	pho
Sponsored by:	The FreeBSD Foundation
MFC after:	2 weeks
Differential revision:	https://reviews.freebsd.org/D40493
2023-07-04 06:43:16 +03:00
Konstantin Belousov 3360b48525 killpg(2): close a race with fork(2), part1
If the process group member performs fork(), the child could escape
signalling from killpg(). Prevent it by introducing an sx process group
lock pg_killsx which is taken interruptibly shared around fork. If there
is a pending signal, do the trip through userspace with ERESTART to
handle signal ASTs. The lock is taken exclusively during killpg().

The lock is also locked exclusive when the process changes group
membership, to avoid escaping a signal by this means, by ensuring that
the process group is stable during fork.

Note that the new lock is before proctree lock, so in some situations we
could only do trylocking to obtain it.

This relatively simple approach cannot work for REAP_KILL, because
process potentially belongs to more than one reaper tree by having
sub-reapers.

Reported by:	dchagin
Tested by:	dchagin, pho
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	2 weeks
Differential revision:	https://reviews.freebsd.org/D40493
2023-07-04 06:21:53 +03:00
Konstantin Belousov 4b59d1724b killpg1(): update the herald comment
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D40493
2023-07-04 06:21:53 +03:00
Mark Johnston 0d3f1b4f25 signal: Make the signal disposition table const
No functional change intended.

MFC after:	1 week
2023-06-01 17:18:23 -04:00
Konstantin Belousov 974be51b3f Fixes for ptrace_syscallreq()
Re-assign the sc local (syscall number) before moving args for SYS_syscall.
Correct the audit and kdtrace hooks invocations.

Fixes:	140ceb5d95
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
2022-12-23 01:53:41 +02:00
Konstantin Belousov 140ceb5d95 ptrace(2): add PT_SC_REMOTE remote syscall request
Reviewed by:	markj
Discussed with:	jhb
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D37590
2022-12-22 23:11:35 +02:00
Konstantin Belousov f0592b3c8d Add a thread debugging flag TDB_BOUNDARY
It indicates to a debugger that the thread is stopped at the
kernel->user exit path.

Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D37590
2022-12-22 23:11:35 +02:00
Konstantin Belousov e6feeae2f9 sys: rename td_coredump to td_remotereq
and TDB_COREDUMPRQ to TDB_COREDUMPREQ

Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D37590
2022-12-22 23:11:35 +02:00
Mateusz Guzik 69413598d2 signal: use proc_iterate to save on work
Most notably poudriere performs kill -9 -1 in jails for each port
being built. This reduces the scan from hundrends of processes to
literally 1.

Reviewed by:	jamie, markj
Differential Revision:	https://reviews.freebsd.org/D34522
2022-09-05 11:54:47 +00:00
Konstantin Belousov 0a4f2ac3b7 kern_sig.c: style
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	2 weeks
Differential revision:	https://reviews.freebsd.org/D36207
2022-08-20 20:33:18 +03:00
Konstantin Belousov cdb58f9d04 ksiginfo_tryfree(): change return type to bool
The function result is already used as bool.

Reviewed by:	markj
Tested by:	pho
Sponsored by:	The FreeBSD Foundation
MFC after:	2 weeks
Differential revision:	https://reviews.freebsd.org/D36207
2022-08-20 20:33:18 +03:00
Konstantin Belousov cc29f221aa ksiginfo_alloc(): change to directly take M_WAITOK/NOWAIT flags
Also style, and remove unneeded cast.

Reviewed by:	markj
Tested by:	pho
Sponsored by:	The FreeBSD Foundation
MFC after:	2 weeks
Differential revision:	https://reviews.freebsd.org/D36207
2022-08-20 20:33:17 +03:00
Konstantin Belousov c53fec7603 sig_suspend_threads(): remove 'sending' arg
The TDA_AST flag is set on td2 unconditionally (as it was TDF_ASTPENDING
before AST rework), so it is not used practically for some time.

Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D36033
2022-08-03 16:56:23 +03:00
Konstantin Belousov f2fd7d8bfc ast_sig(): add missed TDAI()
Mask checked was completely wrong

Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D36033
2022-08-03 16:56:23 +03:00
Konstantin Belousov 4fced8642f sigfastblock_setpend() and fastblock_mask can be static now
Reviewed by:	markj
Tested by:	pho
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D35888
2022-08-02 21:11:10 +03:00
Konstantin Belousov c6d31b8306 AST: rework
Make most AST handlers dynamically registered.  This allows to have
subsystem-specific handler source located in the subsystem files,
instead of making subr_trap.c aware of it.  For instance, signal
delivery code on return to userspace is now moved to kern_sig.c.

Also, it allows to have some handlers designated as the cleanup (kclear)
type, which are called both at AST and on thread/process exit.  For
instance, ast(), exit1(), and NFS server no longer need to be aware
about UFS softdep processing.

The dynamic registration also allows third-party modules to register AST
handlers if needed.  There is one caveat with loadable modules: the
code does not make any effort to ensure that the module is not unloaded
before all threads processed through AST handler in it.  In fact, this
is already present behavior for hwpmc.ko and ufs.ko.  I do not think it
is worth the efforts and the runtime overhead to try to fix it.

Reviewed by:	markj
Tested by:	emaste (arm64), pho
Discussed with:	jhb
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D35888
2022-08-02 21:11:09 +03:00
Konstantin Belousov 4493a13e3b Do not single-thread itself when the process single-threaded some another process
Since both self single-threading and remote single-threading rely on
suspending the thread doing thread_single(), it cannot be mixed: thread
doing thread_suspend_switch() might be subject to thread_suspend_one()
and vice versa.

In collaboration with:	pho
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	2 weeks
Differential revision:	https://reviews.freebsd.org/D35310
2022-06-13 22:30:03 +03:00
Konstantin Belousov 02a2aacbe2 issignal(): ignore signals when process is single-threading for exit
Places that will wait for curproc->p_singlethr to become zero (in the
next commit, the counter of number of external single-threading is
to be introduced), must wait for it interruptible, otherwise we
deadlock.  On the other hand, a signal delivered during this window,
if directed to the waiting thread, would cause the wait loop to become
a busy loop.

Since we are exiting, it is safe to ignore the signals.

Reviewed by:	markj
Tested by:	pho
Sponsored by:	The FreeBSD Foundation
MFC after:	2 weeks
Differential revision:	https://reviews.freebsd.org/D35310
2022-06-13 22:30:02 +03:00
Konstantin Belousov d3000939c7 P2_WEXIT: avoid thread_single() for exiting process earlier
before the process itself does thread_single(SINGLE_EXIT).  We cannot
single-thread such process in ALLPROC (external) mode, and properly
detect and report the failure to do so due to the process becoming
zombie is easier to prevent than handle.

In collaboration with:	pho
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	2 weeks
Differential revision:	https://reviews.freebsd.org/D35310
2022-06-13 22:30:02 +03:00
Dmitry Chagin 4a700f3c32 sigtimedwait: Prevent timeout math overflows.
Our kern_sigtimedwait() calculates absolute sleep timo value as 'uptime+timeout'.
So, when the user specifies a big timeout value (LONG_MAX), the calculated
timo can be less the the current uptime value.
In that case kern_sigtimedwait() returns EAGAIN instead of EINTR, if
unblocked signal was caught.

While here switch to a high-precision sleep method.

Reviewed by:		mav, kib
In collaboration with:	mav
Differential revision:	https://reviews.freebsd.org/D34981
MFC after:		2 weeks
2022-04-25 10:23:15 +03:00
Mateusz Guzik c5c981d443 signals: plug a set-but-not-used var
Sponsored by:	Rubicon Communications, LLC ("Netgate")
2022-04-19 12:45:57 +00:00
Eric van Gyzen 863070bbf6 ksiginfo_alloc: pass M_WAITOK or M_NOWAIT to uma_zalloc
It expects exactly one of those flags.  A future commit will assert this.

Reviewed by:	rstone
MFC after:	1 month
Sponsored by:	Dell EMC Isilon
Differential Revision:	https://reviews.freebsd.org/D34451
2022-03-25 20:10:37 -05:00
Mateusz Guzik bb92cd7bcd vfs: NDFREE(&nd, NDF_ONLY_PNBUF) -> NDFREE_PNBUF(&nd) 2022-03-24 10:20:51 +00:00
Konstantin Belousov a24afbb4e6 Ignore debugger-injected signals left after detaching
PR:	261010
Reported by:	Martin Simmons <martin@lispworks.com>
Reviewed by:	markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D33787
2022-01-12 07:33:30 +02:00
John Baldwin 753c851387 sigev_findtd: Fix whitespace nit in argument list.
Obtained from:	CheriBSD
2022-01-04 13:37:39 -08:00
Alexander Motin fe27f1db5f kern: Remove CTLFLAG_NEEDGIANT from some sysctls.
MFC after:	2 weeks
2021-12-26 12:03:33 -05:00
Mateusz Guzik 7e1d3eefd4 vfs: remove the unused thread argument from NDINIT*
See b4a58fbf64 ("vfs: remove cn_thread")

Bump __FreeBSD_version to 1400043.
2021-11-25 22:50:42 +00:00
Konstantin Belousov 3d2778515a sig_ast_checksusp(): mark the local p as __diagused
It is only used to assert that the (current) process is locked

Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
2021-10-21 21:40:46 +03:00
Mark Johnston 81f2e9063d signal: Add SIG_FOREACH and refactor issignal()
Add a SIG_FOREACH macro that can be used to iterate over a signal set.
This is a bit cleaner and more efficient than calling sig_ffs() in a
loop.  The implementation is based on BIT_FOREACH_ISSET(), except
that the bitset limbs are always 32 bits wide, and signal sets are
1-indexed rather than 0-indexed like bitset(9) sets.

issignal() cannot really be modified to use SIG_FOREACH() directly.
Take this opportunity to split the function into two explicit loops.
I've always found this function hard to read and think that this change
is an improvement.

Remove sig_ffs(), nothing uses it now.

Reviewed by:	kib
MFC after:	2 weeks
Sponsored by:	The FreeBSD Foundation
Differential Revision:	https://reviews.freebsd.org/D32473
2021-10-18 09:56:58 -04:00
Konstantin Belousov 1adebca1fc Style
Sponsored by:	The FreeBSD Foundation
MFC after:	3 days
2021-10-14 23:07:32 +03:00
Konstantin Belousov 244ab56611 Add curproc_sigkilled()
Function returns an indicator that the process was killed with SIGKILL

Reviewed by:	imp, markj
Tested by:	pho
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D32313
2021-10-08 03:21:43 +03:00
Konstantin Belousov dc2d0899bb kern_sig.c: Remove unused SIGPROP_CANTMASK
Reviewed by:	imp, markj
Sponsored by:	The FreeBSD Foundation
MFC after:	1 week
Differential revision:	https://reviews.freebsd.org/D32313
2021-10-08 03:21:42 +03:00