param_security.h 3.6 KB
Newer Older
S
sun_fan 已提交
1
/*
Z
zhong_ning 已提交
2
 * Copyright (c) 2021 Huawei Device Co., Ltd.
S
sun_fan 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * 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.
 */

#ifndef BASE_STARTUP_PARAM_SECURITY_H
#define BASE_STARTUP_PARAM_SECURITY_H
#include <stdint.h>
M
Mupceet 已提交
19 20 21
#ifndef __LINUX__
#include <sys/socket.h>
#endif
S
sun_fan 已提交
22
#include <sys/types.h>
M
Mupceet 已提交
23 24 25 26 27 28 29
#ifdef PARAM_SUPPORT_SELINUX
#include "selinux_parameter.h"
#else
typedef struct ParamContextsList_ {
} ParamContextsList;
#endif

M
Mupceet 已提交
30 31
#include "beget_ext.h"

S
sun_fan 已提交
32 33 34 35 36 37 38 39
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif

#define DAC_GROUP_START 3
#define DAC_OTHER_START 6
M
Mupceet 已提交
40 41 42
#define DAC_READ 0x0100  // 4
#define DAC_WRITE 0x0080 // 2
#define DAC_WATCH 0x0040 // 1
S
sun_fan 已提交
43 44 45
#define DAC_ALL_PERMISSION 0777

#define LABEL_ALL_PERMISSION 0x04
M
Mupceet 已提交
46
#define LABEL_CHECK_IN_ALL_PROCESS 0x02
S
sun_fan 已提交
47 48
#define LABEL_INIT_FOR_INIT 0x01

M
Mupceet 已提交
49 50
#define SELINUX_CONTENT_LEN 64
#define SYS_UID_INDEX      1000
S
sun_fan 已提交
51

M
Mupceet 已提交
52
#define DAC_RESULT_PERMISSION 0
S
sun_fan 已提交
53 54 55 56 57 58 59

typedef struct UserCred {
    pid_t pid;
    uid_t uid;
    gid_t gid;
} UserCred;

M
Mupceet 已提交
60 61 62 63 64 65 66 67
typedef enum {
    PARAM_SECURITY_DAC = 0,
#ifdef PARAM_SUPPORT_SELINUX
    PARAM_SECURITY_SELINUX,
#endif
    PARAM_SECURITY_MAX
} ParamSecurityType;

S
sun_fan 已提交
68 69
typedef struct {
    UserCred cred;
M
Mupceet 已提交
70
    uint32_t flags[PARAM_SECURITY_MAX];
S
sun_fan 已提交
71 72 73 74 75 76
} ParamSecurityLabel;

typedef struct {
    pid_t pid;
    uid_t uid;
    gid_t gid;
M
Mupceet 已提交
77 78
    uint16_t mode;
    uint8_t paramType;
S
sun_fan 已提交
79 80 81 82 83
} ParamDacData;

typedef struct {
    ParamDacData dacData;
    const char *name;
M
Mupceet 已提交
84 85 86
#ifdef PARAM_SUPPORT_SELINUX
    char label[SELINUX_CONTENT_LEN];
#endif
S
sun_fan 已提交
87 88 89
} ParamAuditData;

typedef struct {
M
Mupceet 已提交
90 91 92
    char name[10];
    int (*securityInitLabel)(ParamSecurityLabel *label, int isInit);
    int (*securityGetLabel)(const char *path);
S
sun_fan 已提交
93
    int (*securityCheckFilePermission)(const ParamSecurityLabel *label, const char *fileName, int flags);
M
Mupceet 已提交
94
    int (*securityCheckParamPermission)(const ParamSecurityLabel *srcLabel, const char *name, uint32_t mode);
S
sun_fan 已提交
95 96 97 98
    int (*securityFreeLabel)(ParamSecurityLabel *srcLabel);
} ParamSecurityOps;

typedef int (*RegisterSecurityOpsPtr)(ParamSecurityOps *ops, int isInit);
M
Mupceet 已提交
99 100 101
typedef int (*SelinuxSetParamCheck)(const char *paraName, struct ucred *uc);
typedef struct SelinuxSpace_ {
    void *selinuxHandle;
102
    void (*setSelinuxLogCallback)(void);
M
Mupceet 已提交
103
    int (*setParamCheck)(const char *paraName, struct ucred *uc);
M
Mupceet 已提交
104
    const char *(*getParamLabel)(const char *paraName);
105
    void (*initParamSelinux)(void);
M
Mupceet 已提交
106
    int (*readParamCheck)(const char *paraName);
107
    ParamContextsList *(*getParamList)(void);
M
Mupceet 已提交
108 109
    void (*destroyParamList)(ParamContextsList **list);
} SelinuxSpace;
S
sun_fan 已提交
110
#ifdef PARAM_SUPPORT_SELINUX
M
Mupceet 已提交
111
INIT_LOCAL_API int RegisterSecuritySelinuxOps(ParamSecurityOps *ops, int isInit);
S
sun_fan 已提交
112
#endif
M
Mupceet 已提交
113

M
Mupceet 已提交
114
#if defined STARTUP_INIT_TEST || defined LOCAL_TEST
M
Mupceet 已提交
115
int RegisterSecurityOps(int onlyRead);
M
Mupceet 已提交
116
void SetSelinuxOps(const SelinuxSpace *space);
S
sun_fan 已提交
117 118
#endif

M
Mupceet 已提交
119 120 121 122 123
INIT_LOCAL_API ParamSecurityOps *GetParamSecurityOps(int type);
INIT_LOCAL_API void LoadGroupUser(void);
INIT_LOCAL_API int RegisterSecurityDacOps(ParamSecurityOps *ops, int isInit);
INIT_LOCAL_API void OpenPermissionWorkSpace(void);

S
sun_fan 已提交
124 125 126 127 128
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif
M
Mupceet 已提交
129
#endif  // BASE_STARTUP_PARAM_SECURITY_H