Fix c++test defects (snprintf, strncpy usage) 00/205200/2
authorKonrad Lipinski <k.lipinski2@partner.samsung.com>
Tue, 30 Apr 2019 07:11:06 +0000 (09:11 +0200)
committerKonrad Lipinski <k.lipinski2@partner.samsung.com>
Tue, 7 May 2019 08:40:44 +0000 (10:40 +0200)
Change-Id: I1e548235272c53be62a304443a4847b98a9b1f90

srcs/key_handler.c
srcs/key_manager.c

index e095903653774679312a2d4f758bdce0a802c2c0..a60142ef6024af56f0a0027c7664c9818936ebc3 100644 (file)
@@ -1,5 +1,5 @@
 /*
- *  Copyright (c) 2016 Samsung Electronics Co., Ltd All Rights Reserved
+ *  Copyright (c) 2016-2019 Samsung Electronics Co., Ltd. All rights reserved
  *
  *  Licensed under the Apache License, Version 2.0 (the "License");
  *  you may not use this file except in compliance with the License.
@@ -248,7 +248,8 @@ static int _entry_callback_remove_all(
        (void) user_data; // TODO: use UNUSED macro
 
        char file_path_buff[MAX_PATH_LEN] = {0, };
-       if (snprintf(file_path_buff, sizeof(file_path_buff), "%s/%s", path, entry->d_name) < 0)
+       if ((unsigned)snprintf(file_path_buff, sizeof(file_path_buff), "%s/%s",
+                               path, entry->d_name) >= sizeof(file_path_buff))
                return WAE_ERROR_INVALID_PARAMETER; /* buffer size too small */
 
        int ret = WAE_ERROR_NONE;
@@ -273,8 +274,8 @@ void _remove_directory(const char *path)
 
 int _get_preloaded_app_dek_file_path(const char *pkg_id, size_t size, char *path)
 {
-       if (snprintf(path, size, "%s/%s_%s.adek",
-                               _get_dek_store_path(), APP_DEK_FILE_PFX, pkg_id) < 0)
+       if ((unsigned)snprintf(path, size, "%s/%s_%s.adek",
+                               _get_dek_store_path(), APP_DEK_FILE_PFX, pkg_id) >= size)
                return WAE_ERROR_INVALID_PARAMETER; /* buffer size too small */
        else
                return WAE_ERROR_NONE;
@@ -297,6 +298,11 @@ static int _extract_pkg_id_from_file_name(const char *file_name, char *pkg_id)
                return WAE_ERROR_FILE;
        }
 
+       if (end - start >= MAX_PKGID_LEN) {
+               WAE_SLOGE("WAE: pkgid extracted from APP_DEK file too long. file_name=%s", file_name);
+               return WAE_ERROR_INVALID_PARAMETER;
+       }
+
        strncpy(pkg_id, start, end - start);
        pkg_id[end - start] = 0; //terminate string
 
