asterisk-11.x: add upstream patches for CVEs
[feed/telephony.git] / net / asterisk-11.x / patches / 025-AST-2017-006-11.diff
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
5
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.
11
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.
19
20 * Changed app_minivm.c:run_externnotify() to use the new ast_safe_execvp()
21 instead of ast_safe_system() to avoid command injection.
22
23 * Document code injection potential from untrusted data sources for other
24 shell commands that are under user control.
25
26 ASTERISK-27103
27
28 Change-Id: I7552472247a84cde24e1358aaf64af160107aef1
29 ---
30
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
35 @@ -94,6 +94,13 @@
36 ways in which you can mitigate this impact: stricter pattern matching, or using
37 the FILTER() dialplan function.
38
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.
45 +
46 Strict Pattern Matching
47 -----------------------
48
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)
56 {
57 - char arguments[BUFSIZ];
58 + char fquser[AST_MAX_CONTEXT * 2];
59 + char *argv[5] = { NULL };
60 + struct ast_party_caller *caller;
61 + char *cid;
62 + int idx;
63
64 - if (ast_strlen_zero(vmu->externnotify) && ast_strlen_zero(global_externnotify))
65 + if (ast_strlen_zero(vmu->externnotify) && ast_strlen_zero(global_externnotify)) {
66 return;
67 + }
68
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);
77
78 - ast_debug(1, "Executing: %s\n", arguments);
79 - ast_safe_system(arguments);
80 + caller = ast_channel_caller(chan);
81 + idx = 0;
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);
85 + if (cid) {
86 + argv[idx++] = cid;
87 + }
88 + cid = S_COR(caller->id.number.valid, caller->id.number.str, NULL);
89 + if (cid) {
90 + argv[idx++] = cid;
91 + }
92 + argv[idx] = NULL;
93 +
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);
97 }
98
99 /*!\internal
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
104 @@ -127,6 +127,11 @@
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>
113 </parameter>
114 </syntax>
115 <description>
116 @@ -143,6 +148,11 @@
117 <para>Will contain the filename used to record.</para>
118 </variable>
119 </variablelist>
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>
125 </description>
126 <see-also>
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
132 @@ -48,6 +48,11 @@
133 <syntax>
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>
141 </parameter>
142 </syntax>
143 <description>
144 @@ -73,6 +78,11 @@
145 <syntax>
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>
153 </parameter>
154 </syntax>
155 <description>
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
160 @@ -51,7 +51,7 @@
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
163 ;
164 -; <app> <username@domain> <callerid-number> <callerid-name>
165 +; <app> <username@domain> <callerid-name> <callerid-number>
166 ;
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
173 @@ -84,6 +84,11 @@
174 <syntax>
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>
182 </parameter>
183 </syntax>
184 <description>
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
189 @@ -577,9 +577,34 @@
190 int ast_vm_test_create_user(const char *context, const char *mailbox);
191 #endif
192
193 -/*! \brief Safely spawn an external program while closing file descriptors
194 - \note This replaces the \b system call in all Asterisk modules
195 -*/
196 +/*!
197 + * \brief Safely spawn an external program while closing file descriptors
198 + *
199 + * \note This replaces the \b execvp call in all Asterisk modules
200 + *
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
209 + */
210 +int ast_safe_execvp(int dualfork, const char *file, char *const argv[]);
211 +
212 +/*!
213 + * \brief Safely spawn an OS shell command while closing file descriptors
214 + *
215 + * \note This replaces the \b system call in all Asterisk modules
216 + *
217 + * \param s - OS shell command string to execute.
218 + *
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.
223 + */
224 int ast_safe_system(const char *s);
225
226 /*!
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);
233 }
234
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)
238 {
239 pid_t pid;
240 - int res;
241 - struct rusage rusage;
242 - int status;
243
244 #if defined(HAVE_WORKING_FORK) || defined(HAVE_WORKING_VFORK)
245 ast_replace_sigchld();
246 @@ -1129,35 +1127,102 @@
247 cap_free(cap);
248 #endif
249 #ifdef HAVE_WORKING_FORK
250 - if (ast_opt_high_priority)
251 + if (ast_opt_high_priority) {
252 ast_set_priority(0);
253 + }
254 /* Close file descriptors and launch system command */
255 ast_close_fds_above_n(STDERR_FILENO);
256 #endif
257 - execl("/bin/sh", "/bin/sh", "-c", s, (char *) NULL);
258 - _exit(1);
259 - } else if (pid > 0) {
260 + if (dualfork) {
261 +#ifdef HAVE_WORKING_FORK
262 + pid = fork();
263 +#else
264 + pid = vfork();
265 +#endif
266 + if (pid < 0) {
267 + /* Second fork failed. */
268 + /* No logger available. */
269 + _exit(1);
270 + }
271 +
272 + if (pid > 0) {
273 + /* This is the first fork, exit so the reaper finishes right away. */
274 + _exit(0);
275 + }
276 +
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.
281 + */
282 + }
283 + }
284 +
285 + if (pid < 0) {
286 + ast_log(LOG_WARNING, "Fork failed: %s\n", strerror(errno));
287 + }
288 +#else
289 + ast_log(LOG_WARNING, "Fork failed: %s\n", strerror(ENOTSUP));
290 + pid = -1;
291 +#endif
292 +
293 + return pid;
294 +}
295 +
296 +/*! \brief wait for spawned application to complete and unreplace sigchld */
297 +static int safe_exec_wait(pid_t pid)
298 +{
299 + int res = -1;
300 +
301 +#if defined(HAVE_WORKING_FORK) || defined(HAVE_WORKING_VFORK)
302 + if (pid > 0) {
303 for (;;) {
304 + struct rusage rusage;
305 + int status;
306 +
307 res = wait4(pid, &status, 0, &rusage);
308 if (res > -1) {
309 res = WIFEXITED(status) ? WEXITSTATUS(status) : -1;
310 break;
311 - } else if (errno != EINTR)
312 + }
313 + if (errno != EINTR) {
314 break;
315 + }
316 }
317 - } else {
318 - ast_log(LOG_WARNING, "Fork failed: %s\n", strerror(errno));
319 - res = -1;
320 }
321
322 ast_unreplace_sigchld();
323 -#else /* !defined(HAVE_WORKING_FORK) && !defined(HAVE_WORKING_VFORK) */
324 - res = -1;
325 #endif
326
327 return res;
328 }
329
330 +int ast_safe_execvp(int dualfork, const char *file, char *const argv[])
331 +{
332 + pid_t pid = safe_exec_prep(dualfork);
333 +
334 + if (pid == 0) {
335 + execvp(file, argv);
336 + _exit(1);
337 + /* noreturn from _exit */
338 + }
339 +
340 + return safe_exec_wait(pid);
341 +}
342 +
343 +int ast_safe_system(const char *s)
344 +{
345 + pid_t pid = safe_exec_prep(0);
346 +
347 + if (pid == 0) {
348 + execl("/bin/sh", "/bin/sh", "-c", s, (char *) NULL);
349 + _exit(1);
350 + /* noreturn from _exit */
351 + }
352 +
353 + return safe_exec_wait(pid);
354 +}
355 +
356 /*!
357 * \brief enable or disable a logging level to a specified console
358 */
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
363 @@ -57,17 +57,17 @@
364 <syntax>
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>
369 </argument>
370 <argument name="urlbase" />
371 </parameter>
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>
375 </parameter>
376 <parameter name="options">
377 <optionlist>
378 <option name="m">
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.
384 @@ -78,6 +78,13 @@
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>
395 </option>
396 <option name="b">
397 <para>Don't begin recording unless a call is bridged to another channel.</para>