Skip to content

Commit 36268e3

Browse files
committed
access: tighten IPv6 hostname matching portability
1 parent 84c9292 commit 36268e3

5 files changed

Lines changed: 109 additions & 193 deletions

File tree

‎Makefile.in‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ CHECK_SYMLINKS = testsuite/chown-fake_test.py testsuite/devices-fake_test.py \
6868
# Objects for CHECK_PROGS to clean
6969
CHECK_OBJS=tls.o testrun.o getgroups.o getfsdev.o t_stub.o t_unsafe.o t_chmod_secure.o t_rename_secure.o t_symlink_secure.o t_secure_relpath.o t_acl.o t_hashtable_overflow.o t_iwildmatch.o t_clean_fname.o t_safe_arg.o trimslash.o wildtest.o
7070
# Compile-only feature-shape checks.
71-
CHECK_COMPILE_OBJS=syscall-no-at-fdcwd.o
71+
CHECK_COMPILE_OBJS=syscall-no-at-fdcwd.o access-no-inet6.o
7272

7373
# note that the -I. is needed to handle config.h when using VPATH
7474
.c.o:
@@ -85,6 +85,10 @@ syscall-no-at-fdcwd.o: syscall.c $(HEADERS)
8585
$(CC) -I. -I$(srcdir) $(CFLAGS) $(CPPFLAGS) \
8686
-DRSYNC_TEST_NO_AT_FDCWD -c $(srcdir)/syscall.c -o $@
8787

88+
access-no-inet6.o: access.c $(HEADERS)
89+
$(CC) -I. -I$(srcdir) $(CFLAGS) $(CPPFLAGS) \
90+
-DRSYNC_TEST_NO_INET6 -c $(srcdir)/access.c -o $@
91+
8892
.PHONY: install
8993
install: all
9094
-$(MKDIR_P) $(DESTDIR)$(bindir)

‎access.c‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020

2121
#include "rsync.h"
2222
#include "ifuncs.h"
23+
#ifdef RSYNC_TEST_NO_INET6
24+
#undef INET6
25+
#endif
2326
#ifdef HAVE_NETGROUP_H
2427
#include <netgroup.h>
2528
#endif
@@ -115,9 +118,17 @@ static int match_hostname(const char **host_ptr, const char *addr, const char *t
115118
t = (char *)&((struct sockaddr_in *)res->ai_addr)->sin_addr;
116119
break;
117120
#ifdef INET6
118-
case PF_INET6:
119-
t = (char *)&((struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
121+
case PF_INET6: {
122+
const struct sockaddr_in6 *peer6 = (const struct sockaddr_in6 *)resa->ai_addr;
123+
const struct sockaddr_in6 *resolved6 = (const struct sockaddr_in6 *)res->ai_addr;
124+
125+
#ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
126+
if (resolved6->sin6_scope_id && peer6->sin6_scope_id != resolved6->sin6_scope_id)
127+
continue;
128+
#endif
129+
t = (char *)&resolved6->sin6_addr;
120130
break;
131+
}
121132
#endif
122133
default:
123134
continue;
@@ -132,9 +143,10 @@ static int match_hostname(const char **host_ptr, const char *addr, const char *t
132143
return 1;
133144
}
134145
}
135-
freeaddrinfo(resa);
136146
}
137147

148+
if (resa)
149+
freeaddrinfo(resa);
138150
freeaddrinfo(res0);
139151
return 0;
140152
}
Lines changed: 70 additions & 175 deletions
Original file line numberDiff line numberDiff line change
@@ -1,189 +1,84 @@
11
#!/usr/bin/env python3
2-
"""Daemon coverage: hosts allow / hosts deny hostname tokens matched by
3-
forward DNS (access.c match_hostname).
4-
5-
"reverse lookup" is off in every config below, so the daemon has no
6-
reverse-DNS name for the peer ("UNDETERMINED") and the reverse-name match
7-
in match_hostname() can never fire: only the forward-lookup path -- resolve
8-
the config's hostname token and compare the result against the peer's
9-
numeric address -- can match. The token is "localhost", which resolves to
10-
the loopback address in both families, so the same token exercises the
11-
IPv4 path against a 127.0.0.1 peer and the IPv6 path against a ::1 peer.
12-
13-
Also covered: "forward lookup = no" must disable the forward match, and a
14-
token that fails to resolve must fail CLOSED in a deny list (the peer is
15-
denied) while simply not matching in an allow list.
16-
17-
Like daemon-access-ip_test.py this needs a real TCP peer (--use-tcp), and
18-
the configs set no global hosts allow so each module's own patterns are
19-
the sole decider. The IPv6 daemon is spawned directly (the shared
20-
start_test_daemon helper binds 127.0.0.1 only) and its section is skipped
21-
with a note when this host or build has no IPv6 loopback.
22-
"""
23-
24-
import atexit
2+
"""Daemon forward-DNS hostname rules against IPv4 and IPv6 peers."""
3+
254
import socket
265
import subprocess
27-
import time
286