@@ -616,7 +622,7 @@ static int _entry_callback_load_preloaded_adeks(
        const char *pri_key_path = _get_dek_kek_pri_key_path();
 
        char file_path_buff[MAX_PATH_LEN] = {0, };
-       if (snprintf(file_path_buff, sizeof(file_path_buff), "%s/%s", path, entry->d_name) < 0)
+       if ((unsigned)snprintf(file_path_buff, sizeof(file_path_buff), "%s/%s", path, entry->d_name) >= sizeof(file_path_buff))
                return WAE_ERROR_INVALID_PARAMETER; /* buffer size too small */
 
        if (strcmp(file_path_buff, pub_key_path) == 0 ||
index f4c049c50374c0497fc9f305909d117c8f9d67f9..1f7a96d190f3d390c5ddf69f5ca4e27aa897938b 100644 (file)
@@ -192,14 +192,18 @@ error:
        return ret;
 }
 
-static void _get_alias(const char *name, UNUSED wae_app_type_e type, UNUSED bool forSave,
+static int _get_alias(const char *name, UNUSED wae_app_type_e type, UNUSED bool forSave,
                                           char *alias, size_t buff_len)
 {
-       snprintf(alias, buff_len, "%s%s%s%s",
+       if ((unsigned)snprintf(alias, buff_len, "%s%s%s%s",
                         ckmc_owner_id_system,
                         ckmc_owner_id_separator,
                         APP_DEK_ALIAS_PFX,
-                        name);
+                        name) >= buff_len) {
+               WAE_SLOGE("Alias buffer too small for name(%s)", name);
+               return WAE_ERROR_INVALID_PARAMETER;
+       }
+       return WAE_ERROR_NONE;
 }
 
 int save_to_key_manager(const char *name, const char *pkg_id, wae_app_type_e type,
@@ -207,10 +211,12 @@ int save_to_key_manager(const char *name, const char *pkg_id, wae_app_type_e typ
 {
        char alias[MAX_ALIAS_LEN] = {0, };
 
-       _get_alias(name, type, true, alias, sizeof(alias));
+       int ret = _get_alias(name, type, true, alias, sizeof(alias));
+       if (ret != WAE_ERROR_NONE)
+               return ret;
 
        ckmc_raw_buffer_s *buf = NULL;
-       int ret = _serialize(ce, &buf);
+       ret = _serialize(ce, &buf);
        if (ret != WAE_ERROR_NONE) {
                WAE_SLOGE("Failed to serialize crypto element of name(%s)", name);
                return ret;
@@ -251,10 +257,12 @@ int get_from_key_manager(const char *name, wae_app_type_e type, crypto_element_s
 
        char alias[MAX_ALIAS_LEN] = {0, };
 
-       _get_alias(name, type, false, alias, sizeof(alias));
+       int ret = _get_alias(name, type, false, alias, sizeof(alias));
+       if (ret != WAE_ERROR_NONE)
+               return ret;
 
        ckmc_raw_buffer_s *buf = NULL;
-       int ret = _to_wae_error(ckmc_get_data(alias, NULL, &buf));
+       ret = _to_wae_error(ckmc_get_data(alias, NULL, &buf));
        if (ret != WAE_ERROR_NONE)
                return ret;
 
@@ -269,17 +277,21 @@ int remove_from_key_manager(const char *name, wae_app_type_e type)
 {
        char alias[MAX_ALIAS_LEN] = {0, };
 
-       _get_alias(name, type, true, alias, sizeof(alias));
+       int ret = _get_alias(name, type, true, alias, sizeof(alias));
+       if (ret != WAE_ERROR_NONE)
+               return ret;
 
        return _to_wae_error(ckmc_remove_alias(alias));
 }
 
-static void _get_dek_kek_alias(char *alias, size_t buff_len)
+static int _get_dek_kek_alias(char *alias, size_t buff_len)
 {
-       snprintf(alias, buff_len, "%s%s%s",
+       return (unsigned)snprintf(alias, buff_len, "%s%s%s",
                         ckmc_owner_id_system,
                         ckmc_owner_id_separator,
-                        APP_DEK_KEK_ALIAS);
+                        APP_DEK_KEK_ALIAS) >= buff_len
+               ? WAE_ERROR_INVALID_PARAMETER
+               : WAE_ERROR_NONE;
 }
 
 int get_dek_kek_from_key_manager(raw_buffer_s **pdek_kek)
@@ -290,9 +302,11 @@ int get_dek_kek_from_key_manager(raw_buffer_s **pdek_kek)
        ckmc_raw_buffer_s *buf = NULL;
 
        char alias[MAX_ALIAS_LEN] = {0, };
-       _get_dek_kek_alias(alias, sizeof(alias));
+       int ret = _get_dek_kek_alias(alias, sizeof(alias));
+       if (ret != WAE_ERROR_NONE)
+               return ret;
 
-       int ret = _to_wae_error(ckmc_get_data(alias, NULL, &buf));
+       ret = _to_wae_error(ckmc_get_data(alias, NULL, &buf));
        if (ret != WAE_ERROR_NONE) {
                WAE_SLOGE("Failed to get dek kek from key-manager. alias(%s) ret(%d)",
                                  alias, ret);