OH4.1Release运行HOS应用适配方案
涉及模块功能简介
XPM
通过扩展内核能力,为应用的二进制和abc代码提供运行时的管控,仅拥有合法签名的代码允许分配可执行权限、
CODE_SIGN
保证系统中被加载的代码是可信的。在设备使用过程中,系统通过代码签名特性。在应用启动过程中进行签名校验,如果app在经过代码签名后被篡改,则不允许被加载。
OH系统从4.0release切换至4.1release后无法运行debug应用问题分析
在 OpenHarmony 从 4.0 release 切换到 4.1 release 后,debug应用无法正常运行。
经分析定位,4.1release在 foundation/ability/ability_runtime/services/abilitymgr/src/ability_manager_service.cpp
文件下的 CheckDebugAppInDeveloperMode
函数中,新增了对常量 const.security.developermode.state
数值的判断。
constexpr char DEVELOPER_MODE_STATE[] = "const.security.developermode.state";
bool AbilityManagerService::CheckDebugAppInDeveloperMode(bool isDebugApp)
{
if (isDebugApp && !system::GetBoolParameter(DEVELOPER_MODE_STATE, false)) {
HILOG_ERROR("Debugging application cannot run in non developer mode.");
return false;
}
return true;
}
此变量由XPM 模块创建节点并赋值,赋值逻辑可见kernel/linux/common_modules/xpm/developer/dsmm_developer.c
static int get_developer_status(uint32_t *status)
{
if (!strstr(saved_command_line, "developer_mode=")) {
*status = CMDLINE_DEV_STATE_NA;
} else if (strstr(saved_command_line, "developer_mode=1")) {
*status = CMDLINE_DEV_STATE_ON;
} else if (strstr(saved_command_line, "developer_mode=0")) {
*status = CMDLINE_DEV_STATE_OFF;
} else {
xpm_log_error("invalid developer_mode value in cmdline");
return -EINVAL;
}
return 0;
}
static int get_build_variant(uint32_t *variant)
{
if (strstr(saved_command_line, "buildvariant=user")) {
*variant = BUILD_VARIANT_USER;
} else if (strstr(saved_command_line, "buildvariant=eng")) {
*variant = BUILD_VARIANT_ENG;
} else {
xpm_log_error("invalid buildvariant value in cmdline");
return -EINVAL;
}
return 0;
}
由代码可见,XPM模块会根据cmdline中的buildvariant
以及developer_mode
取值情况对 const.security.developermode.state
进行初始化。取值关系可见同文件中的g_state_table
结构体。
static uint32_t g_state_table[BUILD_VARIANT_MAX][CMDLINE_DEV_STATE_MAX] = {
{ STATE_OFF, STATE_ON, STATE_OFF },
{ STATE_ON, STATE_ON, STATE_ON },
};
int get_developer_mode_state(void)
{
uint32_t variant, status;
if (developer_state != STATE_UNINT)
return developer_state;
#ifdef CONFIG_DSMM_DEVELOPER_ENABLE
if (get_build_variant(&variant) || get_developer_status(&status)) {
xpm_log_error("get build variant or developer status failed");
developer_state = STATE_OFF;
} else {
developer_state = g_state_table[variant][status];
}
#else
developer_state = STATE_ON;
#endif
return developer_state;
}
HOS应用安装依赖
HOS应用在安装时,由于本身携带有签名特性,会在安装时被CODE_SIGN模块用户态代码(在开发者手机中,此部分代码是指向社区仓库,因此会存在该代码逻辑)进行签名检测并拦截,未合入XPM与CODE_SIGN的情况下,安装必然失败。目前并未验证删去CODE_SIGN用户态代码后,是否仍会安装失败。
模块移植
目前可依照以下几个pr进行代码合入:
https://gitee.com/openharmony/kernel_linux_5.10/pulls/770
https://gitee.com/openharmony/kernel_linux_5.10/pulls/1084
https://gitee.com/openharmony/kernel_linux_5.10/pulls/1119
https://gitee.com/openharmony/kernel_linux_5.10/pulls/1131
https://gitee.com/openharmony/kernel_linux_5.10/pulls/1141
https://gitee.com/openharmony/kernel_linux_5.10/commit/539328c4934d0d8385b16765093669c9faad7be0
此外,还需合入kernel/linux/common_modules路径下的code_sign以及xpm文件夹。
整体合入完毕后,在设备对应的defconfig文件中打开以下几个特性开关:
CONFIG_FS_VERITY
CONFIG_SECURITY_CODE_SIGN
CONFIG_CRYPTO_ECDSA
CONFIG_SECURITY_XPM
CONFIG_SECURITY_XPM_DEBUG
CONFIG_SECURITY_SELINUX
移植完整性验证
目前有两种方案可验证code_sign功能是否移植完成:
1、在用户态CODE_SIGN代码仓下,存在code_sign相关测试用例,可借助tdd测试框架执行。
2、通过deveco,添加签名信息后生成简单应用demo,通过hdc install 指令安装生成的hap包。若移植成功,则会在桌面上生成相应的应用图标,且能够正常运行。移植失败则会导致死机重启。
更多推荐
所有评论(0)