param_selinux.c 12.5 KB
Newer Older
S
sun_fan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*
 * Copyright (c) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
C
cheng_jinsong 已提交
15
#include <errno.h>
M
Mupceet 已提交
16 17
#include <dlfcn.h>
#include <sys/socket.h>
S
sun_fan 已提交
18

19
#include "init_utils.h"
M
Mupceet 已提交
20
#include "param_manager.h"
S
sun_fan 已提交
21 22
#include "param_security.h"
#include "param_utils.h"
M
Mupceet 已提交
23
#include "param_base.h"
M
Mupceet 已提交
24 25 26
#ifdef PARAM_SUPPORT_SELINUX
#include "selinux_parameter.h"
#endif
S
sun_fan 已提交
27

M
Mupceet 已提交
28 29
#ifdef __aarch64__
#define CHECKER_LIB_NAME "/system/lib64/libparaperm_checker.z.so"
C
cheng_jinsong 已提交
30
#define CHECKER_UPDATER_LIB "/lib64/libparaperm_checker.z.so"
M
Mupceet 已提交
31 32
#else
#define CHECKER_LIB_NAME "/system/lib/libparaperm_checker.z.so"
C
cheng_jinsong 已提交
33
#define CHECKER_UPDATER_LIB "/lib/libparaperm_checker.z.so"
M
Mupceet 已提交
34
#endif
C
cheng_jinsong 已提交
35
typedef int (*SelinuxSetParamCheck)(const char *paraName, const char *destContext, const SrcInfo *info);
M
Mupceet 已提交
36

A
an_xinwei 已提交
37
static int InitSelinuxOpsForInit(SelinuxSpace *selinuxSpace)
S
sun_fan 已提交
38
{
A
an_xinwei 已提交
39
    if (selinuxSpace->selinuxHandle == NULL) {
C
cheng_jinsong 已提交
40
        const char *libname = (GetParamWorkSpace()->ops.updaterMode == 1) ? CHECKER_UPDATER_LIB : CHECKER_LIB_NAME;
A
an_xinwei 已提交
41 42
        selinuxSpace->selinuxHandle = dlopen(libname, RTLD_LAZY);
        PARAM_CHECK(selinuxSpace->selinuxHandle != NULL,
M
Mupceet 已提交
43
            return 0, "Failed to dlsym selinuxHandle, %s", dlerror());
M
Mupceet 已提交
44
    }
A
an_xinwei 已提交
45 46 47 48
    void *handle = selinuxSpace->selinuxHandle;
    if (selinuxSpace->setParamCheck == NULL) {
        selinuxSpace->setParamCheck = (SelinuxSetParamCheck)dlsym(handle, "SetParamCheck");
        PARAM_CHECK(selinuxSpace->setParamCheck != NULL, return -1, "Failed to dlsym setParamCheck %s", dlerror());
M
Mupceet 已提交
49
    }
A
an_xinwei 已提交
50 51 52
    if (selinuxSpace->getParamList == NULL) {
        selinuxSpace->getParamList = (ParamContextsList *(*)()) dlsym(handle, "GetParamList");
        PARAM_CHECK(selinuxSpace->getParamList != NULL, return -1, "Failed to dlsym getParamList %s", dlerror());
M
Mupceet 已提交
53
    }
A
an_xinwei 已提交
54 55 56
    if (selinuxSpace->getParamLabel == NULL) {
        selinuxSpace->getParamLabel = (const char *(*)(const char *))dlsym(handle, "GetParamLabel");
        PARAM_CHECK(selinuxSpace->getParamLabel != NULL, return -1, "Failed to dlsym getParamLabel %s", dlerror());
M
Mupceet 已提交
57
    }
A
an_xinwei 已提交
58 59 60
    if (selinuxSpace->initParamSelinux == NULL) {
        selinuxSpace->initParamSelinux = (int (*)())dlsym(handle, "InitParamSelinux");
        PARAM_CHECK(selinuxSpace->initParamSelinux != NULL, return -1, "Failed to dlsym initParamSelinux ");
M
Mupceet 已提交
61
    }
C
cheng_jinsong 已提交
62 63 64
    if (selinuxSpace->getParamLabelIndex == NULL) {
        selinuxSpace->getParamLabelIndex = (int (*)(const char *))dlsym(handle, "GetParamLabelIndex");
        PARAM_CHECK(selinuxSpace->getParamLabelIndex != NULL, return -1, "Failed to dlsym getParamLabelIndex ");
M
Mupceet 已提交
65
    }
A
an_xinwei 已提交
66 67 68 69 70
    if (selinuxSpace->setSelinuxLogCallback == NULL) {
        selinuxSpace->setSelinuxLogCallback = (void (*)())dlsym(handle, "SetInitSelinuxLog");
    }
    if (selinuxSpace->destroyParamList == NULL) {
        selinuxSpace->destroyParamList =
M
Mupceet 已提交
71
            (void (*)(ParamContextsList **))dlsym(handle, "DestroyParamList");
A
an_xinwei 已提交
72
        PARAM_CHECK(selinuxSpace->destroyParamList != NULL,
M
Mupceet 已提交
73 74
            return -1, "Failed to dlsym destroyParamList %s", dlerror());
    }
A
an_xinwei 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94

    // init and open avc log
    int ret = selinuxSpace->initParamSelinux();
    if (selinuxSpace->setSelinuxLogCallback != NULL) {
        selinuxSpace->setSelinuxLogCallback();
    }
    return ret;
}

