33
33
#include "load_conf.h"
34
34
#include "log_sys.h"
35
35
#include "probeutils.h"
36
+ #include "strutils.h"
36
37
37
38
#define CRASH_CURRENT_LOG "currentcrashlog"
38
39
#define STATS_CURRENT_LOG "currentstatslog"
@@ -98,6 +99,7 @@ static int compute_key(char *key, size_t key_len, const char *seed)
98
99
{
99
100
SHA256_CTX sha ;
100
101
char buf [VERSION_SIZE ];
102
+ int len ;
101
103
long long time_ns ;
102
104
char * tmp_key = key ;
103
105
unsigned char results [SHA256_DIGEST_LENGTH ];
@@ -110,15 +112,20 @@ static int compute_key(char *key, size_t key_len, const char *seed)
110
112
111
113
SHA256_Init (& sha );
112
114
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 ;
114
119
115
120
SHA256_Update (& sha , (unsigned char * )buf , strlen (buf ));
116
121
SHA256_Update (& sha , (unsigned char * )seed , strlen (seed ));
117
122
118
123
SHA256_Final (results , & sha );
119
124
120
125
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 ;
122
129
tmp_key += 2 ;
123
130
}
124
131
* tmp_key = 0 ;
@@ -194,8 +201,10 @@ char *generate_event_id(const char *seed1, const char *seed2,
194
201
static int reserve_log_folder (enum e_dir_mode mode , char * dir ,
195
202
unsigned int * current )
196
203
{
197
- char path [512 ];
204
+ char path [PATH_MAX ];
198
205
int res ;
206
+ int plen ;
207
+ int dlen ;
199
208
struct sender_t * crashlog ;
200
209
char * outdir ;
201
210
unsigned int maxdirs ;
@@ -208,22 +217,29 @@ static int reserve_log_folder(enum e_dir_mode mode, char *dir,
208
217
209
218
switch (mode ) {
210
219
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" );
213
223
break ;
214
224
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" );
217
228
break ;
218
229
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" );
221
233
break ;
222
234
default :
223
235
LOGW ("Invalid mode %d\n" , mode );
224
236
return -1 ;
225
237
}
226
238
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
+ }
227
243
/* Read current value in file */
228
244
res = file_read_int (path , current );
229
245
if (res < 0 )
0 commit comments