Merge changes from topic "bionic_fdsan_java"

* changes:
  ParcelFileDescriptor: support bionic's fd ownership tracking.
  Reenable fdsan in the zygote post-fork.
This commit is contained in:
Treehugger Robot
2018-07-21 02:06:06 +00:00
committed by Gerrit Code Review
4 changed files with 13 additions and 104 deletions

View File

@@ -37,7 +37,6 @@ import libcore.util.SneakyThrow;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.FileDescriptor; import java.io.FileDescriptor;
import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.ObjectInputStream; import java.io.ObjectInputStream;
import java.io.ObjectOutputStream; import java.io.ObjectOutputStream;
@@ -2136,19 +2135,6 @@ public final class Parcel {
} }
} }
/** @deprecated use {@link android.system.Os#open(String, int, int)} */
@Deprecated
static native FileDescriptor openFileDescriptor(String file, int mode)
throws FileNotFoundException;
/** @deprecated use {@link android.system.Os#dup(FileDescriptor)} */
@Deprecated
static native FileDescriptor dupFileDescriptor(FileDescriptor orig) throws IOException;
/** @deprecated use {@link android.system.Os#close(FileDescriptor)} */
@Deprecated
static native void closeFileDescriptor(FileDescriptor desc) throws IOException;
/** /**
* Read a byte value from the parcel at the current dataPosition(). * Read a byte value from the parcel at the current dataPosition().
*/ */

View File

