Skip to content

Commit f32932f

Browse files
committed
build: support systems without O_CLOEXEC or O_DIRECTORY
1 parent be12460 commit f32932f

10 files changed

Lines changed: 102 additions & 110 deletions

File tree

‎Makefile.in‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ 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 syscall-no-o-directory.o \
72+
authenticate-no-o-cloexec.o rsync-no-o-cloexec.o clientserver-no-o-directory.o \
73+
flist-no-o-directory.o generator-no-o-directory.o sender-no-o-directory.o \
74+
lib/acl-no-o-directory.o
7275

7376
# note that the -I. is needed to handle config.h when using VPATH
7477
.c.o:
@@ -85,6 +88,38 @@ syscall-no-at-fdcwd.o: syscall.c $(HEADERS)
8588
$(CC) -I. -I$(srcdir) $(CFLAGS) $(CPPFLAGS) \
8689
-DRSYNC_TEST_NO_AT_FDCWD -c $(srcdir)/syscall.c -o $@
8790

91+
syscall-no-o-directory.o: syscall.c $(HEADERS)
92+
$(CC) -I. -I$(srcdir) $(CFLAGS) $(CPPFLAGS) \
93+
-DRSYNC_TEST_NO_O_DIRECTORY -c $(srcdir)/syscall.c -o $@
94+
95+
authenticate-no-o-cloexec.o: authenticate.c $(HEADERS)
96+
$(CC) -I. -I$(srcdir) $(CFLAGS) $(CPPFLAGS) \
97+
-DRSYNC_TEST_NO_O_CLOEXEC -c $(srcdir)/authenticate.c -o $@
98+
99+
rsync-no-o-cloexec.o: rsync.c $(HEADERS)
100+
$(CC) -I. -I$(srcdir) $(CFLAGS) $(CPPFLAGS) \
101+
-DRSYNC_TEST_NO_O_CLOEXEC -c $(srcdir)/rsync.c -o $@
102+
103+
clientserver-no-o-directory.o: clientserver.c $(HEADERS)
104+
$(CC) -I. -I$(srcdir) $(CFLAGS) $(CPPFLAGS) \
105+
-DRSYNC_TEST_NO_O_DIRECTORY -c $(srcdir)/clientserver.c -o $@
106+
107+
flist-no-o-directory.o: flist.c $(HEADERS)
108+
$(CC) -I. -I$(srcdir) $(CFLAGS) $(CPPFLAGS) \
109+
-DRSYNC_TEST_NO_O_DIRECTORY -c $(srcdir)/flist.c -o $@
110+
111+
generator-no-o-directory.o: generator.c $(HEADERS)
112+
$(CC) -I. -I$(srcdir) $(CFLAGS) $(CPPFLAGS) \
113+
-DRSYNC_TEST_NO_O_DIRECTORY -c $(srcdir)/generator.c -o $@
114+
115+
sender-no-o-directory.o: sender.c $(HEADERS)
116+
$(CC) -I. -I$(srcdir) $(CFLAGS) $(CPPFLAGS) \
117+
-DRSYNC_TEST_NO_O_DIRECTORY -c $(srcdir)/sender.c -o $@
118+
119+
lib/acl-no-o-directory.o: lib/acl.c $(HEADERS)
120+
$(CC) -I. -I$(srcdir) $(CFLAGS) $(CPPFLAGS) \
121+
-DRSYNC_TEST_NO_O_DIRECTORY -c $(srcdir)/lib/acl.c -o $@
122+
88123
.PHONY: install
89124
install: all
90125
-$(MKDIR_P) $(DESTDIR)$(bindir)

