All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jason Andryuk <jandryuk@gmail.com>
To: xen-devel@lists.xenproject.org
Cc: Olaf Hering <olaf@aepfle.de>,
	Ian Jackson <ian.jackson@eu.citrix.com>, Wei Liu <wl@xen.org>,
	Jason Andryuk <jandryuk@gmail.com>
Subject: [PATCH v2 01/10] vchan-socket-proxy: Ensure UNIX path NUL terminated
Date: Wed, 10 Jun 2020 23:29:27 -0400	[thread overview]
Message-ID: <20200611032936.350657-2-jandryuk@gmail.com> (raw)
In-Reply-To: <20200611032936.350657-1-jandryuk@gmail.com>

Check the socket path length to ensure sun_path is NUL terminated.

This was spotted by Citrix's Coverity.

Also use strcpy to avoid a warning "'__builtin_strncpy' specified bound
108 equals destination size [-Werror=stringop-truncation]" flagged by
gcc 10.

Signed-off-by: Jason Andryuk <jandryuk@gmail.com>
---
CC: Olaf Hering <olaf@aepfle.de>

With Ubuntu's gcc-10, which is a pre-release "gcc-10 (Ubuntu
10-20200411-0ubuntu1) 10.0.1 20200411 (experimental)", I couldn't
actualy generate the strncpy warning.

 tools/libvchan/vchan-socket-proxy.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/tools/libvchan/vchan-socket-proxy.c b/tools/libvchan/vchan-socket-proxy.c
index 13700c5d67..6ae1d84143 100644
--- a/tools/libvchan/vchan-socket-proxy.c
+++ b/tools/libvchan/vchan-socket-proxy.c
@@ -148,12 +148,18 @@ static int connect_socket(const char *path_or_fd) {
         return fd;
     }
 
+    if (strlen(path_or_fd) >= sizeof(addr.sun_path)) {
+        fprintf(stderr, "UNIX socket path \"%s\" too long (%zd >= %zd)\n",
+                path_or_fd, strlen(path_or_fd), sizeof(addr.sun_path));
+        return -1;
+    }
+
     fd = socket(AF_UNIX, SOCK_STREAM, 0);
     if (fd == -1)
         return -1;
 
     addr.sun_family = AF_UNIX;
-    strncpy(addr.sun_path, path_or_fd, sizeof(addr.sun_path));
+    strcpy(addr.sun_path, path_or_fd);
     if (connect(fd, (const struct sockaddr *)&addr, sizeof(addr)) == -1) {
         close(fd);
         return -1;
@@ -174,13 +180,19 @@ static int listen_socket(const char *path_or_fd) {
         return fd;
     }
 
+    if (strlen(path_or_fd) >= sizeof(addr.sun_path)) {
+        fprintf(stderr, "UNIX socket path \"%s\" too long (%zd >= %zd)\n",
+                path_or_fd, strlen(path_or_fd), sizeof(addr.sun_path));
+        return -1;
+    }
+
     /* if not a number, assume a socket path */
     fd = socket(AF_UNIX, SOCK_STREAM, 0);
     if (fd == -1)
         return -1;
 
     addr.sun_family = AF_UNIX;
-    strncpy(addr.sun_path, path_or_fd, sizeof(addr.sun_path));
+    strcpy(addr.sun_path, path_or_fd);
     if (bind(fd, (const struct sockaddr *)&addr, sizeof(addr)) == -1) {
         close(fd);
         return -1;
-- 
2.25.1



  reply	other threads:[~2020-06-11  3:30 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-06-11  3:29 [PATCH v2 00/10] Coverity fixes for vchan-socket-proxy Jason Andryuk
2020-06-11  3:29 ` Jason Andryuk [this message]
2020-06-11  3:29 ` [PATCH v2 02/10] vchan-socket-proxy: Move perror() into listen_socket Jason Andryuk
2020-06-11  3:29 ` [PATCH v2 03/10] vchan-socket-proxy: Move perror() into connect_socket Jason Andryuk
2020-06-11  3:29 ` [PATCH v2 04/10] vchan-socket-proxy: Check xs_watch return value Jason Andryuk
2020-06-11  3:29 ` [PATCH v2 05/10] vchan-socket-proxy: Unify main " Jason Andryuk
2020-06-11  3:29 ` [PATCH v2 06/10] vchan-socket-proxy: Use a struct to store state Jason Andryuk
2020-06-11  3:29 ` [PATCH v2 07/10] vchan-socket-proxy: Switch data_loop() to take state Jason Andryuk
2020-06-11  3:29 ` [PATCH v2 08/10] vchan-socket-proxy: Set closed FDs to -1 Jason Andryuk
2020-06-11  3:29 ` [PATCH v2 09/10] vchan-socket-proxy: Cleanup resources on exit Jason Andryuk
2020-06-11  3:29 ` [PATCH v2 10/10] vchan-socket-proxy: Handle closing shared input/output_fd Jason Andryuk
2020-06-26 10:18 ` [PATCH v2 00/10] Coverity fixes for vchan-socket-proxy Wei Liu
2020-06-26 10:41   ` Paul Durrant
2020-06-26 11:12   ` Marek Marczykowski-Górecki
2020-06-26 11:31     ` Wei Liu

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=20200611032936.350657-2-jandryuk@gmail.com \
    --to=jandryuk@gmail.com \
    --cc=ian.jackson@eu.citrix.com \
    --cc=olaf@aepfle.de \
    --cc=wl@xen.org \
    --cc=xen-devel@lists.xenproject.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.