478641 fix (String overflow in SS_FSUpdate.c) 99/271799/4
authorArkadiusz Nowak <a.nowak3@samsung.com>
Tue, 1 Mar 2022 08:40:14 +0000 (09:40 +0100)
committerArkadiusz Nowak <a.nowak3@samsung.com>
Tue, 8 Mar 2022 07:36:27 +0000 (08:36 +0100)
Change-Id: I9cdad08fec128b3bbe67e1722ad9ab3037480888

ss_engine/SS_FSUpdate.c

index ee8c6d095ca4ad33139aac91857d0b0cf0ac6bb7..8cc8250ff654cf767cbef9eb1f7e32de61702bc4 100755 (executable)
@@ -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