Gentoo Archives: gentoo-portage-dev

From: Mike Gilbert <floppym@g.o>
To: gentoo-portage-dev@l.g.o
Subject: [gentoo-portage-dev] [PATCH v2] Configure additional addresses on the lo interface for network-sandbox
Date: Thu, 01 Aug 2019 13:23:04
Message-Id: 20190801132258.27402-1-floppym@gentoo.org
In Reply to: [gentoo-portage-dev] [PATCH] Configure a dummy network interface for network-sandbox by Mike Gilbert
1 This works around some strange behavior in glibc's getaddrinfo()
2 implementation when the AI_ADDRCONFIG flag is set.
3
4 For example:
5
6 struct addrinfo *res, hints = { .ai_family = AF_INET, .ai_flags = AI_ADDRCONFIG };
7 getaddrinfo("localhost", NULL, &hints, &res);
8
9 This returns no results if there are no non-loopback addresses configured.
10
11 Bug: https://bugs.gentoo.org/690758
12 Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=12377#c13
13 Signed-off-by: Mike Gilbert <floppym@g.o>
14 ---
15 lib/portage/process.py | 50 +++++++++++++++++++++++++++++++-----------
16 1 file changed, 37 insertions(+), 13 deletions(-)
17
18 diff --git a/lib/portage/process.py b/lib/portage/process.py
19 index dfbda75de..77f7fac02 100644
20 --- a/lib/portage/process.py
21 +++ b/lib/portage/process.py
22 @@ -446,6 +446,42 @@ def spawn(mycommand, env=None, opt_name=None, fd_pipes=None, returnpid=False,
23 # Everything succeeded
24 return 0
25
26 +def _configure_loopback_interface():
27 + """
28 + Configure the loopback interface.
29 + """
30 +
31 + IFF_UP = 0x1
32 + ifreq = struct.pack('16sh', b'lo', IFF_UP)
33 + SIOCSIFFLAGS = 0x8914
34 +
35 + sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
36 + try:
37 + fcntl.ioctl(sock, SIOCSIFFLAGS, ifreq)
38 + except IOError as e:
39 + writemsg("Unable to enable loopback interface: %s\n" % e.strerror, noiselevel=-1)
40 + sock.close()
41 +
42 + # We add some additional addresses to work around odd behavior in glibc's
43 + # getaddrinfo() implementation when the AI_ADDRCONFIG flag is set.
44 + #
45 + # For example:
46 + #
47 + # struct addrinfo *res, hints = { .ai_family = AF_INET, .ai_flags = AI_ADDRCONFIG };
48 + # getaddrinfo("localhost", NULL, &hints, &res);
49 + #
50 + # This returns no results if there are no non-loopback addresses
51 + # configured for a given address family.
52 + #
53 + # Bug: https://bugs.gentoo.org/690758
54 + # Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=12377#c13
55 +
56 + try:
57 + subprocess.call(['ip', 'address', 'add', '10.0.0.1/8', 'dev', 'lo'])
58 + subprocess.call(['ip', 'address', 'add', 'fd00::1/8', 'dev', 'lo'])
59 + except OSError as e:
60 + writemsg("Error calling 'ip': %s\n" % e.strerror, noiselevel=-1)
61 +
62 def _exec(binary, mycommand, opt_name, fd_pipes,
63 env, gid, groups, uid, umask, cwd,
64 pre_exec, close_fds, unshare_net, unshare_ipc, unshare_mount, unshare_pid,
65 @@ -624,19 +660,7 @@ def _exec(binary, mycommand, opt_name, fd_pipes,
66 noiselevel=-1)
67 os._exit(1)
68 if unshare_net:
69 - # 'up' the loopback
70 - IFF_UP = 0x1
71 - ifreq = struct.pack('16sh', b'lo', IFF_UP)
72 - SIOCSIFFLAGS = 0x8914
73 -
74 - sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
75 - try:
76 - fcntl.ioctl(sock, SIOCSIFFLAGS, ifreq)
77 - except IOError as e:
78 - writemsg("Unable to enable loopback interface: %s\n" % (
79 - errno.errorcode.get(e.errno, '?')),
80 - noiselevel=-1)
81 - sock.close()
82 + _configure_loopback_interface()
83 except AttributeError:
84 # unshare() not supported by libc
85 pass
86 --
87 2.22.0

Replies