From: Jonathan Rajotte Date: Mon, 23 Mar 2020 21:26:47 +0000 (-0400) Subject: Generate bytecodes related to the trigger on reception X-Git-Tag: v2.13.0-rc1~401 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=58daac01d91347336f24e1fc1cacd4e7a3101e93;p=lttng-tools.git Generate bytecodes related to the trigger on reception The compositing objects of a trigger might need to generate internal bytecode. Doing it at the registration step allows an early validation of the filter expressions. There is no need to generate it for the unregister command since bytecodes are not used for comparison and are for internal use only. Signed-off-by: Jonathan Rajotte Signed-off-by: Jérémie Galarneau Change-Id: Ia1282d55f028e6b056e8ff3877790894c582acdb --- diff --git a/include/lttng/event-rule/event-rule-internal.h b/include/lttng/event-rule/event-rule-internal.h index 9fb115caf..7aca4a02b 100644 --- a/include/lttng/event-rule/event-rule-internal.h +++ b/include/lttng/event-rule/event-rule-internal.h @@ -9,6 +9,7 @@ #define LTTNG_EVENT_RULE_INTERNAL_H #include +#include #include #include #include @@ -32,7 +33,8 @@ typedef ssize_t (*event_rule_create_from_payload_cb)( struct lttng_payload_view *view, struct lttng_event_rule **event_rule); typedef enum lttng_error_code (*event_rule_generate_filter_bytecode_cb)( - struct lttng_event_rule *event_rule, uid_t uid, gid_t gid); + struct lttng_event_rule *event_rule, + const struct lttng_credentials *creds); typedef const char *(*event_rule_get_filter_cb)( const struct lttng_event_rule *event_rule); typedef const struct lttng_filter_bytecode *( @@ -92,7 +94,8 @@ enum lttng_domain_type lttng_event_rule_get_domain_type( LTTNG_HIDDEN enum lttng_error_code lttng_event_rule_generate_filter_bytecode( - struct lttng_event_rule *rule, uid_t uid, gid_t gid); + struct lttng_event_rule *rule, + const struct lttng_credentials *creds); /* * If not present/implemented returns NULL. diff --git a/include/lttng/trigger/trigger-internal.h b/include/lttng/trigger/trigger-internal.h index e32ea737e..3b760e167 100644 --- a/include/lttng/trigger/trigger-internal.h +++ b/include/lttng/trigger/trigger-internal.h @@ -192,4 +192,13 @@ LTTNG_HIDDEN enum lttng_domain_type lttng_trigger_get_underlying_domain_type_restriction( const struct lttng_trigger *trigger); +/* + * Generate any bytecode related to the trigger. + * On success LTTNG_OK. On error, returns lttng_error code. + */ +LTTNG_HIDDEN +enum lttng_error_code lttng_trigger_generate_bytecode( + struct lttng_trigger *trigger, + const struct lttng_credentials *creds); + #endif /* LTTNG_TRIGGER_INTERNAL_H */ diff --git a/src/bin/lttng-sessiond/cmd.c b/src/bin/lttng-sessiond/cmd.c index 2f5a4e98c..2abcdf48e 100644 --- a/src/bin/lttng-sessiond/cmd.c +++ b/src/bin/lttng-sessiond/cmd.c @@ -4342,6 +4342,15 @@ int cmd_register_trigger(struct command_ctx *cmd_ctx, int sock, } } + /* + * The bytecode generation also serves as a validation step for the + * bytecode expressions. + */ + ret = lttng_trigger_generate_bytecode(trigger, &cmd_creds); + if (ret != LTTNG_OK) { + goto end; + } + /* * A reference to the trigger is acquired by the notification thread. * It is safe to return the same trigger to the caller since it the diff --git a/src/common/event-rule/event-rule.c b/src/common/event-rule/event-rule.c index fbcb7a018..627674ba9 100644 --- a/src/common/event-rule/event-rule.c +++ b/src/common/event-rule/event-rule.c @@ -242,10 +242,11 @@ void lttng_event_rule_put(struct lttng_event_rule *event_rule) LTTNG_HIDDEN enum lttng_error_code lttng_event_rule_generate_filter_bytecode( - struct lttng_event_rule *rule, uid_t uid, gid_t gid) + struct lttng_event_rule *rule, + const struct lttng_credentials *creds) { assert(rule->generate_filter_bytecode); - return rule->generate_filter_bytecode(rule, uid, gid); + return rule->generate_filter_bytecode(rule, creds); } LTTNG_HIDDEN diff --git a/src/common/event-rule/kprobe.c b/src/common/event-rule/kprobe.c index 9cc5d7c54..6846f0cb1 100644 --- a/src/common/event-rule/kprobe.c +++ b/src/common/event-rule/kprobe.c @@ -6,6 +6,7 @@ */ #include +#include #include #include #include @@ -146,7 +147,8 @@ end: } static enum lttng_error_code lttng_event_rule_kprobe_generate_filter_bytecode( - struct lttng_event_rule *rule, uid_t uid, gid_t gid) + struct lttng_event_rule *rule, + const struct lttng_credentials *creds) { /* Nothing to do. */ return LTTNG_OK; diff --git a/src/common/event-rule/syscall.c b/src/common/event-rule/syscall.c index 826953c21..b2feb2c6e 100644 --- a/src/common/event-rule/syscall.c +++ b/src/common/event-rule/syscall.c @@ -6,6 +6,7 @@ */ #include +#include #include #include #include @@ -137,7 +138,8 @@ end: } static enum lttng_error_code lttng_event_rule_syscall_generate_filter_bytecode( - struct lttng_event_rule *rule, uid_t uid, gid_t gid) + struct lttng_event_rule *rule, + const struct lttng_credentials *creds) { int ret; enum lttng_error_code ret_code = LTTNG_OK; @@ -177,7 +179,7 @@ static enum lttng_error_code lttng_event_rule_syscall_generate_filter_bytecode( } ret = run_as_generate_filter_bytecode( - syscall->internal_filter.filter, uid, gid, &bytecode); + syscall->internal_filter.filter, creds, &bytecode); if (ret) { ret_code = LTTNG_ERR_FILTER_INVAL; } diff --git a/src/common/event-rule/tracepoint.c b/src/common/event-rule/tracepoint.c index f750af47f..0d0864719 100644 --- a/src/common/event-rule/tracepoint.c +++ b/src/common/event-rule/tracepoint.c @@ -6,6 +6,7 @@ */ #include +#include #include #include #include @@ -357,7 +358,8 @@ end: static enum lttng_error_code lttng_event_rule_tracepoint_generate_filter_bytecode( - struct lttng_event_rule *rule, uid_t uid, gid_t gid) + struct lttng_event_rule *rule, + const struct lttng_credentials *creds) { int ret; enum lttng_error_code ret_code; @@ -429,7 +431,7 @@ lttng_event_rule_tracepoint_generate_filter_bytecode( } ret = run_as_generate_filter_bytecode( - tracepoint->internal_filter.filter, uid, gid, + tracepoint->internal_filter.filter, creds, &bytecode); if (ret) { ret_code = LTTNG_ERR_FILTER_INVAL; diff --git a/src/common/event-rule/uprobe.c b/src/common/event-rule/uprobe.c index d9c1fc20c..b02332b61 100644 --- a/src/common/event-rule/uprobe.c +++ b/src/common/event-rule/uprobe.c @@ -6,6 +6,7 @@ */ #include +#include #include #include #include @@ -138,7 +139,8 @@ end: } static enum lttng_error_code lttng_event_rule_uprobe_generate_filter_bytecode( - struct lttng_event_rule *rule, uid_t uid, gid_t gid) + struct lttng_event_rule *rule, + const struct lttng_credentials *creds) { /* Nothing to do. */ return LTTNG_OK; diff --git a/src/common/runas.c b/src/common/runas.c index 30bf4263a..1147296ad 100644 --- a/src/common/runas.c +++ b/src/common/runas.c @@ -1799,8 +1799,7 @@ error: LTTNG_HIDDEN int run_as_generate_filter_bytecode(const char *filter_expression, - uid_t uid, - gid_t gid, + const struct lttng_credentials *creds, struct lttng_filter_bytecode **bytecode) { int ret; @@ -1808,6 +1807,8 @@ int run_as_generate_filter_bytecode(const char *filter_expression, struct run_as_ret run_as_ret = {}; const struct lttng_filter_bytecode *view_bytecode = NULL; struct lttng_filter_bytecode *local_bytecode = NULL; + const uid_t uid = lttng_credentials_get_uid(creds); + const gid_t gid = lttng_credentials_get_gid(creds); DBG3("generate_filter_bytecode() from expression=\"%s\" for uid %d and gid %d", filter_expression, (int) uid, (int) gid); diff --git a/src/common/runas.h b/src/common/runas.h index 398221708..40b4f91b0 100644 --- a/src/common/runas.h +++ b/src/common/runas.h @@ -72,8 +72,7 @@ int run_as_extract_sdt_probe_offsets(int fd, const char *provider_name, uint64_t **offsets, uint32_t *num_offset); LTTNG_HIDDEN int run_as_generate_filter_bytecode(const char *filter_expression, - uid_t uid, - gid_t gid, + const struct lttng_credentials *creds, struct lttng_filter_bytecode **bytecode); LTTNG_HIDDEN int run_as_create_worker(const char *procname, diff --git a/src/common/trigger.c b/src/common/trigger.c index 132fda138..f47f76d55 100644 --- a/src/common/trigger.c +++ b/src/common/trigger.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -928,3 +929,47 @@ enum lttng_domain_type lttng_trigger_get_underlying_domain_type_restriction( return type; } + +/* + * Generate bytecode related to the trigger. + * On success LTTNG_OK. On error, returns lttng_error code. + */ +LTTNG_HIDDEN +enum lttng_error_code lttng_trigger_generate_bytecode( + struct lttng_trigger *trigger, + const struct lttng_credentials *creds) +{ + enum lttng_error_code ret; + struct lttng_condition *condition = NULL; + + condition = lttng_trigger_get_condition(trigger); + if (!condition) { + ret = LTTNG_ERR_INVALID_TRIGGER; + goto end; + } + + switch (lttng_condition_get_type(condition)) { + case LTTNG_CONDITION_TYPE_EVENT_RULE_HIT: + { + struct lttng_event_rule *event_rule; + const enum lttng_condition_status condition_status = + lttng_condition_event_rule_borrow_rule_mutable( + condition, &event_rule); + + assert(condition_status == LTTNG_CONDITION_STATUS_OK); + ret = lttng_event_rule_generate_filter_bytecode( + event_rule, creds); + if (ret != LTTNG_OK) { + goto end; + } + + ret = LTTNG_OK; + break; + } + default: + ret = LTTNG_OK; + break; + } +end: + return ret; +}