Generate bytecodes related to the trigger on reception
authorJonathan Rajotte <jonathan.rajotte-julien@efficios.com>
Mon, 23 Mar 2020 21:26:47 +0000 (17:26 -0400)
committerJérémie Galarneau <jeremie.galarneau@efficios.com>
Tue, 8 Dec 2020 17:25:37 +0000 (12:25 -0500)
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 <jonathan.rajotte-julien@efficios.com>
Signed-off-by: Jérémie Galarneau <jeremie.galarneau@efficios.com>
Change-Id: Ia1282d55f028e6b056e8ff3877790894c582acdb

include/lttng/event-rule/event-rule-internal.h
include/lttng/trigger/trigger-internal.h
src/bin/lttng-sessiond/cmd.c
src/common/event-rule/event-rule.c
src/common/event-rule/kprobe.c
src/common/event-rule/syscall.c
src/common/event-rule/tracepoint.c
src/common/event-rule/uprobe.c
src/common/runas.c
src/common/runas.h
src/common/trigger.c

index 9fb115caf670bd5b924c112be6369ec003361911..7aca4a02ba62098be6d6fe271bc151920ddc82e3 100644 (file)
@@ -9,6 +9,7 @@
 #define LTTNG_EVENT_RULE_INTERNAL_H
 
 #include <common/macros.h>
+#include <common/credentials.h>
 #include <lttng/domain.h>
 #include <lttng/event-rule/event-rule.h>
 #include <lttng/lttng-error.h>
@@ -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.
index e32ea737ea8a911c9dc56402601186d08c5a5a5d..3b760e1675a83709e4e12bb2568d1957de8c0d39 100644 (file)
@@ -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 */
index 2f5a4e98c74978d8ac8d7480cb539bc595e2afd7..2abcdf48eb5f161e207d48288c07cfa781116b2f 100644 (file)
@@ -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
index fbcb7a018a0c0889b32725877e850f1d85a81549..627674ba90026a84cb912aa407b2a4909465f8e0 100644 (file)
@@ -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
index 9cc5d7c5433b0016dea8efb51675d2a70956e8fa..6846f0cb16a2b664c4635227b09d4b2ee58535b9 100644 (file)
@@ -6,6 +6,7 @@
  */
 
 #include <assert.h>
+#include <common/credentials.h>
 #include <common/error.h>
 #include <common/macros.h>
 #include <common/payload.h>
@@ -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;
index 826953c21fe6cc965bc1064a8434f0ebea412112..b2feb2c6ecf4c772049d9c41d304544f185959d1 100644 (file)
@@ -6,6 +6,7 @@
  */
 
 #include <assert.h>
+#include <common/credentials.h>
 #include <common/error.h>
 #include <common/macros.h>
 #include <common/payload.h>
@@ -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;
        }
index f750af47f8ced8e496462daa9acd6a9b70b32e56..0d0864719df5425500bb04f11c1665dbeaa1bdc1 100644 (file)
@@ -6,6 +6,7 @@
  */
 
 #include <assert.h>
+#include <common/credentials.h>
 #include <common/error.h>
 #include <common/macros.h>
 #include <common/payload.h>
@@ -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;
index d9c1fc20c5a268070c00bc6c28806d8f93946aae..b02332b61c11202780f64511cb690446791d37d4 100644 (file)
@@ -6,6 +6,7 @@
  */
 
 #include <assert.h>
+#include <common/credentials.h>
 #include <common/error.h>
 #include <common/macros.h>
 #include <common/payload.h>
@@ -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;
index 30bf4263aa3d55c365c99a2ff4e57ef61a7614ed..1147296ad3357e21d4fd169ef54f73c78f13512f 100644 (file)
@@ -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);
index 398221708e16948dfc62221d7df228cf3fb6f850..40b4f91b07792864c00cc463bc1ea87f39423c52 100644 (file)
@@ -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,
index 132fda138912ef8ac264f6452f274614c1b19eca..f47f76d5529a732a8f6e263d32a373d622ac1b92 100644 (file)
@@ -8,6 +8,7 @@
 #include <lttng/trigger/trigger-internal.h>
 #include <lttng/condition/condition-internal.h>
 #include <lttng/condition/event-rule.h>
+#include <lttng/condition/event-rule-internal.h>
 #include <lttng/condition/buffer-usage.h>
 #include <lttng/event-rule/event-rule-internal.h>
 #include <lttng/action/action-internal.h>
@@ -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;
+}
This page took 0.045765 seconds and 4 git commands to generate.