Skip to content

Commit

Permalink
Check return value of snprintf() and unlink()
Browse files Browse the repository at this point in the history
No change in behavior if nothing goes wrong.
  • Loading branch information
DemiMarie committed Apr 11, 2024
1 parent d9fca57 commit 4f278aa
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 16 deletions.
4 changes: 3 additions & 1 deletion agent/qrexec-agent.c
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,9 @@ static void handle_trigger_io(void)
if (command[command_len-1] != '\0')
goto error;

snprintf(params.request_id.ident, sizeof(params.request_id), "SOCKET%d", client_fd);
int res = snprintf(params.request_id.ident, sizeof(params.request_id), "SOCKET%d", client_fd);
if (res < 0 || res >= (int)sizeof(params.request_id))
abort();
if (libvchan_send(ctrl_vchan, &hdr, sizeof(hdr)) != sizeof(hdr))
handle_vchan_error("write hdr");
if (libvchan_send(ctrl_vchan, &params, sizeof(params)) != sizeof(params))
Expand Down
3 changes: 1 addition & 2 deletions agent/qrexec-client-vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,7 @@ int main(int argc, char **argv)
strncpy(params.target_domain, argv[optind],
sizeof(params.target_domain) - 1);

snprintf(params.request_id.ident,
sizeof(params.request_id.ident), "SOCKET");
memcpy(params.request_id.ident, "SOCKET", sizeof("SOCKET"));

if (!write_all(trigger_fd, &hdr, sizeof(hdr))) {
PERROR("write(hdr) to agent");
Expand Down
11 changes: 8 additions & 3 deletions daemon/qrexec-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ static int handle_daemon_handshake(int fd)

static int connect_unix_socket(const char *domname)
{
int s, len;
int s, len, res;
struct sockaddr_un remote;

if ((s = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) {
Expand All @@ -180,8 +180,13 @@ static int connect_unix_socket(const char *domname)
}

remote.sun_family = AF_UNIX;
snprintf(remote.sun_path, sizeof remote.sun_path,
"%s/qrexec.%s", socket_dir, domname);
res = snprintf(remote.sun_path, sizeof remote.sun_path,
"%s/qrexec.%s", socket_dir, domname);
if (res < 0)
err(1, "snprintf");
if (res >= (int)sizeof(remote.sun_path))
errx(1, "%s/qrexec.%s is too long for AF_UNIX socket path",
socket_dir, domname);
len = strlen(remote.sun_path) + sizeof(remote.sun_family);
if (connect(s, (struct sockaddr *) &remote, len) == -1) {
PERROR("connect");
Expand Down
31 changes: 21 additions & 10 deletions daemon/qrexec-daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,8 +163,12 @@ static void unlink_qrexec_socket(void)
"%s/qrexec.%s", socket_dir, remote_domain_name);
if (v < (int)sizeof("/qrexec.") || v >= (int)sizeof(link_to_socket_name))
abort();
unlink(socket_address);
unlink(link_to_socket_name);
v = unlink(socket_address);
if (v != 0 && !(v == -1 && errno == ENOENT))
err(1, "unlink(%s)", socket_address);
v = unlink(link_to_socket_name);
if (v != 0 && !(v == -1 && errno == ENOENT))
err(1, "unlink(%s)", link_to_socket_name);
}

static void handle_vchan_error(const char *op)
Expand All @@ -178,12 +182,17 @@ static int create_qrexec_socket(int domid, const char *domname)
{
char socket_address[40];
char link_to_socket_name[strlen(domname) + sizeof(socket_address)];

snprintf(socket_address, sizeof(socket_address),
"%s/qrexec.%d", socket_dir, domid);
snprintf(link_to_socket_name, sizeof link_to_socket_name,
"%s/qrexec.%s", socket_dir, domname);
unlink(link_to_socket_name);
int res;

if ((unsigned)snprintf(socket_address, sizeof(socket_address),
"%s/qrexec.%d", socket_dir, domid) >= sizeof(socket_address))
errx(1, "socket name too long");
if ((unsigned)snprintf(link_to_socket_name, sizeof link_to_socket_name,
"%s/qrexec.%s", socket_dir, domname) >= sizeof link_to_socket_name)
errx(1, "socket link name too long");
res = unlink(link_to_socket_name);
if (res != 0 && !(res == -1 && errno == ENOENT))
err(1, "unlink(%s)", link_to_socket_name);

/* When running as root, make the socket accessible; perms on /var/run/qubes still apply */
umask(0);
Expand Down Expand Up @@ -330,8 +339,10 @@ static void init(int xid)
close(0);

if (!opt_direct) {
snprintf(qrexec_error_log_name, sizeof(qrexec_error_log_name),
"/var/log/qubes/qrexec.%s.log", remote_domain_name);
if ((unsigned)snprintf(qrexec_error_log_name, sizeof(qrexec_error_log_name),
"/var/log/qubes/qrexec.%s.log", remote_domain_name) >=
sizeof(qrexec_error_log_name))
errx(1, "remote domain name too long");
umask(0007); // make the log readable by the "qubes" group
logfd =
open(qrexec_error_log_name, O_WRONLY | O_CREAT | O_TRUNC,
Expand Down

0 comments on commit 4f278aa

Please sign in to comment.