1 From 31676ce058596b57e10fbf83ff1817ca7907c3b1 Mon Sep 17 00:00:00 2001
2 From: Corey Farrell <git@cfware.com>
3 Date: Sat, 01 Jul 2017 20:24:27 -0400
4 Subject: [PATCH] AST-2017-006: Fix app_minivm application MinivmNotify command injection
6 An admin can configure app_minivm with an externnotify program to be run
7 when a voicemail is received. The app_minivm application MinivmNotify
8 uses ast_safe_system() for this purpose which is vulnerable to command
9 injection since the Caller-ID name and number values given to externnotify
10 can come from an external untrusted source.
12 * Add ast_safe_execvp() function. This gives modules the ability to run
13 external commands with greater safety compared to ast_safe_system().
14 Specifically when some parameters are filled by untrusted sources the new
15 function does not allow malicious input to break argument encoding. This
16 may be of particular concern where CALLERID(name) or CALLERID(num) may be
17 used as a parameter to a script run by ast_safe_system() which could
18 potentially allow arbitrary command execution.
20 * Changed app_minivm.c:run_externnotify() to use the new ast_safe_execvp()
21 instead of ast_safe_system() to avoid command injection.
23 * Document code injection potential from untrusted data sources for other
24 shell commands that are under user control.
28 Change-Id: I7552472247a84cde24e1358aaf64af160107aef1
31 diff --git a/README-SERIOUSLY.bestpractices.txt b/README-SERIOUSLY.bestpractices.txt
32 index 281d0d3..d63f1df 100644
33 --- a/README-SERIOUSLY.bestpractices.txt
34 +++ b/README-SERIOUSLY.bestpractices.txt
36 ways in which you can mitigate this impact: stricter pattern matching, or using
37 the FILTER() dialplan function.
39 +The CALLERID(num) and CALLERID(name) values are other commonly used values that
40 +are sources of data potentially supplied by outside sources. If you use these
41 +values as parameters to the System(), MixMonitor(), or Monitor() applications
42 +or the SHELL() dialplan function, you can allow injection of arbitrary operating
43 +system command execution. The FILTER() dialplan function is available to remove
44 +dangerous characters from untrusted strings to block the command injection.
46 Strict Pattern Matching
47 -----------------------
49 diff --git a/apps/app_minivm.c b/apps/app_minivm.c
50 index ecdf9c6..8edc132 100644
51 --- a/apps/app_minivm.c
52 +++ b/apps/app_minivm.c
53 @@ -1741,21 +1741,35 @@
54 /*! \brief Run external notification for voicemail message */
55 static void run_externnotify(struct ast_channel *chan, struct minivm_account *vmu)
57 - char arguments[BUFSIZ];
58 + char fquser[AST_MAX_CONTEXT * 2];
59 + char *argv[5] = { NULL };
60 + struct ast_party_caller *caller;
64 - if (ast_strlen_zero(vmu->externnotify) && ast_strlen_zero(global_externnotify))
65 + if (ast_strlen_zero(vmu->externnotify) && ast_strlen_zero(global_externnotify)) {
69 - snprintf(arguments, sizeof(arguments), "%s %s@%s %s %s&",
70 - ast_strlen_zero(vmu->externnotify) ? global_externnotify : vmu->externnotify,
71 - vmu->username, vmu->domain,
72 - (ast_channel_caller(chan)->id.name.valid && ast_channel_caller(chan)->id.name.str)
73 - ? ast_channel_caller(chan)->id.name.str : "",
74 - (ast_channel_caller(chan)->id.number.valid && ast_channel_caller(chan)->id.number.str)
75 - ? ast_channel_caller(chan)->id.number.str : "");
76 + snprintf(fquser, sizeof(fquser), "%s@%s", vmu->username, vmu->domain);
78 - ast_debug(1, "Executing: %s\n", arguments);
79 - ast_safe_system(arguments);
80 + caller = ast_channel_caller(chan);
82 + argv[idx++] = ast_strlen_zero(vmu->externnotify) ? global_externnotify : vmu->externnotify;
83 + argv[idx++] = fquser;
84 + cid = S_COR(caller->id.name.valid, caller->id.name.str, NULL);
88 + cid = S_COR(caller->id.number.valid, caller->id.number.str, NULL);
94 + ast_debug(1, "Executing: %s %s %s %s\n",
95 + argv[0], argv[1], argv[2] ?: "", argv[3] ?: "");
96 + ast_safe_execvp(1, argv[0], argv);
100 diff --git a/apps/app_mixmonitor.c b/apps/app_mixmonitor.c
101 index 89a1d8c..96adb9a 100644
102 --- a/apps/app_mixmonitor.c
103 +++ b/apps/app_mixmonitor.c
105 <para>Will be executed when the recording is over.</para>
106 <para>Any strings matching <literal>^{X}</literal> will be unescaped to <variable>X</variable>.</para>
107 <para>All variables will be evaluated at the time MixMonitor is called.</para>
108 + <warning><para>Do not use untrusted strings such as <variable>CALLERID(num)</variable>
109 + or <variable>CALLERID(name)</variable> as part of the command parameters. You
110 + risk a command injection attack executing arbitrary commands if the untrusted
111 + strings aren't filtered to remove dangerous characters. See function
112 + <variable>FILTER()</variable>.</para></warning>
117 <para>Will contain the filename used to record.</para>
120 + <warning><para>Do not use untrusted strings such as <variable>CALLERID(num)</variable>
121 + or <variable>CALLERID(name)</variable> as part of ANY of the application's
122 + parameters. You risk a command injection attack executing arbitrary commands
123 + if the untrusted strings aren't filtered to remove dangerous characters. See
124 + function <variable>FILTER()</variable>.</para></warning>
127 <ref type="application">Monitor</ref>
128 diff --git a/apps/app_system.c b/apps/app_system.c
129 index 7fe453d..e868a07 100644
130 --- a/apps/app_system.c
131 +++ b/apps/app_system.c
134 <parameter name="command" required="true">
135 <para>Command to execute</para>
136 + <warning><para>Do not use untrusted strings such as <variable>CALLERID(num)</variable>
137 + or <variable>CALLERID(name)</variable> as part of the command parameters. You
138 + risk a command injection attack executing arbitrary commands if the untrusted
139 + strings aren't filtered to remove dangerous characters. See function
140 + <variable>FILTER()</variable>.</para></warning>
146 <parameter name="command" required="true">
147 <para>Command to execute</para>
148 + <warning><para>Do not use untrusted strings such as <variable>CALLERID(num)</variable>
149 + or <variable>CALLERID(name)</variable> as part of the command parameters. You
150 + risk a command injection attack executing arbitrary commands if the untrusted
151 + strings aren't filtered to remove dangerous characters. See function
152 + <variable>FILTER()</variable>.</para></warning>
156 diff --git a/configs/minivm.conf.sample b/configs/minivm.conf.sample
157 index 55a39c8..3dcd59d 100644
158 --- a/configs/minivm.conf.sample
159 +++ b/configs/minivm.conf.sample
161 ; If you need to have an external program, i.e. /usr/bin/myapp called when a
162 ; voicemail is received by the server. The arguments are
164 -; <app> <username@domain> <callerid-number> <callerid-name>
165 +; <app> <username@domain> <callerid-name> <callerid-number>
167 ;externnotify=/usr/bin/myapp
168 ; The character set for voicemail messages can be specified here
169 diff --git a/funcs/func_shell.c b/funcs/func_shell.c
170 index e403efc..79b7f99 100644
171 --- a/funcs/func_shell.c
172 +++ b/funcs/func_shell.c
175 <parameter name="command" required="true">
176 <para>The command that the shell should execute.</para>
177 + <warning><para>Do not use untrusted strings such as <variable>CALLERID(num)</variable>
178 + or <variable>CALLERID(name)</variable> as part of the command parameters. You
179 + risk a command injection attack executing arbitrary commands if the untrusted
180 + strings aren't filtered to remove dangerous characters. See function
181 + <variable>FILTER()</variable>.</para></warning>
185 diff --git a/include/asterisk/app.h b/include/asterisk/app.h
186 index d10a0a6..8cdaea1 100644
187 --- a/include/asterisk/app.h
188 +++ b/include/asterisk/app.h
190 int ast_vm_test_create_user(const char *context, const char *mailbox);
193 -/*! \brief Safely spawn an external program while closing file descriptors
194 - \note This replaces the \b system call in all Asterisk modules
197 + * \brief Safely spawn an external program while closing file descriptors
199 + * \note This replaces the \b execvp call in all Asterisk modules
201 + * \param dualfork Non-zero to simulate running the program in the
202 + * background by forking twice. The option provides similar
203 + * functionality to the '&' in the OS shell command "cmd &". The
204 + * option allows Asterisk to run a reaper loop to watch the first fork
205 + * which immediately exits after spaning the second fork. The actual
206 + * program is run in the second fork.
207 + * \param file execvp(file, argv) file parameter
208 + * \param argv execvp(file, argv) argv parameter
210 +int ast_safe_execvp(int dualfork, const char *file, char *const argv[]);
213 + * \brief Safely spawn an OS shell command while closing file descriptors
215 + * \note This replaces the \b system call in all Asterisk modules
217 + * \param s - OS shell command string to execute.
219 + * \warning Command injection can happen using this call if the passed
220 + * in string is created using untrusted data from an external source.
221 + * It is best not to use untrusted data. However, the caller could
222 + * filter out dangerous characters to avoid command injection.
224 int ast_safe_system(const char *s);
227 diff --git a/main/asterisk.c b/main/asterisk.c
228 index ce1d153..92256bd 100644
229 --- a/main/asterisk.c
230 +++ b/main/asterisk.c
231 @@ -1102,12 +1102,10 @@
232 ast_mutex_unlock(&safe_system_lock);
235 -int ast_safe_system(const char *s)
236 +/*! \brief fork and perform other preparations for spawning applications */
237 +static pid_t safe_exec_prep(int dualfork)
241 - struct rusage rusage;
244 #if defined(HAVE_WORKING_FORK) || defined(HAVE_WORKING_VFORK)
245 ast_replace_sigchld();
246 @@ -1129,35 +1127,102 @@
249 #ifdef HAVE_WORKING_FORK
250 - if (ast_opt_high_priority)
251 + if (ast_opt_high_priority) {
254 /* Close file descriptors and launch system command */
255 ast_close_fds_above_n(STDERR_FILENO);
257 - execl("/bin/sh", "/bin/sh", "-c", s, (char *) NULL);
259 - } else if (pid > 0) {
261 +#ifdef HAVE_WORKING_FORK
267 + /* Second fork failed. */
268 + /* No logger available. */
273 + /* This is the first fork, exit so the reaper finishes right away. */
277 + /* This is the second fork. The first fork will exit immediately so
278 + * Asterisk doesn't have to wait for completion.
279 + * ast_safe_system("cmd &") would run in the background, but the '&'
280 + * cannot be added with ast_safe_execvp, so we have to double fork.
286 + ast_log(LOG_WARNING, "Fork failed: %s\n", strerror(errno));
289 + ast_log(LOG_WARNING, "Fork failed: %s\n", strerror(ENOTSUP));
296 +/*! \brief wait for spawned application to complete and unreplace sigchld */
297 +static int safe_exec_wait(pid_t pid)
301 +#if defined(HAVE_WORKING_FORK) || defined(HAVE_WORKING_VFORK)
304 + struct rusage rusage;
307 res = wait4(pid, &status, 0, &rusage);
309 res = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
311 - } else if (errno != EINTR)
313 + if (errno != EINTR) {
318 - ast_log(LOG_WARNING, "Fork failed: %s\n", strerror(errno));
322 ast_unreplace_sigchld();
323 -#else /* !defined(HAVE_WORKING_FORK) && !defined(HAVE_WORKING_VFORK) */
330 +int ast_safe_execvp(int dualfork, const char *file, char *const argv[])
332 + pid_t pid = safe_exec_prep(dualfork);
335 + execvp(file, argv);
337 + /* noreturn from _exit */
340 + return safe_exec_wait(pid);
343 +int ast_safe_system(const char *s)
345 + pid_t pid = safe_exec_prep(0);
348 + execl("/bin/sh", "/bin/sh", "-c", s, (char *) NULL);
350 + /* noreturn from _exit */
353 + return safe_exec_wait(pid);
357 * \brief enable or disable a logging level to a specified console
359 diff --git a/res/res_monitor.c b/res/res_monitor.c
360 index 76c43e1..12f478a 100644
361 --- a/res/res_monitor.c
362 +++ b/res/res_monitor.c
365 <parameter name="file_format" argsep=":">
366 <argument name="file_format" required="true">
367 - <para>optional, if not set, defaults to <literal>wav</literal></para>
368 + <para>Optional. If not set, defaults to <literal>wav</literal></para>
370 <argument name="urlbase" />
372 <parameter name="fname_base">
373 - <para>if set, changes the filename used to the one specified.</para>
374 + <para>If set, changes the filename used to the one specified.</para>
376 <parameter name="options">
379 - <para>when the recording ends mix the two leg files into one and
380 + <para>When the recording ends mix the two leg files into one and
381 delete the two leg files. If the variable <variable>MONITOR_EXEC</variable>
382 is set, the application referenced in it will be executed instead of
383 soxmix/sox and the raw leg files will NOT be deleted automatically.
385 will be passed on as additional arguments to <variable>MONITOR_EXEC</variable>.
386 Both <variable>MONITOR_EXEC</variable> and the Mix flag can be set from the
387 administrator interface.</para>
388 + <warning><para>Do not use untrusted strings such as
389 + <variable>CALLERID(num)</variable> or <variable>CALLERID(name)</variable>
390 + as part of <variable>MONITOR_EXEC</variable> or
391 + <variable>MONITOR_EXEC_ARGS</variable>. You risk a command injection
392 + attack executing arbitrary commands if the untrusted strings aren't
393 + filtered to remove dangerous characters. See function
394 + <variable>FILTER()</variable>.</para></warning>
397 <para>Don't begin recording unless a call is bridged to another channel.</para>