@@ -188,7 +188,13 @@ public class ParcelFileDescriptor implements Parcelable, Closeable {
} }
mWrapped = null; mWrapped = null;
mFd = fd; mFd = fd;
IoUtils.setFdOwner(mFd, this);
mCommFd = commChannel; mCommFd = commChannel;
if (mCommFd != null) {
IoUtils.setFdOwner(mCommFd, this);
}
mGuard.open("close"); mGuard.open("close");
} }
@@ -682,8 +688,7 @@ public class ParcelFileDescriptor implements Parcelable, Closeable {
if (mClosed) { if (mClosed) {
throw new IllegalStateException("Already closed"); throw new IllegalStateException("Already closed");
} }
final int fd = getFd(); int fd = IoUtils.acquireRawFd(mFd);
mFd.setInt$(-1);
writeCommStatusAndClose(Status.DETACHED, null); writeCommStatusAndClose(Status.DETACHED, null);
mClosed = true; mClosed = true;
mGuard.close(); mGuard.close();

View File

@@ -470,90 +470,6 @@ static jobject android_os_Parcel_readFileDescriptor(JNIEnv* env, jclass clazz, j
return NULL; return NULL;
} }
static jobject android_os_Parcel_openFileDescriptor(JNIEnv* env, jclass clazz,
jstring name, jint mode)
{
if (name == NULL) {
jniThrowNullPointerException(env, NULL);
return NULL;
}
ScopedUtfChars name8(env, name);
if (name8.c_str() == NULL) {
return NULL;
}
int flags=0;
switch (mode&0x30000000) {
case 0:
case 0x10000000:
flags = O_RDONLY;
break;
case 0x20000000:
flags = O_WRONLY;
break;
case 0x30000000:
flags = O_RDWR;
break;
}
if (mode&0x08000000) flags |= O_CREAT;
if (mode&0x04000000) flags |= O_TRUNC;
if (mode&0x02000000) flags |= O_APPEND;
int realMode = S_IRWXU|S_IRWXG;
if (mode&0x00000001) realMode |= S_IROTH;
if (mode&0x00000002) realMode |= S_IWOTH;
int fd = open(name8.c_str(), flags, realMode);
if (fd < 0) {
jniThrowException(env, "java/io/FileNotFoundException", strerror(errno));
return NULL;
}
jobject object = jniCreateFileDescriptor(env, fd);
if (object == NULL) {
close(fd);
}
return object;
}
static jobject android_os_Parcel_dupFileDescriptor(JNIEnv* env, jclass clazz, jobject orig)
{
if (orig == NULL) {
jniThrowNullPointerException(env, NULL);
return NULL;
}
int origfd = jniGetFDFromFileDescriptor(env, orig);
if (origfd < 0) {
jniThrowException(env, "java/lang/IllegalArgumentException", "bad FileDescriptor");
return NULL;
}
int fd = dup(origfd);
if (fd < 0) {
jniThrowIOException(env, errno);
return NULL;
}
jobject object = jniCreateFileDescriptor(env, fd);
if (object == NULL) {
close(fd);
}
return object;
}
static void android_os_Parcel_closeFileDescriptor(JNIEnv* env, jclass clazz, jobject object)
{
if (object == NULL) {
jniThrowNullPointerException(env, NULL);
return;
}
int fd = jniGetFDFromFileDescriptor(env, object);
if (fd >= 0) {
jniSetFileDescriptorOfFD(env, object, -1);
//ALOGI("Closing ParcelFileDescriptor %d\n", fd);
close(fd);
}
}
static jlong android_os_Parcel_create(JNIEnv* env, jclass clazz) static jlong android_os_Parcel_create(JNIEnv* env, jclass clazz)
{ {
Parcel* parcel = new Parcel(); Parcel* parcel = new Parcel();
@@ -796,10 +712,6 @@ static const JNINativeMethod gParcelMethods[] = {
{"nativeReadStrongBinder", "(J)Landroid/os/IBinder;", (void*)android_os_Parcel_readStrongBinder}, {"nativeReadStrongBinder", "(J)Landroid/os/IBinder;", (void*)android_os_Parcel_readStrongBinder},
{"nativeReadFileDescriptor", "(J)Ljava/io/FileDescriptor;", (void*)android_os_Parcel_readFileDescriptor}, {"nativeReadFileDescriptor", "(J)Ljava/io/FileDescriptor;", (void*)android_os_Parcel_readFileDescriptor},
{"openFileDescriptor", "(Ljava/lang/String;I)Ljava/io/FileDescriptor;", (void*)android_os_Parcel_openFileDescriptor},
{"dupFileDescriptor", "(Ljava/io/FileDescriptor;)Ljava/io/FileDescriptor;", (void*)android_os_Parcel_dupFileDescriptor},
{"closeFileDescriptor", "(Ljava/io/FileDescriptor;)V", (void*)android_os_Parcel_closeFileDescriptor},
{"nativeCreate", "()J", (void*)android_os_Parcel_create}, {"nativeCreate", "()J", (void*)android_os_Parcel_create},
{"nativeFreeBuffer", "(J)J", (void*)android_os_Parcel_freeBuffer}, {"nativeFreeBuffer", "(J)J", (void*)android_os_Parcel_freeBuffer},
{"nativeDestroy", "(J)V", (void*)android_os_Parcel_destroy}, {"nativeDestroy", "(J)V", (void*)android_os_Parcel_destroy},

View File

@@ -24,6 +24,7 @@
#include <sstream> #include <sstream>
#include <string> #include <string>
#include <android/fdsan.h>
#include <fcntl.h> #include <fcntl.h>
#include <grp.h> #include <grp.h>
#include <inttypes.h> #include <inttypes.h>
@@ -789,6 +790,8 @@ static pid_t ForkCommon(JNIEnv* env, jstring java_se_name, bool is_system_server
fail_fn(error_msg); fail_fn(error_msg);
} }
android_fdsan_error_level fdsan_error_level = android_fdsan_get_error_level();
pid_t pid = fork(); pid_t pid = fork();
if (pid == 0) { if (pid == 0) {
@@ -805,6 +808,9 @@ static pid_t ForkCommon(JNIEnv* env, jstring java_se_name, bool is_system_server
if (!gOpenFdTable->ReopenOrDetach(&error_msg)) { if (!gOpenFdTable->ReopenOrDetach(&error_msg)) {
fail_fn(error_msg); fail_fn(error_msg);
} }
// Turn fdsan back on.
android_fdsan_set_error_level(fdsan_error_level);
} }
// We blocked SIGCHLD prior to a fork, we unblock it here. // We blocked SIGCHLD prior to a fork, we unblock it here.