aa7e8de
From c3655b89e7c06555a2e0bf13affb8a63a49f4296 Mon Sep 17 00:00:00 2001
aa7e8de
From: Jarek Prokop <jprokop@redhat.com>
aa7e8de
Date: Fri, 26 Jan 2024 11:19:48 +0100
aa7e8de
Subject: [PATCH] Revert "Set AI_ADDRCONFIG when making getaddrinfo(3) calls
aa7e8de
 for outgoing conns (#7295)"
aa7e8de
aa7e8de
This reverts commit d2ba8ea54a4089959afdeecdd963e3c4ff391748.
aa7e8de
aa7e8de
The purpose of the commit is to workaround a GLIBC bug [0] still present
aa7e8de
in older Ubuntu [1]. C8S/RHEL 8 has the fix for some time [2] and the
aa7e8de
Ruby workaround is causing problems for us [3]. Therefore we can
aa7e8de
revert it for EL8, EL9, and Fedora distros.
aa7e8de
aa7e8de
[0] https://sourceware.org/bugzilla/show_bug.cgi?id=26600
aa7e8de
[1] https://bugs.launchpad.net/ubuntu/+source/glibc/+bug/1961697
aa7e8de
[2] https://bugzilla.redhat.com/show_bug.cgi?id=1868106
aa7e8de
[3] https://bugs.ruby-lang.org/issues/20208
aa7e8de
---
aa7e8de
 ext/socket/extconf.rb   |   2 -
aa7e8de
 ext/socket/ipsocket.c   |  11 +--
aa7e8de
 test/socket/test_tcp.rb | 164 ----------------------------------------
aa7e8de
 3 files changed, 2 insertions(+), 175 deletions(-)
aa7e8de
aa7e8de
diff --git a/ext/socket/extconf.rb b/ext/socket/extconf.rb
aa7e8de
index 544bed5298..1ca52da366 100644
aa7e8de
--- a/ext/socket/extconf.rb
aa7e8de
+++ b/ext/socket/extconf.rb
aa7e8de
@@ -607,8 +607,6 @@ def %(s) s || self end
aa7e8de
 EOS
aa7e8de
   end
aa7e8de
 
aa7e8de
-  have_const('AI_ADDRCONFIG', headers)
aa7e8de
-
aa7e8de
   case with_config("lookup-order-hack", "UNSPEC")
aa7e8de
   when "INET"
aa7e8de
     $defs << "-DLOOKUP_ORDER_HACK_INET"
aa7e8de
diff --git a/ext/socket/ipsocket.c b/ext/socket/ipsocket.c
aa7e8de
index 0a693655b4..0c13620258 100644
aa7e8de
--- a/ext/socket/ipsocket.c
aa7e8de
+++ b/ext/socket/ipsocket.c
aa7e8de
@@ -54,22 +54,15 @@ init_inetsock_internal(VALUE v)
aa7e8de
     VALUE connect_timeout = arg->connect_timeout;
aa7e8de
     struct timeval tv_storage;
aa7e8de
     struct timeval *tv = NULL;
aa7e8de
-    int remote_addrinfo_hints = 0;
aa7e8de
 
aa7e8de
     if (!NIL_P(connect_timeout)) {
aa7e8de
         tv_storage = rb_time_interval(connect_timeout);
aa7e8de
         tv = &tv_storage;
aa7e8de
     }
aa7e8de
 
aa7e8de
-    if (type == INET_SERVER) {
aa7e8de
-      remote_addrinfo_hints |= AI_PASSIVE;
aa7e8de
-    }
aa7e8de
-#ifdef HAVE_CONST_AI_ADDRCONFIG
aa7e8de
-    remote_addrinfo_hints |= AI_ADDRCONFIG;
aa7e8de
-#endif
aa7e8de
-
aa7e8de
     arg->remote.res = rsock_addrinfo(arg->remote.host, arg->remote.serv,
aa7e8de
-                                     family, SOCK_STREAM, remote_addrinfo_hints);
aa7e8de
+                                     family, SOCK_STREAM,
aa7e8de
+                                     (type == INET_SERVER) ? AI_PASSIVE : 0);
aa7e8de
 
aa7e8de
 
