From 87a4abdd9d45cf29389f37a478acc943048bd987 Mon Sep 17 00:00:00 2001 From: CHEN Gang Date: Mon, 30 Jul 2018 09:43:08 +0800 Subject: [PATCH] tools: acrn-crashlog: fix build warnings with gcc8.1.1 This patch is to fix the build warning with gcc8.1.1. Most of them are warnings for buffer overflow from snprintf and strncpy. Signed-off-by: CHEN Gang Reviewed-by: Zhi Jin Reviewed-by: Liu, Xinwu Reviewed-by: xiaojin2 --- tools/acrn-crashlog/acrnprobe/history.c | 6 ++--- .../acrnprobe/include/property.h | 10 +++++--- tools/acrn-crashlog/acrnprobe/probeutils.c | 23 +++++++++++-------- tools/acrn-crashlog/acrnprobe/property.c | 9 ++++---- tools/acrn-crashlog/common/include/fsutils.h | 2 +- tools/acrn-crashlog/usercrash/crash_dump.c | 4 +++- tools/acrn-crashlog/usercrash/protocol.c | 1 + 7 files changed, 33 insertions(+), 22 deletions(-) diff --git a/tools/acrn-crashlog/acrnprobe/history.c b/tools/acrn-crashlog/acrnprobe/history.c index f0e099f6c..005d916fc 100644 --- a/tools/acrn-crashlog/acrnprobe/history.c +++ b/tools/acrn-crashlog/acrnprobe/history.c @@ -56,10 +56,10 @@ static void entry_to_history_line(struct history_entry *entry, newline[0] = 0; if (entry->log != NULL) { char *ptr; - char tmp[MAXLINESIZE]; + char tmp[PATH_MAX]; - strncpy(tmp, entry->log, MAXLINESIZE); - tmp[MAXLINESIZE-1] = 0; + strncpy(tmp, entry->log, PATH_MAX); + tmp[PATH_MAX - 1] = 0; ptr = strrchr(tmp, '/'); if (ptr && ptr[1] == 0) ptr[0] = 0; diff --git a/tools/acrn-crashlog/acrnprobe/include/property.h b/tools/acrn-crashlog/acrnprobe/include/property.h index 379b44152..c61ca8123 100644 --- a/tools/acrn-crashlog/acrnprobe/include/property.h +++ b/tools/acrn-crashlog/acrnprobe/include/property.h @@ -23,10 +23,14 @@ #define __PROPERTY_H__ #include "load_conf.h" -#define VERSION_SIZE 256 +#define VERSION_SIZE 256 +/* UUID_SIZE contains the UUID number, dashes and some buffer*/ +#define UUID_SIZE 48 +/* General BUILD_VERSION like 23690 */ +#define BUILD_VERSION_SIZE 16 -char guuid[VERSION_SIZE]; -char gbuildversion[VERSION_SIZE]; +char guuid[UUID_SIZE]; +char gbuildversion[BUILD_VERSION_SIZE]; int init_properties(struct sender_t *sender); int swupdated(struct sender_t *sender); diff --git a/tools/acrn-crashlog/acrnprobe/probeutils.c b/tools/acrn-crashlog/acrnprobe/probeutils.c index 3f0ffedae..1639bffba 100644 --- a/tools/acrn-crashlog/acrnprobe/probeutils.c +++ b/tools/acrn-crashlog/acrnprobe/probeutils.c @@ -106,7 +106,7 @@ int get_current_time_long(char *buf) static int compute_key(char *key, char *seed1, char *seed2) { static SHA_CTX *sha; - char buf[256] = {'\0',}; + char buf[VERSION_SIZE] = {'\0',}; long long time_ns = 0; char *tmp_key = key; unsigned char results[SHA_DIGEST_LENGTH]; @@ -134,7 +134,7 @@ static int compute_key(char *key, char *seed1, char *seed2) return -1; time_ns = get_uptime(); - snprintf(buf, 256, "%s%s%s%s%lld", gbuildversion, guuid, seed1, + snprintf(buf, VERSION_SIZE, "%s%s%s%s%lld", gbuildversion, guuid, seed1, seed2, time_ns); ret = SHA1_Update(sha, (unsigned char *)buf, strlen(buf)); @@ -171,7 +171,7 @@ static int compute_key(char *key, char *seed1, char *seed2) static int compute_key256(char *key, char *seed) { static SHA256_CTX *sha; - char buf[256] = {'\0',}; + char buf[VERSION_SIZE] = {'\0',}; long long time_ns = 0; char *tmp_key = key; unsigned char results[SHA256_DIGEST_LENGTH]; @@ -199,7 +199,7 @@ static int compute_key256(char *key, char *seed) return -1; time_ns = get_uptime(); - snprintf(buf, 256, "%s%s%s%lld", gbuildversion, guuid, seed, + snprintf(buf, VERSION_SIZE, "%s%s%s%lld", gbuildversion, guuid, seed, time_ns); ret = SHA256_Update(sha, (unsigned char *)buf, strlen(buf)); @@ -430,7 +430,7 @@ void generate_crashfile(char *dir, char *event, char *hashkey, */ char *generate_log_dir(enum e_dir_mode mode, char *hashkey) { - char path[PATH_MAX]; + char *path; char dir[PATH_MAX]; unsigned int current; int ret; @@ -439,19 +439,22 @@ char *generate_log_dir(enum e_dir_mode mode, char *hashkey) if (ret) return NULL; - snprintf(path, sizeof(path), "%s%d_", dir, current); - strncat(path, hashkey, - (strlen(path) + strlen(hashkey) >= sizeof(path)) ? - (sizeof(path) - strlen(path)) : strlen(hashkey)); + ret = asprintf(&path, "%s%d_%s", dir, current, hashkey); + if (ret == -1) { + LOGE("construct log path failed, out of memory\n"); + hist_raise_infoerror("DIR CREATE"); + return NULL; + } ret = mkdir(path, 0777); if (ret == -1) { LOGE("Cannot create dir %s\n", path); hist_raise_infoerror("DIR CREATE"); + free(path); return NULL; } - return strdup(path); + return path; } int is_boot_id_changed(void) diff --git a/tools/acrn-crashlog/acrnprobe/property.c b/tools/acrn-crashlog/acrnprobe/property.c index 27224dd2c..b1b4d5891 100644 --- a/tools/acrn-crashlog/acrnprobe/property.c +++ b/tools/acrn-crashlog/acrnprobe/property.c @@ -47,7 +47,7 @@ static void get_device_id(struct sender_t *sender) return; } - ret = file_read_string(MACHINE_ID, guuid, VERSION_SIZE); + ret = file_read_string(MACHINE_ID, guuid, BUILD_VERSION_SIZE); if (ret <= 0) LOGE("Could not get mmc id: %d (%s)\n", ret, strerror(-ret)); @@ -56,7 +56,8 @@ static void get_device_id(struct sender_t *sender) LOGE("Could not find DeviceId, set it to '%s'\n", DEVICE_ID_UNKNOWN); - strncpy(guuid, DEVICE_ID_UNKNOWN, strlen(DEVICE_ID_UNKNOWN)); + strncpy(guuid, DEVICE_ID_UNKNOWN, UUID_SIZE); + guuid[UUID_SIZE - 1] = '\0'; write: overwrite_file(loguuid, guuid); @@ -66,7 +67,7 @@ write: static int get_buildversion(struct sender_t *sender) { int ret; - char lastbuild[VERSION_SIZE]; + char lastbuild[BUILD_VERSION_SIZE]; char *logbuildid; char *currentbuild = gbuildversion; @@ -84,7 +85,7 @@ static int get_buildversion(struct sender_t *sender) return ret; } - ret = file_read_string(logbuildid, lastbuild, VERSION_SIZE); + ret = file_read_string(logbuildid, lastbuild, BUILD_VERSION_SIZE); if (ret == -ENOENT || !ret || (ret > 0 && strcmp(currentbuild, lastbuild))) { diff --git a/tools/acrn-crashlog/common/include/fsutils.h b/tools/acrn-crashlog/common/include/fsutils.h index 160aea0d4..c5ba5fa71 100644 --- a/tools/acrn-crashlog/common/include/fsutils.h +++ b/tools/acrn-crashlog/common/include/fsutils.h @@ -32,7 +32,7 @@ #define KB (1024) #define MB (KB * KB) -#define MAXLINESIZE (4 * KB) +#define MAXLINESIZE (PATH_MAX + 128) #define CPBUFFERSIZE (4 * KB) #define PAGE_SIZE (4 * KB) diff --git a/tools/acrn-crashlog/usercrash/crash_dump.c b/tools/acrn-crashlog/usercrash/crash_dump.c index 1cfac635a..79cd47c32 100644 --- a/tools/acrn-crashlog/usercrash/crash_dump.c +++ b/tools/acrn-crashlog/usercrash/crash_dump.c @@ -24,6 +24,8 @@ #define DUMP_FILE "/tmp/core" #define BUFFER_SIZE 8196 #define LINK_LEN 512 +/* 128 means the length of the DUMP_FILE */ +#define FORMAT_LENGTH (LINK_LEN + 128) static void loginfo(int fd, const char *fmt, ...) { @@ -110,7 +112,7 @@ static int save_coredump(const char *filename) static int get_backtrace(int pid, int fd, int sig, const char *comm) { char *membkt; - char format[512]; + char format[FORMAT_LENGTH]; loginfo(fd, "\nBackTrace:\n\n"); memset(format, 0, sizeof(format)); diff --git a/tools/acrn-crashlog/usercrash/protocol.c b/tools/acrn-crashlog/usercrash/protocol.c index 58110fe07..8dcfdc199 100644 --- a/tools/acrn-crashlog/usercrash/protocol.c +++ b/tools/acrn-crashlog/usercrash/protocol.c @@ -112,6 +112,7 @@ static int socket_bind(int fd, const char *name) if (name_len >= SUN_PATH_MAX) return -1; strncpy(addr.sun_path, name, SUN_PATH_MAX); + addr.sun_path[SUN_PATH_MAX - 1] = '\0'; unlink(addr.sun_path); alen = strlen(addr.sun_path) + sizeof(addr.sun_family);