Blame SOURCES/BZ-1600618-reposync-prevent-path-traversal.patch

707651
diff -up yum-utils-1.1.31/docs/reposync.1.orig yum-utils-1.1.31/docs/reposync.1
707651
--- yum-utils-1.1.31/docs/reposync.1.orig	2018-07-20 11:01:16.789747160 +0200
707651
+++ yum-utils-1.1.31/docs/reposync.1	2018-07-20 11:01:21.765700655 +0200
707651
@@ -47,6 +47,16 @@ Download all the non-default metadata.
707651
 Download only newest packages per-repo.
707651
 .IP "\fB\-q, \-\-quiet\fP"
707651
 Output as little information as possible.
707651
+.IP "\fB\-\-allow-path-traversal\fP"
707651
+Allow packages stored outside their repo directory to be synced.
707651
+These are packages that are referenced in metadata by using absolute paths or
707651
+up-level ".." symbols, and are normally skipped by \fBreposync\fR for security
707651
+reasons.
707651
+
707651
+\fBCAUTION:\fR Using this option has potential security implications since, by
707651
+providing malicious repodata, an attacker could make \fBreposync\fR write to
707651
+arbitrary locations on the file system that are accessible by the user running
707651
+it.
707651
 .SH "EXAMPLES"
707651
 .IP "Sync all packages from the 'updates' repo to the current directory:"
707651
 \fB reposync \-\-repoid=updates\fP
707651
diff -up yum-utils-1.1.31/reposync.py.orig yum-utils-1.1.31/reposync.py
707651
--- yum-utils-1.1.31/reposync.py.orig	2018-07-20 11:01:16.752747506 +0200
707651
+++ yum-utils-1.1.31/reposync.py	2018-07-20 11:01:54.717392693 +0200
707651
@@ -84,6 +84,12 @@ def localpkgs(directory):
707651
             cache[name] = { 'path': fn, 'size': st.st_size, 'device': st.st_dev }
707651
     return cache
707651
 
707651
+def is_subpath(path, root):
707651
+    root = os.path.realpath(root)
707651
+    path = os.path.realpath(os.path.join(root, path))
707651
+    # join() is used below to ensure root ends with a slash
707651
+    return path.startswith(os.path.join(root, ''))
707651
+
707651
 def parseArgs():
707651
     usage = """
707651
     Reposync is used to synchronize a remote yum repository to a local 
707651
@@ -126,6 +132,10 @@ def parseArgs():
707651
     parser.add_option("","--download-metadata", dest="downloadmd", 
707651
         default=False, action="store_true", 
707651
         help="download all the non-default metadata")
707651
+    parser.add_option("", "--allow-path-traversal", default=False,
707651
+        action="store_true",
707651
+        help="Allow packages stored outside their repo directory to be synced "
707651
+             "(UNSAFE, USE WITH CAUTION!)")
707651
     (opts, args) = parser.parse_args()
707651
     return (opts, args)
707651
 
707651
@@ -226,13 +236,36 @@ def main():
707651
         else:
707651
             local_repo_path = opts.destdir + '/' + repo.id
707651
 
707651
+        # Ensure we don't traverse out of local_repo_path by dropping any
707651
+        # packages whose remote_path is absolute or contains up-level
707651
+        # references (unless explicitly allowed).
707651
+        # See RHBZ#1600221 for details.
707651
+        if not opts.allow_path_traversal:
707651
+            newlist = []
707651
+            skipped = False
707651
+            for pkg in download_list:
707651
+                if is_subpath(pkg.remote_path, local_repo_path):
707651
+                    newlist.append(pkg)
707651
+                    continue
707651
+                my.logger.warning(
707651
+                    'WARNING: skipping package %s: remote path "%s" not '
707651
+                    'within repodir, unsafe to mirror locally'
707651
+                    % (pkg, pkg.remote_path)
707651
+                )
707651
+                skipped = True
707651
+            if skipped:
707651
+                my.logger.info(
707651
+                    'You can enable unsafe remote paths by using '
707651
+                    '--allow-path-traversal (see reposync(1) for details)'
707651
+                )
707651
+            download_list = newlist
707651
+
707651
         if opts.delete and os.path.exists(local_repo_path):
707651
             current_pkgs = localpkgs(local_repo_path)
707651
 
707651
             download_set = {}
707651
             for pkg in download_list:
707651
-                remote = pkg.returnSimple('relativepath')
707651
-                rpmname = os.path.basename(remote)
707651
+                rpmname = os.path.basename(pkg.remote_path)
707651
                 download_set[rpmname] = 1
707651
 
707651
             for pkg in current_pkgs:
707651
@@ -280,8 +313,7 @@ def main():
707651
         local_size  = 0
707651
         if not opts.urls:
707651
             for pkg in download_list:
707651
-                remote = pkg.returnSimple('relativepath')
707651
-                local = local_repo_path + '/' + remote
707651
+                local = os.path.join(local_repo_path, pkg.remote_path)
707651
                 sz = int(pkg.returnSimple('packagesize'))
707651
                 if os.path.exists(local) and os.path.getsize(local) == sz:
707651
                     continue
707651
@@ -293,10 +325,9 @@ def main():
707651
         download_list.sort(sortPkgObj)
707651
         if opts.urls:
707651
             for pkg in download_list:
707651
-                remote = pkg.returnSimple('relativepath')
707651
-                local = os.path.join(local_repo_path, remote)
707651
+                local = os.path.join(local_repo_path, pkg.remote_path)
707651
                 if not (os.path.exists(local) and my.verifyPkg(local, pkg, False)):
707651
-                    print urljoin(pkg.repo.urls[0], pkg.relativepath)
707651
+                    print urljoin(pkg.repo.urls[0], pkg.remote_path)
707651
             continue
707651
 
707651
         # create dest dir
707651
@@ -305,8 +336,7 @@ def main():
707651
 
707651
         # set localpaths
707651
         for pkg in download_list:
707651
-            rpmfn = pkg.remote_path
707651
-            pkg.localpath = os.path.join(local_repo_path, rpmfn)
707651
+            pkg.localpath = os.path.join(local_repo_path, pkg.remote_path)
707651
             pkg.repo.copy_local = True
707651
             pkg.repo.cache = 0
707651
             localdir = os.path.dirname(pkg.localpath)