169570cc8Sopenharmony_ci/* 269570cc8Sopenharmony_ci * Copyright (c) 2021-2022 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 <errno.h> 1769570cc8Sopenharmony_ci#include <sys/prctl.h> 1869570cc8Sopenharmony_ci#include <sys/stat.h> 1969570cc8Sopenharmony_ci#include <sys/types.h> 2069570cc8Sopenharmony_ci#include <unistd.h> 2169570cc8Sopenharmony_ci 2269570cc8Sopenharmony_ci#ifdef __LINUX__ 2369570cc8Sopenharmony_ci#include <linux/capability.h> 2469570cc8Sopenharmony_ci#include <linux/securebits.h> 2569570cc8Sopenharmony_ci#include <sys/resource.h> 2669570cc8Sopenharmony_ci#include <sys/time.h> 2769570cc8Sopenharmony_ci#else 2869570cc8Sopenharmony_ci#include <sys/capability.h> 2969570cc8Sopenharmony_ci#endif // __LINUX__ 3069570cc8Sopenharmony_ci 3169570cc8Sopenharmony_ci#include "ability_main.h" 3269570cc8Sopenharmony_ci#include "appspawn_message.h" 3369570cc8Sopenharmony_ci#include "appspawn_server.h" 3469570cc8Sopenharmony_ci#include "appspawn_service.h" 3569570cc8Sopenharmony_ci#include "securec.h" 3669570cc8Sopenharmony_ci 3769570cc8Sopenharmony_ci#define DEFAULT_UMASK 002 3869570cc8Sopenharmony_ci#define CAP_NUM 2 3969570cc8Sopenharmony_ci#define ENV_TITLE "LD_LIBRARY_PATH=" 4069570cc8Sopenharmony_ci#define UPPER_BOUND_GID 999 4169570cc8Sopenharmony_ci#define LOWER_BOUND_GID 100 4269570cc8Sopenharmony_ci#define GRP_NUM 2 4369570cc8Sopenharmony_ci#define DEVMGR_GRP 99 4469570cc8Sopenharmony_ci 4569570cc8Sopenharmony_cistatic int SetAmbientCapability(int cap) 4669570cc8Sopenharmony_ci{ 4769570cc8Sopenharmony_ci#ifdef __LINUX__ 4869570cc8Sopenharmony_ci if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_RAISE, cap, 0, 0)) { 4969570cc8Sopenharmony_ci printf("[Init] prctl PR_CAP_AMBIENT failed\n"); 5069570cc8Sopenharmony_ci return -1; 5169570cc8Sopenharmony_ci } 5269570cc8Sopenharmony_ci#endif 5369570cc8Sopenharmony_ci return 0; 5469570cc8Sopenharmony_ci} 5569570cc8Sopenharmony_ci 5669570cc8Sopenharmony_cistatic int SetCapability(unsigned int capsCnt, const unsigned int *caps) 5769570cc8Sopenharmony_ci{ 5869570cc8Sopenharmony_ci struct __user_cap_header_struct capHeader; 5969570cc8Sopenharmony_ci capHeader.version = _LINUX_CAPABILITY_VERSION_3; 6069570cc8Sopenharmony_ci capHeader.pid = 0; 6169570cc8Sopenharmony_ci 6269570cc8Sopenharmony_ci // common user, clear all caps 6369570cc8Sopenharmony_ci struct __user_cap_data_struct capData[CAP_NUM] = {0}; 6469570cc8Sopenharmony_ci for (unsigned int i = 0; i < capsCnt; ++i) { 6569570cc8Sopenharmony_ci capData[CAP_TO_INDEX(caps[i])].effective |= CAP_TO_MASK(caps[i]); 6669570cc8Sopenharmony_ci capData[CAP_TO_INDEX(caps[i])].permitted |= CAP_TO_MASK(caps[i]); 6769570cc8Sopenharmony_ci capData[CAP_TO_INDEX(caps[i])].inheritable |= CAP_TO_MASK(caps[i]); 6869570cc8Sopenharmony_ci } 6969570cc8Sopenharmony_ci 7069570cc8Sopenharmony_ci if (capset(&capHeader, capData) != 0) { 7169570cc8Sopenharmony_ci APPSPAWN_LOGE("[appspawn] capset failed, err: %d.", errno); 7269570cc8Sopenharmony_ci return -1; 7369570cc8Sopenharmony_ci } 7469570cc8Sopenharmony_ci for (unsigned int i = 0; i < capsCnt; ++i) { 7569570cc8Sopenharmony_ci if (SetAmbientCapability(caps[i]) != 0) { 7669570cc8Sopenharmony_ci APPSPAWN_LOGE("[appspawn] SetAmbientCapability failed, err: %d.", errno); 7769570cc8Sopenharmony_ci return -1; 7869570cc8Sopenharmony_ci } 7969570cc8Sopenharmony_ci } 8069570cc8Sopenharmony_ci return 0; 8169570cc8Sopenharmony_ci} 8269570cc8Sopenharmony_ci 8369570cc8Sopenharmony_cistatic int SetProcessName(struct AppSpawnContent *content, AppSpawnClient *client, 8469570cc8Sopenharmony_ci char *longProcName, uint32_t longProcNameLen) 8569570cc8Sopenharmony_ci{ 8669570cc8Sopenharmony_ci AppSpawnClientLite *appProperty = (AppSpawnClientLite *)client; 8769570cc8Sopenharmony_ci return prctl(PR_SET_NAME, appProperty->message.bundleName); 8869570cc8Sopenharmony_ci} 8969570cc8Sopenharmony_ci 9069570cc8Sopenharmony_cistatic int SetKeepCapabilities(struct AppSpawnContent *content, AppSpawnClient *client) 9169570cc8Sopenharmony_ci{ 9269570cc8Sopenharmony_ci APPSPAWN_LOGE("SetKeepCapabilities"); 9369570cc8Sopenharmony_ci#ifdef __LINUX__ 9469570cc8Sopenharmony_ci if (prctl(PR_SET_SECUREBITS, SECBIT_NO_SETUID_FIXUP | SECBIT_NO_SETUID_FIXUP_LOCKED)) { 9569570cc8Sopenharmony_ci printf("prctl failed\n"); 9669570cc8Sopenharmony_ci return -1; 9769570cc8Sopenharmony_ci } 9869570cc8Sopenharmony_ci#endif 9969570cc8Sopenharmony_ci return 0; 10069570cc8Sopenharmony_ci} 10169570cc8Sopenharmony_ci 10269570cc8Sopenharmony_cistatic int SetUidGid(struct AppSpawnContent *content, AppSpawnClient *client) 10369570cc8Sopenharmony_ci{ 10469570cc8Sopenharmony_ci AppSpawnClientLite *appProperty = (AppSpawnClientLite *)client; 10569570cc8Sopenharmony_ci APPSPAWN_LOGE("SetUidGid %d %d", appProperty->message.uID, appProperty->message.gID); 10669570cc8Sopenharmony_ci if (setgid(appProperty->message.gID) != 0) { 10769570cc8Sopenharmony_ci APPSPAWN_LOGE("[appspawn] setgid failed, gID %u, err: %d.", appProperty->message.gID, errno); 10869570cc8Sopenharmony_ci return -1; 10969570cc8Sopenharmony_ci } 11069570cc8Sopenharmony_ci 11169570cc8Sopenharmony_ci if (setuid(appProperty->message.uID) != 0) { 11269570cc8Sopenharmony_ci APPSPAWN_LOGE("[appspawn] setuid failed, uID %u, err: %d.", appProperty->message.uID, errno); 11369570cc8Sopenharmony_ci return -1; 11469570cc8Sopenharmony_ci } 11569570cc8Sopenharmony_ci gid_t groups[GRP_NUM]; 11669570cc8Sopenharmony_ci // add device groups for system app 11769570cc8Sopenharmony_ci if (appProperty->message.gID >= LOWER_BOUND_GID && appProperty->message.gID <= UPPER_BOUND_GID) { 11869570cc8Sopenharmony_ci groups[0] = appProperty->message.gID; 11969570cc8Sopenharmony_ci groups[1] = DEVMGR_GRP; 12069570cc8Sopenharmony_ci if (setgroups(GRP_NUM, groups)) { 12169570cc8Sopenharmony_ci APPSPAWN_LOGE("[appspawn] setgroups failed, uID %u, err: %d.", appProperty->message.uID, errno); 12269570cc8Sopenharmony_ci return -1; 12369570cc8Sopenharmony_ci } 12469570cc8Sopenharmony_ci } 12569570cc8Sopenharmony_ci 12669570cc8Sopenharmony_ci // umask call always succeeds and return the previous mask value which is not needed here 12769570cc8Sopenharmony_ci (void)umask(DEFAULT_UMASK); 12869570cc8Sopenharmony_ci return 0; 12969570cc8Sopenharmony_ci} 13069570cc8Sopenharmony_ci 13169570cc8Sopenharmony_cistatic int SetCapabilities(struct AppSpawnContent *content, AppSpawnClient *client) 13269570cc8Sopenharmony_ci{ 13369570cc8Sopenharmony_ci AppSpawnClientLite *appProperty = (AppSpawnClientLite *)client; 13469570cc8Sopenharmony_ci APPSPAWN_LOGE("SetCapabilities appProperty->message.capsCnt %d", appProperty->message.capsCnt); 13569570cc8Sopenharmony_ci // set rlimit 13669570cc8Sopenharmony_ci#ifdef __LINUX__ 13769570cc8Sopenharmony_ci static const rlim_t DEFAULT_RLIMIT = 40; 13869570cc8Sopenharmony_ci struct rlimit rlim = {0}; 13969570cc8Sopenharmony_ci rlim.rlim_cur = DEFAULT_RLIMIT; 14069570cc8Sopenharmony_ci rlim.rlim_max = DEFAULT_RLIMIT; 14169570cc8Sopenharmony_ci if (setrlimit(RLIMIT_NICE, &rlim) != 0) { 14269570cc8Sopenharmony_ci APPSPAWN_LOGE("[appspawn] setrlimit failed, err: %d.", errno); 14369570cc8Sopenharmony_ci return -1; 14469570cc8Sopenharmony_ci } 14569570cc8Sopenharmony_ci 14669570cc8Sopenharmony_ci#ifndef APPSPAWN_TEST 14769570cc8Sopenharmony_ci unsigned int tmpCaps[] = {17}; // 17 means CAP_SYS_RAWIO 14869570cc8Sopenharmony_ci unsigned int tmpsCapCnt = sizeof(tmpCaps) / sizeof(tmpCaps[0]); 14969570cc8Sopenharmony_ci if (SetCapability(tmpsCapCnt, tmpCaps) != 0) { 15069570cc8Sopenharmony_ci APPSPAWN_LOGE("[appspawn] setrlimit failed, err: %d.", errno); 15169570cc8Sopenharmony_ci return -1; 15269570cc8Sopenharmony_ci } 15369570cc8Sopenharmony_ci#endif 15469570cc8Sopenharmony_ci#else 15569570cc8Sopenharmony_ci if (SetCapability(appProperty->message.capsCnt, appProperty->message.caps) != 0) { 15669570cc8Sopenharmony_ci APPSPAWN_LOGE("[appspawn] SetCapability failed, err: %d.", errno); 15769570cc8Sopenharmony_ci return -1; 15869570cc8Sopenharmony_ci } 15969570cc8Sopenharmony_ci#endif // __LINUX__ 16069570cc8Sopenharmony_ci APPSPAWN_LOGE("SetCapabilities appProperty->message.capsCnt %d", appProperty->message.capsCnt); 16169570cc8Sopenharmony_ci return 0; 16269570cc8Sopenharmony_ci} 16369570cc8Sopenharmony_ci 16469570cc8Sopenharmony_cistatic void RunChildProcessor(AppSpawnContent *content, AppSpawnClient *client) 16569570cc8Sopenharmony_ci{ 16669570cc8Sopenharmony_ci APPSPAWN_LOGI("AbilityMain"); 16769570cc8Sopenharmony_ci AppSpawnClientLite *appProperty = (AppSpawnClientLite *)client; 16869570cc8Sopenharmony_ci APPSPAWN_LOGI("[appspawn] invoke, msg<%s,%s,%d,%d>", 16969570cc8Sopenharmony_ci appProperty->message.bundleName, appProperty->message.identityID, appProperty->message.uID, 17069570cc8Sopenharmony_ci appProperty->message.gID); 17169570cc8Sopenharmony_ci 17269570cc8Sopenharmony_ci#ifndef APPSPAWN_TEST 17369570cc8Sopenharmony_ci if (AbilityMain(appProperty->message.identityID) != 0) { 17469570cc8Sopenharmony_ci APPSPAWN_LOGE("[appspawn] AbilityMain execute failed, pid %d.", getpid()); 17569570cc8Sopenharmony_ci exit(0x7f); // 0x7f: user specified 17669570cc8Sopenharmony_ci } 17769570cc8Sopenharmony_ci#endif 17869570cc8Sopenharmony_ci} 17969570cc8Sopenharmony_ci 18069570cc8Sopenharmony_civoid SetContentFunction(AppSpawnContent *content) 18169570cc8Sopenharmony_ci{ 18269570cc8Sopenharmony_ci APPSPAWN_LOGI("SetContentFunction"); 18369570cc8Sopenharmony_ci content->runChildProcessor = RunChildProcessor; 18469570cc8Sopenharmony_ci} 18569570cc8Sopenharmony_ci 18669570cc8Sopenharmony_ciint AppSpawnExecuteSpawningHook(AppSpawnContent *content, AppSpawnClient *client) 18769570cc8Sopenharmony_ci{ 18869570cc8Sopenharmony_ci (void)umask(DEFAULT_UMASK); 18969570cc8Sopenharmony_ci int ret = SetKeepCapabilities(content, client); 19069570cc8Sopenharmony_ci if (ret != 0) { 19169570cc8Sopenharmony_ci return ret; 19269570cc8Sopenharmony_ci } 19369570cc8Sopenharmony_ci ret = SetProcessName(content, client, content->longProcName, content->longProcNameLen); 19469570cc8Sopenharmony_ci if (ret != 0) { 19569570cc8Sopenharmony_ci return ret; 19669570cc8Sopenharmony_ci } 19769570cc8Sopenharmony_ci ret = SetUidGid(content, client); 19869570cc8Sopenharmony_ci if (ret != 0) { 19969570cc8Sopenharmony_ci return ret; 20069570cc8Sopenharmony_ci } 20169570cc8Sopenharmony_ci ret = SetCapabilities(content, client); 20269570cc8Sopenharmony_ci if (ret != 0) { 20369570cc8Sopenharmony_ci return ret; 20469570cc8Sopenharmony_ci } 20569570cc8Sopenharmony_ci return 0; 20669570cc8Sopenharmony_ci} 20769570cc8Sopenharmony_ci 20869570cc8Sopenharmony_ciint AppSpawnExecuteClearEnvHook(AppSpawnContent *content, AppSpawnClient *client) 20969570cc8Sopenharmony_ci{ 21069570cc8Sopenharmony_ci return 0; 21169570cc8Sopenharmony_ci} 21269570cc8Sopenharmony_ci 21369570cc8Sopenharmony_ciint AppSpawnExecutePreReplyHook(AppSpawnContent *content, AppSpawnClient *client) 21469570cc8Sopenharmony_ci{ 21569570cc8Sopenharmony_ci return 0; 21669570cc8Sopenharmony_ci} 21769570cc8Sopenharmony_ci 21869570cc8Sopenharmony_ciint AppSpawnExecutePostReplyHook(AppSpawnContent *content, AppSpawnClient *client) 21969570cc8Sopenharmony_ci{ 22069570cc8Sopenharmony_ci return 0; 22169570cc8Sopenharmony_ci} 22269570cc8Sopenharmony_ci 22369570cc8Sopenharmony_civoid AppSpawnEnvClear(AppSpawnContent *content, AppSpawnClient *client) 22469570cc8Sopenharmony_ci{ 22569570cc8Sopenharmony_ci} 226