Skip to content

Commit 6a979cd

Browse files
committed
Support FBE. Android cannot boot this way though.
1 parent 2abe345 commit 6a979cd

File tree

11 files changed

+281
-90
lines changed

11 files changed

+281
-90
lines changed

install_zip/Android.mk

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ ifeq ($(MR_ENCRYPTION),true)
1717
multirom_extra_dep += trampoline_encmnt linker
1818

1919
multirom_cp_enc_libs := \
20-
libcryptfslollipop.so libcrypto.so libc.so libcutils.so \
20+
libcryptfslollipop.so libcrypto.so libext4_utils.so libsparse.so libext2_uuid.so libe4crypt.so libc.so libcutils.so \
2121
libdl.so libhardware.so liblog.so libm.so libstdc++.so \
22-
libc++.so
22+
libc++.so libwifikeystorehal.so libsoftkeymasterdevice.so android.system.wifi.keystore@1.0.so android.hardware.weaver@1.0.so
2323

2424
ifeq ($(TARGET_HW_DISK_ENCRYPTION),true)
2525
multirom_cp_enc_libs += \

lib/util.c

+2
Original file line numberDiff line numberDiff line change
@@ -432,11 +432,13 @@ void clone_dir(DIR* d, char* dirpath, char* target, bool preserve_context, char*
432432
copy_dir_contents(dir, in, out, exclude_dir);
433433
continue;
434434
} else if (dp->d_type == DT_LNK) {
435+
ERROR("copying link %s to %s\n", in, out);
435436
char target[256];
436437
readlink(in, target, sizeof(target));
437438
symlink(target, out);
438439
setattr(out, &a);
439440
} else if (dp->d_type == DT_REG) {
441+
ERROR("copying file %s to %s\n", in, out);
440442
if (preserve_context) {
441443
char* context = calloc(1, 50);
442444
getfilecon(in, &context);

trampoline/Android.mk

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ LOCAL_MODULE_TAGS := optional
1212

1313
LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT)
1414
LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_UNSTRIPPED)
15-
LOCAL_STATIC_LIBRARIES := libcutils libc libmultirom_static libbootimg libselinux
15+
LOCAL_STATIC_LIBRARIES := libcutils libc libmultirom_static libbootimg libselinux libext4_utils libkeyutils libbase liblog
1616
LOCAL_C_INCLUDES += system/extras/libbootimg/include
1717
LOCAL_FORCE_STATIC_EXECUTABLE := true
1818

trampoline/encryption.c

+54-34
Original file line numberDiff line numberDiff line change
@@ -32,16 +32,18 @@
3232
#include "../trampoline_encmnt/encmnt_defines.h"
3333
#include "../hooks.h"
3434

35+
extern int e4crypt_install_keyring();
36+
3537
static char encmnt_cmd_arg[64] = { 0 };
3638
static char *const encmnt_cmd[] = { "/mrom_enc/trampoline_encmnt", encmnt_cmd_arg, NULL };
3739
#ifdef MR_ENCRYPTION_FAKE_PROPERTIES
38-
static char *const encmnt_envp[] = { "LD_LIBRARY_PATH=/mrom_enc/", "LD_PRELOAD=/mrom_enc/libmultirom_fake_properties.so /mrom_enc/libmultirom_fake_propertywait.so", NULL };
40+
static char *const encmnt_envp[] = { "LD_CONFIG_FILE='/mrom_enc/ld.config.txt'", "LD_LIBRARY_PATH=/mrom_enc/", "LD_PRELOAD=/mrom_enc/libmultirom_fake_properties.so /mrom_enc/libmultirom_fake_propertywait.so", NULL };
3941
#else
40-
static char *const encmnt_envp[] = { "LD_LIBRARY_PATH=/mrom_enc/", NULL };
42+
static char *const encmnt_envp[] = { "LD_CONFIG_FILE='/mrom_enc/ld.config.txt'", "LD_LIBRARY_PATH=/mrom_enc/", NULL };
4143
#endif
4244
static int g_decrypted = 0;
4345

