Don't allow slashes and dots in overriden trace chunk names
[lttng-tools.git] / src / bin / lttng-relayd / cmd-2-11.c
1 /*
2 * Copyright (C) 2018 - Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License, version 2 only, as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
11 * more details.
12 *
13 * You should have received a copy of the GNU General Public License along with
14 * this program; if not, write to the Free Software Foundation, Inc., 51
15 * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
16 */
17
18 #define _LGPL_SOURCE
19 #include <assert.h>
20 #include <inttypes.h>
21
22 #include <common/common.h>
23 #include <common/sessiond-comm/relayd.h>
24
25 #include <common/compat/endian.h>
26 #include <common/compat/string.h>
27 #include <lttng/constant.h>
28
29 #include "cmd-2-11.h"
30 #include "utils.h"
31
32 int cmd_create_session_2_11(const struct lttng_buffer_view *payload,
33 char *session_name, char *hostname,
34 uint32_t *live_timer, bool *snapshot,
35 uint64_t *id_sessiond, lttng_uuid sessiond_uuid,
36 bool *has_current_chunk, uint64_t *current_chunk_id)
37 {
38 int ret;
39 struct lttcomm_relayd_create_session_2_11 header;
40 size_t header_len, received_names_size;
41 struct lttng_buffer_view session_name_view;
42 struct lttng_buffer_view hostname_view;
43
44 header_len = sizeof(header);
45
46 if (payload->size < header_len) {
47 ERR("Unexpected payload size in \"cmd_create_session_2_11\": expected >= %zu bytes, got %zu bytes",
48 header_len, payload->size);
49 ret = -1;
50 goto error;
51 }
52 memcpy(&header, payload->data, header_len);
53
54 header.session_name_len = be32toh(header.session_name_len);
55 header.hostname_len = be32toh(header.hostname_len);
56 header.live_timer = be32toh(header.live_timer);
57 header.current_chunk_id.value = be64toh(header.current_chunk_id.value);
58 header.current_chunk_id.is_set = !!header.current_chunk_id.is_set;
59
60 lttng_uuid_copy(sessiond_uuid, header.sessiond_uuid);
61
62 received_names_size = header.session_name_len + header.hostname_len;
63 if (payload->size < header_len + received_names_size) {
64 ERR("Unexpected payload size in \"cmd_create_session_2_11\": expected >= %zu bytes, got %zu bytes",
65 header_len + received_names_size, payload->size);
66 ret = -1;
67 goto error;
68 }
69
70 /* Validate length against defined constant. */
71 if (header.session_name_len > LTTNG_NAME_MAX) {
72 ret = -ENAMETOOLONG;
73 ERR("Length of session name (%" PRIu32 " bytes) received in create_session command exceeds maximum length (%d bytes)", header.session_name_len, LTTNG_NAME_MAX);
74 goto error;
75 }
76 if (header.hostname_len > LTTNG_HOST_NAME_MAX) {
77 ret = -ENAMETOOLONG;
78 ERR("Length of hostname (%" PRIu32 " bytes) received in create_session command exceeds maximum length (%d bytes)", header.hostname_len, LTTNG_HOST_NAME_MAX);
79 goto error;
80 }
81
82 session_name_view = lttng_buffer_view_from_view(payload, header_len,
83 header.session_name_len);
84 hostname_view = lttng_buffer_view_from_view(payload,
85 header_len + header.session_name_len, header.hostname_len);
86
87 /* Validate that names are NULL terminated. */
88 if (session_name_view.data[session_name_view.size - 1] != '\0') {
89 ERR("cmd_create_session_2_11 session_name is invalid (not NULL terminated)");
90 ret = -1;
91 goto error;
92 }
93
94 if (hostname_view.data[hostname_view.size - 1] != '\0') {
95 ERR("cmd_create_session_2_11 hostname is invalid (not NULL terminated)");
96 ret = -1;
97 goto error;
98 }
99
100 /*
101 * Length and null-termination check are already performed.
102 * LTTNG_NAME_MAX and LTTNG_HOST_NAME_MAX max size are expected.
103 */
104 strcpy(session_name, session_name_view.data);
105 strcpy(hostname, hostname_view.data);
106
107 *live_timer = header.live_timer;
108 *snapshot = !!header.snapshot;
109 *current_chunk_id = header.current_chunk_id.value;
110 *has_current_chunk = header.current_chunk_id.is_set;
111
112 ret = 0;
113
114 error:
115 return ret;
116 }
117
118 /*
119 * cmd_recv_stream_2_11 allocates path_name and channel_name.
120 */
121 int cmd_recv_stream_2_11(const struct lttng_buffer_view *payload,
122 char **ret_path_name, char **ret_channel_name,
123 uint64_t *tracefile_size, uint64_t *tracefile_count,
124 uint64_t *trace_archive_id)
125 {
126 int ret;
127 struct lttcomm_relayd_add_stream_2_11 header;
128 size_t header_len, received_names_size;
129 struct lttng_buffer_view channel_name_view;
130 struct lttng_buffer_view pathname_view;
131 char *path_name = NULL;
132 char *channel_name = NULL;
133
134 header_len = sizeof(header);
135
136 if (payload->size < header_len) {
137 ERR("Unexpected payload size in \"cmd_recv_stream_2_11\": expected >= %zu bytes, got %zu bytes",
138 header_len, payload->size);
139 ret = -1;
140 goto error;
141 }
142 memcpy(&header, payload->data, header_len);
143
144 header.channel_name_len = be32toh(header.channel_name_len);
145 header.pathname_len = be32toh(header.pathname_len);
146 header.tracefile_size = be64toh(header.tracefile_size);
147 header.tracefile_count = be64toh(header.tracefile_count);
148 header.trace_archive_id = be64toh(header.trace_archive_id);
149
150 received_names_size = header.channel_name_len + header.pathname_len;
151 if (payload->size < header_len + received_names_size) {
152 ERR("Unexpected payload size in \"cmd_recv_stream_2_11\": expected >= %zu bytes, got %zu bytes",
153 header_len + received_names_size, payload->size);
154 ret = -1;
155 goto error;
156 }
157
158 /* Validate length against defined constant. */
159 if (header.channel_name_len > DEFAULT_STREAM_NAME_LEN) {
160 ret = -ENAMETOOLONG;
161 ERR("Channel name too long");
162 goto error;
163 }
164 if (header.pathname_len > LTTNG_NAME_MAX) {
165 ret = -ENAMETOOLONG;
166 ERR("Pathname too long");
167 goto error;
168 }
169
170 /* Validate that names are (NULL terminated. */
171 channel_name_view = lttng_buffer_view_from_view(payload, header_len,
172 header.channel_name_len);
173 pathname_view = lttng_buffer_view_from_view(payload,
174 header_len + header.channel_name_len, header.pathname_len);
175
176 if (channel_name_view.data[channel_name_view.size - 1] != '\0') {
177 ERR("cmd_recv_stream_2_11 channel_name is invalid (not NULL terminated)");
178 ret = -1;
179 goto error;
180 }
181
182 if (pathname_view.data[pathname_view.size - 1] != '\0') {
183 ERR("cmd_recv_stream_2_11 patname is invalid (not NULL terminated)");
184 ret = -1;
185 goto error;
186 }
187
188 channel_name = strdup(channel_name_view.data);
189 if (!channel_name) {
190 ret = -errno;
191 PERROR("Channel name allocation");
192 goto error;
193 }
194
195 path_name = create_output_path(pathname_view.data);
196 if (!path_name) {
197 PERROR("Path name allocation");
198 ret = -ENOMEM;
199 goto error;
200 }
201
202 *tracefile_size = header.tracefile_size;
203 *tracefile_count = header.tracefile_count;
204 *trace_archive_id = header.trace_archive_id;
205 *ret_path_name = path_name;
206 *ret_channel_name = channel_name;
207 /* Move ownership to caller */
208 path_name = NULL;
209 channel_name = NULL;
210 ret = 0;
211 error:
212 free(channel_name);
213 free(path_name);
214 return ret;
215 }
This page took 0.033961 seconds and 5 git commands to generate.