asterisk.c: Add opt-in posix_spawn() for ast_safe_system().

asterisk.c: Add opt-in posix_spawn() for ast_safe_system().

fork() copies the page tables of the whole Asterisk process while
holding its mm lock, so its cost grows with the process. Time spent in
the clone() system call made by fork() for one System(), measured with
strace -T on production systems: 8.8-9.9 ms with 293 MB RSS / 99
threads, 44-52 ms with 1.9 GB / 325 threads (both EL9), 57-61 ms with
2.3 GB / 333 threads (CentOS 7). During that time other threads that
fault or allocate are delayed, including timing threads, so System(),
TrySystem(), voicemail mailcmd, externnotify etc. can cause late timer
wakeups on large systems.

With the new asterisk.conf option safe_system_spawn enabled and glibc
2.34 or later, ast_safe_system() starts "/bin/sh -c" with
posix_spawn(), which uses clone(CLONE_VM|CLONE_VFORK) and does not copy
page tables. The child is set up as on the fork() path:
- fds above stderr closed (posix_spawn_file_actions_addclosefrom_np);
- with -p, scheduling reset to SCHED_OTHER;
- with HAVE_CAP, the same capabilities after execve(): the spawning
  thread clears its own inheritable (and so ambient) set around
  posix_spawn() and restores it afterwards. Capabilities are per
  thread, so no other thread is affected. All signals are blocked on
  the thread meanwhile, as ast_safe_fork() does around fork(); the
  child gets the thread's original signal mask. This is only done when
  the restore is expected to succeed (each inheritable capability is
  also permitted and in the bounding set); otherwise the fork() path
  is used. Should a restore fail anyway, it is logged and fork() is
  used from then on. With no_new_privs set the fork() path is also
  used: there execve() does not hand back the permitted and effective
  capabilities that the fork() path drops, so the result can differ.
If the option is off (default), posix_spawn() is unavailable, or
anything in the spawn setup fails, the existing fork() path runs
unchanged. The implementation is in the new file
main/safe_system_spawn.c; asterisk.c only calls it.

Known difference: on glibc 2.34 (EL9) the spawned child ignores
signals 32 and 33, which glibc reserves for its own use (SigIgn
0x180000000 in /proc/<pid>/status). This comes from glibc's
posix_spawn() and was not observed on the glibc 2.39 test system.
glibc programs install their own handlers for these signals when
they need them.

The new option is the first ast_options flag above bit 31. This exposed
AST_OPT_FLAG_GENERIC_PLC_ON_EQUAL_CODECS = (1 << 31): as a signed int it
is negative, so with a 64-bit enum it became 0xFFFFFFFF80000000.
Clearing it (main/plc.c does so at every load unless
genericplc_on_equal_codecs is set) cleared all flags from bit 31 up, and
setting it set all of them. It is now (1ULL << 31).

For the same reason handle_debug_or_trace() in main/cli.c now keeps its
flag in a uint64_t. It was an unsigned int, so turning off the last
per-module debug or trace entry ("core set debug 0 <module>") cleared
every flag above bit 31.

AI assistance: Claude Opus 5.5, Claude Fable 5.2

Resolves: #2289

UserNote: A new asterisk.conf [options] setting, safe_system_spawn
(default no), makes System(), TrySystem(), voicemail
mailcmd/externnotify and other users of ast_safe_system() start their
child process with posix_spawn() instead of fork(). On large Asterisk
processes fork() can delay other Asterisk threads for tens of
milliseconds per call. Requires glibc 2.34 or later; on other systems
the setting has no effect. The current state is shown by
"core show settings".
pull/2290/head
phoneben 3 weeks ago
parent 7dc7542a95
commit ff75b32a4f

@ -95,6 +95,15 @@ documentation_language = en_US ; Set the language you want documentation
; gosub - Invoke the stdexten using a gosub as
; documented in extensions.conf.sample.
; Default gosub.
;safe_system_spawn = no ; Start the children of System(), TrySystem(),
; voicemail mailcmd/externnotify etc. with
; posix_spawn() instead of fork(). fork() copies
; the page tables of the whole Asterisk process,
; which on large systems can delay other threads
; (including timing) by tens of milliseconds
; per call.
; Requires glibc 2.34 or later; otherwise fork()
; is used. Default no.
;live_dangerously = no ; Enable the execution of 'dangerous' dialplan
; functions and configuration file access from
; external sources (AMI, etc.) These functions

