478641 fix (String overflow in SS_FSUpdate.c) sandbox/anowak03/libtota_tizen_6.5_devel_test
authorArkadiusz Nowak <a.nowak3@samsung.com>
Fri, 25 Feb 2022 08:07:46 +0000 (09:07 +0100)
committerArkadiusz Nowak <a.nowak3@samsung.com>
Tue, 8 Mar 2022 07:25:36 +0000 (08:25 +0100)
Test recursive folder create

Change-Id: I817a6076198f61987f5423185f0e4bf21fb1fe0e

ss_engine/SS_FSUpdate.c
test/CMakeLists.txt
test/test_brotli_decompress.c [new file with mode: 0644]
test/test_decompress_bytes.c [new file with mode: 0644]
test/test_recursive_folder_creater.c [new file with mode: 0644]

index ee8c6d095ca4ad33139aac91857d0b0cf0ac6bb7..62719477eb560fd4aece380d154fa6f1c767d2e9 100755 (executable)
@@ -22,7 +22,7 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
-#include <sys/stat.h>
+//#include <sys/stat.h>
 #include <sys/vfs.h>
 #include <sys/types.h>
 #include <stdarg.h>
@@ -99,34 +99,50 @@ void SS_char_to_unicode(const char *src, char *dest, int size)
        strncpy(dest, src, size);
 }
 
