[FIX] prevent issues 14/28614/1
authorVitaliy Cherepanov <v.cherepanov@samsung.com>
Fri, 10 Oct 2014 13:27:44 +0000 (17:27 +0400)
committerVitaliy Cherepanov <v.cherepanov@samsung.com>
Fri, 10 Oct 2014 13:27:44 +0000 (17:27 +0400)
Change-Id: I1cde206e7ffe470399c7d4a0ae2e090409160210
Signed-off-by: Vitaliy Cherepanov <v.cherepanov@samsung.com>
daemon/da_debug.c
daemon/da_protocol.c
daemon/da_protocol_check.c
daemon/da_protocol_check.h
daemon/device_system_info.c
daemon/elf.c
daemon/sys_stat.c
daemon/threads.c
daemon/transfer_thread.c

index 15a019a..1160e6d 100644 (file)
 #include <unistd.h>
 
 #include "daemon.h"
+#include "debug.h"
 
 #define DEBUG_LOGFILE          "/tmp/daemonlog.da"
 
 #if DEBUG
 static inline void close_on_exec_dup(int old, int new)
 {
-       dup2(old, new);
-       fcntl(new, F_SETFD, fcntl(new, F_GETFD) | FD_CLOEXEC);
+       if (dup2(old, new) != -1)
+               fcntl(new, F_SETFD, fcntl(new, F_GETFD) | FD_CLOEXEC);
+       else
+               LOGE("dup2 fail\n");
 }
 
 void initialize_log(void)
index bb97f4f..5d20d25 100644 (file)
@@ -141,7 +141,6 @@ static char *msgErrStr(enum ErrorCode err)
        default:
                return "unknown error";
        }
-       return "unknown error";
 }
 
 
@@ -777,12 +776,13 @@ static void get_file_md5sum(md5_byte_t digest[16], const char *filename)
        int fd = open(filename, O_RDONLY);
 
        md5_init(&md5_state);
-       if (fd > 0)
+       if (fd > 0) {
                while ((size = read(fd, buffer, sizeof(buffer))) > 0)
                        md5_append(&md5_state, buffer, size);
+               close(fd);
+       }
 
        md5_finish(&md5_state, digest);
-       close(fd);
 }
 
 static const char* basename(const char *filename)