static int InitLocalSecurityLabel(ParamSecurityLabel *security, int isInit)
{
    PARAM_CHECK(GetParamWorkSpace() != NULL, return -1, "Invalid workspace");
    UNUSED(isInit);
    PARAM_CHECK(security != NULL, return -1, "Invalid security");
    security->cred.pid = getpid();
    security->cred.uid = geteuid();
    security->cred.gid = getegid();
    security->flags[PARAM_SECURITY_SELINUX] = 0;
    PARAM_LOGV("InitLocalSecurityLabel");
#if !(defined STARTUP_INIT_TEST || defined LOCAL_TEST)
C
codex  
chengjinsong 已提交
95
    if ((bool)isInit) {
A
an_xinwei 已提交
96 97 98 99 100 101 102 103
        int ret = InitSelinuxOpsForInit(&GetParamWorkSpace()->selinuxSpace);
        PARAM_CHECK(ret == 0, return -1, "Failed to init selinux ops");
    } else {
        SelinuxSpace *selinuxSpace = &GetParamWorkSpace()->selinuxSpace;
        selinuxSpace->initParamSelinux = InitParamSelinux;
        selinuxSpace->getParamList = GetParamList;
        selinuxSpace->getParamLabel = GetParamLabel;
        selinuxSpace->destroyParamList = DestroyParamList;
C
cheng_jinsong 已提交
104
        selinuxSpace->getParamLabelIndex = GetParamLabelIndex;
A
an_xinwei 已提交
105 106 107
        // init
        selinuxSpace->initParamSelinux();
    }
M
Mupceet 已提交
108
#endif
C
cheng_jinsong 已提交
109
    PARAM_LOGV("Load selinux lib success.");
S
sun_fan 已提交
110 111 112 113 114 115 116 117
    return 0;
}

static int FreeLocalSecurityLabel(ParamSecurityLabel *srcLabel)
{
    return 0;
}

M
Mupceet 已提交
118 119
static void SetSelinuxFileCon(const char *name, const char *context)
{
A
an_xinwei 已提交
120 121
    PARAM_CHECK(GetParamWorkSpace() != NULL && GetParamWorkSpace()->ops.setfilecon != NULL,
        return, "Invalid workspace or setfilecon");
M
Mupceet 已提交
122 123 124 125
    static char buffer[FILENAME_LEN_MAX] = {0};
    int len = ParamSprintf(buffer, sizeof(buffer), "%s/%s", PARAM_STORAGE_PATH, context);
    if (len > 0) {
        buffer[len] = '\0';
C
cheng_jinsong 已提交
126
        PARAM_LOGV("setfilecon name %s path: %s %s ", name, context, buffer);
A
an_xinwei 已提交
127
        if (GetParamWorkSpace()->ops.setfilecon(buffer, context) < 0) {
M
Mupceet 已提交
128 129 130 131 132
            PARAM_LOGE("Failed to setfilecon %s ", context);
        }
    }
}

