mirror of
https://github.com/ArcaneChat/android.git
synced 2026-07-03 14:05:24 +02:00
Replace Signal's getFileDescriptorOwner with K-9's SafeContentResolver (#2976)
This commit is contained in:
@@ -64,6 +64,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'
|
||||
|
||||
+1
-3
@@ -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)
|
||||
|
||||
@@ -1,31 +0,0 @@
|
||||
#include "org_thoughtcrime_securesms_util_FileUtils.h"
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
jint JNICALL Java_org_thoughtcrime_securesms_util_FileUtils_getFileDescriptorOwner
|
||||
(JNIEnv *env, jclass clazz, jobject fileDescriptor)
|
||||
{
|
||||
jclass fdClass = env->GetObjectClass(fileDescriptor);
|
||||
|
||||
if (fdClass == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
jfieldID fdFieldId = env->GetFieldID(fdClass, "descriptor", "I");
|
||||
|
||||
if (fdFieldId == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
int fd = env->GetIntField(fileDescriptor, fdFieldId);
|
||||
|
||||
struct stat stat_struct;
|
||||
|
||||
if (fstat(fd, &stat_struct) != 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return stat_struct.st_uid;
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
/* DO NOT EDIT THIS FILE - it is machine generated */
|
||||
#include <jni.h>
|
||||
/* Header for class org_thoughtcrime_securesms_util_FileUtils */
|
||||
|
||||
#ifndef _Included_org_thoughtcrime_securesms_util_FileUtils
|
||||
#define _Included_org_thoughtcrime_securesms_util_FileUtils
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
/*
|
||||
* Class: org_thoughtcrime_securesms_util_FileUtils
|
||||
* Method: getFileDescriptorOwner
|
||||
* Signature: (Ljava/io/FileDescriptor;)I
|
||||
*/
|
||||
JNIEXPORT jint JNICALL Java_org_thoughtcrime_securesms_util_FileUtils_getFileDescriptorOwner
|
||||
(JNIEnv *, jclass, jobject);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@@ -13,13 +13,16 @@ import org.thoughtcrime.securesms.util.FileUtils;
|
||||
|
||||
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();
|
||||
@@ -51,16 +54,8 @@ public class ResolveMediaTask extends AsyncTask<Uri, Void, Uri> {
|
||||
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;
|
||||
@@ -87,7 +82,7 @@ public class ResolveMediaTask extends AsyncTask<Uri, Void, Uri> {
|
||||
|
||||
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;
|
||||
}
|
||||
@@ -119,20 +114,6 @@ public class ResolveMediaTask extends AsyncTask<Uri, Void, Uri> {
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
@@ -7,8 +7,6 @@ import java.nio.charset.StandardCharsets;
|
||||
|
||||
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)";
|
||||
|
||||
Reference in New Issue
Block a user