net engine: fix potential buffer overrun in socket path
[fio.git] / engines / net.c
index dd06861cf4b841852255ad84ac29c76ad25c568a..3e26131be9a9913b853080b8ab85695aff60fb98 100644 (file)
@@ -392,7 +392,7 @@ static int fio_netio_send(struct thread_data *td, struct io_u *io_u)
 
        do {
                if (is_udp(o)) {
-                       struct sockaddr *to;
+                       const struct sockaddr *to;
                        socklen_t len;
 
                        if (is_ipv6(o)) {
@@ -945,7 +945,8 @@ static int fio_netio_setup_connect_unix(struct thread_data *td,
        struct sockaddr_un *soun = &nd->addr_un;
 
        soun->sun_family = AF_UNIX;
-       strcpy(soun->sun_path, path);
+       memset(soun->sun_path, 0, sizeof(soun->sun_path));
+       strncpy(soun->sun_path, path, sizeof(soun->sun_path) - 1);
        return 0;
 }
 
@@ -976,7 +977,7 @@ static int fio_netio_setup_listen_unix(struct thread_data *td, const char *path)
 
        memset(addr, 0, sizeof(*addr));
        addr->sun_family = AF_UNIX;
-       strcpy(addr->sun_path, path);
+       strncpy(addr->sun_path, path, sizeof(addr->sun_path) - 1);
        unlink(path);
 
        len = sizeof(addr->sun_family) + strlen(path) + 1;
@@ -1092,6 +1093,7 @@ static int fio_netio_setup_listen_inet(struct thread_data *td, short port)
        }
 
        if (bind(fd, saddr, len) < 0) {
+               close(fd);
                td_verror(td, errno, "bind");
                return 1;
        }
@@ -1194,8 +1196,9 @@ static int fio_netio_setup(struct thread_data *td)
        struct netio_data *nd;
 
        if (!td->files_index) {
-               add_file(td, td->o.filename ?: "net", 0);
+               add_file(td, td->o.filename ?: "net", 0, 0);
                td->o.nr_files = td->o.nr_files ?: 1;
+               td->o.open_files++;
        }
 
        if (!td->io_ops->data) {