DivestOS/Patches/LineageOS-20.0/ASB-2023-10/mediaprovider-01.patch

453 lines
19 KiB
Diff
Raw Normal View History

20.0: October ASB picks wget https://github.com/GrapheneOS/platform_packages_services_Telecomm/commit/c873988898e1b520e0e4cfda77e26ec4377a4ce9.patch -O telecomm-01.patch wget https://github.com/GrapheneOS/platform_packages_providers_MediaProvider/commit/0fb5786dbf8b462eb106df912a7f65ab240f0d6a.patch -O mediaprovider-01.patch wget https://github.com/GrapheneOS/platform_packages_modules_Wifi/commit/1a4b9ef510410a8d8c90e80352357f08c49f10c5.patch -O wifi-01.patch wget https://github.com/GrapheneOS/platform_packages_modules_Bluetooth/commit/364a1d99624e8dca6501d98166efbb8061362970.patch -O bluetooth-01.patch wget https://github.com/GrapheneOS/platform_packages_apps_Settings/commit/87a06448b96e1ccd2403ae5c90b15efdd8585444.patch -O settings-01.patch wget https://github.com/GrapheneOS/platform_packages_apps_Settings/commit/aaba724a6858fba3754c11bfafd92b22f090c570.patch -O settings-02.patch wget https://github.com/GrapheneOS/platform_frameworks_native/commit/507304e1f59236675bfd820290b329f5f7334ec0.patch -O native-01.patch wget https://github.com/GrapheneOS/platform_frameworks_base/commit/89489ff5dd9b7717f0421ca4e90bc060af1ba8b7.patch -O base-01.patch wget https://github.com/GrapheneOS/platform_frameworks_base/commit/d1765c47157a99ecdc44537b5cadbb9726892967.patch -O base-02.patch wget https://github.com/GrapheneOS/platform_frameworks_base/commit/cbb1a0ecd6b67735bdb735d76606bc03f6b955bf.patch -O base-03.patch wget https://github.com/GrapheneOS/platform_frameworks_base/commit/4725772c0b3f0db2940e70851e145ec4ec71768b.patch -O base-04.patch wget https://github.com/GrapheneOS/platform_frameworks_base/commit/19747f69235d208e3d61099c76fa47aa792fe3a7.patch -O base-05.patch wget https://github.com/GrapheneOS/platform_frameworks_base/commit/e7a1aa9ed0bc69853bc83d098e15b8fa3b1881b4.patch -O base-06.patch wget https://github.com/GrapheneOS/platform_frameworks_base/commit/922a7860b1baf29ff5cb53a31d01c341cd2b9ecb.patch -O base-07.patch wget https://github.com/GrapheneOS/platform_frameworks_base/commit/ed183ed9122416026ed27d4877f96a545fe42316.patch -O base-08.patch wget https://github.com/GrapheneOS/platform_frameworks_base/commit/c6fbe1330a77c479ea3e29b54523682d0f248420.patch -O base-09.patch wget https://github.com/GrapheneOS/platform_frameworks_base/commit/9141cac175caaf176377d088e334d0991482fd6a.patch -O base-10.patch wget https://github.com/GrapheneOS/platform_frameworks_av/commit/41235bcc67a2122bc1d6a4d19e8356b3d1ada91e.patch -O av-01.patch wget https://github.com/GrapheneOS/platform_frameworks_av/commit/a89f704701e6af4a4809f4bb4911af88a023226f.patch -O av-02.patch wget https://github.com/GrapheneOS/platform_frameworks_av/commit/6d7cd80d77ed35efbe168f627dda021a5d8dd766.patch -O av-03.patch wget https://github.com/GrapheneOS/platform_frameworks_av/commit/75fc175a08c1a8e86d4649c19fd3136121518b96.patch -O av-04.patch wget https://github.com/GrapheneOS/platform_frameworks_av/commit/b023ec300f437494d6d6b23b03607e308dae43d2.patch -O av-05.patch wget https://github.com/GrapheneOS/platform_frameworks_av/commit/c8117d1539078bb3339b5d5fffe063a9135c2c21.patch -O av-06.patch wget https://github.com/GrapheneOS/platform_frameworks_av/commit/f06d23d824f60e98299d03f21c0715477666936d.patch -O av-07.patch wget https://github.com/GrapheneOS/platform_frameworks_av/commit/9c7408ab0710a9e36fd2a258098afde863cb6544.patch -O av-08.patch wget https://github.com/GrapheneOS/platform_packages_apps_Launcher3/commit/cfbfcefb3ce6bcd4d099cba4f45a8c6a0c02e6e6.patch -O launcher-01.patch wget https://github.com/GrapheneOS/platform_external_libxml2/commit/4a27a7f162907facfbeddf2d4ae4c6ab7c6eb15a.patch -O libxml-01.patch Signed-off-by: Tad <tad@spotco.us>
2023-10-03 14:30:46 -04:00
From 0fb5786dbf8b462eb106df912a7f65ab240f0d6a Mon Sep 17 00:00:00 2001
From: Sergey Nikolaienkov <sergeynv@google.com>
Date: Tue, 28 Mar 2023 12:22:31 +0200
Subject: [PATCH] Fix path traversal vulnerabilities in MediaProvider
Canonicalize filepath provided by the caller when hanling SCAN_FILE_CALL
method call in MediaProvider.
Additionally, make sure to check access permission in SCAN_FILE_CALL
(using enforceCallingPermissionInternal()).
Preemptively canonicalize Files provided as an arguments to the public
API methods in ModernMediaScanner (scanFile(), scanDirectory() and
onDirectoryDirty()) to prevent path traversal attacks.
Bug: 262244882
Test: atest MediaProviderTests
(cherry picked from https://googleplex-android-review.googlesource.com/q/commit:5bb32d7fba00b9e53c7e20ae8acaf6f84a8b2e8d)
Merged-In: I61e77d69ae857984b819fa0ea27bec5c26a34842
Change-Id: I61e77d69ae857984b819fa0ea27bec5c26a34842
---
.../providers/media/MediaProvider.java | 61 +++++++++++--------
.../media/scan/LegacyMediaScanner.java | 7 ---
.../providers/media/scan/MediaScanner.java | 25 ++++----
.../media/scan/ModernMediaScanner.java | 56 +++++++++++------
.../media/scan/NullMediaScanner.java | 6 --
.../providers/media/util/FileUtils.java | 47 ++++++++++----
.../media/scan/LegacyMediaScannerTest.java | 6 --
.../media/scan/ModernMediaScannerTest.java | 18 ------
.../media/scan/NullMediaScannerTest.java | 2 -
9 files changed, 117 insertions(+), 111 deletions(-)
diff --git a/src/com/android/providers/media/MediaProvider.java b/src/com/android/providers/media/MediaProvider.java
index 21eeed7f3..8c1df561b 100644
--- a/src/com/android/providers/media/MediaProvider.java
+++ b/src/com/android/providers/media/MediaProvider.java
@@ -1692,11 +1692,7 @@ public void scanDirectory(File file, int reason) {
}
public Uri scanFile(File file, int reason) {
- return scanFile(file, reason, null);
- }
-
- public Uri scanFile(File file, int reason, String ownerPackage) {
- return mMediaScanner.scanFile(file, reason, ownerPackage);
+ return mMediaScanner.scanFile(file, reason);
}
private Uri scanFileAsMediaProvider(File file, int reason) {
@@ -6197,38 +6193,51 @@ private Bundle callInternal(String method, String arg, Bundle extras) {
}
return null;
}
- case MediaStore.SCAN_FILE_CALL:
+ case MediaStore.SCAN_FILE_CALL: {
+ final LocalCallingIdentity token = clearLocalCallingIdentity();
+ final CallingIdentity providerToken = clearCallingIdentity();
+
+ final String filePath = arg;
+ final Uri uri;
+ try {
+ File file;
+ try {
+ file = FileUtils.getCanonicalFile(filePath);
+ } catch (IOException e) {
+ file = null;
+ }
+
+ uri = file != null ? scanFile(file, REASON_DEMAND) : null;
+ } finally {
+ restoreCallingIdentity(providerToken);
+ restoreLocalCallingIdentity(token);
+ }
+
+ // TODO(b/262244882): maybe enforceCallingPermissionInternal(uri, ...)
+
+ final Bundle res = new Bundle();
+ res.putParcelable(Intent.EXTRA_STREAM, uri);
+ return res;
+ }
case MediaStore.SCAN_VOLUME_CALL: {
final int userId = uidToUserId(Binder.getCallingUid());
final LocalCallingIdentity token = clearLocalCallingIdentity();
final CallingIdentity providerToken = clearCallingIdentity();
+
+ final String volumeName = arg;
try {
- final Bundle res = new Bundle();
- switch (method) {
- case MediaStore.SCAN_FILE_CALL: {
- final File file = new File(arg);
- res.putParcelable(Intent.EXTRA_STREAM, scanFile(file, REASON_DEMAND));
- break;
- }
- case MediaStore.SCAN_VOLUME_CALL: {
- final String volumeName = arg;
- try {
- MediaVolume volume = mVolumeCache.findVolume(volumeName,
- UserHandle.of(userId));
- MediaService.onScanVolume(getContext(), volume, REASON_DEMAND);
- } catch (FileNotFoundException e) {
- Log.w(TAG, "Failed to find volume " + volumeName, e);
- }
- break;
- }
- }
- return res;
+ final MediaVolume volume = mVolumeCache.findVolume(volumeName,
+ UserHandle.of(userId));
+ MediaService.onScanVolume(getContext(), volume, REASON_DEMAND);
+ } catch (FileNotFoundException e) {
+ Log.w(TAG, "Failed to find volume " + volumeName, e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
restoreCallingIdentity(providerToken);
restoreLocalCallingIdentity(token);
}
+ return Bundle.EMPTY;
}
case MediaStore.GET_VERSION_CALL: {
final String volumeName = extras.getString(Intent.EXTRA_TEXT);
diff --git a/src/com/android/providers/media/scan/LegacyMediaScanner.java b/src/com/android/providers/media/scan/LegacyMediaScanner.java
index d8d3bed41..d73dda584 100644
--- a/src/com/android/providers/media/scan/LegacyMediaScanner.java
+++ b/src/com/android/providers/media/scan/LegacyMediaScanner.java
@@ -19,8 +19,6 @@
import android.content.Context;
import android.net.Uri;
-import androidx.annotation.Nullable;
-
import com.android.providers.media.MediaVolume;
import java.io.File;
@@ -48,11 +46,6 @@ public Uri scanFile(File file, int reason) {
throw new UnsupportedOperationException();
}
- @Override
- public Uri scanFile(File file, int reason, @Nullable String ownerPackage) {
- throw new UnsupportedOperationException();
- }
-
@Override
public void onDetachVolume(MediaVolume volume) {
throw new UnsupportedOperationException();
diff --git a/src/com/android/providers/media/scan/MediaScanner.java b/src/com/android/providers/media/scan/MediaScanner.java
index 45d2a2436..eb5e4d6c7 100644
--- a/src/com/android/providers/media/scan/MediaScanner.java
+++ b/src/com/android/providers/media/scan/MediaScanner.java
@@ -24,23 +24,20 @@
import android.content.Context;
import android.net.Uri;
-import androidx.annotation.Nullable;
-
import com.android.providers.media.MediaVolume;
import java.io.File;
public interface MediaScanner {
- public static final int REASON_UNKNOWN = MEDIA_PROVIDER_SCAN_OCCURRED__REASON__UNKNOWN;
- public static final int REASON_MOUNTED = MEDIA_PROVIDER_SCAN_OCCURRED__REASON__MOUNTED;
- public static final int REASON_DEMAND = MEDIA_PROVIDER_SCAN_OCCURRED__REASON__DEMAND;
- public static final int REASON_IDLE = MEDIA_PROVIDER_SCAN_OCCURRED__REASON__IDLE;
-
- public Context getContext();
- public void scanDirectory(File file, int reason);
- public Uri scanFile(File file, int reason);
- public Uri scanFile(File file, int reason, @Nullable String ownerPackage);
- public void onDetachVolume(MediaVolume volume);
- public void onIdleScanStopped();
- public void onDirectoryDirty(File file);
+ int REASON_UNKNOWN = MEDIA_PROVIDER_SCAN_OCCURRED__REASON__UNKNOWN;
+ int REASON_MOUNTED = MEDIA_PROVIDER_SCAN_OCCURRED__REASON__MOUNTED;
+ int REASON_DEMAND = MEDIA_PROVIDER_SCAN_OCCURRED__REASON__DEMAND;
+ int REASON_IDLE = MEDIA_PROVIDER_SCAN_OCCURRED__REASON__IDLE;
+
+ Context getContext();
+ void scanDirectory(File file, int reason);
+ Uri scanFile(File file, int reason);
+ void onDetachVolume(MediaVolume volume);
+ void onIdleScanStopped();
+ void onDirectoryDirty(File file);
}
diff --git a/src/com/android/providers/media/scan/ModernMediaScanner.java b/src/com/android/providers/media/scan/ModernMediaScanner.java
index 41f53d57a..81133f7aa 100644
--- a/src/com/android/providers/media/scan/ModernMediaScanner.java
+++ b/src/com/android/providers/media/scan/ModernMediaScanner.java
@@ -50,6 +50,8 @@
import static com.android.providers.media.util.Metrics.translateReason;
+import static java.util.Objects.requireNonNull;
+
import android.content.ContentProviderClient;
import android.content.ContentProviderOperation;
import android.content.ContentProviderResult;
@@ -236,23 +238,36 @@ public Context getContext() {
}
@Override
- public void scanDirectory(File file, int reason) {
- try (Scan scan = new Scan(file, reason, /*ownerPackage*/ null)) {
+ public void scanDirectory(@NonNull File file, int reason) {
+ requireNonNull(file);
+ try {
+ file = file.getCanonicalFile();
+ } catch (IOException e) {
+ Log.e(TAG, "Couldn't canonicalize directory to scan" + file, e);
+ return;
+ }
+
+ try (Scan scan = new Scan(file, reason)) {
scan.run();
- } catch (OperationCanceledException ignored) {
} catch (FileNotFoundException e) {
- Log.e(TAG, "Couldn't find directory to scan", e) ;
+ Log.e(TAG, "Couldn't find directory to scan", e);
+ } catch (OperationCanceledException ignored) {
+ // No-op.
}
}
@Override
- public Uri scanFile(File file, int reason) {
- return scanFile(file, reason, /*ownerPackage*/ null);
- }
+ @Nullable
+ public Uri scanFile(@NonNull File file, int reason) {
+ requireNonNull(file);
+ try {
+ file = file.getCanonicalFile();
+ } catch (IOException e) {
+ Log.e(TAG, "Couldn't canonicalize file to scan" + file, e);
+ return null;
+ }
- @Override
- public Uri scanFile(File file, int reason, @Nullable String ownerPackage) {
- try (Scan scan = new Scan(file, reason, ownerPackage)) {
+ try (Scan scan = new Scan(file, reason)) {
scan.run();
return scan.getFirstResult();
} catch (OperationCanceledException ignored) {
@@ -286,10 +301,18 @@ public void onIdleScanStopped() {
}
@Override
- public void onDirectoryDirty(File dir) {
+ public void onDirectoryDirty(@NonNull File dir) {
+ requireNonNull(dir);
+ try {
+ dir = dir.getCanonicalFile();
+ } catch (IOException e) {
+ Log.e(TAG, "Couldn't canonicalize directory" + dir, e);
+ return;
+ }
+
synchronized (mPendingCleanDirectories) {
mPendingCleanDirectories.remove(dir.getPath());
- FileUtils.setDirectoryDirty(dir, /*isDirty*/ true);
+ FileUtils.setDirectoryDirty(dir, /* isDirty */ true);
}
}
@@ -320,7 +343,6 @@ private class Scan implements Runnable, FileVisitor<Path>, AutoCloseable {
private final String mVolumeName;
private final Uri mFilesUri;
private final CancellationSignal mSignal;
- private final String mOwnerPackage;
private final List<String> mExcludeDirs;
private final long mStartGeneration;
@@ -349,7 +371,7 @@ private class Scan implements Runnable, FileVisitor<Path>, AutoCloseable {
*/
private boolean mIsDirectoryTreeDirty;
- public Scan(File root, int reason, @Nullable String ownerPackage)
+ public Scan(File root, int reason)
throws FileNotFoundException {
Trace.beginSection("ctor");
@@ -371,7 +393,6 @@ public Scan(File root, int reason, @Nullable String ownerPackage)
mStartGeneration = MediaStore.getGeneration(mResolver, mVolumeName);
mSingleFile = mRoot.isFile();
- mOwnerPackage = ownerPackage;
mExcludeDirs = new ArrayList<>();
Trace.endSection();
@@ -800,10 +821,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
}
if (op != null) {
op.withValue(FileColumns._MODIFIER, FileColumns._MODIFIER_MEDIA_SCAN);
- // Add owner package name to new insertions when package name is provided.
- if (op.build().isInsert() && !attrs.isDirectory() && mOwnerPackage != null) {
- op.withValue(MediaColumns.OWNER_PACKAGE_NAME, mOwnerPackage);
- }
+
// Force DRM files to be marked as DRM, since the lower level
// stack may not set this correctly
if (isDrm) {
diff --git a/src/com/android/providers/media/scan/NullMediaScanner.java b/src/com/android/providers/media/scan/NullMediaScanner.java
index 7a1a39610..e53f96468 100644
--- a/src/com/android/providers/media/scan/NullMediaScanner.java
+++ b/src/com/android/providers/media/scan/NullMediaScanner.java
@@ -56,12 +56,6 @@ public Uri scanFile(File file, int reason) {
return null;
}
- @Override
- public Uri scanFile(File file, int reason, @Nullable String ownerPackage) {
- Log.w(TAG, "Ignoring scan request for " + file);
- return null;
- }
-
@Override
public void onDetachVolume(MediaVolume volume) {
// Ignored
diff --git a/src/com/android/providers/media/util/FileUtils.java b/src/com/android/providers/media/util/FileUtils.java
index 097eca8c9..6c2695c78 100644
--- a/src/com/android/providers/media/util/FileUtils.java
+++ b/src/com/android/providers/media/util/FileUtils.java
@@ -1110,18 +1110,25 @@ public static int extractUserId(@Nullable String data) {
}
public static @Nullable String extractRelativePath(@Nullable String data) {
- data = getCanonicalPath(data);
if (data == null) return null;
- final Matcher matcher = PATTERN_RELATIVE_PATH.matcher(data);
+ final String path;
+ try {
+ path = getCanonicalPath(data);
+ } catch (IOException e) {
+ Log.d(TAG, "Unable to get canonical path from invalid data path: " + data, e);
+ return null;
+ }
+
+ final Matcher matcher = PATTERN_RELATIVE_PATH.matcher(path);
if (matcher.find()) {
- final int lastSlash = data.lastIndexOf('/');
+ final int lastSlash = path.lastIndexOf('/');
if (lastSlash == -1 || lastSlash < matcher.end()) {
// This is a file in the top-level directory, so relative path is "/"
// which is different than null, which means unknown path
return "/";
} else {
- return data.substring(matcher.end(), lastSlash + 1);
+ return path.substring(matcher.end(), lastSlash + 1);
}
} else {
return null;
@@ -1769,15 +1776,29 @@ public static File fromFuseFile(File file) {
return new File(file.getPath().replaceFirst(FUSE_FS_PREFIX, LOWER_FS_PREFIX));
}
- @Nullable
- private static String getCanonicalPath(@Nullable String path) {
- if (path == null) return null;
+ /**
+ * Returns the canonical {@link File} for the provided abstract pathname.
+ *
+ * @return The canonical pathname string denoting the same file or directory as this abstract
+ * pathname
+ * @see File#getCanonicalFile()
+ */
+ @NonNull
+ public static File getCanonicalFile(@NonNull String path) throws IOException {
+ Objects.requireNonNull(path);
+ return new File(path).getCanonicalFile();
+ }
- try {
- return new File(path).getCanonicalPath();
- } catch (IOException e) {
- Log.d(TAG, "Unable to get canonical path from invalid data path: " + path, e);
- return null;
- }
+ /**
+ * Returns the canonical pathname string of the provided abstract pathname.
+ *
+ * @return The canonical pathname string denoting the same file or directory as this abstract
+ * pathname.
+ * @see File#getCanonicalPath()
+ */
+ @NonNull
+ public static String getCanonicalPath(@NonNull String path) throws IOException {
+ Objects.requireNonNull(path);
+ return new File(path).getCanonicalPath();
}
}
diff --git a/tests/src/com/android/providers/media/scan/LegacyMediaScannerTest.java b/tests/src/com/android/providers/media/scan/LegacyMediaScannerTest.java
index cf9cb395d..2831e963e 100644
--- a/tests/src/com/android/providers/media/scan/LegacyMediaScannerTest.java
+++ b/tests/src/com/android/providers/media/scan/LegacyMediaScannerTest.java
@@ -47,12 +47,6 @@ public void testSimple() throws Exception {
fail();
} catch (UnsupportedOperationException expected) {
}
- try {
- scanner.scanFile(new File("/dev/null"), MediaScanner.REASON_UNKNOWN,
- InstrumentationRegistry.getContext().getPackageName());
- fail();
- } catch (UnsupportedOperationException expected) {
- }
try {
scanner.onDetachVolume(null);
fail();
diff --git a/tests/src/com/android/providers/media/scan/ModernMediaScannerTest.java b/tests/src/com/android/providers/media/scan/ModernMediaScannerTest.java
index 0450c1cee..06f10ed21 100644
--- a/tests/src/com/android/providers/media/scan/ModernMediaScannerTest.java
+++ b/tests/src/com/android/providers/media/scan/ModernMediaScannerTest.java
@@ -779,24 +779,6 @@ public void testScan_missingFile() throws Exception {
assertThat(mModern.scanFile(image, REASON_UNKNOWN)).isNull();
}
- @Test
- public void testScanFileAndUpdateOwnerPackageName() throws Exception {
- final File image = new File(mDir, "image.jpg");
- final String thisPackageName = InstrumentationRegistry.getContext().getPackageName();
- stage(R.raw.test_image, image);
-
- assertQueryCount(0, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
- // scanning the image file inserts new database entry with OWNER_PACKAGE_NAME as
- // thisPackageName.
- assertNotNull(mModern.scanFile(image, REASON_UNKNOWN, thisPackageName));
- try (Cursor cursor = mIsolatedResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
- new String[] {MediaColumns.OWNER_PACKAGE_NAME}, null, null, null)) {
- assertEquals(1, cursor.getCount());
- cursor.moveToNext();
- assertEquals(thisPackageName, cursor.getString(0));
- }
- }
-
/**
* Verify fix for obscure bug which would cause us to delete files outside a
* directory that share a common prefix.
diff --git a/tests/src/com/android/providers/media/scan/NullMediaScannerTest.java b/tests/src/com/android/providers/media/scan/NullMediaScannerTest.java
index 063f1b7a3..265d1a97a 100644
--- a/tests/src/com/android/providers/media/scan/NullMediaScannerTest.java
+++ b/tests/src/com/android/providers/media/scan/NullMediaScannerTest.java
@@ -38,8 +38,6 @@ public void testSimple() throws Exception {
scanner.scanDirectory(new File("/dev/null"), MediaScanner.REASON_UNKNOWN);
scanner.scanFile(new File("/dev/null"), MediaScanner.REASON_UNKNOWN);
- scanner.scanFile(new File("/dev/null"), MediaScanner.REASON_UNKNOWN,
- InstrumentationRegistry.getContext().getPackageName());
scanner.onDetachVolume(null);
}