Replaced sprintf() by safer snprintf(), removed possible buffer overflow
[tinc] / src / net.c
index fdc2847..ebcaeb3 100644 (file)
--- a/src/net.c
+++ b/src/net.c
@@ -109,27 +109,6 @@ cp
   return 0;
 }
 
-/*
-  write as many bytes as possible to the tap
-  device, possibly in multiple turns.
-*/
-int write_n(int fd, void *buf, size_t len)
-{
-  int r, done = 0;
-cp  
-  do
-    {
-      if((r = write(fd, buf, len)) < 0)
-       return -1;
-      len -= r;
-      buf += r;
-      done += r;
-    } while(len > 0);
-
-  return done;
-cp
-}
-
 int xrecv(conn_list_t *cl, void *packet)
 {
   vpn_packet_t vp;
@@ -138,7 +117,7 @@ cp
   do_decrypt((real_packet_t*)packet, &vp, cl->key);
   add_mac_addresses(&vp);
 
-  if((lenin = write_n(tap_fd, &vp, vp.len + 2)) < 0)
+  if((lenin = write(tap_fd, &vp, vp.len + sizeof(vp.len))) < 0)
     syslog(LOG_ERR, "Can't write to tap device: %m");
   else
     total_tap_out += lenin;
@@ -152,29 +131,72 @@ cp
 */
 void add_queue(packet_queue_t **q, void *packet, size_t s)
 {
-  queue_element_t *e, *p;
+  queue_element_t *e;
 cp
   if(debug_lvl > 3)
     syslog(LOG_DEBUG, "packet to queue: %d", s);
 
-  e = xmalloc(sizeof(queue_element_t));
+  e = xmalloc(sizeof(*e));
   e->packet = xmalloc(s);
   memcpy(e->packet, packet, s);
+
   if(!*q)
     {
-      *q = xmalloc(sizeof(packet_queue_t));
+      *q = xmalloc(sizeof(**q));
       (*q)->head = (*q)->tail = NULL;
     }
 
-  e->next = NULL;
+  e->next = NULL;                      /* We insert at the tail */
 
-  if((*q)->tail != NULL)
-    (*q)->tail->next = e;
+  if((*q)->tail)                       /* Do we have a tail? */
+    {
+      (*q)->tail->next = e;
+      e->prev = (*q)->tail;
+    }
+  else                                 /* No tail -> no head too */
+    {
+      (*q)->head = e;
+      e->prev = NULL;
+    }
 
   (*q)->tail = e;
+cp
+}
 
-  if((*q)->head == NULL)
-    (*q)->head = e;
+/* Remove a queue element */
+void del_queue(packet_queue_t **q, queue_element_t *e)
+{
+cp
+  free(e->packet);
+
+  if(e->next)                          /* There is a successor, so we are not tail */
+    {
+      if(e->prev)                      /* There is a predecessor, so we are not head */
+        {
+          e->next->prev = e->prev;
+          e->prev->next = e->next;
+        }
+      else                             /* We are head */
+        {
+          e->next->prev = NULL;
+          (*q)->head = e->next;
+        }
+    }
+  else                                 /* We are tail (or all alone!) */
+    {          
+      if(e->prev)                      /* We are not alone :) */
+        {
+          e->prev->next = NULL;
+          (*q)->tail = e->prev;
+        }
+      else                             /* Adieu */
+        {
+          free(*q);
+          *q = NULL;
+        }
+    }
+    
+  free(e);
 cp
 }
 
@@ -183,28 +205,18 @@ cp
   each packet, and removing it when that
   returned a zero exit code
 */
-void flush_queue(conn_list_t *cl, packet_queue_t *pq,
+void flush_queue(conn_list_t *cl, packet_queue_t **pq,
                 int (*function)(conn_list_t*,void*))
 {
-  queue_element_t *p, *prev = NULL, *next = NULL;
+  queue_element_t *p, *next = NULL;
 cp
-  for(p = pq->head; p != NULL; )
+  for(p = (*pq)->head; p != NULL; )
     {
       next = p->next;
 
       if(!function(cl, p->packet))
-        {
-          if(prev)
-            prev->next = next;
-          else
-            pq->head = next;
-
-          free(p->packet);
-         free(p);
-        }
-      else
-        prev = p;
-
+        del_queue(pq, p);
+        
       p = next;
     }
 
@@ -226,7 +238,7 @@ cp
       if(debug_lvl > 1)
        syslog(LOG_DEBUG, "Flushing send queue for " IP_ADDR_S,
               IP_ADDR_V(cl->vpn_ip));
-      flush_queue(cl, cl->sq, xsend);
+      flush_queue(cl, &(cl->sq), xsend);
     }
 
   if(cl->rq)
@@ -234,7 +246,7 @@ cp
       if(debug_lvl > 1)
        syslog(LOG_DEBUG, "Flushing receive queue for " IP_ADDR_S,
               IP_ADDR_V(cl->vpn_ip));
-      flush_queue(cl, cl->rq, xrecv);
+      flush_queue(cl, &(cl->rq), xrecv);
     }
 cp
 }
@@ -273,7 +285,7 @@ cp
     {
       add_queue(&(cl->sq), packet, packet->len + 2);
       if(!cl->status.waitingforkey)
-       send_key_request(to);
+       send_key_request(cl->vpn_ip);                   /* Keys should be sent to the host running the tincd */
       return 0;
     }
 
