e_util: replaced getenv by e_util_env_get to avoid copying tainted string 99/163099/3
authorGwanglim Lee <gl77.lee@samsung.com>
Thu, 7 Dec 2017 07:29:32 +0000 (16:29 +0900)
committerSung-Jin Park <sj76.park@samsung.com>
Thu, 7 Dec 2017 11:16:40 +0000 (11:16 +0000)
Change-Id: I54b8cd66378594580c44502dda7c8ad50b210342

src/bin/e_comp_wl_input.c
src/bin/e_comp_wl_rsm.c
src/bin/e_config.c
src/bin/e_info_server.c
src/bin/e_input_device.c
src/bin/e_main.c
src/bin/e_module.c
src/bin/e_object.c
src/bin/e_user.c
src/bin/e_user.h

index fce5eba..0d9bed5 100644 (file)
@@ -7,6 +7,13 @@ static Eina_Bool dont_set_e_input_keymap = EINA_FALSE;
 static Eina_Bool dont_use_xkb_cache = EINA_FALSE;
 static Eina_Bool use_cache_keymap = EINA_FALSE;
 
+/* default XKB values from enviroment variables */
+static char *_env_e_default_xkb_rules   = NULL;
+static char *_env_e_default_xkb_model   = NULL;
+static char *_env_e_default_xkb_layout  = NULL;
+static char *_env_e_default_xkb_variant = NULL;
+static char *_env_e_default_xkb_opts    = NULL;
+
 static void
 _e_comp_wl_input_update_seat_caps(struct wl_client *wc)
 {
@@ -369,22 +376,25 @@ static int
 _e_comp_wl_input_keymap_fd_get(off_t size)
 {
    int fd = 0, blen = 0, len = 0;
-   const char *path;
+   char *path;
    char tmp[PATH_MAX] = {0, };
    long flags;
 
    blen = sizeof(tmp) - 20;
 
-   if (!(path = getenv("XDG_RUNTIME_DIR"))) return -1;
+   path = e_util_env_get("XDG_RUNTIME_DIR");
+   if (!path) return -1;
 
    len = strlen(path);
    if (len < blen)
      {
         strncpy(tmp, path, len + 1);
         strncat(tmp, "/e-wl-keymap-XXXXXX", 19);
+        E_FREE(path);
      }
    else
      {
+        E_FREE(path);
         return -1;
      }
 
@@ -550,6 +560,13 @@ e_comp_wl_input_init(void)
 
    E_EVENT_TEXT_INPUT_PANEL_VISIBILITY_CHANGE = ecore_event_type_new();
 
+   /* get string values from environment variables */
+   _env_e_default_xkb_rules   = e_util_env_get("E_DEFAULT_XKB_RULES"  );
+   _env_e_default_xkb_model   = e_util_env_get("E_DEFAULT_XKB_MODEL"  );
+   _env_e_default_xkb_layout  = e_util_env_get("E_DEFAULT_XKB_LAYOUT" );
+   _env_e_default_xkb_variant = e_util_env_get("E_DEFAULT_XKB_VARIANT");
+   _env_e_default_xkb_opts    = e_util_env_get("E_DEFAULT_XKB_OPTIONS");
+
    return EINA_TRUE;
 }
 
@@ -558,6 +575,13 @@ e_comp_wl_input_shutdown(void)
 {
    struct wl_resource *res;
 
+   /* free environment variable string */
+   E_FREE(_env_e_default_xkb_rules  );
+   E_FREE(_env_e_default_xkb_model  );
+   E_FREE(_env_e_default_xkb_layout );
+   E_FREE(_env_e_default_xkb_variant);
+   E_FREE(_env_e_default_xkb_opts   );
+
    /* destroy pointer resources */
    EINA_LIST_FREE(e_comp_wl->ptr.resources, res)
      wl_resource_destroy(res);
@@ -880,71 +904,61 @@ e_comp_wl_input_keymap_set(const char *rules, const char *model, const char *lay
 E_API const char*
 e_comp_wl_input_keymap_default_rules_get(void)
 {
-   const char *rules;
    if (e_config->xkb.default_rmlvo.rules)
      return e_config->xkb.default_rmlvo.rules;
-   else
-     {
-        rules = getenv("E_DEFAULT_XKB_RULES");
-        if (rules) return rules;
-        else return "evdev";
-     }
+
+   if (_env_e_default_xkb_rules)
+     return _env_e_default_xkb_rules;
+
+   return "evdev";
 }
 
 E_API const char*
 e_comp_wl_input_keymap_default_model_get(void)
 {
-   const char *model;
    if (e_config->xkb.default_rmlvo.model)
      return e_config->xkb.default_rmlvo.model;
-   else
-     {
-        model = getenv("E_DEFAULT_XKB_MODEL");
-        if (model) return model;
-        else return "pc105";
-     }
+
+   if (_env_e_default_xkb_model)
+     return _env_e_default_xkb_model;
+
+   return "pc105";
 }
 
 E_API const char*
 e_comp_wl_input_keymap_default_layout_get(void)
 {
-   const char *layout;
    if (e_config->xkb.default_rmlvo.layout)
      return e_config->xkb.default_rmlvo.layout;
-   else
-     {
-        layout = getenv("E_DEFAULT_XKB_LAYOUT");
-        if (layout) return layout;
-        else return "us";
-     }
+
+   if (_env_e_default_xkb_layout)
+     return _env_e_default_xkb_layout;
+
+   return "us";
 }
 
 E_API const char*
 e_comp_wl_input_keymap_default_variant_get(void)
 {
-   const char *variant;
    if (e_config->xkb.default_rmlvo.variant)
      return e_config->xkb.default_rmlvo.variant;
-   else
-     {
-        variant = getenv("E_DEFAULT_XKB_VARIANT");
-        if (variant) return variant;
-        else return NULL;
-     }
+
+   if (_env_e_default_xkb_variant)
+     return _env_e_default_xkb_variant;
+
+   return NULL;
 }
 
 E_API const char*
 e_comp_wl_input_keymap_default_options_get(void)
 {
-   const char *options;
    if (e_config->xkb.default_rmlvo.options)
      return e_config->xkb.default_rmlvo.options;
-   else
-     {
-        options = getenv("E_DEFAULT_XKB_OPTIONS");
-        if (options) return options;
-        else return NULL;
-     }
+
+   if (_env_e_default_xkb_opts)
+     return _env_e_default_xkb_opts;
+
+   return NULL;
 }
 
 E_API void
index cda84d8..08a3bb0 100644 (file)
@@ -1221,7 +1221,8 @@ _remote_source_save(void *data, Ecore_Thread *th)
    E_Client *ec;
    char name[1024];
    char dest_dir[1024];
-   const char *dest_path, *run_dir, *dupname, *dupdir;
+   const char *dest_path, *dupname, *dupdir;
+   char *run_dir;
 
    if (!(td = data)) return;
 
@@ -1229,10 +1230,12 @@ _remote_source_save(void *data, Ecore_Thread *th)
    if (!ec) return;
    if (ecore_thread_check(th)) return;
 
-   if (!(run_dir = getenv("XDG_RUNTIME_DIR")))
-     return;
+   run_dir = e_util_env_get("XDG_RUNTIME_DIR");
+   if (!run_dir) return;
 
    snprintf(dest_dir, sizeof(dest_dir), "%s/.e-img", run_dir);
+   E_FREE(run_dir);
+
    if (!ecore_file_exists(dest_dir))
      ecore_file_mkdir(dest_dir);
    dupdir = strdup(dest_dir);
@@ -2933,22 +2936,26 @@ static int
 _e_comp_wl_remote_surface_dummy_fd_get(void)
 {
    int fd = 0, blen = 0, len = 0;
-   const char *path;
+   char *path;
    char tmp[PATH_MAX];
 
    blen = sizeof(tmp) - 1;
 
-   if (!(path = getenv("XDG_RUNTIME_DIR")))
-     return -1;
+   path = e_util_env_get("XDG_RUNTIME_DIR");
+   if (!path) return -1;
 
    len = strlen(path);
    if (len < blen)
      {
         strncpy(tmp, path, len + 1);
         strncat(tmp, "/enlightenment_rsm_dummy_fdXXXXXX", 34);
+        E_FREE(path);
      }
    else
-     return -1;
+     {
+        E_FREE(path);
+        return -1;
+     }
 
    if ((fd = mkstemp(tmp)) < 0)
      return -1;
index 2790a3d..9c13946 100644 (file)
@@ -319,7 +319,12 @@ e_config_init(void)
    if (getenv("E_CONF_RO")) e_config_save_block_set(1);
 
    /* if environment var set - use this profile name */
-   _e_config_profile = eina_stringshare_add(getenv("E_CONF_PROFILE"));
+   char *s = e_util_env_get("E_CONF_PROFILE");
+   if (s)
+     {
+        _e_config_profile = eina_stringshare_add(s);
+        E_FREE(s);
+     }
 
    _e_config_edd_init(EINA_FALSE);
 
index 183f1cb..6d68b49 100644 (file)
@@ -5230,6 +5230,8 @@ e_info_server_protocol_trace_path_init(char *trace_path)
 static Eina_Bool
 _e_info_server_dbus_init(void *data EINA_UNUSED)
 {
+   char *s = NULL;
+
    if (e_info_server.conn) return ECORE_CALLBACK_CANCEL;
 
    if (!e_info_server.conn)
@@ -5249,8 +5251,15 @@ _e_info_server_dbus_init(void *data EINA_UNUSED)
    E_EVENT_INFO_ROTATION_MESSAGE = ecore_event_type_new();
 
    e_info_protocol_init();
-   e_info_server_protocol_rule_path_init(getenv("E_INFO_RULE_FILE"));
-   e_info_server_protocol_trace_path_init(getenv("E_INFO_TRACE_FILE"));
+
+   s = e_util_env_get("E_INFO_RULE_FILE");
+   e_info_server_protocol_rule_path_init(s);
+   E_FREE(s);
+
+   s = e_util_env_get("E_INFO_TRACE_FILE");
+   e_info_server_protocol_trace_path_init(s);
+   E_FREE(s);
+
    e_main_hook_call(E_MAIN_HOOK_E_INFO_READY);
 
    return ECORE_CALLBACK_CANCEL;
index 210a12d..dc95f1d 100644 (file)
@@ -722,10 +722,17 @@ e_input_device_input_create_libinput_udev(E_Input_Device *dev)
      }
 
    /* set libinput log priority */
-   if ((env = getenv(E_INPUT_ENV_LIBINPUT_LOG_DISABLE)) && (atoi(env) == 1))
+   env = e_util_env_get(E_INPUT_ENV_LIBINPUT_LOG_DISABLE);
+   if ((env) && (atoi(env) == 1))
      libinput_log_set_handler(input->libinput, NULL);
-   else if ((env = getenv(E_INPUT_ENV_LIBINPUT_LOG_EINA_LOG)) && (atoi(env) == 1))
-     libinput_log_set_handler(input->libinput, e_input_device_libinput_log_handler);
+   else
+     {
+        env = e_util_env_get(E_INPUT_ENV_LIBINPUT_LOG_EINA_LOG);
+        if ((env) && (atoi(env) == 1))
+          libinput_log_set_handler(input->libinput,
+                                   e_input_device_libinput_log_handler);
+     }
+   E_FREE(env);
 
    libinput_log_set_priority(input->libinput, LIBINPUT_LOG_PRIORITY_INFO);
 
@@ -768,13 +775,17 @@ e_input_device_input_create_libinput_path(E_Input_Device *dev)
    struct libinput_device *device;
    int devices_num = 0;
    char *env;
-   Eina_Stringshare *path;
 
    /* check for valid device */
    EINA_SAFETY_ON_NULL_RETURN_VAL(dev, EINA_FALSE);
 
-   if ((env = getenv("PATH_DEVICES_NUM")))
-     devices_num = atoi(env);
+   env = e_util_env_get("PATH_DEVICES_NUM");
+   if (env)
+     {
+        devices_num = atoi(env);
+        E_FREE(env);
+     }
+
    if (devices_num <= 0 || devices_num >= INT_MAX)
      {
         return EINA_TRUE;
@@ -801,10 +812,17 @@ e_input_device_input_create_libinput_path(E_Input_Device *dev)
      }
 
    /* set libinput log priority */
-   if ((env = getenv(E_INPUT_ENV_LIBINPUT_LOG_DISABLE)) && (atoi(env) == 1))
+   env = e_util_env_get(E_INPUT_ENV_LIBINPUT_LOG_DISABLE);
+   if ((env) && (atoi(env) == 1))
      libinput_log_set_handler(input->libinput, NULL);
-   else if ((env = getenv(E_INPUT_ENV_LIBINPUT_LOG_EINA_LOG)) && (atoi(env) == 1))
-     libinput_log_set_handler(input->libinput, e_input_device_libinput_log_handler);
+   else
+     {
+        env = e_util_env_get(E_INPUT_ENV_LIBINPUT_LOG_EINA_LOG);
+        if ((env) && (atoi(env) == 1))
+          libinput_log_set_handler(input->libinput,
+                                   e_input_device_libinput_log_handler);
+     }
+   E_FREE(env);
 
    libinput_log_set_priority(input->libinput, LIBINPUT_LOG_PRIORITY_INFO);
 
@@ -812,15 +830,15 @@ e_input_device_input_create_libinput_path(E_Input_Device *dev)
      {
         char buf[1024] = "PATH_DEVICE_";
         eina_convert_itoa(i + 1, buf + 12);
-        env = getenv(buf);
+        env = e_util_env_get(buf);
         if (env)
           {
-             path = eina_stringshare_add(env);
-             device = libinput_path_add_device(input->libinput, path);
+             device = libinput_path_add_device(input->libinput, env);
              if (!device)
-               ERR("Failed to initialized device %s", path);
+               ERR("Failed to initialized device %s", env);
              else
-               INF("libinput_path created input device %s", path);
+               INF("libinput_path created input device %s", env);
+             E_FREE(env);
           }
      }
 
index 3c9d6b4..56f91e0 100644 (file)
@@ -158,13 +158,13 @@ _xdg_check_str(const char *env, const char *str)
 static void
 _xdg_data_dirs_augment(void)
 {
-   const char *s;
+   char *s;
    const char *p = e_prefix_get();
    char newpath[4096], buf[4096];
 
    if (!p) return;
 
-   s = getenv("XDG_DATA_DIRS");
+   s = e_util_env_get("XDG_DATA_DIRS");
    if (s)
      {
         Eina_Bool pfxdata, pfx;
@@ -182,6 +182,7 @@ _xdg_data_dirs_augment(void)
                s);
              e_util_env_set("XDG_DATA_DIRS", buf);
           }
+        E_FREE(s);
      }
    else
      {
@@ -189,7 +190,7 @@ _xdg_data_dirs_augment(void)
         e_util_env_set("XDG_DATA_DIRS", buf);
      }
 
-   s = getenv("XDG_CONFIG_DIRS");
+   s = e_util_env_get("XDG_CONFIG_DIRS");
    snprintf(newpath, sizeof(newpath), "%s/etc/xdg", p);
    if (s)
      {
@@ -198,6 +199,7 @@ _xdg_data_dirs_augment(void)
              snprintf(buf, sizeof(buf), "%s:%s", newpath, s);
              e_util_env_set("XDG_CONFIG_DIRS", buf);
           }
+        E_FREE(s);
      }
    else
      {
@@ -205,7 +207,10 @@ _xdg_data_dirs_augment(void)
         e_util_env_set("XDG_CONFIG_DIRS", buf);
      }
 
-   if (!getenv("XDG_RUNTIME_DIR"))
+   s = e_util_env_get("XDG_RUNTIME_DIR");
+   if (s)
+     E_FREE(s);
+   else
      {
         const char *dir;
 
@@ -221,10 +226,11 @@ _xdg_data_dirs_augment(void)
      }
 
    /* set menu prefix so we get our e menu */
-   if (!getenv("XDG_MENU_PREFIX"))
-     {
-        e_util_env_set("XDG_MENU_PREFIX", "e-");
-     }
+   s = e_util_env_get("XDG_MENU_PREFIX");
+   if (s)
+     E_FREE(s);
+   else
+     e_util_env_set("XDG_MENU_PREFIX", "e-");
 }
 
 static Eina_Bool
@@ -331,7 +337,7 @@ main(int argc, char **argv)
 {
    Eina_Bool safe_mode = EINA_FALSE;
    double t = 0.0, tstart = 0.0;
-   char *s = NULL, buff[32];
+   char *s = NULL, *s1 = NULL, buff[32];
    struct sigaction action;
 
 #ifdef __linux__
@@ -357,7 +363,10 @@ main(int argc, char **argv)
    /* Wayland shm sets up a sigbus handler for catching invalid shm region */
    /* access. If we setup our sigbus handler here, then the wl-shm sigbus */
    /* handler will not function properly */
-   if (!getenv("NOTIFY_SOCKET"))
+   s = e_util_env_get("NOTIFY_SOCKET");
+   if (s)
+     E_FREE(s);
+   else
      {
         TSB("Signal Trap");
         action.sa_sigaction = e_sigseg_act;
@@ -383,18 +392,37 @@ main(int argc, char **argv)
      }
 
    t = ecore_time_unix_get();
-   s = getenv("E_START_TIME");
-   if ((s) && (!getenv("E_RESTART_OK")))
+   s = e_util_env_get("E_START_TIME");
+   if (s)
      {
-        tstart = atof(s);
-        if ((t - tstart) < 5.0) safe_mode = EINA_TRUE;
+        s1 = e_util_env_get("E_RESTART_OK");
+        if (s1)
+          E_FREE(s1); /* do nothing... */
+        else
+          {
+             /* enable safe mode
+              * 1. E_RESTART_OK is not defined.
+              * 2. the diff time between this and previous start is less than 5 seconds.
+              */
+             tstart = atof(s);
+             if ((t - tstart) < 5.0) safe_mode = EINA_TRUE;
+          }
+        E_FREE(s);
      }
+
+   /* save start time to environment variable to calculate
+    * diff time at the next restart
+    */
    tstart = t;
    snprintf(buff, sizeof(buff), "%1.1f", tstart);
    e_util_env_set("E_START_TIME", buff);
 
-   if (getenv("E_START_MTRACK"))
-     e_util_env_set("MTRACK", NULL);
+   s = e_util_env_get("E_START_MTRACK");
+   if (s)
+     {
+        e_util_env_set("MTRACK", NULL);
+        E_FREE(s);
+     }
    TSB("Eina Init");
    if (!eina_init())
      {
@@ -535,6 +563,15 @@ main(int argc, char **argv)
 
    /*** Initialize E Subsystems We Need ***/
 
+   TSB("E User Init");
+   if (!e_user_init())
+     {
+        e_error_message_show(_("Enlightenment cannot set up user home path\n"));
+        goto failed;
+     }
+   TSE("E User Init Done");
+   _e_main_shutdown_push(e_user_shutdown);
+
    TSB("E Directories Init");
    /* setup directories we will be using for configurations storage etc. */
    if (!_e_main_dirs_init())
@@ -779,8 +816,12 @@ main(int argc, char **argv)
    if (restart)
      {
         e_util_env_set("E_RESTART_OK", "1");
-        if (getenv("E_START_MTRACK"))
-          e_util_env_set("MTRACK", "track");
+        s = e_util_env_get("E_START_MTRACK");
+        if (s)
+          {
+             e_util_env_set("MTRACK", "track");
+             E_FREE(s);
+          }
         ecore_app_restart();
      }
 
@@ -825,7 +866,7 @@ _e_main_shutdown(int errcode)
 {
    int i = 0;
    char buf[PATH_MAX];
-   const char *dir;
+   char *dir;
 
    printf("E: Begin Shutdown Procedure!\n");
 
@@ -836,13 +877,14 @@ _e_main_shutdown(int errcode)
    if (_idle_after) ecore_idle_enterer_del(_idle_after);
    _idle_after = NULL;
 
-   dir = getenv("XDG_RUNTIME_DIR");
+   dir = e_util_env_get("XDG_RUNTIME_DIR");
    if (dir)
      {
         char buf_env[PATH_MAX];
         snprintf(buf_env, sizeof(buf_env), "%s", dir);
         snprintf(buf, sizeof(buf), "%s/.e-deleteme", buf_env);
         if (ecore_file_exists(buf)) ecore_file_recursive_rm(buf_env);
+        E_FREE(dir);
      }
    for (i = (_e_main_lvl - 1); i >= 0; i--)
      (*_e_main_shutdown_func[i])();
index 8c67ffb..3456550 100644 (file)
@@ -152,12 +152,13 @@ e_module_init(void)
 
    if (!mod_src_path)
      {
-        const char *src_path = getenv("E_MODULE_SRC_PATH");
+        char *src_path = e_util_env_get("E_MODULE_SRC_PATH");
         if (src_path)
           {
              char buf_p[PATH_MAX];
              snprintf(buf_p, sizeof(buf_p), "%s", src_path);
              mod_src_path = eina_stringshare_add((const char*)buf_p);
+             E_FREE(src_path);
           }
      }
 
@@ -342,12 +343,13 @@ e_module_new(const char *name)
 
         if (!mod_src_path)
           {
-             const char *src_path = getenv("E_MODULE_SRC_PATH");
+             char *src_path = e_util_env_get("E_MODULE_SRC_PATH");
              if (src_path)
                {
                   char buf_p[PATH_MAX];
                   snprintf(buf_p, sizeof(buf_p), "%s", src_path);
                   mod_src_path = eina_stringshare_add((const char*)buf_p);
+                  E_FREE(src_path);
                }
           }
         if (mod_src_path)
index b0be256..4d36692 100644 (file)
@@ -469,12 +469,15 @@ e_object_hash_init(void)
 
    e_obj_hash_check = EINA_FALSE;
 
-   val = getenv("E_OBJECT_HASH_CHECK");
-   if ((val) && (!strcmp(val, "1")))
+   val = e_util_env_get("E_OBJECT_HASH_CHECK");
+   if (!val) return;
+   if (!strcmp(val, "1"))
      {
         e_obj_hash_check = EINA_TRUE;
         e_obj_hash = eina_hash_pointer_new(NULL);
      }
+
+   E_FREE(val);
 }
 
 E_API void
index 2c4582d..c8c81ac 100644 (file)
@@ -1,23 +1,21 @@
 #include "e.h"
 
-static const char *_e_user_homedir = NULL;
+static char *_e_user_homedir = NULL;
 static size_t _e_user_homedir_len = 0;
 
 /* externally accessible functions */
-E_API const char *
-e_user_homedir_get(void)
+EINTERN int
+e_user_init(void)
 {
    char *d;
 
-   if (_e_user_homedir)
-     return _e_user_homedir;
-
-   _e_user_homedir = d = getenv("HOME");
+   /* e_user_shutdown will free for the d string */
+   _e_user_homedir = d = e_util_env_get("HOME");
    if (!_e_user_homedir)
      {
         _e_user_homedir = "/tmp";
         _e_user_homedir_len = sizeof("/tmp") - 1;
-        return _e_user_homedir;
+        return 1;
      }
 
    _e_user_homedir_len = strlen(_e_user_homedir);
@@ -27,6 +25,21 @@ e_user_homedir_get(void)
         _e_user_homedir_len--;
         d[_e_user_homedir_len] = '\0';
      }
+
+   return 1;
+}
+
+EINTERN int
+e_user_shutdown(void)
+{
+   E_FREE(_e_user_homedir);
+   _e_user_homedir_len = 0;
+   return 1;
+}
+
+E_API const char *
+e_user_homedir_get(void)
+{
    return _e_user_homedir;
 }
 
@@ -101,20 +114,22 @@ e_user_dir_get(void)
 
    if (!dir[0])
      {
-        char *d;
-        
-        if ((d = getenv("E_HOME")))
+        char *d = e_util_env_get("E_HOME");
+        if (d)
           {
              snprintf(dir, sizeof(dir), "%s/e", d);
              _e_user_dir_len = strlen(dir);
+             E_FREE(d);
           }
         else
           {
 #ifdef DOXDG             
-             if ((d = getenv("XDG_CONFIG_HOME")))
+             d = e_util_env_get("XDG_CONFIG_HOME");
+             if (d)
                {
                   snprintf(dir, sizeof(dir), "%s/e", d);
                   _e_user_dir_len = strlen(dir);
+                  E_FREE(d);
                }
              else
 #endif               
index 2f99c12..f5ea686 100644 (file)
@@ -3,6 +3,9 @@
 #ifndef E_USER_H
 #define E_USER_H
 
+EINTERN int       e_user_init(void);
+EINTERN int       e_user_shutdown(void);
+
 E_API const char *e_user_homedir_get(void);
 E_API size_t      e_user_homedir_concat_len(char *dst, size_t size, const char *path, size_t path_len);
 E_API size_t      e_user_homedir_concat(char *dst, size_t size, const char *path);