./
Coverage:
low: ≥ 0%
medium: ≥ 50.0%
high: ≥ 80.0%
Lines:
183 / 219
83.6%
Branches:
89 / 113
78.8%
Functions:
20 / 23
87.0%

kfsw-services/src/remexec/remexec.c
Line Branch Exec Source
1 #include <errno.h>
2 #include <stdbool.h>
3 #include <stddef.h>
4 #include <stdint.h>
5 #include <string.h>
6
7 #include <zephyr/kernel.h>
8 #include <zephyr/shell/shell.h>
9 #include <zephyr/sys/util.h>
10
11 #define KFSW_LOG_MODULE KFSW_LOG_MODULE_REMEXEC
12 #include <kfsw/services/log.h>
13 #include <kfsw/services/remexec.h>
14
15 #include "remexec_internal.h"
16
17 /*
18 * The allowlist, the output cap and the counters. Execution goes through a
19 * shell instance of this service's own, whose transport counts every byte a
20 * command prints and keeps the first KFSW_REMEXEC_OUTPUT_MAX of them. Zephyr's
21 * dummy backend would be the obvious fixture, but it discards what overflows
22 * without saying so, which is the one thing a reply here must never do.
23 */
24
25 BUILD_ASSERT(KFSW_REMEXEC_COMMAND_MAX < CONFIG_SHELL_CMD_BUFF_SIZE,
26 "A command line must fit the shell command buffer");
27
28 /** Printable ASCII only: a control byte in a command line is a mistake. */
29 #define REMEXEC_FIRST_PRINTABLE 0x20
30 #define REMEXEC_LAST_PRINTABLE 0x7e
31
32 static struct {
33 const struct kfsw_remexec_entry *entries;
34 size_t count;
35 bool initialized;
36 struct kfsw_remexec_stats stats;
37 char last_refusal[KFSW_REMEXEC_REFUSAL_MAX];
38 char last_command[KFSW_REMEXEC_COMMAND_MAX + 1U];
39 } service;
40
41 /* One execution at a time; a second request is refused, never queued. */
42 K_MUTEX_DEFINE(remexec_executor);
43
44 static struct {
45 /** Bytes the running command printed, whether or not they were kept. */
46 size_t written;
47 size_t held;
48 char buf[KFSW_REMEXEC_OUTPUT_MAX + 1U];
49 } capture;
50
51 4 static int capture_init(const struct shell_transport *transport, const void *config,
52 shell_transport_handler_t handler, void *context)
53 {
54 ARG_UNUSED(transport);
55 ARG_UNUSED(config);
56 ARG_UNUSED(handler);
57 ARG_UNUSED(context);
58
59 4 return 0;
60 }
61
62 ✗ static int capture_uninit(const struct shell_transport *transport)
63 {
64 ARG_UNUSED(transport);
65
66 ✗ return 0;
67 }
68
69 ✗ static int capture_enable(const struct shell_transport *transport, bool blocking)
70 {
71 ARG_UNUSED(transport);
72 ARG_UNUSED(blocking);
73
74 ✗ return 0;
75 }
76
77 70 static int capture_write(const struct shell_transport *transport, const void *data, size_t length,
78 size_t *cnt)
79 {
80 70 size_t room = KFSW_REMEXEC_OUTPUT_MAX - capture.held;
81 70 size_t kept = MIN(length, room);
82
83 ARG_UNUSED(transport);
84
85 70 memcpy(&capture.buf[capture.held], data, kept);
86 70 capture.held += kept;
87 70 capture.buf[capture.held] = '\0';
88 /* Counted whole, so the reply can say how much went missing. */
89 70 capture.written += length;
90 70 *cnt = length;
91 70 return 0;
92 }
93
94 4 static int capture_read(const struct shell_transport *transport, void *data, size_t length,
95 size_t *cnt)
96 {
97 ARG_UNUSED(transport);
98 ARG_UNUSED(data);
99 ARG_UNUSED(length);
100
101 /* Nothing ever types at this shell; it exists to be executed against. */
102 4 *cnt = 0;
103 4 return 0;
104 }
105
106 static const struct shell_transport_api capture_transport_api = {
107 .init = capture_init,
108 .uninit = capture_uninit,
109 .enable = capture_enable,
110 .write = capture_write,
111 .read = capture_read,
112 };
113
114 static struct shell_transport capture_transport = {
115 .api = &capture_transport_api,
116 .ctx = &capture,
117 };
118
119 /* CRLF_DEFAULT: newlines travel as the command wrote them, so the byte count
120 * the reply reports is the byte count the command produced.
121 */
122 SHELL_DEFINE(remexec_shell, "", &capture_transport, 0, 0, SHELL_FLAG_CRLF_DEFAULT);
123
124 36 static void saturating_increment(uint32_t *counter)
125 {
126
1/2
✓ Branch 0 taken 36 times.
✗ Branch 1 not taken.
36 if (*counter != UINT32_MAX) {
127 36 (*counter)++;
128 }
129 36 }
130
131 62 static void reset_reply(struct kfsw_remexec_reply *reply)
132 {
133 62 reply->status = KFSW_REMEXEC_OK;
134 62 reply->command_result = 0;
135 62 reply->produced = 0U;
136 62 reply->dropped = 0U;
137 62 reply->truncated = false;
138 62 reply->size = 0U;
139 62 reply->text[0] = '\0';
140 62 }
141
142 /** Append @p text, keeping what fits and counting what does not. */
143 48 static void append(struct kfsw_remexec_reply *reply, const char *text)
144 {
145 48 size_t length = strlen(text);
146 48 size_t room = KFSW_REMEXEC_OUTPUT_MAX - reply->size;
147 48 size_t kept = MIN(length, room);
148
149 48 memcpy(&reply->text[reply->size], text, kept);
150 48 reply->size = (uint16_t)(reply->size + kept);
151 48 reply->text[reply->size] = '\0';
152 48 reply->produced += (uint32_t)length;
153 48 }
154
155 40 static void finish(struct kfsw_remexec_reply *reply)
156 {
157 40 reply->dropped = reply->produced - (uint32_t)reply->size;
158 40 reply->truncated = (reply->dropped != 0U);
159
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 38 times.
40 if (reply->truncated) {
160 2 saturating_increment(&service.stats.truncated);
161 }
162 40 }
163
164 /** Refuse, and leave a reason the operator and the parameter table can read. */
165 22 static void refuse(struct kfsw_remexec_reply *reply, enum kfsw_remexec_status status,
166 const char *reason)
167 {
168 22 reset_reply(reply);
169 22 reply->status = status;
170 22 append(reply, reason);
171 22 finish(reply);
172 22 (void)strncpy(service.last_refusal, reason, sizeof(service.last_refusal) - 1U);
173 22 service.last_refusal[sizeof(service.last_refusal) - 1U] = '\0';
174 22 saturating_increment(&service.stats.refused);
175 22 kfsw_log_warning("Remote execution refused: %s", service.last_refusal);
176 22 }
177
178 12 static void refuse_not_offered(struct kfsw_remexec_reply *reply, const char *command)
179 {
180 char reason[KFSW_REMEXEC_REFUSAL_MAX];
181
182 12 (void)snprintk(reason, sizeof(reason), "'%s' is not offered for remote execution", command);
183 12 refuse(reply, KFSW_REMEXEC_NOT_OFFERED, reason);
184 12 }
185
186 46 static bool printable(const char *text)
187 {
188
2/2
✓ Branch 0 taken 538 times.
✓ Branch 1 taken 42 times.
580 for (const char *cursor = text; *cursor != '\0'; cursor++) {
189
3/4
✓ Branch 0 taken 534 times.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 534 times.
538 if ((*cursor < REMEXEC_FIRST_PRINTABLE) || (*cursor > REMEXEC_LAST_PRINTABLE)) {
190 4 return false;
191 }
192 }
193 42 return true;
194 }
195
196 74 static bool matches_or_extends(const char *text, const char *prefix)
197 {
198 74 size_t length = strlen(prefix);
199
200
2/2
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 56 times.
92 return (strncmp(text, prefix, length) == 0) &&
201
4/4
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 10 times.
✓ Branch 3 taken 2 times.
18 ((text[length] == '\0') || (text[length] == ' '));
202 }
203
204 20 static const struct kfsw_remexec_entry *offered(const char *line)
205 {
206
2/2
✓ Branch 0 taken 54 times.
✓ Branch 1 taken 8 times.
62 for (size_t index = 0U; index < service.count; index++) {
207
2/2
✓ Branch 1 taken 12 times.
✓ Branch 2 taken 42 times.
54 if (matches_or_extends(line, service.entries[index].command)) {
208 12 return &service.entries[index];
209 }
210 }
211 8 return NULL;
212 }
213
214 24 static int check_entry(const struct kfsw_remexec_entry *entry, size_t index)
215 {
216
6/6
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 18 times.
✓ Branch 5 taken 2 times.
24 if ((entry->command == NULL) || (entry->help == NULL) || (entry->command[0] == '\0') ||
217
3/4
✓ Branch 0 taken 18 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 2 times.
✓ Branch 4 taken 16 times.
18 (entry->help[0] == '\0') || !printable(entry->command)) {
218 8 return -EINVAL;
219 }
220
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 14 times.
16 if (strnlen(entry->command, KFSW_REMEXEC_COMMAND_MAX + 1U) > KFSW_REMEXEC_COMMAND_MAX) {
221 2 return -ENAMETOOLONG;
222 }
223
2/2
✓ Branch 0 taken 22 times.
✓ Branch 1 taken 12 times.
34 for (size_t earlier = 0U; earlier < index; earlier++) {
224
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 20 times.
22 if (strcmp(service.entries[earlier].command, entry->command) == 0) {
225 2 return -EINVAL;
226 }
227 }
228 12 return 0;
229 }
230
231 22 int kfsw_remexec_init(const struct kfsw_remexec_allowlist *allowlist)
232 {
233 static const struct shell_backend_config_flags flags = {
234 .insert_mode = 0,
235 .echo = 0,
236 .obscure = 0,
237 .mode_delete = 0,
238 /* Escape sequences would be counted as output and confuse a reader. */
239 .use_colors = 0,
240 .use_vt100 = 0,
241 };
242 int result;
243
244
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 20 times.
22 if (service.initialized) {
245 2 return -EALREADY;
246 }
247
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 service.entries = (allowlist != NULL) ? allowlist->entries : NULL;
248
1/2
✓ Branch 0 taken 20 times.
✗ Branch 1 not taken.
20 service.count = (allowlist != NULL) ? allowlist->count : 0U;
249
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 18 times.
20 if (service.count > (size_t)CONFIG_KFSW_REMEXEC_MAX_ENTRIES) {
250 2 service.entries = NULL;
251 2 service.count = 0U;
252 2 return -E2BIG;
253 }
254
4/4
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 14 times.
18 if ((service.count != 0U) && (service.entries == NULL)) {
255 2 service.count = 0U;
256 2 return -EINVAL;
257 }
258
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 4 times.
28 for (size_t index = 0U; index < service.count; index++) {
259 24 result = check_entry(&service.entries[index], index);
260
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 12 times.
24 if (result != 0) {
261 12 service.entries = NULL;
262 12 service.count = 0U;
263 12 return result;
264 }
265 }
266
267 4 result = shell_init(&remexec_shell, NULL, flags, false, 0U);
268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (result != 0) {
269 ✗ service.entries = NULL;
270 ✗ service.count = 0U;
271 ✗ return result;
272 }
273 /* A shell that is only initialised discards everything a command
274 * prints, so start it here. CONFIG_SHELL_AUTOSTART makes its own
275 * thread race for the same call; -ENOTSUP means that thread won.
276 */
277 4 result = shell_start(&remexec_shell);
278
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
4 if ((result != 0) && (result != -ENOTSUP)) {
279 ✗ service.entries = NULL;
280 ✗ service.count = 0U;
281 ✗ return result;
282 }
283
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
4 if (!shell_ready(&remexec_shell)) {
284 ✗ service.entries = NULL;
285 ✗ service.count = 0U;
286 ✗ return -EAGAIN;
287 }
288
289 4 service.stats.entries = (uint16_t)service.count;
290 4 service.initialized = true;
291 4 kfsw_log_info("Remote execution offers %u command(s)", (unsigned int)service.count);
292 4 return 0;
293 }
294
295 4 bool kfsw_remexec_is_initialized(void)
296 {
297 4 return service.initialized;
298 }
299
300 22 void kfsw_remexec_get_stats(struct kfsw_remexec_stats *stats)
301 {
302
1/2
✓ Branch 0 taken 22 times.
✗ Branch 1 not taken.
22 if (stats != NULL) {
303 22 *stats = service.stats;
304 }
305 22 }
306
307 2 const char *kfsw_remexec_last_refusal(void)
308 {
309 2 return service.last_refusal;
310 }
311
312 ✗ const char *kfsw_remexec_last_command(void)
313 {
314 ✗ return service.last_command;
315 }
316
317 12 const char *kfsw_remexec_status_name(enum kfsw_remexec_status status)
318 {
319
2/9
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
✗ Branch 8 not taken.
12 switch (status) {
320 10 case KFSW_REMEXEC_OK:
321 10 return "ok";
322 ✗ case KFSW_REMEXEC_NOT_OFFERED:
323 ✗ return "not offered";
324 ✗ case KFSW_REMEXEC_TOO_LONG:
325 ✗ return "too long";
326 ✗ case KFSW_REMEXEC_INVALID:
327 ✗ return "invalid";
328 ✗ case KFSW_REMEXEC_BUSY:
329 ✗ return "busy";
330 ✗ case KFSW_REMEXEC_TIMEOUT:
331 ✗ return "timeout";
332 2 case KFSW_REMEXEC_FAILED:
333 2 return "failed";
334 ✗ case KFSW_REMEXEC_UNAVAILABLE:
335 ✗ return "unavailable";
336 ✗ default:
337 ✗ return "unknown";
338 }
339 }
340
341 10 void kfsw_remexec_list_local(const char *prefix, struct kfsw_remexec_reply *reply)
342 {
343
3/4
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 4 times.
10 bool narrowed = (prefix != NULL) && (prefix[0] != '\0');
344 10 size_t matches = 0U;
345
346 10 reset_reply(reply);
347
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 if (!service.initialized) {
348 ✗ refuse(reply, KFSW_REMEXEC_UNAVAILABLE, "remote execution is not initialised");
349 ✗ return;
350 }
351
4/6
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4 times.
✓ Branch 3 taken 6 times.
✗ Branch 4 not taken.
✗ Branch 6 not taken.
✓ Branch 7 taken 6 times.
10 if (narrowed && (!printable(prefix) || (strnlen(prefix, KFSW_REMEXEC_COMMAND_MAX + 1U) >
352 KFSW_REMEXEC_COMMAND_MAX))) {
353 ✗ refuse(reply, KFSW_REMEXEC_INVALID, "the requested command is not a valid name");
354 ✗ return;
355 }
356
357
2/2
✓ Branch 0 taken 30 times.
✓ Branch 1 taken 10 times.
40 for (size_t index = 0U; index < service.count; index++) {
358 30 const struct kfsw_remexec_entry *entry = &service.entries[index];
359 char line[KFSW_REMEXEC_COMMAND_MAX + KFSW_REMEXEC_REFUSAL_MAX];
360
361
4/4
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 10 times.
✓ Branch 3 taken 16 times.
✓ Branch 4 taken 4 times.
30 if (narrowed && !matches_or_extends(entry->command, prefix)) {
362 16 continue;
363 }
364 14 matches++;
365
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 10 times.
14 if (narrowed) {
366 4 (void)snprintk(line, sizeof(line), "%s\t%s\n", entry->command, entry->help);
367 } else {
368 10 (void)snprintk(line, sizeof(line), "%s\n", entry->command);
369 }
370 14 append(reply, line);
371 }
372
373 /* An empty allowlist answers with an empty listing: a node that never
374 * opted in is not an error, it offers nothing.
375 */
376
4/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 4 times.
✓ Branch 3 taken 2 times.
10 if (narrowed && (matches == 0U)) {
377 4 refuse_not_offered(reply, prefix);
378 4 return;
379 }
380 6 finish(reply);
381 }
382
383 30 void kfsw_remexec_run_local(const char *line, struct kfsw_remexec_reply *reply)
384 {
385 const struct kfsw_remexec_entry *entry;
386 int64_t started;
387 int64_t elapsed;
388 int handler_result;
389
390 30 reset_reply(reply);
391
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 28 times.
30 if (!service.initialized) {
392 2 refuse(reply, KFSW_REMEXEC_UNAVAILABLE, "remote execution is not initialised");
393 2 return;
394 }
395
4/4
✓ Branch 0 taken 26 times.
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 2 times.
✓ Branch 3 taken 24 times.
28 if ((line == NULL) || (line[0] == '\0')) {
396 4 refuse(reply, KFSW_REMEXEC_INVALID, "the request carried no command");
397 4 return;
398 }
399
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 22 times.
24 if (strnlen(line, KFSW_REMEXEC_COMMAND_MAX + 1U) > KFSW_REMEXEC_COMMAND_MAX) {
400 char reason[KFSW_REMEXEC_REFUSAL_MAX];
401
402 2 (void)snprintk(reason, sizeof(reason), "a command is at most %u bytes",
403 KFSW_REMEXEC_COMMAND_MAX);
404 2 refuse(reply, KFSW_REMEXEC_TOO_LONG, reason);
405 2 return;
406 }
407
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 20 times.
22 if (!printable(line)) {
408 2 refuse(reply, KFSW_REMEXEC_INVALID,
409 "the command holds a byte that is not printable");
410 2 return;
411 }
412 20 entry = offered(line);
413
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 12 times.
20 if (entry == NULL) {
414 8 refuse_not_offered(reply, line);
415 8 return;
416 }
417
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 12 times.
12 if (k_mutex_lock(&remexec_executor, K_NO_WAIT) != 0) {
418 ✗ refuse(reply, KFSW_REMEXEC_BUSY, "another remote execution is running");
419 ✗ return;
420 }
421
422 12 capture.written = 0U;
423 12 capture.held = 0U;
424 12 capture.buf[0] = '\0';
425 /* What was asked for, recorded before it runs: a command that never
426 returns is the one an operator most needs to see named. */
427 12 (void)strncpy(service.last_command, line, sizeof(service.last_command) - 1U);
428 12 service.last_command[sizeof(service.last_command) - 1U] = '\0';
429 12 service.stats.running = true;
430 12 started = k_uptime_get();
431 12 handler_result = shell_execute_cmd(&remexec_shell, line);
432 12 elapsed = k_uptime_get() - started;
433 12 service.stats.running = false;
434 12 service.stats.last_result = (int32_t)handler_result;
435 12 service.stats.last_duration_ms = (uint32_t)elapsed;
436
437 12 append(reply, capture.buf);
438 /* append() counted what it kept; the transport counted the whole lot. */
439 12 reply->produced = (uint32_t)capture.written;
440 12 reply->command_result = (int32_t)handler_result;
441
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (elapsed > (int64_t)CONFIG_KFSW_REMEXEC_TIMEOUT_MS) {
442 ✗ reply->status = KFSW_REMEXEC_TIMEOUT;
443
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
12 } else if (handler_result != 0) {
444 2 reply->status = KFSW_REMEXEC_FAILED;
445 } else {
446 10 reply->status = KFSW_REMEXEC_OK;
447 }
448 12 finish(reply);
449 12 saturating_increment(&service.stats.accepted);
450 12 k_mutex_unlock(&remexec_executor);
451 12 kfsw_log_info("Remote execution '%s': %s result=%d output=%u/%u", line,
452 kfsw_remexec_status_name(reply->status), handler_result,
453 (unsigned int)reply->size, (unsigned int)reply->produced);
454 }
455