C
cheng_jinsong 已提交
133 134 135 136 137 138
static uint32_t GetWorkSpaceSize(const char *content)
{
    if (strcmp(content, WORKSPACE_NAME_DEF_SELINUX) == 0) {
        return PARAM_WORKSPACE_MAX;
    }
    char name[PARAM_NAME_LEN_MAX] = {0};
C
cheng_jinsong 已提交
139 140
    int index = 6; // 6 strlen for const.
    (void)ParamMemcpy(name, sizeof(name), "const.", index);
C
cheng_jinsong 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154
    size_t len = strlen(content);
    for (size_t i = strlen("u:object_r:"); i < len; i++) {
        if (*(content + i) == ':') {
            break;
        }
        name[index++] = *(content + i);
    }
    if (index == 0) {
#ifdef STARTUP_INIT_TEST
        return PARAM_WORKSPACE_DEF;
#else
        return PARAM_WORKSPACE_MIN;
#endif
    }
C
cheng_jinsong 已提交
155
    ParamNode *node = GetParamNode(WORKSPACE_INDEX_BASE, name);
C
cheng_jinsong 已提交
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    if (node == NULL) {
#ifdef STARTUP_INIT_TEST
        return PARAM_WORKSPACE_DEF;
#else
        return PARAM_WORKSPACE_MIN;
#endif
    }
    int ret = ParamMemcpy(name, sizeof(name) - 1, node->data + node->keyLength + 1, node->valueLength);
    if (ret == 0) {
        name[node->valueLength] = '\0';
        errno = 0;
        uint32_t value = (uint32_t)strtoul(name, NULL, DECIMAL_BASE);
        return (errno != 0) ? PARAM_WORKSPACE_MIN : value;
    }
    return PARAM_WORKSPACE_MIN;
}

C
cheng_jinsong 已提交
173
static void HandleSelinuxLabelForInit(const ParameterNode *paramNode, int readOnly)
S
sun_fan 已提交
174
{
C
cheng_jinsong 已提交
175 176
    uint32_t labelIndex = paramNode->index + WORKSPACE_INDEX_BASE;
    int ret = AddWorkSpace(paramNode->paraContext, labelIndex, readOnly, GetWorkSpaceSize(paramNode->paraContext));
C
cheng_jinsong 已提交
177
    if (ret != 0) {
C
cheng_jinsong 已提交
178
        PARAM_LOGE("Forbid to add selinux workspace %s %s", paramNode->paraName, paramNode->paraContext);
C
cheng_jinsong 已提交
179 180 181
        return;
    }
    // set selinux label
C
cheng_jinsong 已提交
182
    SetSelinuxFileCon(paramNode->paraName, paramNode->paraContext);
C
cheng_jinsong 已提交
183
}
M
Mupceet 已提交
184

C
cheng_jinsong 已提交
185
static int SelinuxGetAllLabel(int readOnly,
C
cheng_jinsong 已提交
186
    void (*handleSelinuxLabel)(const ParameterNode *paramNode, int readOnly))