index c8fa651..149c35f 100644 (file)
@@ -153,9 +153,7 @@ int check_conf_datamsg_period(uint32_t data_message_period)
 int check_us_app_count(uint32_t app_count)
 {
        int res = 1;
-       if ((app_count < US_APP_COUNT_MIN) ||
-               (app_count > US_APP_COUNT_MAX))
-       {
+       if (app_count > US_APP_COUNT_MAX) {
                LOGE("wrong user space app count %u (0x%08X)\n",
                     (unsigned int)app_count, app_count);
                res = 0;
@@ -167,9 +165,7 @@ int check_us_app_count(uint32_t app_count)
 int check_us_app_inst_func_count(uint32_t func_count)
 {
        int res = 1;
-       if ((func_count < US_APP_INST_FUNC_MIN) ||
-               (func_count > US_APP_INST_FUNC_MAX))
-       {
+       if (func_count > US_APP_INST_FUNC_MAX) {
                LOGE("wrong US app inst func count %u (0x%08X)\n",
                     (unsigned int)func_count, func_count);
                res = 0;
@@ -203,9 +199,7 @@ int check_us_inst_func_ret_type(char ret_type)
 int check_lib_inst_count(uint32_t lib_count)
 {
        int res = 1;
-       if ((lib_count < US_APP_INST_LIB_MIN) ||
-               (lib_count > US_APP_INST_LIB_MAX))
-       {
+       if (lib_count > US_APP_INST_LIB_MAX) {
                LOGE("wrong US app inst lib count %u (0x%08X)\n",
                     (unsigned int)lib_count, lib_count);
                res = 0;
index fc3db9e..5157df7 100644 (file)
 #define CONF_DATA_MSG_PERIOD_MIN 1
 #define CONF_DATA_MSG_PERIOD_MAX 100
 
-#define US_APP_COUNT_MIN 0
 #define US_APP_COUNT_MAX MAX_TARGET_COUNT
 
-#define US_APP_INST_FUNC_MIN 0
 #define US_APP_INST_FUNC_MAX 100000
 
 #define US_FUNC_ARGS "bcdxpfw"
 #define US_FUNC_RETURN "vnbcdxpfw"
 
-#define US_APP_INST_LIB_MIN 0
 #define US_APP_INST_LIB_MAX 1000
 
 int check_app_type(uint32_t app_type);
index 3dab571..94048e6 100644 (file)
 #include <system_info.h>
 #include <runtime_info.h>
 #include "device_system_info.h"
+#include "debug.h"
 
 static int is_available(const char *path)
 {
-       bool res;
+       bool res = 0;
 
-       system_info_get_platform_bool(path, &res);
+       if (system_info_get_platform_bool(path, &res) != SYSTEM_INFO_ERROR_NONE) {
+               /* TODO do something */
+               LOGE("get bool value fail\n");
+       }
 
        return res;
 }
index ca61c3b..7f5c6d8 100644 (file)
@@ -55,7 +55,10 @@ typedef Elf32_Shdr Elf_Shdr;
 static size_t fsize(int fd)
 {
        struct stat buf;
-       fstat(fd, &buf);
+       if (fstat(fd, &buf) != 0) {
+               LOGE("cannot get file size\n");
+               return 0;
+       }
        return buf.st_size;
 }
 
index 6a39a71..67ea263 100644 (file)
@@ -322,7 +322,12 @@ static void get_cpu_frequency(float *freqs)
                        freqs[cpu_n] = 0.0;
                } else {
                        //core enabled, get frequency
-                       fscanf(f, "%s", freq_str);
+                       if (fscanf(f, "%s", freq_str) != 1) {
+                               /* TODO return error code */
+                               LOGE("scan fail\n");
+                               return;
+                       }
+
                        freqs[cpu_n] = atof(freq_str);
                        LOGI_th_samp("core #%d freq = %.0f\n", cpu_n, freqs[cpu_n]);
                        fclose(f);
@@ -1430,22 +1435,31 @@ static int fill_system_cpu_info(struct system_info_t *sys_info)
        return 0;
 }
 
+/* TODO add return value to skip_lines */
 static void skip_lines(FILE * fp, unsigned int count)
 {
        char *buffer = NULL;
        size_t buflen;
        unsigned int index;
-       for (index = 0; index != count; ++index)
-               getline(&buffer, &buflen, fp);
+       for (index = 0; index != count; ++index) {
+               if (getline(&buffer, &buflen, fp) < 0)
+                       LOGE("file scan fail\n");
+       }
        free(buffer);
 }
 
-static void skip_tokens(FILE * fp, unsigned int count)
+static int skip_tokens(FILE * fp, unsigned int count)
 {
+       int res;
        unsigned int index;
 
-       for (index = 0; index != count; ++index)
-               fscanf(fp, "%*s");
+       for (index = 0; index != count; ++index) {
+               res = fscanf(fp, "%*s");
+               if (res == EOF && index != count - 1)
+                       return -1;
+       }
+
+       return 0;
 }
 
 static void init_network_stat()
@@ -1469,16 +1483,24 @@ static void get_network_stat(uint32_t *recv, uint32_t *send)
 
        while (fscanf(fp, "%s", ifname) != EOF)
                if (strcmp("lo:", ifname)) {
-                       fscanf(fp, "%" SCNuMAX, &irecv);
+                       if (fscanf(fp, "%" SCNuMAX, &irecv) <= 0)
+                               goto scan_error;
                        skip_tokens(fp, 7);
 
-                       fscanf(fp, "%" SCNuMAX, &isend);
+                       if (fscanf(fp, "%" SCNuMAX, &isend) <= 0)
+                               goto scan_error;
                        skip_tokens(fp, 7);
 
                        *recv += irecv;
                        *send += isend;
                } else
                        skip_tokens(fp, 16);
+
+       goto exit;
+scan_error:
+       LOGE("scan fail\n");
+exit:
+       return;
 }
 
 static void peek_network_stat_diff(uint32_t *recv, uint32_t *send)
@@ -1520,13 +1542,14 @@ static uint32_t get_partition_sector_size(char *partition_name)
                /* reset error code */
                errno = 0;
                /* scan partition sector size */
-               fscanf(f, "%d", &res);
-               /* check errors */
-               int errsv = errno;
-               if (errsv) {
-                       LOGE("scan file <%s> error: %s\n", buf,
-                            strerror(errsv));
-                       res = 0;
+               if (fscanf(f, "%d", &res) != 1) {
+                       /* check errors */
+                       int errsv = errno;
+                       if (errsv) {
+                               LOGE("scan file <%s> error: %s\n", buf,
+                                    strerror(errsv));
+                               res = 0;
+                       }
                }
                /* close source file */
                fclose(f);
@@ -1565,25 +1588,33 @@ static void get_disk_stat(uint32_t *reads, uint32_t *bytes_reads,
                char partition[partition_name_maxlength];
                uintmax_t preads, pwrites;
                uintmax_t psec_read, psec_write;
-               skip_tokens(fp, 2);
-               fscanf(fp, "%s", partition);
+               if (skip_tokens(fp, 2) < 0)
+                       goto exit;
+
+               if (fscanf(fp, "%s", partition) != 1)
+                       goto scan_error;
                if (*master_partition
                    && !strncmp(master_partition, partition,
                               strlen(master_partition))) {
                        /* subpartition */
                        skip_tokens(fp, 11);
                } else {
+                       /* TODO add check err in skip_token func */
                        //1
-                       fscanf(fp, "%" SCNuMAX, &preads);
+                       if (fscanf(fp, "%" SCNuMAX, &preads) == EOF)
+                               goto scan_error;
                        skip_tokens(fp, 1);
                        //3
-                       fscanf(fp, "%" SCNuMAX, &psec_read);
+                       if (fscanf(fp, "%" SCNuMAX, &psec_read) == EOF)
+                               goto scan_error;
                        skip_tokens(fp, 1);
                        //5
-                       fscanf(fp, "%" SCNuMAX, &pwrites);
+                       if (fscanf(fp, "%" SCNuMAX, &pwrites) == EOF)
+                               goto scan_error;
                        skip_tokens(fp, 1);
                        //7
-                       fscanf(fp, "%" SCNuMAX, &psec_write);
+                       if (fscanf(fp, "%" SCNuMAX, &psec_write) == EOF)
+                               goto scan_error;
                        skip_tokens(fp, 4);
 
                        memcpy(master_partition, partition,
@@ -1601,6 +1632,11 @@ static void get_disk_stat(uint32_t *reads, uint32_t *bytes_reads,
                }
        }
 
+       goto exit;
+scan_error:
+       LOGE("scan fail\n");
+exit:
+       return;
 }
 
 static void peek_disk_stat_diff(uint32_t *reads, uint32_t *bytes_reads,
index 02c5634..81480a0 100644 (file)
@@ -64,7 +64,7 @@ static void* recvThread(void* data)
 
        for (;;) {
                err = target_recv_msg(target, &log);
-               if (err != 0) {
+               if ((err != 0) || (log.length >= TARGER_MSG_MAX_LEN)) {
                        /* disconnect */
                        event = EVENT_STOP;
                        write(target->event_fd, &event, sizeof(event));
index a2ab5bb..ef4dc85 100644 (file)
@@ -59,7 +59,12 @@ static void *transfer_thread(void *arg)
        pthread_setcancelstate(PTHREAD_CANCEL_DISABLE, NULL);
 
        //init pipe
-       pipe(fd_pipe);
+       if (pipe(fd_pipe) != 0) {
+               /* TODO posible need total instrumentation stop up there */
+               LOGE("can not pipe!\n");
+               return NULL;
+       }
+
        //set cleanup function
        pthread_cleanup_push(transfer_thread_cleanup, (void *)fd_pipe);
 
@@ -116,7 +121,10 @@ int start_transfer(void)
        }
 
        saved_flags = fcntl(manager.buf_fd, F_GETFL);
-       fcntl(manager.buf_fd, F_SETFL, saved_flags & ~O_NONBLOCK);
+       if (fcntl(manager.buf_fd, F_SETFL, saved_flags & ~O_NONBLOCK) == -1) {
+               LOGE("can not set buf_fd flags\n");
+               return -1;
+       }
 
        if(pthread_create(&(manager.transfer_thread),
                          NULL,
@@ -143,7 +151,10 @@ void stop_transfer(void)
 
        flush_buf();
        saved_flags = fcntl(manager.buf_fd, F_GETFL);
-       fcntl(manager.buf_fd, F_SETFL, saved_flags | O_NONBLOCK);
+       if (fcntl(manager.buf_fd, F_SETFL, saved_flags | O_NONBLOCK) == -1) {
+               /* TODO do something on error */
+               LOGE("can not set buf_fd flags\n");
+       }
        wake_up_buf();
 
        LOGI("joining thread...\n");