Prevent oracle attacks (CVE-2018-16737, CVE-2018-16738)
[oweals/tinc.git] / src / net_socket.c
index 3467804eec16f00bd995d439adb493feb620a92e..6195c16c74550b084cc1fa7097b11390bf347f79 100644 (file)
@@ -639,6 +639,9 @@ void setup_outgoing_connection(outgoing_t *outgoing) {
   new connection
 */
 bool handle_new_meta_connection(int sock) {
+       static const int max_accept_burst = 10;
+       static int last_accept_burst;
+       static int last_accept_time;
        connection_t *c;
        sockaddr_t sa;
        int fd;
@@ -651,6 +654,22 @@ bool handle_new_meta_connection(int sock) {
                return false;
        }
 
+       if(last_accept_time == now) {
+               last_accept_burst++;
+
+               if(last_accept_burst >= max_accept_burst) {
+                       if(last_accept_burst == max_accept_burst) {
+                               ifdebug(CONNECTIONS) logger(LOG_WARNING, "Throttling incoming connections");
+                       }
+
+                       tarpit(fd);
+                       return false;
+               }
+       } else {
+               last_accept_burst = 0;
+               last_accept_time = now;
+       }
+
        sockaddrunmap(&sa);
 
        c = new_connection();
@@ -672,7 +691,6 @@ bool handle_new_meta_connection(int sock) {
        connection_add(c);
 
        c->allow_request = ID;
-       send_id(c);
 
        return true;
 }