Blame fish-upstream-CVE-2014-2905-part2.patch

Andy Lutomirski 4de0b6
From b5cd21c337a8990c0c343ab2c22d3dc123a03d25 Mon Sep 17 00:00:00 2001
Andy Lutomirski 4de0b6
Message-Id: <b5cd21c337a8990c0c343ab2c22d3dc123a03d25.1407803973.git.luto@amacapital.net>
Andy Lutomirski 4de0b6
From: David Adam <zanchey@ucc.gu.uwa.edu.au>
Andy Lutomirski 4de0b6
Date: Mon, 4 Aug 2014 13:26:14 +0800
Andy Lutomirski 4de0b6
Subject: [PATCH 1/3] Further fixes to universal variable server socket
Andy Lutomirski 4de0b6
 management
Andy Lutomirski 4de0b6
Andy Lutomirski 4de0b6
- Change fishd_path to std::string
Andy Lutomirski 4de0b6
- Warn, rather than exiting with an error, if the universal variable
Andy Lutomirski 4de0b6
  server path is not available, and provide more useful advice.
Andy Lutomirski 4de0b6
- Export the new __fishd_runtime_dir variable.
Andy Lutomirski 4de0b6
---
Andy Lutomirski 4de0b6
 common.cpp        | 13 +++++++------
Andy Lutomirski 4de0b6
 common.h          |  2 +-
Andy Lutomirski 4de0b6
 env.cpp           |  4 ++--
Andy Lutomirski 4de0b6
 env_universal.cpp | 22 ++++++++++++++--------
Andy Lutomirski 4de0b6
 env_universal.h   |  2 +-
Andy Lutomirski 4de0b6
 fish_pager.cpp    |  4 ++--
Andy Lutomirski 4de0b6
 fishd.cpp         |  9 +++++++--
Andy Lutomirski 4de0b6
 7 files changed, 34 insertions(+), 22 deletions(-)
Andy Lutomirski 4de0b6
Andy Lutomirski 4de0b6
diff --git a/common.cpp b/common.cpp
Andy Lutomirski 4de0b6
index 3e5a2c8..203eda5 100644
Andy Lutomirski 4de0b6
--- a/common.cpp
Andy Lutomirski 4de0b6
+++ b/common.cpp
Andy Lutomirski 4de0b6
@@ -2381,10 +2381,11 @@ static int check_runtime_path(const char * path)
Andy Lutomirski 4de0b6
 }
Andy Lutomirski 4de0b6
 
Andy Lutomirski 4de0b6
 /** Return the path of an appropriate runtime data directory */