‎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
@@ -1616,6 +1616,13 @@ static void create_pid_file(void)
16161616
const char *slash = strrchr(pid_file, '/');
16171617
char dirbuf[MAXPATHLEN];
16181618
const char *dir = ".";
1619+
int dir_flags = O_RDONLY;
1620+
#ifdef O_DIRECTORY
1621+
dir_flags |= O_DIRECTORY;
1622+
#elif defined O_NONBLOCK
1623+
/* Avoid blocking on a non-directory special file before fstat below. */
1624+
dir_flags |= O_NONBLOCK;
1625+
#endif
16191626
if (slash) {
16201627
size_t dlen = slash == pid_file ? 1 : (size_t)(slash - pid_file);
16211628
if (dlen >= sizeof dirbuf) {
@@ -1627,7 +1634,25 @@ static void create_pid_file(void)
16271634
dir = dirbuf;
16281635
base = slash + 1;
16291636
}
1630-
if ((pdfd = do_open(dir, O_RDONLY|O_DIRECTORY, 0)) < 0) {
1637+
pdfd = do_open(dir, dir_flags, 0);
1638+
#ifndef O_DIRECTORY
1639+
if (pdfd >= 0) {
1640+
STRUCT_STAT dir_st;
1641+
int save_errno;
1642+
if (do_fstat(pdfd, &dir_st) < 0)
1643+
save_errno = errno;
1644+
else if (!S_ISDIR(dir_st.st_mode))
1645+
save_errno = ENOTDIR;
1646+
else
1647+
save_errno = 0;
1648+
if (save_errno) {
1649+
close(pdfd);
1650+
pdfd = -1;
1651+
errno = save_errno;
1652+
}
1653+
}
1654+
#endif
1655+
if (pdfd < 0) {
16311656
rsyserr(FLOG, errno, "failed to open pid-file directory \"%s\"", dir);
16321657
exit_cleanup(RERR_FILEIO);
16331658
}

‎flist.c‎

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,8 @@ int open_sender_source_path(const char *path, int flags, int *matched)
379379
errno = saved_errno;
380380
return fd;
381381
#else
382+
(void)path;
383+
(void)flags;
382384
*matched = 0;
383385
errno = ENOSYS;
384386
return -1;
@@ -2259,14 +2261,21 @@ static void interpret_stat_error(const char *fname, int is_dir)
22592261
static DIR *secure_opendir(const char *fbuf)
22602262
{
22612263
int dfd, fl, matched;
2264+
int dir_flags = O_RDONLY;
22622265
DIR *d;
22632266

2267+
#ifdef O_DIRECTORY
2268+
dir_flags |= O_DIRECTORY;
2269+
#elif defined O_NONBLOCK
2270+
/* Avoid blocking on a raced special file before fdopendir validates it. */
2271+
dir_flags |= O_NONBLOCK;
2272+
#endif
22642273
if (filesfrom_owner_walk_active()) {
22652274
/* The source base is operator-selected, while each list entry may not
22662275
* be. Follow only trusted-owned symlinks while opening the directory. */
2267-
dfd = open_no_attacker_symlinks(fbuf, O_RDONLY | O_DIRECTORY, 0);
2276+
dfd = open_no_attacker_symlinks(fbuf, dir_flags, 0);
22682277
} else if (!am_daemon && am_sender
2269-
&& (dfd = open_sender_source_path(fbuf, O_RDONLY | O_DIRECTORY, &matched), matched)) {
2278+
&& (dfd = open_sender_source_path(fbuf, dir_flags, &matched), matched)) {
22702279
/* The command-line directory is the operator-selected transfer root.
22712280
* Follow that root, then keep every recursive scan beneath its held fd. */
22722281
} else if (am_daemon && (!am_chrooted || module_dirlen)
@@ -2295,18 +2304,18 @@ static DIR *secure_opendir(const char *fbuf)
22952304
return NULL;
22962305
}
22972306
dfd = secure_relative_open_at(module_dirfd, *modrel ? modrel : ".",
2298-
O_RDONLY | O_DIRECTORY, 0);
2307+
dir_flags, 0);
22992308
} else if (*fbuf == '/') {
23002309
/* An absolute scan path (an absolute --relative / --files-from name, or a
23012310
* "/" transfer root): anchor at "/" -- operator-named, trusted. */
23022311
const char *relp = fbuf;
23032312
while (*relp == '/')
23042313
relp++;
2305-
dfd = secure_relative_open("/", relp, O_RDONLY | O_DIRECTORY, 0);
2314+
dfd = secure_relative_open("/", relp, dir_flags, 0);
23062315
} else {
23072316
/* Non-daemon (or chrooted) sender: confine beneath the cwd the sender
23082317
* chdir'd into (the transfer root). */
2309-
dfd = secure_relative_open(NULL, fbuf, O_RDONLY | O_DIRECTORY, 0);
2318+
dfd = secure_relative_open(NULL, fbuf, dir_flags, 0);
23102319
}
23112320

23122321
if (dfd < 0)

‎generator.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -999,9 +999,9 @@ static int copy_altdest_file(const char *src, const char *dest, struct file_stru
999999
static int basis_link_stat(const char *path, STRUCT_STAT *stp)
10001000
{
10011001
extern int am_chrooted;
1002-
extern int operator_path_resolve;
10031002
extern unsigned int module_dirlen;
10041003
#if defined AT_FDCWD && defined O_NOFOLLOW && defined O_DIRECTORY
1004+
extern int operator_path_resolve;
10051005
/* The basis dir (--link-dest/--compare-dest/--copy-dest) is an operator-
10061006
* supplied path. For a non-daemon receiver, resolve it with the ownership
10071007
* walk: a symlink component owned by uid 0 or the euid (the operator's own
@@ -1603,7 +1603,7 @@ static int gen_entry_copy_xattrs(const char *src, const char *fname, struct file
16031603
* relative basis goes through the RESOLVE_BENEATH resolver; an absolute one
16041604
* through the operator ownership walk. Refuse (don't path-read) when we are
16051605
* meant to confine but can't pin; a non-hardened receiver path-reads (sfd<0). */
1606-
#if defined AT_FDCWD && defined O_NOFOLLOW
1606+
#if defined AT_FDCWD && defined O_NOFOLLOW && defined O_DIRECTORY
16071607
if (secure_relpath_active() && src && *src && !symlink_optout_allowed()) {
16081608
int odir = 0;
16091609
#ifdef O_DIRECTORY

‎lib/acl.c‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,10 +402,15 @@ int xacl_del_default_at(int dirfd, const char *leaf)
402402
* work race-safely via the /proc compat on a pre-6.13 kernel. */
403403
static int proc_self_fd_usable(void)
404404
{
405-
int dfd = open(".", O_RDONLY | O_DIRECTORY | O_CLOEXEC);
405+
int open_flags = O_RDONLY | O_CLOEXEC;
406+
int dfd;
406407
char p[64];
407408
int usable = 0;
408409

410+
#ifdef O_DIRECTORY
411+
open_flags |= O_DIRECTORY;
412+
#endif
413+
dfd = open(".", open_flags);
409414
if (dfd < 0)
410415
return 0;
411416
if (snprintf(p, sizeof p, "/proc/self/fd/%d/.", dfd) < (int)sizeof p) {

‎rsync.h‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,21 @@ enum delret {
411411
#endif
412412
#endif
413413

414+
/* O_CLOEXEC is absent on some still-supported targets. Defining it to zero
415+
* preserves the open flags there; the test macro exercises that fallback on
416+
* build hosts that provide O_CLOEXEC. */
417+
#ifdef RSYNC_TEST_NO_O_CLOEXEC
418+
#undef O_CLOEXEC
419+
#endif
420+
#ifndef O_CLOEXEC
421+
#define O_CLOEXEC 0
422+
#endif
423+
424+
/* Exercise the O_DIRECTORY portability path on build hosts that provide it. */
425+
#ifdef RSYNC_TEST_NO_O_DIRECTORY
426+
#undef O_DIRECTORY
427+
#endif
428+
414429
#ifdef HAVE_SYS_IOCTL_H
415430
#include <sys/ioctl.h>
416431
#endif

‎sender.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ static const char *orig_subdir(const char *path)
8282

8383
static int secure_sender_parent_fd(struct file_struct *file, const char *fname, const char **bname_p)
8484
{
85-
#ifdef AT_FDCWD
85+
#if defined AT_FDCWD && defined O_NOFOLLOW && defined O_DIRECTORY
8686
const char *path, *slash, *relp, *bslash, *fslash;
8787
char secure_path[MAXPATHLEN];
8888
int dfd, fl, slen;

‎syscall.c‎

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -424,11 +424,6 @@ static int ona_open_absolute_root(char *remaining, char *abspath, size_t abspath
424424
static int ona_open(const char *path, int flags, mode_t mode, char *out_abs, size_t out_cap)
425425
{
426426
#if defined AT_FDCWD && defined O_NOFOLLOW && defined O_DIRECTORY
427-
/* O_CLOEXEC predates some still-supported targets; mirror rand_bytes()'s
428-
* fallback in syscall.c so a build without it still compiles. */
429-
#ifndef O_CLOEXEC
430-
#define O_CLOEXEC 0
431-
#endif
432427
const int dir_traverse_flags = directory_traverse_flags() | O_CLOEXEC;
433428
if (!path || !*path) {
434429
errno = EINVAL;
@@ -3525,15 +3520,12 @@ int secure_relative_dirfd_at_beneath(int anchor_fd, const char *relpath)
35253520
directory_traverse_flags(), 0, 1);
35263521
}
35273522

3528-
#if defined O_NOFOLLOW && defined O_DIRECTORY && defined AT_FDCWD
3523+
#if defined O_NOFOLLOW && defined AT_FDCWD
35293524
/* Fill buf with len random bytes. Prefers /dev/urandom for cryptographic
35303525
* quality; falls back to rand() if /dev/urandom cannot be opened or read
35313526
* (e.g. inside a chroot or container without /dev populated). */
35323527
static void rand_bytes(unsigned char *buf, size_t len)
35333528
{
3534-
#ifndef O_CLOEXEC
3535-
#define O_CLOEXEC 0
3536-
#endif
35373529
int fd = open("/dev/urandom", O_RDONLY | O_CLOEXEC);
35383530
if (fd >= 0) {
35393531
ssize_t n = read(fd, buf, len);
@@ -3555,7 +3547,7 @@ static void rand_bytes(unsigned char *buf, size_t len)
35553547
* This is the create loop shared with secure_mkstemp(). */
35563548
int do_mkstemp_atfd(int dfd, char *filename, mode_t perms)
35573549
{
3558-
#ifdef AT_FDCWD
3550+
#if defined O_NOFOLLOW && defined AT_FDCWD
35593551
static const char letters[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
35603552
size_t filename_len = strlen(filename);
35613553
char *suffix;

‎testsuite/authenticate-no-ocloexec-build-regression_test.py‎

Lines changed: 0 additions & 82 deletions
This file was deleted.

0 commit comments

Comments
 (0)