Skip to content

Commit 0a73336

Browse files
berrangemstsirkin
authored andcommitted
net: don't poke at chardev internal QemuOpts
The vhost-user & colo code is poking at the QemuOpts instance in the CharDriverState struct, not realizing that it is valid for this to be NULL. e.g. the following crash shows a codepath where it will be NULL: Program terminated with signal SIGSEGV, Segmentation fault. #0 0x000055baf6ab4adc in qemu_opt_foreach (opts=0x0, func=0x55baf696b650 <net_vhost_chardev_opts>, opaque=0x7ffc51368c00, errp=0x7ffc51368e48) at util/qemu-option.c:617 617 QTAILQ_FOREACH(opt, &opts->head, next) { [Current thread is 1 (Thread 0x7f1d4970bb40 (LWP 6603))] (gdb) bt #0 0x000055baf6ab4adc in qemu_opt_foreach (opts=0x0, func=0x55baf696b650 <net_vhost_chardev_opts>, opaque=0x7ffc51368c00, errp=0x7ffc51368e48) at util/qemu-option.c:617 #1 0x000055baf696b7da in net_vhost_parse_chardev (opts=0x55baf8ff9260, errp=0x7ffc51368e48) at net/vhost-user.c:314 #2 0x000055baf696b985 in net_init_vhost_user (netdev=0x55baf8ff9250, name=0x55baf879d270 "hostnet2", peer=0x0, errp=0x7ffc51368e48) at net/vhost-user.c:360 #3 0x000055baf6960216 in net_client_init1 (object=0x55baf8ff9250, is_netdev=true, errp=0x7ffc51368e48) at net/net.c:1051 #4 0x000055baf6960518 in net_client_init (opts=0x55baf776e7e0, is_netdev=true, errp=0x7ffc51368f00) at net/net.c:1108 #5 0x000055baf696083f in netdev_add (opts=0x55baf776e7e0, errp=0x7ffc51368f00) at net/net.c:1186 qemu#6 0x000055baf69608c7 in qmp_netdev_add (qdict=0x55baf7afaf60, ret=0x7ffc51368f50, errp=0x7ffc51368f48) at net/net.c:1205 qemu#7 0x000055baf6622135 in handle_qmp_command (parser=0x55baf77fb590, tokens=0x7f1d24011960) at /path/to/qemu.git/monitor.c:3978 qemu#8 0x000055baf6a9d099 in json_message_process_token (lexer=0x55baf77fb598, input=0x55baf75acd20, type=JSON_RCURLY, x=113, y=19) at qobject/json-streamer.c:105 qemu#9 0x000055baf6abf7aa in json_lexer_feed_char (lexer=0x55baf77fb598, ch=125 '}', flush=false) at qobject/json-lexer.c:319 qemu#10 0x000055baf6abf8f2 in json_lexer_feed (lexer=0x55baf77fb598, buffer=0x7ffc51369170 "}R\204\367\272U", size=1) at qobject/json-lexer.c:369 qemu#11 0x000055baf6a9d13c in json_message_parser_feed (parser=0x55baf77fb590, buffer=0x7ffc51369170 "}R\204\367\272U", size=1) at qobject/json-streamer.c:124 qemu#12 0x000055baf66221f7 in monitor_qmp_read (opaque=0x55baf77fb530, buf=0x7ffc51369170 "}R\204\367\272U", size=1) at /path/to/qemu.git/monitor.c:3994 qemu#13 0x000055baf6757014 in qemu_chr_be_write_impl (s=0x55baf7610a40, buf=0x7ffc51369170 "}R\204\367\272U", len=1) at qemu-char.c:387 qemu#14 0x000055baf6757076 in qemu_chr_be_write (s=0x55baf7610a40, buf=0x7ffc51369170 "}R\204\367\272U", len=1) at qemu-char.c:399 qemu#15 0x000055baf675b3b0 in tcp_chr_read (chan=0x55baf90244b0, cond=G_IO_IN, opaque=0x55baf7610a40) at qemu-char.c:2927 qemu#16 0x000055baf6a5d655 in qio_channel_fd_source_dispatch (source=0x55baf7610df0, callback=0x55baf675b25a <tcp_chr_read>, user_data=0x55baf7610a40) at io/channel-watch.c:84 qemu#17 0x00007f1d3e80cbbd in g_main_context_dispatch () from /usr/lib64/libglib-2.0.so.0 qemu#18 0x000055baf69d3720 in glib_pollfds_poll () at main-loop.c:213 qemu#19 0x000055baf69d37fd in os_host_main_loop_wait (timeout=126000000) at main-loop.c:258 qemu#20 0x000055baf69d38ad in main_loop_wait (nonblocking=0) at main-loop.c:506 qemu#21 0x000055baf676587b in main_loop () at vl.c:1908 qemu#22 0x000055baf676d3bf in main (argc=101, argv=0x7ffc5136a6c8, envp=0x7ffc5136a9f8) at vl.c:4604 (gdb) p opts $1 = (QemuOpts *) 0x0 The crash occurred when attaching vhost-user net via QMP: { "execute": "chardev-add", "arguments": { "id": "charnet2", "backend": { "type": "socket", "data": { "addr": { "type": "unix", "data": { "path": "/var/run/openvswitch/vhost-user1" } }, "wait": false, "server": false } } }, "id": "libvirt-19" } { "return": { }, "id": "libvirt-19" } { "execute": "netdev_add", "arguments": { "type": "vhost-user", "chardev": "charnet2", "id": "hostnet2" }, "id": "libvirt-20" } Code using chardevs should not be poking at the internals of the CharDriverState struct. What vhost-user wants is a chardev that is operating as reconnectable network service, along with the ability to do FD passing over the connection. The colo code simply wants a network service. Add a feature concept to the char drivers so that chardev users can query the actual features they wish to have supported. The QemuOpts member is removed to prevent future mistakes in this area. Signed-off-by: Daniel P. Berrange <[email protected]> Reviewed-by: Marc-André Lureau <[email protected]> Reviewed-by: Michael S. Tsirkin <[email protected]> Signed-off-by: Michael S. Tsirkin <[email protected]>
1 parent ad14a46 commit 0a73336