297
from rsyncfns import (
30-
FROMDIR, RSYNC_PEER, SCRATCHDIR,
31-
claim_ports, make_tree, require_tcp, rmtree, rsync_argv,
32-
split_rsync_cmd, start_test_daemon, test_fail,
8+
FROMDIR, SCRATCHDIR, RSYNC,
9+
make_tree, require_tcp, rmtree, rsync_argv, start_test_daemon,
10+
test_fail, test_skipped,
3311
)
3412

35-
DAEMON_PORT = 12914
36-
V6_PORT = 12917
37-
UNRESOLVABLE = 'rsync-test-nonexistent.invalid'
13+
IPV4_PORT = 12895
14+
IPV6_PORT = 12897
15+
require_tcp("hostname access matching needs a real TCP peer")
16+
17+
try:
18+
for family, address in ((socket.AF_INET, '127.0.0.1'), (socket.AF_INET6, '::1')):
19+
with socket.socket(family, socket.SOCK_STREAM) as probe:
20+
probe.bind((address, 0))
21+
except OSError:
22+
test_skipped("IPv4 or IPv6 loopback is unavailable")
3823

39-
require_tcp("hosts allow/deny forward-DNS matching needs a real TCP peer")
24+
25+
def loopback_name(family, address):
26+
for candidate in ('localhost', 'ip6-localhost', 'ip6-loopback', socket.gethostname()):
27+
try:
28+
answers = socket.getaddrinfo(candidate, None, socket.AF_UNSPEC, socket.SOCK_STREAM)
29+
except socket.gaierror:
30+
continue
31+
if any(item[0] == family and item[4][0] == address for item in answers):
32+
return candidate
33+
test_skipped(f"no local hostname resolves to {address}")
34+
35+
36+
host4 = loopback_name(socket.AF_INET, '127.0.0.1')
37+
host6 = loopback_name(socket.AF_INET6, '::1')
4038

4139
src = FROMDIR
4240
rmtree(src)
4341
make_tree(src, depth=2)
4442

4543

