From 3d01c25c586ef3fac9e8bc64122de779992e56b9 Mon Sep 17 00:00:00 2001 From: zhangqilong Date: Fri, 29 Jul 2022 17:52:01 +0800 Subject: [PATCH] fscrypt:fix init unittest compile failed Signed-off-by: zhangqilong Change-Id: I1d4def8c08dd4f54ae48191340c4c36ba319b8a8 --- interfaces/innerkits/fs_manager/fstab.c | 50 ++++++++++++------- .../innerkits/include/fs_manager/fs_manager.h | 2 +- services/init/standard/init_cmds.c | 13 ++--- test/unittest/BUILD.gn | 9 +++- 4 files changed, 45 insertions(+), 29 deletions(-) diff --git a/interfaces/innerkits/fs_manager/fstab.c b/interfaces/innerkits/fs_manager/fstab.c index 67b4c4f4..a35878f3 100644 --- a/interfaces/innerkits/fs_manager/fstab.c +++ b/interfaces/innerkits/fs_manager/fstab.c @@ -42,11 +42,7 @@ struct MountFlags { unsigned long flags; }; -#define POLICY_BUFFER (100) - -static const char *g_fscryptPre = "fscrypt="; -static const char *g_mountPoint = "/data"; -static char g_fscryptPolicy[POLICY_BUFFER] = { 0 }; +static char *g_fscryptPolicy = NULL; static unsigned int ConvertFlags(char *flagBuffer) { @@ -392,10 +388,12 @@ static unsigned long ParseDefaultMountFlag(const char *str) static bool IsFscryptOption(const char *option) { + BEGET_LOGI("IsFscryptOption start"); if (!option) { return false; } - if (strncmp(option, g_fscryptPre, strlen(g_fscryptPre)) == 0) { + char *fscryptPre = "fscrypt="; + if (strncmp(option, fscryptPre, strlen(fscryptPre)) == 0) { return true; } return false; @@ -403,23 +401,41 @@ static bool IsFscryptOption(const char *option) static void StoreFscryptPolicy(const char *option) { - if (!option) { + if (option == NULL) { return; } - if (strcpy_s(g_fscryptPolicy, POLICY_BUFFER - 1, option) != EOK) { - g_fscryptPolicy[0] = '\0'; - BEGET_LOGE("StoreFscryptPolicy: copy policy failed"); + if (g_fscryptPolicy != NULL) { + BEGET_LOGW("StoreFscryptPolicy:inited policy is not empty"); + free(g_fscryptPolicy); + } + g_fscryptPolicy = strdup(option); + if (g_fscryptPolicy == NULL) { + BEGET_LOGE("StoreFscryptPolicy:no memory"); return; } - BEGET_LOGI("StoreFscryptPolicy:load fscrypt policy, %s", option); + BEGET_LOGI("StoreFscryptPolicy:store fscrypt policy, %s", option); } -const char *LoadFscryptPolicy(void) +int LoadFscryptPolicy(char *buf, size_t size) { - if (strnlen(g_fscryptPolicy, POLICY_BUFFER - 1) == 0) { - return NULL; + BEGET_LOGI("LoadFscryptPolicy start"); + if (buf == NULL || g_fscryptPolicy == NULL) { + BEGET_LOGE("LoadFscryptPolicy:buf or fscrypt policy is empty"); + return -ENOMEM; } - return g_fscryptPolicy; + if (size <= 0) { + BEGET_LOGE("LoadFscryptPloicy:size is invalid"); + return -EINVAL; + } + if (strcpy_s(buf, size, g_fscryptPolicy) != 0) { + BEGET_LOGE("loadFscryptPolicy:strcmp failed, error = %d", errno); + return -EFAULT; + } + free(g_fscryptPolicy); + g_fscryptPolicy = NULL; + BEGET_LOGI("LoadFscryptPolicy success"); + + return 0; } unsigned long GetMountFlags(char *mountFlag, char *fsSpecificData, size_t fsSpecificDataSize, @@ -448,8 +464,8 @@ unsigned long GetMountFlags(char *mountFlag, char *fsSpecificData, size_t fsSpec flags |= ParseDefaultMountFlag(p); } else { if (IsFscryptOption(p) && - !strncmp(mountPoint, g_mountPoint, strlen(g_mountPoint))) { - StoreFscryptPolicy(p + strlen(g_fscryptPre)); + !strncmp(mountPoint, "/data", strlen("/data"))) { + StoreFscryptPolicy(p + strlen("fscrypt=")); continue; } if (strncat_s(fsSpecificData, fsSpecificDataSize - 1, p, strlen(p)) != EOK) { diff --git a/interfaces/innerkits/include/fs_manager/fs_manager.h b/interfaces/innerkits/include/fs_manager/fs_manager.h index cef85d45..225557a2 100644 --- a/interfaces/innerkits/include/fs_manager/fs_manager.h +++ b/interfaces/innerkits/include/fs_manager/fs_manager.h @@ -76,7 +76,7 @@ unsigned long GetMountFlags(char *mountFlag, char *fsSpecificFlags, size_t fsSpe int GetBlockDevicePath(const char *partName, char *path, int size); // Get fscrypt policy if exist -const char *LoadFscryptPolicy(void); +int LoadFscryptPolicy(char *buf, size_t size); #ifdef __cplusplus #if __cplusplus } diff --git a/services/init/standard/init_cmds.c b/services/init/standard/init_cmds.c index e2661868..54925800 100755 --- a/services/init/standard/init_cmds.c +++ b/services/init/standard/init_cmds.c @@ -46,6 +46,7 @@ #endif #include "fscrypt_utils.h" +#define FSCRYPT_POLICY_BUF_SIZE (60) int GetParamValue(const char *symValue, unsigned int symLen, char *paramValue, unsigned int paramLen) { @@ -414,8 +415,8 @@ static void DoTimerStop(const struct CmdArgs *ctx) static bool InitFscryptPolicy(void) { - const char *policy = LoadFscryptPolicy(); - if (!policy) { + char policy[FSCRYPT_POLICY_BUF_SIZE]; + if (LoadFscryptPolicy(policy, FSCRYPT_POLICY_BUF_SIZE) != 0) { return false; } if (SetFscryptSysparam(policy) == 0) { @@ -436,9 +437,8 @@ static void DoInitGlobalKey(const struct CmdArgs *ctx) INIT_LOGE("DoInitGlobalKey: not data partitation"); return; } - if (!InitFscryptPolicy()) { - INIT_LOGE("DoInitGlobalKey: init policy failed"); + INIT_LOGI("DoInitGlobalKey:init fscrypt failed,not enable fscrypt"); return; } @@ -460,10 +460,6 @@ static void DoInitMainUser(const struct CmdArgs *ctx) INIT_LOGE("DoInitMainUser: para invalid"); return; } - if (LoadFscryptPolicy() == NULL) { - INIT_LOGI("DoInitMainUser: file crypto is not enabled"); - return; - } char * const argv[] = { "/system/bin/sdc", @@ -604,7 +600,6 @@ void OpenHidebug(const char *name) int SetFileCryptPolicy(const char *dir) { - INIT_LOGI("SetFileCryptPolicy:start:%s", dir); if (dir == NULL) { INIT_LOGE("SetFileCryptPolicy:dir is null"); return -EINVAL; diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 929ac4d8..bb47492b 100755 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -108,6 +108,13 @@ ohos_unittest("init_unittest") { "//base/startup/init/ueventd/ueventd_socket.c", ] + sources += [ + "${FSCRYPT_PATH}/libfscrypt/src/fscrypt_control.c", + "${FSCRYPT_PATH}/libfscrypt/src/fscrypt_utils.c", + "${FSCRYPT_PATH}/libfscrypt/src/key_control.c", + "${FSCRYPT_PATH}/libfscrypt/src/sysparam_static.c", + ] + if (defined(build_selinux) && build_selinux) { sources += [ "//base/startup/init/services/param/adapter/param_selinux.c" ] } @@ -216,8 +223,6 @@ ohos_unittest("init_unittest") { "//utils/native/base:utils", ] - deps += [ "${FSCRYPT_PATH}/libfscrypt:libfscryptutils_static" ] - defines = [ "READ_CHECK", "INIT_AGENT", -- GitLab