File tree

5 files changed

+49
-66
lines changed

5 files changed

+49
-66
lines changed

hmp.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1909,6 +1909,7 @@ void hmp_chardev_add(Monitor *mon, const QDict *qdict)
19091909
error_setg(&err, "Parsing chardev args failed");
19101910
} else {
19111911
qemu_chr_new_from_opts(opts, NULL, &err);
1912+
qemu_opts_del(opts);
19121913
}
19131914
hmp_handle_error(mon, &err);
19141915
}

include/sysemu/char.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "qapi/qmp/qobject.h"
1010
#include "qapi/qmp/qstring.h"
1111
#include "qemu/main-loop.h"
12+
#include "qemu/bitmap.h"
1213

1314
/* character device */
1415

@@ -58,6 +59,20 @@ struct ParallelIOArg {
5859

5960
typedef void IOEventHandler(void *opaque, int event);
6061

62+
typedef enum {
63+
/* Whether the chardev peer is able to close and
64+
* reopen the data channel, thus requiring support
65+
* for qemu_chr_wait_connected() to wait for a
66+
* valid connection */
67+
QEMU_CHAR_FEATURE_RECONNECTABLE,
68+
/* Whether it is possible to send/recv file descriptors
69+
* over the data channel */
70+
QEMU_CHAR_FEATURE_FD_PASS,
71+
72+
QEMU_CHAR_FEATURE_LAST,
73+
} CharDriverFeature;
74+
75+
6176
struct CharDriverState {
6277
QemuMutex chr_write_lock;
6378
void (*init)(struct CharDriverState *s);
@@ -93,8 +108,8 @@ struct CharDriverState {
93108
int avail_connections;
94109
int is_mux;
95110
guint fd_in_tag;
96-
QemuOpts *opts;
97111
bool replay;
112+
DECLARE_BITMAP(features, QEMU_CHAR_FEATURE_LAST);
98113
QTAILQ_ENTRY(CharDriverState) next;
99114
};
100115

@@ -437,6 +452,10 @@ int qemu_chr_add_client(CharDriverState *s, int fd);
437452
CharDriverState *qemu_chr_find(const char *name);
438453
bool chr_is_ringbuf(const CharDriverState *chr);
439454

455+
bool qemu_chr_has_feature(CharDriverState *chr,
456+
CharDriverFeature feature);
457+
void qemu_chr_set_feature(CharDriverState *chr,
458+
CharDriverFeature feature);
440459
QemuOpts *qemu_chr_parse_compat(const char *label, const char *filename);
441460

442461
void register_char_driver(const char *name, ChardevBackendKind kind,

net/colo-compare.c

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -564,29 +564,6 @@ static void compare_sec_rs_finalize(SocketReadState *sec_rs)
564564
}
565565
}
566566

567-
static int compare_chardev_opts(void *opaque,
568-
const char *name, const char *value,
569-
Error **errp)
570-
{
571-
CompareChardevProps *props = opaque;
572-
573-
if (strcmp(name, "backend") == 0 &&
574-
strcmp(value, "socket") == 0) {
575-
props->is_socket = true;
576-
return 0;
577-
} else if (strcmp(name, "host") == 0 ||
578-
(strcmp(name, "port") == 0) ||
579-
(strcmp(name, "server") == 0) ||
580-
(strcmp(name, "wait") == 0) ||
581-
(strcmp(name, "path") == 0)) {
582-
return 0;
583-
} else {
584-
error_setg(errp,
585-
"COLO-compare does not support a chardev with option %s=%s",
586-
name, value);
587-
return -1;
588-
}
589-
}
590567

591568
/*
592569
* Return 0 is success.
@@ -606,12 +583,9 @@ static int find_and_check_chardev(CharDriverState **chr,
606583
}
607584

608585
memset(&props, 0, sizeof(props));
609-
if (qemu_opt_foreach((*chr)->opts, compare_chardev_opts, &props, errp)) {
610-
return 1;
611-
}
612586

613-
if (!props.is_socket) {
614-
error_setg(errp, "chardev \"%s\" is not a tcp socket",
587+
if (!qemu_chr_has_feature(*chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) {
588+
error_setg(errp, "chardev \"%s\" is not reconnectable",
615589
chr_name);
616590
return 1;
617591
}

net/vhost-user.c

Lines changed: 7 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ typedef struct VhostUserState {
2727
bool started;
2828
} VhostUserState;
2929

30-
typedef struct VhostUserChardevProps {
31-
bool is_socket;
32-
bool is_unix;
33-
} VhostUserChardevProps;
34-
3530
VHostNetState *vhost_user_get_vhost_net(NetClientState *nc)
3631
{
3732
VhostUserState *s = DO_UPCAST(VhostUserState, nc, nc);
@@ -278,45 +273,23 @@ static int net_vhost_user_init(NetClientState *peer, const char *device,
278273
return 0;
279274
}
280275

281-
static int net_vhost_chardev_opts(void *opaque,
282-
const char *name, const char *value,
283-
Error **errp)
284-
{
285-
VhostUserChardevProps *props = opaque;
286-
287-
if (strcmp(name, "backend") == 0 && strcmp(value, "socket") == 0) {
288-
props->is_socket = true;
289-
} else if (strcmp(name, "path") == 0) {
290-
props->is_unix = true;
291-
} else if (strcmp(name, "server") == 0) {
292-
} else {
293-
error_setg(errp,
294-
"vhost-user does not support a chardev with option %s=%s",
295-
name, value);
296-
return -1;
297-
}
298-
return 0;
299-
}
300-
301-
static CharDriverState *net_vhost_parse_chardev(
276+
static CharDriverState *net_vhost_claim_chardev(
302277
const NetdevVhostUserOptions *opts, Error **errp)
303278
{
304279
CharDriverState *chr = qemu_chr_find(opts->chardev);
305-
VhostUserChardevProps props;
306280

307281
if (chr == NULL) {
308282
error_setg(errp, "chardev \"%s\" not found", opts->chardev);
309283
return NULL;
310284
}
311285

312-
/* inspect chardev opts */
313-
memset(&props, 0, sizeof(props));
314-
if (qemu_opt_foreach(chr->opts, net_vhost_chardev_opts, &props, errp)) {
286+
if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE)) {
287+
error_setg(errp, "chardev \"%s\" is not reconnectable",
288+
opts->chardev);
315289
return NULL;
316290
}
317-
318-
if (!props.is_socket || !props.is_unix) {
319-
error_setg(errp, "chardev \"%s\" is not a unix socket",
291+
if (!qemu_chr_has_feature(chr, QEMU_CHAR_FEATURE_FD_PASS)) {
292+
error_setg(errp, "chardev \"%s\" does not support FD passing",
320293
opts->chardev);
321294
return NULL;
322295
}
@@ -357,7 +330,7 @@ int net_init_vhost_user(const Netdev *netdev, const char *name,
357330
assert(netdev->type == NET_CLIENT_DRIVER_VHOST_USER);
358331
vhost_user_opts = &netdev->u.vhost_user;
359332

360-
chr = net_vhost_parse_chardev(vhost_user_opts, errp);
333+
chr = net_vhost_claim_chardev(vhost_user_opts, errp);
361334
if (!chr) {
362335
return -1;
363336
}

qemu-char.c

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3996,7 +3996,6 @@ CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
39963996
}
39973997

39983998
chr = qemu_chr_find(id);
3999-
chr->opts = opts;
40003999

40014000
qapi_out:
40024001
qapi_free_ChardevBackend(backend);
@@ -4005,7 +4004,6 @@ CharDriverState *qemu_chr_new_from_opts(QemuOpts *opts,
40054004
return chr;
40064005

40074006
err:
4008-
qemu_opts_del(opts);
40094007
return NULL;
40104008
}
40114009

@@ -4033,6 +4031,7 @@ CharDriverState *qemu_chr_new_noreplay(const char *label, const char *filename,
40334031
qemu_chr_fe_claim_no_fail(chr);
40344032
monitor_init(chr, MONITOR_USE_READLINE);
40354033
}
4034+
qemu_opts_del(opts);
40364035
return chr;
40374036
}
40384037

@@ -4132,7 +4131,6 @@ static void qemu_chr_free_common(CharDriverState *chr)
41324131
{
41334132
g_free(chr->filename);
41344133
g_free(chr->label);
4135-
qemu_opts_del(chr->opts);
41364134
if (chr->logfd != -1) {
41374135
close(chr->logfd);
41384136
}
@@ -4513,6 +4511,11 @@ static CharDriverState *qmp_chardev_open_socket(const char *id,
45134511

45144512
s->addr = QAPI_CLONE(SocketAddress, sock->addr);
45154513

4514+
qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_RECONNECTABLE);
4515+
if (s->is_unix) {
4516+
qemu_chr_set_feature(chr, QEMU_CHAR_FEATURE_FD_PASS);
4517+
}
4518+
45164519
chr->opaque = s;
45174520
chr->chr_wait_connected = tcp_chr_wait_connected;
45184521
chr->chr_write = tcp_chr_write;
@@ -4596,6 +4599,19 @@ static CharDriverState *qmp_chardev_open_udp(const char *id,
45964599
return qemu_chr_open_udp(sioc, common, errp);
45974600
}
45984601

4602+
4603+
bool qemu_chr_has_feature(CharDriverState *chr,
4604+
CharDriverFeature feature)
4605+
{
4606+
return test_bit(feature, chr->features);
4607+
}
4608+
4609+
void qemu_chr_set_feature(CharDriverState *chr,
4610+
CharDriverFeature feature)
4611+
{
4612+
return set_bit(feature, chr->features);
4613+
}
4614+
45994615
ChardevReturn *qmp_chardev_add(const char *id, ChardevBackend *backend,
46004616
Error **errp)
46014617
{

0 commit comments

Comments
 (0)