Use snprintf instead of sprintf to be safe 45/60845/1 accepted/tizen/common/20160303.110357 accepted/tizen/ivi/20160305.090455 accepted/tizen/mobile/20160305.090405 accepted/tizen/tv/20160305.090421 accepted/tizen/wearable/20160305.090436 submit/tizen/20160303.045232
authorKyungwook Tak <k.tak@samsung.com>
Wed, 2 Mar 2016 11:45:52 +0000 (20:45 +0900)
committerKyungwook Tak <k.tak@samsung.com>
Wed, 2 Mar 2016 11:45:52 +0000 (20:45 +0900)
Change-Id: I664d9f039b09b576c4ebe84c29d8a7c459bc1384
Signed-off-by: Kyungwook Tak <k.tak@samsung.com>
srcs/key_handler.c
srcs/key_handler.h
tests/wae_tests.c

index 7c986c0d63c82e4150981bcefcf156a3fdf19a6e..7b050d6a701ab5549191424d69d23c7192fb4895 100644 (file)
@@ -231,9 +231,16 @@ error:
 }
 
 
-int _get_preloaded_app_dek_file_path(const char* pPkgId, char *path)
+int _get_preloaded_app_dek_file_path(const char* pPkgId, size_t size, char *path)
 {
-    sprintf(path, "%s/%s_%s.adek", _get_dek_store_path(), APP_DEK_FILE_PFX, pPkgId);
+    int ret = -1;
+
+    ret = snprintf(path, size, "%s/%s_%s.adek",
+            _get_dek_store_path(), APP_DEK_FILE_PFX, pPkgId);
+
+    if (ret < 0)
+        return WAE_ERROR_INVALID_PARAMETER; /* buffer size too small */
+
     return WAE_ERROR_NONE;
 }
 
@@ -258,14 +265,14 @@ int _extract_pkg_id_from_file_name(const char* fileName, char* pkgId)
 int _read_encrypted_app_dek_from_file(const char* pPkgId, unsigned char** encrypted_app_dek, size_t *len)
 {
     char path[MAX_PATH_LEN] = {0,};
-    _get_preloaded_app_dek_file_path(pPkgId, path);
+    _get_preloaded_app_dek_file_path(pPkgId, sizeof(path), path);
     return _read_from_file(path, encrypted_app_dek, len);
 }
 
 int _write_encrypted_app_dek_to_file(const char* pPkgId, const unsigned char* encrypted_app_dek, size_t len)
 {
     char path[MAX_PATH_LEN] = {0,};
-    _get_preloaded_app_dek_file_path(pPkgId, path);
+    _get_preloaded_app_dek_file_path(pPkgId, sizeof(path), path);
     return _write_to_file( path, encrypted_app_dek, len);
 }
 
@@ -683,7 +690,13 @@ int load_preloaded_app_deks(int reload)
         // regular file && start with KEY_MANAGER_INITIAL_VALUE_FILE_PFX
         if(entry.d_type == DT_REG && strstr(entry.d_name, APP_DEK_FILE_PFX) != NULL) {
             memset(file_path_buff, 0, sizeof(file_path_buff));
-            sprintf(file_path_buff, "%s/%s", _get_dek_store_path(), entry.d_name);
+            ret = snprintf(file_path_buff, sizeof(file_path_buff), "%s/%s",
+                    _get_dek_store_path(), entry.d_name);
+            if(ret < 0) {
+                WAE_SLOGE("Failed to make file path by snprintf.");
+                ret = WAE_ERROR_INVALID_PARAMETER; /* buffer size too small */
+                goto error;
+            }
 
             ret = _extract_pkg_id_from_file_name(entry.d_name, pkgId);
             if(ret != WAE_ERROR_NONE) {
index 03de1ab39e03b0834d666dd050318be342647468..a417fdd090db834b80124bc86aacdba7f22c8fbc 100644 (file)
@@ -61,7 +61,7 @@ const char* _get_dek_kek_pub_key_path();
 const char* _get_dek_kek_pri_key_path();
 const char* _get_dek_store_path();
 int _add_dek_to_key_manager(const char* pPkgId, wae_app_type_e appType, const unsigned char* pDek, size_t len);
-int _get_preloaded_app_dek_file_path(const char* pPkgId, char *path);
+int _get_preloaded_app_dek_file_path(const char* pPkgId, size_t size, char *path);
 int _extract_pkg_id_from_file_name(const char* fileName, char* pkgId);
 int _read_encrypted_app_dek_from_file(const char* pPkgId, unsigned char** encrypted_app_dek, size_t*len);
 int _write_encrypted_app_dek_to_file(const char* pPkgId, const unsigned char* encrypted_app_dek, size_t len);
index 631e9eaad508a12c8eac966c850c68226b1f85c5..0b584e56849d5cbc7cf8eeecf5f71dbfe09f159d 100644 (file)
@@ -430,7 +430,7 @@ int wae_tc_get_preloaded_app_dek_file_path()
     char path[256];
 
     FPRINTF("...expected path : %s\n", expectedPath);
-    ret = _get_preloaded_app_dek_file_path(pkgId, path);
+    ret = _get_preloaded_app_dek_file_path(pkgId, sizeof(path), path);
     FPRINTF("...returned path : %s\n", path);
 
     if(ret != WAE_ERROR_NONE || strncmp(expectedPath, path, strlen(expectedPath)) != 0) {
@@ -623,8 +623,8 @@ int wae_tc_load_preloaded_app_deks()
     char path2[MAX_PATH_LEN] = {0, };
     FILE *f2 = NULL;
 
-    _get_preloaded_app_dek_file_path(pkgId1, path1);
-    _get_preloaded_app_dek_file_path(pkgId2, path2);
+    _get_preloaded_app_dek_file_path(pkgId1, sizeof(path1), path1);
+    _get_preloaded_app_dek_file_path(pkgId2, sizeof(path2), path2);
 
     // remove old test data
     remove_app_dek(pkgId1, WAE_PRELOADED_APP);