Skip to content

Commit 876cc68

Browse files
lauxinwlijinxia
authored andcommitted
tools:acrn-crashlog: Change the algorithm of generating event key
Acrnprobe is using SHA to generate ids for events. These ids are only used to index events, not for cryptographic purpose. This patch unify the generating algorithm of short and long ids to SHA256. Tracked-On: #1024 Signed-off-by: Liu, Xinwu <xinwu.liu@intel.com> Reviewed-by: Zhi Jin <zhi.jin@intel.com> Acked-by: Chen Gang <gang.c.chen@intel.com>
1 parent b1ba12a commit 876cc68

File tree

5 files changed

+71
-155
lines changed

5 files changed

+71
-155
lines changed

tools/acrn-crashlog/acrnprobe/history.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,8 @@ void hist_raise_uptime(char *lastuptime)
192192
if (hours / uptime_hours >= loop_uptime_event) {
193193
loop_uptime_event = (hours / uptime_hours) + 1;
194194

195-
key = generate_event_id(uptime->name, "");
195+
key = generate_event_id((const char *)uptime->name,
196+
NULL, KEY_SHORT);
196197
if (key == NULL) {
197198
LOGE("generate event id failed, error (%s)\n",
198199
strerror(errno));
@@ -210,7 +211,7 @@ void hist_raise_infoerror(char *type)
210211
{
211212
char *key;
212213

213-
key = generate_event_id("ERROR", type);
214+
key = generate_event_id("ERROR", (const char *)type, KEY_SHORT);
214215
if (key == NULL) {
215216
LOGE("generate event id failed, error (%s)\n",
216217
strerror(errno));

tools/acrn-crashlog/acrnprobe/include/load_conf.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <openssl/sha.h>
1212
#include <ext2fs/ext2fs.h>
1313
#include "event_queue.h"
14+
#include "probeutils.h"
1415

1516
#define CONTENT_MAX 10
1617
#define EXPRESSION_MAX 5
@@ -38,7 +39,7 @@ struct vm_t {
3839
ext2_filsys datafs;
3940
unsigned long history_size[SENDER_MAX];
4041
char *history_data;
41-
char last_synced_line_key[SENDER_MAX][SHA_DIGEST_LENGTH + 1];
42+
char last_synced_line_key[SENDER_MAX][SHORT_KEY_LENGTH + 1];
4243
};
4344

4445
struct log_t {

tools/acrn-crashlog/acrnprobe/include/probeutils.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,18 +24,25 @@
2424

2525
#define UPTIME_SIZE 24
2626
#define LONG_TIME_SIZE 32
27+
#define SHORT_KEY_LENGTH 20
28+
#define LONG_KEY_LENGTH 32
2729

2830
enum e_dir_mode {
2931
MODE_CRASH = 0,
3032
MODE_STATS,
3133
MODE_VMEVENT,
3234
};
3335

36+
enum key_type {
37+
KEY_SHORT = 0,
38+
KEY_LONG,
39+
};
40+
3441
int get_uptime_string(char newuptime[24], int *hours);
3542
int get_current_time_long(char buf[32]);
3643
unsigned long long get_uptime(void);
37-
char *generate_event_id(char *seed1, char *seed2);
38-
char *generate_eventid256(char *seed);
44+
char *generate_event_id(const char *seed1, const char *seed2,
45+
enum key_type type);
3946
void generate_crashfile(char *dir, char *event, char *hashkey,
4047
char *type, char *data0,
4148
char *data1, char *data2);

tools/acrn-crashlog/acrnprobe/probeutils.c

Lines changed: 48 additions & 142 deletions
Original file line numberDiff line numberDiff line change
@@ -94,129 +94,30 @@ int get_current_time_long(char *buf)
9494
return strftime(buf, LONG_TIME_SIZE, "%Y-%m-%d/%H:%M:%S ", time_val);
9595
}
9696

97-
/**
98-
* Compute a key with 20 characters
99-
*
100-
* @param[out] key The result key.
101-
* @param seed1 Seed.
102-
* @param seed2 Seed.
103-
*
104-
* @return 0 if successful, or -1 if not.
105-
*/
106-
static int compute_key(char *key, char *seed1, char *seed2)
107-
{
108-
static SHA_CTX *sha;
109-
char buf[VERSION_SIZE] = {'\0',};
110-
long long time_ns = 0;
111-
char *tmp_key = key;
112-
unsigned char results[SHA_DIGEST_LENGTH];
113-
int i;
114-
int ret;
115-
116-
if (sha == NULL) {
117-
sha = (SHA_CTX *)malloc(sizeof(SHA_CTX));
118-
if (sha == NULL) {
119-
LOGE("cannot create SHA_CTX memory...\n");
120-
return -1;
121-
}
122-
123-
ret = SHA1_Init(sha);
124-
if (ret != 1) {
125-
LOGE("SHA1_Init failed, error (%s)\n",
126-
strerror(errno));
127-
free(sha);
128-
sha = NULL;
129-
return -1;
130-
}
131-
}
132-
133-
if (!key || !seed1 || !seed2)
134-
return -1;
135-
136-
time_ns = get_uptime();
137-
snprintf(buf, VERSION_SIZE, "%s%s%s%s%lld", gbuildversion, guuid, seed1,
138-
seed2, time_ns);
139-
140-
ret = SHA1_Update(sha, (unsigned char *)buf, strlen(buf));
141-
if (ret != 1) {
142-
LOGE("SHA1_Update failed, error (%s)\n",
143-
strerror(errno));
144-
return -1;
145-
}
146-
147-
ret = SHA1_Final(results, sha);
148-
if (ret != 1) {
149-
LOGE("SHA1_Final failed, error (%s)\n",
150-
strerror(errno));
151-
return -1;
152-
}
153-
154-
for (i = 0; i < SHA_DIGEST_LENGTH / 2; i++) {
155-
sprintf(tmp_key, "%02x", results[i]);
156-
tmp_key += 2;
157-
}
158-
*tmp_key = 0;
159-
160-
return 0;
161-
}
162-
163-
/**
164-
* Compute a key with 32 characters
165-
*
166-
* @param[out] key The result key.
167-
* @param seed Seed.
168-
*
169-
* @return 0 if successful, or -1 if not.
170-
*/
171-
static int compute_key256(char *key, char *seed)
97+
static int compute_key(char *key, size_t key_len, const char *seed)
17298
{
173-
static SHA256_CTX *sha;
174-
char buf[VERSION_SIZE] = {'\0',};
175-
long long time_ns = 0;
99+
SHA256_CTX sha;
100+
char buf[VERSION_SIZE];
101+
long long time_ns;
176102
char *tmp_key = key;
177103
unsigned char results[SHA256_DIGEST_LENGTH];
178-
int i;
179-
int ret;
180-
181-
if (sha == NULL) {
182-
sha = (SHA256_CTX *)malloc(sizeof(SHA256_CTX));
183-
if (sha == NULL) {
184-
LOGE("cannot create SHA256_CTX memory...\n");
185-
return -1;
186-
}
187-
188-
ret = SHA256_Init(sha);
189-
if (ret != 1) {
190-
LOGE("SHA256_Init failed, error (%s)\n",
191-
strerror(errno));
192-
free(sha);
193-
sha = NULL;
194-
return -1;
195-
}
196-
}
104+
size_t i;
197105

198106
if (!key || !seed)
199107
return -1;
108+
if (key_len > SHA256_DIGEST_LENGTH * 2 || !key_len)
109+
return -1;
200110

111+
SHA256_Init(&sha);
201112
time_ns = get_uptime();
202-
snprintf(buf, VERSION_SIZE, "%s%s%s%lld", gbuildversion, guuid, seed,
203-
time_ns);
113+
snprintf(buf, VERSION_SIZE, "%s%s%lld", gbuildversion, guuid, time_ns);
204114

205-
ret = SHA256_Update(sha, (unsigned char *)buf, strlen(buf));
206-
if (ret != 1) {
207-
LOGE("SHA256_Update failed, error (%s)\n",
208-
strerror(errno));
209-
return -1;
210-
}
115+
SHA256_Update(&sha, (unsigned char *)buf, strlen(buf));
116+
SHA256_Update(&sha, (unsigned char *)seed, strlen(seed));
211117

212-
ret = SHA256_Final(results, sha);
213-
if (ret != 1) {
214-
LOGE("SHA256_Final failed, error (%s)\n",
215-
strerror(errno));
216-
return -1;
217-
}
118+
SHA256_Final(results, &sha);
218119

219-
for (i = 0; i < SHA256_DIGEST_LENGTH / 2; i++) {
120+
for (i = 0; i < key_len / 2; i++) {
220121
sprintf(tmp_key, "%02x", results[i]);
221122
tmp_key += 2;
222123
}
@@ -226,49 +127,54 @@ static int compute_key256(char *key, char *seed)
226127
}
227128

228129
/**
229-
* Generate a event id with 20 characters
130+
* Generate an event id with specified type.
230131
*
231-
* @param seed1 Seed.
232-
* @param seed2 Seed.
132+
* @param seed1 Seed1.
133+
* @param seed2 Seed2, this parameter will be ignored if the value is NULL.
134+
* @param type The type of key. The length of generated id will be 20
135+
* characters if type is KEY_SHORT; 32 characters if type is
136+
* KEY_LONG.
233137
*
234138
* @return a pointer to result haskkey if successful, or NULL if not.
235139
*/
236-
char *generate_event_id(char *seed1, char *seed2)
140+
char *generate_event_id(const char *seed1, const char *seed2,
141+
enum key_type type)
237142
{
238143
int ret;
239-
char *key = (char *)malloc(SHA_DIGEST_LENGTH + 1);
144+
char *buf;
145+
char *key;
146+
size_t klen;
240147

241-
if (!key)
148+
if (!seed1)
242149
return NULL;
243150

244-
ret = compute_key(key, seed1, seed2);
245-
if (ret < 0) {
246-
LOGE("compute_key error\n");
247-
free(key);
248-
key = NULL;
249-
}
250-
251-
return key;
252-
}
253-
254-
/**
255-
* Generate a event id with 32 characters
256-
*
257-
* @param seed Seed.
258-
*
259-
* @return a pointer to result haskkey if successful, or NULL if not.
260-
*/
261-
char *generate_eventid256(char *seed)
262-
{
263-
int ret;
264-
char *key = (char *)malloc(SHA256_DIGEST_LENGTH + 1);
151+
if (type == KEY_SHORT)
152+
klen = SHORT_KEY_LENGTH;
153+
else if (type == KEY_LONG)
154+
klen = LONG_KEY_LENGTH;
155+
else
156+
return NULL;
265157

266-
if (!key)
158+
key = (char *)malloc(klen + 1);
159+
if (!key) {
160+
LOGE("failed to generate event id, out of memory\n");
267161
return NULL;
162+
}
163+
164+
if (seed2) {
165+
if (asprintf(&buf, "%s%s", seed1, seed2) == -1) {
166+
LOGE("failed to generate event id, out of memory\n");
167+
free(key);
168+
return NULL;
169+
}
170+
ret = compute_key(key, klen, (const char *)buf);
171+
free(buf);
172+
} else {
173+
ret = compute_key(key, klen, seed1);
174+
}
268175

269-
ret = compute_key256(key, seed);
270176
if (ret < 0) {
271-
LOGE("compute_key256 error\n");
177+
LOGE("compute_key error\n");
272178
free(key);
273179
key = NULL;
274180
}

tools/acrn-crashlog/acrnprobe/sender.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ static void telemd_send_crash(struct event_t *e)
357357
return;
358358
}
359359

360-
eventid = generate_eventid256(class);
360+
eventid = generate_event_id((const char *)class, NULL, KEY_LONG);
361361
if (eventid == NULL) {
362362
LOGE("generate eventid failed, error (%s)\n", strerror(errno));
363363
goto free_class;
@@ -436,7 +436,7 @@ static void telemd_send_info(struct event_t *e)
436436
return;
437437
}
438438

439-
eventid = generate_eventid256(class);
439+
eventid = generate_event_id((const char *)class, NULL, KEY_LONG);
440440
if (eventid == NULL) {
441441
LOGE("generate eventid failed, error (%s)\n", strerror(errno));
442442
goto free_class;
@@ -608,7 +608,7 @@ static int telemd_new_vmevent(const char *line_to_sync,
608608
goto free_vmlogpath;
609609
}
610610

611-
eventid = generate_eventid256(class);
611+
eventid = generate_event_id((const char *)class, NULL, KEY_LONG);
612612
if (eventid == NULL) {
613613
LOGE("generate eventid failed, error (%s)\n", strerror(errno));
614614
ret = VMEVT_DEFER;
@@ -739,7 +739,7 @@ static void crashlog_send_crash(struct event_t *e)
739739

740740
/* change the class for other senders */
741741
e->private = (void *)crash;
742-
key = generate_event_id("CRASH", crash->name);
742+
key = generate_event_id("CRASH", (const char *)crash->name, KEY_SHORT);
743743
if (key == NULL) {
744744
LOGE("generate event id failed, error (%s)\n",
745745
strerror(errno));
@@ -818,7 +818,8 @@ static void crashlog_send_info(struct event_t *e)
818818
int id;
819819
struct info_t *info = (struct info_t *)e->private;
820820
struct log_t *log;
821-
char *key = generate_event_id("INFO", info->name);
821+
char *key = generate_event_id("INFO", (const char *)info->name,
822+
KEY_SHORT);
822823

823824
if (key == NULL) {
824825
LOGE("generate event id failed, error (%s)\n",
@@ -862,7 +863,7 @@ static void crashlog_send_reboot(void)
862863
return;
863864

864865
if (swupdated(crashlog)) {
865-
key = generate_event_id("INFO", "SWUPDATE");
866+
key = generate_event_id("INFO", "SWUPDATE", KEY_SHORT);
866867
if (key == NULL) {
867868
LOGE("generate event id failed, error (%s)\n",
868869
strerror(errno));
@@ -874,7 +875,7 @@ static void crashlog_send_reboot(void)
874875
}
875876

876877
read_startupreason(reason, sizeof(reason));
877-
key = generate_event_id("REBOOT", reason);
878+
key = generate_event_id("REBOOT", (const char *)reason, KEY_SHORT);
878879
if (key == NULL) {
879880
LOGE("generate event id failed, error (%s)\n",
880881
strerror(errno));
@@ -931,7 +932,7 @@ static int crashlog_new_vmevent(const char *line_to_sync,
931932
return ret;
932933
}
933934

934-
key = generate_event_id("SOS", vmkey);
935+
key = generate_event_id("SOS", (const char *)vmkey, KEY_SHORT);
935936
if (key == NULL) {
936937
LOGE("generate event id failed, error (%s)\n",
937938
strerror(errno));

0 commit comments

Comments
 (0)