1 /*
2 * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3 * Licensed under the Mulan PSL v2.
4 * You can use this software according to the terms and conditions of the Mulan PSL v2.
5 * You may obtain a copy of Mulan PSL v2 at:
6 * http://license.coscl.org.cn/MulanPSL2
7 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9 * PURPOSE.
10 * See the Mulan PSL v2 for more details.
11 */
12 #include "drv_auth.h"
13 #include <stdio.h>
14 #include <string.h>
15 #include <inttypes.h>
16 #include <tee_log.h>
17 #include <tee_drv_internal.h>
18 #include "drv_dyn_conf_mgr.h"
19 #include "drvcall_dyn_conf_mgr.h"
20
get_mac_perm_by_uuid(const struct drv_conf_t * drv_conf,const struct tee_uuid * srv_uuid,uint64_t * mac_perm)21 static int32_t get_mac_perm_by_uuid(const struct drv_conf_t *drv_conf, const struct tee_uuid *srv_uuid,
22 uint64_t *mac_perm)
23 {
24 uint32_t i;
25
26 for (i = 0; i < drv_conf->mac_info_list_size; i++) {
27 struct drv_mac_info_t drv_mac_info = drv_conf->mac_info_list[i];
28
29 if (memcmp(&drv_mac_info.uuid, srv_uuid, sizeof(struct tee_uuid)) == 0) {
30 if (mac_perm != NULL)
31 *mac_perm = drv_mac_info.perm;
32 return TEE_SUCCESS;
33 }
34 }
35 tloge("cannot find uuid %08x-%04x-%04x in mac list\n",
36 srv_uuid->timeLow, srv_uuid->timeMid, srv_uuid->timeHiAndVersion);
37 return TEE_ERROR_GENERIC;
38 }
39
get_perm_by_service_name_in_perm_apply_list(const struct task_tlv * tlv,const char * service_name,uint32_t service_name_size,uint64_t * perm)40 static int32_t get_perm_by_service_name_in_perm_apply_list(const struct task_tlv *tlv,
41 const char *service_name, uint32_t service_name_size,
42 uint64_t *perm)
43 {
44 uint32_t i;
45
46 for (i = 0; i < tlv->drvcall_perm_apply_list_size; i++) {
47 struct drvcall_perm_apply_item_t item = tlv->drvcall_perm_apply_list[i];
48 if (item.name_size != service_name_size)
49 continue;
50
51 if (strncmp(item.name, service_name, item.name_size) == 0) {
52 if (perm != NULL)
53 *perm = item.perm;
54 return TEE_SUCCESS;
55 }
56 }
57
58 tloge("cannot find service name %s in perm apply list\n", service_name);
59 return TEE_ERROR_GENERIC;
60 }
61
caller_open_auth_check(const struct task_node * call_node,const char * drv_name,uint32_t name_len)62 bool caller_open_auth_check(const struct task_node *call_node, const char *drv_name, uint32_t name_len)
63 {
64 if (call_node == NULL || drv_name == NULL || name_len == 0) {
65 tloge("invalid param\n");
66 return false;
67 }
68
69 /* check if TA have registed drv name in perm apply list */
70 if (get_perm_by_service_name_in_perm_apply_list(&call_node->tlv, drv_name, name_len, NULL) != TEE_SUCCESS)
71 return false;
72
73 return true;
74 }
75
drv_mac_open_auth_check(const struct drv_conf_t * drv_conf,const struct tee_uuid * uuid)76 bool drv_mac_open_auth_check(const struct drv_conf_t *drv_conf, const struct tee_uuid *uuid)
77 {
78 if (uuid == NULL || drv_conf == NULL) {
79 tloge("invalid uuid or drv_conf\n");
80 return false;
81 }
82
83 /* if drv not have mac list, all TA can open */
84 if (drv_conf->mac_info_list_size == 0)
85 return true;
86
87 /* check if TA in drv's mac list */
88 if (get_mac_perm_by_uuid(drv_conf, uuid, NULL) == TEE_SUCCESS)
89 return true;
90
91 return false;
92 }
93
get_drvcaller_cmd_perm(const struct task_node * call_node,const struct task_node * dnode,uint64_t * perm)94 int32_t get_drvcaller_cmd_perm(const struct task_node *call_node, const struct task_node *dnode, uint64_t *perm)
95 {
96 if (call_node == NULL || dnode == NULL || dnode->tlv.drv_conf == NULL || perm == NULL) {
97 tloge("invalid param\n");
98 return -1;
99 }
100
101 int32_t ret;
102 if (dnode->tlv.drv_conf->mac_info_list_size == 0) {
103 tlogd("no mac perm, use drvcaller perm\n");
104 ret = get_perm_by_service_name_in_perm_apply_list(&call_node->tlv,
105 dnode->tlv.drv_conf->mani.service_name, dnode->tlv.drv_conf->mani.service_name_size, perm);
106 return ret;
107 }
108
109 ret = get_mac_perm_by_uuid(dnode->tlv.drv_conf, &call_node->tlv.uuid, perm);
110 return ret;
111 }
112