Skip to content

Commit 0bafc5a

Browse files
Mrhs121HyukjinKwon
andcommitted
[SPARK-38631][CORE] Uses Java-based implementation for un-tarring at Utils.unpack (Kyligence#643)
### What changes were proposed in this pull request? This PR proposes to use `FileUtil.unTarUsingJava` that is a Java implementation for un-tar `.tar` files. `unTarUsingJava` is not public but it exists in all Hadoop versions from 2.1+, see HADOOP-9264. The security issue reproduction requires a non-Windows platform, and a non-gzipped TAR archive file name (contents don't matter). ### Why are the changes needed? There is a risk for arbitrary shell command injection via `Utils.unpack` when the filename is controlled by a malicious user. This is due to an issue in Hadoop's `unTar`, that is not properly escaping the filename before passing to a shell command:https://github.com/apache/hadoop/blob/trunk/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/fs/FileUtil.java#L904 ### Does this PR introduce _any_ user-facing change? Yes, it prevents a security issue that, previously, allowed users to execute arbitrary shall command. ### How was this patch tested? Manually tested in local, and existing test cases should cover. Closes apache#35946 from HyukjinKwon/SPARK-38631. Authored-by: Hyukjin Kwon <gurwls223@apache.org> (cherry picked from commit 057c051) Signed-off-by: Hyukjin Kwon <gurwls223@apache.org> Co-authored-by: Hyukjin Kwon <gurwls223@apache.org>
1 parent 289884d commit 0bafc5a

1 file changed

Lines changed: 28 additions & 2 deletions

File tree

core/src/main/scala/org/apache/spark/util/Utils.scala

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -596,16 +596,42 @@ private[spark] object Utils extends Logging {
596596
if (lowerSrc.endsWith(".jar")) {
597597
RunJar.unJar(source, dest, RunJar.MATCH_ANY)
598598
} else if (lowerSrc.endsWith(".zip")) {
599+
// TODO(SPARK-37677): should keep file permissions. Java implementation doesn't.
599600
FileUtil.unZip(source, dest)
600-
} else if (
601-
lowerSrc.endsWith(".tar.gz") || lowerSrc.endsWith(".tgz") || lowerSrc.endsWith(".tar")) {
601+
} else if (lowerSrc.endsWith(".tar.gz") || lowerSrc.endsWith(".tgz")) {
602602
FileUtil.unTar(source, dest)
603+
} else if (lowerSrc.endsWith(".tar")) {
604+
// TODO(SPARK-38632): should keep file permissions. Java implementation doesn't.
605+
unTarUsingJava(source, dest)
603606
} else {
604607
logWarning(s"Cannot unpack $source, just copying it to $dest.")
605608
copyRecursive(source, dest)
606609
}
607610
}
608611

612+
/**
613+
* The method below was copied from `FileUtil.unTar` but uses Java-based implementation
614+
* to work around a security issue, see also SPARK-38631.
615+
*/
616+
private def unTarUsingJava(source: File, dest: File): Unit = {
617+
if (!dest.mkdirs && !dest.isDirectory) {
618+
throw new IOException(s"Mkdirs failed to create $dest")
619+
} else {
620+
try {
621+
// Should not fail because all Hadoop 2.1+ (from HADOOP-9264)
622+
// have 'unTarUsingJava'.
623+
val mth = classOf[FileUtil].getDeclaredMethod(
624+
"unTarUsingJava", classOf[File], classOf[File], classOf[Boolean])
625+
mth.setAccessible(true)
626+
mth.invoke(null, source, dest, java.lang.Boolean.FALSE)
627+
} catch {
628+
// Re-throw the original exception.
629+
case e: java.lang.reflect.InvocationTargetException if e.getCause != null =>
630+
throw e.getCause
631+
}
632+
}
633+
}
634+
609635
/** Records the duration of running `body`. */
610636
def timeTakenMs[T](body: => T): (T, Long) = {
611637
val startTime = System.nanoTime()

0 commit comments

Comments
 (0)