169570cc8Sopenharmony_ci/* 269570cc8Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 369570cc8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 469570cc8Sopenharmony_ci * you may not use this file except in compliance with the License. 569570cc8Sopenharmony_ci * You may obtain a copy of the License at 669570cc8Sopenharmony_ci * 769570cc8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 869570cc8Sopenharmony_ci * 969570cc8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1069570cc8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1169570cc8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1269570cc8Sopenharmony_ci * See the License for the specific language governing permissions and 1369570cc8Sopenharmony_ci * limitations under the License. 1469570cc8Sopenharmony_ci */ 1569570cc8Sopenharmony_ci 1669570cc8Sopenharmony_ci#include "sandbox_adapter.h" 1769570cc8Sopenharmony_ci#include "init_utils.h" 1869570cc8Sopenharmony_ci 1969570cc8Sopenharmony_ci#ifdef WITH_SELINUX 2069570cc8Sopenharmony_ci#include "hap_restorecon.h" 2169570cc8Sopenharmony_ci#endif 2269570cc8Sopenharmony_ci 2369570cc8Sopenharmony_civoid MakeAtomicServiceDir(const SandboxContext *context, const char *path) 2469570cc8Sopenharmony_ci{ 2569570cc8Sopenharmony_ci APPSPAWN_CHECK_ONLY_EXPER(context != NULL && path != NULL, return); 2669570cc8Sopenharmony_ci if (access(path, F_OK) == 0) { 2769570cc8Sopenharmony_ci APPSPAWN_LOGV("path %{public}s already exist, no need to recreate", path); 2869570cc8Sopenharmony_ci return; 2969570cc8Sopenharmony_ci } 3069570cc8Sopenharmony_ci int ret = mkdir(path, S_IRWXU); 3169570cc8Sopenharmony_ci APPSPAWN_CHECK(ret == 0, return, "mkdir %{public}s failed, errno %{public}d", path, errno); 3269570cc8Sopenharmony_ci 3369570cc8Sopenharmony_ci if (strstr(path, "/database") != NULL) { 3469570cc8Sopenharmony_ci ret = chmod(path, S_IRWXU | S_IRWXG | S_ISGID); 3569570cc8Sopenharmony_ci } else if (strstr(path, "/log") != NULL) { 3669570cc8Sopenharmony_ci ret = chmod(path, S_IRWXU | S_IRWXG); 3769570cc8Sopenharmony_ci } 3869570cc8Sopenharmony_ci APPSPAWN_CHECK(ret == 0, return, "chmod %{public}s failed, errno %{public}d", path, errno); 3969570cc8Sopenharmony_ci 4069570cc8Sopenharmony_ci#ifdef WITH_SELINUX 4169570cc8Sopenharmony_ci AppSpawnMsgDomainInfo *msgDomainInfo = (AppSpawnMsgDomainInfo *)GetSpawningMsgInfo(context, TLV_DOMAIN_INFO); 4269570cc8Sopenharmony_ci APPSPAWN_CHECK(msgDomainInfo != NULL, return, "No domain info for %{public}s", context->bundleName); 4369570cc8Sopenharmony_ci HapContext hapContext; 4469570cc8Sopenharmony_ci HapFileInfo hapFileInfo; 4569570cc8Sopenharmony_ci hapFileInfo.pathNameOrig.push_back(path); 4669570cc8Sopenharmony_ci hapFileInfo.apl = msgDomainInfo->apl; 4769570cc8Sopenharmony_ci hapFileInfo.packageName = context->bundleName; 4869570cc8Sopenharmony_ci hapFileInfo.hapFlags = msgDomainInfo->hapFlags; 4969570cc8Sopenharmony_ci if (CheckAppSpawnMsgFlag(context->message, TLV_MSG_FLAGS, APP_FLAGS_DEBUGGABLE)) { 5069570cc8Sopenharmony_ci hapFileInfo.hapFlags |= SELINUX_HAP_DEBUGGABLE; 5169570cc8Sopenharmony_ci } 5269570cc8Sopenharmony_ci if ((strstr(path, "/base") != NULL) || (strstr(path, "/database") != NULL)) { 5369570cc8Sopenharmony_ci ret = hapContext.HapFileRestorecon(hapFileInfo); 5469570cc8Sopenharmony_ci APPSPAWN_CHECK(ret == 0, return, "set dir %{public}s selinuxLabel failed, apl %{public}s, ret %{public}d", 5569570cc8Sopenharmony_ci path, hapFileInfo.apl.c_str(), ret); 5669570cc8Sopenharmony_ci } 5769570cc8Sopenharmony_ci#endif 5869570cc8Sopenharmony_ci AppSpawnMsgDacInfo *dacInfo = (AppSpawnMsgDacInfo *)GetSpawningMsgInfo(context, TLV_DAC_INFO); 5969570cc8Sopenharmony_ci APPSPAWN_CHECK(dacInfo != NULL, return, "No dac info for %{public}s", context->bundleName); 6069570cc8Sopenharmony_ci if (strstr(path, "/base") != NULL) { 6169570cc8Sopenharmony_ci ret = chown(path, dacInfo->uid, dacInfo->gid); 6269570cc8Sopenharmony_ci } else if (strstr(path, "/database") != NULL) { 6369570cc8Sopenharmony_ci ret = chown(path, dacInfo->uid, DecodeGid("ddms")); 6469570cc8Sopenharmony_ci } else if (strstr(path, "/log") != NULL) { 6569570cc8Sopenharmony_ci ret = chown(path, dacInfo->uid, DecodeGid("log")); 6669570cc8Sopenharmony_ci } 6769570cc8Sopenharmony_ci APPSPAWN_CHECK(ret == 0, return, "chown %{public}s failed, errno %{public}d", path, errno); 6869570cc8Sopenharmony_ci} 69