@@ -692,7 +704,8 @@ cp
   p->real_ip = ntohl(ci.sin_addr.s_addr);
   p->meta_socket = sfd;
   p->status.meta = 1;
-
+  p->buflen = 0;
+  
   syslog(LOG_NOTICE, "Connection from %s:%d", p->hostname, htons(ci.sin_port));
 
   if(send_basic_info(p) < 0)
@@ -761,7 +774,7 @@ cp
   if(rp.len >= 0)
     {
       f = lookup_conn(rp.from);
-      if(debug_lvl > 2)
+      if(debug_lvl > 3)
        syslog(LOG_DEBUG, "packet from " IP_ADDR_S " (len %d)",
               IP_ADDR_V(rp.from), rp.len);
       if(!f)
@@ -881,7 +894,7 @@ int handle_new_meta_connection(conn_list_t *cl)
 {
   conn_list_t *ncn;
   struct sockaddr client;
-  int nfd, len = sizeof(struct sockaddr);
+  int nfd, len = sizeof(client);
 cp
   if((nfd = accept(cl->meta_socket, &client, &len)) < 0)
     {
@@ -909,9 +922,9 @@ cp
 */
 int handle_incoming_meta_data(conn_list_t *cl)
 {
-  int x, l = sizeof(x), lenin;
-  unsigned char tmp[1600];
-  int request;
+  int x, l = sizeof(x);
+  int request, oldlen, p, i;
+  int lenin = 0;
 cp
   if(getsockopt(cl->meta_socket, SOL_SOCKET, SO_ERROR, &x, &l) < 0)
     {
@@ -924,22 +937,66 @@ cp
       return -1;
     }
 
-  if((lenin = recv(cl->meta_socket, &tmp, sizeof(tmp), 0)) <= 0)
+  if(cl->buflen >= MAXBUFSIZE)
     {
-      syslog(LOG_ERR, "Receive failed: %m");
+      syslog(LOG_ERR, "Metadata read buffer full! Discarding contents.");
+      cl->buflen = 0;
+    }
+
+  lenin = read(cl->meta_socket, cl->buffer, MAXBUFSIZE-cl->buflen);
+
+  if(lenin<=0)
+    {
+      syslog(LOG_ERR, "Metadata socket read error: %m");
       return -1;
     }
 
-  request = (int)(tmp[0]);
+  oldlen = cl->buflen;
+  cl->buflen += lenin;
 
-  if(debug_lvl > 3)
-    syslog(LOG_DEBUG, "got request %d", request);
+  for(;;)
+    {
+      cl->reqlen = 0;
 
-  if(request_handlers[request] == NULL)
-    syslog(LOG_ERR, "Unknown request %d.", request);
-  else
-    if(request_handlers[request](cl, tmp, lenin) < 0)
-      return -1;
+      for(i = oldlen; i < cl->buflen; i++)
+        {
+          if(cl->buffer[i] == '\n')
+            {
+              cl->buffer[i] = 0;  /* replace end-of-line by end-of-string so we can use sscanf */
+              cl->reqlen = i + 1;
+              break;
+            }
+        }
+
+      if(cl->reqlen)
+        {
+          if(sscanf(cl->buffer, "%d", &request) == 1)
+            {
+              if(request_handlers[request] == NULL)
+                {
+                  syslog(LOG_ERR, "Unknown request: %s", cl->buffer);
+                  return 0;
+                }
+
+              if(debug_lvl > 3)
+                syslog(LOG_DEBUG, "Got request: %s", cl->buffer);                             
+
+              request_handlers[request](cl);
+            }
+          else
+            {
+              syslog(LOG_ERR, "Bogus data received: %s", cl->buffer);
+            }
+
+          cl->buflen -= cl->reqlen;
+          memmove(cl->buffer, cl->buffer + cl->reqlen, cl->buflen);
+          oldlen = 0;
+        }
+      else
+        {
+          break;
+        }
+    }
 cp  
   return 0;
 }
@@ -958,7 +1015,7 @@ cp
       if(p->status.remove)
        continue;
 
-      if(p->status.active)
+      if(p->status.dataopen)
        if(FD_ISSET(p->socket, f))
          {
            /*
@@ -1028,10 +1085,10 @@ cp
   from = ntohl(*((unsigned long*)(&vp.data[26])));
   to = ntohl(*((unsigned long*)(&vp.data[30])));
 
-  if(debug_lvl > 2)
+  if(debug_lvl > 3)
     syslog(LOG_DEBUG, "An IP packet (%04x) for " IP_ADDR_S " from " IP_ADDR_S,
           ether_type, IP_ADDR_V(to), IP_ADDR_V(from));
-  if(debug_lvl > 3)
+  if(debug_lvl > 4)
     syslog(LOG_DEBUG, MAC_ADDR_S " to " MAC_ADDR_S,
           MAC_ADDR_V(vp.data[0]), MAC_ADDR_V(vp.data[6]));
   
@@ -1044,7 +1101,7 @@ cp
 }
 
 /*
-  this is where it al happens...
+  this is where it all happens...
 */
 void main_loop(void)
 {