Skip to content

Commit cc35190

Browse files
steadytaoseks99xdarkguy2008sairex53scarabeusiv
committed
Backport 3.5.2 security and confinement fixes
Co-authored-by: Omar Elsayed <omarelsayed161@gmail.com> Co-authored-by: Alemar <6304136+darkguy2008@users.noreply.github.com> Co-authored-by: SAIREX <hackerowskiy@gmail.com> Co-authored-by: Tomáš Chvátal <tomas@chvatal.me>
1 parent 65b1b3e commit cc35190

22 files changed

Lines changed: 1595 additions & 59 deletions

‎Makefile.in‎

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,53 @@ CHECK_SYMLINKS = testsuite/chown-fake.test testsuite/devices-fake.test testsuite
6565

6666
# Objects for CHECK_PROGS to clean
6767
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 trimslash.o wildtest.o
68+
CHECK_COMPILE_OBJS=syscall-no-o-directory.o authenticate-no-o-cloexec.o \
69+
rsync-no-o-cloexec.o clientserver-no-o-directory.o \
70+
flist-no-o-directory.o generator-no-o-directory.o \
71+
sender-no-o-directory.o lib/acl-no-o-directory.o access-no-inet6.o
6872

6973
# note that the -I. is needed to handle config.h when using VPATH
7074
.c.o:
7175
@OBJ_SAVE@
7276
$(CC) -I. -I$(srcdir) $(CFLAGS) $(CPPFLAGS) -c $< @CC_SHOBJ_FLAG@
7377
@OBJ_RESTORE@
7478

79+
syscall-no-o-directory.o: syscall.c $(HEADERS)
80+
$(CC) -I. -I$(srcdir) $(CFLAGS) $(CPPFLAGS) \
81+
-DRSYNC_TEST_NO_O_DIRECTORY -c $(srcdir)/syscall.c -o $@
82+
83+
authenticate-no-o-cloexec.o: authenticate.c $(HEADERS)
84+
$(CC) -I. -I$(srcdir) $(CFLAGS) $(CPPFLAGS) \
85+
-DRSYNC_TEST_NO_O_CLOEXEC -c $(srcdir)/authenticate.c -o $@
86+
87+
rsync-no-o-cloexec.o: rsync.c $(HEADERS)
88+
$(CC) -I. -I$(srcdir) $(CFLAGS) $(CPPFLAGS) \
89+
-DRSYNC_TEST_NO_O_CLOEXEC -c $(srcdir)/rsync.c -o $@
90+
91+
clientserver-no-o-directory.o: clientserver.c $(HEADERS)
92+
$(CC) -I. -I$(srcdir) $(CFLAGS) $(CPPFLAGS) \
93+
-DRSYNC_TEST_NO_O_DIRECTORY -c $(srcdir)/clientserver.c -o $@
94+
95+
flist-no-o-directory.o: flist.c $(HEADERS)
96+
$(CC) -I. -I$(srcdir) $(CFLAGS) $(CPPFLAGS) \
97+
-DRSYNC_TEST_NO_O_DIRECTORY -c $(srcdir)/flist.c -o $@
98+
99+
generator-no-o-directory.o: generator.c $(HEADERS)
100+
$(CC) -I. -I$(srcdir) $(CFLAGS) $(CPPFLAGS) \
101+
-DRSYNC_TEST_NO_O_DIRECTORY -c $(srcdir)/generator.c -o $@
102+
103+
sender-no-o-directory.o: sender.c $(HEADERS)
104+
$(CC) -I. -I$(srcdir) $(CFLAGS) $(CPPFLAGS) \
105+
-DRSYNC_TEST_NO_O_DIRECTORY -c $(srcdir)/sender.c -o $@
106+
107+
lib/acl-no-o-directory.o: lib/acl.c $(HEADERS)
108+
$(CC) -I. -I$(srcdir) $(CFLAGS) $(CPPFLAGS) \
109+
-DRSYNC_TEST_NO_O_DIRECTORY -c $(srcdir)/lib/acl.c -o $@
110+
111+
access-no-inet6.o: access.c $(HEADERS)
112+
$(CC) -I. -I$(srcdir) $(CFLAGS) $(CPPFLAGS) \
113+
-DRSYNC_TEST_NO_INET6 -c $(srcdir)/access.c -o $@
114+
75115
# NOTE: consider running "packaging/smart-make" instead of "make" to auto-handle
76116
# any changes to configure.sh and the main Makefile prior to a "make all".
77117
all: Makefile rsync$(EXEEXT) stunnel-rsyncd.conf @MAKE_RRSYNC@ @MAKE_MAN@
@@ -300,7 +340,7 @@ rrsync.1: support/rrsync.1.md md-convert Makefile
300340

