Bug 1542180

Summary: rsync: fails to start if the current directory is unreachable
Product: Red Hat Enterprise Linux 7 Reporter: Florian Weimer <fweimer>
Component: rsyncAssignee: Pavel Zhukov <pzhukov>
Status: CLOSED WONTFIX QA Contact: BaseOS QE Security Team <qe-baseos-security>
Severity: urgent Docs Contact:
Priority: high    
Version: 7.5-AltCC: bugzilla, mruprich, rhinduja, szidek, thozza
Target Milestone: rc   
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: 2018-02-22 05:17:17 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On:    
Bug Blocks: 1534635    
Attachments:
Description Flags
rsync patch
none
rsync patch none

Description Florian Weimer 2018-02-05 18:58:02 UTC
After the fix for CVE-2018-1000001, getcwd returns ENOENT if the current directory is unreachable and does not have a name.

This happens because this:

	/* Initialize change_dir() here because on some old systems getcwd
	 * (implemented by forking "pwd" and reading its output) doesn't
	 * work when there are other child processes.  Also, on all systems
	 * that implement getcwd that way "pwd" can't be found after chroot. */
	change_dir(NULL, CD_NORMAL);

And this:

/* Like chdir(), but it keeps track of the current directory (in the
 * global "curr_dir"), and ensures that the path size doesn't overflow.
 * Also cleans the path using the clean_fname() function. */
int change_dir(const char *dir, int set_path_only)
{
	static int initialised, skipped_chdir;
	unsigned int len;

	if (!initialised) {
		initialised = 1;
		if (getcwd(curr_dir, sizeof curr_dir - 1) == NULL) {
			rsyserr(FERROR, errno, "getcwd()");
			exit_cleanup(RERR_FILESELECT);
		}
		curr_dir_len = strlen(curr_dir);
	}

This error is reached even if all input paths to rsync are absolute and the current directory path is never needed.

To trigger this, it is sufficient to execve rsync from a process whose current directory has a pending lazy umount (MNT_DETACH/umount -l).  The execve makes the current directory unreachable in the new process image (at least on kernel 4.14.14).

Reportedly, this break glusterfs geo-replication.

References:

https://lists.gluster.org/pipermail/gluster-users/2018-January/033293.html
https://sourceware.org/ml/libc-alpha/2018-02/msg00152.html
https://bugs.launchpad.net/bugs/1746995

Comment 1 Florian Weimer 2018-02-06 13:20:42 UTC
Created attachment 1392075 [details]
rsync patch

Posted for upstream review:

https://lists.samba.org/archive/rsync/2018-February/031476.html

Comment 2 Florian Weimer 2018-02-06 13:44:16 UTC
Created attachment 1392082 [details]
rsync patch

Got tricked by Git and submitted the wrong patch.

Comment 6 Mrten 2018-02-08 10:04:48 UTC
Glusterfs problem also reported here: bug 1542979

Comment 13 Pavel Zhukov 2018-02-21 17:41:31 UTC
I've dig into the issue a bit more.
Actually there is bug in rsync which *allowed* it to operate if directory is unavailable (i.e. getcwd() returned (unavailable)/path before the CVE fixed). It's clear from the util.c:change_dir() and [1] that it was designed to prevent this situation as it may lead undefined behavior.
IMO Gluster's code should be changed to not exec() applications from unmounted directory instead of putting all rsync users on risk of running from withing unavailable directory. 

Small reproducer is in [1] and looks like: mkdir test && cd test && rmdir ../test &&  rsync /etc/fstab /tmp/

[1] https://bugzilla.samba.org/show_bug.cgi?id=6422

Comment 14 Pavel Zhukov 2018-02-22 05:17:17 UTC
Based on #13 I'm closing this bug.
@Florian, thank you for your time and patch provided. However it's too risky to operate if current directory is not available and upper level software (in this case it's gluster) should take care of this.