Fix: unchecked buffer size for communication header
[lttng-tools.git] / src / common / event-rule / syscall.c
1 /*
2 * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
3 *
4 * SPDX-License-Identifier: LGPL-2.1-only
5 *
6 */
7
8 #include <assert.h>
9 #include <common/error.h>
10 #include <common/macros.h>
11 #include <common/payload.h>
12 #include <common/payload-view.h>
13 #include <common/runas.h>
14 #include <lttng/event-rule/event-rule-internal.h>
15 #include <lttng/event-rule/syscall-internal.h>
16
17 #define IS_SYSCALL_EVENT_RULE(rule) \
18 (lttng_event_rule_get_type(rule) == LTTNG_EVENT_RULE_TYPE_SYSCALL)
19
20 static void lttng_event_rule_syscall_destroy(struct lttng_event_rule *rule)
21 {
22 struct lttng_event_rule_syscall *syscall;
23
24 if (rule == NULL) {
25 return;
26 }
27
28 syscall = container_of(rule, struct lttng_event_rule_syscall, parent);
29
30 free(syscall->pattern);
31 free(syscall->filter_expression);
32 free(syscall->internal_filter.filter);
33 free(syscall->internal_filter.bytecode);
34 free(syscall);
35 }
36
37 static bool lttng_event_rule_syscall_validate(
38 const struct lttng_event_rule *rule)
39 {
40 bool valid = false;
41 struct lttng_event_rule_syscall *syscall;
42
43 if (!rule) {
44 goto end;
45 }
46
47 syscall = container_of(rule, struct lttng_event_rule_syscall, parent);
48
49 /* Required field. */
50 if (!syscall->pattern) {
51 ERR("Invalid syscall event rule: a pattern must be set.");
52 goto end;
53 }
54
55 valid = true;
56 end:
57 return valid;
58 }
59
60 static int lttng_event_rule_syscall_serialize(
61 const struct lttng_event_rule *rule,
62 struct lttng_payload *payload)
63 {
64 int ret;
65 size_t pattern_len, filter_expression_len;
66 struct lttng_event_rule_syscall *syscall;
67 struct lttng_event_rule_syscall_comm syscall_comm;
68
69 if (!rule || !IS_SYSCALL_EVENT_RULE(rule)) {
70 ret = -1;
71 goto end;
72 }
73
74 DBG("Serializing syscall event rule");
75 syscall = container_of(rule, struct lttng_event_rule_syscall, parent);
76
77 pattern_len = strlen(syscall->pattern) + 1;
78
79 if (syscall->filter_expression != NULL) {
80 filter_expression_len = strlen(syscall->filter_expression) + 1;
81 } else {
82 filter_expression_len = 0;
83 }
84
85 syscall_comm.pattern_len = pattern_len;
86 syscall_comm.filter_expression_len = filter_expression_len;
87
88 ret = lttng_dynamic_buffer_append(
89 &payload->buffer, &syscall_comm, sizeof(syscall_comm));
90 if (ret) {
91 goto end;
92 }
93
94 ret = lttng_dynamic_buffer_append(
95 &payload->buffer, syscall->pattern, pattern_len);
96 if (ret) {
97 goto end;
98 }
99
100 ret = lttng_dynamic_buffer_append(&payload->buffer,
101 syscall->filter_expression, filter_expression_len);
102 end:
103 return ret;
104 }
105
106 static bool lttng_event_rule_syscall_is_equal(const struct lttng_event_rule *_a,
107 const struct lttng_event_rule *_b)
108 {
109 bool is_equal = false;
110 struct lttng_event_rule_syscall *a, *b;
111
112 a = container_of(_a, struct lttng_event_rule_syscall, parent);
113 b = container_of(_b, struct lttng_event_rule_syscall, parent);
114
115 if (!!a->filter_expression != !!b->filter_expression) {
116 goto end;
117 }
118
119 assert(a->pattern);
120 assert(b->pattern);
121 if (strcmp(a->pattern, b->pattern)) {
122 goto end;
123 }
124
125 if (a->filter_expression && b->filter_expression) {
126 if (strcmp(a->filter_expression, b->filter_expression)) {
127 goto end;
128 }
129 } else if (!!a->filter_expression != !!b->filter_expression) {
130 /* One is set and not the other. */
131 goto end;
132 }
133
134 is_equal = true;
135 end:
136 return is_equal;
137 }
138
139 static enum lttng_error_code lttng_event_rule_syscall_generate_filter_bytecode(
140 struct lttng_event_rule *rule, uid_t uid, gid_t gid)
141 {
142 int ret;
143 enum lttng_error_code ret_code = LTTNG_OK;
144 struct lttng_event_rule_syscall *syscall;
145 enum lttng_event_rule_status status;
146 const char *filter;
147 struct lttng_filter_bytecode *bytecode = NULL;
148
149 assert(rule);
150
151 syscall = container_of(rule, struct lttng_event_rule_syscall, parent);
152
153 /* Generate the filter bytecode. */
154 status = lttng_event_rule_syscall_get_filter(rule, &filter);
155 if (status == LTTNG_EVENT_RULE_STATUS_UNSET) {
156 filter = NULL;
157 } else if (status != LTTNG_EVENT_RULE_STATUS_OK) {
158 ret_code = LTTNG_ERR_FILTER_INVAL;
159 goto end;
160 }
161
162 if (filter && filter[0] == '\0') {
163 ret_code = LTTNG_ERR_FILTER_INVAL;
164 goto end;
165 }
166
167 if (filter == NULL) {
168 /* Nothing to do. */
169 ret = LTTNG_OK;
170 goto end;
171 }
172
173 syscall->internal_filter.filter = strdup(filter);
174 if (syscall->internal_filter.filter == NULL) {
175 ret_code = LTTNG_ERR_NOMEM;
176 goto end;
177 }
178
179 ret = run_as_generate_filter_bytecode(
180 syscall->internal_filter.filter, uid, gid, &bytecode);
181 if (ret) {
182 ret_code = LTTNG_ERR_FILTER_INVAL;
183 }
184
185 syscall->internal_filter.bytecode = bytecode;
186 bytecode = NULL;
187
188 end:
189 free(bytecode);
190 return ret_code;
191 }
192
193 static const char *lttng_event_rule_syscall_get_internal_filter(
194 const struct lttng_event_rule *rule)
195 {
196 struct lttng_event_rule_syscall *syscall;
197
198 assert(rule);
199 syscall = container_of(rule, struct lttng_event_rule_syscall, parent);
200
201 return syscall->internal_filter.filter;
202 }
203
204 static const struct lttng_filter_bytecode *
205 lttng_event_rule_syscall_get_internal_filter_bytecode(
206 const struct lttng_event_rule *rule)
207 {
208 struct lttng_event_rule_syscall *syscall;
209
210 assert(rule);
211 syscall = container_of(rule, struct lttng_event_rule_syscall, parent);
212
213 return syscall->internal_filter.bytecode;
214 }
215
216 static struct lttng_event_exclusion *
217 lttng_event_rule_syscall_generate_exclusions(
218 const struct lttng_event_rule *rule)
219 {
220 /* Not supported. */
221 return NULL;
222 }
223
224 struct lttng_event_rule *lttng_event_rule_syscall_create()
225 {
226 struct lttng_event_rule *rule = NULL;
227 struct lttng_event_rule_syscall *syscall_rule;
228
229 syscall_rule = zmalloc(sizeof(struct lttng_event_rule_syscall));
230 if (!syscall_rule) {
231 goto end;
232 }
233
234 rule = &syscall_rule->parent;
235 lttng_event_rule_init(
236 &syscall_rule->parent, LTTNG_EVENT_RULE_TYPE_SYSCALL);
237 syscall_rule->parent.validate = lttng_event_rule_syscall_validate;
238 syscall_rule->parent.serialize = lttng_event_rule_syscall_serialize;
239 syscall_rule->parent.equal = lttng_event_rule_syscall_is_equal;
240 syscall_rule->parent.destroy = lttng_event_rule_syscall_destroy;
241 syscall_rule->parent.generate_filter_bytecode =
242 lttng_event_rule_syscall_generate_filter_bytecode;
243 syscall_rule->parent.get_filter =
244 lttng_event_rule_syscall_get_internal_filter;
245 syscall_rule->parent.get_filter_bytecode =
246 lttng_event_rule_syscall_get_internal_filter_bytecode;
247 syscall_rule->parent.generate_exclusions =
248 lttng_event_rule_syscall_generate_exclusions;
249 end:
250 return rule;
251 }
252
253 LTTNG_HIDDEN
254 ssize_t lttng_event_rule_syscall_create_from_payload(
255 struct lttng_payload_view *view,
256 struct lttng_event_rule **_event_rule)
257 {
258 ssize_t ret, offset = 0;
259 enum lttng_event_rule_status status;
260 const struct lttng_event_rule_syscall_comm *syscall_comm;
261 const char *pattern;
262 const char *filter_expression = NULL;
263 struct lttng_buffer_view current_buffer_view;
264 struct lttng_event_rule *rule = NULL;
265
266 if (!_event_rule) {
267 ret = -1;
268 goto end;
269 }
270
271 if (view->buffer.size < sizeof(*syscall_comm)) {
272 ERR("Failed to initialize from malformed event rule syscall: buffer too short to contain header");
273 ret = -1;
274 goto end;
275 }
276
277 current_buffer_view = lttng_buffer_view_from_view(
278 &view->buffer, offset, sizeof(*syscall_comm));
279 if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
280 ret = -1;
281 goto end;
282 }
283
284 syscall_comm = (typeof(syscall_comm)) current_buffer_view.data;
285 rule = lttng_event_rule_syscall_create();
286 if (!rule) {
287 ERR("Failed to create event rule syscall");
288 ret = -1;
289 goto end;
290 }
291
292 /* Skip to payload. */
293 offset += current_buffer_view.size;
294
295 /* Map the pattern. */
296 current_buffer_view = lttng_buffer_view_from_view(
297 &view->buffer, offset, syscall_comm->pattern_len);
298 if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
299 ret = -1;
300 goto end;
301 }
302
303 pattern = current_buffer_view.data;
304 if (!lttng_buffer_view_contains_string(&current_buffer_view, pattern,
305 syscall_comm->pattern_len)) {
306 ret = -1;
307 goto end;
308 }
309
310 /* Skip after the pattern. */
311 offset += syscall_comm->pattern_len;
312
313 if (!syscall_comm->filter_expression_len) {
314 goto skip_filter_expression;
315 }
316
317 /* Map the filter_expression. */
318 current_buffer_view = lttng_buffer_view_from_view(&view->buffer, offset,
319 syscall_comm->filter_expression_len);
320 if (!lttng_buffer_view_is_valid(&current_buffer_view)) {
321 ret = -1;
322 goto end;
323 }
324
325 filter_expression = current_buffer_view.data;
326 if (!lttng_buffer_view_contains_string(&current_buffer_view,
327 filter_expression,
328 syscall_comm->filter_expression_len)) {
329 ret = -1;
330 goto end;
331 }
332
333 /* Skip after the pattern. */
334 offset += syscall_comm->filter_expression_len;
335
336 skip_filter_expression:
337
338 status = lttng_event_rule_syscall_set_pattern(rule, pattern);
339 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
340 ERR("Failed to set event rule syscall pattern");
341 ret = -1;
342 goto end;
343 }
344
345 if (filter_expression) {
346 status = lttng_event_rule_syscall_set_filter(
347 rule, filter_expression);
348 if (status != LTTNG_EVENT_RULE_STATUS_OK) {
349 ERR("Failed to set event rule syscall pattern");
350 ret = -1;
351 goto end;
352 }
353 }
354
355 *_event_rule = rule;
356 rule = NULL;
357 ret = offset;
358 end:
359 lttng_event_rule_destroy(rule);
360 return ret;
361 }
362
363 enum lttng_event_rule_status lttng_event_rule_syscall_set_pattern(
364 struct lttng_event_rule *rule, const char *pattern)
365 {
366 char *pattern_copy = NULL;
367 struct lttng_event_rule_syscall *syscall;
368 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
369
370 if (!rule || !IS_SYSCALL_EVENT_RULE(rule) || !pattern ||
371 strlen(pattern) == 0) {
372 status = LTTNG_EVENT_RULE_STATUS_INVALID;
373 goto end;
374 }
375
376 syscall = container_of(rule, struct lttng_event_rule_syscall, parent);
377 pattern_copy = strdup(pattern);
378 if (!pattern_copy) {
379 status = LTTNG_EVENT_RULE_STATUS_ERROR;
380 goto end;
381 }
382
383 if (syscall->pattern) {
384 free(syscall->pattern);
385 }
386
387 syscall->pattern = pattern_copy;
388 pattern_copy = NULL;
389 end:
390 return status;
391 }
392
393 enum lttng_event_rule_status lttng_event_rule_syscall_get_pattern(
394 const struct lttng_event_rule *rule, const char **pattern)
395 {
396 struct lttng_event_rule_syscall *syscall;
397 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
398
399 if (!rule || !IS_SYSCALL_EVENT_RULE(rule) || !pattern) {
400 status = LTTNG_EVENT_RULE_STATUS_INVALID;
401 goto end;
402 }
403
404 syscall = container_of(rule, struct lttng_event_rule_syscall, parent);
405 if (!syscall->pattern) {
406 status = LTTNG_EVENT_RULE_STATUS_UNSET;
407 goto end;
408 }
409
410 *pattern = syscall->pattern;
411 end:
412 return status;
413 }
414
415 enum lttng_event_rule_status lttng_event_rule_syscall_set_filter(
416 struct lttng_event_rule *rule, const char *expression)
417 {
418 char *expression_copy = NULL;
419 struct lttng_event_rule_syscall *syscall;
420 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
421
422 /* TODO: validate that the passed expression is valid. */
423
424 if (!rule || !IS_SYSCALL_EVENT_RULE(rule) || !expression ||
425 strlen(expression) == 0) {
426 status = LTTNG_EVENT_RULE_STATUS_INVALID;
427 goto end;
428 }
429
430 syscall = container_of(rule, struct lttng_event_rule_syscall, parent);
431 expression_copy = strdup(expression);
432 if (!expression_copy) {
433 status = LTTNG_EVENT_RULE_STATUS_ERROR;
434 goto end;
435 }
436
437 if (syscall->filter_expression) {
438 free(syscall->filter_expression);
439 }
440
441 syscall->filter_expression = expression_copy;
442 expression_copy = NULL;
443 end:
444 return status;
445 }
446
447 enum lttng_event_rule_status lttng_event_rule_syscall_get_filter(
448 const struct lttng_event_rule *rule, const char **expression)
449 {
450 struct lttng_event_rule_syscall *syscall;
451 enum lttng_event_rule_status status = LTTNG_EVENT_RULE_STATUS_OK;
452
453 if (!rule || !IS_SYSCALL_EVENT_RULE(rule) || !expression) {
454 status = LTTNG_EVENT_RULE_STATUS_INVALID;
455 goto end;
456 }
457
458 syscall = container_of(rule, struct lttng_event_rule_syscall, parent);
459 if (!syscall->filter_expression) {
460 status = LTTNG_EVENT_RULE_STATUS_UNSET;
461 goto end;
462 }
463
464 *expression = syscall->filter_expression;
465 end:
466 return status;
467 }
This page took 0.057279 seconds and 4 git commands to generate.