From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: lux Newsgroups: gmane.emacs.bugs Subject: bug#59817: [PATCH] Fix etags local command injection vulnerability Date: Tue, 6 Dec 2022 21:11:35 +0800 Message-ID: References: <83r0xf9qsx.fsf@gnu.org> <834jua9ggd.fsf@gnu.org> <83lenk7kuq.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="MP_/IyOO.Vb0zttQLsQMmeo=aLR" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="15777"; mail-complaints-to="usenet@ciao.gmane.io" Cc: stefankangas@gmail.com, 59817@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Dec 06 14:12:27 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1p2XkI-0003s6-1A for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 06 Dec 2022 14:12:26 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1p2Xjy-00048O-SC; Tue, 06 Dec 2022 08:12:06 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1p2Xju-000477-Af for bug-gnu-emacs@gnu.org; Tue, 06 Dec 2022 08:12:02 -0500 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1p2Xju-0000s2-0x for bug-gnu-emacs@gnu.org; Tue, 06 Dec 2022 08:12:02 -0500 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1p2Xjt-0001Pt-Kx for bug-gnu-emacs@gnu.org; Tue, 06 Dec 2022 08:12:01 -0500 X-Loop: help-debbugs@gnu.org Resent-From: lux Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 06 Dec 2022 13:12:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 59817 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: patch Original-Received: via spool by 59817-submit@debbugs.gnu.org id=B59817.16703323125439 (code B ref 59817); Tue, 06 Dec 2022 13:12:01 +0000 Original-Received: (at 59817) by debbugs.gnu.org; 6 Dec 2022 13:11:52 +0000 Original-Received: from localhost ([127.0.0.1]:42929 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p2Xjk-0001Pf-Co for submit@debbugs.gnu.org; Tue, 06 Dec 2022 08:11:52 -0500 Original-Received: from out203-205-221-235.mail.qq.com ([203.205.221.235]:50961) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1p2Xjg-0001PZ-CV for 59817@debbugs.gnu.org; Tue, 06 Dec 2022 08:11:50 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=qq.com; s=s201512; t=1670332298; bh=IoGQdl3aZIepG2ZYPGs3uFGEaBRZl1PjZsTktcJm7l8=; h=Date:From:To:Cc:Subject:In-Reply-To:References; b=dnWfSibq6+Bn4JfM+FJwp7mujSxEayuAEY7bJd9b1LTmxvt/sGNs9/7Mv5qef36L4 4aciB5KGZiww9himG3aQdRY4hN0k3C8uSuHeFcgEwP+g4YFmQhdIjpCDUSmKzGFq3P r21ldoLezjJ1yuGEyQzcJ9my5hiKNGAgL4UpSU4M= Original-Received: from lx-pc ([240e:399:e6f:ee32:f815:4044:ba50:97f9]) by newxmesmtplogicsvrszc2-0.qq.com (NewEsmtp) with SMTP id 2E530E9C; Tue, 06 Dec 2022 21:11:37 +0800 X-QQ-mid: xmsmtpt1670332297tc5zruu3z X-QQ-XMAILINFO: OEgU9iHXvJ5F82U4wQGOJKWpH+eW05UUQCMWiu0AXlnJvchdG1MF1dxNILiZW+ jLZ8isSmvWvtKRVTF9mFnIxqCVS0jZ1JBSDAZ3V0FtHQiyzIlifU9tvXoX9lHiq+qPwSukeY+Ape 7IFVbhByyUfcsLoyozfrnOaIY42ivoWlYYp01j+Fa/acCsNXv6ewelnwm4t5iV9cWiMchQX5eFcZ vIekaez82SvMjDeAM3aj10NcTX82XSNBNgY+ncUzu3rxY2SJ6YmNJtsvjxrR6yLp3N5lxXEDtvko ++61X93FB5yyC2FfGt33ivQUNcLB82SDmSw6NgHucONStTeY9gapxCjJ75V9jQp8dUlcFaTZwtY/ 7SmdETWkhjuZX49RDenWZIym1kRUO5Z9iSOiuAuMmpU5aK3IeJ1tKCScacZis7Qg0rPIie+8iMQO A6+UDBCWp72o2K7ZS4w3wp4xKqMXMml4fHdU5TuQYmbbbeKYcW+4iMC9jq1oH5vOdj3prxBS2xPx oGV0VCNJsrDwPyZm0GbmoqmSQj4So6NBfSioIifat4KXHI3k3PVTG1aV6gm59Yr+Z5sBWUsS3DST YeuBFhvnG+86ceTGdiv/tmJOZcSljyUaQ7Lq0USuCrq9rwxmZG/p0v0npc7iTOgO/fiJHzJHu4HP wBcPkHRrjG8gsa+jEcDTaN3rjUSGOLXXwk1n6cBsDq2Q8Tjdv6ueKDilyoF9bXOSM7BkGCleO75+ 3QBdoxTXns/w0PoDK+JL8SwVpccrwamxrRTM/JCxaLjsk2xF8XKgZoIjfQjf/u/9yRa4obf/3lJX fVzgqh9tShIl2G+nhwi3pImfm0EOMKC8Zi7E2JPE X-OQ-MSGID: <20221206211135.0c5304a9@lx-pc> In-Reply-To: <83lenk7kuq.fsf@gnu.org> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.35; x86_64-redhat-linux-gnu) X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:250108 Archived-At: --MP_/IyOO.Vb0zttQLsQMmeo=aLR Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit Content-Disposition: inline On Tue, 06 Dec 2022 14:55:09 +0200 Eli Zaretskii wrote: > The "MSDOS || DOS_NT" case also needs a small change: > > > char *cmd = concat (cmd1, "\" > ", tmp_name); > > This doesn't quote tmp_name; it should. Because double quotes have been used here, I have not reproduced this vulnerability in Windows, so I have not dealt: $ touch "etags.c\" && ipconfig \".z" $ ./etags.exe "etags.c\" && ipconfig \".z" etags.c" && ipconfig ".z: Invalid argument $ ./etags.exe * etags.exe: skipping inclusion of TAGS in self. etags.c" && ipconfig ".z: Invalid argument > > +static char* > ^^ > There should be a space before "*". done. > > > + if (*p == '\'') > > + { > > + new_str[i+1] = '\\'; > > + new_str[i+2] = '\''; > > + new_str[i+3] = '\''; > > + i += 3; > > I don't understand why you are adding ''\'' and not just \'. > Wouldn't the latter work for some reason? > Because the single quote escape is: '\'' $ echo ''\''hello world'\''' 'hello world' $ echo 'I'\''m a poor man' I'm a poor man --MP_/IyOO.Vb0zttQLsQMmeo=aLR Content-Type: text/x-patch Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename=0001-Fix-etags-local-command-injection-vulnerability.patch >From e182ffe325c882696e20d6e7f8fcbefe82198748 Mon Sep 17 00:00:00 2001 From: lu4nx Date: Tue, 6 Dec 2022 15:42:40 +0800 Subject: [PATCH] Fix etags local command injection vulnerability * lib-src/etags.c: (escape_shell_arg_string): New function. --- lib-src/etags.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 56 insertions(+), 2 deletions(-) diff --git a/lib-src/etags.c b/lib-src/etags.c index d1d20858cd..519829ec79 100644 --- a/lib-src/etags.c +++ b/lib-src/etags.c @@ -401,6 +401,7 @@ #define xrnew(op, n, m) ((op) = xnrealloc (op, n, (m) * sizeof *(op))) static void put_entries (node *); static void cleanup_tags_file (char const * const, char const * const); +static char *escape_shell_arg_string (char *); static void do_move_file (const char *, const char *); static char *concat (const char *, const char *, const char *); static char *skip_spaces (char *); @@ -1716,8 +1717,12 @@ process_file_name (char *file, language *lang) char *cmd1 = concat (compr->command, " \"", real_name); char *cmd = concat (cmd1, "\" > ", tmp_name); #else - char *cmd1 = concat (compr->command, " '", real_name); - char *cmd = concat (cmd1, "' > ", tmp_name); + char *new_real_name = escape_shell_arg_string (real_name); + char *new_tmp_name = escape_shell_arg_string (tmp_name); + char *cmd1 = concat (compr->command, " ", new_real_name); + char *cmd = concat (cmd1, " > ", new_tmp_name); + free (new_real_name); + free (new_tmp_name); #endif free (cmd1); inf = (system (cmd) == -1 @@ -7707,6 +7712,55 @@ etags_mktmp (void) return templt; } +/* + * Adds single quotes around a string, if found single quotes, escaped it. + * Return a newly-allocated string. + * + * For example: + * escape_shell_arg_string("test.txt") => 'test.txt' + * escape_shell_arg_string("'test.txt") => ''\''test.txt' + */ +static char * +escape_shell_arg_string (char *str) +{ + char *p = str; + int need_space = 2; /* ' at begin and end */ + + while (*p != '\0') + { + if (*p == '\'') + need_space += 4; /* ' to '\'', length is 4 */ + else + need_space++; + + p++; + } + + char *new_str = xnew (need_space + 1, char); + new_str[0] = '\''; + new_str[need_space-1] = '\''; + + int i = 1; /* skip first byte */ + p = str; + while (*p != '\0') + { + new_str[i] = *p; + if (*p == '\'') + { + new_str[i+1] = '\\'; + new_str[i+2] = '\''; + new_str[i+3] = '\''; + i += 3; + } + + i++; + p++; + } + + new_str[need_space] = '\0'; + return new_str; +} + static void do_move_file(const char *src_file, const char *dst_file) { -- 2.38.1 --MP_/IyOO.Vb0zttQLsQMmeo=aLR--