C
cheng_jinsong 已提交
187 188 189 190
{
    SelinuxSpace *selinuxSpace = &GetParamWorkSpace()->selinuxSpace;
    PARAM_CHECK(selinuxSpace->getParamList != NULL, return DAC_RESULT_FORBIDED, "Invalid getParamList");
    ParamContextsList *node = selinuxSpace->getParamList();
M
Mupceet 已提交
191 192
    int count = 0;
    while (node != NULL) {
C
cheng_jinsong 已提交
193 194
        PARAM_LOGV("SelinuxGetAllLabel index %d name %s content %s",
            node->info.index, node->info.paraName, node->info.paraContext);
M
Mupceet 已提交
195 196
        if (node->info.paraContext == NULL || node->info.paraName == NULL) {
            node = node->next;
S
sun_fan 已提交
197 198
            continue;
        }
C
cheng_jinsong 已提交
199
        handleSelinuxLabel(&node->info, readOnly);
M
Mupceet 已提交
200
        count++;
M
Mupceet 已提交
201
        node = node->next;
4
411148299@qq.com 已提交
202
    }
C
cheng_jinsong 已提交
203 204
    ParameterNode tmpNode = {WORKSPACE_NAME_DEF_SELINUX, WORKSPACE_NAME_DEF_SELINUX, 0};
    handleSelinuxLabel(&tmpNode, readOnly);
C
fix log  
cheng_jinsong 已提交
205
    PARAM_LOGV("Selinux get all label counts %d.", count);
M
Mupceet 已提交
206
    return 0;
S
sun_fan 已提交
207 208
}

C
cheng_jinsong 已提交
209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
static void HandleSelinuxLabelForPermission(const ParameterNode *paramNode, int readOnly)
{
    uint32_t labelIndex = paramNode->index + WORKSPACE_INDEX_BASE;
    if (labelIndex == WORKSPACE_INDEX_BASE) {
        return;
    }
    if (*(paramNode->paraName + strlen(paramNode->paraName) - 1) != '.') {
        return;
    }
    // save selinux index
    ParamWorkSpace *paramWorkspace = GetParamWorkSpace();
    PARAM_CHECK(paramWorkspace != NULL && paramWorkspace->workSpace != NULL, return, "Invalid workspace");
    WorkSpace *space = paramWorkspace->workSpace[WORKSPACE_INDEX_DAC];
    PARAM_CHECK(space != NULL && space->area != NULL, return, "Failed to get dac space %s", paramNode->paraName);
    uint32_t index = 0;
    (void)FindTrieNode(space, paramNode->paraName, strlen(paramNode->paraName), &index);
    ParamSecurityNode *node = (ParamSecurityNode *)GetTrieNode(space, index);
    PARAM_CHECK(node != NULL, return, "Can not get security label for %s", paramNode->paraName);
    PARAM_LOGV("HandleSelinuxLabelForPermission %s selinuxIndex [ %u %u] dac %u %s ",
        paramNode->paraName, labelIndex, node->selinuxIndex, index, paramNode->paraContext);
    ParamAuditData auditData = {0};
    auditData.dacData.gid = node->gid;
    auditData.dacData.uid = node->uid;
    auditData.dacData.mode = node->mode;
    auditData.dacData.paramType = node->type;
    auditData.selinuxIndex = labelIndex;
    auditData.name = paramNode->paraName;
    AddSecurityLabel(&auditData);
}

239 240 241
static int SelinuxGetParamSecurityLabel(const char *path)
{
    UNUSED(path);
C
cheng_jinsong 已提交
242 243 244 245
    if (path == NULL) {
        return SelinuxGetAllLabel(0, HandleSelinuxLabelForInit);
    }
    return SelinuxGetAllLabel(0, HandleSelinuxLabelForPermission);
246 247
}

S
sun_fan 已提交
248 249 250
static int CheckFilePermission(const ParamSecurityLabel *localLabel, const char *fileName, int flags)
{
    UNUSED(flags);
C
cheng_jinsong 已提交
251 252
    UNUSED(localLabel);
    UNUSED(fileName);
S
sun_fan 已提交
253 254 255
    return 0;
}

