Skip to content

Commit fb02928

Browse files
lauxinwNanlinXie
authored andcommitted
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>
1 parent 5ecf107 commit fb02928

File tree

2 files changed

+40
-12
lines changed

2 files changed

+40
-12
lines changed

tools/acrn-crashlog/acrnprobe/history.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "history.h"
3333
#include "log_sys.h"
3434
#include "probeutils.h"
35+
#include "strutils.h"
3536

3637
#define HISTORY_FIRST_LINE_FMT \
3738
"#V1.0 CURRENTUPTIME %-24s\n"
@@ -186,7 +187,12 @@ void hist_raise_uptime(char *lastuptime)
186187
return;
187188
}
188189

189-
sprintf(firstline, HISTORY_FIRST_LINE_FMT, boot_time);
190+
ret = snprintf(firstline, sizeof(firstline),
191+
HISTORY_FIRST_LINE_FMT, boot_time);
192+
if (s_not_expect(ret, sizeof(firstline))) {
193+
LOGE("failed to construct the firstline\n");
194+
return;
195+
}
190196
replace_file_head(history_file, firstline);
191197

192198
if (hours / uptime_hours >= loop_uptime_event) {
@@ -249,8 +255,9 @@ static int get_time_firstline(char *buffer)
249255
int prepare_history(void)
250256
{
251257
int ret;
258+
int llen;
252259
struct sender_t *crashlog;
253-
char linebuf[MAXLINESIZE] = {0};
260+
char linebuf[MAXLINESIZE];
254261

255262
crashlog = get_sender_by_name("crashlog");
256263
if (!crashlog)
@@ -272,7 +279,12 @@ int prepare_history(void)
272279
} else {
273280
/* new history */
274281
LOGW("new history\n");
275-
sprintf(linebuf, HISTORY_FIRST_LINE_FMT, "0000:00:00");
282+
llen = snprintf(linebuf, sizeof(linebuf),
283+
HISTORY_FIRST_LINE_FMT, "0000:00:00");
284+
if (s_not_expect(llen, sizeof(linebuf))) {
285+
LOGE("failed to construct the fristline\n");
286+
return -EINVAL;
287+
}
276288
ret = overwrite_file(history_file, linebuf);
277289
if (ret < 0) {
278290
LOGE("Write (%s, %s) failed, error (%s)\n",

tools/acrn-crashlog/acrnprobe/probeutils.c

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "load_conf.h"
3434
#include "log_sys.h"
3535
#include "probeutils.h"
36+
#include "strutils.h"
3637

3738
#define CRASH_CURRENT_LOG "currentcrashlog"
3839
#define STATS_CURRENT_LOG "currentstatslog"
@@ -98,6 +99,7 @@ static int compute_key(char *key, size_t key_len, const char *seed)
9899
{
99100
SHA256_CTX sha;
100101
char buf[VERSION_SIZE];
102+
int len;
101103
long long time_ns;
102104
char *tmp_key = key;
103105
unsigned char results[SHA256_DIGEST_LENGTH];
@@ -110,15 +112,20 @@ static int compute_key(char *key, size_t key_len, const char *seed)
110112

111113
SHA256_Init(&sha);
112114
time_ns = get_uptime();
113-
snprintf(buf, VERSION_SIZE, "%s%s%lld", gbuildversion, guuid, time_ns);
115+
len = snprintf(buf, VERSION_SIZE, "%s%s%lld",
116+
gbuildversion, guuid, time_ns);
117+
if (s_not_expect(len , VERSION_SIZE))
118+
return -1;
114119

115120
SHA256_Update(&sha, (unsigned char *)buf, strlen(buf));
116121
SHA256_Update(&sha, (unsigned char *)seed, strlen(seed));
117122

118123
SHA256_Final(results, &sha);
119124

120125
for (i = 0; i < key_len / 2; i++) {
121-
sprintf(tmp_key, "%02x", results[i]);
126+
len = snprintf(tmp_key, 3, "%02x", results[i]);
127+
if (s_not_expect(len, 3))
128+
return -1;
122129
tmp_key += 2;
123130
}
124131
*tmp_key = 0;
@@ -194,8 +201,10 @@ char *generate_event_id(const char *seed1, const char *seed2,
194201
static int reserve_log_folder(enum e_dir_mode mode, char *dir,
195202
unsigned int *current)
196203
{
197-
char path[512];
204+
char path[PATH_MAX];
198205
int res;
206+
int plen;
207+
int dlen;
199208
struct sender_t *crashlog;
200209
char *outdir;
201210
unsigned int maxdirs;
@@ -208,22 +217,29 @@ static int reserve_log_folder(enum e_dir_mode mode, char *dir,
208217

209218
switch (mode) {
210219
case MODE_CRASH:
211-
sprintf(path, "%s/%s", outdir, CRASH_CURRENT_LOG);
212-
sprintf(dir, "%s/%s", outdir, "crashlog");
220+
plen = snprintf(path, PATH_MAX, "%s/%s", outdir,
221+
CRASH_CURRENT_LOG);
222+
dlen = snprintf(dir, PATH_MAX, "%s/%s", outdir, "crashlog");
213223
break;
214224
case MODE_STATS:
215-
sprintf(path, "%s/%s", outdir, STATS_CURRENT_LOG);
216-
sprintf(dir, "%s/%s", outdir, "stats");
225+
plen = snprintf(path, PATH_MAX, "%s/%s", outdir,
226+
STATS_CURRENT_LOG);
227+
dlen = snprintf(dir, PATH_MAX, "%s/%s", outdir, "stats");
217228
break;
218229
case MODE_VMEVENT:
219-
sprintf(path, "%s/%s", outdir, VM_CURRENT_LOG);
220-
sprintf(dir, "%s/%s", outdir, "vmevent");
230+
plen = snprintf(path, PATH_MAX, "%s/%s", outdir,
231+
VM_CURRENT_LOG);
232+
dlen = snprintf(dir, PATH_MAX, "%s/%s", outdir, "vmevent");
221233
break;
222234
default:
223235
LOGW("Invalid mode %d\n", mode);
224236
return -1;
225237
}
226238

239+
if (s_not_expect(plen, PATH_MAX) || s_not_expect(dlen, PATH_MAX)) {
240+
LOGE("the length of path/dir is too long\n");
241+
return -1;
242+
}
227243
/* Read current value in file */
228244
res = file_read_int(path, current);
229245
if (res < 0)

0 commit comments

Comments
 (0)