1d9f0492fSopenharmony_ci/*
2d9f0492fSopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3d9f0492fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4d9f0492fSopenharmony_ci * you may not use this file except in compliance with the License.
5d9f0492fSopenharmony_ci * You may obtain a copy of the License at
6d9f0492fSopenharmony_ci *
7d9f0492fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8d9f0492fSopenharmony_ci *
9d9f0492fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10d9f0492fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11d9f0492fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12d9f0492fSopenharmony_ci * See the License for the specific language governing permissions and
13d9f0492fSopenharmony_ci * limitations under the License.
14d9f0492fSopenharmony_ci */
15d9f0492fSopenharmony_ci#include "param_osadp.h"
16d9f0492fSopenharmony_ci#include "param_security.h"
17d9f0492fSopenharmony_ci#include "securec.h"
18d9f0492fSopenharmony_ci
19d9f0492fSopenharmony_cistatic int InitLocalSecurityLabel(ParamSecurityLabel *security, int isInit)
20d9f0492fSopenharmony_ci{
21d9f0492fSopenharmony_ci    UNUSED(isInit);
22d9f0492fSopenharmony_ci    PARAM_CHECK(security != NULL, return -1, "Invalid security");
23d9f0492fSopenharmony_ci#if defined __LITEOS_A__
24d9f0492fSopenharmony_ci    security->cred.pid = getpid();
25d9f0492fSopenharmony_ci    security->cred.uid = getuid();
26d9f0492fSopenharmony_ci    security->cred.gid = 0;
27d9f0492fSopenharmony_ci#else
28d9f0492fSopenharmony_ci    security->cred.pid = 0;
29d9f0492fSopenharmony_ci    security->cred.uid = 0;
30d9f0492fSopenharmony_ci    security->cred.gid = 0;
31d9f0492fSopenharmony_ci#endif
32d9f0492fSopenharmony_ci    security->flags[PARAM_SECURITY_DAC] |= LABEL_CHECK_IN_ALL_PROCESS;
33d9f0492fSopenharmony_ci    return 0;
34d9f0492fSopenharmony_ci}
35d9f0492fSopenharmony_ci
36d9f0492fSopenharmony_cistatic int FreeLocalSecurityLabel(ParamSecurityLabel *srcLabel)
37d9f0492fSopenharmony_ci{
38d9f0492fSopenharmony_ci    (void)srcLabel;
39d9f0492fSopenharmony_ci    return 0;
40d9f0492fSopenharmony_ci}
41d9f0492fSopenharmony_ci
42d9f0492fSopenharmony_cistatic int DacGetParamSecurityLabel(const char *path)
43d9f0492fSopenharmony_ci{
44d9f0492fSopenharmony_ci    UNUSED(path);
45d9f0492fSopenharmony_ci    return 0;
46d9f0492fSopenharmony_ci}
47d9f0492fSopenharmony_ci
48d9f0492fSopenharmony_cistatic int CheckFilePermission(const ParamSecurityLabel *localLabel, const char *fileName, int flags)
49d9f0492fSopenharmony_ci{
50d9f0492fSopenharmony_ci    UNUSED(flags);
51d9f0492fSopenharmony_ci    PARAM_CHECK(localLabel != NULL && fileName != NULL, return -1, "Invalid param");
52d9f0492fSopenharmony_ci    return 0;
53d9f0492fSopenharmony_ci}
54d9f0492fSopenharmony_ci
55d9f0492fSopenharmony_cistatic int LiteDacCheckParamPermission(const ParamLabelIndex *labelIndex,
56d9f0492fSopenharmony_ci    const ParamSecurityLabel *srcLabel, const char *name, uint32_t mode)
57d9f0492fSopenharmony_ci{
58d9f0492fSopenharmony_ci    UNUSED(labelIndex);
59d9f0492fSopenharmony_ci    UNUSED(srcLabel);
60d9f0492fSopenharmony_ci    UNUSED(name);
61d9f0492fSopenharmony_ci    UNUSED(mode);
62d9f0492fSopenharmony_ci#if defined(__LITEOS_A__)
63d9f0492fSopenharmony_ci    uid_t uid = getuid();
64d9f0492fSopenharmony_ci    return uid <= SYS_UID_INDEX ? DAC_RESULT_PERMISSION : DAC_RESULT_FORBIDED;
65d9f0492fSopenharmony_ci#endif
66d9f0492fSopenharmony_ci    return DAC_RESULT_PERMISSION;
67d9f0492fSopenharmony_ci}
68d9f0492fSopenharmony_ci
69d9f0492fSopenharmony_ciINIT_LOCAL_API int RegisterSecurityDacOps(ParamSecurityOps *ops, int isInit)
70d9f0492fSopenharmony_ci{
71d9f0492fSopenharmony_ci    PARAM_CHECK(ops != NULL, return -1, "Invalid param");
72d9f0492fSopenharmony_ci    PARAM_LOGV("RegisterSecurityDacOps %d", isInit);
73d9f0492fSopenharmony_ci    int ret = strcpy_s(ops->name, sizeof(ops->name), "dac");
74d9f0492fSopenharmony_ci    ops->securityGetLabel = NULL;
75d9f0492fSopenharmony_ci    ops->securityInitLabel = InitLocalSecurityLabel;
76d9f0492fSopenharmony_ci    ops->securityCheckFilePermission = CheckFilePermission;
77d9f0492fSopenharmony_ci    ops->securityCheckParamPermission = LiteDacCheckParamPermission;
78d9f0492fSopenharmony_ci    ops->securityFreeLabel = FreeLocalSecurityLabel;
79d9f0492fSopenharmony_ci    if (isInit) {
80d9f0492fSopenharmony_ci        ops->securityGetLabel = DacGetParamSecurityLabel;
81d9f0492fSopenharmony_ci    }
82d9f0492fSopenharmony_ci    return ret;
83d9f0492fSopenharmony_ci}
84