@ -127,4 +127,22 @@ int ast_parking_stasis_init(void);
*/
int ast_endpoint_init(void);
/*!
* \brief Start "/bin/sh -c s" for ast_safe_system() with posix_spawn().
*
* Only when the asterisk.conf option safe_system_spawn is enabled and the
* C library supports it (glibc >= 2.34). On success ast_replace_sigchld()
* has been called; the caller waits for the child and then calls
* ast_unreplace_sigchld().
*
* \return pid of the child
* \retval -1 not enabled or not possible: use the fork() path
*/
pid_t ast_safe_system_spawn(const char *s);
/*!
* \brief State of the safe_system_spawn option, for "core show settings".
*/
const char *ast_safe_system_spawn_status(void);
#endif /* _ASTERISK__PRIVATE_H */

@ -100,7 +100,9 @@ enum ast_option_flags {
/*! Generic PLC */
AST_OPT_FLAG_GENERIC_PLC = (1 << 30),
/*! Generic PLC onm equal codecs */
AST_OPT_FLAG_GENERIC_PLC_ON_EQUAL_CODECS = (1 << 31),
AST_OPT_FLAG_GENERIC_PLC_ON_EQUAL_CODECS = (1ULL << 31),
/*! Start ast_safe_system() children with posix_spawn() instead of fork() */
AST_OPT_FLAG_SAFE_SYSTEM_SPAWN = (1ULL << 32),
/*!
* ast_options is now an ast_flags64 structure so if you add more
* options, make sure to use (1ULL << <bits>) to ensure that the
@ -146,6 +148,7 @@ enum ast_option_flags {
#define ast_opt_generic_plc_on_equal_codecs ast_test_flag64(&ast_options, AST_OPT_FLAG_GENERIC_PLC_ON_EQUAL_CODECS)
#define ast_opt_hide_messaging_ami_events ast_test_flag64(&ast_options, AST_OPT_FLAG_HIDE_MESSAGING_AMI_EVENTS)
#define ast_opt_sounds_search_custom ast_test_flag64(&ast_options, AST_OPT_FLAG_SOUNDS_SEARCH_CUSTOM)
#define ast_opt_safe_system_spawn ast_test_flag64(&ast_options, AST_OPT_FLAG_SAFE_SYSTEM_SPAWN)
/*! Maximum log level defined by PJPROJECT. */
#define MAX_PJ_LOG_MAX_LEVEL 6

@ -561,6 +561,7 @@ static char *handle_show_settings(struct ast_cli_entry *e, int cmd, struct ast_c
ast_cli(a->fd, " Generic PLC on equal codecs: %s\n", ast_opt_generic_plc_on_equal_codecs ? "Enabled" : "Disabled");
ast_cli(a->fd, " Hide Msg Chan AMI events: %s\n", ast_opt_hide_messaging_ami_events ? "Enabled" : "Disabled");
ast_cli(a->fd, " Sounds search custom dir: %s\n", ast_opt_sounds_search_custom ? "Enabled" : "Disabled");
ast_cli(a->fd, " safe_system_spawn option: %s\n", ast_safe_system_spawn_status());
ast_cli(a->fd, " Min DTMF duration:: %u\n", option_dtmfminduration);
#if !defined(LOW_MEMORY)
ast_cli(a->fd, " Cache media frames: %s\n", ast_opt_cache_media_frames ? "Enabled" : "Disabled");
@ -1246,7 +1247,14 @@ int ast_safe_execvp(int dualfork, const char *file, char *const argv[])
int ast_safe_system(const char *s)
{
pid_t pid = safe_exec_prep(0);
pid_t pid;
pid = ast_safe_system_spawn(s);
if (pid > 0) {
return safe_exec_wait(pid);
}
pid = safe_exec_prep(0);
if (pid == 0) {
execl("/bin/sh", "/bin/sh", "-c", s, (char *) NULL);

@ -456,7 +456,7 @@ static char *handle_debug_or_trace(int handler, struct ast_cli_entry *e, int cmd
int atleast = 0;
struct module_level *ml;
struct module_level_list *modules;
unsigned int module_option;
uint64_t module_option;
int *core_option;
const char *handler_name;

@ -474,6 +474,8 @@ void load_asterisk_conf(void)
ast_set2_flag64(&ast_options, ast_true(v->value), AST_OPT_FLAG_HIDE_MESSAGING_AMI_EVENTS);
} else if (!strcasecmp(v->name, "sounds_search_custom_dir")) {
ast_set2_flag64(&ast_options, ast_true(v->value), AST_OPT_FLAG_SOUNDS_SEARCH_CUSTOM);
} else if (!strcasecmp(v->name, "safe_system_spawn")) {
ast_set2_flag64(&ast_options, ast_true(v->value), AST_OPT_FLAG_SAFE_SYSTEM_SPAWN);
} else if (!strcasecmp(v->name, "channel_storage_backend")) {
internal_channel_set_current_storage_driver(v->value);
} else if (!strcasecmp(v->name, "disable_remote_console_shell")) {

@ -0,0 +1,375 @@
/*
* Asterisk -- An open source telephony toolkit.
*
* Copyright (C) 2026, Ben Kolodny
*
* Ben Kolodny <3232963@gmail.com>
*
* See http://www.asterisk.org for more information about
* the Asterisk project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, mailing lists and IRC
* channels for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
*/
/*! \file
*
* \brief Opt-in posix_spawn() for ast_safe_system()
*
* fork() copies the page tables of the whole Asterisk process while holding
* its mm lock, which on large processes delays other threads (timing
* included) for tens of milliseconds. With the asterisk.conf option
* safe_system_spawn, ast_safe_system() starts its child with posix_spawn()
* instead (glibc >= 2.34).
*/
/*** MODULEINFO
<support_level>core</support_level>
***/
#include "asterisk.h"
#include <sched.h>
#include <signal.h>
#include <unistd.h>
#include "asterisk/_private.h"
#include "asterisk/app.h"
#include "asterisk/lock.h"
#include "asterisk/logger.h"
#include "asterisk/options.h"
#if defined(__linux__) && defined(__GLIBC__) && \
(__GLIBC__ > 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ >= 34))
/*
* glibc's posix_spawn() has been clone(CLONE_VM|CLONE_VFORK) based since 2.24,
* so it does not copy our page tables. 2.34 is required for
* posix_spawn_file_actions_addclosefrom_np(), needed to close inherited fds
* the way safe_exec_prep() does.
*/
#define AST_SAFE_SYSTEM_SPAWN 1
#include <spawn.h>
#ifdef HAVE_CAP
#include <sys/capability.h>
#include <sys/prctl.h>
#include <linux/securebits.h>
#endif
#endif
#ifdef AST_SAFE_SYSTEM_SPAWN
extern char **environ;
/*! \brief Set if capabilities could not be restored after a spawn: fork() is used from then on */
static int spawn_disabled;
/*! \brief What safe_system_caps_clear() changed, for safe_system_caps_restore() */
struct safe_system_caps {
#ifdef HAVE_CAP
/*! The thread's capabilities before the change, NULL if nothing was changed */
cap_t saved;
/*! The thread's ambient capabilities before the change, one bit each */
uint64_t ambient;
#else
char unused;
#endif
};
#ifdef HAVE_CAP
/*! \brief Capabilities are numbered 0-63 in the kernel ABI and are kept one bit each */
#define AST_SAFE_SYSTEM_MAX_CAPS 64
/*!
* \internal
* \brief Clear the calling thread's inheritable (and so ambient) capabilities.
*
* The fork() path drops every capability in the child before execve()
* (child_cap is the empty set). posix_spawn() cannot do that in the child.
* Of the capability sets only the inheritable and ambient ones carry into
* the executed program, and they are per thread on Linux: cap_set_proc()
* changes the calling thread only. (This relies on Asterisk not being
* linked with libpsx, which makes libcap apply changes to all threads.)
* So the spawning thread clears its own inheritable set for the duration
* of posix_spawn(), which gives the child the same capabilities after
* execve() as the fork() path and touches no other thread. Clearing the
* inheritable set also clears the ambient set.
*
* Asterisk itself makes CAP_NET_ADMIN inheritable when it changes to a run
* user; a service manager may set others, and ambient capabilities.
*
* Only done when safe_system_caps_restore() is expected to be able to put
* everything back: each inheritable capability must also be permitted and
* in the bounding set, and ambient capabilities must be raisable.
*
* \retval 0 done, or nothing to do
* \retval -1 nothing was changed: use the fork() path
*/
static int safe_system_caps_clear(struct safe_system_caps *caps)
{
cap_t cur;
cap_t tmp;
cap_t checked = NULL;
cap_value_t cap;
int res = -1;
caps->saved = NULL;
caps->ambient = 0;
/*
* The fork() path also drops the permitted and effective sets, which
* cannot be done here. Normally that makes no difference, because
* execve() works both sets out again from scratch. With no_new_privs
* it only lets the program keep what the caller still had, so the
* result can differ: leave that to the fork() path.
*/
if (prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0) != 0) {
return -1;
}
cur = cap_get_proc();
tmp = cur ? cap_dup(cur) : NULL;
if (!tmp || cap_clear_flag(tmp, CAP_INHERITABLE)) {
goto done;
}
if (!cap_compare(cur, tmp)) {
/* Nothing inheritable, so nothing ambient either */
res = 0;
goto done;
}
/* The capabilities without the inheritable ones, which are added back as they are checked */
checked = cap_dup(tmp);
if (!checked) {
goto done;
}
for (cap = 0; cap < AST_SAFE_SYSTEM_MAX_CAPS; cap++) {
cap_flag_value_t inheritable;
cap_flag_value_t permitted;
if (cap_get_flag(cur, cap, CAP_INHERITABLE, &inheritable)) {
/* Past the last capability libcap knows */
break;
}
if (inheritable != CAP_SET) {
continue;
}
if (cap_get_flag(cur, cap, CAP_PERMITTED, &permitted) || permitted != CAP_SET
|| cap_get_bound(cap) != 1) {
/* Could not be made inheritable again */
goto done;
}
if (cap_set_flag(checked, CAP_INHERITABLE, 1, &cap, CAP_SET)) {
goto done;
}
switch (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_IS_SET, cap, 0, 0)) {
case 0:
break;
case 1:
caps->ambient |= 1ULL << cap;
break;
default:
/* Not known whether it is ambient, so it could not be put back */
goto done;
}
}
if (cap_compare(cur, checked)) {
/* An inheritable capability that could not be checked */
goto done;
}
if (caps->ambient && (prctl(PR_GET_SECUREBITS, 0, 0, 0, 0) & SECBIT_NO_CAP_AMBIENT_RAISE)) {
/* Could not be made ambient again */
goto done;
}
if (cap_set_proc(tmp)) {
goto done;
}
caps->saved = cur;
cur = NULL;
res = 0;
done:
if (checked) {
cap_free(checked);
}
if (tmp) {
cap_free(tmp);
}
if (cur) {
cap_free(cur);
}
return res;
}
/*!
* \internal
* \brief Undo safe_system_caps_clear() on the calling thread
*
* \retval 0 restored, or nothing to restore
* \retval -1 failed; not expected, safe_system_caps_clear() checks first
*/
static int safe_system_caps_restore(struct safe_system_caps *caps)
{
cap_value_t cap;
int res = 0;
if (!caps->saved) {
return 0;
}
if (cap_set_proc(caps->saved)) {
ast_log(LOG_ERROR, "Unable to restore capabilities after spawn: %s\n", strerror(errno));
res = -1;
}
for (cap = 0; !res && cap < AST_SAFE_SYSTEM_MAX_CAPS; cap++) {
if ((caps->ambient & (1ULL << cap)) && prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, cap, 0, 0)) {
ast_log(LOG_ERROR, "Unable to restore ambient capability %d after spawn: %s\n", cap, strerror(errno));
res = -1;
}
}
cap_free(caps->saved);
return res;
}
#else
static int safe_system_caps_clear(struct safe_system_caps *caps)
{
return 0;
}
static int safe_system_caps_restore(struct safe_system_caps *caps)
{
return 0;
}
#endif
/*!
* \internal
* \brief Start "/bin/sh -c s" with posix_spawn() instead of fork().
*
* The child is set up the same way safe_exec_prep() sets up a forked one:
* descriptors above stderr are closed, with high priority (-p) the
* scheduling policy is reset, and it has the same capabilities after
* execve() (see safe_system_caps_clear()). Application signal dispositions
* and the original signal mask are preserved; signals glibc reserves for
* itself may differ from the fork() path.
*
* As in ast_safe_fork(), all signals are blocked on this thread around the
* spawn, so no signal handler runs while its capabilities are changed.
*
* \return pid of the child, after ast_replace_sigchld() (the caller waits)
* \retval -1 spawn not possible, the caller uses the fork() path
*/
static pid_t safe_system_spawn(const char *s)
{
posix_spawn_file_actions_t actions;
posix_spawnattr_t attr;
struct safe_system_caps caps;
char *argv[] = { "/bin/sh", "-c", (char *) s, NULL };
sigset_t all;
sigset_t old_mask;
short flags = POSIX_SPAWN_SETSIGMASK;
pid_t pid = -1;
int res;
int sigmask_res;
if (ast_atomic_fetch_add(&spawn_disabled, 0, __ATOMIC_RELAXED)) {
return -1;
}
if (posix_spawn_file_actions_init(&actions)) {
return -1;
}
if (posix_spawnattr_init(&attr)) {
posix_spawn_file_actions_destroy(&actions);
return -1;
}
sigfillset(&all);
if (pthread_sigmask(SIG_BLOCK, &all, &old_mask)) {
posix_spawnattr_destroy(&attr);
posix_spawn_file_actions_destroy(&actions);
return -1;
}
res = posix_spawn_file_actions_addclosefrom_np(&actions, STDERR_FILENO + 1);
if (!res) {
/* The child gets this thread's original signal mask, as with fork() */
res = posix_spawnattr_setsigmask(&attr, &old_mask);
}
if (!res && ast_opt_high_priority) {
struct sched_param sched = { .sched_priority = 0, };
res = posix_spawnattr_setschedpolicy(&attr, SCHED_OTHER);
if (!res) {
res = posix_spawnattr_setschedparam(&attr, &sched);
}
flags |= POSIX_SPAWN_SETSCHEDULER;
}
if (!res) {
res = posix_spawnattr_setflags(&attr, flags);
}
if (!res && safe_system_caps_clear(&caps)) {
res = EPERM;
}
if (!res) {
ast_replace_sigchld();
res = posix_spawn(&pid, "/bin/sh", &actions, &attr, argv, environ);
if (safe_system_caps_restore(&caps)) {
/* The child, if any, is already running: this is not a spawn failure */
ast_atomic_fetch_or(&spawn_disabled, 1, __ATOMIC_RELAXED);
ast_log(LOG_ERROR, "Using fork() for ast_safe_system() from now on\n");
}
if (res) {
ast_unreplace_sigchld();
pid = -1;
}
}
sigmask_res = pthread_sigmask(SIG_SETMASK, &old_mask, NULL);
if (sigmask_res) {
ast_log(LOG_ERROR, "Unable to restore the signal mask after spawn: %s\n", strerror(sigmask_res));
}
if (res) {
ast_debug(1, "posix_spawn() not usable (%s), using fork()\n", strerror(res));
}
posix_spawnattr_destroy(&attr);
posix_spawn_file_actions_destroy(&actions);
return pid;
}
#endif /* AST_SAFE_SYSTEM_SPAWN */
const char *ast_safe_system_spawn_status(void)
{
if (!ast_opt_safe_system_spawn) {
return "Disabled";
}
#ifdef AST_SAFE_SYSTEM_SPAWN
if (ast_atomic_fetch_add(&spawn_disabled, 0, __ATOMIC_RELAXED)) {
return "Enabled, but fork() forced after a capability restore failure";
}
return "Enabled (fork() still used when a spawn cannot be set up)";
#else
return "Enabled, but not supported here (fork() used)";
#endif
}
pid_t ast_safe_system_spawn(const char *s)
{
if (!ast_opt_safe_system_spawn) {
return -1;
}
#ifdef AST_SAFE_SYSTEM_SPAWN
return safe_system_spawn(s);
#else
return -1;
#endif
}
Loading…
Cancel
Save