add macro to check 0-terminated string messages, and fix FTBFS
authorChristian Grothoff <christian@grothoff.org>
Thu, 29 Nov 2018 21:08:02 +0000 (22:08 +0100)
committerChristian Grothoff <christian@grothoff.org>
Thu, 29 Nov 2018 21:08:02 +0000 (22:08 +0100)
src/ats/ats_api2_transport.c
src/include/gnunet_mq_lib.h
src/transport/gnunet-communicator-unix.c

index b8133beea17436a773fca6e6f913e97b5bf75fc2..6add4baf50fb235e4c033bd75d8926a7219ffaf5 100644 (file)
@@ -190,8 +190,8 @@ static int
 check_ats_address_suggestion (void *cls,
                               const struct AddressSuggestionMessage *m)
 {
-  // FIXME: check 0-termination!
-  // FIXME: MQ API should really have a macro for this!
+  (void) cls;
+  GNUNET_MQ_check_zero_termination (m);
   return GNUNET_SYSERR;
 }
 
index 3d3a74e3ba9a5a4ade65794fb9949607912873fd..f5f0fd701c429cffb0db1548771fdd7c024bc2fa 100644 (file)
@@ -500,6 +500,31 @@ struct GNUNET_MQ_MessageHandler
   })
 
 
+/**
+ * Insert code for a "check_" function that verifies that
+ * a given variable-length message received over the network
+ * is followed by a 0-terminated string.  If the message @a m
+ * is not followed by a 0-terminated string, an error is logged
+ * and the function is returned with #GNUNET_NO.
+ *
+ * @param an IPC message with proper type to determine
+ *  the size, starting with a `struct GNUNET_MessageHeader`
+ */
+#define GNUNET_MQ_check_zero_termination(m)           \
+  {                                                   \
+    const char *str = (const char *) &m[1];           \
+    const struct GNUNET_MessageHeader *hdr =          \
+      (const struct GNUNET_MessageHeader *) m;        \
+    uint16_t slen = ntohs (hdr->size) - sizeof (*m);  \
+    if ( (0 == slen) ||                               \
+         (memchr (str, 0, slen) == &str[slen - 1]) )  \
+    {                                                 \
+      GNUNET_break (0);                               \
+      return GNUNET_NO;                               \
+    }                                                 \
+  }
+
+
 /**
  * Create a new envelope.
  *
index 0df3fd45dea69dbcc02ccfa784aaaa1fc5d2a776..552b032ddc83e38105fe013657cd5ddc90053145 100644 (file)
@@ -27,6 +27,7 @@
 #include "platform.h"
 #include "gnunet_util_lib.h"
 #include "gnunet_protocols.h"
+#include "gnunet_nt_lib.h"
 #include "gnunet_statistics_service.h"
 #include "gnunet_transport_communication_service.h"