Andy Lutomirski 4de0b6
-const char* common_get_runtime_path(void)
Andy Lutomirski 4de0b6
+std::string common_get_runtime_path()
Andy Lutomirski 4de0b6
 {
Andy Lutomirski 4de0b6
     const char *dir = getenv("XDG_RUNTIME_DIR");
Andy Lutomirski 4de0b6
     const char *uname = getenv("USER");
Andy Lutomirski 4de0b6
+    std::string path;
Andy Lutomirski 4de0b6
 
Andy Lutomirski 4de0b6
     if (uname == NULL)
Andy Lutomirski 4de0b6
     {
Andy Lutomirski 4de0b6
@@ -2396,19 +2397,19 @@ const char* common_get_runtime_path(void)
Andy Lutomirski 4de0b6
     {
Andy Lutomirski 4de0b6
         // /tmp/fish.user
Andy Lutomirski 4de0b6
         dir = "/tmp/fish.";
Andy Lutomirski 4de0b6
-        std::string path;
Andy Lutomirski 4de0b6
         path.reserve(strlen(dir) + strlen(uname));
Andy Lutomirski 4de0b6
         path.append(dir);
Andy Lutomirski 4de0b6
         path.append(uname);
Andy Lutomirski 4de0b6
         if (check_runtime_path(path.c_str()) != 0)
Andy Lutomirski 4de0b6
         {
Andy Lutomirski 4de0b6
-            debug(0, L"Couldn't create secure runtime path: '%s'", path.c_str());
Andy Lutomirski 4de0b6
-            exit(EXIT_FAILURE);
Andy Lutomirski 4de0b6
+            debug(0, L"Runtime path not available. Try deleting the directory %s and restarting fish.", path.c_str());
Andy Lutomirski 4de0b6
+            path.clear();
Andy Lutomirski 4de0b6
         }
Andy Lutomirski 4de0b6
-        return strdup(path.c_str());
Andy Lutomirski 4de0b6
     }
Andy Lutomirski 4de0b6
     else
Andy Lutomirski 4de0b6
     {
Andy Lutomirski 4de0b6
-        return dir;
Andy Lutomirski 4de0b6
+        path.reserve(strlen(dir));
Andy Lutomirski 4de0b6
+        path.append(dir);
Andy Lutomirski 4de0b6
     }
Andy Lutomirski 4de0b6
+    return path;
Andy Lutomirski 4de0b6
 }
Andy Lutomirski 4de0b6
diff --git a/common.h b/common.h
Andy Lutomirski 4de0b6
index 4d18aca..b160245 100644
Andy Lutomirski 4de0b6
--- a/common.h
Andy Lutomirski 4de0b6
+++ b/common.h
Andy Lutomirski 4de0b6
@@ -814,6 +814,6 @@ extern "C" {
Andy Lutomirski 4de0b6
 }
Andy Lutomirski 4de0b6
 
Andy Lutomirski 4de0b6
 /** Return the path of an appropriate runtime data directory */
Andy Lutomirski 4de0b6
-const char* common_get_runtime_path(void);
Andy Lutomirski 4de0b6
+std::string common_get_runtime_path();
Andy Lutomirski 4de0b6
 
Andy Lutomirski 4de0b6
 #endif
Andy Lutomirski 4de0b6
diff --git a/env.cpp b/env.cpp
Andy Lutomirski 4de0b6
index 0bda417..703d619 100644
Andy Lutomirski 4de0b6
--- a/env.cpp
Andy Lutomirski 4de0b6
+++ b/env.cpp
Andy Lutomirski 4de0b6
@@ -620,8 +620,8 @@ void env_init(const struct config_paths_t *paths /* or NULL */)
Andy Lutomirski 4de0b6
 
Andy Lutomirski 4de0b6
     const env_var_t user_dir_wstr = env_get_string(L"USER");
Andy Lutomirski 4de0b6
 
Andy Lutomirski 4de0b6
-    const char * fishd_dir = common_get_runtime_path();
Andy Lutomirski 4de0b6
-    env_set(L"__fish_runtime_dir", str2wcstring(fishd_dir).c_str(), ENV_GLOBAL);
Andy Lutomirski 4de0b6
+    std::string fishd_dir = common_get_runtime_path();
Andy Lutomirski 4de0b6
+    env_set(L"__fish_runtime_dir", str2wcstring(fishd_dir).c_str(), ENV_GLOBAL | ENV_EXPORT);
Andy Lutomirski 4de0b6
 
Andy Lutomirski 4de0b6
     wchar_t * user_dir = user_dir_wstr.missing()?NULL:const_cast<wchar_t*>(user_dir_wstr.c_str());
Andy Lutomirski 4de0b6
 
Andy Lutomirski 4de0b6
diff --git a/env_universal.cpp b/env_universal.cpp
Andy Lutomirski 4de0b6
index 1a97443..78e3130 100644
Andy Lutomirski 4de0b6
--- a/env_universal.cpp
Andy Lutomirski 4de0b6
+++ b/env_universal.cpp
Andy Lutomirski 4de0b6
@@ -242,23 +242,29 @@ static void reconnect()
Andy Lutomirski 4de0b6
 }
Andy Lutomirski 4de0b6
 
Andy Lutomirski 4de0b6
 
