1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
|
/*
* Copyright (C) 2019 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specic language governing permissions and
* limitations under the License.
*/
#include "MediaProviderWrapper.h"
#include "libfuse_jni/ReaddirHelper.h"
#include <android-base/logging.h>
#include <android-base/properties.h>
#include <jni.h>
#include <nativehelper/scoped_local_ref.h>
#include <nativehelper/scoped_primitive_array.h>
#include <nativehelper/scoped_utf_chars.h>
#include <pthread.h>
#include <mutex>
#include <unordered_map>
namespace mediaprovider {
namespace fuse {
using android::base::GetBoolProperty;
using std::string;
namespace {
constexpr const char* kPropRedactionEnabled = "persist.sys.fuse.redaction-enabled";
constexpr uid_t ROOT_UID = 0;
constexpr uid_t SHELL_UID = 2000;
/** Private helper functions **/
inline bool shouldBypassMediaProvider(uid_t uid) {
return uid == SHELL_UID || uid == ROOT_UID;
}
static bool CheckForJniException(JNIEnv* env) {
if (env->ExceptionCheck()) {
env->ExceptionDescribe();
env->ExceptionClear();
return true;
}
return false;
}
std::unique_ptr<RedactionInfo> getRedactionInfoInternal(JNIEnv* env, jobject media_provider_object,
jmethodID mid_get_redaction_ranges,
uid_t uid, pid_t tid, const string& path) {
ScopedLocalRef<jstring> j_path(env, env->NewStringUTF(path.c_str()));
ScopedLongArrayRO redaction_ranges(
env, static_cast<jlongArray>(env->CallObjectMethod(
media_provider_object, mid_get_redaction_ranges, j_path.get(), uid, tid)));
if (CheckForJniException(env)) {
return nullptr;
}
std::unique_ptr<RedactionInfo> ri;
if (redaction_ranges.size() % 2) {
LOG(ERROR) << "Error while calculating redaction ranges: array length is uneven";
} else if (redaction_ranges.size() > 0) {
ri = std::make_unique<RedactionInfo>(redaction_ranges.size() / 2, redaction_ranges.get());
} else {
// No ranges to redact
ri = std::make_unique<RedactionInfo>();
}
return ri;
}
int insertFileInternal(JNIEnv* env, jobject media_provider_object, jmethodID mid_insert_file,
const string& path, uid_t uid) {
ScopedLocalRef<jstring> j_path(env, env->NewStringUTF(path.c_str()));
int res = env->CallIntMethod(media_provider_object, mid_insert_file, j_path.get(), uid);
if (CheckForJniException(env)) {
return EFAULT;
}
return res;
}
int deleteFileInternal(JNIEnv* env, jobject media_provider_object, jmethodID mid_delete_file,
const string& path, uid_t uid) {
ScopedLocalRef<jstring> j_path(env, env->NewStringUTF(path.c_str()));
int res = env->CallIntMethod(media_provider_object, mid_delete_file, j_path.get(), uid);
if (CheckForJniException(env)) {
return EFAULT;
}
return res;
}
int isOpenAllowedInternal(JNIEnv* env, jobject media_provider_object, jmethodID mid_is_open_allowed,
const string& path, uid_t uid, bool for_write) {
ScopedLocalRef<jstring> j_path(env, env->NewStringUTF(path.c_str()));
int res = env->CallIntMethod(media_provider_object, mid_is_open_allowed, j_path.get(), uid,
for_write);
if (CheckForJniException(env)) {
return EFAULT;
}
return res;
}
void scanFileInternal(JNIEnv* env, jobject media_provider_object, jmethodID mid_scan_file,
const string& path) {
ScopedLocalRef<jstring> j_path(env, env->NewStringUTF(path.c_str()));
env->CallVoidMethod(media_provider_object, mid_scan_file, j_path.get());
CheckForJniException(env);
}
int isMkdirOrRmdirAllowedInternal(JNIEnv* env, jobject media_provider_object,
jmethodID mid_is_mkdir_or_rmdir_allowed, const string& path,
uid_t uid, bool forCreate) {
ScopedLocalRef<jstring> j_path(env, env->NewStringUTF(path.c_str()));
int res = env->CallIntMethod(media_provider_object, mid_is_mkdir_or_rmdir_allowed, j_path.get(),
uid, forCreate);
if (CheckForJniException(env)) {
return EFAULT;
}
return res;
}
int isOpendirAllowedInternal(JNIEnv* env, jobject media_provider_object,
jmethodID mid_is_opendir_allowed, const string& path, uid_t uid,
bool forWrite) {
ScopedLocalRef<jstring> j_path(env, env->NewStringUTF(path.c_str()));
int res = env->CallIntMethod(media_provider_object, mid_is_opendir_allowed, j_path.get(), uid,
forWrite);
if (CheckForJniException(env)) {
return EFAULT;
}
return res;
}
bool isUidForPackageInternal(JNIEnv* env, jobject media_provider_object,
jmethodID mid_is_uid_for_package, const string& pkg, uid_t uid) {
ScopedLocalRef<jstring> j_pkg(env, env->NewStringUTF(pkg.c_str()));
bool res = env->CallBooleanMethod(media_provider_object, mid_is_uid_for_package, j_pkg.get(),
uid);
if (CheckForJniException(env)) {
return false;
}
return res;
}
std::vector<std::shared_ptr<DirectoryEntry>> getFilesInDirectoryInternal(
JNIEnv* env, jobject media_provider_object, jmethodID mid_get_files_in_dir, uid_t uid,
const string& path) {
std::vector<std::shared_ptr<DirectoryEntry>> directory_entries;
ScopedLocalRef<jstring> j_path(env, env->NewStringUTF(path.c_str()));
ScopedLocalRef<jobjectArray> files_list(
env, static_cast<jobjectArray>(env->CallObjectMethod(
media_provider_object, mid_get_files_in_dir, j_path.get(), uid)));
if (CheckForJniException(env)) {
directory_entries.push_back(std::make_shared<DirectoryEntry>("", EFAULT));
return directory_entries;
}
int de_count = env->GetArrayLength(files_list.get());
if (de_count == 1) {
ScopedLocalRef<jstring> j_d_name(env,
(jstring)env->GetObjectArrayElement(files_list.get(), 0));
ScopedUtfChars d_name(env, j_d_name.get());
if (d_name.c_str() == nullptr) {
LOG(ERROR) << "Error reading file name returned from MediaProvider at index " << 0;
directory_entries.push_back(std::make_shared<DirectoryEntry>("", EFAULT));
return directory_entries;
} else if (d_name.c_str()[0] == '\0') {
// Calling package has no storage permissions.
directory_entries.push_back(std::make_shared<DirectoryEntry>("", EPERM));
return directory_entries;
}
}
for (int i = 0; i < de_count; i++) {
ScopedLocalRef<jstring> j_d_name(env,
(jstring)env->GetObjectArrayElement(files_list.get(), i));
ScopedUtfChars d_name(env, j_d_name.get());
if (d_name.c_str() == nullptr) {
LOG(ERROR) << "Error reading file name returned from MediaProvider at index " << i;
directory_entries.resize(0);
directory_entries.push_back(std::make_shared<DirectoryEntry>("", EFAULT));
break;
}
directory_entries.push_back(std::make_shared<DirectoryEntry>(d_name.c_str(), DT_REG));
}
return directory_entries;
}
int renameInternal(JNIEnv* env, jobject media_provider_object, jmethodID mid_rename,
const string& old_path, const string& new_path, uid_t uid) {
ScopedLocalRef<jstring> j_old_path(env, env->NewStringUTF(old_path.c_str()));
ScopedLocalRef<jstring> j_new_path(env, env->NewStringUTF(new_path.c_str()));
int res = env->CallIntMethod(media_provider_object, mid_rename, j_old_path.get(),
j_new_path.get(), uid);
if (CheckForJniException(env)) {
return EFAULT;
}
return res;
}
void onFileCreatedInternal(JNIEnv* env, jobject media_provider_object,
jmethodID mid_on_file_created, const string& path) {
ScopedLocalRef<jstring> j_path(env, env->NewStringUTF(path.c_str()));
env->CallVoidMethod(media_provider_object, mid_on_file_created, j_path.get());
CheckForJniException(env);
return;
}
} // namespace
/*****************************************************************************************/
/******************************* Public API Implementation *******************************/
/*****************************************************************************************/
JavaVM* MediaProviderWrapper::gJavaVm = nullptr;
pthread_key_t MediaProviderWrapper::gJniEnvKey;
void MediaProviderWrapper::OneTimeInit(JavaVM* vm) {
gJavaVm = vm;
CHECK(gJavaVm != nullptr);
pthread_key_create(&MediaProviderWrapper::gJniEnvKey,
MediaProviderWrapper::DetachThreadFunction);
}
MediaProviderWrapper::MediaProviderWrapper(JNIEnv* env, jobject media_provider) {
if (!media_provider) {
LOG(FATAL) << "MediaProvider is null!";
}
media_provider_object_ = reinterpret_cast<jobject>(env->NewGlobalRef(media_provider));
media_provider_class_ = env->FindClass("com/android/providers/media/MediaProvider");
if (!media_provider_class_) {
LOG(FATAL) << "Could not find class MediaProvider";
}
media_provider_class_ = reinterpret_cast<jclass>(env->NewGlobalRef(media_provider_class_));
// Cache methods - Before calling a method, make sure you cache it here
mid_get_redaction_ranges_ = CacheMethod(env, "getRedactionRanges", "(Ljava/lang/String;II)[J",
/*is_static*/ false);
mid_insert_file_ = CacheMethod(env, "insertFileIfNecessary", "(Ljava/lang/String;I)I",
/*is_static*/ false);
mid_delete_file_ = CacheMethod(env, "deleteFile", "(Ljava/lang/String;I)I", /*is_static*/ false);
mid_is_open_allowed_ = CacheMethod(env, "isOpenAllowed", "(Ljava/lang/String;IZ)I",
/*is_static*/ false);
mid_scan_file_ = CacheMethod(env, "scanFile", "(Ljava/lang/String;)V",
/*is_static*/ false);
mid_is_mkdir_or_rmdir_allowed_ = CacheMethod(env, "isDirectoryCreationOrDeletionAllowed",
"(Ljava/lang/String;IZ)I", /*is_static*/ false);
mid_is_opendir_allowed_ = CacheMethod(env, "isOpendirAllowed", "(Ljava/lang/String;IZ)I",
/*is_static*/ false);
mid_get_files_in_dir_ =
CacheMethod(env, "getFilesInDirectory", "(Ljava/lang/String;I)[Ljava/lang/String;",
/*is_static*/ false);
mid_rename_ = CacheMethod(env, "rename", "(Ljava/lang/String;Ljava/lang/String;I)I",
/*is_static*/ false);
mid_is_uid_for_package_ = CacheMethod(env, "isUidForPackage", "(Ljava/lang/String;I)Z",
/*is_static*/ false);
mid_on_file_created_ = CacheMethod(env, "onFileCreated", "(Ljava/lang/String;)V",
/*is_static*/ false);
}
MediaProviderWrapper::~MediaProviderWrapper() {
JNIEnv* env = MaybeAttachCurrentThread();
env->DeleteGlobalRef(media_provider_object_);
env->DeleteGlobalRef(media_provider_class_);
}
std::unique_ptr<RedactionInfo> MediaProviderWrapper::GetRedactionInfo(const string& path, uid_t uid,
pid_t tid) {
if (shouldBypassMediaProvider(uid) || !GetBoolProperty(kPropRedactionEnabled, true)) {
return std::make_unique<RedactionInfo>();
}
// Default value in case JNI thread was being terminated, causes the read to fail.
std::unique_ptr<RedactionInfo> res = nullptr;
JNIEnv* env = MaybeAttachCurrentThread();
auto ri = getRedactionInfoInternal(env, media_provider_object_, mid_get_redaction_ranges_, uid,
tid, path);
res = std::move(ri);
return res;
}
int MediaProviderWrapper::InsertFile(const string& path, uid_t uid) {
if (uid == ROOT_UID) {
return 0;
}
JNIEnv* env = MaybeAttachCurrentThread();
return insertFileInternal(env, media_provider_object_, mid_insert_file_, path, uid);
}
int MediaProviderWrapper::DeleteFile(const string& path, uid_t uid) {
if (uid == ROOT_UID) {
int res = unlink(path.c_str());
return res;
}
JNIEnv* env = MaybeAttachCurrentThread();
return deleteFileInternal(env, media_provider_object_, mid_delete_file_, path, uid);
}
int MediaProviderWrapper::IsOpenAllowed(const string& path, uid_t uid, bool for_write) {
if (shouldBypassMediaProvider(uid)) {
return 0;
}
JNIEnv* env = MaybeAttachCurrentThread();
return isOpenAllowedInternal(env, media_provider_object_, mid_is_open_allowed_, path, uid,
for_write);
}
void MediaProviderWrapper::ScanFile(const string& path) {
JNIEnv* env = MaybeAttachCurrentThread();
scanFileInternal(env, media_provider_object_, mid_scan_file_, path);
}
int MediaProviderWrapper::IsCreatingDirAllowed(const string& path, uid_t uid) {
if (shouldBypassMediaProvider(uid)) {
return 0;
}
JNIEnv* env = MaybeAttachCurrentThread();
return isMkdirOrRmdirAllowedInternal(env, media_provider_object_,
mid_is_mkdir_or_rmdir_allowed_, path, uid,
/*forCreate*/ true);
}
int MediaProviderWrapper::IsDeletingDirAllowed(const string& path, uid_t uid) {
if (shouldBypassMediaProvider(uid)) {
return 0;
}
JNIEnv* env = MaybeAttachCurrentThread();
return isMkdirOrRmdirAllowedInternal(env, media_provider_object_,
mid_is_mkdir_or_rmdir_allowed_, path, uid,
/*forCreate*/ false);
}
std::vector<std::shared_ptr<DirectoryEntry>> MediaProviderWrapper::GetDirectoryEntries(
uid_t uid, const string& path, DIR* dirp) {
// Default value in case JNI thread was being terminated
std::vector<std::shared_ptr<DirectoryEntry>> res;
if (shouldBypassMediaProvider(uid)) {
addDirectoryEntriesFromLowerFs(dirp, /* filter */ nullptr, &res);
return res;
}
JNIEnv* env = MaybeAttachCurrentThread();
res = getFilesInDirectoryInternal(env, media_provider_object_, mid_get_files_in_dir_, uid, path);
const int res_size = res.size();
if (res_size && res[0]->d_name[0] == '/') {
// Path is unknown to MediaProvider, get files and directories from lower file system.
res.resize(0);
addDirectoryEntriesFromLowerFs(dirp, /* filter */ nullptr, &res);
} else if (res_size == 0 || !res[0]->d_name.empty()) {
// add directory names from lower file system.
addDirectoryEntriesFromLowerFs(dirp, /* filter */ &isDirectory, &res);
}
return res;
}
int MediaProviderWrapper::IsOpendirAllowed(const string& path, uid_t uid, bool forWrite) {
if (shouldBypassMediaProvider(uid)) {
return 0;
}
JNIEnv* env = MaybeAttachCurrentThread();
return isOpendirAllowedInternal(env, media_provider_object_, mid_is_opendir_allowed_, path, uid,
forWrite);
}
bool MediaProviderWrapper::IsUidForPackage(const string& pkg, uid_t uid) {
if (shouldBypassMediaProvider(uid)) {
return true;
}
JNIEnv* env = MaybeAttachCurrentThread();
return isUidForPackageInternal(env, media_provider_object_, mid_is_uid_for_package_, pkg, uid);
}
int MediaProviderWrapper::Rename(const string& old_path, const string& new_path, uid_t uid) {
// Rename from SHELL_UID should go through MediaProvider to update database rows, so only bypass
// MediaProvider for ROOT_UID.
if (uid == ROOT_UID) {
int res = rename(old_path.c_str(), new_path.c_str());
if (res != 0) res = -errno;
return res;
}
JNIEnv* env = MaybeAttachCurrentThread();
return renameInternal(env, media_provider_object_, mid_rename_, old_path, new_path, uid);
}
void MediaProviderWrapper::OnFileCreated(const string& path) {
JNIEnv* env = MaybeAttachCurrentThread();
return onFileCreatedInternal(env, media_provider_object_, mid_on_file_created_, path);
}
/*****************************************************************************************/
/******************************** Private member functions *******************************/
/*****************************************************************************************/
/**
* Finds MediaProvider method and adds it to methods map so it can be quickly called later.
*/
jmethodID MediaProviderWrapper::CacheMethod(JNIEnv* env, const char method_name[],
const char signature[], bool is_static) {
jmethodID mid;
string actual_method_name(method_name);
actual_method_name.append("ForFuse");
if (is_static) {
mid = env->GetStaticMethodID(media_provider_class_, actual_method_name.c_str(), signature);
} else {
mid = env->GetMethodID(media_provider_class_, actual_method_name.c_str(), signature);
}
if (!mid) {
// SHOULD NOT HAPPEN!
LOG(FATAL) << "Error caching method: " << method_name << signature;
}
return mid;
}
void MediaProviderWrapper::DetachThreadFunction(void* unused) {
int detach = gJavaVm->DetachCurrentThread();
CHECK_EQ(detach, 0);
}
JNIEnv* MediaProviderWrapper::MaybeAttachCurrentThread() {
// We could use pthread_getspecific here as that's likely quicker but
// that would result in wrong behaviour for threads that don't need to
// be attached (e.g, those that were created in managed code).
JNIEnv* env = nullptr;
if (gJavaVm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_4) == JNI_OK) {
return env;
}
// This thread is currently unattached, so it must not have any TLS
// value. Note that we don't really care about the actual value we store
// in TLS -- we only care about the value destructor being called, which
// will happen as long as the key is not null.
CHECK(pthread_getspecific(gJniEnvKey) == nullptr);
CHECK_EQ(gJavaVm->AttachCurrentThread(&env, nullptr), 0);
CHECK(env != nullptr);
pthread_setspecific(gJniEnvKey, env);
return env;
}
} // namespace fuse
} // namespace mediaprovider
|