diff --git a/configs/samples/asterisk.conf.sample b/configs/samples/asterisk.conf.sample index 77352bcb6b..d15f683fbe 100644 --- a/configs/samples/asterisk.conf.sample +++ b/configs/samples/asterisk.conf.sample @@ -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 diff --git a/include/asterisk/_private.h b/include/asterisk/_private.h index 7a964a167a..10793fb5a1 100644 --- a/include/asterisk/_private.h +++ b/include/asterisk/_private.h @@ -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 */ diff --git a/include/asterisk/options.h b/include/asterisk/options.h index eda758519c..fb250e7e96 100644 --- a/include/asterisk/options.h +++ b/include/asterisk/options.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 << ) 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 diff --git a/main/asterisk.c b/main/asterisk.c index 544b22fe9e..584c9f18c9 100644 --- a/main/asterisk.c +++ b/main/asterisk.c @@ -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); diff --git a/main/cli.c b/main/cli.c index 90a901198e..2ba7dd35fa 100644 --- a/main/cli.c +++ b/main/cli.c @@ -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; diff --git a/main/options.c b/main/options.c index acd870cae6..a6d542f4f2 100644 --- a/main/options.c +++ b/main/options.c @@ -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")) { diff --git a/main/safe_system_spawn.c b/main/safe_system_spawn.c new file mode 100644 index 0000000000..49f5fd95c7 --- /dev/null +++ b/main/safe_system_spawn.c @@ -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 + core + ***/ + +#include "asterisk.h" + +#include +#include +#include + +#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 +#ifdef HAVE_CAP +#include +#include +#include +#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 +}