-long SS_recursive_folder_creater(const char *path, const mode_t mode)
-{
-       int ret = 0;
-       int offset = 0;
-       char temppath[MAX_PATH] = { '\0' };
+long SS_recursive_folder_creater(const char *path, const mode_t mode) {
 
-       LOGL(LOG_SSENGINE, "path: %s\n", path);
+       if (path == 0) {
+               LOGL(LOG_SSENGINE, "Bad path param value \n");
+               return -E_SS_BAD_PARAMS;
+       }
 
-       if ((offset = strlen(path)) == 0) // For counting back until the '/' delimiter
-               return -1;      //if from some reason we got to the end return error!!!.
+       if (path[0] == '\0') {
+               LOGL(LOG_SSENGINE, "Path empty \n");
+               return -E_SS_FILENAMELENERROR;
+       }
 
-       while (path[offset] != '/')     // get to the next '/' place
-               offset--;
+       char input_path[MAX_PATH] = {'\0'};
+       char temppath[MAX_PATH] = {'\0'};
 
-       strncpy(temppath, path, offset);        // copy one depth below till and without the char '/'
-       LOGL(LOG_SSENGINE, " temppath: %s\n", temppath);
-       ret = mkdir(temppath, mode);
-       LOGL(LOG_SSENGINE, " mkdir result: %d errno: %d\n", ret, errno);
+       strncpy(input_path, path, MAX_PATH);
 
-       if (ret == 0 || ((ret == -1) && (errno == EEXIST))) {
-               return 0;       //meaning the depth creation is success.
-       } else if ((ret == -1) && (errno == ENOENT)) {
-               if ((ret = SS_recursive_folder_creater(temppath, mode)) == 0)
-                       ret = mkdir(temppath, mode);
-               return ret;
-       } else {
-               return -1;
+       if (input_path[0] == '/') {
+               temppath[0] = '/';
+       }
+
+       char *path_token;
+       char *path_pos = input_path;
+
+       int temppath_current_len = (temppath[0] == '/');
+
+       while((path_token = strtok_r(path_pos, "/", &path_pos))) {
+
+               int path_token_len = strnlen(path_token, MAX_PATH-temppath_current_len);
+               strncat(temppath, path_token, path_token_len);
+               temppath_current_len += path_token_len;
+
+       int mkd_res = mkdir(temppath, mode);
+
+               if(mkd_res != 0 && errno != EEXIST) {
+                       LOGL(LOG_SSENGINE,"cannot create dir %s\n system error: %d  error: %s\n", temppath, errno, strerror(errno));
+                       return -E_SS_CANNOT_CREATE_DIRECTORY;
+               }
+
+               if(path_token_len < (MAX_PATH-1)) {
+                       strcat(temppath, "/");
+               }
        }
+       return 0;
 }
 
 long
index e35b5385a544d4ce7fd6ef0e6fd5fb1ccf56bf18..cc8fd94c91b2c6a83818be545ee281323a2caec7 100644 (file)
@@ -19,7 +19,7 @@ function(add_mocked_test name wraps)
        include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../ss_engine/)
        include_directories(${CMAKE_CURRENT_SOURCE_DIR}/../bsdiff/)
 
-       add_executable(test_${name} test_${name}.c  common.c ${all_prj_srcs} )
+       add_executable(test_${name} common.c ${all_prj_srcs} test_${name}.c )
        target_link_libraries(test_${name} cmocka  ${${PROJECT_NAME}_pkgs_LDFLAGS} divsufsort brotlicommon brotlidec brotlienc pthread ${wraps})
 
        message("--> ${CMAKE_CURRENT_BINARY_DIR}")
@@ -30,3 +30,4 @@ endfunction(add_mocked_test)
 add_mocked_test(sha1 "")
 add_mocked_test(img_verfiy_partition "-Wl,--defsym=SS_GetAvailableFreeSpace=mock_SS_GetAvailableFreeSpace -Wl,--defsym=SS_CalculateFileSha=mock_SS_CalculateFileSha")
 add_mocked_test(brotli_compress "-Wl,--wrap=open -Wl,--wrap=mmap -Wl,--wrap=ftruncate -Wl,--wrap=lseek")
+add_mocked_test(recursive_folder_creater  "-Wl,--wrap=mkdir")
diff --git a/test/test_brotli_decompress.c b/test/test_brotli_decompress.c
new file mode 100644 (file)
index 0000000..3d5e271
--- /dev/null
@@ -0,0 +1,32 @@
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <cmocka.h>
+#include <arpa/inet.h>
+
+#include "sha1.h"
+
+#include "test_vars.h"
+
+
+int _system_cmd_wait(const char * cmd) {
+       return system(cmd);
+}
+
+static void test_brotli_decompress(void **state) {
+       (void) state;
+
+       assert_true(0==1);
+}
+int main(void) {
+
+       __log_out_file__ = stdout;
+
+       const struct CMUnitTest tests[] = {
+               cmocka_unit_test(test_brotli_decompress),
+       };
+       return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/test/test_decompress_bytes.c b/test/test_decompress_bytes.c
new file mode 100644 (file)
index 0000000..f514510
--- /dev/null
@@ -0,0 +1,33 @@
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <cmocka.h>
+#include <arpa/inet.h>
+
+#include "sha1.h"
+
+#include "test_vars.h"
+#include "common.h"
+
+int _system_cmd_wait(const char * cmd) {
+       return system(cmd);
+}
+
+static void test_decompress_bytes(void **state) {
+       (void) state;
+
+       assert_true(0==1);
+}
+
+int main(void) {
+
+       __log_out_file__ = stdout;
+
+       const struct CMUnitTest tests[] = {
+               cmocka_unit_test(test_decompress_bytes),
+       };
+       return cmocka_run_group_tests(tests, NULL, NULL);
+}
diff --git a/test/test_recursive_folder_creater.c b/test/test_recursive_folder_creater.c
new file mode 100644 (file)
index 0000000..80cde59
--- /dev/null
@@ -0,0 +1,134 @@
+#include <stdarg.h>
+#include <stddef.h>
+#include <setjmp.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <cmocka.h>
+#include <arpa/inet.h>
+#include <errno.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <unistd.h>
+
+#include "sha1.h"
+
+#include "test_vars.h"
+
+#include "SS_Common.h"
+
+
+int _system_cmd_wait(const char * cmd) {
+       return system(cmd);
+}
+extern long SS_recursive_folder_createrN(const char *path, const mode_t mode);
+
+int mck_errno = 0;
+
+#define PATH_SIM_SIZE 512
+char path_sim[PATH_SIM_SIZE];
+
+int __wrap_mkdir(const char *path, mode_t mode) {
+
+       print_message("-->wrappend mkdir\n");
+       print_message("-->path: %s\n", path);
+       int abs_path_mark = 0;
+
+       char local_path[256];
+       char l_path_sim[512];
+
+       if(path[0] == '/') {
+               abs_path_mark = 1;
+       }
+
+       strcpy(l_path_sim, path_sim);
+       strcpy(local_path, path);
+
+       char *input_path_token;
+       char *last_input_token = NULL;;
+       char *curr_input_path_pos = local_path;
+       int input_path_tokens = 0;
+
+       while((input_path_token = strtok_r(curr_input_path_pos, "/", &curr_input_path_pos))) {
+               input_path_tokens++;
+               //print_message("-->input_path_token: %s\n", input_path_token);
+               last_input_token = input_path_token;
+       }
+
+       if(last_input_token == NULL) {
+               print_message("--> last_input_token == NULL\n");
+               errno = ENOENT;
+               return -1;
+       }
+
+       if(input_path_tokens == 0) {
+               print_message("-->input_path_tokens: %d\n", input_path_tokens);
+               errno = ENOENT;
+               return -1;
+       }
+
+       char *path_sim_token;
+       char *curr_path_sim_pos = l_path_sim;
+       int path_sim_tokens = 0;
+
+       while((path_sim_token = strtok_r(curr_path_sim_pos, "/", &curr_path_sim_pos))) {
+               path_sim_tokens++;
+               //print_message("-->path_sim_token: %s\n", path_sim_token);
+       }
+
+       if(input_path_tokens == path_sim_tokens) {
+               print_message("-->path_sim_tokens: %d\n", path_sim_tokens);
+               errno = EEXIST;
+               return -1;
+       }
+
+       if(path_sim_tokens != (input_path_tokens - 1 )) {
+               print_message("--> path_sim_tokens != (input_path_tokens-1)\n");
+               print_message("--> path_sim_tokens: %d\n", path_sim_tokens);
+               print_message("--> input_path_tokens: %d\n", input_path_tokens);
+
+               errno = ENOENT;
+               return -1;
+       }
+
+       if(path_sim[0] != '\0') {
+               strcat(path_sim, "/");
+       }
+       if (abs_path_mark == 1 && path_sim[0] != '/') {
+               strcat(path_sim, "/");
+       }
+
+       strcat(path_sim, last_input_token);
+       print_message("--> path_sim: %s\n", path_sim);
+       return 0;
+}
+
+static void test_recursive_folder_creater(void **state) {
+       (void) state;
+
+
+       char  target_path[16] = "a/b/c/d/e";
+       char  src_path[16] = "a/b/c/d/e";
+       memset(path_sim, 0, PATH_SIM_SIZE);
+
+       SS_recursive_folder_creater(src_path, 0);
+
+       print_message("src :: %s\n", src_path);
+       print_message("target :: %s\n", target_path);
+       print_message("path_sim :: %s\n", path_sim);
+
+       int res_target_vs_src = strncmp(target_path, src_path, 16);
+       int res_src_vs_path_sim = strncmp(target_path, path_sim, 16);
+
+       assert_int_equal(res_target_vs_src, res_src_vs_path_sim);
+}
+
+int main(void) {
+
+       __log_out_file__ = stdout;
+
+       const struct CMUnitTest tests[] = {
+               cmocka_unit_test(test_recursive_folder_creater),
+       };
+       return cmocka_run_group_tests(tests, NULL, NULL);
+}