CVE-2019-16779 (published 2019-12-16T20:15Z)

In RubyGem excon before 0.71.0, there was a race condition around persistent connections, where a connection which is interrupted (such as by a timeout) would leave data on the socket. Subsequent requests would then read this data, returning content from the previous response. The race condition window appears to be short, and it would be difficult to purposefully exploit this.

Severity: MEDIUM
CVSS v2 base score: 4.3 (AV:N/AC:M/Au:N/C:P/I:N/A:N)
CVSS v3 base score: 5.9, MEDIUM (CVSS:3.1/AV:N/AC:H/PR:N/UI:N/S:U/C:H/I:N/A:N)

Weakness information:
cwe_id: CWE-362
cwe_name: Concurrent Execution using Shared Resource with Improper Synchronization ('Race Condition')
description: The product contains a code sequence that can run concurrently with other code, and the code sequence requires temporary, exclusive access to a shared resource, but a timing window exists in which the shared resource can be modified by another code sequence that is operating concurrently.
extended_description: 
            This can have security implications when the expected synchronization is in security-critical code, such as recording whether a user is authenticated or modifying important state information that should not be influenced by an outsider.
            A race condition occurs within concurrent environments, and is effectively a property of a code sequence. Depending on the context, a code sequence may be in the form of a function call, a small number of instructions, a series of program invocations, etc.
            A race condition violates these properties, which are closely related:
               
                  Exclusivity - the code sequence is given exclusive access to the shared resource, i.e., no other code sequence can modify properties of the shared resource before the original sequence has completed execution.
                  Atomicity - the code sequence is behaviorally atomic, i.e., no other thread or process can concurrently execute the same sequence of instructions (or a subset) against the same resource.
               
            A race condition exists when an "interfering code sequence" can still access the shared resource, violating exclusivity. Programmers may assume that certain code sequences execute too quickly to be affected by an interfering code sequence; when they are not, this violates atomicity. For example, the single "x++" statement may appear atomic at the code layer, but it is actually non-atomic at the instruction layer, since it involves a read (the original value of x), followed by a computation (x+1), followed by a write (save the result to x).
            The interfering code sequence could be "trusted" or "untrusted." A trusted interfering code sequence occurs within the product; it cannot be modified by the attacker, and it can only be invoked indirectly. An untrusted interfering code sequence can be authored directly by the attacker, and typically it is external to the vulnerable product.
         
         

Fix information:
repo_url: https://github.com/excon/excon
commit_details: repo_url: https://github.com/excon/excon
author: geemus
author_date: 2019-12-12 16:45:12-06:00
msg: fix for leftover data with interrupted persistent connections

Thanks to @pje for disclosure, initial patch, and input
repository: repo_url: https://github.com/excon/excon
repo_name: excon/excon
description: Usable, fast, simple HTTP 1.1 for Ruby
repo_language: Ruby
owner: excon
file_changes: filename: connection.rb
change_type: ModificationType.MODIFY
diff: @@ -262,6 +262,11 @@ module Excon
 
       datum[:connection] = self
 
+      # cleanup data left behind on persistent connection after interrupt
+      if datum[:persistent] && !@persistent_socket_reusable
+        reset
+      end
+
       datum[:stack] = datum[:middlewares].map do |middleware|
         lambda {|stack| middleware.new(stack)}
       end.reverse.inject(self) do |middlewares, middleware|
@@ -270,7 +275,9 @@ module Excon
       datum = datum[:stack].request_call(datum)
 
       unless datum[:pipeline]
+        @persistent_socket_reusable = false
         datum = response(datum)
+        @persistent_socket_reusable = true
 
         if datum[:persistent]
           if key = datum[:response][:headers].keys.detect {|k| k.casecmp('Connection') == 0 }
@@ -344,6 +351,7 @@ module Excon
       if old_socket = sockets.delete(@socket_key)
         old_socket.close rescue nil
       end
+      @persistent_socket_reusable = true
     end
 
     # Generate HTTP request verb methods

programming_language: Ruby
filename: connection_tests.rb
change_type: ModificationType.MODIFY
diff: @@ -14,6 +14,29 @@ Shindo.tests('Excon Connection') do
       connection.send(:socket) # creates/connects socket
       connection.data[:remote_ip]
     end
+
+    tests("persistent connections") do
+      connection = Excon.new('http://127.0.0.1:9292', persistent: true)
+
+      response_body = connection.request(path: '/foo', method: 'get').body
+      test("successful uninterrupted request") do
+        connection.request(path: '/foo', method: 'get').body == 'foo'
+      end
+
+      begin
+        # simulate an interrupted connection which leaves data behind
+        Timeout::timeout(0.0000000001) do
+          connection.request(path: '/foo', method: 'get')
+        end
+      rescue Timeout::Error
+        nil
+      end
+
+      test("resets connection after interrupt") do
+        response = connection.request(path: '/bar', method: 'get')
+        response.body == 'bar'
+      end
+    end
   end
 
   tests("inspect redaction") do

programming_language: Ruby
filename: basic.rb
change_type: ModificationType.MODIFY
diff: @@ -32,6 +32,14 @@ class Basic < Sinatra::Base
     echo
   end
 
+  get('/foo') do
+    'foo'
+  end
+
+  get('/bar') do
+    'bar'
+  end
+
   private
 
   def echo

programming_language: Ruby