Fix memory issue 80/201980/1 accepted/tizen/unified/20190325.070918 submit/tizen/20190322.082036
authorYunjin Lee <yunjin-.lee@samsung.com>
Thu, 21 Mar 2019 10:22:26 +0000 (19:22 +0900)
committerYunjin Lee <yunjin-.lee@samsung.com>
Thu, 21 Mar 2019 10:25:41 +0000 (19:25 +0900)
- The function setlocale() returns a pointer to a string that represents
the current locale setting and the string can be overwritten by
subsequent calls to setlocale(). Hence to store original locale
properly, modify it to copy the string and free after use. Otherwise, it
can cause heap buffer overflow or heap use after free issue.

Change-Id: I8267904aa83e602da8e5567a97b44f17d7a892cb
Signed-off-by: Yunjin Lee <yunjin-.lee@samsung.com>
src/privilege_information.c

index 17ef740..995c29c 100755 (executable)
@@ -349,12 +349,16 @@ int privilege_info_get_privilege_info_list(const char* locale, GList* privilege_
                LOGE("[PRVINFO_ERROR_INVALID_PARAMETER] privilege_list is NULL");
                return PRVINFO_ERROR_INVALID_PARAMETER;
        }
-
-       char *orig_locale = (char *)setlocale(LC_ALL, NULL);
-       if (!orig_locale) {
+       char *orig_locale = NULL;
+       char *result = (char *)setlocale(LC_ALL, NULL);
+       if (!result) {
                LOGE("failed to get original locale. orig_locale = %s", orig_locale);
+       } else {
+               orig_locale = strdup(result);
+               TryReturn(orig_locale != NULL, , PRVINFO_ERROR_OUT_OF_MEMORY, "[PRVINFO_ERROR_OUT_OF_MEMORY] strdup of orig_locale failed");
+               LOGD("orig_locale = %s", orig_locale);
        }
-       char *result = (char *)setlocale(LC_ALL, locale);
+       result = (char *)setlocale(LC_ALL, locale);
        if (result) {
                LOGI("succeeded in setting locale = %s", result);
        } else {
@@ -473,8 +477,11 @@ FINISH:
                free(privilege_description);
                privilege_description = NULL;
        }
-
-       setlocale(LC_ALL, orig_locale);
+       if (orig_locale != NULL) {
+               setlocale(LC_ALL, orig_locale);
+               free(orig_locale);
+               orig_locale = NULL;
+       }
 
        return ret;
 }