Skip to content

Commit a3ad7fa

Browse files
authored
Merge branch 'master' into solaris-o-cloexec
2 parents f32932f + 7472f8d commit a3ad7fa

5 files changed

Lines changed: 181 additions & 24 deletions

File tree

‎Makefile.in‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ CHECK_COMPILE_OBJS=syscall-no-at-fdcwd.o syscall-no-o-directory.o \
7272
authenticate-no-o-cloexec.o rsync-no-o-cloexec.o clientserver-no-o-directory.o \
7373
flist-no-o-directory.o generator-no-o-directory.o sender-no-o-directory.o \
7474
lib/acl-no-o-directory.o
75+
CHECK_COMPILE_OBJS=syscall-no-at-fdcwd.o access-no-inet6.o
7576

7677
# note that the -I. is needed to handle config.h when using VPATH
7778
.c.o:
@@ -119,6 +120,9 @@ sender-no-o-directory.o: sender.c $(HEADERS)
119120
lib/acl-no-o-directory.o: lib/acl.c $(HEADERS)
120121
$(CC) -I. -I$(srcdir) $(CFLAGS) $(CPPFLAGS) \
121122
-DRSYNC_TEST_NO_O_DIRECTORY -c $(srcdir)/lib/acl.c -o $@
123+
access-no-inet6.o: access.c $(HEADERS)
124+
$(CC) -I. -I$(srcdir) $(CFLAGS) $(CPPFLAGS) \
125+
-DRSYNC_TEST_NO_INET6 -c $(srcdir)/access.c -o $@
122126

123127
.PHONY: install
124128
install: all

‎access.c‎

Lines changed: 74 additions & 10 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
@@ -30,8 +33,9 @@ extern const char undetermined_hostname[];
3033

