From 40a001d517b3a49f11485621ebae22ea1b1a44a5 Mon Sep 17 00:00:00 2001 From: Nan Wu Date: Wed, 18 Jan 2023 16:01:21 +0000 Subject: [PATCH] Log instances where ParcelFileDescriptor.open is called with mode w, but without a or t. Starting in Android Q, the ParcelFileDescriptor#parseMode method no longer implicitly adds the truncate flag to mode "w". This commit adds logging to the central location where the result of parseMode would typically be passed to identify apps potentially affected by this change. Test: manual test. Change-Id: I35e0ceaafff0bef616ce8f998530cc610206e46b --- core/java/android/os/ParcelFileDescriptor.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/core/java/android/os/ParcelFileDescriptor.java b/core/java/android/os/ParcelFileDescriptor.java index 810bd636de074..7ad1735210c50 100644 --- a/core/java/android/os/ParcelFileDescriptor.java +++ b/core/java/android/os/ParcelFileDescriptor.java @@ -34,6 +34,7 @@ import static android.system.OsConstants.S_IWOTH; import android.annotation.NonNull; import android.annotation.SuppressLint; import android.annotation.TestApi; +import android.app.ActivityThread; import android.compat.annotation.UnsupportedAppUsage; import android.content.BroadcastReceiver; import android.content.ContentProvider; @@ -45,6 +46,7 @@ import android.system.Os; import android.system.OsConstants; import android.system.StructStat; import android.util.Log; +import android.util.Slog; import dalvik.system.CloseGuard; import dalvik.system.VMRuntime; @@ -329,6 +331,17 @@ public class ParcelFileDescriptor implements Parcelable, Closeable { } private static FileDescriptor openInternal(File file, int mode) throws FileNotFoundException { + if ((mode & MODE_WRITE_ONLY) != 0 && (mode & MODE_APPEND) == 0 + && (mode & MODE_TRUNCATE) == 0 && ((mode & MODE_READ_ONLY) == 0) + && file.exists()) { + String packageName = ActivityThread.currentApplication().getApplicationContext() + .getPackageName(); + Slog.wtfQuiet(TAG, "ParcelFileDescriptor.open is called with w without t or a or r, " + + "which will have a different behavior beginning in Android Q." + + "\nPackage Name: " + packageName + "\nMode: " + mode + + "\nFilename: " + file.getPath()); + } + final int flags = FileUtils.translateModePfdToPosix(mode) | ifAtLeastQ(O_CLOEXEC); int realMode = S_IRWXU | S_IRWXG;