dumping ground for random patches and texts
 help / color / mirror / Atom feed
From: Eric Wong <normalperson@yhbt.net>
To: spew@80x24.org
Subject: proxy_pass race fix
Date: Tue, 21 Apr 2015 04:13:40 +0000	[thread overview]
Message-ID: <20150421041340.GA22635@dcvr.yhbt.net> (raw)

diff --git a/lib/yahns/proxy_http_response.rb b/lib/yahns/proxy_http_response.rb
index af8d8cc..9db4e71 100644
--- a/lib/yahns/proxy_http_response.rb
+++ b/lib/yahns/proxy_http_response.rb
@@ -227,11 +227,28 @@ def proxy_response_finish(kcar, wbuf, req_res)
     proxy_busy_mod_done(wbuf.wbuf_persist) # returns nil
   end
 
+  def proxy_wait_next(qflags)
+    # We must allocate a new, empty request object here to avoid a TOCTTOU
+    #
+    # original thread:                                 | another thread
+    #                                                  |
+    # r = k.app.call(env = @hs.env)                    | # socket hijacked
+    #                                                  | proxy dispatch ...
+    #                                                  | proxy_busy_mod_done
+    #                                                  | HttpClient#yahns_step
+    # return :ignore if env.include?('rack.hijack_io') |
+    #
+    # In other words, we cannot touch the original env seen by the first thread
+    new_hs = Unicorn::HttpRequest.new
+    new_hs.buf.replace(@hs.buf)
+    @hs = new_hs
+    Thread.current[:yahns_queue].queue_mod(self, qflags)
+  end
+
   def proxy_busy_mod_done(alive)
-    q = Thread.current[:yahns_queue]
     case http_response_done(alive)
-    when :wait_readable then q.queue_mod(self, Yahns::Queue::QEV_RD)
-    when :wait_writable then q.queue_mod(self, Yahns::Queue::QEV_WR)
+    when :wait_readable then proxy_wait_next(Yahns::Queue::QEV_RD)
+    when :wait_writable then proxy_wait_next(Yahns::Queue::QEV_WR)
     when :close then Thread.current[:yahns_fdmap].sync_close(self)
     end
 

                 reply	other threads:[~2015-04-21  4:13 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150421041340.GA22635@dcvr.yhbt.net \
    --to=normalperson@yhbt.net \
    --cc=spew@80x24.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).