libmemif: fix insecure uses of strncpy
[vpp.git] / extras / libmemif / src / socket.c
index 7c4bbd4..b801cac 100644 (file)
@@ -71,7 +71,7 @@ memif_msg_send (int fd, memif_msg_t * msg, int afd)
 static_fn int
 memif_msg_enq_ack (memif_connection_t * c)
 {
-  libmemif_main_t *lm = &libmemif_main;
+  libmemif_main_t *lm = get_libmemif_main (c->args.socket);
   memif_msg_queue_elt_t *e =
     (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
   if (e == NULL)
@@ -99,9 +99,8 @@ memif_msg_enq_ack (memif_connection_t * c)
 }
 
 static_fn int
-memif_msg_send_hello (int fd)
+memif_msg_send_hello (libmemif_main_t * lm, int fd)
 {
-  libmemif_main_t *lm = &libmemif_main;
   memif_msg_t msg = { 0 };
   memif_msg_hello_t *h = &msg.hello;
   msg.type = MEMIF_MSG_TYPE_HELLO;
@@ -112,8 +111,7 @@ memif_msg_send_hello (int fd)
   h->max_region = MEMIF_MAX_REGION;
   h->max_log2_ring_size = MEMIF_MAX_LOG2_RING_SIZE;
 
-  strncpy ((char *) h->name, (char *) lm->app_name,
-          strlen ((char *) lm->app_name));
+  strlcpy ((char *) h->name, (char *) lm->app_name, sizeof (h->name));
 
   /* msg hello is not enqueued but sent directly,
      because it is the first msg to be sent */
@@ -124,7 +122,7 @@ memif_msg_send_hello (int fd)
 static_fn int
 memif_msg_enq_init (memif_connection_t * c)
 {
-  libmemif_main_t *lm = &libmemif_main;
+  libmemif_main_t *lm = get_libmemif_main (c->args.socket);
   memif_msg_queue_elt_t *e =
     (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
   if (e == NULL)
@@ -140,8 +138,7 @@ memif_msg_enq_init (memif_connection_t * c)
   i->id = c->args.interface_id;
   i->mode = c->args.mode;
 
-  strncpy ((char *) i->name, (char *) lm->app_name,
-          strlen ((char *) lm->app_name));
+  strlcpy ((char *) i->name, (char *) lm->app_name, sizeof (i->name));
   if (strlen ((char *) c->args.secret) > 0)
     strncpy ((char *) i->secret, (char *) c->args.secret, sizeof (i->secret));
 
@@ -166,7 +163,7 @@ memif_msg_enq_init (memif_connection_t * c)
 static_fn int
 memif_msg_enq_add_region (memif_connection_t * c, uint8_t region_index)
 {
-  libmemif_main_t *lm = &libmemif_main;
+  libmemif_main_t *lm = get_libmemif_main (c->args.socket);
   memif_region_t *mr = &c->regions[region_index];
 
   memif_msg_queue_elt_t *e =
@@ -203,7 +200,7 @@ memif_msg_enq_add_region (memif_connection_t * c, uint8_t region_index)
 static_fn int
 memif_msg_enq_add_ring (memif_connection_t * c, uint8_t index, uint8_t dir)
 {
-  libmemif_main_t *lm = &libmemif_main;
+  libmemif_main_t *lm = get_libmemif_main (c->args.socket);
   memif_msg_queue_elt_t *e =
     (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
   if (e == NULL)
@@ -250,7 +247,7 @@ memif_msg_enq_add_ring (memif_connection_t * c, uint8_t index, uint8_t dir)
 static_fn int
 memif_msg_enq_connect (memif_connection_t * c)
 {
-  libmemif_main_t *lm = &libmemif_main;
+  libmemif_main_t *lm = get_libmemif_main (c->args.socket);
   memif_msg_queue_elt_t *e =
     (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
   if (e == NULL)
@@ -261,8 +258,8 @@ memif_msg_enq_connect (memif_connection_t * c)
 
   e->msg.type = MEMIF_MSG_TYPE_CONNECT;
   e->fd = -1;
-  strncpy ((char *) cm->if_name, (char *) c->args.interface_name,
-          strlen ((char *) c->args.interface_name));
+  strlcpy ((char *) cm->if_name, (char *) c->args.interface_name,
+          sizeof (cm->if_name));
 
   e->next = NULL;
   if (c->msg_queue == NULL)
@@ -285,7 +282,7 @@ memif_msg_enq_connect (memif_connection_t * c)
 static_fn int
 memif_msg_enq_connected (memif_connection_t * c)
 {
-  libmemif_main_t *lm = &libmemif_main;
+  libmemif_main_t *lm = get_libmemif_main (c->args.socket);
   memif_msg_queue_elt_t *e =
     (memif_msg_queue_elt_t *) lm->alloc (sizeof (memif_msg_queue_elt_t));
   if (e == NULL)
@@ -296,8 +293,8 @@ memif_msg_enq_connected (memif_connection_t * c)
 
   e->msg.type = MEMIF_MSG_TYPE_CONNECTED;
   e->fd = -1;
-  strncpy ((char *) cm->if_name, (char *) c->args.interface_name,
-          strlen ((char *) c->args.interface_name));
+  strlcpy ((char *) cm->if_name, (char *) c->args.interface_name,
+          sizeof (cm->if_name));
 
   e->next = NULL;
   if (c->msg_queue == NULL)
@@ -317,7 +314,7 @@ memif_msg_enq_connected (memif_connection_t * c)
 }
 
 /* immediately send disconnect msg */
-    /* specifie protocol for disconnect msg err_code
+    /* specify protocol for disconnect msg err_code
        so that it will be compatible with VPP? (header/doc) */
 int
 memif_msg_send_disconnect (int fd, uint8_t * err_string, uint32_t err_code)
@@ -328,12 +325,12 @@ memif_msg_send_disconnect (int fd, uint8_t * err_string, uint32_t err_code)
   msg.type = MEMIF_MSG_TYPE_DISCONNECT;
   d->code = err_code;
   uint16_t l = strlen ((char *) err_string);
-  if (l > 96)
+  if (l > sizeof (d->string) - 1)
     {
-      DBG ("Disconnect string too long. Sending first 96 characters.");
-      l = 96;
+      DBG ("Disconnect string too long. Sending the first %d characters.",
+          sizeof (d->string) - 1);
     }
-  strncpy ((char *) d->string, (char *) err_string, l);
+  strlcpy ((char *) d->string, (char *) err_string, sizeof (d->string));
 
   return memif_msg_send (fd, &msg, -1);
 }
@@ -357,8 +354,7 @@ memif_msg_receive_hello (memif_connection_t * c, memif_msg_t * msg)
   c->run_args.log2_ring_size = memif_min (h->max_log2_ring_size,
                                          c->args.log2_ring_size);
   c->run_args.buffer_size = c->args.buffer_size;
-  strncpy ((char *) c->remote_name, (char *) h->name,
-          strlen ((char *) h->name));
+  strlcpy ((char *) c->remote_name, (char *) h->name, sizeof (c->remote_name));
 
   return MEMIF_ERR_SUCCESS;    /* 0 */
 }
@@ -371,11 +367,11 @@ memif_msg_receive_init (memif_socket_t * ms, int fd, memif_msg_t * msg)
   memif_list_elt_t *elt = NULL;
   memif_list_elt_t elt2;
   memif_connection_t *c = NULL;
-  libmemif_main_t *lm = &libmemif_main;
+  libmemif_main_t *lm = get_libmemif_main (ms);
   uint8_t err_string[96];
   memset (err_string, 0, sizeof (char) * 96);
   int err = MEMIF_ERR_SUCCESS; /* 0 */
-  int err_disc;
+
   if (i->version != MEMIF_VERSION)
     {
       DBG ("MEMIF_VER_ERR");
@@ -421,8 +417,7 @@ memif_msg_receive_init (memif_socket_t * ms, int fd, memif_msg_t * msg)
       goto error;
     }
 
-  strncpy ((char *) c->remote_name, (char *) i->name,
-          strlen ((char *) i->name));
+  strlcpy ((char *) c->remote_name, (char *) i->name, sizeof (c->remote_name));
 
   if (strlen ((char *) c->args.secret) > 0)
     {
@@ -465,14 +460,14 @@ memif_msg_receive_init (memif_socket_t * ms, int fd, memif_msg_t * msg)
   elt2.key = c->fd;
   elt2.data_struct = c;
 
-  add_list_elt (&elt2, &lm->control_list, &lm->control_list_len);
+  add_list_elt (lm, &elt2, &lm->control_list, &lm->control_list_len);
   free_list_elt (lm->pending_list, lm->pending_list_len, fd);
 
   return err;
 
 error:
   memif_msg_send_disconnect (fd, err_string, 0);
-  lm->control_fd_update (fd, MEMIF_FD_EVENT_DEL);
+  lm->control_fd_update (fd, MEMIF_FD_EVENT_DEL, lm->private_ctx);
   free_list_elt (lm->pending_list, lm->pending_list_len, fd);
   close (fd);
   fd = -1;
@@ -484,7 +479,7 @@ static_fn int
 memif_msg_receive_add_region (memif_connection_t * c, memif_msg_t * msg,
                              int fd)
 {
-  libmemif_main_t *lm = &libmemif_main;
+  libmemif_main_t *lm = get_libmemif_main (c->args.socket);
 
   memif_msg_add_region_t *ar = &msg->add_region;
   memif_region_t *mr;
@@ -506,7 +501,8 @@ memif_msg_receive_add_region (memif_connection_t * c, memif_msg_t * msg,
   c->regions[ar->index].region_size = ar->size;
   c->regions[ar->index].addr = NULL;
 
-  if (lm->get_external_region_addr)
+  /* region 0 is never external */
+  if (lm->get_external_region_addr && (ar->index != 0))
     c->regions[ar->index].is_external = 1;
 
   return MEMIF_ERR_SUCCESS;    /* 0 */
@@ -517,7 +513,7 @@ memif_msg_receive_add_region (memif_connection_t * c, memif_msg_t * msg,
 static_fn int
 memif_msg_receive_add_ring (memif_connection_t * c, memif_msg_t * msg, int fd)
 {
-  libmemif_main_t *lm = &libmemif_main;
+  libmemif_main_t *lm = get_libmemif_main (c->args.socket);
 
   memif_msg_add_ring_t *ar = &msg->add_ring;
 
@@ -580,7 +576,7 @@ static_fn int
 memif_msg_receive_connect (memif_connection_t * c, memif_msg_t * msg)
 {
   memif_msg_connect_t *cm = &msg->connect;
-  libmemif_main_t *lm = &libmemif_main;
+  libmemif_main_t *lm = get_libmemif_main (c->args.socket);
   memif_list_elt_t elt;
 
   int err;
@@ -588,8 +584,8 @@ memif_msg_receive_connect (memif_connection_t * c, memif_msg_t * msg)
   if (err != MEMIF_ERR_SUCCESS)
     return err;
 
-  strncpy ((char *) c->remote_if_name, (char *) cm->if_name,
-          strlen ((char *) cm->if_name));
+  strlcpy ((char *) c->remote_if_name, (char *) cm->if_name,
+          sizeof (c->remote_if_name));
 
   int i;
   if (c->on_interrupt != NULL)
@@ -598,9 +594,11 @@ memif_msg_receive_connect (memif_connection_t * c, memif_msg_t * msg)
        {
          elt.key = c->rx_queues[i].int_fd;
          elt.data_struct = c;
-         add_list_elt (&elt, &lm->interrupt_list, &lm->interrupt_list_len);
+         add_list_elt (lm, &elt, &lm->interrupt_list,
+                       &lm->interrupt_list_len);
 
-         lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ);
+         lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ,
+                                lm->private_ctx);
        }
 
     }
@@ -615,7 +613,7 @@ static_fn int
 memif_msg_receive_connected (memif_connection_t * c, memif_msg_t * msg)
 {
   memif_msg_connect_t *cm = &msg->connect;
-  libmemif_main_t *lm = &libmemif_main;
+  libmemif_main_t *lm = get_libmemif_main (c->args.socket);
 
   int err;
   err = memif_connect1 (c);
@@ -623,14 +621,15 @@ memif_msg_receive_connected (memif_connection_t * c, memif_msg_t * msg)
     return err;
 
   strncpy ((char *) c->remote_if_name, (char *) cm->if_name,
-          strlen ((char *) cm->if_name));
+          sizeof (c->remote_if_name));
 
   int i;
   if (c->on_interrupt != NULL)
     {
       for (i = 0; i < c->run_args.num_s2m_rings; i++)
        {
-         lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ);
+         lm->control_fd_update (c->rx_queues[i].int_fd, MEMIF_FD_EVENT_READ,
+                                lm->private_ctx);
        }
     }
 
@@ -647,7 +646,7 @@ memif_msg_receive_disconnect (memif_connection_t * c, memif_msg_t * msg)
   memset (c->remote_disconnect_string, 0,
          sizeof (c->remote_disconnect_string));
   strncpy ((char *) c->remote_disconnect_string, (char *) d->string,
-          strlen ((char *) d->string));
+          sizeof (c->remote_disconnect_string));
 
   /* on returning error, handle function will call memif_disconnect () */
   DBG ("disconnect received: %s, mode: %d",
@@ -656,7 +655,7 @@ memif_msg_receive_disconnect (memif_connection_t * c, memif_msg_t * msg)
 }
 
 static_fn int
-memif_msg_receive (int ifd)
+memif_msg_receive (libmemif_main_t * lm, int ifd)
 {
   char ctl[CMSG_SPACE (sizeof (int)) +
           CMSG_SPACE (sizeof (struct ucred))] = { 0 };
@@ -667,7 +666,6 @@ memif_msg_receive (int ifd)
   int err = MEMIF_ERR_SUCCESS; /* 0 */
   int fd = -1;
   int i;
-  libmemif_main_t *lm = &libmemif_main;
   memif_connection_t *c = NULL;
   memif_socket_t *ms = NULL;
   memif_list_elt_t *elt = NULL;
@@ -681,7 +679,6 @@ memif_msg_receive (int ifd)
 
   DBG ("recvmsg fd %d", ifd);
   size = recvmsg (ifd, &mh, 0);
-  DBG ("done");
   if (size != sizeof (memif_msg_t))
     {
       if (size == 0)
@@ -690,7 +687,6 @@ memif_msg_receive (int ifd)
        return MEMIF_ERR_MFMSG;
     }
 
-  struct ucred *cr = 0;
   struct cmsghdr *cmsg;
 
   cmsg = CMSG_FIRSTHDR (&mh);
@@ -700,7 +696,7 @@ memif_msg_receive (int ifd)
        {
          if (cmsg->cmsg_type == SCM_CREDENTIALS)
            {
-             cr = (struct ucred *) CMSG_DATA (cmsg);
+             /* Do nothing */ ;
            }
          else if (cmsg->cmsg_type == SCM_RIGHTS)
            {
@@ -810,8 +806,7 @@ memif_msg_receive (int ifd)
 
   if (c != NULL)
     c->flags |= MEMIF_CONNECTION_FLAG_WRITE;
-/*    libmemif_main_t *lm = &libmemif_main;
-    lm->control_fd_update (c->fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_MOD); */
+
   return MEMIF_ERR_SUCCESS;    /* 0 */
 }
 
@@ -828,8 +823,10 @@ memif_conn_fd_error (memif_connection_t * c)
 int
 memif_conn_fd_read_ready (memif_connection_t * c)
 {
+  libmemif_main_t *lm = get_libmemif_main (c->args.socket);
   int err;
-  err = memif_msg_receive (c->fd);
+
+  err = memif_msg_receive (lm, c->fd);
   if (err != 0)
     {
       err = memif_disconnect_internal (c);
@@ -841,7 +838,7 @@ memif_conn_fd_read_ready (memif_connection_t * c)
 int
 memif_conn_fd_write_ready (memif_connection_t * c)
 {
-  libmemif_main_t *lm = &libmemif_main;
+  libmemif_main_t *lm = get_libmemif_main (c->args.socket);
   int err = MEMIF_ERR_SUCCESS; /* 0 */
 
 
@@ -855,12 +852,7 @@ memif_conn_fd_write_ready (memif_connection_t * c)
   c->msg_queue = c->msg_queue->next;
 
   c->flags &= ~MEMIF_CONNECTION_FLAG_WRITE;
-/*
-    libmemif_main_t *lm = &libmemif_main;
 
-    lm->control_fd_update (c->fd,
-        MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE | MEMIF_FD_EVENT_MOD);
-*/
   err = memif_msg_send (c->fd, &e->msg, e->fd);
   lm->free (e);
   goto done;
@@ -875,7 +867,7 @@ memif_conn_fd_accept_ready (memif_socket_t * ms)
   int addr_len;
   struct sockaddr_un client;
   int conn_fd;
-  libmemif_main_t *lm = &libmemif_main;
+  libmemif_main_t *lm = get_libmemif_main (ms);
 
   DBG ("accept called");
 
@@ -894,18 +886,19 @@ memif_conn_fd_accept_ready (memif_socket_t * ms)
   elt.key = conn_fd;
   elt.data_struct = ms;
 
-  add_list_elt (&elt, &lm->pending_list, &lm->pending_list_len);
-  lm->control_fd_update (conn_fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE);
+  add_list_elt (lm, &elt, &lm->pending_list, &lm->pending_list_len);
+  lm->control_fd_update (conn_fd, MEMIF_FD_EVENT_READ | MEMIF_FD_EVENT_WRITE,
+                        lm->private_ctx);
 
-  return memif_msg_send_hello (conn_fd);
+  return memif_msg_send_hello (lm, conn_fd);
 }
 
 int
-memif_read_ready (int fd)
+memif_read_ready (libmemif_main_t * lm, int fd)
 {
   int err;
-  DBG ("call recv");
-  err = memif_msg_receive (fd);
-  DBG ("recv finished");
+
+  err = memif_msg_receive (lm, fd);
+
   return err;
 }