Andy Lutomirski 4de0b6
-void env_universal_init(const char * p,
Andy Lutomirski 4de0b6
+void env_universal_init(std::string p,
Andy Lutomirski 4de0b6
                         wchar_t *u,
Andy Lutomirski 4de0b6
                         void (*sf)(),
Andy Lutomirski 4de0b6
                         void (*cb)(fish_message_type_t type, const wchar_t *name, const wchar_t *val))
Andy Lutomirski 4de0b6
 {
Andy Lutomirski 4de0b6
-    path=p;
Andy Lutomirski 4de0b6
+    path=p.c_str();
Andy Lutomirski 4de0b6
     user=u;
Andy Lutomirski 4de0b6
     start_fishd=sf;
Andy Lutomirski 4de0b6
     external_callback = cb;
Andy Lutomirski 4de0b6
 
Andy Lutomirski 4de0b6
-    env_universal_server.fd = get_socket();
Andy Lutomirski 4de0b6
-    env_universal_common_init(&callback);
Andy Lutomirski 4de0b6
-    env_universal_read_all();
Andy Lutomirski 4de0b6
-    s_env_univeral_inited = true;
Andy Lutomirski 4de0b6
-    if (env_universal_server.fd >= 0)
Andy Lutomirski 4de0b6
+    if (p == "") {
Andy Lutomirski 4de0b6
+        debug(1, L"Could not connect to universal variable server. You will not be able to share variable values between fish sessions.");
Andy Lutomirski 4de0b6
+    }
Andy Lutomirski 4de0b6
+    else
Andy Lutomirski 4de0b6
     {
Andy Lutomirski 4de0b6
-        env_universal_barrier();
Andy Lutomirski 4de0b6
+        env_universal_server.fd = get_socket();
Andy Lutomirski 4de0b6
+        env_universal_common_init(&callback);
Andy Lutomirski 4de0b6
+        env_universal_read_all();
Andy Lutomirski 4de0b6
+        s_env_univeral_inited = true;
Andy Lutomirski 4de0b6
+        if (env_universal_server.fd >= 0)
Andy Lutomirski 4de0b6
+        {
Andy Lutomirski 4de0b6
+            env_universal_barrier();
Andy Lutomirski 4de0b6
+        }
Andy Lutomirski 4de0b6
     }
Andy Lutomirski 4de0b6
 }
Andy Lutomirski 4de0b6
 
Andy Lutomirski 4de0b6
diff --git a/env_universal.h b/env_universal.h
Andy Lutomirski 4de0b6
index 9e6ab85..f14db29 100644
Andy Lutomirski 4de0b6
--- a/env_universal.h
Andy Lutomirski 4de0b6
+++ b/env_universal.h
Andy Lutomirski 4de0b6
@@ -17,7 +17,7 @@ extern connection_t env_universal_server;
Andy Lutomirski 4de0b6
 /**
Andy Lutomirski 4de0b6
    Initialize the envuni library
Andy Lutomirski 4de0b6
 */
Andy Lutomirski 4de0b6
-void env_universal_init(const char * p,
Andy Lutomirski 4de0b6
+void env_universal_init(std::string p,
Andy Lutomirski 4de0b6
                         wchar_t *u,
Andy Lutomirski 4de0b6
                         void (*sf)(),
Andy Lutomirski 4de0b6
                         void (*cb)(fish_message_type_t type, const wchar_t *name, const wchar_t *val));
Andy Lutomirski 4de0b6
diff --git a/fish_pager.cpp b/fish_pager.cpp
Andy Lutomirski 4de0b6
index 27bc80e..6d05774 100644
Andy Lutomirski 4de0b6
--- a/fish_pager.cpp
Andy Lutomirski 4de0b6
+++ b/fish_pager.cpp
Andy Lutomirski 4de0b6
@@ -1032,8 +1032,8 @@ static void init(int mangle_descriptors, int out)
Andy Lutomirski 4de0b6
         exit(1);
Andy Lutomirski 4de0b6
     }
Andy Lutomirski 4de0b6
 
Andy Lutomirski 4de0b6
-
Andy Lutomirski 4de0b6
-    env_universal_init("", 0, 0, 0);
Andy Lutomirski 4de0b6
+    std::string dir = common_get_runtime_path();
Andy Lutomirski 4de0b6
+    env_universal_init(dir, 0, 0, 0);
Andy Lutomirski 4de0b6
     input_common_init(&interrupt_handler);
Andy Lutomirski 4de0b6
     output_set_writer(&pager_buffered_writer);
Andy Lutomirski 4de0b6
 
Andy Lutomirski 4de0b6
diff --git a/fishd.cpp b/fishd.cpp
Andy Lutomirski 4de0b6
index dd43647..d725e43 100644
Andy Lutomirski 4de0b6
--- a/fishd.cpp
Andy Lutomirski 4de0b6
+++ b/fishd.cpp
Andy Lutomirski 4de0b6
@@ -159,10 +159,15 @@ static int quit=0;
Andy Lutomirski 4de0b6
 */
Andy Lutomirski 4de0b6
 static std::string get_socket_filename(void)
Andy Lutomirski 4de0b6
 {
Andy Lutomirski 4de0b6
-    const char *dir = common_get_runtime_path();
Andy Lutomirski 4de0b6
+    std::string dir = common_get_runtime_path();
Andy Lutomirski 4de0b6
+
Andy Lutomirski 4de0b6
+    if (dir == "") {
Andy Lutomirski 4de0b6
+        debug(0, L"Cannot access desired socket path.");
Andy Lutomirski 4de0b6
+        exit(EXIT_FAILURE);
Andy Lutomirski 4de0b6
+    }
Andy Lutomirski 4de0b6
 
Andy Lutomirski 4de0b6
     std::string name;
Andy Lutomirski 4de0b6
-    name.reserve(strlen(dir) + strlen(SOCK_FILENAME) + 1);
Andy Lutomirski 4de0b6
+    name.reserve(dir.length() + strlen(SOCK_FILENAME) + 1);
Andy Lutomirski 4de0b6
     name.append(dir);
Andy Lutomirski 4de0b6
     name.push_back('/');
Andy Lutomirski 4de0b6
     name.append(SOCK_FILENAME);
Andy Lutomirski 4de0b6
-- 
Andy Lutomirski 4de0b6
1.9.3
Andy Lutomirski 4de0b6