• 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 #include <ctype.h>
16 #include <limits.h>
17 
18 #include "param_manager.h"
19 #include "param_trie.h"
20 #include "param_base.h"
21 
GenerateKeyHasCode(const char * buff,size_t len)22 INIT_LOCAL_API int GenerateKeyHasCode(const char *buff, size_t len)
23 {
24     int code = 0;
25     for (size_t i = 0; i < len; i++) {
26         code += buff[i] - 'A';
27     }
28     return code;
29 }
30 
GetParamHandle(const WorkSpace * workSpace,uint32_t index,const char * name)31 INIT_LOCAL_API ParamHandle GetParamHandle(const WorkSpace *workSpace, uint32_t index, const char *name)
32 {
33     (void)name;
34     PARAM_CHECK(workSpace != NULL && workSpace->area != NULL, return -1, "Invalid param");
35     uint32_t hashCode = (uint32_t)GenerateKeyHasCode(workSpace->fileName, strlen(workSpace->fileName));
36     uint32_t handle = (hashCode % HASH_BUTT) << 24; // 24 left shift
37     handle = handle | (index + workSpace->area->startIndex);
38     return handle;
39 }
40 
GetWorkSpace(const char * name)41 INIT_LOCAL_API WorkSpace *GetWorkSpace(const char *name)
42 {
43     ParamWorkSpace *paramSpace = GetParamWorkSpace();
44     PARAM_CHECK(paramSpace != NULL, return NULL, "Invalid paramSpace");
45     PARAM_CHECK(paramSpace->workSpaceHashHandle != NULL, return NULL, "Invalid workSpaceHashHandle");
46     char *tmpName = (char *)name;
47 #ifndef PARAM_SUPPORT_SELINUX
48     tmpName = WORKSPACE_NAME_NORMAL;
49 #else
50     if (strcmp(name, WORKSPACE_NAME_DAC) != 0) {
51         tmpName = (paramSpace->selinuxSpace.getParamLabel != NULL) ?
52             (char *)paramSpace->selinuxSpace.getParamLabel(name) : WORKSPACE_NAME_NORMAL;
53     }
54 #endif
55     WorkSpace *space = NULL;
56     WORKSPACE_RD_LOCK(*paramSpace);
57     HashNode *node = OH_HashMapGet(paramSpace->workSpaceHashHandle, (const void *)tmpName);
58     if (node != NULL) {
59         space = HASHMAP_ENTRY(node, WorkSpace, hashNode);
60     }
61     WORKSPACE_RW_UNLOCK(*paramSpace);
62     return (space != NULL && space->area != NULL) ? space : NULL;
63 }
64 
CompareIndex(const HashNode * node,const void * key)65 static int CompareIndex(const HashNode *node, const void *key)
66 {
67     WorkSpace *workSpace = HASHMAP_ENTRY(node, WorkSpace, hashNode);
68     if (workSpace == NULL || workSpace->area == NULL) {
69         return 1;
70     }
71     uint32_t index = *(uint32_t *)key;
72     if (workSpace->area->startIndex <= index && index < (workSpace->area->startIndex + workSpace->area->dataSize)) {
73         return 0;
74     }
75     return 1;
76 }
77 
GetTrieNodeByHandle(ParamHandle handle)78 INIT_LOCAL_API ParamTrieNode *GetTrieNodeByHandle(ParamHandle handle)
79 {
80     ParamWorkSpace *paramSpace = GetParamWorkSpace();
81     PARAM_CHECK(paramSpace != NULL, return NULL, "Invalid paramSpace");
82     PARAM_ONLY_CHECK(handle != (ParamHandle)-1, return NULL);
83     int hashCode = ((handle >> 24) & 0x000000ff);  // 24 left shift
84     uint32_t index = handle & 0x00ffffff;
85     WORKSPACE_RD_LOCK(*paramSpace);
86     HashNode *node = OH_HashMapFind(paramSpace->workSpaceHashHandle, hashCode, (const void *)&index, CompareIndex);
87     if (node == NULL) {
88         WORKSPACE_RW_UNLOCK(*paramSpace);
89         PARAM_LOGV("Failed to get workspace for 0x%x index %d hashCode %d", handle, index, hashCode);
90         return NULL;
91     }
92     WorkSpace *workSpace = HASHMAP_ENTRY(node, WorkSpace, hashNode);
93     WORKSPACE_RW_UNLOCK(*paramSpace);
94     index = index - workSpace->area->startIndex;
95     if (PARAM_IS_ALIGNED(index)) {
96         return (ParamTrieNode *)GetTrieNode(workSpace, index);
97     }
98     return NULL;
99 }
100 
GetFirstWorkSpace(void)101 INIT_LOCAL_API WorkSpace *GetFirstWorkSpace(void)
102 {
103     ParamWorkSpace *paramSpace = GetParamWorkSpace();
104     PARAM_CHECK(paramSpace != NULL, return NULL, "Invalid paramSpace");
105 
106     WorkSpace *workSpace = NULL;
107     WORKSPACE_RD_LOCK(*paramSpace);
108     ListNode *node = paramSpace->workSpaceList.next;
109     if (node != &paramSpace->workSpaceList) {
110         workSpace = HASHMAP_ENTRY(node, WorkSpace, node);
111     }
112     WORKSPACE_RW_UNLOCK(*paramSpace);
113     return workSpace;
114 }
115 
GetNextWorkSpace(WorkSpace * curr)116 INIT_LOCAL_API WorkSpace *GetNextWorkSpace(WorkSpace *curr)
117 {
118     ParamWorkSpace *paramSpace = GetParamWorkSpace();
119     PARAM_CHECK(paramSpace != NULL, return NULL, "Invalid paramSpace");
120     PARAM_CHECK(curr != NULL, return NULL, "Invalid curr");
121     WorkSpace *workSpace = NULL;
122     WORKSPACE_RD_LOCK(*paramSpace);
123     ListNode *node = curr->node.next;
124     if (node != &paramSpace->workSpaceList) {
125         workSpace = HASHMAP_ENTRY(node, WorkSpace, node);
126     }
127     WORKSPACE_RW_UNLOCK(*paramSpace);
128     return workSpace;
129 }
130 
ReadParamWithCheck(const char * name,uint32_t op,ParamHandle * handle)131 INIT_LOCAL_API int ReadParamWithCheck(const char *name, uint32_t op, ParamHandle *handle)
132 {
133     ParamWorkSpace *paramSpace = GetParamWorkSpace();
134     PARAM_CHECK(paramSpace != NULL, return -1, "Invalid paramSpace");
135     PARAM_WORKSPACE_CHECK(paramSpace, return -1, "Invalid space");
136     PARAM_CHECK(handle != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid param handle");
137     PARAM_CHECK(name != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid param name");
138     *handle = -1;
139     int ret = CheckParamPermission(GetParamSecurityLabel(), name, op);
140     PARAM_CHECK(ret == 0, return ret, "Forbid to access parameter %s", name);
141 
142     WorkSpace *space = GetWorkSpace(name);
143     PARAM_CHECK(space != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid workSpace");
144     ParamTrieNode *node = FindTrieNode(space, name, strlen(name), NULL);
145     if (node != NULL && node->dataIndex != 0) {
146         *handle = GetParamHandle(space, node->dataIndex, name);
147         PARAM_LOGV("ReadParamWithCheck %s 0x%x %d", name, *handle, node->dataIndex);
148         return 0;
149     } else if (node != NULL) {
150         return PARAM_CODE_NODE_EXIST;
151     }
152     return PARAM_CODE_NOT_FOUND;
153 }
154 
GetParamValueType(const char * name)155 INIT_LOCAL_API uint8_t GetParamValueType(const char *name)
156 {
157     uint32_t labelIndex = 0;
158     WorkSpace *space = GetWorkSpace(WORKSPACE_NAME_DAC);
159     if (space == NULL) {
160         return PARAM_TYPE_STRING;
161     }
162     (void)FindTrieNode(space, name, strlen(name), &labelIndex);
163     ParamSecurityNode *securityNode = (ParamSecurityNode *)GetTrieNode(space, labelIndex);
164     if (securityNode == NULL) {
165         return PARAM_TYPE_STRING;
166     }
167     return securityNode->type;
168 }
169 
CheckParamValueType(const char * name,const char * value,uint8_t paramType)170 static int CheckParamValueType(const char *name, const char *value, uint8_t paramType)
171 {
172     (void)name;
173     if (paramType == PARAM_TYPE_INT) {
174         long long int temp1 = 0;
175         if (strlen(value) > 1 && value[0] == '-' && StringToLL(value, &temp1) != 0) {
176             PARAM_LOGE("Illegal param value %s for int", value);
177             return PARAM_CODE_INVALID_VALUE;
178         }
179         unsigned long long int temp2 = 0;
180         if (StringToULL(value, &temp2) != 0) {
181             PARAM_LOGE("Illegal param value %s for int", value);
182             return PARAM_CODE_INVALID_VALUE;
183         }
184     } else if (paramType == PARAM_TYPE_BOOL) {
185         static const char *validValue[] = {
186             "1", "0", "true", "false", "y", "yes", "on", "off", "n", "no"
187         };
188         size_t i = 0;
189         for (; i < ARRAY_LENGTH(validValue); i++) {
190             if (strcasecmp(validValue[i], value) == 0) {
191                 break;
192             }
193         }
194         if (i >= ARRAY_LENGTH(validValue)) {
195             PARAM_LOGE("Illegal param value %s for bool", value);
196             return PARAM_CODE_INVALID_VALUE;
197         }
198     }
199     return 0;
200 }
201 
CheckParamValue(const ParamTrieNode * node,const char * name,const char * value,uint8_t paramType)202 INIT_LOCAL_API int CheckParamValue(const ParamTrieNode *node, const char *name, const char *value, uint8_t paramType)
203 {
204     if (IS_READY_ONLY(name)) {
205         PARAM_CHECK(strlen(value) < PARAM_CONST_VALUE_LEN_MAX,
206             return PARAM_CODE_INVALID_VALUE, "Illegal param value %s", value);
207         if (node != NULL && node->dataIndex != 0) {
208             PARAM_LOGE("Read-only param was already set %s", name);
209             return PARAM_CODE_READ_ONLY;
210         }
211     } else {
212         PARAM_CHECK(strlen(value) < GetParamMaxLen(paramType),
213             return PARAM_CODE_INVALID_VALUE, "Illegal param value %s length", value);
214     }
215     return CheckParamValueType(name, value, paramType);
216 }
217 
CheckParamName(const char * name,int info)218 INIT_LOCAL_API int CheckParamName(const char *name, int info)
219 {
220     PARAM_CHECK(name != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid param");
221     size_t nameLen = strlen(name);
222     if (nameLen >= PARAM_NAME_LEN_MAX) {
223         return PARAM_CODE_INVALID_NAME;
224     }
225     if (strcmp(name, "#") == 0) {
226         return 0;
227     }
228 
229     if (nameLen < 1 || name[0] == '.' || (!info && name[nameLen - 1] == '.')) {
230         PARAM_LOGE("CheckParamName %s %d", name, info);
231         return PARAM_CODE_INVALID_NAME;
232     }
233 
234     /* Only allow alphanumeric, plus '.', '-', '@', ':', or '_' */
235     /* Don't allow ".." to appear in a param name */
236     for (size_t i = 0; i < nameLen; i++) {
237         if (name[i] == '.') {
238             if (name[i - 1] == '.') {
239                 return PARAM_CODE_INVALID_NAME;
240             }
241             continue;
242         }
243         if (name[i] == '_' || name[i] == '-' || name[i] == '@' || name[i] == ':') {
244             continue;
245         }
246         if (isalnum(name[i])) {
247             continue;
248         }
249         return PARAM_CODE_INVALID_NAME;
250     }
251     return 0;
252 }
253 
AddParam(WorkSpace * workSpace,uint8_t type,const char * name,const char * value,uint32_t * dataIndex)254 static int AddParam(WorkSpace *workSpace, uint8_t type, const char *name, const char *value, uint32_t *dataIndex)
255 {
256     ParamTrieNode *node = AddTrieNode(workSpace, name, strlen(name));
257     PARAM_CHECK(node != NULL, return PARAM_CODE_REACHED_MAX,
258         "Failed to add node name %s space %s", name, workSpace->fileName);
259     ParamNode *entry = (ParamNode *)GetTrieNode(workSpace, node->dataIndex);
260     if (entry == NULL) {
261         uint32_t offset = AddParamNode(workSpace, type, name, strlen(name), value, strlen(value));
262         PARAM_CHECK(offset > 0, return PARAM_CODE_REACHED_MAX,
263             "Failed to allocate name %s space %s", name, workSpace->fileName);
264         SaveIndex(&node->dataIndex, offset);
265         long long globalCommitId = ATOMIC_LOAD_EXPLICIT(&workSpace->area->commitId, memory_order_relaxed);
266         ATOMIC_STORE_EXPLICIT(&workSpace->area->commitId, ++globalCommitId, memory_order_release);
267 #ifdef PARAM_SUPPORT_SELINUX
268         WorkSpace *space = GetWorkSpace(WORKSPACE_NAME_DAC);
269         if (space != NULL && space != workSpace) { // dac commit is global commit
270             globalCommitId = ATOMIC_LOAD_EXPLICIT(&space->area->commitId, memory_order_relaxed);
271             ATOMIC_STORE_EXPLICIT(&space->area->commitId, ++globalCommitId, memory_order_release);
272         }
273 #endif
274     }
275     if (dataIndex != NULL) {
276         *dataIndex = node->dataIndex;
277     }
278     PARAM_LOGV("AddParam name %s value: %s", name, value);
279     return 0;
280 }
281 
UpdateParam(const WorkSpace * workSpace,uint32_t * dataIndex,const char * name,const char * value)282 static int UpdateParam(const WorkSpace *workSpace, uint32_t *dataIndex, const char *name, const char *value)
283 {
284     ParamNode *entry = (ParamNode *)GetTrieNode(workSpace, *dataIndex);
285     PARAM_CHECK(entry != NULL, return PARAM_CODE_REACHED_MAX, "Failed to update param value %s %u", name, *dataIndex);
286     PARAM_CHECK(entry->keyLength == strlen(name), return PARAM_CODE_INVALID_NAME, "Failed to check name len %s", name);
287 
288     uint32_t valueLen = strlen(value);
289     uint32_t commitId = ATOMIC_LOAD_EXPLICIT(&entry->commitId, memory_order_relaxed);
290     ATOMIC_STORE_EXPLICIT(&entry->commitId, commitId | PARAM_FLAGS_MODIFY, memory_order_relaxed);
291     long long globalCommitId = ATOMIC_LOAD_EXPLICIT(&workSpace->area->commitId, memory_order_relaxed);
292     if (entry->valueLength < PARAM_VALUE_LEN_MAX && valueLen < PARAM_VALUE_LEN_MAX) {
293         int ret = ParamMemcpy(entry->data + entry->keyLength + 1, PARAM_VALUE_LEN_MAX, value, valueLen + 1);
294         PARAM_CHECK(ret == 0, return PARAM_CODE_INVALID_VALUE, "Failed to copy value");
295         entry->valueLength = valueLen;
296     }
297     uint32_t flags = commitId & ~PARAM_FLAGS_COMMITID;
298     ATOMIC_STORE_EXPLICIT(&entry->commitId, (++commitId) | flags, memory_order_release);
299     ATOMIC_STORE_EXPLICIT(&workSpace->area->commitId, ++globalCommitId, memory_order_release);
300 #ifdef PARAM_SUPPORT_SELINUX
301     WorkSpace *space = GetWorkSpace(WORKSPACE_NAME_DAC);
302     if (space != NULL && space != workSpace) { // dac commit is global commit
303         globalCommitId = ATOMIC_LOAD_EXPLICIT(&space->area->commitId, memory_order_relaxed);
304         ATOMIC_STORE_EXPLICIT(&space->area->commitId, ++globalCommitId, memory_order_release);
305     }
306 #endif
307     PARAM_LOGV("UpdateParam name %s value: %s", name, value);
308     futex_wake(&entry->commitId, INT_MAX);
309     return 0;
310 }
311 
WriteParam(const char * name,const char * value,uint32_t * dataIndex,int mode)312 INIT_LOCAL_API int WriteParam(const char *name, const char *value, uint32_t *dataIndex, int mode)
313 {
314     PARAM_LOGV("WriteParam %s", name);
315     ParamWorkSpace *paramSpace = GetParamWorkSpace();
316     PARAM_CHECK(paramSpace != NULL, return -1, "Invalid paramSpace");
317     PARAM_WORKSPACE_CHECK(paramSpace, return -1, "Invalid space");
318     PARAM_CHECK(value != NULL && name != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid name or value");
319     WorkSpace *workSpace = GetWorkSpace(name);
320     PARAM_CHECK(workSpace != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid workSpace");
321     ParamTrieNode *node = FindTrieNode(workSpace, name, strlen(name), NULL);
322     int ret = 0;
323     if (node != NULL && node->dataIndex != 0) {
324         if (dataIndex != NULL) {
325             *dataIndex = node->dataIndex;
326         }
327         if ((mode & LOAD_PARAM_ONLY_ADD) == LOAD_PARAM_ONLY_ADD) {
328             return PARAM_CODE_READ_ONLY;
329         }
330         ParamNode *entry = (ParamNode *)GetTrieNode(workSpace, node->dataIndex);
331         PARAM_CHECK(entry != NULL, return PARAM_CODE_REACHED_MAX,
332             "Failed to update param value %s %u", name, node->dataIndex);
333         // use save type to check value
334         ret = CheckParamValue(node, name, value, entry->type);
335         PARAM_CHECK(ret == 0, return ret, "Invalid param value param: %s=%s", name, value);
336         PARAMSPACE_AREA_RW_LOCK(workSpace);
337         ret = UpdateParam(workSpace, &node->dataIndex, name, value);
338         PARAMSPACE_AREA_RW_UNLOCK(workSpace);
339     } else {
340         uint8_t type = GetParamValueType(name);
341         ret = CheckParamValue(node, name, value, type);
342         PARAM_CHECK(ret == 0, return ret, "Invalid param value param: %s=%s", name, value);
343         PARAMSPACE_AREA_RW_LOCK(workSpace);
344         ret = AddParam((WorkSpace *)workSpace, type, name, value, dataIndex);
345         PARAMSPACE_AREA_RW_UNLOCK(workSpace);
346     }
347     return ret;
348 }
349 
AddSecurityLabel(const ParamAuditData * auditData)350 INIT_LOCAL_API int AddSecurityLabel(const ParamAuditData *auditData)
351 {
352     ParamWorkSpace *paramSpace = GetParamWorkSpace();
353     PARAM_CHECK(paramSpace != NULL, return -1, "Invalid paramSpace");
354     PARAM_WORKSPACE_CHECK(paramSpace, return -1, "Invalid space");
355     PARAM_CHECK(auditData != NULL && auditData->name != NULL, return -1, "Invalid auditData");
356     WorkSpace *workSpace = GetWorkSpace(WORKSPACE_NAME_DAC);
357     PARAM_CHECK(workSpace != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid workSpace");
358     int ret = CheckParamName(auditData->name, 1);
359     PARAM_CHECK(ret == 0, return ret, "Illegal param name \"%s\"", auditData->name);
360 
361     ParamTrieNode *node = FindTrieNode(workSpace, auditData->name, strlen(auditData->name), NULL);
362     if (node == NULL) {
363         node = AddTrieNode(workSpace, auditData->name, strlen(auditData->name));
364     }
365     PARAM_CHECK(node != NULL, return PARAM_CODE_REACHED_MAX, "Failed to add node %s", auditData->name);
366     uint32_t offset = node->labelIndex;
367     if (node->labelIndex == 0) {  // can not support update for label
368         offset = AddParamSecurityNode(workSpace, auditData);
369         PARAM_CHECK(offset != 0, return PARAM_CODE_REACHED_MAX, "Failed to add label");
370         SaveIndex(&node->labelIndex, offset);
371     } else {
372 #ifdef STARTUP_INIT_TEST
373         ParamSecurityNode *label = (ParamSecurityNode *)GetTrieNode(workSpace, node->labelIndex);
374         PARAM_CHECK(label != NULL, return -1, "Failed to get trie node");
375         label->mode = auditData->dacData.mode;
376         label->uid = auditData->dacData.uid;
377         label->gid = auditData->dacData.gid;
378         label->type = auditData->dacData.paramType & PARAM_TYPE_MASK;
379 #endif
380         PARAM_LOGE("Error, repeat to add label for name %s", auditData->name);
381     }
382     PARAM_LOGV("AddSecurityLabel label %d gid %d uid %d mode %o type:%d name: %s", offset,
383         auditData->dacData.gid, auditData->dacData.uid, auditData->dacData.mode,
384         auditData->dacData.paramType, auditData->name);
385     return 0;
386 }
387 
GetParamSecurityOps(int type)388 INIT_LOCAL_API ParamSecurityOps *GetParamSecurityOps(int type)
389 {
390     PARAM_CHECK(type < PARAM_SECURITY_MAX, return NULL, "Invalid type");
391     ParamWorkSpace *paramSpace = GetParamWorkSpace();
392     PARAM_CHECK(paramSpace != NULL, return NULL, "Invalid paramSpace");
393     return &paramSpace->paramSecurityOps[type];
394 }
395 
GetParamSecurityLabel()396 INIT_LOCAL_API ParamSecurityLabel *GetParamSecurityLabel()
397 {
398     ParamWorkSpace *paramSpace = GetParamWorkSpace();
399     PARAM_CHECK(paramSpace != NULL, return NULL, "Invalid paramSpace");
400 #if !(defined __LITEOS_A__ || defined __LITEOS_M__)
401 #ifndef STARTUP_INIT_TEST
402     paramSpace->securityLabel.cred.pid = getpid();
403     paramSpace->securityLabel.cred.uid = geteuid();
404     paramSpace->securityLabel.cred.gid = getegid();
405 #endif
406 #endif
407     return &paramSpace->securityLabel;
408 }
409 
SplitParamString(char * line,const char * exclude[],uint32_t count,int (* result)(const uint32_t * context,const char * name,const char * value),const uint32_t * context)410 INIT_LOCAL_API int SplitParamString(char *line, const char *exclude[], uint32_t count,
411     int (*result)(const uint32_t *context, const char *name, const char *value), const uint32_t *context)
412 {
413     // Skip spaces
414     char *name = line;
415     while (isspace(*name) && (*name != '\0')) {
416         name++;
417     }
418     // Empty line or Comment line
419     if (*name == '\0' || *name == '#') {
420         return 0;
421     }
422 
423     char *value = name;
424     // find the first delimiter '='
425     while (*value != '\0') {
426         if (*value == '=') {
427             (*value) = '\0';
428             value = value + 1;
429             break;
430         }
431         value++;
432     }
433 
434     // Skip spaces
435     char *tmp = name;
436     while ((tmp < value) && (*tmp != '\0')) {
437         if (isspace(*tmp)) {
438             (*tmp) = '\0';
439             break;
440         }
441         tmp++;
442     }
443 
444     // empty name, just ignore this line
445     if (*value == '\0') {
446         return 0;
447     }
448 
449     // Filter excluded parameters
450     for (uint32_t i = 0; i < count; i++) {
451         if (strncmp(name, exclude[i], strlen(exclude[i])) == 0) {
452             return 0;
453         }
454     }
455 
456     // Skip spaces for value
457     while (isspace(*value) && (*value != '\0')) {
458         value++;
459     }
460 
461     // Trim the ending spaces of value
462     char *pos = value + strlen(value);
463     pos--;
464     while (isspace(*pos) && pos > value) {
465         (*pos) = '\0';
466         pos--;
467     }
468 
469     // Strip starting and ending " for value
470     if ((*value == '"') && (pos > value) && (*pos == '"')) {
471         value = value + 1;
472         *pos = '\0';
473     }
474     return result(context, name, value);
475 }
476 
ReadCommitId(ParamNode * entry)477 INIT_LOCAL_API uint32_t ReadCommitId(ParamNode *entry)
478 {
479     uint32_t commitId = ATOMIC_LOAD_EXPLICIT(&entry->commitId, memory_order_acquire);
480     while (commitId & PARAM_FLAGS_MODIFY) {
481         futex_wait(&entry->commitId, commitId);
482         commitId = ATOMIC_LOAD_EXPLICIT(&entry->commitId, memory_order_acquire);
483     }
484     return commitId & PARAM_FLAGS_COMMITID;
485 }
486 
ReadParamName(ParamHandle handle,char * name,uint32_t length)487 INIT_LOCAL_API int ReadParamName(ParamHandle handle, char *name, uint32_t length)
488 {
489     ParamWorkSpace *paramSpace = GetParamWorkSpace();
490     PARAM_CHECK(paramSpace != NULL, return -1, "Invalid workspace");
491     PARAM_CHECK(name != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid param");
492     ParamNode *entry = (ParamNode *)GetTrieNodeByHandle(handle);
493     if (entry == NULL) {
494         return PARAM_CODE_NOT_FOUND;
495     }
496     PARAM_CHECK(length > entry->keyLength, return -1, "Invalid param size %u %u", entry->keyLength, length);
497     int ret = ParamMemcpy(name, length, entry->data, entry->keyLength);
498     PARAM_CHECK(ret == 0, return PARAM_CODE_INVALID_PARAM, "Failed to copy name");
499     name[entry->keyLength] = '\0';
500     return 0;
501 }
502 
CheckParamPermission(const ParamSecurityLabel * srcLabel,const char * name,uint32_t mode)503 INIT_LOCAL_API int CheckParamPermission(const ParamSecurityLabel *srcLabel, const char *name, uint32_t mode)
504 {
505     PARAM_CHECK(srcLabel != NULL, return DAC_RESULT_FORBIDED, "The srcLabel is null");
506     ParamWorkSpace *paramSpace = GetParamWorkSpace();
507     PARAM_CHECK(paramSpace != NULL, return DAC_RESULT_FORBIDED, "Invalid workspace");
508     int ret = DAC_RESULT_PERMISSION;
509     PARAM_LOGV("CheckParamPermission mode 0x%x name: %s uid:%d gid:%d pid:%d",
510         mode, name, (int)srcLabel->cred.uid, (int)srcLabel->cred.gid, (int)srcLabel->cred.pid);
511     // for root, all permission, but for appspawn must to check
512     if (srcLabel->cred.uid == 0 && srcLabel->cred.pid == 1) {
513         return DAC_RESULT_PERMISSION;
514     }
515     for (int i = 0; i < PARAM_SECURITY_MAX; i++) {
516         if (PARAM_TEST_FLAG(paramSpace->securityLabel.flags[i], LABEL_ALL_PERMISSION)) {
517             continue;
518         }
519         ParamSecurityOps *ops = GetParamSecurityOps(i);
520         if (ops == NULL) {
521             continue;
522         }
523         if (ops->securityCheckParamPermission == NULL) {
524             continue;
525         }
526         ret = ops->securityCheckParamPermission(srcLabel, name, mode);
527         if (ret == DAC_RESULT_FORBIDED) {
528             PARAM_LOGW("CheckParamPermission %s %s FORBID", ops->name, name);
529             break;
530         }
531     }
532     return ret;
533 }
534 
ParamSprintf(char * buffer,size_t buffSize,const char * format,...)535 INIT_LOCAL_API int ParamSprintf(char *buffer, size_t buffSize, const char *format, ...)
536 {
537     int len = -1;
538     va_list vargs;
539     va_start(vargs, format);
540 #ifdef PARAM_BASE
541     len = vsnprintf(buffer, buffSize - 1, format, vargs);
542 #else
543     len = vsnprintf_s(buffer, buffSize, buffSize - 1, format, vargs);
544 #endif
545     va_end(vargs);
546     return len;
547 }
548 
ParamMemcpy(void * dest,size_t destMax,const void * src,size_t count)549 INIT_LOCAL_API int ParamMemcpy(void *dest, size_t destMax, const void *src, size_t count)
550 {
551     int ret = 0;
552 #ifdef PARAM_BASE
553     memcpy(dest, src, count);
554 #else
555     ret = memcpy_s(dest, destMax, src, count);
556 #endif
557     return ret;
558 }
559 
ParamStrCpy(char * strDest,size_t destMax,const char * strSrc)560 INIT_LOCAL_API int ParamStrCpy(char *strDest, size_t destMax, const char *strSrc)
561 {
562     int ret = 0;
563 #ifdef PARAM_BASE
564     if (strlen(strSrc) >= destMax) {
565         return -1;
566     }
567     size_t i = 0;
568     while ((i < destMax) && *strSrc != '\0') {
569         *strDest = *strSrc;
570         strDest++;
571         strSrc++;
572         i++;
573     }
574     *strDest = '\0';
575 #else
576     ret = strcpy_s(strDest, destMax, strSrc);
577 #endif
578     return ret;
579 }