Debian Codegraph @

libdrgn/qemu_machine_protocol.c

raw
1 // Copyright (c) Meta Platforms, Inc. and affiliates.
2 // SPDX-License-Identifier: LGPL-2.1-or-later
3
4 #include <errno.h>
5 #include <gelf.h>
6 #include <inttypes.h>
7 #include <json-c/json.h>
8 #include <netdb.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/syscall.h>
12 #include <sys/socket.h>
13 #include <sys/un.h>
14 #include <unistd.h>
15
16 #include "cleanup.h"
17 #include "drgn_internal.h"
18 #include "elf_notes.h"
19 #include "error.h"
20 #include "hexlify.h"
21 #include "io.h"
22 #include "linux_kernel.h"
23 #include "minmax.h"
24 #include "platform.h"
25 #include "plugins.h"
26 #include "program.h"
27 #include "util.h"
28
29 #define _cleanup_json_object_ _cleanup_(json_object_putp)
30 static inline void json_object_putp(struct json_object **objp)
31 {
32 json_object_put(*objp);
33 }
34
35 void drgn_qmp_conn_deinit(struct drgn_qmp_conn *conn)
36 {
37 if (conn->json_tok)
38 json_tokener_free(conn->json_tok);
39 string_builder_deinit(&conn->write_buf);
40 string_builder_deinit(&conn->read_buf);
41 if (conn->fd >= 0)
42 close(conn->fd);
43 }
44
45 static struct drgn_error *qmp_recv_msg(struct drgn_qmp_conn *conn,
46 struct json_object **ret)
47 {
48 struct string_builder *buf = &conn->read_buf;
49 for (;;) {
50 // Check if we already have a complete line in the buffer.
51 char *newline = memchr(buf->str, '\n', buf->len);
52 if (newline) {
53 struct json_object *obj =
54 json_tokener_parse_ex(conn->json_tok, buf->str,
55 newline - buf->str);
56 if (!obj) {
57 enum json_tokener_error jerr =
58 json_tokener_get_error(conn->json_tok);
59 json_tokener_reset(conn->json_tok);
60 return drgn_error_format(DRGN_ERROR_OTHER,
61 "could not parse QMP message: %s",
62 json_tokener_error_desc(jerr));
63 }
64
65 size_t line_len = newline - buf->str + 1;
66 buf->len -= line_len;
67 memmove(buf->str, newline + 1, buf->len);
68
69 // Skip asynchronous events.
70 if (json_object_object_get_ex(obj, "event", NULL)) {
71 json_object_put(obj);
72 continue;
73 }
74 *ret = obj;
75 return NULL;
76 }
77
78 // Need more data.
79 if (!string_builder_reserve_for_append(buf, 4096))
80 return &drgn_enomem;
81 ssize_t r = read(conn->fd, buf->str + buf->len, 4096);
82 if (r < 0) {
83 if (errno == EINTR)
84 continue;
85 return drgn_error_create_os("read", errno, NULL);
86 }
87 if (r == 0) {
88 return drgn_error_create(DRGN_ERROR_OTHER,
89 "QMP connection closed unexpectedly");
90 }
91 buf->len += r;
92 }
93 }
94
95 static struct drgn_error *drgn_error_qmp(struct json_object *obj)
96 {
97 struct json_object *value;
98 const char *error_class = NULL, *error_desc = NULL;
99 if (json_object_object_get_ex(obj, "class", &value)
100 && json_object_is_type(value, json_type_string))
101 error_class = json_object_get_string(value);
102 if (json_object_object_get_ex(obj, "desc", &value)
103 && json_object_is_type(value, json_type_string))
104 error_desc = json_object_get_string(value);
105
106 if (error_class && error_desc) {
107 return drgn_error_format(DRGN_ERROR_OTHER, "QMP error: %s: %s",
108 error_class, error_desc);
109 } else if (error_class || error_desc) {
110 return drgn_error_format(DRGN_ERROR_OTHER, "QMP error: %s",
111 error_class ?: error_desc);
112 } else {
113 return drgn_error_create(DRGN_ERROR_OTHER, "unknown QMP error");
114 }
115 }
116
117 static struct drgn_error *qmp_execute_str(struct drgn_qmp_conn *conn,
118 const char *cmd, size_t cmd_len,
119 struct json_object **ret)
120 {
121 struct drgn_error *err;
122
123 if (write_all(conn->fd, cmd, cmd_len))
124 return drgn_error_create_os("write", errno, NULL);
125
126 _cleanup_json_object_ struct json_object *response = NULL;
127 err = qmp_recv_msg(conn, &response);
128 if (err)
129 return err;
130
131 struct json_object *error;
132 if (json_object_object_get_ex(response, "error", &error))
133 return drgn_error_qmp(error);
134
135 if (ret) {
136 struct json_object *return_val;
137 if (!json_object_object_get_ex(response, "return",
138 &return_val)) {
139 return drgn_error_create(DRGN_ERROR_OTHER,
140 "QMP response missing \"return\"");
141 }
142 *ret = json_object_get(return_val);
143 }
144 return NULL;
145 }
146
147 #define QMP_CMD_NO_ARGS(cmd) "{\"execute\":\"" cmd "\"}\r\n"
148 #define QMP_EXECUTE_NO_ARGS(conn, cmd, ret) \
149 qmp_execute_str(conn, QMP_CMD_NO_ARGS(cmd), sizeof(QMP_CMD_NO_ARGS(cmd)) - 1, ret)
150
151 // Send a QMP command with an fd as SCM_RIGHTS ancillary data.
152 static struct drgn_error *qmp_send_fd(struct drgn_qmp_conn *conn,
153 const char *cmd, size_t cmd_len, int fd)
154 {
155 struct drgn_error *err;
156
157 struct iovec iov = { .iov_base = (void *)cmd, .iov_len = cmd_len };
158 union {
159 char buf[CMSG_SPACE(sizeof(int))];
160 struct cmsghdr align;
161 } cmsg_buf;
162 struct msghdr msg = {
163 .msg_iov = &iov,
164 .msg_iovlen = 1,
165 .msg_control = cmsg_buf.buf,
166 .msg_controllen = sizeof(cmsg_buf.buf),
167 };
168 struct cmsghdr *cmsg = CMSG_FIRSTHDR(&msg);
169 cmsg->cmsg_level = SOL_SOCKET;
170 cmsg->cmsg_type = SCM_RIGHTS;
171 cmsg->cmsg_len = CMSG_LEN(sizeof(int));
172 memcpy(CMSG_DATA(cmsg), &fd, sizeof(int));
173
174 ssize_t r;
175 do {
176 r = sendmsg(conn->fd, &msg, 0);
177 } while (r < 0 && errno == EINTR);
178 if (r < 0)
179 return drgn_error_create_os("sendmsg", errno, NULL);
180
181 // In the unlikely case that sendmsg() didn't send the full command,
182 // send the rest with write_all(). The fd should only be sent once.
183 if (r < cmd_len && write_all(conn->fd, cmd + r, cmd_len - r))
184 return drgn_error_create_os("write", errno, NULL);
185
186 _cleanup_json_object_ struct json_object *response = NULL;
187 err = qmp_recv_msg(conn, &response);
188 if (err)
189 return err;
190
191 struct json_object *error;
192 if (json_object_object_get_ex(response, "error", &error))
193 return drgn_error_qmp(error);
194
195 return NULL;
196 }
197
198 static struct drgn_error *qmp_negotiate(struct drgn_qmp_conn *conn)
199 {
200 struct drgn_error *err;
201
202 // Read the greeting.
203 _cleanup_json_object_ struct json_object *greeting = NULL;
204 err = qmp_recv_msg(conn, &greeting);
205 if (err)
206 return err;
207 if (!json_object_object_get_ex(greeting, "QMP", NULL)) {
208 return drgn_error_create(DRGN_ERROR_OTHER,
209 "did not receive QMP greeting");
210 }
211
212 // Send qmp_capabilities to enter command mode.
213 return QMP_EXECUTE_NO_ARGS(conn, "qmp_capabilities", NULL);
214 }
215
216 static struct drgn_error *qmp_detect_platform(struct drgn_qmp_conn *conn,
217 struct drgn_platform *ret)
218 {
219 struct drgn_error *err;
220
221 _cleanup_json_object_ struct json_object *result = NULL;
222 err = QMP_EXECUTE_NO_ARGS(conn, "query-target", &result);
223 if (err)
224 return err;
225
226 struct json_object *arch_obj;
227 if (!json_object_object_get_ex(result, "arch", &arch_obj)) {
228 return drgn_error_create(DRGN_ERROR_OTHER,
229 "QMP query-target response missing \"arch\"");
230 }
231 if (!json_object_is_type(arch_obj, json_type_string)) {
232 return drgn_error_create(DRGN_ERROR_OTHER,
233 "QMP query-target \"arch\" is not string");
234 }
235
236 const char *arch = json_object_get_string(arch_obj);
237 const struct drgn_architecture_info *arch_info;
238 if (strcmp(arch, "x86_64") == 0)
239 arch_info = &arch_info_x86_64;
240 else if (strcmp(arch, "i386") == 0)
241 arch_info = &arch_info_i386;
242 else if (strcmp(arch, "aarch64") == 0)
243 arch_info = &arch_info_aarch64;
244 else if (strcmp(arch, "arm") == 0)
245 arch_info = &arch_info_arm;
246 else if (strcmp(arch, "ppc64") == 0)
247 arch_info = &arch_info_ppc64;
248 else if (strcmp(arch, "riscv64") == 0)
249 arch_info = &arch_info_riscv64;
250 else if (strcmp(arch, "riscv32") == 0)
251 arch_info = &arch_info_riscv32;
252 else if (strcmp(arch, "s390x") == 0)
253 arch_info = &arch_info_s390x;
254 else
255 arch_info = &arch_info_unknown;
256
257 ret->arch = arch_info;
258 ret->flags = arch_info->default_flags;
259 return NULL;
260 }
261
262 static struct drgn_error *parse_qemu_xp(const char *str, void *buf,
263 size_t count)
264 {
265 uint8_t *p = buf, *end = p + count;
266 const char *s = str;
267
268 while (p < end) {
269 const char *address_str = s;
270 // Skip "address:" prefix.
271 const char *colon = strchr(s, ':');
272 if (!colon) {
273 return drgn_error_create(DRGN_ERROR_OTHER,
274 "could not parse QEMU monitor xp response");
275 }
276 s = colon + 1;
277
278 // Parse hex byte values.
279 while (p < end && *s != '\0') {
280 // Skip whitespace.
281 while (*s == ' ' || *s == '\t')
282 s++;
283 if (*s == '\r' || *s == '\n' || *s == '\0')
284 break;
285
286 uint8_t lo, hi;
287 if (s[0] != '0' || (s[1] != 'x' && s[1] != 'X')
288 || !hex_digit_to_nibble(s[2], &hi)
289 || !hex_digit_to_nibble(s[3], &lo)) {
290 // We always return a fault error in this case
291 // just in case the error message from QEMU
292 // changes, but we indicate this in our own
293 // error message.
294 const char *error_msg =
295 strstartswith(s, "Cannot access memory")
296 ? "QEMU could not access memory"
297 : "could not parse QEMU monitor xp response";
298 unsigned long long address =
299 strtoull(address_str, NULL, 16);
300 return drgn_error_create_fault(error_msg,
301 address);
302 }
303 *p++ = (hi << 4) | lo;
304 s += 4;
305 }
306
307 // Skip to next line.
308 while (*s == '\r' || *s == '\n')
309 s++;
310 }
311 return NULL;
312 }
313
314 static struct drgn_error *drgn_qmp_read_memory(void *buf, uint64_t address,
315 size_t count, uint64_t offset,
316 void *arg, bool physical)
317 {
318 // Maximum number of bytes we'll read at once.
319 static const size_t xp_max_bytes = 1024;
320 struct drgn_error *err;
321 struct drgn_program *prog = arg;
322 struct drgn_qmp_conn *conn = &prog->qmp_conn;
323 uint8_t *p = buf;
324
325 drgn_blocking_guard(blocking_guard);
326
327 while (count > 0) {
328 size_t n = min(count, xp_max_bytes);
329
330 conn->write_buf.len = 0;
331 if (!string_builder_appendf(&conn->write_buf,
332 "{\"execute\":\"human-monitor-command\","
333 "\"arguments\":{\"command-line\":"
334 "\"xp /%zuxb 0x%" PRIx64 "\"}}\r\n",
335 n, address))
336 return &drgn_enomem;
337
338 _cleanup_json_object_ struct json_object *result = NULL;
339 err = qmp_execute_str(conn, conn->write_buf.str,
340 conn->write_buf.len, &result);
341 if (err)
342 return err;
343
344 if (!json_object_is_type(result, json_type_string)) {
345 return drgn_error_create(DRGN_ERROR_OTHER,
346 "QMP human-monitor-command xp return value is not string");
347 }
348
349 err = parse_qemu_xp(json_object_get_string(result), p, n);
350 if (err)
351 return err;
352
353 p += n;
354 address += n;
355 count -= n;
356 }
357 return NULL;
358 }
359
360 #define _cleanup_elf_end_ _cleanup_(elf_endp)
361 static inline void elf_endp(Elf **elfp)
362 {
363 elf_end(*elfp);
364 }
365
366 static struct drgn_error *parse_vmcoreinfo_from_dump(struct drgn_program *prog,
367 int fd)
368 {
369 struct drgn_error *err;
370
371 elf_version(EV_CURRENT);
372 _cleanup_elf_end_ Elf *elf = elf_begin(fd, ELF_C_READ, NULL);
373 if (!elf)
374 return drgn_error_libelf();
375
376 const void *desc;
377 size_t size;
378 if (find_elf_note(elf, "VMCOREINFO", 0, &desc, &size))
379 return drgn_error_libelf();
380
381 if (!desc)
382 return NULL;
383
384 err = drgn_program_parse_vmcoreinfo(prog, desc, size);
385 if (err)
386 return err;
387
388 prog->flags |= DRGN_PROGRAM_IS_LINUX_KERNEL;
389 if (prog->platform.arch->linux_kernel_pgtable_iterator_next) {
390 err = drgn_program_add_memory_segment(prog, 0, UINT64_MAX,
391 read_memory_via_pgtable,
392 prog, false);
393 if (err)
394 return err;
395 }
396 return drgn_program_finish_set_kernel(prog);
397 }
398
399 // Find a valid RAM address by parsing "info mtree -f" output.
400 static struct drgn_error *qmp_find_ram_address(struct drgn_qmp_conn *conn,
401 uint64_t *ret)
402 {
403 struct drgn_error *err;
404
405 static const char cmd[] =
406 "{\"execute\":\"human-monitor-command\","
407 "\"arguments\":{\"command-line\":"
408 "\"info mtree -f\"}}\r\n";
409 _cleanup_json_object_ struct json_object *result = NULL;
410 err = qmp_execute_str(conn, cmd, sizeof(cmd) - 1, &result);
411 if (err)
412 return err;
413
414 if (!json_object_is_type(result, json_type_string)) {
415 return drgn_error_create(DRGN_ERROR_OTHER,
416 "QMP human-monitor-command info mtree return value is not string");
417 }
418
419 // The format is " START-END (prio N, TYPE): NAME". Find a line with
420 // TYPE == "ram".
421 const char *s = json_object_get_string(result);
422 for (;;) {
423 const char *ram = strstr(s, ", ram):");
424 if (!ram) {
425 return drgn_error_create(DRGN_ERROR_OTHER,
426 "could not find RAM region in QEMU memory tree");
427 }
428 const char *line = ram;
429 while (line > s && line[-1] != '\n')
430 line--;
431 char *end;
432 uint64_t address = strtoull(line, &end, 16);
433 if (end > line && *end == '-') {
434 *ret = address;
435 return NULL;
436 }
437 // Couldn't parse this line, try the next match.
438 s = ram + 1;
439 }
440 }
441
442 // Read VMCOREINFO over QMP if possible.
443 //
444 // Unfortunately, QMP does not provide a straightforward way to read VMCOREINFO.
445 // Our extremely hacky approach is to use the dump-guest-memory command with a
446 // minimal memory range, which produces a tiny core dump that includes the
447 // VMCOREINFO note (as long as the guest is configured properly).
448 //
449 // Since the dump is written to a file, this only works if the QEMU process is
450 // local. The QEMU process may also be sandboxed, containerized, etc., so it may
451 // not be able to write to a path that we can also access.
452 //
453 // dump-guest-memory can write to an fd that is passed via SCM_RIGHTS (which
454 // requires that the QMP connection is a Unix domain socket). We do that instead
455 // (using a memfd to avoid polluting the filesystem).
456 static struct drgn_error *qmp_read_vmcoreinfo(struct drgn_program *prog)
457 {
458 struct drgn_error *err;
459 struct drgn_qmp_conn *conn = &prog->qmp_conn;
460
461 // Check that the connection is a Unix domain socket.
462 struct sockaddr sa;
463 socklen_t sa_len = sizeof(sa);
464 if (getsockname(conn->fd, &sa, &sa_len) < 0 || sa.sa_family != AF_UNIX)
465 return NULL;
466
467 // dump-guest-memory requires a valid address, and there's nothing we
468 // can hard-code that works on all architectures/machines.
469 uint64_t ram_address;
470 err = qmp_find_ram_address(conn, &ram_address);
471 if (err)
472 return err;
473
474 _cleanup_close_ int fd = syscall(SYS_memfd_create, "drgn-qmp-dump", 0U);
475 if (fd < 0)
476 return drgn_error_create_os("memfd_create", errno, NULL);
477
478 static const char getfd_cmd[] =
479 "{\"execute\":\"getfd\","
480 "\"arguments\":{\"fdname\":\"drgn-dump\"}}\r\n";
481 err = qmp_send_fd(conn, getfd_cmd, sizeof(getfd_cmd) - 1, fd);
482 if (err)
483 return err;
484
485 conn->write_buf.len = 0;
486 if (!string_builder_appendf(&conn->write_buf,
487 "{\"execute\":\"dump-guest-memory\","
488 "\"arguments\":{\"paging\":false,"
489 "\"protocol\":\"fd:drgn-dump\","
490 "\"begin\":%" PRIu64 ",\"length\":1}}\r\n",
491 ram_address))
492 return &drgn_enomem;
493 err = qmp_execute_str(conn, conn->write_buf.str, conn->write_buf.len,
494 NULL);
495 if (err) {
496 // dump-guest-memory normally closes the fd itself. If it failed
497 // before retrieving the fd, QEMU still has it from getfd, so
498 // try to close it.
499 static const char closefd_cmd[] =
500 "{\"execute\":\"closefd\","
501 "\"arguments\":{\"fdname\":\"drgn-dump\"}}\r\n";
502 struct drgn_error *close_err =
503 qmp_execute_str(conn, closefd_cmd,
504 sizeof(closefd_cmd) - 1, NULL);
505 drgn_error_destroy(close_err);
506 return err;
507 }
508
509 return parse_vmcoreinfo_from_dump(prog, fd);
510 }
511
512 static struct drgn_error *qmp_connect_unix(const char *address, int *ret)
513 {
514 struct sockaddr_un sa = { .sun_family = AF_UNIX };
515 size_t path_len = strlen(address);
516 if (path_len >= sizeof(sa.sun_path)) {
517 return drgn_error_format(DRGN_ERROR_INVALID_ARGUMENT,
518 "QMP Unix socket path is too long");
519 }
520 memcpy(sa.sun_path, address, path_len + 1);
521
522 _cleanup_close_ int fd = socket(AF_UNIX, SOCK_STREAM, 0);
523 if (fd < 0)
524 return drgn_error_create_os("socket", errno, NULL);
525
526 if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
527 return drgn_error_create_os("connect", errno, address);
528
529 *ret = fd;
530 fd = -1;
531 return NULL;
532 }
533
534 static struct drgn_error *qmp_connect_tcp(const char *address,
535 const char *colon, int *ret)
536 {
537 _cleanup_free_ char *host = NULL;
538 if (colon > address) {
539 host = strndup(address, colon - address);
540 if (!host)
541 return &drgn_enomem;
542 }
543 const char *port = colon + 1;
544
545 struct addrinfo hints = {
546 .ai_flags = AI_ADDRCONFIG,
547 .ai_socktype = SOCK_STREAM,
548 };
549 struct addrinfo *res;
550 int gai_err = getaddrinfo(host, port, &hints, &res);
551 if (gai_err == EAI_SYSTEM) {
552 return drgn_error_create_os("getaddrinfo", errno, address);
553 } else if (gai_err) {
554 return drgn_error_format(DRGN_ERROR_OTHER,
555 "getaddrinfo: %s: %s", address,
556 gai_strerror(gai_err));
557 }
558
559 int saved_errno = 0;
560 _cleanup_close_ int fd = -1;
561 for (struct addrinfo *ai = res; ai; ai = ai->ai_next) {
562 fd = socket(ai->ai_family, ai->ai_socktype, ai->ai_protocol);
563 if (fd < 0) {
564 saved_errno = errno;
565 continue;
566 }
567 if (connect(fd, ai->ai_addr, ai->ai_addrlen) == 0)
568 break;
569 saved_errno = errno;
570 close(fd);
571 fd = -1;
572 }
573 freeaddrinfo(res);
574
575 if (fd < 0)
576 return drgn_error_create_os("connect", saved_errno, address);
577
578 *ret = fd;
579 fd = -1;
580 return NULL;
581 }
582
583 LIBDRGN_PUBLIC struct drgn_error *
584 drgn_program_set_qemu_qmp(struct drgn_program *prog, const char *address)
585 {
586 struct drgn_error *err;
587 int fd;
588
589 const char *colon;
590 char *end;
591 unsigned long port;
592 if (address[0] == '/' || address[0] == '.'
593 || !(colon = strrchr(address, ':'))
594 || colon[1] < '0' || colon[1] > '9'
595 || (port = strtoul(colon + 1, &end, 10), *end != '\0')
596 || port < 1 || port > 65535)
597 err = qmp_connect_unix(address, &fd);
598 else
599 err = qmp_connect_tcp(address, colon, &fd);
600 if (err)
601 return err;
602
603 return drgn_program_set_qemu_qmp_fd(prog, fd);
604 }
605
606 LIBDRGN_PUBLIC struct drgn_error *
607 drgn_program_set_qemu_qmp_fd(struct drgn_program *prog, int fd)
608 {
609 struct drgn_error *err;
610 enum drgn_program_flags old_flags = prog->flags;
611 bool had_platform = prog->has_platform;
612 bool had_vmcoreinfo = prog->vmcoreinfo.raw;
613
614 prog->qmp_conn.fd = fd;
615 prog->qmp_conn.json_tok = json_tokener_new();
616 if (!prog->qmp_conn.json_tok) {
617 err = &drgn_enomem;
618 goto err;
619 }
620
621 err = drgn_program_check_initialized(prog);
622 if (err)
623 goto err;
624
625 err = qmp_negotiate(&prog->qmp_conn);
626 if (err)
627 goto err;
628
629 if (!had_platform) {
630 struct drgn_platform platform;
631 err = qmp_detect_platform(&prog->qmp_conn, &platform);
632 if (err)
633 goto err;
634 drgn_program_set_platform(prog, &platform);
635 }
636
637 err = drgn_program_add_memory_segment(prog, 0, UINT64_MAX,
638 drgn_qmp_read_memory, prog, true);
639 if (err)
640 goto err;
641
642 if (!had_vmcoreinfo) {
643 err = qmp_read_vmcoreinfo(prog);
644 if (err)
645 goto err;
646 }
647
648 prog->flags |= DRGN_PROGRAM_IS_LIVE;
649
650 drgn_call_plugins_prog("drgn_prog_set", prog);
651 return NULL;
652
653 err:
654 drgn_memory_reader_clear(&prog->reader);
655 drgn_qmp_conn_deinit(&prog->qmp_conn);
656 drgn_qmp_conn_init(&prog->qmp_conn);
657 if (!had_vmcoreinfo) {
658 free(prog->vmcoreinfo.raw);
659 memset(&prog->vmcoreinfo, 0, sizeof(prog->vmcoreinfo));
660 }
661 prog->has_platform = had_platform;
662 prog->flags = old_flags;
663 return err;
664 }