Skip to content

Commit 43b2f30

Browse files
author
Erik Karlsson
committed
in_kmsg: fix /dev/kmsg parsing
* use strtoul for priority since it is unsigned * check for overflow correctly by comparing against ULONG_MAX * fail if no digits were consumed or next character is not comma * calculate tv.tv_usec correctly in case tv.tv_sec 32-bit * remove redundant errno = 0 added in 3254b9a * remove erroneous time.h added with 72d9dc8 Signed-off-by: Erik Karlsson <[email protected]>
1 parent 1132279 commit 43b2f30

File tree

1 file changed

+26
-15
lines changed

1 file changed

+26
-15
lines changed

plugins/in_kmsg/in_kmsg.c

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@
3636
#include <sys/stat.h>
3737
#include <sys/time.h>
3838
#include <inttypes.h>
39-
#include <time.h>
4039

4140
#include "in_kmsg.h"
4241

@@ -114,7 +113,6 @@ static inline int process_line(const char *line,
114113
struct timeval tv; /* time value */
115114
int line_len;
116115
uint64_t val;
117-
long pri_val;
118116
const char *p = line;
119117
char *end = NULL;
120118
struct flb_time ts;
@@ -124,34 +122,41 @@ static inline int process_line(const char *line,
124122
ctx->buffer_id++;
125123

126124
errno = 0;
127-
pri_val = strtol(p, &end, 10);
128-
if ((errno == ERANGE && (pri_val == INT_MAX || pri_val == INT_MIN))
129-
|| (errno != 0 && pri_val == 0)) {
125+
val = strtoul(p, &end, 10);
126+
if ((errno == ERANGE && val == ULONG_MAX)
127+
|| (errno != 0 && val == 0)) {
128+
goto fail;
129+
}
130+
131+
/* ensure something was consumed */
132+
/* after facility/priority, the next char must be ',' */
133+
if (end == p || *end != ',') {
130134
goto fail;
131135
}
132136

133137
/* Priority */
134-
priority = FLB_KLOG_PRI(pri_val);
138+
priority = FLB_KLOG_PRI(val);
135139

136140
if (priority > ctx->prio_level) {
137141
/* Drop line */
138142
return 0;
139143
}
140144

141145
/* Sequence */
142-
p = strchr(p, ',');
143-
if (!p) {
144-
goto fail;
145-
}
146-
p++;
146+
p = ++end;
147147

148-
errno = 0;
149148
val = strtoull(p, &end, 10);
150149
if ((errno == ERANGE && val == ULLONG_MAX)
151150
|| (errno != 0 && val == 0)) {
152151
goto fail;
153152
}
154153

154+
/* make sure strtoull consumed something */
155+
/* after the sequence number, the next char must be ',' */
156+
if (end == p || *end != ',') {
157+
goto fail;
158+
}
159+
155160
sequence = val;
156161
p = ++end;
157162

@@ -162,13 +167,19 @@ static inline int process_line(const char *line,
162167
goto fail;
163168
}
164169

165-
tv.tv_sec = val/1000000;
166-
tv.tv_usec = val - (tv.tv_sec * 1000000);
170+
/* ensure something was consumed */
171+
/* after the timestamp, the next char must be ',' */
172+
if (end == p || *end != ',') {
173+
goto fail;
174+
}
175+
176+
tv.tv_sec = val / KMSG_USEC_PER_SEC;
177+
tv.tv_usec = val % KMSG_USEC_PER_SEC;
167178

168179
flb_time_set(&ts, ctx->boot_time.tv_sec + tv.tv_sec, tv.tv_usec * 1000);
169180

170181
/* Now process the human readable message */
171-
p = strchr(p, ';');
182+
p = strchr(end, ';');
172183
if (!p) {
173184
goto fail;
174185
}

0 commit comments

Comments
 (0)