Skip to content

Commit d113fa1

Browse files
cosmo0920edsiper
authored andcommitted
storage: fix heap corruption in chunk sort parser
Signed-off-by: Hiroshi Hatake <[email protected]>
1 parent 2b16b24 commit d113fa1

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

include/fluent-bit/flb_storage.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
#include <chunkio/chunkio.h>
2525
#include <chunkio/cio_stats.h>
2626

27+
#include <inttypes.h>
28+
2729
/* Storage type */
2830
#define FLB_STORAGE_FS CIO_STORE_FS /* 0 */
2931
#define FLB_STORAGE_MEM CIO_STORE_MEM /* 1 */

src/flb_storage.c

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -390,30 +390,44 @@ struct flb_storage_metrics *flb_storage_metrics_create(struct flb_config *ctx)
390390
return sm;
391391
}
392392

393+
static int parse_stamp(const char *p, time_t *sec, long *nsec)
394+
{
395+
uint64_t s = 0, ns = 0;
396+
397+
if (!p || !sec || !nsec) {
398+
return -1;
399+
}
400+
/* expected: "1234567890.123456789.flb" format */
401+
if (sscanf(p, "%" SCNu64 ".%" SCNu64 ".flb", &s, &ns) != 2) {
402+
return -1;
403+
}
404+
if (ns >= 1000000000ULL) {
405+
return -1;
406+
}
407+
408+
*sec = (time_t)s;
409+
*nsec = (long)ns;
410+
return 0;
411+
}
412+
393413
static int sort_chunk_cmp(const void *a_arg, const void *b_arg)
394414
{
395-
char *p;
396415
struct cio_chunk *chunk_a = *(struct cio_chunk **) a_arg;
397416
struct cio_chunk *chunk_b = *(struct cio_chunk **) b_arg;
398-
struct timespec tm_a;
399-
struct timespec tm_b;
417+
const char *p;
418+
struct timespec tm_a = {0}, tm_b = {0};
400419

401420
/* Scan Chunk A */
402421
p = strchr(chunk_a->name, '-');
403-
if (!p) {
422+
if (!p || parse_stamp(p + 1, &tm_a.tv_sec, &tm_a.tv_nsec) != 0) {
404423
return -1;
405424
}
406-
p++;
407-
408-
sscanf(p, "%lu.%lu.flb", &tm_a.tv_sec, &tm_a.tv_nsec);
409425

410426
/* Scan Chunk B */
411427
p = strchr(chunk_b->name, '-');
412-
if (!p) {
428+
if (!p || parse_stamp(p + 1, &tm_b.tv_sec, &tm_b.tv_nsec) != 0) {
413429
return -1;
414430
}
415-
p++;
416-
sscanf(p, "%lu.%lu.flb", &tm_b.tv_sec, &tm_b.tv_nsec);
417431

418432
/* Compare */
419433
if (tm_a.tv_sec != tm_b.tv_sec) {

0 commit comments

Comments
 (0)