Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace Signal's getFileDescriptorOwner with K-9's SafeContentResolver #2976

Merged
merged 3 commits into from
Apr 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,11 @@ dependencies {
exclude group: 'com.google.android.gms'
}

// Replacement for ContentResolver
// that protects against the Surreptitious Sharing attack.
// <https://github.com/cketti/SafeContentResolver>
implementation 'de.cketti.safecontentresolver:safe-content-resolver-v14:1.0.0'

testImplementation 'junit:junit:4.13.1'
testImplementation 'org.assertj:assertj-core:1.7.1'
testImplementation 'org.mockito:mockito-core:1.9.5'
Expand Down
4 changes: 1 addition & 3 deletions jni/Android.mk
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,6 @@ LOCAL_CFLAGS := -Werror -Wno-pointer-to-int-cast -Wno-int-to-pointer-cast -DNUL
LOCAL_CFLAGS += -Drestrict='' -D__EMX__ -DFIXED_POINT -DUSE_ALLOCA -DHAVE_LRINT -DHAVE_LRINTF -fno-math-errno
LOCAL_CFLAGS += -DANDROID_NDK -DDISABLE_IMPORTGL -fno-strict-aliasing -DAVOID_TABLES -DANDROID_TILE_BASED_DECODE -DANDROID_ARMV6_IDCT -ffast-math -D__STDC_CONSTANT_MACROS

LOCAL_SRC_FILES := \
utils/org_thoughtcrime_securesms_util_FileUtils.cpp \
dc_wrapper.c
LOCAL_SRC_FILES := dc_wrapper.c

include $(BUILD_SHARED_LIBRARY)
31 changes: 0 additions & 31 deletions jni/utils/org_thoughtcrime_securesms_util_FileUtils.cpp

This file was deleted.

21 changes: 0 additions & 21 deletions jni/utils/org_thoughtcrime_securesms_util_FileUtils.h

This file was deleted.

33 changes: 7 additions & 26 deletions src/org/thoughtcrime/securesms/ResolveMediaTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,16 @@

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.util.HashSet;

import static org.thoughtcrime.securesms.util.MediaUtil.getMimeType;

import de.cketti.safecontentresolver.SafeContentResolver;
import de.cketti.safecontentresolver.SafeContentResolverCompat;

public class ResolveMediaTask extends AsyncTask<Uri, Void, Uri> {

private static final String TAG = ResolveMediaTask.class.getSimpleName();
Expand Down Expand Up @@ -51,16 +54,8 @@ protected Uri doInBackground(Uri... uris) {
String fileName = null;
Long fileSize = null;

if (hasFileScheme(uri)) {
inputStream = this.openFileUri(uri);
if (uri.getPath() != null) {
File file = new File(uri.getPath());
fileName = file.getName();
fileSize = file.length();
}
} else {
inputStream = contextRef.get().getContentResolver().openInputStream(uri);
}
SafeContentResolver safeContentResolver = SafeContentResolverCompat.newInstance(contextRef.get());
inputStream = safeContentResolver.openInputStream(uri);

if (inputStream == null) {
return null;
Expand All @@ -87,7 +82,7 @@ protected Uri doInBackground(Uri... uris) {

String mimeType = getMimeType(contextRef.get(), uri);
return PersistentBlobProvider.getInstance().create(contextRef.get(), inputStream, mimeType, fileName, fileSize);
} catch (NullPointerException | IOException ioe) {
} catch (NullPointerException | FileNotFoundException ioe) {
Log.w(TAG, ioe);
return null;
}
Expand Down Expand Up @@ -119,20 +114,6 @@ public static void cancelTasks() {
}
}

private InputStream openFileUri(Uri uri) throws IOException {
FileInputStream fin = new FileInputStream(uri.getPath());
int owner = FileUtils.getFileDescriptorOwner(fin.getFD());


if (owner == -1 || owner == Process.myUid()) {
fin.close();
throw new IOException("File owned by application");
}

return fin;
}


private boolean hasFileScheme(Uri uri) {
if (uri == null) {
return false;
Expand Down
2 changes: 0 additions & 2 deletions src/org/thoughtcrime/securesms/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

public class FileUtils {

public static native int getFileDescriptorOwner(FileDescriptor fileDescriptor);

public static String sanitizeFilename(String name) {
if (TextUtils.isEmpty(name) || ".".equals(name) || "..".equals(name)) {
return "(invalid)";
Expand Down
Loading