3134
static int match_hostname(const char **host_ptr, const char *addr, const char *tok, int deny)
3235
{
33-
struct hostent *hp;
34-
unsigned int i;
36+
struct addrinfo hints, *res, *res0, *resa = NULL;
37+
const char *a = NULL;
38+
int addrlen = 0;
3539
const char *host = *host_ptr;
3640
#ifdef SUPPORT_IDN
3741
char idn_tok[1024];
@@ -65,7 +69,10 @@ static int match_hostname(const char **host_ptr, const char *addr, const char *t
6569
return 0;
6670

6771
/* Now try forward-DNS on the token (config-specified hostname) and see if the IP matches. */
68-
if (!(hp = gethostbyname(tok))) {
72+
memset(&hints, 0, sizeof hints);
73+
hints.ai_family = PF_UNSPEC;
74+
hints.ai_socktype = SOCK_STREAM;
75+
if (getaddrinfo(tok, NULL, &hints, &res0) != 0) {
6976
/* A deny-list hostname token we cannot resolve must fail CLOSED:
7077
* we can't prove the peer isn't the denied host, so treat the
7178
* unresolvable token as a match (deny). Allow-list tokens keep
@@ -74,16 +81,73 @@ static int match_hostname(const char **host_ptr, const char *addr, const char *t
7481
return deny;
7582
}
7683

77-
for (i = 0; hp->h_addr_list[i] != NULL; i++) {
78-
if (strcmp(addr, inet_ntoa(*(struct in_addr*)(hp->h_addr_list[i]))) == 0) {
79-
/* If reverse lookups are off, we'll use the conf-specified
80-
* hostname in preference to UNDETERMINED. */
81-
if (host == undetermined_hostname)
82-
*host_ptr = strdup(tok);
83-
return 1;
84+
/* Parse the peer address once so the resolved addresses below can be
85+
* compared against it in binary form by family, as match_address()
86+
* does, rather than by comparing formatted strings. */
87+
memset(&hints, 0, sizeof hints);
88+
hints.ai_family = PF_UNSPEC;
89+
hints.ai_socktype = SOCK_STREAM;
90+
#ifdef AI_NUMERICHOST
91+
hints.ai_flags = AI_NUMERICHOST;
92+
#endif
93+
if (getaddrinfo(addr, NULL, &hints, &resa) == 0) {
94+
switch (resa->ai_family) {
95+
case PF_INET:
96+
a = (char *)&((struct sockaddr_in *)resa->ai_addr)->sin_addr;
97+
addrlen = 4;
98+
break;
99+
#ifdef INET6
100+
case PF_INET6:
101+
a = (char *)&((struct sockaddr_in6 *)resa->ai_addr)->sin6_addr;
102+
addrlen = 16;
103+
break;
104+
#endif
105+
default:
106+
break;
84107
}
85108
}
86109

110+
if (a) {
111+
for (res = res0; res; res = res->ai_next) {
112+
const char *t = NULL;
113+
114+
if (res->ai_family != resa->ai_family)
115+
continue;
116+
switch (res->ai_family) {
117+
case PF_INET:
118+
t = (char *)&((struct sockaddr_in *)res->ai_addr)->sin_addr;
119+
break;
120+
#ifdef INET6
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;
130+
break;
131+
}
132+
#endif
133+
default:
134+
continue;
135+
}
136+
if (memcmp(a, t, addrlen) == 0) {
137+
/* If reverse lookups are off, we'll use the conf-specified
138+
* hostname in preference to UNDETERMINED. */
139+
if (host == undetermined_hostname)
140+
*host_ptr = strdup(tok);
141+
freeaddrinfo(resa);
142+
freeaddrinfo(res0);
143+
return 1;
144+
}
145+
}
146+
}
147+
148+
if (resa)
149+
freeaddrinfo(resa);
150+
freeaddrinfo(res0);
87151
return 0;
88152
}
89153

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
#!/usr/bin/env python3
2+
"""Daemon forward-DNS hostname rules against IPv4 and IPv6 peers."""
3+
4+
import socket
5+
import subprocess
6+
7+
from rsyncfns import (
8+
FROMDIR, SCRATCHDIR, RSYNC,
9+
make_tree, require_tcp, rmtree, rsync_argv, start_test_daemon,
10+
test_fail, test_skipped,
11+
)
12+
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")
23+
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')
38+
39+
src = FROMDIR
40+
rmtree(src)
41+
make_tree(src, depth=2)
42+
43+
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")

‎testsuite/rsyncfns.py‎

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -768,12 +768,12 @@ def _cleanup_rsyncd(proc, port: int) -> 'None':
768768

769769

770770
def start_rsyncd(conf_path, port: int, rsync_cmd: str = None,
771-
stdin=subprocess.DEVNULL) -> 'subprocess.Popen':
772-
"""Spawn `rsync --daemon --no-detach --address=127.0.0.1 --port=N
771+
stdin=subprocess.DEVNULL, address: str = '127.0.0.1') -> 'subprocess.Popen':
772+
"""Spawn `rsync --daemon --no-detach --address=ADDRESS --port=N
773773
--config=conf` and return the Popen handle after the port is accepting
774774
connections.
775775
776-
The daemon is bound to LOOPBACK ONLY (--address=127.0.0.1): without it,
776+
The daemon is bound to LOOPBACK ONLY: without an explicit address,
777777
rsync --daemon binds 0.0.0.0 and the test modules would be reachable from
778778
the whole LAN. The daemon is killed automatically when this Python
779779
process exits (atexit). On Linux, the kernel also signals SIGTERM to the
@@ -791,9 +791,11 @@ def start_rsyncd(conf_path, port: int, rsync_cmd: str = None,
791791
This is only ever reached from start_test_daemon() in --use-tcp mode; the
792792
default (pipe) mode never starts a listening daemon.
793793
"""
794+
if address not in ('127.0.0.1', '::1'):
795+
test_fail(f"test daemon address is not loopback: {address}")
794796
argv = shlex.split(rsync_cmd or RSYNC_PEER) + [
795797
'--daemon', '--no-detach',
796-
'--address=127.0.0.1',
798+
f'--address={address}',
797799
f'--port={port}',
798800
f'--config={conf_path}',
799801
]
@@ -820,17 +822,18 @@ def start_rsyncd(conf_path, port: int, rsync_cmd: str = None,
820822
f"(status={proc.returncode})"
821823
)
822824
try:
823-
with _socket.create_connection(('127.0.0.1', port), timeout=0.5):
825+
with _socket.create_connection((address, port), timeout=0.5):
824826
return proc
825827
except OSError as e:
826828
last_err = e
827829
time.sleep(0.05)
828830

829831
_stop_rsyncd(proc)
830-
test_fail(f"rsyncd never listened on 127.0.0.1:{port}: {last_err}")
832+
test_fail(f"rsyncd never listened on {address}:{port}: {last_err}")
831833

832834

833-
def start_test_daemon(conf_path, port: int, rsync_cmd: str = None) -> str:
835+
def start_test_daemon(conf_path, port: int, rsync_cmd: str = None,
836+
address: str = '127.0.0.1') -> str:
834837
"""Bring up the test daemon and return a URL prefix for client commands.
835838
836839
rsync_cmd selects the daemon-side binary (default RSYNC_PEER); pass
@@ -844,20 +847,21 @@ def start_test_daemon(conf_path, port: int, rsync_cmd: str = None) -> str:
844847
'rsync://localhost/'. Another local user can't reach it; nothing is
845848
listening.
846849
847-
* --use-tcp -- starts a real rsyncd bound to 127.0.0.1 on the given
848-
claim_ports()-reserved port. Returns 'rsync://localhost:PORT/'. Bound
849-
to loopback so off-host/LAN access is impossible; a same-host user
850-
could still connect during the test window, which is the documented,
851-
accepted cost of explicitly opting into TCP.
850+
* --use-tcp -- starts a real rsyncd bound to IPv4 loopback by default,
851+
or to the selected loopback address, on the claim_ports()-reserved
852+
port. Off-host/LAN access is impossible; a same-host user could still
853+
connect during the test window, which is the documented, accepted cost
854+
of explicitly opting into TCP.
852855
853856
Build URLs as f"{prefix}module/path". `port` is only used (and claimed)
854857
in --use-tcp mode.
855858
"""
856859
daemon_cmd = rsync_cmd or RSYNC_PEER
857860
if USE_TCP:
858861
port = claim_free_port(port)
859-
start_rsyncd(conf_path, port, daemon_cmd)
860-
return f'rsync://localhost:{port}/'
862+
start_rsyncd(conf_path, port, daemon_cmd, address=address)
863+
host = 'localhost' if address == '127.0.0.1' else f'[{address}]'
864+
return f'rsync://{host}:{port}/'
861865
# RSYNC_CONNECT_PROG is run by a shell, so every word has to survive
862866
# re-parsing: a build path with a space would otherwise exec its prefix.
863867
os.environ['RSYNC_CONNECT_PROG'] = (

‎testsuite/skiplist/common.txt‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
checksum-zero-blocklen # the pure-Python receiver needs a real TCP socket; run with --use-tcp
1313
chroot-basis-forge-inner-module # the pure-Python sender needs a real TCP daemon; run with --use-tcp
14+
daemon-access-hostname # IPv6 hostname access matching needs a real TCP peer
1415
daemon-access-idn # hosts allow/deny hostname matching needs a real TCP peer
1516
daemon-access-ip # hosts allow/deny address matching needs a real TCP peer
1617
daemon-argv-limit # raw malicious daemon client needs a real TCP daemon; run with --use-tcp

0 commit comments

Comments
 (0)