diff options
author | Vitaliy Cherepanov <v.cherepanov@samsung.com> | 2015-10-07 18:02:05 +0300 |
---|---|---|
committer | Vitaliy Cherepanov <v.cherepanov@samsung.com> | 2015-10-07 18:02:05 +0300 |
commit | b5e47a501e2a57c09669aa973d9a9c14941a9097 (patch) | |
tree | 107f7cd34c7b751cd79ef1e64806b6a4f2edbc00 | |
parent | c16dbc189e51fb9c95abc35d692d24757d61adab (diff) | |
download | swap-probe-b5e47a501e2a57c09669aa973d9a9c14941a9097.tar.gz swap-probe-b5e47a501e2a57c09669aa973d9a9c14941a9097.tar.bz2 swap-probe-b5e47a501e2a57c09669aa973d9a9c14941a9097.zip |
[FIX] prevent issues
-----------------------------------------------------------------------|
| CID | Type | File | Function |
|-----------------------------------------------------------------------|
|452748 |DC.SECURE_CODING_STRCAT | btsym.c | da_backtrace_symbol|
|452682 |DC.SECURE_CODING_STRCPY | btsym.c | da_backtrace_symbol|
|452674 |DC.SECURE_CODING_BACKTRACE | libdaprobe.c | getBacktraceString |
|452625 |DC.SECURE_CODING_SPRINTF | btsym.c | |
|452589 |DC.SECURE_CODING_SPRINTF | btsym.c | |
|452571 |DC.SECURE_CODING_USLEEP | damaps.c | print_list_sorted |
|-----------------------------------------------------------------------|
Change-Id: Ib27b74d14a011b4e9b8d4f41b0d7e9f7ae3810f8
Signed-off-by: Vitaliy Cherepanov <v.cherepanov@samsung.com>
-rwxr-xr-x | helper/btsym.c | 37 | ||||
-rwxr-xr-x | helper/dahelper.c | 11 | ||||
-rwxr-xr-x | helper/damaps.c | 2 | ||||
-rwxr-xr-x | helper/libdaprobe.c | 63 | ||||
-rwxr-xr-x | include/dahelper.h | 1 |
5 files changed, 38 insertions, 76 deletions
diff --git a/helper/btsym.c b/helper/btsym.c index 068e45b..38835d8 100755 --- a/helper/btsym.c +++ b/helper/btsym.c @@ -240,7 +240,9 @@ char** cached_backtrace_symbols (void* const* array, int size) } /* Allocate memory for the result. */ - result = (char **) malloc (size * sizeof (char *) + total); + uint32_t allocated_size = size * sizeof (char *) + total; + uint32_t tail_size; + result = (char **) malloc (allocated_size); if (result != NULL) { char *last = (char *) (result + size); @@ -251,7 +253,8 @@ char** cached_backtrace_symbols (void* const* array, int size) if(chararr[cnt] != NULL) // there is a cache { - last += (1 + sprintf(last, "%s", chararr[cnt])); + tail_size = allocated_size - ((void *)last - (void *)result); + last += (1 + snprintf(last, tail_size, "%s", chararr[cnt])); } else // there is no cache { @@ -264,7 +267,8 @@ char** cached_backtrace_symbols (void* const* array, int size) if (info[cnt].dli_sname == NULL && info[cnt].dli_saddr == 0) { - tstrlen = sprintf (last, "%s(%s) [%p]", info[cnt].dli_fname ?: "", info[cnt].dli_sname ?: "", array[cnt]); + tail_size = allocated_size - ((void *)last - (void *)result); + tstrlen = snprintf (last, tail_size, "%s(%s) [%p]", info[cnt].dli_fname ?: "", info[cnt].dli_sname ?: "", array[cnt]); } else { @@ -281,7 +285,8 @@ char** cached_backtrace_symbols (void* const* array, int size) offset = info[cnt].dli_saddr - array[cnt]; } - tstrlen = sprintf (last, "%s(%s%c%#tx) [%p]", + tail_size = allocated_size - ((void *)last - (void *)result); + tstrlen = snprintf (last, tail_size, "%s(%s%c%#tx) [%p]", info[cnt].dli_fname ?: "", info[cnt].dli_sname ?: "", sign, offset, array[cnt]); @@ -289,7 +294,8 @@ char** cached_backtrace_symbols (void* const* array, int size) } else { - tstrlen = sprintf (last, "[%p]", array[cnt]); + tail_size = allocated_size - ((void *)last - (void *)result); + tstrlen = snprintf (last, tail_size, "[%p]", array[cnt]); } tstrlen++; @@ -343,7 +349,7 @@ char** da_backtrace_symbols (void* const* array, int size) size_t len = 0; if(map->l_origin && strlen(map->l_origin) < FILEPATH_MAX) { - strcpy(filepath, map->l_origin); + strncpy(filepath, map->l_origin, sizeof(filepath)); len = strlen(filepath); if(len > 0 && filepath[len-1] != '/') { @@ -355,7 +361,7 @@ char** da_backtrace_symbols (void* const* array, int size) else filepath[0] = '\0'; if (strlen(map->l_name) < FILEPATH_MAX - len) - strcat(filepath, map->l_name); + strncat(filepath, map->l_name, sizeof(filepath)); } symdata_t* pdata = _get_symboldata(filepath); @@ -412,7 +418,9 @@ char** da_backtrace_symbols (void* const* array, int size) } /* Allocate memory for the result. */ - result = (char **) malloc (size * sizeof (char *) + total); + uint32_t allocated_size = size * sizeof (char *) + total; + uint32_t tail_size; + result = (char **) malloc (allocated_size); if (result != NULL) { char *last = (char *) (result + size); @@ -423,7 +431,8 @@ char** da_backtrace_symbols (void* const* array, int size) if(chararr[cnt] != NULL) // there is a cache { - last += (1 + sprintf(last, "%s", chararr[cnt])); + tail_size = allocated_size - ((void *)last - (void *)result); + last += (1 + snprintf(last, tail_size, "%s", chararr[cnt])); } else // there is no cache { @@ -436,7 +445,8 @@ char** da_backtrace_symbols (void* const* array, int size) if (info[cnt].dli_sname == NULL && info[cnt].dli_saddr == 0) { - tstrlen = sprintf (last, "%s(%s) [%p]", info[cnt].dli_fname ?: "", info[cnt].dli_sname ?: "", array[cnt]); + tail_size = allocated_size - ((void *)last - (void *)result); + tstrlen = snprintf (last, tail_size, "%s(%s) [%p]", info[cnt].dli_fname ?: "", info[cnt].dli_sname ?: "", array[cnt]); } else { @@ -453,7 +463,8 @@ char** da_backtrace_symbols (void* const* array, int size) offset = info[cnt].dli_saddr - array[cnt]; } - tstrlen = sprintf (last, "%s(%s%c%#tx) [%p]", + tail_size = allocated_size - ((void *)last - (void *)result); + tstrlen = snprintf (last, tail_size, "%s(%s%c%#tx) [%p]", info[cnt].dli_fname ?: "", info[cnt].dli_sname ?: "", sign, offset, array[cnt]); @@ -461,7 +472,9 @@ char** da_backtrace_symbols (void* const* array, int size) } else { - tstrlen = sprintf (last, "[%p]", array[cnt]); + + tail_size = allocated_size - ((void *)last - (void *)result); + tstrlen = snprintf (last, tail_size, "[%p]", array[cnt]); } tstrlen++; diff --git a/helper/dahelper.c b/helper/dahelper.c index 152bfc2..8c8e67d 100755 --- a/helper/dahelper.c +++ b/helper/dahelper.c @@ -154,3 +154,14 @@ char *real_abs_path(int fd, char *buffer, size_t bufsiz) return buffer; } + +void swap_usleep(useconds_t usec) +{ + struct timespec req; + struct timespec rem; + req.tv_sec = usec / 1000000; + req.tv_nsec = (usec % 1000000) * 1000; + if (nanosleep(&req, &rem) == -1) { + PRINTWRN("sleep was terminated by signal\n"); + } +} diff --git a/helper/damaps.c b/helper/damaps.c index bc0281b..ef58508 100755 --- a/helper/damaps.c +++ b/helper/damaps.c @@ -207,7 +207,7 @@ static void __attribute__((used)) print_list_sorted(struct map_t **list) m->hash, m->is_instrument, m->filename); - usleep(500); + swap_usleep(500); } } diff --git a/helper/libdaprobe.c b/helper/libdaprobe.c index b7fc6d1..36d5dd6 100755 --- a/helper/libdaprobe.c +++ b/helper/libdaprobe.c @@ -712,69 +712,6 @@ bool print_log_fmt(int msgType, const char *func_name, int line, ...) return (res == len); } -// get backtrace string -// return stack depth if succeed, otherwise return 0 -// parameter 'log' cannot be null -/* TODO remove unused code (getBacktraceString)*/ -int getBacktraceString(log_t* log, int bufsize) -{ - void* array[MAX_STACK_DEPTH]; - char** strings = NULL; - size_t i, size; - int initsize; - int curlen; - int stringlen; - - if(log == NULL) - return 0; - - initsize = log->length; - curlen = initsize; - log->data[curlen] = '\0'; // is this necessary ? - size = backtrace(array, MAX_STACK_DEPTH); - if(likely(size > TRIM_STACK_DEPTH)) - { - strings = BACKTRACE_SYMBOLS(array + TRIM_STACK_DEPTH, size - TRIM_STACK_DEPTH); - - if(likely(strings != NULL)) - { - for(i = TRIM_STACK_DEPTH; i < size; i++) - { - stringlen = strlen(strings[i - TRIM_STACK_DEPTH]) + 14; - if(curlen + stringlen >= bufsize + initsize) - break; - - curlen += snprintf(log->data + curlen, bufsize - curlen, "%010u`,%s`,", (unsigned int)(array[i]), strings[i - TRIM_STACK_DEPTH]); - } - curlen -= 2; - log->data[curlen] = '\0'; - log->length = curlen; - free(strings); - } - else // failed to get backtrace symbols - { - // just print trace address - for(i = TRIM_STACK_DEPTH; i < size; i++) - { - stringlen = 23; - if(curlen + stringlen >= bufsize + initsize) - break; - - curlen += snprintf(log->data + curlen, bufsize - curlen, "%010u`,(unknown)`,", (unsigned int)(array[i])); - } - curlen -= 2; - log->data[curlen] = '\0'; - log->length = curlen; - } - - return (int)(size - TRIM_STACK_DEPTH); - } - else - { - return 0; - } -} - /************************************************************************* * probe block control functions *************************************************************************/ diff --git a/include/dahelper.h b/include/dahelper.h index b4c1edb..8229cfb 100755 --- a/include/dahelper.h +++ b/include/dahelper.h @@ -230,6 +230,7 @@ int getOrientation(); void on_orientation_changed(int angle, bool capi); int remove_indir(const char* dirname); +void swap_usleep(useconds_t usec); // query functions #define isOptionEnabled(OPT) ((gTraceInfo.optionflag & OPT) != 0) |