Skip to content

Commit 30afda7

Browse files
committed
Copying debug ramdisk files to /debug_ramdisk/*
In previous implementation, userdebug sepoilcy and property files are loaded from the system.img. This CL changes this to: - first-stage init copies userdebug files from ramdisk to /debug_ramisk/* - second-stage init loads files from /debug_ramdisk/*. Note: same as before, the above can only be triggered, if the device is UNLOCKED With this, we don't have to put userdebug related files into the USER system.img. Bug: 126493225 Test: boot device with a ramdisk with /force_debuggable, checks related files are loaded Change-Id: I63f5f846e82ba78427062bf7615c26173878d8f3
1 parent fc17492 commit 30afda7

File tree

8 files changed

+58
-9
lines changed

8 files changed

+58
-9
lines changed

init/Android.bp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ cc_defaults {
6161
static_libs: [
6262
"libseccomp_policy",
6363
"libavb",
64+
"libc++fs",
6465
"libcgrouprc_format",
6566
"libprotobuf-cpp-lite",
6667
"libpropertyinfoserializer",

init/Android.mk

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,14 @@ LOCAL_UNSTRIPPED_PATH := $(TARGET_RAMDISK_OUT_UNSTRIPPED)
6868
# Set up the same mount points on the ramdisk that system-as-root contains.
6969
LOCAL_POST_INSTALL_CMD := mkdir -p \
7070
$(TARGET_RAMDISK_OUT)/apex \
71+
$(TARGET_RAMDISK_OUT)/debug_ramdisk \
7172
$(TARGET_RAMDISK_OUT)/dev \
7273
$(TARGET_RAMDISK_OUT)/mnt \
7374
$(TARGET_RAMDISK_OUT)/proc \
7475
$(TARGET_RAMDISK_OUT)/sys \
7576

7677
LOCAL_STATIC_LIBRARIES := \
78+
libc++fs \
7779
libfs_avb \
7880
libfs_mgr \
7981
libfec \

init/debug_ramdisk.h

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/*
2+
* Copyright (C) 2019 The Android Open Source Project
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#pragma once
18+
19+
namespace android {
20+
namespace init {
21+
22+
constexpr const char kDebugRamdiskProp[] = "/debug_ramdisk/adb_debug.prop";
23+
constexpr const char kDebugRamdiskSEPolicy[] = "/debug_ramdisk/userdebug_plat_sepolicy.cil";
24+
25+
} // namespace init
26+
} // namespace android

init/first_stage_init.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <sys/types.h>
2727
#include <unistd.h>
2828

29+
#include <filesystem>
2930
#include <string>
3031
#include <vector>
3132

@@ -35,6 +36,7 @@
3536
#include <cutils/android_reboot.h>
3637
#include <private/android_filesystem_config.h>
3738

39+
#include "debug_ramdisk.h"
3840
#include "first_stage_mount.h"
3941
#include "reboot_utils.h"
4042
#include "switch_root.h"
@@ -44,6 +46,8 @@ using android::base::boot_clock;
4446

4547
using namespace std::literals;
4648

49+
namespace fs = std::filesystem;
50+
4751
namespace android {
4852
namespace init {
4953

@@ -159,6 +163,9 @@ int FirstStageMain(int argc, char** argv) {
159163
CHECKCALL(mount("tmpfs", "/apex", "tmpfs", MS_NOEXEC | MS_NOSUID | MS_NODEV,
160164
"mode=0755,uid=0,gid=0"));
161165

166+
// /debug_ramdisk is used to preserve additional files from the debug ramdisk
167+
CHECKCALL(mount("tmpfs", "/debug_ramdisk", "tmpfs", MS_NOEXEC | MS_NOSUID | MS_NODEV,
168+
"mode=0755,uid=0,gid=0"));
162169
#undef CHECKCALL
163170

164171
// Now that tmpfs is mounted on /dev and we have /dev/kmsg, we can actually
@@ -202,7 +209,14 @@ int FirstStageMain(int argc, char** argv) {
202209
// If this file is present, the second-stage init will use a userdebug sepolicy
203210
// and load adb_debug.prop to allow adb root, if the device is unlocked.
204211
if (access("/force_debuggable", F_OK) == 0) {
205-
setenv("INIT_FORCE_DEBUGGABLE", "true", 1);
212+
std::error_code ec; // to invoke the overloaded copy_file() that won't throw.
213+
if (!fs::copy_file("/adb_debug.prop", kDebugRamdiskProp, ec) ||
214+
!fs::copy_file("/userdebug_plat_sepolicy.cil", kDebugRamdiskSEPolicy, ec)) {
215+
LOG(ERROR) << "Failed to setup debug ramdisk";
216+
} else {
217+
// setenv for second-stage init to read above kDebugRamdisk* files.
218+
setenv("INIT_FORCE_DEBUGGABLE", "true", 1);
219+
}
206220
}
207221

208222
if (!DoFirstStageMount()) {

init/init.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -621,6 +621,12 @@ static void GlobalSeccomp() {
621621
});
622622
}
623623

624+
static void UmountDebugRamdisk() {
625+
if (umount("/debug_ramdisk") != 0) {
626+
LOG(ERROR) << "Failed to umount /debug_ramdisk";
627+
}
628+
}
629+
624630
int SecondStageMain(int argc, char** argv) {
625631
if (REBOOT_BOOTLOADER_ON_PANIC) {
626632
InstallRebootSignalHandlers();
@@ -685,6 +691,7 @@ int SecondStageMain(int argc, char** argv) {
685691
InstallSignalFdHandler(&epoll);
686692

687693
property_load_boot_defaults(load_debug_prop);
694+
UmountDebugRamdisk();
688695
fs_mgr_vendor_overlay_mount_all();
689696
export_oem_lock_status();
690697
StartPropertyService(&epoll);

init/property_service.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
#include <selinux/label.h>
5757
#include <selinux/selinux.h>
5858

59+
#include "debug_ramdisk.h"
5960
#include "epoll.h"
6061
#include "init.h"
6162
#include "persistent_properties.h"
@@ -887,9 +888,8 @@ void property_load_boot_defaults(bool load_debug_prop) {
887888
load_properties_from_file("/factory/factory.prop", "ro.*", &properties);
888889

889890
if (load_debug_prop) {
890-
constexpr static const char kAdbDebugProp[] = "/system/etc/adb_debug.prop";
891-
LOG(INFO) << "Loading " << kAdbDebugProp;
892-
load_properties_from_file(kAdbDebugProp, nullptr, &properties);
891+
LOG(INFO) << "Loading " << kDebugRamdiskProp;
892+
load_properties_from_file(kDebugRamdiskProp, nullptr, &properties);
893893
}
894894

895895
for (const auto& [name, value] : properties) {

init/selinux.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
#include <fs_avb/fs_avb.h>
6565
#include <selinux/android.h>
6666

67+
#include "debug_ramdisk.h"
6768
#include "reboot_utils.h"
6869
#include "util.h"
6970

@@ -271,8 +272,6 @@ bool GetVendorMappingVersion(std::string* plat_vers) {
271272
}
272273

273274
constexpr const char plat_policy_cil_file[] = "/system/etc/selinux/plat_sepolicy.cil";
274-
constexpr const char userdebug_plat_policy_cil_file[] =
275-
"/system/etc/selinux/userdebug_plat_sepolicy.cil";
276275

277276
bool IsSplitPolicyDevice() {
278277
return access(plat_policy_cil_file, R_OK) != -1;
@@ -292,7 +291,7 @@ bool LoadSplitPolicy() {
292291
const char* force_debuggable_env = getenv("INIT_FORCE_DEBUGGABLE");
293292
bool use_userdebug_policy =
294293
((force_debuggable_env && "true"s == force_debuggable_env) &&
295-
AvbHandle::IsDeviceUnlocked() && access(userdebug_plat_policy_cil_file, F_OK) == 0);
294+
AvbHandle::IsDeviceUnlocked() && access(kDebugRamdiskSEPolicy, F_OK) == 0);
296295
if (use_userdebug_policy) {
297296
LOG(WARNING) << "Using userdebug system sepolicy";
298297
}
@@ -367,7 +366,7 @@ bool LoadSplitPolicy() {
367366
// clang-format off
368367
std::vector<const char*> compile_args {
369368
"/system/bin/secilc",
370-
use_userdebug_policy ? userdebug_plat_policy_cil_file : plat_policy_cil_file,
369+
use_userdebug_policy ? kDebugRamdiskSEPolicy: plat_policy_cil_file,
371370
"-m", "-M", "true", "-G", "-N",
372371
"-c", version_as_string.c_str(),
373372
plat_mapping_file.c_str(),

rootdir/Android.mk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ endif
9797
#
9898
# create some directories (some are mount points) and symlinks
9999
LOCAL_POST_INSTALL_CMD := mkdir -p $(addprefix $(TARGET_ROOT_OUT)/, \
100-
dev proc sys system data odm oem acct config storage mnt apex $(BOARD_ROOT_EXTRA_FOLDERS)); \
100+
dev proc sys system data odm oem acct config storage mnt apex debug_ramdisk $(BOARD_ROOT_EXTRA_FOLDERS)); \
101101
ln -sf /system/bin $(TARGET_ROOT_OUT)/bin; \
102102
ln -sf /system/etc $(TARGET_ROOT_OUT)/etc; \
103103
ln -sf /data/user_de/0/com.android.shell/files/bugreports $(TARGET_ROOT_OUT)/bugreports; \

0 commit comments

Comments
 (0)