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 <stdlib.h> 21 #include <unistd.h> 22 #include "errno.h" 23 #include "selinux_error.h" 24 #include "contexts_trie.h" 25 26 static pthread_mutex_t g_mutex = PTHREAD_MUTEX_INITIALIZER; 27 static const char DEFAULT_CONTEXT[] = "u:object_r:default_param:s0"; 28 static const int INVALID_INDEX = -1; 29 static ParamContextsTrie *g_contextsTrie = NULL; 30 static ParamContextsList *g_contextsList = NULL; 31 ParameterContextsLoad(int isInit)32static int ParameterContextsLoad(int isInit) 33 { 34 if (isInit) { // process init will load parameter_contexts to shared memory 35 int res = LoadParameterContextsToSharedMem(); 36 if (res != 0) { 37 return res; 38 } 39 } 40 if (!ReadParamFromSharedMem(&g_contextsTrie, &g_contextsList)) { // other process load parameters from shared memory 41 return -SELINUX_CONTEXTS_FILE_LOAD_ERROR; 42 } 43 return 0; 44 } 45 InitParamSelinux(int isInit)46int InitParamSelinux(int isInit) 47 { 48 pthread_mutex_lock(&g_mutex); 49 if (g_contextsTrie != NULL) { 50 pthread_mutex_unlock(&g_mutex); 51 return 0; 52 } 53 int res = ParameterContextsLoad(isInit); 54 pthread_mutex_unlock(&g_mutex); 55 return res; 56 } 57 DestroyParamList(ParamContextsList ** list)58void DestroyParamList(ParamContextsList **list) 59 { 60 if (list == NULL) { 61 return; 62 } 63 ParamContextsList *tmpNode; 64 ParamContextsList *listHead = *list; 65 while (listHead != NULL) { 66 tmpNode = listHead->next; 67 free(listHead); 68 listHead = tmpNode; 69 } 70 *list = NULL; 71 return; 72 } 73 GetParamList()74ParamContextsList *GetParamList() 75 { 76 if (g_contextsList == NULL) { 77 return NULL; 78 } 79 return g_contextsList; 80 } 81 GetParamLabel(const char * paraName)82const char *GetParamLabel(const char *paraName) 83 { 84 if (paraName == NULL || g_contextsTrie == NULL) { 85 return DEFAULT_CONTEXT; 86 } 87 return SearchFromParamTrie(g_contextsTrie, paraName); 88 } 89 GetParamLabelIndex(const char * paraName)90int GetParamLabelIndex(const char *paraName) 91 { 92 if ((paraName == NULL) || (g_contextsTrie == NULL)) { 93 return INVALID_INDEX; 94 } 95 return GetLabelIndex(g_contextsTrie, paraName); 96 } 97