44-
int encryption_before_mount(struct fstab *fstab)
46+
int encryption_before_mount(struct fstab *fstab, bool isFbe)
4547
{
4648
int exit_code = -1;
4749
char *output = NULL, *itr;
@@ -80,52 +82,70 @@ int encryption_before_mount(struct fstab *fstab)
8082

8183
INFO("Running trampoline_encmnt\n");
8284

83-
strcpy(encmnt_cmd_arg, "decrypt");
85+
if (isFbe) {
86+
//rename("/realdata", "/data");
87+
int err = mount("/realdata", "/data", NULL, MS_MOVE, NULL);
88+
INFO("err %d %s\n", err, strerror(errno));
89+
int ret = e4crypt_install_keyring();
90+
strcpy(encmnt_cmd_arg, "decryptfbe");
91+
} else {
92+
strcpy(encmnt_cmd_arg, "decrypt");
93+
}
8494
output = run_get_stdout_with_exit_with_env(encmnt_cmd, &exit_code, encmnt_envp);
85-
if(exit_code != 0 || !output)
95+
if(exit_code != 0 || (!isFbe && !output))
8696
{
8797
ERROR("Failed to run trampoline_encmnt, exit code %d: %s\n", exit_code, output);
8898
goto exit;
8999
}
90100

91-
itr = output + strlen(output) - 1;
92-
while(itr >= output && isspace(*itr))
93-
*itr-- = 0;
94-
95-
if(strcmp(output, ENCMNT_BOOT_INTERNAL_OUTPUT) == 0)
96-
{
97-
INFO("trampoline_encmnt requested to boot internal ROM.\n");
98-
res = ENC_RES_BOOT_INTERNAL;
99-
goto exit;
101+
if (!isFbe || exit_code != 0) {
102+
itr = output + strlen(output) - 1;
103+
while(itr >= output && isspace(*itr))
104+
*itr-- = 0;
105+
106+
if(strcmp(output, ENCMNT_BOOT_INTERNAL_OUTPUT) == 0)
107+
{
108+
INFO("trampoline_encmnt requested to boot internal ROM.\n");
109+
res = ENC_RES_BOOT_INTERNAL;
110+
goto exit;
111+
}
112+
113+
if(strcmp(output, ENCMNT_BOOT_RECOVERY_OUTPUT) == 0)
114+
{
115+
INFO("trampoline_encmnt requested to boot recovery.\n");
116+
res = ENC_RES_BOOT_RECOVERY;
117+
goto exit;
118+
}
100119
}
101120

102-
if(strcmp(output, ENCMNT_BOOT_RECOVERY_OUTPUT) == 0)
103-
{
104-
INFO("trampoline_encmnt requested to boot recovery.\n");
105-
res = ENC_RES_BOOT_RECOVERY;
106-
goto exit;
107-
}
121+
if (!isFbe) {
108122

109-
if(!strstartswith(output, "/dev"))
110-
{
111-
ERROR("Invalid trampoline_encmnt output: %s\n", output);
112-
goto exit;
113-
}
123+
if(!strstartswith(output, "/dev"))
124+
{
125+
ERROR("Invalid trampoline_encmnt output: %s\n", output);
126+
goto exit;
127+
}
114128

115-
g_decrypted = 1;
129+
g_decrypted = 1;
116130

117-
struct fstab_part *datap = fstab_find_first_by_path(fstab, "/data");
118-
if(!datap)
119-
{
120-
ERROR("Failed to find /data in fstab!\n");
121-
goto exit;
122-
}
131+
struct fstab_part *datap = fstab_find_first_by_path(fstab, "/data");
132+
if(!datap)
133+
{
134+
ERROR("Failed to find /data in fstab!\n");
135+
goto exit;
136+
}
123137

124-
INFO("Updating device %s to %s in fstab due to encryption.\n", datap->device, output);
125-
fstab_update_device(fstab, datap->device, output);
138+
INFO("Updating device %s to %s in fstab due to encryption.\n", datap->device, output);
139+
fstab_update_device(fstab, datap->device, output);
140+
}
126141

127142
res = ENC_RES_OK;
128143
exit:
144+
if (isFbe) {
145+
//rename("/data", "/realdata");
146+
mount("/data", "/realdata", NULL, MS_MOVE, NULL);
147+
mkdir("/data", 0755);
148+
}
129149
free(output);
130150
return res;
131151
}

trampoline/encryption.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,11 @@
2424
#define ENC_RES_BOOT_RECOVERY 2
2525

2626
#ifdef MR_ENCRYPTION
27-
int encryption_before_mount(struct fstab *fstab);
27+
int encryption_before_mount(struct fstab *fstab, bool isFbe);
2828
void encryption_destroy(void);
2929
int encryption_cleanup(void);
3030
#else
31-
int encryption_before_mount(struct fstab *fstab) { return ENC_RES_OK; }
31+
int encryption_before_mount(struct fstab *fstab, bool isFbe) { return ENC_RES_OK; }
3232
void encryption_destroy(void) { }
3333
int encryption_cleanup(void) { return 0; }
3434
#endif

trampoline/trampoline.cpp

+24-6
Original file line numberDiff line numberDiff line change
@@ -241,8 +241,15 @@ static int try_mount_all_entries(struct fstab *fstab, struct fstab_part *first_d
241241
// su binaries on /data
242242
p_itr->mountflags &= ~(MS_NOSUID);
243243

244-
if(mount(p_itr->device, REALDATA, p_itr->type, p_itr->mountflags, p_itr->options) >= 0)
245-
return 0;
244+
if(mount(p_itr->device, REALDATA, p_itr->type, p_itr->mountflags, p_itr->options) >= 0) {
245+
struct stat info;
246+
if(stat("/realdata/unencrypted/key/version", &info) < 0) {
247+
return 0;
248+
} else {
249+
INFO("File system is FBE encrypted");
250+
return -2;
251+
}
252+
}
246253
}
247254
while((p_itr = fstab_find_next_by_path(fstab, "/data", p_itr)));
248255

@@ -261,6 +268,13 @@ static int try_mount_all_entries(struct fstab *fstab, struct fstab_part *first_d
261268
if(mount(first_data_p->device, REALDATA, fs_types[i], first_data_p->mountflags, fs_opts[i]) >= 0)
262269
{
263270
INFO("/realdata successfuly mounted with fs %s\n", fs_types[i]);
271+
struct stat info;
272+
if(stat("/realdata/unencrypted/key/version", &info) < 0) {
273+
return 0;
274+
} else {
275+
INFO("File system is FBE encrypted\n");
276+
return -2;
277+
}
264278
return 0;
265279
}
266280
}
@@ -290,14 +304,15 @@ static int mount_and_run(struct fstab *fstab)
290304
mkdir(REALDATA, 0755);
291305
mkdir("/data", 0755);
292306

293-
if(try_mount_all_entries(fstab, datap) < 0)
307+
int ret = try_mount_all_entries(fstab, datap);
308+
if(ret < 0)
294309
{
295310
#ifndef MR_ENCRYPTION
296311
ERROR("Failed to mount /data with all possible filesystems!\n");
297312
return -1;
298313
#else
299-
INFO("Failed to mount /data, trying encryption...\n");
300-
switch(encryption_before_mount(fstab))
314+
INFO("Failed to mount /realdata, trying encryption...\n");
315+
switch(encryption_before_mount(fstab, ret == -2))
301316
{
302317
case ENC_RES_ERR:
303318
ERROR("/data decryption failed!\n");
@@ -314,7 +329,7 @@ static int mount_and_run(struct fstab *fstab)
314329
default:
315330
case ENC_RES_OK:
316331
{
317-
if(try_mount_all_entries(fstab, datap) < 0)
332+
if(ret != -2 && try_mount_all_entries(fstab, datap) < 0)
318333
{
319334
ERROR("Failed to mount decrypted /data with all possible filesystems!\n");
320335
return -1;
@@ -635,6 +650,7 @@ int main(int argc, char *argv[])
635650
mkdir("/dev/socket", 0755);
636651
mkdir("/proc", 0755);
637652
mkdir("/sys", 0755);
653+
mkdir("/tmp", 0755);
638654

639655
mount("tmpfs", "/dev", "tmpfs", MS_NOSUID, "mode=0755");
640656
mount("devpts", "/dev/pts", "devpts", 0, NULL);
@@ -691,6 +707,8 @@ int main(int argc, char *argv[])
691707
if (access("/fakefstab/", F_OK)) {
692708
DIR* dir = opendir("/proc/device-tree/firmware/android");
693709
copy_dir_contents(dir, "/proc/device-tree/firmware/android", "/fakefstab", NULL);
710+
//remove("/fakefstab/fstab/system/mnt_point");
711+
rmdir("/fakefstab/fstab/vendor");
694712
}
695713
umount("/proc");
696714
umount("/sys/fs/pstore");

trampoline_encmnt/Android.mk

+6-4
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ LOCAL_MODULE:= trampoline_encmnt
55
LOCAL_MODULE_TAGS := optional
66
LOCAL_MODULE_PATH := $(TARGET_ROOT_OUT)
77
LOCAL_UNSTRIPPED_PATH := $(TARGET_ROOT_OUT_UNSTRIPPED)
8-
LOCAL_SHARED_LIBRARIES := libcryptfslollipop libcutils
9-
LOCAL_STATIC_LIBRARIES := libmultirom_static
8+
LOCAL_SHARED_LIBRARIES := libcryptfslollipop libcutils libe4crypt
9+
LOCAL_STATIC_LIBRARIES := libmultirom_static libext4_utils
1010

1111
LOCAL_ADDITIONAL_DEPENDENCIES += libstdc++
1212

@@ -29,8 +29,9 @@ else
2929
$(error Failed to find path to TWRP, which is required to build MultiROM with encryption support)
3030
endif
3131

32-
LOCAL_C_INCLUDES += $(multirom_local_path) $(mr_twrp_path) $(mr_twrp_path)/crypto/scrypt/lib/crypto external/openssl/include external/boringssl/include
32+
LOCAL_C_INCLUDES += $(multirom_local_path) $(mr_twrp_path) $(mr_twrp_path)/crypto/scrypt/lib/crypto $(mr_twrp_path)/crypto/ext4crypt external/openssl/include external/boringssl/include
3333
LOCAL_C_INCLUDES += system/extras/libbootimg/include
34+
LOCAL_C_INCLUDES += system/extras/ext4_utils/include/ext4_utils
3435

3536
LOCAL_SRC_FILES := \
3637
encmnt.cpp \
@@ -49,8 +50,9 @@ ifeq ($(MR_ENCRYPTION_FAKE_PROPERTIES),true)
4950
LOCAL_MODULE := libmultirom_fake_properties
5051
LOCAL_MODULE_TAGS := optional
5152
LOCAL_C_INCLUDES += $(multirom_local_path)
53+
LOCAL_C_INCLUDES += system/extras/libbootimg/include
5254

53-
LOCAL_SRC_FILES := fake_properties.c
55+
LOCAL_SRC_FILES := fake_properties.c klog.c
5456
LOCAL_SHARED_LIBRARIES := liblog
5557

5658
ifneq ($(MR_ENCRYPTION_FAKE_PROPERTIES_EXTRAS),)

0 commit comments

Comments
 (0)