aa7e8de
     /*
aa7e8de
diff --git a/test/socket/test_tcp.rb b/test/socket/test_tcp.rb
aa7e8de
index 35d361f060..7f9dc53cae 100644
aa7e8de
--- a/test/socket/test_tcp.rb
aa7e8de
+++ b/test/socket/test_tcp.rb
aa7e8de
@@ -140,168 +140,4 @@ def test_accept_multithread
aa7e8de
       server_threads.each(&:join)
aa7e8de
     end
aa7e8de
   end
aa7e8de
-
aa7e8de
-  def test_ai_addrconfig
aa7e8de
-    # This test verifies that we pass AI_ADDRCONFIG to the DNS resolver when making
aa7e8de
-    # an outgoing connection.
aa7e8de
-    # The verification of this is unfortunately incredibly convoluted. We perform the
aa7e8de
-    # test by setting up a fake DNS server to receive queries. Then, we construct
aa7e8de
-    # an environment which has only IPv4 addresses and uses that fake DNS server. We
aa7e8de
-    # then attempt to make an outgoing TCP connection. Finally, we verify that we
aa7e8de
-    # only received A and not AAAA queries on our fake resolver.
aa7e8de
-    # This test can only possibly work on Linux, and only when run as root. If either
aa7e8de
-    # of these conditions aren't met, the test will be skipped.
aa7e8de
-
aa7e8de
-    # The construction of our IPv6-free environment must happen in a child process,
aa7e8de
-    # which we can put in its own network & mount namespaces.
aa7e8de
-
aa7e8de
-    omit "This test is disabled.  It is retained to show the original intent of [ruby-core:110870]"
aa7e8de
-
aa7e8de
-    IO.popen("-") do |test_io|
aa7e8de
-      if test_io.nil?
aa7e8de
-        begin
aa7e8de
-          # Child program
aa7e8de
-          require 'fiddle'
aa7e8de
-          require 'resolv'
aa7e8de
-          require 'open3'
aa7e8de
-
aa7e8de
-          libc = Fiddle.dlopen(nil)
aa7e8de
-          begin
aa7e8de
-            unshare = Fiddle::Function.new(libc['unshare'], [Fiddle::TYPE_INT], Fiddle::TYPE_INT)
aa7e8de
-          rescue Fiddle::DLError
aa7e8de
-            # Test can't run because we don't have unshare(2) in libc
aa7e8de
-            # This will be the case on not-linux, and also on very old glibc versions (or
aa7e8de
-            # possibly other libc's that don't expose this syscall wrapper)
aa7e8de
-            $stdout.write(Marshal.dump({result: :skip, reason: "unshare(2) or mount(2) not in libc"}))
aa7e8de
-            exit
aa7e8de
-          end
aa7e8de
-
aa7e8de
-          # Move our test process into a new network & mount namespace.
aa7e8de
-          # This environment will be configured to be IPv6 free and point DNS resolution
aa7e8de
-          # at a fake DNS server.
aa7e8de
-          # (n.b. these flags are CLONE_NEWNS | CLONE_NEWNET)
aa7e8de
-          ret = unshare.call(0x00020000 | 0x40000000)
aa7e8de
-          errno = Fiddle.last_error
aa7e8de
-          if ret == -1 && errno == Errno::EPERM::Errno
aa7e8de
-            # Test can't run because we're not root.
aa7e8de
-            $stdout.write(Marshal.dump({result: :skip, reason: "insufficient permissions to unshare namespaces"}))
aa7e8de
-            exit
aa7e8de
-          elsif ret == -1 && (errno == Errno::ENOSYS::Errno || errno == Errno::EINVAL::Errno)
aa7e8de
-            # No unshare(2) in the kernel (or kernel too old to know about this namespace type)
aa7e8de
-            $stdout.write(Marshal.dump({result: :skip, reason: "errno #{errno} calling unshare(2)"}))
aa7e8de
-            exit
aa7e8de
-          elsif ret == -1
aa7e8de
-            # Unexpected failure
aa7e8de
-            raise "errno #{errno} calling unshare(2)"
aa7e8de
-          end
aa7e8de
-
aa7e8de
-          # Set up our fake DNS environment. Clean out /etc/hosts...
aa7e8de
-          fake_hosts_file = Tempfile.new('ruby_test_hosts')
aa7e8de
-          fake_hosts_file.write <<~HOSTS
aa7e8de
-            127.0.0.1 localhost
aa7e8de
-            ::1 localhost
aa7e8de
-          HOSTS
aa7e8de
-          fake_hosts_file.flush
aa7e8de
-
aa7e8de
-          # Have /etc/resolv.conf point to 127.0.0.1...
aa7e8de
-          fake_resolv_conf = Tempfile.new('ruby_test_resolv')
aa7e8de
-          fake_resolv_conf.write <<~RESOLV
aa7e8de
-            nameserver 127.0.0.1
aa7e8de
-          RESOLV
aa7e8de
-          fake_resolv_conf.flush
aa7e8de
-
aa7e8de
-          # Also stub out /etc/nsswitch.conf; glibc can have other resolver modules
aa7e8de
-          # (like systemd-resolved) configured in there other than just using dns,
aa7e8de
-          # so rewrite it to remove any `hosts:` lines and add one which just uses
aa7e8de
-          # dns.
aa7e8de
-          real_nsswitch_conf = File.read('/etc/nsswitch.conf') rescue ""
aa7e8de
-          fake_nsswitch_conf = Tempfile.new('ruby_test_nsswitch')
aa7e8de
-          real_nsswitch_conf.lines.reject { _1 =~ /^\s*hosts:/ }.each do |ln|
aa7e8de
-            fake_nsswitch_conf.puts ln
aa7e8de
-          end
aa7e8de
-          fake_nsswitch_conf.puts "hosts: files myhostname dns"
aa7e8de
-          fake_nsswitch_conf.flush
aa7e8de
-
aa7e8de
-          # This is needed to make sure our bind-mounds aren't visible outside this process.
aa7e8de
-          system 'mount', '--make-rprivate', '/', exception: true
aa7e8de
-          # Bind-mount the fake files over the top of the real files.
aa7e8de
-          system 'mount', '--bind', '--make-private', fake_hosts_file.path, '/etc/hosts', exception: true
aa7e8de
-          system 'mount', '--bind', '--make-private', fake_resolv_conf.path, '/etc/resolv.conf', exception: true
aa7e8de
-          system 'mount', '--bind', '--make-private', fake_nsswitch_conf.path, '/etc/nsswitch.conf', exception: true
aa7e8de
-
aa7e8de
-          # Create a dummy interface with only an IPv4 address
aa7e8de
-          system 'ip', 'link', 'add', 'dummy0', 'type', 'dummy', exception: true
aa7e8de
-          system 'ip', 'addr', 'add', '192.168.1.2/24', 'dev', 'dummy0', exception: true
aa7e8de
-          system 'ip', 'link', 'set', 'dummy0', 'up', exception: true
aa7e8de
-          system 'ip', 'link', 'set', 'lo', 'up', exception: true
aa7e8de
-
aa7e8de
-          # Disable IPv6 on this interface (this is needed to disable the link-local
aa7e8de
-          # IPv6 address)
aa7e8de
-          File.open('/proc/sys/net/ipv6/conf/dummy0/disable_ipv6', 'w') do |f|
aa7e8de
-            f.puts "1"
aa7e8de
-          end
aa7e8de
-
aa7e8de
-          # Create a fake DNS server which will receive the DNS queries triggered by TCPSocket.new
aa7e8de
-          fake_dns_server_socket = UDPSocket.new
aa7e8de
-          fake_dns_server_socket.bind('127.0.0.1', 53)
aa7e8de
-          received_dns_queries = []
aa7e8de
-          fake_dns_server_thread = Thread.new do
aa7e8de
-            Socket.udp_server_loop_on([fake_dns_server_socket]) do |msg, msg_src|
aa7e8de
-              request = Resolv::DNS::Message.decode(msg)
aa7e8de
-              received_dns_queries << request
aa7e8de
-              response = request.dup.tap do |r|
aa7e8de
-                r.qr = 0
aa7e8de
-                r.rcode = 3 # NXDOMAIN
aa7e8de
-              end
aa7e8de
-              msg_src.reply response.encode
aa7e8de
-            end
aa7e8de
-          end
aa7e8de
-
aa7e8de
-          # Make a request which will hit our fake DNS swerver - this needs to be in _another_
aa7e8de
-          # process because glibc will cache resolver info across the fork otherwise.
aa7e8de
-          load_path_args = $LOAD_PATH.flat_map { ['-I', _1] }
aa7e8de
-          Open3.capture3('/proc/self/exe', *load_path_args, '-rsocket', '-e', <<~RUBY)
aa7e8de
-            TCPSocket.open('www.example.com', 4444)
aa7e8de
-          RUBY
aa7e8de
-
aa7e8de
-          fake_dns_server_thread.kill
aa7e8de
-          fake_dns_server_thread.join
aa7e8de
-
aa7e8de
-          have_aaaa_qs = received_dns_queries.any? do |query|
aa7e8de
-            query.question.any? do |question|
aa7e8de
-              question[1] == Resolv::DNS::Resource::IN::AAAA
aa7e8de
-            end
aa7e8de
-          end
aa7e8de
-
aa7e8de
-          have_a_q = received_dns_queries.any? do |query|
aa7e8de
-            query.question.any? do |question|
aa7e8de
-              question[0].to_s == "www.example.com"
aa7e8de
-            end
aa7e8de
-          end
aa7e8de
-
aa7e8de
-          if have_aaaa_qs
aa7e8de
-            $stdout.write(Marshal.dump({result: :fail, reason: "got AAAA queries, expected none"}))
aa7e8de
-          elsif !have_a_q
aa7e8de
-            $stdout.write(Marshal.dump({result: :fail, reason: "got no A query for example.com"}))
aa7e8de
-          else
aa7e8de
-            $stdout.write(Marshal.dump({result: :success}))
aa7e8de
-          end
aa7e8de
-        rescue => ex
aa7e8de
-          $stdout.write(Marshal.dump({result: :fail, reason: ex.full_message}))
aa7e8de
-        ensure
aa7e8de
-          # Make sure the child process does not transfer control back into the test runner.
aa7e8de
-          exit!
aa7e8de
-        end
aa7e8de
-      else
aa7e8de
-        test_result = Marshal.load(test_io.read)
aa7e8de
-
aa7e8de
-        case test_result[:result]
aa7e8de
-        when :skip
aa7e8de
-          omit test_result[:reason]
aa7e8de
-        when :fail
aa7e8de
-          fail test_result[:reason]
aa7e8de
-        end
aa7e8de
-      end
aa7e8de
-    end
aa7e8de
-  end
aa7e8de
 end if defined?(TCPSocket)
aa7e8de
-- 
aa7e8de
2.43.0
aa7e8de