1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3  * Copyright (c) 2020, Open Mobile Platform LLC
4  */
5 
6 #include <assert.h>
7 #include <ctype.h>
8 #include <dlfcn.h>
9 #include <dirent.h>
10 #include <plugin.h>
11 #include <stdio.h>
12 #include <stddef.h>
13 #include <stdint.h>
14 #include <stdlib.h>
15 #include <string.h>
16 #include <tee_client_api.h>
17 #include <teec_trace.h>
18 #include <tee_supplicant.h>
19 
20 #include "optee_msg_supplicant.h"
21 
22 #ifndef __aligned
23 #define __aligned(x) __attribute__((__aligned__(x)))
24 #endif
25 #include <linux/tee.h>
26 
27 /* internal possible returned values */
28 enum plugin_err {
29 	PLUGIN_OK = 0,
30 	PLUGIN_DL_OPEN_ERR = -1,
31 	PLUGIN_DL_SYM_ERR = -2,
32 };
33 
34 static struct plugin *plugin_list_head;
35 
36 /* returns 0, if u1 and u2 are equal */
uuid_cmp(TEEC_UUID * u1,TEEC_UUID * u2)37 static int uuid_cmp(TEEC_UUID *u1, TEEC_UUID *u2)
38 {
39 	if (!memcmp(u1, u2, sizeof(TEEC_UUID)))
40 		return 0;
41 
42 	return 1;
43 }
44 
uuid_from_octets(TEEC_UUID * d,const uint8_t s[TEE_IOCTL_UUID_LEN])45 static void uuid_from_octets(TEEC_UUID *d, const uint8_t s[TEE_IOCTL_UUID_LEN])
46 {
47 	d->timeLow = ((uint32_t)s[0] << 24) | ((uint32_t)s[1] << 16) |
48 		((uint32_t)s[2] << 8) | s[3];
49 	d->timeMid = ((uint32_t)s[4] << 8) | s[5];
50 	d->timeHiAndVersion = ((uint32_t)s[6] << 8) | s[7];
51 	memcpy(d->clockSeqAndNode, s + 8, sizeof(d->clockSeqAndNode));
52 }
53 
push_plugin(struct plugin * p)54 static void push_plugin(struct plugin *p)
55 {
56 	p->next = plugin_list_head;
57 	plugin_list_head = p;
58 }
59 
find_plugin(TEEC_UUID * u)60 static struct plugin *find_plugin(TEEC_UUID *u)
61 {
62 	struct plugin *p = plugin_list_head;
63 
64 	while (p) {
65 		if (!uuid_cmp(&p->method->uuid, u))
66 			return p;
67 
68 		p = p->next;
69 	}
70 
71 	return NULL;
72 }
73 
load_plugin(const char * name,struct plugin * p)74 static enum plugin_err load_plugin(const char *name, struct plugin *p)
75 {
76 	void *handle = NULL;
77 	struct plugin_method *m = NULL;
78 
79 	handle = dlopen(name, RTLD_LAZY);
80 	if (!handle)
81 		return PLUGIN_DL_OPEN_ERR;
82 
83 	p->handle = handle;
84 
85 	m = (struct plugin_method *)dlsym(handle, "plugin_method");
86 	if (!m || !m->name || !m->invoke)
87 		return PLUGIN_DL_SYM_ERR;
88 
89 	p->method = m;
90 
91 	return PLUGIN_OK;
92 }
93 
plugin_invoke(TEEC_UUID * u,unsigned int cmd,unsigned int sub_cmd,void * data,size_t in_len,size_t * out_len)94 static TEEC_Result plugin_invoke(TEEC_UUID *u, unsigned int cmd,
95 				 unsigned int sub_cmd, void *data,
96 				 size_t in_len, size_t *out_len)
97 {
98 	struct plugin *p = NULL;
99 
100 	p = find_plugin(u);
101 	if (!p)
102 		return TEEC_ERROR_ITEM_NOT_FOUND;
103 
104 	assert(p->method->invoke);
105 
106 	return p->method->invoke(cmd, sub_cmd, data, in_len, out_len);
107 }
108 
plugin_load_all(void)109 TEEC_Result plugin_load_all(void)
110 {
111 	DIR *dir = NULL;
112 	enum plugin_err res = PLUGIN_OK;
113 	TEEC_Result teec_res = TEEC_SUCCESS;
114 	struct dirent *entry = NULL;
115 
116 	dir = opendir(TEE_PLUGIN_LOAD_PATH);
117 	if (!dir) {
118 		IMSG("could not open directory %s", TEE_PLUGIN_LOAD_PATH);
119 
120 		/* don't generate error if there is no the dir */
121 		return TEEC_SUCCESS;
122 	}
123 
124 	while ((entry = readdir(dir))) {
125 		struct plugin *p;
126 
127 		if (!strcmp(entry->d_name, "..") || !strcmp(entry->d_name, "."))
128 			continue;
129 
130 		p = calloc(1, sizeof(struct plugin));
131 		if (!p) {
132 			EMSG("allocate mem for plugin <%s> failed",
133 			     entry->d_name);
134 			closedir(dir);
135 			return TEEC_ERROR_OUT_OF_MEMORY;
136 		}
137 
138 		res = load_plugin((const char *)entry->d_name, p);
139 		switch (res) {
140 		case PLUGIN_DL_OPEN_ERR:
141 			EMSG("open plugin <%s> failed: %s",
142 			     entry->d_name, dlerror());
143 			free(p);
144 			continue;
145 		case PLUGIN_DL_SYM_ERR:
146 			EMSG("find 'plugin_method' sym in <%s> failed: %s",
147 			     entry->d_name, dlerror());
148 			free(p);
149 			continue;
150 		default:
151 			DMSG("loading the <%s> plugin were successful",
152 			     p->method->name);
153 			break;
154 		}
155 
156 		/* Init the plugin */
157 		if (p->method->init) {
158 			teec_res = p->method->init();
159 			if (teec_res) {
160 				EMSG("init the <%s> plugin failed with 0x%x",
161 				     p->method->name, teec_res);
162 				free(p);
163 				continue;
164 			}
165 		}
166 
167 		push_plugin(p);
168 	}
169 
170 	closedir(dir);
171 	return TEEC_SUCCESS;
172 }
173 
plugin_process(size_t num_params,struct tee_ioctl_param * params)174 TEEC_Result plugin_process(size_t num_params, struct tee_ioctl_param *params)
175 {
176 	unsigned int cmd = 0;
177 	unsigned int sub_cmd = 0;
178 	void *data = NULL;
179 	uint32_t data_len = 0;
180 	TEEC_UUID uuid = { };
181 	uint32_t uuid_words[4] = { };
182 	size_t outlen = 0;
183 	TEEC_Result res = TEEC_ERROR_NOT_SUPPORTED;
184 
185 	if (num_params != 4 ||
186 	    (params[0].attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) !=
187 		    TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT ||
188 	    (params[1].attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) !=
189 		    TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INPUT ||
190 	    (params[2].attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) !=
191 		    TEE_IOCTL_PARAM_ATTR_TYPE_VALUE_INOUT ||
192 	    (params[3].attr & TEE_IOCTL_PARAM_ATTR_TYPE_MASK) !=
193 		    TEE_IOCTL_PARAM_ATTR_TYPE_MEMREF_INOUT)
194 		return TEEC_ERROR_BAD_PARAMETERS;
195 
196 	uuid_words[0] = params[0].b;
197 	uuid_words[1] = params[0].c;
198 	uuid_words[2] = params[1].a;
199 	uuid_words[3] = params[1].b;
200 
201 	uuid_from_octets(&uuid, (const uint8_t *)uuid_words);
202 
203 	cmd = params[1].c;
204 	sub_cmd = params[2].a;
205 
206 	data = tee_supp_param_to_va(params + 3);
207 	data_len = MEMREF_SIZE(params + 3);
208 
209 	if (data_len && !data)
210 		return TEEC_ERROR_BAD_PARAMETERS;
211 
212 	switch (params[0].a) {
213 	case OPTEE_INVOKE_PLUGIN:
214 		res = plugin_invoke(&uuid, cmd, sub_cmd, data, data_len,
215 				    &outlen);
216 		params[2].b = outlen;
217 	default:
218 		break;
219 	}
220 
221 	return res;
222 }
223