ee26d5
From 01a4035c869b91c153af9a9132c87adb7669ea1c Mon Sep 17 00:00:00 2001
ee26d5
From: lu4nx <lx@shellcodes.org>
ee26d5
Date: Tue, 6 Dec 2022 15:42:40 +0800
ee26d5
Subject: [PATCH] Fix etags local command injection vulnerability
ee26d5
ee26d5
* lib-src/etags.c: (escape_shell_arg_string): New function.
ee26d5
(process_file_name): Use it to quote file names passed to the
ee26d5
shell.  (Bug#59817)
ee26d5
---
ee26d5
 lib-src/etags.c | 63 +++++++++++++++++++++++++++++++++++++++++++++----
ee26d5
 1 file changed, 58 insertions(+), 5 deletions(-)
ee26d5
ee26d5
diff --git a/lib-src/etags.c b/lib-src/etags.c
ee26d5
index d1d20858cdd..ba0092cc637 100644
ee26d5
--- a/lib-src/etags.c
ee26d5
+++ b/lib-src/etags.c
ee26d5
@@ -399,6 +399,7 @@ static void put_entries (node *);
ee26d5
 static void clean_matched_file_tag (char const * const, char const * const);
ee26d5
 
ee26d5
 static void do_move_file (const char *, const char *);
ee26d5
+static char *escape_shell_arg_string (char *);
ee26d5
 static char *concat (const char *, const char *, const char *);
ee26d5
 static char *skip_spaces (char *);
ee26d5
 static char *skip_non_spaces (char *);
ee26d5
@@ -1670,13 +1671,16 @@ process_file_name (char *file, language *lang)
ee26d5
       else
ee26d5
 	{
ee26d5
 #if MSDOS || defined (DOS_NT)
ee26d5
-	  char *cmd1 = concat (compr->command, " \"", real_name);
ee26d5
-	  char *cmd = concat (cmd1, "\" > ", tmp_name);
ee26d5
+          int buf_len = strlen (compr->command) + strlen (" \"\" > \"\"") + strlen (real_name) + strlen (tmp_name) + 1;
ee26d5
+          char *cmd = xmalloc (buf_len);
ee26d5
+          snprintf (cmd, buf_len, "%s \"%s\" > \"%s\"", compr->command, real_name, tmp_name);
ee26d5
 #else
ee26d5
-	  char *cmd1 = concat (compr->command, " '", real_name);
ee26d5
-	  char *cmd = concat (cmd1, "' > ", tmp_name);
ee26d5
+          char *new_real_name = escape_shell_arg_string (real_name);
ee26d5
+          char *new_tmp_name = escape_shell_arg_string (tmp_name);
ee26d5
+          int buf_len = strlen (compr->command) + strlen ("  > ") + strlen (new_real_name) + strlen (new_tmp_name) + 1;
ee26d5
+          char *cmd = xmalloc (buf_len);
ee26d5
+          snprintf (cmd, buf_len, "%s %s > %s", compr->command, new_real_name, new_tmp_name);
ee26d5
 #endif
ee26d5
-	  free (cmd1);
ee26d5
 	  int tmp_errno;
ee26d5
 	  if (system (cmd) == -1)
ee26d5
 	    {
ee26d5
@@ -7124,6 +7128,55 @@ etags_mktmp (void)
ee26d5
   return templt;
ee26d5
 }
ee26d5
 
ee26d5
+/*
ee26d5
+ * Adds single quotes around a string, if found single quotes, escaped it.
ee26d5
+ * Return a newly-allocated string.
ee26d5
+ *
ee26d5
+ * For example:
ee26d5
+ * escape_shell_arg_string("test.txt") => 'test.txt'
ee26d5
+ * escape_shell_arg_string("'test.txt") => ''\''test.txt'
ee26d5
+ */
ee26d5
+static char *
ee26d5
+escape_shell_arg_string (char *str)
ee26d5
+{
ee26d5
+  char *p = str;
ee26d5
+  int need_space = 2;           /* ' at begin and end */
ee26d5
+
ee26d5
+  while (*p != '\0')
ee26d5
+    {
ee26d5
+      if (*p == '\'')
ee26d5
+        need_space += 4;        /* ' to '\'', length is 4 */
ee26d5
+      else
ee26d5
+        need_space++;
ee26d5
+
ee26d5
+      p++;
ee26d5
+    }
ee26d5
+
ee26d5
+  char *new_str = xnew (need_space + 1, char);
ee26d5
+  new_str[0] = '\'';
ee26d5
+  new_str[need_space-1] = '\'';
ee26d5
+
ee26d5
+  int i = 1;                    /* skip first byte */
ee26d5
+  p = str;
ee26d5
+  while (*p != '\0')
ee26d5
+    {
ee26d5
+      new_str[i] = *p;
ee26d5
+      if (*p == '\'')
ee26d5
+        {
ee26d5
+          new_str[i+1] = '\\';
ee26d5
+          new_str[i+2] = '\'';
ee26d5
+          new_str[i+3] = '\'';
ee26d5
+          i += 3;
ee26d5
+        }
ee26d5
+
ee26d5
+      i++;
ee26d5
+      p++;
ee26d5
+    }
ee26d5
+
ee26d5
+  new_str[need_space] = '\0';
ee26d5
+  return new_str;
ee26d5
+}
ee26d5
+
ee26d5
 static void
ee26d5
 do_move_file(const char *src_file, const char *dst_file)
ee26d5
 {
ee26d5
-- 
ee26d5
2.36.1
ee26d5