tools: acrn-crashlog: remove unsafe api sprintf

Using snprintf intead of sprintf.

Tracked-On: #1254
Signed-off-by: Liu, Xinwu <xinwu.liu@intel.com>
Reviewed-by: Yonghua Huang <yonghua.huang@intel.com>
Acked-by: Chen Gang <gang.c.chen@intel.com>
This commit is contained in:
Liu, Xinwu 2018-09-25 10:31:53 +08:00 committed by Xie, Nanlin
parent 5ecf1078ca
commit fb0292846d
2 changed files with 40 additions and 12 deletions

View File

@ -32,6 +32,7 @@
#include "history.h" #include "history.h"
#include "log_sys.h" #include "log_sys.h"
#include "probeutils.h" #include "probeutils.h"
#include "strutils.h"
#define HISTORY_FIRST_LINE_FMT \ #define HISTORY_FIRST_LINE_FMT \
"#V1.0 CURRENTUPTIME %-24s\n" "#V1.0 CURRENTUPTIME %-24s\n"
@ -186,7 +187,12 @@ void hist_raise_uptime(char *lastuptime)
return; return;
} }
sprintf(firstline, HISTORY_FIRST_LINE_FMT, boot_time); ret = snprintf(firstline, sizeof(firstline),
HISTORY_FIRST_LINE_FMT, boot_time);
if (s_not_expect(ret, sizeof(firstline))) {
LOGE("failed to construct the firstline\n");
return;
}
replace_file_head(history_file, firstline); replace_file_head(history_file, firstline);
if (hours / uptime_hours >= loop_uptime_event) { if (hours / uptime_hours >= loop_uptime_event) {
@ -249,8 +255,9 @@ static int get_time_firstline(char *buffer)
int prepare_history(void) int prepare_history(void)
{ {
int ret; int ret;
int llen;
struct sender_t *crashlog; struct sender_t *crashlog;
char linebuf[MAXLINESIZE] = {0}; char linebuf[MAXLINESIZE];
crashlog = get_sender_by_name("crashlog"); crashlog = get_sender_by_name("crashlog");
if (!crashlog) if (!crashlog)
@ -272,7 +279,12 @@ int prepare_history(void)
} else { } else {
/* new history */ /* new history */
LOGW("new history\n"); LOGW("new history\n");
sprintf(linebuf, HISTORY_FIRST_LINE_FMT, "0000:00:00"); llen = snprintf(linebuf, sizeof(linebuf),
HISTORY_FIRST_LINE_FMT, "0000:00:00");
if (s_not_expect(llen, sizeof(linebuf))) {
LOGE("failed to construct the fristline\n");
return -EINVAL;
}
ret = overwrite_file(history_file, linebuf); ret = overwrite_file(history_file, linebuf);
if (ret < 0) { if (ret < 0) {
LOGE("Write (%s, %s) failed, error (%s)\n", LOGE("Write (%s, %s) failed, error (%s)\n",

View File

@ -33,6 +33,7 @@
#include "load_conf.h" #include "load_conf.h"
#include "log_sys.h" #include "log_sys.h"
#include "probeutils.h" #include "probeutils.h"
#include "strutils.h"
#define CRASH_CURRENT_LOG "currentcrashlog" #define CRASH_CURRENT_LOG "currentcrashlog"
#define STATS_CURRENT_LOG "currentstatslog" #define STATS_CURRENT_LOG "currentstatslog"
@ -98,6 +99,7 @@ static int compute_key(char *key, size_t key_len, const char *seed)
{ {
SHA256_CTX sha; SHA256_CTX sha;
char buf[VERSION_SIZE]; char buf[VERSION_SIZE];
int len;
long long time_ns; long long time_ns;
char *tmp_key = key; char *tmp_key = key;
unsigned char results[SHA256_DIGEST_LENGTH]; unsigned char results[SHA256_DIGEST_LENGTH];
@ -110,7 +112,10 @@ static int compute_key(char *key, size_t key_len, const char *seed)
SHA256_Init(&sha); SHA256_Init(&sha);
time_ns = get_uptime(); time_ns = get_uptime();
snprintf(buf, VERSION_SIZE, "%s%s%lld", gbuildversion, guuid, time_ns); len = snprintf(buf, VERSION_SIZE, "%s%s%lld",
gbuildversion, guuid, time_ns);
if (s_not_expect(len , VERSION_SIZE))
return -1;
SHA256_Update(&sha, (unsigned char *)buf, strlen(buf)); SHA256_Update(&sha, (unsigned char *)buf, strlen(buf));
SHA256_Update(&sha, (unsigned char *)seed, strlen(seed)); SHA256_Update(&sha, (unsigned char *)seed, strlen(seed));
@ -118,7 +123,9 @@ static int compute_key(char *key, size_t key_len, const char *seed)
SHA256_Final(results, &sha); SHA256_Final(results, &sha);
for (i = 0; i < key_len / 2; i++) { for (i = 0; i < key_len / 2; i++) {
sprintf(tmp_key, "%02x", results[i]); len = snprintf(tmp_key, 3, "%02x", results[i]);
if (s_not_expect(len, 3))
return -1;
tmp_key += 2; tmp_key += 2;
} }
*tmp_key = 0; *tmp_key = 0;
@ -194,8 +201,10 @@ char *generate_event_id(const char *seed1, const char *seed2,
static int reserve_log_folder(enum e_dir_mode mode, char *dir, static int reserve_log_folder(enum e_dir_mode mode, char *dir,
unsigned int *current) unsigned int *current)
{ {
char path[512]; char path[PATH_MAX];
int res; int res;
int plen;
int dlen;
struct sender_t *crashlog; struct sender_t *crashlog;
char *outdir; char *outdir;
unsigned int maxdirs; unsigned int maxdirs;
@ -208,22 +217,29 @@ static int reserve_log_folder(enum e_dir_mode mode, char *dir,
switch (mode) { switch (mode) {
case MODE_CRASH: case MODE_CRASH:
sprintf(path, "%s/%s", outdir, CRASH_CURRENT_LOG); plen = snprintf(path, PATH_MAX, "%s/%s", outdir,
sprintf(dir, "%s/%s", outdir, "crashlog"); CRASH_CURRENT_LOG);
dlen = snprintf(dir, PATH_MAX, "%s/%s", outdir, "crashlog");
break; break;
case MODE_STATS: case MODE_STATS:
sprintf(path, "%s/%s", outdir, STATS_CURRENT_LOG); plen = snprintf(path, PATH_MAX, "%s/%s", outdir,
sprintf(dir, "%s/%s", outdir, "stats"); STATS_CURRENT_LOG);
dlen = snprintf(dir, PATH_MAX, "%s/%s", outdir, "stats");
break; break;
case MODE_VMEVENT: case MODE_VMEVENT:
sprintf(path, "%s/%s", outdir, VM_CURRENT_LOG); plen = snprintf(path, PATH_MAX, "%s/%s", outdir,
sprintf(dir, "%s/%s", outdir, "vmevent"); VM_CURRENT_LOG);
dlen = snprintf(dir, PATH_MAX, "%s/%s", outdir, "vmevent");
break; break;
default: default:
LOGW("Invalid mode %d\n", mode); LOGW("Invalid mode %d\n", mode);
return -1; return -1;
} }
if (s_not_expect(plen, PATH_MAX) || s_not_expect(dlen, PATH_MAX)) {
LOGE("the length of path/dir is too long\n");
return -1;
}
/* Read current value in file */ /* Read current value in file */
res = file_read_int(path, current); res = file_read_int(path, current);
if (res < 0) if (res < 0)