X-Git-Url: http://git.lttng.org/?p=lttng-tools.git;a=blobdiff_plain;f=src%2Fbin%2Flttng-sessiond%2Fcmd.c;h=96f7433a6345fd90b1f608a272785fe3fc341578;hp=1922fd594c6dc519838a359dc9704eab7318ab01;hb=43ed148559609eba941bb3dffa35bff67341e64d;hpb=99608320e6099c72850fa2df81e6f92fa38352d7 diff --git a/src/bin/lttng-sessiond/cmd.c b/src/bin/lttng-sessiond/cmd.c index 1922fd594..96f7433a6 100644 --- a/src/bin/lttng-sessiond/cmd.c +++ b/src/bin/lttng-sessiond/cmd.c @@ -6,9 +6,6 @@ * */ -#include "bin/lttng-sessiond/tracker.h" -#include "lttng/lttng-error.h" -#include "lttng/tracker.h" #define _LGPL_SOURCE #include #include @@ -40,6 +37,8 @@ #include #include #include +#include +#include #include #include "channel.h" @@ -59,6 +58,7 @@ #include "rotation-thread.h" #include "timer.h" #include "agent-thread.h" +#include "tracker.h" #include "cmd.h" @@ -488,8 +488,14 @@ static int list_lttng_agent_events(struct agent *agt, .loglevel_type = agent_event->loglevel_type, }; - strncpy(event.name, agent_event->name, sizeof(event.name)); - event.name[sizeof(event.name) - 1] = '\0'; + ret = lttng_strncpy(event.name, agent_event->name, sizeof(event.name)); + if (ret) { + /* Internal error, invalid name. */ + ERR("Invalid event name while listing agent events: '%s' exceeds the maximal allowed length of %zu bytes", + agent_event->name, sizeof(event.name)); + ret = -LTTNG_ERR_UNK; + goto end; + } ret = lttng_dynamic_buffer_append( &payload->buffer, &event, sizeof(event)); @@ -556,8 +562,14 @@ static int list_lttng_ust_global_events(char *channel_name, continue; } - strncpy(event.name, uevent->attr.name, sizeof(event.name)); - event.name[sizeof(event.name) - 1] = '\0'; + ret = lttng_strncpy(event.name, uevent->attr.name, sizeof(event.name)); + if (ret) { + /* Internal error, invalid name. */ + ERR("Invalid event name while listing user space tracer events: '%s' exceeds the maximal allowed length of %zu bytes", + uevent->attr.name, sizeof(event.name)); + ret = -LTTNG_ERR_UNK; + goto end; + } event.enabled = uevent->enabled; @@ -647,8 +659,16 @@ static int list_lttng_kernel_events(char *channel_name, cds_list_for_each_entry(kevent, &kchan->events_list.head , list) { struct lttng_event event = {}; - strncpy(event.name, kevent->event->name, sizeof(event.name)); - event.name[sizeof(event.name) - 1] = '\0'; + ret = lttng_strncpy(event.name, kevent->event->name, sizeof(event.name)); + if (ret) { + /* Internal error, invalid name. */ + ERR("Invalid event name while listing kernel events: '%s' exceeds the maximal allowed length of %zu bytes", + kevent->event->name, + sizeof(event.name)); + ret = -LTTNG_ERR_UNK; + goto end; + } + event.enabled = kevent->enabled; event.filter = (unsigned char) !!kevent->filter_expression; @@ -946,7 +966,7 @@ static enum lttng_error_code send_consumer_relayd_socket( struct consumer_socket *consumer_sock, const char *session_name, const char *hostname, const char *base_path, int session_live_timer, - const uint64_t *current_chunk_id, + const uint64_t *current_chunk_id, time_t session_creation_time, bool session_name_contains_creation_time) { @@ -1074,9 +1094,9 @@ int cmd_setup_relayd(struct ltt_session *session) struct ltt_kernel_session *ksess; struct consumer_socket *socket; struct lttng_ht_iter iter; - LTTNG_OPTIONAL(uint64_t) current_chunk_id = {}; + LTTNG_OPTIONAL(uint64_t) current_chunk_id = {}; - assert(session); + assert(session); usess = session->ust_session; ksess = session->kernel_session; @@ -2288,7 +2308,7 @@ static int _cmd_enable_event(struct ltt_session *session, */ ret = validate_ust_event_name(event->name); if (ret) { - WARN("Userspace event name %s failed validation.", + WARN("Userspace event name %s failed validation.", event->name); ret = LTTNG_ERR_INVALID_EVENT_NAME; goto error; @@ -3107,10 +3127,22 @@ enum lttng_error_code cmd_create_session(struct command_ctx *cmd_ctx, int sock, &payload, 0, cmd_ctx->lsm.u.create_session.home_dir_size); + if (cmd_ctx->lsm.u.create_session.home_dir_size > 0 && + !lttng_buffer_view_is_valid(&home_dir_view)) { + ERR("Invalid payload in \"create session\" command: buffer too short to contain home directory"); + ret_code = LTTNG_ERR_INVALID_PROTOCOL; + goto error; + } + session_descriptor_view = lttng_buffer_view_from_dynamic_buffer( &payload, cmd_ctx->lsm.u.create_session.home_dir_size, cmd_ctx->lsm.u.create_session.session_descriptor_size); + if (!lttng_buffer_view_is_valid(&session_descriptor_view)) { + ERR("Invalid payload in \"create session\" command: buffer too short to contain session descriptor"); + ret_code = LTTNG_ERR_INVALID_PROTOCOL; + goto error; + } ret = lttng_session_descriptor_create_from_buffer( &session_descriptor_view, &session_descriptor); @@ -3164,10 +3196,10 @@ void cmd_destroy_session_reply(const struct ltt_session *session, lttng_dynamic_buffer_init(&payload); ret = lttng_dynamic_buffer_append(&payload, &llm, sizeof(llm)); - if (ret) { + if (ret) { ERR("Failed to append session destruction message"); goto error; - } + } cmd_header.rotation_state = (int32_t) (reply_context->implicit_rotation_on_destroy ? @@ -3290,10 +3322,10 @@ int cmd_destroy_session(struct ltt_session *session, session->name, lttng_strerror(-ret)); destruction_last_error = -ret; } - if (reply_context) { + if (reply_context) { reply_context->implicit_rotation_on_destroy = true; - } - } else if (session->has_been_started && session->current_trace_chunk) { + } + } else if (session->has_been_started && session->current_trace_chunk) { /* * The user has not triggered a session rotation. However, to * ensure all data has been consumed, the session is rotated @@ -3391,8 +3423,8 @@ int cmd_destroy_session(struct ltt_session *session, } else { *sock_fd = -1; } - } - ret = LTTNG_OK; + } + ret = LTTNG_OK; end: return ret; } @@ -3699,7 +3731,7 @@ void cmd_list_lttng_sessions(struct lttng_session *sessions, unsigned int i = 0; struct ltt_session *session; struct ltt_session_list *list = session_get_list(); - struct lttng_session_extended *extended = + struct lttng_session_extended *extended = (typeof(extended)) (&sessions[session_count]); DBG("Getting all available session for UID %d GID %d", @@ -3715,7 +3747,7 @@ void cmd_list_lttng_sessions(struct lttng_session *sessions, /* * Only list the sessions the user can control. */ - if (!session_access_ok(session, uid, gid) || + if (!session_access_ok(session, uid) || session->destroyed) { session_put(session); continue; @@ -4257,13 +4289,18 @@ end: } int cmd_register_trigger(struct command_ctx *cmd_ctx, int sock, - struct notification_thread_handle *notification_thread) + struct notification_thread_handle *notification_thread, + struct lttng_trigger **return_trigger) { int ret; size_t trigger_len; ssize_t sock_recv_len; struct lttng_trigger *trigger = NULL; struct lttng_payload trigger_payload; + struct lttng_credentials cmd_creds = { + .uid = LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx->creds.uid), + .gid = LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx->creds.gid), + }; lttng_payload_init(&trigger_payload); trigger_len = (size_t) cmd_ctx->lsm.u.trigger.length; @@ -4278,8 +4315,7 @@ int cmd_register_trigger(struct command_ctx *cmd_ctx, int sock, sock, trigger_payload.buffer.data, trigger_len); if (sock_recv_len < 0 || sock_recv_len != trigger_len) { ERR("Failed to receive \"register trigger\" command payload"); - /* TODO: should this be a new error enum ? */ - ret = LTTNG_ERR_INVALID_TRIGGER; + ret = LTTNG_ERR_INVALID_PROTOCOL; goto end; } @@ -4310,8 +4346,50 @@ int cmd_register_trigger(struct command_ctx *cmd_ctx, int sock, } } + /* + * Validate the trigger credentials against the command credentials. + * Only the root user can register a trigger with non-matching + * credentials. + */ + if (!lttng_credentials_is_equal_uid( + lttng_trigger_get_credentials(trigger), + &cmd_creds)) { + if (lttng_credentials_get_uid(&cmd_creds) != 0) { + ERR("Trigger credentials do not match the command credentials"); + ret = LTTNG_ERR_INVALID_TRIGGER; + goto end; + } + } + + /* + * 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 + * other user holds a reference. + * + * The trigger is modified during the execution of the + * "register trigger" command. However, by the time the command returns, + * it is safe to use without any locking as its properties are + * immutable. + */ ret = notification_thread_command_register_trigger(notification_thread, trigger); + if (ret != LTTNG_OK) { + goto end_notification_thread; + } + + /* Return an updated trigger to the client. */ + *return_trigger = trigger; + +end_notification_thread: /* Ownership of trigger was transferred. */ trigger = NULL; end: @@ -4328,6 +4406,10 @@ int cmd_unregister_trigger(struct command_ctx *cmd_ctx, int sock, ssize_t sock_recv_len; struct lttng_trigger *trigger = NULL; struct lttng_payload trigger_payload; + struct lttng_credentials cmd_creds = { + .uid = LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx->creds.uid), + .gid = LTTNG_OPTIONAL_INIT_VALUE(cmd_ctx->creds.gid), + }; lttng_payload_init(&trigger_payload); trigger_len = (size_t) cmd_ctx->lsm.u.trigger.length; @@ -4373,6 +4455,21 @@ int cmd_unregister_trigger(struct command_ctx *cmd_ctx, int sock, } } + /* + * Validate the trigger credentials against the command credentials. + * Only the root user can unregister a trigger with non-matching + * credentials. + */ + if (!lttng_credentials_is_equal_uid( + lttng_trigger_get_credentials(trigger), + &cmd_creds)) { + if (lttng_credentials_get_uid(&cmd_creds) != 0) { + ERR("Trigger credentials do not match the command credentials"); + ret = LTTNG_ERR_INVALID_TRIGGER; + goto end; + } + } + ret = notification_thread_command_unregister_trigger(notification_thread, trigger); end: @@ -4381,6 +4478,29 @@ end: return ret; } +int cmd_list_triggers(struct command_ctx *cmd_ctx, + struct notification_thread_handle *notification_thread, + struct lttng_triggers **return_triggers) +{ + int ret = 0; + enum lttng_error_code ret_code; + struct lttng_triggers *triggers = NULL; + + /* Get the set of triggers from the notification thread. */ + ret_code = notification_thread_command_list_triggers( + notification_thread, cmd_ctx->creds.uid, &triggers); + if (ret_code != LTTNG_OK) { + ret = ret_code; + goto end; + } + + *return_triggers = triggers; + triggers = NULL; + ret = LTTNG_OK; +end: + lttng_triggers_destroy(triggers); + return ret; +} /* * Send relayd sockets from snapshot output to consumer. Ignore request if the * snapshot output is *not* set with a remote destination. @@ -4996,7 +5116,7 @@ int cmd_rotate_session(struct ltt_session *session, cmd_ret = LTTNG_ERR_CREATE_DIR_FAIL; goto error; } - } + } /* * The current trace chunk becomes the chunk being archived.