libmemif: Fix abstract sockets 80/33180/7
authorNathan Skrzypczak <nathan.skrzypczak@gmail.com>
Fri, 7 May 2021 17:39:07 +0000 (19:39 +0200)
committerNathan Skrzypczak <nathan.skrzypczak@gmail.com>
Wed, 29 Sep 2021 12:49:45 +0000 (14:49 +0200)
This fixes size computation when using
abstract sockets with libmemif

Type: fix

Change-Id: I3a686e4ff2132b9fb295bbe30633958dcfec672b
Signed-off-by: Nathan Skrzypczak <nathan.skrzypczak@gmail.com>
extras/libmemif/src/main.c
extras/libmemif/src/socket.c

index 6a8e8b9..c1ab3b8 100644 (file)
@@ -500,9 +500,6 @@ memif_create_socket (memif_socket_handle_t *sock, memif_socket_args_t *args,
 
   /* copy arguments to internal struct */
   memcpy (&ms->args, args, sizeof (*args));
-  /* Handle abstract socket by converting '@' -> '\0' */
-  if (ms->args.path[0] == '@')
-    ms->args.path[0] = '\0';
   ms->private_ctx = private_ctx;
 
   if (ms->args.alloc == NULL)
@@ -723,6 +720,12 @@ error:
   return err;
 }
 
+static inline int
+memif_path_is_abstract (const char *filename)
+{
+  return (filename[0] == '@');
+}
+
 int
 memif_request_connection (memif_conn_handle_t c)
 {
@@ -736,6 +739,7 @@ memif_request_connection (memif_conn_handle_t c)
   memif_control_channel_t *cc = NULL;
   memif_fd_event_t fde;
   memif_fd_event_data_t *fdata = NULL;
+  int sunlen = sizeof (un);
   void *ctx;
 
   if (conn == NULL)
@@ -773,6 +777,16 @@ memif_request_connection (memif_conn_handle_t c)
       goto error;
     }
 
+  if (memif_path_is_abstract (ms->args.path))
+    {
+      /* Ensure the string is NULL terminated */
+      un.sun_path[sizeof (un.sun_path) - 1] = '\0';
+      /* sunlen is strlen(un.sun_path) + sizeof(un.sun_family) */
+      sunlen = strlen (un.sun_path) + (sizeof (un) - sizeof (un.sun_path));
+      /* Handle abstract socket by converting '@' -> '\0' */
+      un.sun_path[0] = '\0';
+    }
+
   if (conn->args.is_master != 0)
     {
       /* Configure socket optins */
@@ -781,7 +795,7 @@ memif_request_connection (memif_conn_handle_t c)
          err = memif_syscall_error_handler (errno);
          goto error;
        }
-      if (bind (sockfd, (struct sockaddr *) &un, sizeof (un)) < 0)
+      if (bind (sockfd, (struct sockaddr *) &un, sunlen) < 0)
        {
          err = memif_syscall_error_handler (errno);
          goto error;
@@ -791,7 +805,7 @@ memif_request_connection (memif_conn_handle_t c)
          err = memif_syscall_error_handler (errno);
          goto error;
        }
-      if (ms->args.path[0] != '\0')
+      if (!memif_path_is_abstract (ms->args.path))
        {
          /* Verify that the socket was created */
          if (stat ((char *) ms->args.path, &file_stat) < 0)
@@ -815,8 +829,7 @@ memif_request_connection (memif_conn_handle_t c)
          err = MEMIF_ERR_NOMEM;
          goto error;
        }
-      if (connect (sockfd, (struct sockaddr *) &un,
-                  sizeof (struct sockaddr_un)) != 0)
+      if (connect (sockfd, (struct sockaddr *) &un, sunlen) != 0)
        {
          err = MEMIF_ERR_CONNREFUSED;
          goto error;
@@ -1739,7 +1752,7 @@ memif_tx_burst (memif_conn_handle_t conn, uint16_t qid,
              if ((data_offset < 0) ||
                  ((data_offset + b0->len) > c->run_args.buffer_size))
                {
-                 DBG ("slot: %d, data_offset: %d, length: %d",
+                 DBG ("slot: %d, data_offset: %ld, length: %d",
                       b0->desc_index & mask, data_offset, b0->len);
                  err = MEMIF_ERR_INVAL_ARG;
                  goto done;
@@ -1947,10 +1960,6 @@ memif_get_details (memif_conn_handle_t conn, memif_details_t * md,
   if (l0 + l1 < buflen)
     {
       md->socket_path = (uint8_t *) memcpy (buf + l0, ms->args.path, 108);
-      if (md->socket_path[0] == '\0')
-       {
-         md->socket_path[0] = '@';
-       }
       l0 += l1;
     }
   else
index a9db770..eaf9d2f 100644 (file)
@@ -274,7 +274,7 @@ memif_msg_enq_disconnect (memif_control_channel_t *cc, uint8_t *err_string,
   uint16_t l = sizeof (d->string);
   if (l > 96)
     {
-      DBG ("Disconnect string too long. Sending the first %d characters.",
+      DBG ("Disconnect string too long. Sending the first %ld characters.",
           sizeof (d->string) - 1);
     }
   strlcpy ((char *) d->string, (char *) err_string, sizeof (d->string));