From 5fc8bbe1140879141893980194db18760bf8876a Mon Sep 17 00:00:00 2001 From: Josh Gao Date: Tue, 17 Jul 2018 15:00:31 -0700 Subject: [PATCH] Fix double close in NativeLibraryHelper.openApkFd. Prior to this patch, we were taking the file descriptor owned by a ParcelFileDescriptor, and passing it into ZipFileRO::openFd, which expects to take ownership of the file descriptor, closing it upon destruction. This leads to a double-close when the ParcelFileDescriptor tries to close itself. Switch to passing a duped copy of the file descriptor to ZipFileRO::openFd. Test: `pm install foo.apk` with fdsan Change-Id: Ida4ca4a37b82875dc4eef1f37bf2322c422fe038 (cherry-picked from commit b066087d65b720a5c9ac48f64a856284566df82f) --- ...om_android_internal_content_NativeLibraryHelper.cpp | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/core/jni/com_android_internal_content_NativeLibraryHelper.cpp b/core/jni/com_android_internal_content_NativeLibraryHelper.cpp index cc2646cfb7c01..dc0426987b1e3 100644 --- a/core/jni/com_android_internal_content_NativeLibraryHelper.cpp +++ b/core/jni/com_android_internal_content_NativeLibraryHelper.cpp @@ -27,6 +27,7 @@ #include +#include #include #include #include @@ -567,7 +568,14 @@ com_android_internal_content_NativeLibraryHelper_openApkFd(JNIEnv *env, jclass, return 0; } - ZipFileRO* zipFile = ZipFileRO::openFd(fd, debugFilePath.c_str()); + int dupedFd = dup(fd); + if (dupedFd == -1) { + jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", + "Failed to dup FileDescriptor: %s", strerror(errno)); + return 0; + } + + ZipFileRO* zipFile = ZipFileRO::openFd(dupedFd, debugFilePath.c_str()); return reinterpret_cast(zipFile); }