C
cheng_jinsong 已提交
256
static void HandleSelinuxLabel(const ParameterNode *node, int readOnly)
A
an_xinwei 已提交
257
{
C
cheng_jinsong 已提交
258 259 260
    PARAM_LOGV("HandleSelinuxLabel %s %s", node->paraName, node->paraContext);
    uint32_t index = node->index + WORKSPACE_INDEX_BASE;
    int ret = AddWorkSpace(node->paraContext, index, readOnly, GetWorkSpaceSize(node->paraContext));
C
cheng_jinsong 已提交
261
    if (ret != 0) {
C
cheng_jinsong 已提交
262
        PARAM_LOGE("Forbid to add selinux workspace %s %s", node->paraName, node->paraContext);
263
    }
C
cheng_jinsong 已提交
264 265 266
    if (readOnly == 2) { // 2 mean need to open workspace
        ret = OpenWorkSpace(index, 1);
        if (ret != 0) {
C
cheng_jinsong 已提交
267
            PARAM_LOGE("Failed to open selinux workspace %s %s index %u", node->paraName, node->paraContext, index);
C
cheng_jinsong 已提交
268
        }
M
Mupceet 已提交
269
    }
S
sun_fan 已提交
270 271
}

C
cheng_jinsong 已提交
272 273
static int UpdaterCheckParamPermission(const ParamLabelIndex *labelIndex,
    const ParamSecurityLabel *srcLabel, const char *name, uint32_t mode)
M
Mupceet 已提交
274 275 276 277
{
    return DAC_RESULT_PERMISSION;
}

C
cheng_jinsong 已提交
278 279
static int OpenPermissionWorkSpace(const char *path)
{
C
cheng_jinsong 已提交
280 281
    static int loadLabels = 0;
    int ret = 0;
C
cheng_jinsong 已提交
282
    if (path == NULL) {
C
cheng_jinsong 已提交
283
        ret = SelinuxGetAllLabel(1, HandleSelinuxLabel);
C
cheng_jinsong 已提交
284 285
    } else if (strcmp(path, "open") == 0) {
        if (loadLabels == 0) {
C
cheng_jinsong 已提交
286
            ret = SelinuxGetAllLabel(2, HandleSelinuxLabel); // 2 mean need to open workspace
C
cheng_jinsong 已提交
287 288
        }
        loadLabels = 1;
C
cheng_jinsong 已提交
289 290
    }
    return ret;
C
cheng_jinsong 已提交
291 292
}

M
Mupceet 已提交
293
INIT_LOCAL_API int RegisterSecuritySelinuxOps(ParamSecurityOps *ops, int isInit)
S
sun_fan 已提交
294
{
A
an_xinwei 已提交
295
    PARAM_CHECK(GetParamWorkSpace() != NULL, return -1, "Invalid workspace");
S
sun_fan 已提交
296
    PARAM_CHECK(ops != NULL, return -1, "Invalid param");
M
Mupceet 已提交
297
    int ret = ParamStrCpy(ops->name, sizeof(ops->name), "selinux");
S
sun_fan 已提交
298 299 300
    ops->securityGetLabel = NULL;
    ops->securityInitLabel = InitLocalSecurityLabel;
    ops->securityCheckFilePermission = CheckFilePermission;
A
an_xinwei 已提交
301
    if (GetParamWorkSpace()->ops.updaterMode == 1) {
M
Mupceet 已提交
302 303
        ops->securityCheckParamPermission = UpdaterCheckParamPermission;
    } else {
C
cheng_jinsong 已提交
304
#ifdef STARTUP_INIT_TEST
M
Mupceet 已提交
305
        ops->securityCheckParamPermission = SelinuxCheckParamPermission;
C
cheng_jinsong 已提交
306
#endif
M
Mupceet 已提交
307
    }
S
sun_fan 已提交
308
    ops->securityFreeLabel = FreeLocalSecurityLabel;
309
    if (isInit != 0) {
M
Mupceet 已提交
310
        ops->securityGetLabel = SelinuxGetParamSecurityLabel;
C
cheng_jinsong 已提交
311 312
    } else {
        ops->securityGetLabel = OpenPermissionWorkSpace;
S
sun_fan 已提交
313
    }
M
Mupceet 已提交
314
    return ret;
S
sun_fan 已提交
315
}