301341
.PHONY: clean
302342
clean: cleantests
303-
rm -f *~ $(OBJS) $(CHECK_PROGS) $(CHECK_OBJS) $(CHECK_SYMLINKS) @MAKE_RRSYNC@ \
343+
rm -f *~ $(OBJS) $(CHECK_PROGS) $(CHECK_OBJS) $(CHECK_COMPILE_OBJS) $(CHECK_SYMLINKS) @MAKE_RRSYNC@ \
304344
git-version.h rounding rounding.h *.old rsync*.1 rsync*.5 @MAKE_RRSYNC_1@ \
305345
*.html daemon-parm.h help-*.h default-*.h proto.h proto.h-tstamp
306346

@@ -347,18 +387,18 @@ test: check
347387
# an external harness (e.g. fleettest.py) can invoke runtests.py with its own
348388
# options.
349389
.PHONY: check-progs
350-
check-progs: all $(CHECK_PROGS) $(CHECK_SYMLINKS)
390+
check-progs: all $(CHECK_PROGS) $(CHECK_COMPILE_OBJS) $(CHECK_SYMLINKS)
351391

352392
.PHONY: check
353-
check: all $(CHECK_PROGS) $(CHECK_SYMLINKS)
393+
check: all $(CHECK_PROGS) $(CHECK_COMPILE_OBJS) $(CHECK_SYMLINKS)
354394
rsync_bin=`pwd`/rsync$(EXEEXT) $(srcdir)/runtests.sh
355395

356396
.PHONY: check29
357-
check29: all $(CHECK_PROGS) $(CHECK_SYMLINKS)
397+
check29: all $(CHECK_PROGS) $(CHECK_COMPILE_OBJS) $(CHECK_SYMLINKS)
358398
rsync_bin=`pwd`/rsync$(EXEEXT) $(srcdir)/runtests.sh --protocol=29
359399

360400
.PHONY: check30
361-
check30: all $(CHECK_PROGS) $(CHECK_SYMLINKS)
401+
check30: all $(CHECK_PROGS) $(CHECK_COMPILE_OBJS) $(CHECK_SYMLINKS)
362402
rsync_bin=`pwd`/rsync$(EXEEXT) $(srcdir)/runtests.sh --protocol=30
363403

364404
wildtest.o: wildtest.c t_stub.o lib/wildmatch.c rsync.h config.h

‎access.c‎

Lines changed: 69 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

