2 * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
4 * SPDX-License-Identifier: LGPL-2.1-only
9 #include <common/credentials.h>
10 #include <common/error.h>
11 #include <common/macros.h>
12 #include <common/payload.h>
13 #include <common/payload-view.h>
14 #include <common/runas.h>
15 #include <common/hashtable/hashtable.h>
16 #include <common/hashtable/utils.h>
17 #include <common/string-utils/string-utils.h>
18 #include <lttng/event-rule/event-rule-internal.h>
19 #include <lttng/event-rule/kernel-syscall-internal.h>
21 #define IS_SYSCALL_EVENT_RULE(rule) \
22 (lttng_event_rule_get_type(rule) == LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL)
24 static void lttng_event_rule_kernel_syscall_destroy(struct lttng_event_rule
*rule
)
26 struct lttng_event_rule_kernel_syscall
*syscall
;
32 syscall
= container_of(rule
, struct lttng_event_rule_kernel_syscall
, parent
);
34 free(syscall
->pattern
);
35 free(syscall
->filter_expression
);
36 free(syscall
->internal_filter
.filter
);
37 free(syscall
->internal_filter
.bytecode
);
41 static bool lttng_event_rule_kernel_syscall_validate(
42 const struct lttng_event_rule
*rule
)
45 struct lttng_event_rule_kernel_syscall
*syscall
;
51 syscall
= container_of(rule
, struct lttng_event_rule_kernel_syscall
, parent
);
54 if (!syscall
->pattern
) {
55 ERR("Invalid syscall event rule: a pattern must be set.");
64 static int lttng_event_rule_kernel_syscall_serialize(
65 const struct lttng_event_rule
*rule
,
66 struct lttng_payload
*payload
)
69 size_t pattern_len
, filter_expression_len
;
70 struct lttng_event_rule_kernel_syscall
*syscall
;
71 struct lttng_event_rule_kernel_syscall_comm syscall_comm
;
73 if (!rule
|| !IS_SYSCALL_EVENT_RULE(rule
)) {
78 DBG("Serializing syscall event rule");
79 syscall
= container_of(rule
, struct lttng_event_rule_kernel_syscall
, parent
);
81 pattern_len
= strlen(syscall
->pattern
) + 1;
83 if (syscall
->filter_expression
!= NULL
) {
84 filter_expression_len
= strlen(syscall
->filter_expression
) + 1;
86 filter_expression_len
= 0;
89 syscall_comm
.pattern_len
= pattern_len
;
90 syscall_comm
.filter_expression_len
= filter_expression_len
;
91 syscall_comm
.emission_site
= syscall
->emission_site
;
93 ret
= lttng_dynamic_buffer_append(
94 &payload
->buffer
, &syscall_comm
, sizeof(syscall_comm
));
99 ret
= lttng_dynamic_buffer_append(
100 &payload
->buffer
, syscall
->pattern
, pattern_len
);
105 ret
= lttng_dynamic_buffer_append(&payload
->buffer
,
106 syscall
->filter_expression
, filter_expression_len
);
111 static bool lttng_event_rule_kernel_syscall_is_equal(const struct lttng_event_rule
*_a
,
112 const struct lttng_event_rule
*_b
)
114 bool is_equal
= false;
115 struct lttng_event_rule_kernel_syscall
*a
, *b
;
117 a
= container_of(_a
, struct lttng_event_rule_kernel_syscall
, parent
);
118 b
= container_of(_b
, struct lttng_event_rule_kernel_syscall
, parent
);
120 if (!!a
->filter_expression
!= !!b
->filter_expression
) {
126 if (strcmp(a
->pattern
, b
->pattern
)) {
130 if (a
->filter_expression
&& b
->filter_expression
) {
131 if (strcmp(a
->filter_expression
, b
->filter_expression
)) {
134 } else if (!!a
->filter_expression
!= !!b
->filter_expression
) {
135 /* One is set and not the other. */
144 static enum lttng_error_code
lttng_event_rule_kernel_syscall_generate_filter_bytecode(
145 struct lttng_event_rule
*rule
,
146 const struct lttng_credentials
*creds
)
149 enum lttng_error_code ret_code
= LTTNG_OK
;
150 struct lttng_event_rule_kernel_syscall
*syscall
;
151 enum lttng_event_rule_status status
;
153 struct lttng_bytecode
*bytecode
= NULL
;
157 syscall
= container_of(rule
, struct lttng_event_rule_kernel_syscall
, parent
);
159 /* Generate the filter bytecode. */
160 status
= lttng_event_rule_kernel_syscall_get_filter(rule
, &filter
);
161 if (status
== LTTNG_EVENT_RULE_STATUS_UNSET
) {
163 } else if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
164 ret_code
= LTTNG_ERR_FILTER_INVAL
;
168 if (filter
&& filter
[0] == '\0') {
169 ret_code
= LTTNG_ERR_FILTER_INVAL
;
173 if (filter
== NULL
) {
179 syscall
->internal_filter
.filter
= strdup(filter
);
180 if (syscall
->internal_filter
.filter
== NULL
) {
181 ret_code
= LTTNG_ERR_NOMEM
;
185 ret
= run_as_generate_filter_bytecode(
186 syscall
->internal_filter
.filter
, creds
, &bytecode
);
188 ret_code
= LTTNG_ERR_FILTER_INVAL
;
191 syscall
->internal_filter
.bytecode
= bytecode
;
199 static const char *lttng_event_rule_kernel_syscall_get_internal_filter(
200 const struct lttng_event_rule
*rule
)
202 struct lttng_event_rule_kernel_syscall
*syscall
;
205 syscall
= container_of(rule
, struct lttng_event_rule_kernel_syscall
, parent
);
207 return syscall
->internal_filter
.filter
;
210 static const struct lttng_bytecode
*
211 lttng_event_rule_kernel_syscall_get_internal_filter_bytecode(
212 const struct lttng_event_rule
*rule
)
214 struct lttng_event_rule_kernel_syscall
*syscall
;
217 syscall
= container_of(rule
, struct lttng_event_rule_kernel_syscall
, parent
);
219 return syscall
->internal_filter
.bytecode
;
222 static enum lttng_event_rule_generate_exclusions_status
223 lttng_event_rule_kernel_syscall_generate_exclusions(const struct lttng_event_rule
*rule
,
224 struct lttng_event_exclusion
**exclusions
)
228 return LTTNG_EVENT_RULE_GENERATE_EXCLUSIONS_STATUS_NONE
;
232 lttng_event_rule_kernel_syscall_hash(
233 const struct lttng_event_rule
*rule
)
236 struct lttng_event_rule_kernel_syscall
*syscall_rule
=
237 container_of(rule
, typeof(*syscall_rule
), parent
);
239 hash
= hash_key_ulong((void *) LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
,
241 hash
^= hash_key_str(syscall_rule
->pattern
, lttng_ht_seed
);
242 if (syscall_rule
->filter_expression
) {
243 hash
^= hash_key_str(syscall_rule
->filter_expression
,
250 struct lttng_event_rule
*lttng_event_rule_kernel_syscall_create(
251 enum lttng_event_rule_kernel_syscall_emission_site
254 struct lttng_event_rule
*rule
= NULL
;
255 struct lttng_event_rule_kernel_syscall
*syscall_rule
;
256 enum lttng_event_rule_status status
;
258 /* Validate the emission site type */
259 switch (emission_site
) {
260 case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY_EXIT
:
261 case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY
:
262 case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_EXIT
:
265 /* Invalid emission type */
269 syscall_rule
= zmalloc(sizeof(struct lttng_event_rule_kernel_syscall
));
274 rule
= &syscall_rule
->parent
;
275 lttng_event_rule_init(
276 &syscall_rule
->parent
, LTTNG_EVENT_RULE_TYPE_KERNEL_SYSCALL
);
277 syscall_rule
->parent
.validate
= lttng_event_rule_kernel_syscall_validate
;
278 syscall_rule
->parent
.serialize
= lttng_event_rule_kernel_syscall_serialize
;
279 syscall_rule
->parent
.equal
= lttng_event_rule_kernel_syscall_is_equal
;
280 syscall_rule
->parent
.destroy
= lttng_event_rule_kernel_syscall_destroy
;
281 syscall_rule
->parent
.generate_filter_bytecode
=
282 lttng_event_rule_kernel_syscall_generate_filter_bytecode
;
283 syscall_rule
->parent
.get_filter
=
284 lttng_event_rule_kernel_syscall_get_internal_filter
;
285 syscall_rule
->parent
.get_filter_bytecode
=
286 lttng_event_rule_kernel_syscall_get_internal_filter_bytecode
;
287 syscall_rule
->parent
.generate_exclusions
=
288 lttng_event_rule_kernel_syscall_generate_exclusions
;
289 syscall_rule
->parent
.hash
= lttng_event_rule_kernel_syscall_hash
;
291 /* Default pattern is '*'. */
292 status
= lttng_event_rule_kernel_syscall_set_name_pattern(rule
, "*");
293 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
294 lttng_event_rule_destroy(rule
);
298 /* Emission site type */
299 syscall_rule
->emission_site
= emission_site
;
306 ssize_t
lttng_event_rule_kernel_syscall_create_from_payload(
307 struct lttng_payload_view
*view
,
308 struct lttng_event_rule
**_event_rule
)
310 ssize_t ret
, offset
= 0;
311 enum lttng_event_rule_status status
;
312 const struct lttng_event_rule_kernel_syscall_comm
*syscall_comm
;
314 const char *filter_expression
= NULL
;
315 struct lttng_buffer_view current_buffer_view
;
316 struct lttng_event_rule
*rule
= NULL
;
323 if (view
->buffer
.size
< sizeof(*syscall_comm
)) {
324 ERR("Failed to initialize from malformed event rule syscall: buffer too short to contain header");
329 current_buffer_view
= lttng_buffer_view_from_view(
330 &view
->buffer
, offset
, sizeof(*syscall_comm
));
331 if (!lttng_buffer_view_is_valid(¤t_buffer_view
)) {
336 syscall_comm
= (typeof(syscall_comm
)) current_buffer_view
.data
;
337 rule
= lttng_event_rule_kernel_syscall_create(syscall_comm
->emission_site
);
339 ERR("Failed to create event rule syscall");
344 /* Skip to payload. */
345 offset
+= current_buffer_view
.size
;
347 /* Map the pattern. */
348 current_buffer_view
= lttng_buffer_view_from_view(
349 &view
->buffer
, offset
, syscall_comm
->pattern_len
);
350 if (!lttng_buffer_view_is_valid(¤t_buffer_view
)) {
355 pattern
= current_buffer_view
.data
;
356 if (!lttng_buffer_view_contains_string(¤t_buffer_view
, pattern
,
357 syscall_comm
->pattern_len
)) {
362 /* Skip after the pattern. */
363 offset
+= syscall_comm
->pattern_len
;
365 if (!syscall_comm
->filter_expression_len
) {
366 goto skip_filter_expression
;
369 /* Map the filter_expression. */
370 current_buffer_view
= lttng_buffer_view_from_view(&view
->buffer
, offset
,
371 syscall_comm
->filter_expression_len
);
372 if (!lttng_buffer_view_is_valid(¤t_buffer_view
)) {
377 filter_expression
= current_buffer_view
.data
;
378 if (!lttng_buffer_view_contains_string(¤t_buffer_view
,
380 syscall_comm
->filter_expression_len
)) {
385 /* Skip after the pattern. */
386 offset
+= syscall_comm
->filter_expression_len
;
388 skip_filter_expression
:
390 status
= lttng_event_rule_kernel_syscall_set_name_pattern(rule
, pattern
);
391 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
392 ERR("Failed to set event rule syscall pattern");
397 if (filter_expression
) {
398 status
= lttng_event_rule_kernel_syscall_set_filter(
399 rule
, filter_expression
);
400 if (status
!= LTTNG_EVENT_RULE_STATUS_OK
) {
401 ERR("Failed to set event rule syscall pattern");
411 lttng_event_rule_destroy(rule
);
415 enum lttng_event_rule_status
lttng_event_rule_kernel_syscall_set_name_pattern(
416 struct lttng_event_rule
*rule
, const char *pattern
)
418 char *pattern_copy
= NULL
;
419 struct lttng_event_rule_kernel_syscall
*syscall
;
420 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
422 if (!rule
|| !IS_SYSCALL_EVENT_RULE(rule
) || !pattern
||
423 strlen(pattern
) == 0) {
424 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
428 syscall
= container_of(rule
, struct lttng_event_rule_kernel_syscall
, parent
);
429 pattern_copy
= strdup(pattern
);
431 status
= LTTNG_EVENT_RULE_STATUS_ERROR
;
435 strutils_normalize_star_glob_pattern(pattern_copy
);
437 free(syscall
->pattern
);
439 syscall
->pattern
= pattern_copy
;
445 enum lttng_event_rule_status
lttng_event_rule_kernel_syscall_get_name_pattern(
446 const struct lttng_event_rule
*rule
, const char **pattern
)
448 struct lttng_event_rule_kernel_syscall
*syscall
;
449 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
451 if (!rule
|| !IS_SYSCALL_EVENT_RULE(rule
) || !pattern
) {
452 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
456 syscall
= container_of(rule
, struct lttng_event_rule_kernel_syscall
, parent
);
457 if (!syscall
->pattern
) {
458 status
= LTTNG_EVENT_RULE_STATUS_UNSET
;
462 *pattern
= syscall
->pattern
;
467 enum lttng_event_rule_status
lttng_event_rule_kernel_syscall_set_filter(
468 struct lttng_event_rule
*rule
, const char *expression
)
470 char *expression_copy
= NULL
;
471 struct lttng_event_rule_kernel_syscall
*syscall
;
472 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
474 /* TODO: validate that the passed expression is valid. */
476 if (!rule
|| !IS_SYSCALL_EVENT_RULE(rule
) || !expression
||
477 strlen(expression
) == 0) {
478 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
482 syscall
= container_of(rule
, struct lttng_event_rule_kernel_syscall
, parent
);
483 expression_copy
= strdup(expression
);
484 if (!expression_copy
) {
485 status
= LTTNG_EVENT_RULE_STATUS_ERROR
;
489 if (syscall
->filter_expression
) {
490 free(syscall
->filter_expression
);
493 syscall
->filter_expression
= expression_copy
;
494 expression_copy
= NULL
;
499 enum lttng_event_rule_status
lttng_event_rule_kernel_syscall_get_filter(
500 const struct lttng_event_rule
*rule
, const char **expression
)
502 struct lttng_event_rule_kernel_syscall
*syscall
;
503 enum lttng_event_rule_status status
= LTTNG_EVENT_RULE_STATUS_OK
;
505 if (!rule
|| !IS_SYSCALL_EVENT_RULE(rule
) || !expression
) {
506 status
= LTTNG_EVENT_RULE_STATUS_INVALID
;
510 syscall
= container_of(rule
, struct lttng_event_rule_kernel_syscall
, parent
);
511 if (!syscall
->filter_expression
) {
512 status
= LTTNG_EVENT_RULE_STATUS_UNSET
;
516 *expression
= syscall
->filter_expression
;
520 extern enum lttng_event_rule_kernel_syscall_emission_site
521 lttng_event_rule_kernel_syscall_get_emission_site(
522 const struct lttng_event_rule
*rule
)
524 enum lttng_event_rule_kernel_syscall_emission_site emission_site
=
525 LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_UNKNOWN
;
526 struct lttng_event_rule_kernel_syscall
*syscall
;
528 if (!rule
|| !IS_SYSCALL_EVENT_RULE(rule
)) {
532 syscall
= container_of(rule
, struct lttng_event_rule_kernel_syscall
, parent
);
533 emission_site
= syscall
->emission_site
;
536 return emission_site
;
540 const char *lttng_event_rule_kernel_syscall_emission_site_str(
541 enum lttng_event_rule_kernel_syscall_emission_site emission_site
)
543 switch (emission_site
) {
544 case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY
:
546 case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_ENTRY_EXIT
:
548 case LTTNG_EVENT_RULE_KERNEL_SYSCALL_EMISSION_SITE_EXIT
: