• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "selinux_parameter.h"
17 #include <fcntl.h>
18 #include <pthread.h>
19 #include <sys/socket.h>
20 #include <unistd.h>
21 #include "errno.h"
22 #include "selinux_error.h"
23 #include "contexts_trie.h"
24 
25 static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER;
26 static const char DEFAULT_CONTEXT[] = "u:object_r:default_param:s0";
27 static ParamContextsTrie *g_contextsTrie = NULL;
28 static ParamContextsList *g_contextsList = NULL;
29 
ParameterContextsLoad(void)30 static int ParameterContextsLoad(void)
31 {
32     if (getpid() == 1) { // process init will load parameter_contexts to shared memory
33         int res = LoadParameterContextsToSharedMem();
34         if (res != 0) {
35             return res;
36         }
37     }
38     if (!ReadParamFromSharedMem(&g_contextsTrie, &g_contextsList)) { // other process load parameters from shared memory
39         return -SELINUX_CONTEXTS_FILE_LOAD_ERROR;
40     }
41     return 0;
42 }
43 
InitParamSelinux(void)44 int InitParamSelinux(void)
45 {
46     pthread_mutex_lock(&g_mutex);
47     if (g_contextsTrie != NULL) {
48         pthread_mutex_unlock(&g_mutex);
49         return 0;
50     }
51     int res = ParameterContextsLoad();
52     pthread_mutex_unlock(&g_mutex);
53     return res;
54 }
55 
DestroyParamList(ParamContextsList ** list)56 void DestroyParamList(ParamContextsList **list)
57 {
58     if (list == NULL) {
59         return;
60     }
61     ParamContextsList *tmpNode;
62     ParamContextsList *listHead = *list;
63     while (listHead != NULL) {
64         tmpNode = listHead->next;
65         free(listHead);
66         listHead = tmpNode;
67     }
68     *list = NULL;
69     return;
70 }
71 
GetParamList()72 ParamContextsList *GetParamList()
73 {
74     if (g_contextsList == NULL) {
75         return NULL;
76     }
77     return g_contextsList;
78 }
79 
GetParamLabel(const char * paraName)80 const char *GetParamLabel(const char *paraName)
81 {
82     if (paraName == NULL || g_contextsTrie == NULL) {
83         return DEFAULT_CONTEXT;
84     }
85     return SearchFromParamTrie(g_contextsTrie, paraName);
86 }
87