public inbox for connman@lists.linux.dev
 help / color / mirror / Atom feed
From: Brian Fukano <bfukano@gmail.com>
To: bfukano@gmail.com, connman@lists.linux.dev
Subject: [PATCH] dnsproxy: fix coverity issues
Date: Wed, 24 Apr 2024 11:47:51 -0700	[thread overview]
Message-ID: <20240424184751.130804-1-bfukano@gmail.com> (raw)

This patch fixes a handful of coverity issues found in dnsproxy.c.
---
 src/dnsproxy.c | 26 +++++++++++++++++++-------
 1 file changed, 19 insertions(+), 7 deletions(-)

diff --git a/src/dnsproxy.c b/src/dnsproxy.c
index a32755d5..4564d137 100644
--- a/src/dnsproxy.c
+++ b/src/dnsproxy.c
@@ -623,8 +623,11 @@ static gboolean request_timeout(gpointer user_data)
 		 * "not found" result), so send that back to client instead
 		 * of more fatal server failed error.
 		 */
-		sendto(sk, req->resp, req->resplen, MSG_NOSIGNAL,
-			sa, req->sa_len);
+		if (sendto(sk, req->resp, req->resplen, MSG_NOSIGNAL,
+			sa, req->sa_len) < 0) {
+				connman_error("Failed to send response %d: %s",
+					sk, strerror(errno));
+			}
 
 	} else if (req->request) {
 		/*
@@ -1876,7 +1879,7 @@ static const char* uncompress(int16_t field_count, const char *start, const char
 		} else if (dns_type == DNS_TYPE_A || dns_type == DNS_TYPE_AAAA) {
 			dlen = uptr[-2] << 8 | uptr[-1];
 
-			if ((ptr + dlen) > end || (uptr + dlen) > uncomp_end) {
+			if (dlen > (end - ptr) || dlen > (uncomp_end - uptr)) {
 				debug("data len %d too long", dlen);
 				return NULL;
 			}
@@ -3463,7 +3466,7 @@ static gboolean tcp_client_event(GIOChannel *channel, GIOCondition condition,
 	}
 
 	len = recvfrom(client_sk, client->buf + client->buf_end,
-			TCP_MAX_BUF_LEN - client->buf_end, 0,
+			TCP_MAX_BUF_LEN - client->buf_end - 1, 0,
 			client_addr, client_addr_len);
 	if (len < 0) {
 		if (errno == EAGAIN || errno == EWOULDBLOCK)
@@ -3476,6 +3479,8 @@ static gboolean tcp_client_event(GIOChannel *channel, GIOCondition condition,
 		return FALSE;
 	}
 
+	client->buf[client->buf_end + len] = '\0';
+
 	return read_tcp_data(client, client_addr, *client_addr_len, len);
 }
 
@@ -3662,7 +3667,7 @@ static bool udp_listener_event(GIOChannel *channel, GIOCondition condition,
 				struct listener_data *ifdata, int family,
 				guint *listener_watch)
 {
-	unsigned char buf[768];
+	unsigned char buf[769];
 	char query[512];
 	struct request_data *req = NULL;
 	struct domain_hdr *hdr = NULL;
@@ -3691,10 +3696,12 @@ static bool udp_listener_event(GIOChannel *channel, GIOCondition condition,
 
 	memset(client_addr, 0, *client_addr_len);
 	sk = g_io_channel_unix_get_fd(channel);
-	len = recvfrom(sk, buf, sizeof(buf), 0, client_addr, client_addr_len);
+	len = recvfrom(sk, buf, sizeof(buf) - 1, 0, client_addr, client_addr_len);
 	if (len < 2)
 		return true;
 
+	buf[len] = '\0';
+
 	debug("Received %d bytes (id 0x%04x)", len, buf[0] | buf[1] << 8);
 
 	err = parse_request(buf, len, query, sizeof(query));
@@ -3847,7 +3854,12 @@ static GIOChannel *get_listener(int family, int protocol, int index)
 			return NULL;
 		}
 
-		fcntl(sk, F_SETFL, O_NONBLOCK);
+		if (fcntl(sk, F_SETFL, O_NONBLOCK) < 0) {
+			connman_error("Failed to set TCP listener socket to non-blocking %d/%s",
+				-errno, strerror(errno));
+			close(sk);
+			return NULL;
+		}
 	}
 
 	channel = g_io_channel_unix_new(sk);
-- 
2.34.1


                 reply	other threads:[~2024-04-24 18:47 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240424184751.130804-1-bfukano@gmail.com \
    --to=bfukano@gmail.com \
    --cc=connman@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox