Fix fd leak
[platform/core/appfw/pkgmgr-server.git] / src / pkgmgr-server.c
index d99a76e..0ac356f 100644 (file)
@@ -437,7 +437,7 @@ static int __check_csr(const char *path)
 {
        int ret;
        void *context;
-       void *malware;
+       void *malware = NULL;
        void *lib_handle;
        int (*_csr_cs_context_create)(void **handle);
        int (*_csr_cs_scan_file)(void *handle, const char *file_path, void **malware);
@@ -456,41 +456,34 @@ static int __check_csr(const char *path)
        if (!_csr_cs_context_create || !_csr_cs_scan_file ||
                        !_csr_cs_context_destroy) {
                ERR("Failed to load CSR symbols");
-               ret = -1;
-               goto catch;
+               dlclose(lib_handle);
+               return -1;
        }
 
        ret = _csr_cs_context_create(&context);
        if (ret != 0) {
                ERR("Failed to create context");
-               ret = -1;
-               goto catch;
+               dlclose(lib_handle);
+               return -1;
        }
 
        ret = _csr_cs_scan_file(context, path, &malware);
-       if (ret != 0) {
+       /* the csr engine may not exist */
+       if (ret != 0)
                DBG("CSR result[%d]", ret);
-               ret = 0;
-               goto catch;
-       }
 
        ret = _csr_cs_context_destroy(context);
-       if (ret != 0) {
+       if (ret != 0)
                ERR("Failed to destroy context");
-               ret = -1;
-               goto catch;
-       }
+
+       dlclose(lib_handle);
 
        if (malware != NULL) {
-               ERR("CSR denied[%d] installation", path);
-               ret = -1;
+               ERR("CSR detected malware from [%s]", path);
+               return -1;
+       } else {
+               return 0;
        }
-
-catch:
-       if (lib_handle)
-               dlclose(lib_handle);
-
-       return ret;
 }
 
 static int __kill_app(char *appid, uid_t uid)
@@ -510,7 +503,7 @@ static int __kill_app(char *appid, uid_t uid)
 
        ret = aul_terminate_pid_for_uid(pid, uid);
        if (ret != AUL_R_OK) {
-               ERR("failed to terminate app(%d)", appid);
+               ERR("failed to terminate app(%s)", appid);
                return -1;
        }
 
@@ -1365,6 +1358,7 @@ static int __process_getsize_sync(struct backend_job *job)
                goto error;
        }
 
+       job->extra_data = extra_getsize_info;
        extra_getsize_info->getsize_fifo = strdup(fifo_path);
        if (!extra_getsize_info->getsize_fifo) {
                ERR("out of memory");
@@ -1375,7 +1369,6 @@ static int __process_getsize_sync(struct backend_job *job)
                ERR("failed to mkfifo");
                goto error;
        }
-       job->extra_data = extra_getsize_info;
 
        snprintf(args, sizeof(args), "%s %s %s %d -k %s -u %d --sync",
                        backend_cmd, job->pkgid, job->args, job->caller_uid,
@@ -1775,7 +1768,32 @@ static int __process_set_app_label(struct backend_job *job)
 static int __process_set_app_icon(struct backend_job *job)
 {
        int ret;
+       pkgmgrinfo_appinfo_h handle = NULL;
+       char *app_root_path = NULL;
 
+       ret = pkgmgrinfo_appinfo_get_usr_appinfo(job->pkgid, job->target_uid, &handle);
+       if (ret != PMINFO_R_OK) {
+               _return_value_to_caller(job->req_id, g_variant_new("(i)", ret));
+               return PKGMGR_R_ENOPKG;
+       }
+
+       ret = pkgmgrinfo_appinfo_get_root_path(handle, &app_root_path);
+       if (ret != PMINFO_R_OK || !app_root_path) {
+               _return_value_to_caller(job->req_id, g_variant_new("(i)", ret));
+               pkgmgrinfo_appinfo_destroy_appinfo(handle);
+               return PKGMGR_R_ESYSTEM;
+       }
+
+       if (strncasecmp(job->args, app_root_path, strlen(app_root_path)) != 0 ||
+                       strstr(job->args, "..") != NULL ||
+                       access(job->args, F_OK) != 0) {
+               ERR("invalid path[%s]", job->args);
+               _return_value_to_caller(job->req_id, g_variant_new("(i)", ret));
+               pkgmgrinfo_appinfo_destroy_appinfo(handle);
+               return PKGMGR_R_EINVAL;
+       }
+
+       pkgmgrinfo_appinfo_destroy_appinfo(handle);
        ret = pkgmgr_parser_update_app_icon_info_in_usr_db(job->pkgid,
                        job->target_uid, job->args);
        _return_value_to_caller(job->req_id, g_variant_new("(i)", ret));