3741
if (!host || !*host)
@@ -54,7 +58,10 @@ static int match_hostname(const char **host_ptr, const char *addr, const char *t
5458
return 0;
5559

5660
/* Now try forward-DNS on the token (config-specified hostname) and see if the IP matches. */
57-
if (!(hp = gethostbyname(tok))) {
61+
memset(&hints, 0, sizeof hints);
62+
hints.ai_family = PF_UNSPEC;
63+
hints.ai_socktype = SOCK_STREAM;
64+
if (getaddrinfo(tok, NULL, &hints, &res0) != 0) {
5865
/* A deny-list hostname token we cannot resolve must fail CLOSED:
5966
* we can't prove the peer isn't the denied host, so treat the
6067
* unresolvable token as a match (deny). Allow-list tokens keep
@@ -63,16 +70,68 @@ static int match_hostname(const char **host_ptr, const char *addr, const char *t
6370
return deny;
6471
}
6572

66-
for (i = 0; hp->h_addr_list[i] != NULL; i++) {
67-
if (strcmp(addr, inet_ntoa(*(struct in_addr*)(hp->h_addr_list[i]))) == 0) {
68-
/* If reverse lookups are off, we'll use the conf-specified
69-
* hostname in preference to UNDETERMINED. */
70-
if (host == undetermined_hostname)
71-
*host_ptr = strdup(tok);
72-
return 1;
73+
memset(&hints, 0, sizeof hints);
74+
hints.ai_family = PF_UNSPEC;
75+
hints.ai_socktype = SOCK_STREAM;
76+
#ifdef AI_NUMERICHOST
77+
hints.ai_flags = AI_NUMERICHOST;
78+
#endif
79+
if (getaddrinfo(addr, NULL, &hints, &resa) == 0) {
80+
switch (resa->ai_family) {
81+
case PF_INET:
82+
a = (char *)&((struct sockaddr_in *)resa->ai_addr)->sin_addr;
83+
addrlen = 4;
84+
break;
85+
#ifdef INET6
86+
case PF_INET6:
87+
a = (char *)&((struct sockaddr_in6 *)resa->ai_addr)->sin6_addr;
88+
addrlen = 16;
89+
break;
90+
#endif
91+
default:
92+
break;
7393
}
7494
}
7595

96+
if (a) {
97+
for (res = res0; res; res = res->ai_next) {
98+
const char *t = NULL;
99+
100+
if (res->ai_family != resa->ai_family)
101+
continue;
102+
switch (res->ai_family) {
103+
case PF_INET:
104+
t = (char *)&((struct sockaddr_in *)res->ai_addr)->sin_addr;
105+
break;
106+
#ifdef INET6
107+
case PF_INET6: {
108+
const struct sockaddr_in6 *peer6 = (const struct sockaddr_in6 *)resa->ai_addr;
109+
const struct sockaddr_in6 *resolved6 = (const struct sockaddr_in6 *)res->ai_addr;
110+
111+
#ifdef HAVE_SOCKADDR_IN6_SCOPE_ID
112+
if (resolved6->sin6_scope_id && peer6->sin6_scope_id != resolved6->sin6_scope_id)
113+
continue;
114+
#endif
115+
t = (char *)&resolved6->sin6_addr;
116+
break;
117+
}
118+
#endif
119+
default:
120+
continue;
121+
}
122+
if (memcmp(a, t, addrlen) == 0) {
123+
if (host == undetermined_hostname)
124+
*host_ptr = strdup(tok);
125+
freeaddrinfo(resa);
126+
freeaddrinfo(res0);
127+
return 1;
128+
}
129+
}
130+
}
131+
132+
if (resa)
133+
freeaddrinfo(resa);
134+
freeaddrinfo(res0);
76135
return 0;
77136
}
78137

‎authenticate.c‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,6 @@
2222
#include "itypes.h"
2323
#include "ifuncs.h"
2424

25-
/* O_CLOEXEC is absent on some still-supported targets. The random-source fd
26-
* is read and closed synchronously, so the established zero-value fallback is
27-
* sufficient without adding a configure dependency. */
28-
#ifndef O_CLOEXEC
29-
#define O_CLOEXEC 0
30-
#endif
31-
3225
extern int read_only;
3326
extern char *password_file;
3427
extern struct name_num_obj valid_auth_checksums;

‎clientserver.c‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1600,6 +1600,13 @@ static void create_pid_file(void)
16001600
const char *slash = strrchr(pid_file, '/');
16011601
char dirbuf[MAXPATHLEN];
16021602
const char *dir = ".";
1603+
int dir_flags = O_RDONLY;
1604+
#ifdef O_DIRECTORY
1605+
dir_flags |= O_DIRECTORY;
1606+
#elif defined O_NONBLOCK
1607+
/* Avoid blocking on a non-directory special file before fstat below. */
1608+
dir_flags |= O_NONBLOCK;
1609+
#endif
16031610
if (slash) {
16041611
size_t dlen = slash == pid_file ? 1 : (size_t)(slash - pid_file);
16051612
if (dlen >= sizeof dirbuf) {
@@ -1611,7 +1618,25 @@ static void create_pid_file(void)
16111618
dir = dirbuf;
16121619
base = slash + 1;
16131620
}
1614-
if ((pdfd = do_open(dir, O_RDONLY|O_DIRECTORY, 0)) < 0) {
1621+
pdfd = do_open(dir, dir_flags, 0);
1622+
#ifndef O_DIRECTORY
1623+
if (pdfd >= 0) {
1624+
STRUCT_STAT dir_st;
1625+
int save_errno;
1626+
if (do_fstat(pdfd, &dir_st) < 0)
1627+
save_errno = errno;
1628+
else if (!S_ISDIR(dir_st.st_mode))
1629+
save_errno = ENOTDIR;
1630+
else
1631+
save_errno = 0;
1632+
if (save_errno) {
1633+
close(pdfd);
1634+
pdfd = -1;
1635+
errno = save_errno;
1636+
}
1637+
}
1638+
#endif
1639+
if (pdfd < 0) {
16151640
rsyserr(FLOG, errno, "failed to open pid-file directory \"%s\"", dir);
16161641
exit_cleanup(RERR_FILEIO);
16171642
}

0 commit comments

Comments
 (0)