Skip to content

Commit 38cad98

Browse files
committed
Implement connections to TCP-based services
Both IPv4 and IPv6 are supported. The port or both host and port can be taken from the service argument instead of the symbolic link name. Of course, there are full unit tests. Fixes: QubesOS/qubes-issues#9037
1 parent 5d4b549 commit 38cad98

File tree

5 files changed

+246
-8
lines changed

5 files changed

+246
-8
lines changed

libqrexec/exec.c

+138-4
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@
2727
#include <stddef.h>
2828
#include <limits.h>
2929

30-
#include <sys/socket.h>
3130
#include <sys/types.h>
31+
#include <sys/socket.h>
3232
#include <sys/stat.h>
3333
#include <sys/un.h>
3434
#include <sys/wait.h>
35+
#include <netdb.h>
3536
#include <unistd.h>
3637
#include <fcntl.h>
3738
#include "qrexec.h"
@@ -274,6 +275,10 @@ static int find_file(
274275
{
275276
if (target_len >= buffer_size) {
276277
/* buffer too small */
278+
LOG(ERROR, "Buffer size %zu too small for target length %zu", buffer_size, target_len);
279+
rc = -2;
280+
} else if (target_len == sizeof("/dev/tcp")) {
281+
LOG(ERROR, "/dev/tcp/ not followed by host");
277282
rc = -2;
278283
} else {
279284
memcpy(buffer, buf, target_len);
@@ -425,7 +430,7 @@ struct qrexec_parsed_command *parse_qubes_rpc_command(
425430

426431
/* Parse service name ("qubes.Service") */
427432

428-
const char *const plus = memchr(start, '+', descriptor_len);
433+
char *const plus = memchr(start, '+', descriptor_len);
429434
size_t const name_len = plus != NULL ? (size_t)(plus - start) : descriptor_len;
430435
if (name_len > NAME_MAX) {
431436
LOG(ERROR, "Service name too long to execute (length %zu)", name_len);
@@ -445,6 +450,8 @@ struct qrexec_parsed_command *parse_qubes_rpc_command(
445450
goto err;
446451
if (plus == NULL)
447452
cmd->service_descriptor[descriptor_len] = '+';
453+
else
454+
cmd->arg = cmd->service_descriptor + (plus + 1 - start);
448455

449456
/* Parse source domain */
450457

@@ -495,7 +502,7 @@ int execute_qubes_rpc_command(const char *cmdline, int *pid, int *stdin_fd,
495502
}
496503

497504
int execute_parsed_qubes_rpc_command(
498-
const struct qrexec_parsed_command *cmd, int *pid, int *stdin_fd,
505+
struct qrexec_parsed_command *cmd, int *pid, int *stdin_fd,
499506
int *stdout_fd, int *stderr_fd, struct buffer *stdin_buffer) {
500507
if (cmd->service_descriptor) {
501508
// Proper Qubes RPC call
@@ -516,9 +523,81 @@ int execute_parsed_qubes_rpc_command(
516523
pid, stdin_fd, stdout_fd, stderr_fd);
517524
}
518525
}
526+
static bool validate_port(const char *port) {
527+
#define MAXPORT "65535"
528+
#define MAXPORTLEN (sizeof MAXPORT - 1)
529+
if (*port < '1' || *port > '9')
530+
return false;
531+
const char *p = port + 1;
532+
for (; *p != '\0'; ++p) {
533+
if (*p < '0' || *p > '9')
534+
return false;
535+
}
536+
if (p - port > (ptrdiff_t)MAXPORTLEN)
537+
return false;
538+
if (p - port < (ptrdiff_t)MAXPORTLEN)
539+
return true;
540+
return memcmp(port, MAXPORT, MAXPORTLEN) <= 0;
541+
#undef MAXPORT
542+
#undef MAXPORTLEN
543+
}
544+
545+
static int qubes_tcp_connect(const char *host, const char *port)
546+
{
547+
// Work around a glibc bug: overly-large port numbers not rejected
548+
if (!validate_port(port)) {
549+
LOG(ERROR, "Invalid port number %s", port);
550+
return -1;
551+
}
552+
/* If there is ':' or '%' in the host, then this must be an IPv6 address, not IPv4. */
553+
bool const must_be_ipv6_addr = strchr(host, ':') != NULL || strchr(host, '%') != NULL;
554+
LOG(DEBUG, "Connecting to %s%s%s:%s",
555+
must_be_ipv6_addr ? "[" : "",
556+
host,
557+
must_be_ipv6_addr ? "]" : "",
558+
port);
559+
struct addrinfo hints = {
560+
.ai_flags = AI_NUMERICSERV | AI_NUMERICHOST,
561+
.ai_family = must_be_ipv6_addr ? AF_INET6 : AF_UNSPEC,
562+
.ai_socktype = SOCK_STREAM,
563+
.ai_protocol = IPPROTO_TCP,
564+
}, *addrs;
565+
int rc = getaddrinfo(host, port, &hints, &addrs);
566+
if (rc != 0) {
567+
/* data comes from symlink or from qrexec service argument, which has already
568+
* been sanitized */
569+
LOG(ERROR, "getaddrinfo(%s, %s) failed: %s", host, port, gai_strerror(rc));
570+
return -1;
571+
}
572+
rc = -1;
573+
assert(addrs != NULL && "getaddrinfo() returned zero addresses");
574+
assert(addrs->ai_next == NULL &&
575+
"getaddrinfo() returned multiple addresses despite AI_NUMERICHOST | AI_NUMERICSERV");
576+
int sockfd = socket(addrs->ai_family,
577+
addrs->ai_socktype | SOCK_CLOEXEC,
578+
addrs->ai_protocol);
579+
if (sockfd < 0)
580+
goto freeaddrs;
581+
{
582+
int one = 1;
583+
if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one) != 0)
584+
abort();
585+
}
586+
int res = connect(sockfd, addrs->ai_addr, addrs->ai_addrlen);
587+
if (res != 0) {
588+
PERROR("connect");
589+
close(sockfd);
590+
} else {
591+
rc = sockfd;
592+
LOG(DEBUG, "Connection succeeded");
593+
}
594+
freeaddrs:
595+
freeaddrinfo(addrs);
596+
return rc;
597+
}
519598

520599
bool find_qrexec_service(
521-
const struct qrexec_parsed_command *cmd,
600+
struct qrexec_parsed_command *cmd,
522601
int *socket_fd, struct buffer *stdin_buffer) {
523602
assert(cmd->service_descriptor);
524603

@@ -565,6 +644,61 @@ bool find_qrexec_service(
565644

566645
*socket_fd = s;
567646
return true;
647+
} else if (S_ISLNK(statbuf.st_mode)) {
648+
/* TCP-based service */
649+
assert(path_buffer.buflen >= (int)sizeof("/dev/tcp") - 1);
650+
assert(memcmp(path_buffer.data, "/dev/tcp", sizeof("/dev/tcp") - 1) == 0);
651+
char *address = path_buffer.data + (sizeof("/dev/tcp") - 1);
652+
char *host = NULL, *port = NULL;
653+
if (*address == '/') {
654+
host = address + 1;
655+
char *slash = strchr(host, '/');
656+
if (slash != NULL) {
657+
*slash = '\0';
658+
port = slash + 1;
659+
}
660+
} else {
661+
assert(*address == '\0');
662+
}
663+
if (port == NULL) {
664+
if (cmd->arg == NULL || *cmd->arg == '\0') {
665+
LOG(ERROR, "No or empty argument provided, cannot connect to %s",
666+
path_buffer.data);
667+
return -1;
668+
}
669+
if (host == NULL) {
670+
/* Get both host and port from service arguments */
671+
host = cmd->arg;
672+
port = strrchr(cmd->arg, '+');
673+
if (port == NULL) {
674+
LOG(ERROR, "No port provided, cannot connect to %s", cmd->arg);
675+
return -1;
676+
}
677+
*port = '\0';
678+
for (char *p = host; p < port; ++p) {
679+
if (*p == '_') {
680+
LOG(ERROR, "Underscore not allowed in hostname %s", host);
681+
return -1;
682+
}
683+
if (*p == '+')
684+
*p = ':';
685+
}
686+
port++;
687+
} else {
688+
/* Get just port from service arguments */
689+
port = cmd->arg;
690+
}
691+
} else {
692+
if (cmd->arg != NULL && *cmd->arg != '\0') {
693+
LOG(ERROR, "Unexpected argument %s to service %s", cmd->arg, path_buffer.data);
694+
return -1;
695+
}
696+
}
697+
int res = qubes_tcp_connect(host, port);
698+
if (res == -1)
699+
return false;
700+
*socket_fd = res;
701+
return true;
568702
}
569703

570704
if (euidaccess(path_buffer.data, X_OK) == 0) {

libqrexec/libqrexec-utils.h

+12-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ struct buffer {
4949
#define WRITE_STDIN_BUFFERED 1 /* something still in the buffer */
5050
#define WRITE_STDIN_ERROR 2 /* write error, errno set */
5151

52-
/* Parsed Qubes RPC or legacy command. */
52+
/* Parsed Qubes RPC or legacy command.
53+
* The size of this structure is not part of the public API or ABI.
54+
* Only use instances allocated by libqrexec-utils. */
5355
struct qrexec_parsed_command {
5456
const char *cmdline;
5557

@@ -83,6 +85,13 @@ struct qrexec_parsed_command {
8385

8486
/* For socket-based services: Should the service descriptor be sent? */
8587
bool send_service_descriptor;
88+
89+
/* Remaining fields are private to libqrexec-utils. Do not access them
90+
* directly - they may change in any update. */
91+
92+
/* Pointer to the argument, or NULL if there is no argument.
93+
* Same buffer as "service_descriptor". */
94+
char *arg;
8695
};
8796

8897
/* Parse a command, return NULL on failure. Uses cmd->cmdline
@@ -142,7 +151,7 @@ int write_stdin(int fd, const char *data, int len, struct buffer *buffer);
142151
* nonzero on failure.
143152
*/
144153
int execute_parsed_qubes_rpc_command(
145-
const struct qrexec_parsed_command *cmd, int *pid, int *stdin_fd,
154+
struct qrexec_parsed_command *cmd, int *pid, int *stdin_fd,
146155
int *stdout_fd, int *stderr_fd, struct buffer *stdin_buffer);
147156

148157
/**
@@ -157,7 +166,7 @@ int execute_parsed_qubes_rpc_command(
157166
* successfully, false on failure.
158167
*/
159168
bool find_qrexec_service(
160-
const struct qrexec_parsed_command *cmd,
169+
struct qrexec_parsed_command *cmd,
161170
int *socket_fd, struct buffer *stdin_buffer);
162171

163172
/** Suggested buffer size for the path buffer of find_qrexec_service. */

libqrexec/process_io.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ static void close_stdout(int fd, bool restore_block) {
7676
} else if (shutdown(fd, SHUT_RD) == -1) {
7777
if (errno == ENOTSOCK)
7878
close(fd);
79-
else
79+
else if (errno != ENOTCONN) /* can happen with TCP, harmless */
8080
PERROR("shutdown close_stdout");
8181
}
8282
}

qrexec/tests/socket/agent.py

+94
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import os.path
2323
import os
2424
import tempfile
25+
import socket
2526
import shutil
2627
import struct
2728
import getpass
@@ -702,6 +703,99 @@ def test_connect_socket_no_metadata(self):
702703
)
703704
self.check_dom0(dom0)
704705

706+
def test_connect_socket_tcp(self):
707+
socket_path = os.path.join(
708+
self.tempdir, "rpc", "qubes.SocketService+"
709+
)
710+
port = 65534
711+
host = "127.0.0.1"
712+
os.symlink(f"/dev/tcp/{host}/{port}", socket_path)
713+
self._test_tcp(socket.AF_INET, "qubes.SocketService", host, port)
714+
715+
def _test_tcp_raw(self, family: int, service: str, host: str, port: int, accept=True):
716+
server = socket.socket(family, socket.SOCK_STREAM, socket.IPPROTO_TCP)
717+
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
718+
server.bind((host, port))
719+
server.listen(1)
720+
server = qrexec.QrexecServer(server)
721+
self.addCleanup(server.close)
722+
723+
target, dom0 = self.execute_qubesrpc(service, "domX")
724+
if accept:
725+
server.accept()
726+
message = b"stdin data"
727+
target.send_message(qrexec.MSG_DATA_STDIN, message)
728+
target.send_message(qrexec.MSG_DATA_STDIN, b"")
729+
if accept:
730+
self.assertEqual(server.recvall(len(message)), message)
731+
server.sendall(b"stdout data")
732+
server.close()
733+
messages = target.recv_all_messages()
734+
self.check_dom0(dom0)
735+
return util.sort_messages(messages)
736+
737+
def _test_tcp(self, family: int, service: str, host: str, port: int) -> None:
738+
# No stderr
739+
self.assertListEqual(
740+
self._test_tcp_raw(family, service, host, port),
741+
[
742+
(qrexec.MSG_DATA_STDOUT, b"stdout data"),
743+
(qrexec.MSG_DATA_STDOUT, b""),
744+
(qrexec.MSG_DATA_EXIT_CODE, b"\0\0\0\0"),
745+
],
746+
)
747+
748+
def test_connect_socket_tcp_port_from_arg(self):
749+
socket_path = os.path.join(
750+
self.tempdir, "rpc", "qubes.SocketService"
751+
)
752+
port = 65533
753+
host = "127.0.0.1"
754+
os.symlink(f"/dev/tcp/{host}", socket_path)
755+
self._test_tcp(socket.AF_INET, f"qubes.SocketService+{port}", host, port)
756+
757+
def test_connect_socket_tcp_host_and_port_from_arg(self):
758+
socket_path = os.path.join(
759+
self.tempdir, "rpc", "qubes.SocketService"
760+
)
761+
port = 65535
762+
host = "127.0.0.1"
763+
os.symlink(f"/dev/tcp", socket_path)
764+
self._test_tcp(socket.AF_INET, f"qubes.SocketService+{host}+{port}", host, port)
765+
766+
def test_connect_socket_tcp_ipv6(self):
767+
socket_path = os.path.join(
768+
self.tempdir, "rpc", "qubes.SocketService"
769+
)
770+
port = 65532
771+
host = "::1"
772+
os.symlink(f"/dev/tcp", socket_path)
773+
self._test_tcp(socket.AF_INET6, f"qubes.SocketService+{host.replace(':', '+')}+{port}", host, port)
774+
775+
def _test_connect_socket_tcp_unexpected_host(self, host):
776+
socket_path = os.path.join(
777+
self.tempdir, "rpc", "qubes.SocketService"
778+
)
779+
port = 65535
780+
path = f"/dev/tcp/{host}"
781+
os.symlink(path, socket_path)
782+
messages = self._test_tcp_raw(socket.AF_INET, f"qubes.SocketService+{host}+{port}",
783+
host, port, accept=False)
784+
self.assertListEqual(
785+
messages,
786+
[
787+
(qrexec.MSG_DATA_STDOUT, b""),
788+
(qrexec.MSG_DATA_STDERR, b""),
789+
(qrexec.MSG_DATA_EXIT_CODE, b"\177\0\0\0"),
790+
],
791+
)
792+
793+
def test_connect_socket_tcp_unexpected_host(self):
794+
self._test_connect_socket_tcp_unexpected_host("127.0.0.1")
795+
796+
def test_connect_socket_tcp_empty_host(self):
797+
self._test_connect_socket_tcp_unexpected_host("")
798+
705799
def test_connect_socket(self):
706800
socket_path = os.path.join(
707801
self.tempdir, "rpc", "qubes.SocketService+arg"

qrexec/tests/socket/qrexec.py

+1
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ def socket_server(socket_path, socket_path_alt=None):
147147
except FileNotFoundError:
148148
pass
149149
server = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
150+
server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
150151
server.bind(socket_path)
151152
if socket_path_alt is not None:
152153
os.symlink(socket_path, socket_path_alt)

0 commit comments

Comments
 (0)