summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKonrad Lipinski <k.lipinski2@partner.samsung.com>2019-04-30 09:11:06 +0200
committerKonrad Lipinski <k.lipinski2@partner.samsung.com>2019-05-07 10:40:44 +0200
commit199c7f3bf33675e855ec69a75c60dbffd293c490 (patch)
tree05d454b832588d7581a665cf8b067f2f404c5214
parent14e94dc420fdc4eb2a39f8f06f5320b97cc1d6b7 (diff)
downloadlibwebappenc-199c7f3bf33675e855ec69a75c60dbffd293c490.tar.gz
libwebappenc-199c7f3bf33675e855ec69a75c60dbffd293c490.tar.bz2
libwebappenc-199c7f3bf33675e855ec69a75c60dbffd293c490.zip
Fix c++test defects (snprintf, strncpy usage)
Change-Id: I1e548235272c53be62a304443a4847b98a9b1f90
-rw-r--r--srcs/key_handler.c16
-rw-r--r--srcs/key_manager.c40
2 files changed, 38 insertions, 18 deletions
diff --git a/srcs/key_handler.c b/srcs/key_handler.c
index e095903..a60142e 100644
--- a/srcs/key_handler.c
+++ b/srcs/key_handler.c
@@ -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 ||
diff --git a/srcs/key_manager.c b/srcs/key_manager.c
index f4c049c..1f7a96d 100644
--- a/srcs/key_manager.c
+++ b/srcs/key_manager.c
@@ -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);