tools: acrn-crashlog: fix potential issues

Changes include:
1. check the parameter of snprintf
2. remove atoi
3. remove sscanf
4. fix one memleak

Tracked-On: #1024
Signed-off-by: Liu, Xinwu <xinwu.liu@intel.com>
Reviewed-by: Huang, Yonghua <yonghua.huang@intel.com>
Acked-by: Chen, Gang <gang.c.chen@intel.com>
This commit is contained in:
Liu, Xinwu
2018-10-30 17:26:44 +08:00
committed by lijinxia
parent 111f9726d0
commit 3ffa9686ca
13 changed files with 236 additions and 118 deletions

View File

@@ -119,18 +119,24 @@ static int get_backtrace(int pid, int fd, int sig, const char *comm)
char *membkt;
char format[FORMAT_LENGTH];
size_t len, ret;
int flen;
loginfo(fd, "\nBackTrace:\n\n");
memset(format, 0, sizeof(format));
if (sig == DEBUGGER_SIGNAL) {
snprintf(format, sizeof(format), "-p %d", pid);
flen = snprintf(format, sizeof(format), "-p %d", pid);
} else {
snprintf(format, sizeof(format), "%s %s", comm, DUMP_FILE);
flen = snprintf(format, sizeof(format), "%s %s", comm,
DUMP_FILE);
if (save_coredump(DUMP_FILE) == -1) {
LOGE("save core file failed\n");
return -1;
}
}
if (s_not_expect(flen, sizeof(format))) {
LOGE("failed to generate format\n");
return -1;
}
len = exec_out2mem(&membkt, GET_GDB_INFO, format);
if (len <= 0) {
LOGE("get gdb info failed\n");
@@ -163,7 +169,11 @@ static int save_proc_info(int pid, int fd, const char *path, const char *name)
loginfo(fd, "\n%s:\n\n", name);
memset(format, 0, sizeof(format));
snprintf(format, sizeof(format), path, pid);
ret = snprintf(format, sizeof(format), path, pid);
if (s_not_expect(ret, sizeof(format))) {
LOGE("failed to generate format");
return -1;
}
ret = read_file(format, &size, (void *)&data);
if (ret) {
LOGE("read file failed\n");
@@ -191,7 +201,11 @@ static int get_openfiles(int pid, int fd, const char *path, const char *name)
loginfo(fd, "\n%s:\n\n", name);
memset(format, 0, sizeof(format));
snprintf(format, sizeof(format), path, pid);
ret = snprintf(format, sizeof(format), path, pid);
if (s_not_expect(ret, sizeof(format))) {
LOGE("failed to generate format");
return -1;
}
fdcount = lsdir(format, files, ARRAY_SIZE(files));
if (fdcount < 0) {
LOGE("get fd list failed\n");
@@ -263,7 +277,11 @@ static int get_key_value(int pid, const char *path, const char *key,
char format[128];
memset(format, 0, sizeof(format));
snprintf(format, sizeof(format), path, pid);
ret = snprintf(format, sizeof(format), path, pid);
if (s_not_expect(ret, sizeof(format))) {
LOGE("failed to generate format");
return -1;
}
ret = read_file(format, &size, (void *)&data);
if (ret || !data) {
LOGE("read file failed\n");
@@ -306,7 +324,11 @@ void crash_dump(int pid, int sig, int out_fd)
char format[128];
memset(format, 0, sizeof(format));
snprintf(format, sizeof(format), GET_COMM, pid);
ret = snprintf(format, sizeof(format), GET_COMM, pid);
if (s_not_expect(ret, sizeof(format))) {
LOGE("failed to generate format\n");
return;
}
ret = readlink(format, comm, LINK_LEN);
if (ret < 0 || ret >= LINK_LEN) {
LOGE("get process exe link failed\n");

View File

@@ -110,7 +110,7 @@ static int socket_bind(int fd, const char *name)
name_len = strnlen(name, SOCKET_PATH_MAX);
if (name_len >= SUN_PATH_MAX)
return -1;
strncpy(addr.sun_path, name, name_len + 1);
*(char *)mempcpy(addr.sun_path, name, name_len) = '\0';
unlink(addr.sun_path);
alen = strnlen(addr.sun_path, SUN_PATH_MAX) + sizeof(addr.sun_family);

View File

@@ -132,6 +132,7 @@ static struct crash_node *pop_front(void)
static void find_oldest_usercrash(void)
{
int i;
int len;
int oldest_usercrash = 0;
time_t oldest_time = LONG_MAX;
char path[FILE_PATH_LEN_MAX];
@@ -139,8 +140,12 @@ static void find_oldest_usercrash(void)
memset(path, 0, FILE_PATH_LEN_MAX);
for (i = 0; i < usercrash_count; ++i) {
snprintf(path, sizeof(path), "%s/usercrash_%02d",
len = snprintf(path, sizeof(path), "%s/usercrash_%02d",
usercrash_directory, i);
if (s_not_expect(len, sizeof(path))) {
LOGE("failed to generate path\n");
continue;
}
if (stat(path, &st) != 0) {
if (errno == ENOENT) {
oldest_usercrash = i;
@@ -168,11 +173,16 @@ static int get_usercrash(struct crash_node *crash)
* interleaving their output
*/
int result;
int len;
char file_name[FILE_PATH_LEN_MAX];
memset(file_name, 0, FILE_PATH_LEN_MAX);
snprintf(file_name, sizeof(file_name), "%s/usercrash_%02d",
len = snprintf(file_name, sizeof(file_name), "%s/usercrash_%02d",
usercrash_directory, next_usercrash);
if (s_not_expect(len, sizeof(file_name))) {
LOGE("failed to generate file name\n");
return -1;
}
if (unlink(file_name) != 0 && errno != ENOENT) {
LOGE("failed to unlink usercrash at %s\n", file_name);