46-
def resolves_to(name, family, addr):
47-
"""True if `name` forward-resolves to the binary address `addr`."""
48-
try:
49-
infos = socket.getaddrinfo(name, None, family, socket.SOCK_STREAM)
50-
except socket.gaierror:
51-
return False
52-
return any(info[4][0] == addr for info in infos)
53-
54-
55-
def write_conf(path, modules, log, pidfile):
56-
lines = [
57-
'# autogenerated by daemon-access-hostname_test.py',
58-
f'pid file = {pidfile}',
59-
'use chroot = no',
60-
# No reverse-DNS name for the peer: only forward matching can fire.
61-
'reverse lookup = no',
62-
f'log file = {log}',
63-
'',
64-
]
65-
for mod, params in modules:
66-
lines.append(f'[{mod}]')
67-
lines.append(f'\tpath = {src}')
68-
lines.append('\tread only = yes')
69-
lines += [f'\t{k} = {v}' for k, v in params.items()]
70-
lines.append('')
71-
path.write_text('\n'.join(lines) + '\n', encoding='utf-8')
72-
return path
73-
74-
75-
def connect(url, mod):
76-
"""Return rsync's exit code for listing the module over the daemon."""
77-
return subprocess.run(rsync_argv('-r', f'{url}{mod}/'),
78-
stdout=subprocess.DEVNULL, stderr=subprocess.PIPE,
79-
text=True).returncode
80-
81-
82-
def allowed(url, mod, why):
83-
if connect(url, mod) != 0:
84-
test_fail(f"connection to {mod} should be ALLOWED ({why}) but was refused")
85-
86-
87-
def denied(url, mod, why):
88-
if connect(url, mod) == 0:
89-
test_fail(f"connection to {mod} should be DENIED ({why}) but succeeded")
90-
91-
92-
# --- IPv4 peer (127.0.0.1) -------------------------------------------------
93-
94-
if not resolves_to('localhost', socket.AF_INET, '127.0.0.1'):
95-
test_fail("this host does not resolve 'localhost' to 127.0.0.1; "
96-
"the forward-DNS cases cannot be constructed")
97-
98-
modules = [
99-
('plain', {}),
100-
('fwd-allow4', {'hosts allow': 'localhost'}),
101-
('fwd-deny4', {'hosts deny': 'localhost'}),
102-
('fwd-off', {'hosts allow': 'localhost', 'forward lookup': 'no'}),
103-
]
104-
# The unresolvable-token cases are only meaningful if the name really does
105-
# not resolve here (a wildcard resolver would defeat them).
106-
try:
107-
socket.getaddrinfo(UNRESOLVABLE, None, socket.AF_UNSPEC,
108-
socket.SOCK_STREAM)
109-
print(f"note: {UNRESOLVABLE} unexpectedly resolves here; "
110-
"unresolvable-token cases skipped")
111-
except socket.gaierror:
112-
modules += [
113-
('deny-unresolvable', {'hosts deny': UNRESOLVABLE}),
114-
('allow-unresolvable', {'hosts allow': UNRESOLVABLE}),
115-
]
116-
117-
conf = write_conf(SCRATCHDIR / 'access-hostname.conf', modules,
118-
SCRATCHDIR / 'rsyncd.log', SCRATCHDIR / 'rsyncd.pid')
119-
url = start_test_daemon(conf, DAEMON_PORT)
120-
121-
allowed(url, 'plain', "no hosts patterns on the module")
122-
allowed(url, 'fwd-allow4', "localhost forward-resolves to the 127.0.0.1 peer")
123-
denied(url, 'fwd-deny4', "localhost forward-resolves to the 127.0.0.1 peer")
124-
denied(url, 'fwd-off', "forward lookup is disabled for the module")
125-
if any(mod == 'deny-unresolvable' for mod, _ in modules):
126-
denied(url, 'deny-unresolvable',
127-
"an unresolvable deny token fails closed")
128-
denied(url, 'allow-unresolvable',
129-
"an unresolvable allow token does not match")
130-
131-
# --- IPv6 peer (::1) --------------------------------------------------------
132-
133-
v6_ok = resolves_to('localhost', socket.AF_INET6, '::1')
134-
if v6_ok:
135-
try:
136-
with socket.socket(socket.AF_INET6, socket.SOCK_STREAM) as s:
137-
s.bind(('::1', 0))
138-
except OSError:
139-
v6_ok = False
140-
141-
if not v6_ok:
142-
print("note: no IPv6 loopback for 'localhost' here; IPv6 cases skipped")
143-
else:
144-
modules6 = [
145-
('plain6', {}),
146-
('fwd-allow6', {'hosts allow': 'localhost'}),
147-
('fwd-deny6', {'hosts deny': 'localhost'}),
148-
]
149-
conf6 = write_conf(SCRATCHDIR / 'access-hostname6.conf', modules6,
150-
SCRATCHDIR / 'rsyncd6.log', SCRATCHDIR / 'rsyncd6.pid')
151-
claim_ports(V6_PORT)
152-
# start_test_daemon binds 127.0.0.1 only, so spawn the IPv6 daemon
153-
# directly, with the same flags start_rsyncd uses apart from --address.
154-
proc = subprocess.Popen(
155-
split_rsync_cmd(RSYNC_PEER) + [
156-
'--daemon', '--no-detach', '--address=::1',
157-
f'--port={V6_PORT}', f'--config={conf6}',
158-
],
159-
stdin=subprocess.DEVNULL, stdout=subprocess.DEVNULL,
160-
stderr=subprocess.DEVNULL)
161-
atexit.register(proc.terminate)
162-
deadline = time.monotonic() + 10
163-
up = False
164-
while time.monotonic() < deadline:
165-
if proc.poll() is not None:
166-
break
167-
try:
168-
with socket.create_connection(('::1', V6_PORT), timeout=0.5):
169-
up = True
170-
break
171-
except OSError:
172-
time.sleep(0.05)
173-
if not up:
174-
proc.terminate()
175-
print("note: rsyncd did not listen on ::1 (built without INET6?); "
176-
"IPv6 cases skipped")
177-
else:
178-
try:
179-
url6 = f'rsync://[::1]:{V6_PORT}/'
180-
allowed(url6, 'plain6', "no hosts patterns on the module")
181-
allowed(url6, 'fwd-allow6',
182-
"localhost forward-resolves to the ::1 peer")
183-
denied(url6, 'fwd-deny6',
184-
"localhost forward-resolves to the ::1 peer")
185-
finally:
186-
proc.terminate()
187-
proc.wait(timeout=5)
188-
189-
print("daemon-access-hostname: forward-DNS hosts allow/deny matching verified")
44+
def start(family, address, host, port):
45+
conf = SCRATCHDIR / f'access-hostname-{family}.conf'
46+
conf.write_text(
47+
f"pid file = {SCRATCHDIR}/rsyncd-{family}.pid\n"
48+
"use chroot = no\n"
49+
"reverse lookup = no\n"
50+
"forward lookup = yes\n"
51+
f"log file = {SCRATCHDIR}/{'rsyncd.log' if family == 'ipv6' else 'rsyncd-ipv4.log'}\n"
52+
f"\n[plain]\n\tpath = {src}\n\tread only = yes\n"
53+
f"\n[allow-host]\n\tpath = {src}\n\tread only = yes\n\thosts allow = {host}\n"
54+
f"\n[deny-host]\n\tpath = {src}\n\tread only = yes\n\thosts deny = {host}\n"
55+
f"\n[allow-unresolved]\n\tpath = {src}\n\tread only = yes\n\thosts allow = nope.nonexistent.invalid\n"
56+
f"\n[deny-unresolved]\n\tpath = {src}\n\tread only = yes\n\thosts deny = nope.nonexistent.invalid\n"
57+
f"\n[forward-off]\n\tpath = {src}\n\tread only = yes\n\tforward lookup = no\n\thosts allow = {host}\n"
58+
)
59+
return start_test_daemon(conf, port, rsync_cmd=RSYNC, address=address)
60+
61+
62+
urls = (
63+
('IPv4', start('ipv4', '127.0.0.1', host4, IPV4_PORT)),
64+
('IPv6', start('ipv6', '::1', host6, IPV6_PORT)),
65+
)
66+
67+
68+
def connect(url, module):
69+
return subprocess.run(
70+
rsync_argv('-r', f'{url}{module}/'),
71+
stdout=subprocess.DEVNULL, stderr=subprocess.PIPE, text=True,
72+
).returncode
73+
74+
75+
for family, url in urls:
76+
for module in ('plain', 'allow-host'):
77+
if connect(url, module) != 0:
78+
test_fail(f"{family} connection to {module} should be allowed")
79+
80+
for module in ('deny-host', 'allow-unresolved', 'deny-unresolved', 'forward-off'):
81+
if connect(url, module) == 0:
82+
test_fail(f"{family} connection to {module} should be denied")
83+
84+
print("daemon-access-hostname: IPv4 and IPv6 hostname rules verified")

0 commit comments

Comments
 (0)