• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
13 #include "secfile_load_agent.h"
14 #include <limits.h>
15 #include <securec.h>
16 #include <sys/prctl.h>
17 #include "tc_ns_client.h"
18 #include "tee_log.h"
19 
20 #define MAX_PATH_LEN 256
21 #ifdef LOG_TAG
22 #undef LOG_TAG
23 #endif
24 #define LOG_TAG "teecd_agent"
25 #define MAX_BUFFER_LEN (8 * 1024 * 1024)
26 #define H_OFFSET                  32
27 int g_secLoadAgentFd = -1;
28 
GetSecLoadAgentFd(void)29 int GetSecLoadAgentFd(void)
30 {
31     return g_secLoadAgentFd;
32 }
33 
SetSecLoadAgentFd(int secLoadAgentFd)34 void SetSecLoadAgentFd(int secLoadAgentFd)
35 {
36     g_secLoadAgentFd = secLoadAgentFd;
37 }
38 
GetImgLen(FILE * fp,long * totalLlen)39 static int GetImgLen(FILE *fp, long *totalLlen)
40 {
41     int ret;
42 
43     ret = fseek(fp, 0, SEEK_END);
44     if (ret != 0) {
45         tloge("fseek error\n");
46         return -1;
47     }
48     *totalLlen = ftell(fp);
49     if (*totalLlen <= 0 || *totalLlen > MAX_BUFFER_LEN) {
50         tloge("file is not exist or size is too large, filesize = %" PUBLIC "ld\n", *totalLlen);
51         return -1;
52     }
53     ret = fseek(fp, 0, SEEK_SET);
54     if (ret != 0) {
55         tloge("fseek error\n");
56         return -1;
57     }
58     return ret;
59 }
60 
SecFileLoadWork(int tzFd,const char * filePath,enum SecFileType fileType,const TEEC_UUID * uuid)61 static int32_t SecFileLoadWork(int tzFd, const char *filePath, enum SecFileType fileType, const TEEC_UUID *uuid)
62 {
63     char realPath[PATH_MAX + 1] = { 0 };
64     FILE *fp                    = NULL;
65     int ret;
66 
67     if (tzFd < 0) {
68         tloge("fd of tzdriver is valid!\n");
69         return -1;
70     }
71     if (realpath(filePath, realPath) == NULL) {
72         tloge("realpath open file err=%" PUBLIC "d, filePath=%" PUBLIC "s\n", errno, filePath);
73         return -1;
74     }
75 
76 #ifndef TEE_LOAD_FROM_ROOTFS
77     if (strncmp(realPath, TEE_DEFAULT_PATH, strlen(TEE_DEFAULT_PATH)) != 0) {
78         tloge("realpath -%" PUBLIC "s- is invalid\n", realPath);
79         return -1;
80     }
81 #else
82     if (strncmp(realPath, TEE_DEFAULT_PATH_ROOTFS, strlen(TEE_DEFAULT_PATH_ROOTFS)) != 0) {
83         tloge("realpath -%" PUBLIC "s- is invalid\n", realPath);
84         return -1;
85     }
86 #endif
87 
88     fp = fopen(realPath, "r");
89     if (fp == NULL) {
90         tloge("open file err=%" PUBLIC "d, path=%" PUBLIC "s\n", errno, filePath);
91         return -1;
92     }
93     ret = LoadSecFile(tzFd, fp, fileType, uuid);
94     if (fp != NULL) {
95         fclose(fp);
96     }
97     return ret;
98 }
99 
100 // input param uuid may be NULL, so don need to check if uuid is NULL
LoadSecFile(int tzFd,FILE * fp,enum SecFileType fileType,const TEEC_UUID * uuid)101 int32_t LoadSecFile(int tzFd, FILE *fp, enum SecFileType fileType, const TEEC_UUID *uuid)
102 {
103     int32_t ret;
104     char *fileBuffer                   = NULL;
105     struct SecLoadIoctlStruct ioctlArg = {{ 0 }, { 0 }, { NULL } };
106 
107     if (tzFd < 0 || fp == NULL) {
108         tloge("param erro!\n");
109         return -1;
110     }
111 
112     do {
113         long totalLen = 0;
114         ret           = GetImgLen(fp, &totalLen);
115         if (ret != 0) {
116             break;
117         }
118 
119         if (totalLen <= 0 || totalLen > MAX_BUFFER_LEN) {
120             ret = -1;
121             tloge("totalLen is invalid\n");
122             break;
123         }
124 
125         /* alloc a less than 8M heap memory, it needn't slice. */
126         fileBuffer = malloc((size_t)totalLen);
127         if (fileBuffer == NULL) {
128             tloge("alloc TA file buffer(size=%" PUBLIC "ld) failed\n", totalLen);
129             ret = -1;
130             break;
131         }
132 
133         /* read total ta file to file buffer */
134         long fileSize = (long)fread(fileBuffer, 1, totalLen, fp);
135         if (fileSize != totalLen) {
136             tloge("read ta file failed, read size/total size=%" PUBLIC "ld/%" PUBLIC "ld\n", fileSize, totalLen);
137             ret = -1;
138             break;
139         }
140 
141         ioctlArg.secFileInfo.fileType = fileType;
142         ioctlArg.secFileInfo.fileSize = (uint32_t)totalLen;
143         ioctlArg.memref.file_addr = (uint32_t)(uintptr_t)fileBuffer;
144         ioctlArg.memref.file_h_addr = (uint32_t)(((uint64_t)(uintptr_t)fileBuffer) >> H_OFFSET);
145         if (uuid != NULL && memcpy_s((void *)(&ioctlArg.uuid), sizeof(ioctlArg.uuid), uuid, sizeof(*uuid)) != EOK) {
146             tloge("memcpy uuid fail\n");
147             break;
148         }
149 
150         ret = ioctl(tzFd, (int)TC_NS_CLIENT_IOCTL_LOAD_APP_REQ, &ioctlArg);
151         if (ret != 0) {
152             tloge("ioctl to load sec file failed, ret = 0x%" PUBLIC "x\n", ret);
153         }
154     } while (false);
155 
156     if (fileBuffer != NULL) {
157         free(fileBuffer);
158     }
159     return ret;
160 }
161 
IsTaLib(const TEEC_UUID * uuid)162 static bool IsTaLib(const TEEC_UUID *uuid)
163 {
164     char *chr = (char *)uuid;
165     uint32_t i;
166 
167     for (i = 0; i < sizeof(*uuid); i++) {
168         if (chr[i] != 0) {
169             return true;
170         }
171     }
172     return false;
173 }
174 
LoadLib(struct SecAgentControlType * secAgentControl)175 static void LoadLib(struct SecAgentControlType *secAgentControl)
176 {
177     int32_t ret;
178     char fname[MAX_PATH_LEN] = { 0 };
179 
180     if (secAgentControl == NULL) {
181         tloge("secAgentControl is null\n");
182         return;
183     }
184     if (strnlen(secAgentControl->LibSec.libName, MAX_SEC_FILE_NAME_LEN) >= MAX_SEC_FILE_NAME_LEN) {
185         tloge("libName is too long!\n");
186         secAgentControl->ret = -1;
187         return;
188     }
189 
190     if (IsTaLib(&(secAgentControl->LibSec.uuid))) {
191         ret =
192             snprintf_s(fname, sizeof(fname), MAX_PATH_LEN - 1,
193                 "%s/%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x%s.sec",
194                 TEE_DEFAULT_PATH, secAgentControl->LibSec.uuid.timeLow, secAgentControl->LibSec.uuid.timeMid,
195                 secAgentControl->LibSec.uuid.timeHiAndVersion, secAgentControl->LibSec.uuid.clockSeqAndNode[0],
196                 secAgentControl->LibSec.uuid.clockSeqAndNode[1], secAgentControl->LibSec.uuid.clockSeqAndNode[2],
197                 secAgentControl->LibSec.uuid.clockSeqAndNode[3], secAgentControl->LibSec.uuid.clockSeqAndNode[4],
198                 secAgentControl->LibSec.uuid.clockSeqAndNode[5], secAgentControl->LibSec.uuid.clockSeqAndNode[6],
199                 secAgentControl->LibSec.uuid.clockSeqAndNode[7], secAgentControl->LibSec.libName);
200     } else {
201         ret = snprintf_s(fname, sizeof(fname), MAX_PATH_LEN - 1,
202             "%s/%s.sec", TEE_DEFAULT_PATH, secAgentControl->LibSec.libName);
203     }
204     if (ret < 0) {
205         tloge("pack fname err\n");
206         secAgentControl->ret = -1;
207         return;
208     }
209     ret = SecFileLoadWork(g_secLoadAgentFd, (const char *)fname, LOAD_LIB, NULL);
210     if (ret != 0) {
211         tloge("teec load app failed\n");
212         secAgentControl->ret   = -1;
213         secAgentControl->error = errno;
214         return;
215     }
216     secAgentControl->ret = 0;
217     return;
218 }
219 
LoadTa(struct SecAgentControlType * secAgentControl)220 static void LoadTa(struct SecAgentControlType *secAgentControl)
221 {
222     int32_t ret;
223     char fname[MAX_PATH_LEN] = { 0 };
224 
225     if (secAgentControl == NULL) {
226         tloge("secAgentControl is null\n");
227         return;
228     }
229 
230     ret = snprintf_s(fname, sizeof(fname), MAX_PATH_LEN - 1, "%s/%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x.sec",
231                      TEE_DEFAULT_PATH, secAgentControl->TaSec.uuid.timeLow, secAgentControl->TaSec.uuid.timeMid,
232                      secAgentControl->TaSec.uuid.timeHiAndVersion, secAgentControl->TaSec.uuid.clockSeqAndNode[0],
233                      secAgentControl->TaSec.uuid.clockSeqAndNode[1], secAgentControl->TaSec.uuid.clockSeqAndNode[2],
234                      secAgentControl->TaSec.uuid.clockSeqAndNode[3], secAgentControl->TaSec.uuid.clockSeqAndNode[4],
235                      secAgentControl->TaSec.uuid.clockSeqAndNode[5], secAgentControl->TaSec.uuid.clockSeqAndNode[6],
236                      secAgentControl->TaSec.uuid.clockSeqAndNode[7]);
237     if (ret < 0) {
238         tloge("pack fname err\n");
239         secAgentControl->ret = -1;
240         return;
241     }
242     secAgentControl->ret = 0;
243     ret = SecFileLoadWork(g_secLoadAgentFd, (const char *)fname, LOAD_TA, &(secAgentControl->TaSec.uuid));
244     if (ret != 0) {
245         tloge("teec load TA app failed\n");
246         secAgentControl->ret   = ret;
247         secAgentControl->error = errno;
248         return;
249     }
250     return;
251 }
252 
SecLoadAgentWork(struct SecAgentControlType * secAgentControl)253 static void SecLoadAgentWork(struct SecAgentControlType *secAgentControl)
254 {
255     if (secAgentControl == NULL) {
256         tloge("secAgentControl is null\n");
257         return;
258     }
259     switch (secAgentControl->cmd) {
260         case LOAD_LIB_SEC:
261             LoadLib(secAgentControl);
262             break;
263         case LOAD_TA_SEC:
264             LoadTa(secAgentControl);
265             break;
266         case LOAD_SERVICE_SEC:
267         default:
268             tloge("gtask agent error cmd:%" PUBLIC "d\n", secAgentControl->cmd);
269             secAgentControl->ret = -1;
270             break;
271     }
272 }
273 
SecfileLoadAgentThread(void * control)274 void *SecfileLoadAgentThread(void *control)
275 {
276     (void)prctl(PR_SET_NAME, "teecd_sec_load_agent", 0, 0, 0);
277     struct SecAgentControlType *secAgentControl = NULL;
278     if (control == NULL) {
279         tloge("control is NULL\n");
280         return NULL;
281     }
282     secAgentControl = (struct SecAgentControlType *)control;
283     if (g_secLoadAgentFd < 0) {
284         tloge("m_gtask_agent_fd is -1\n");
285         return NULL;
286     }
287     secAgentControl->magic = SECFILE_LOAD_AGENT_ID;
288     while (true) {
289         int ret = ioctl(g_secLoadAgentFd, (int)TC_NS_CLIENT_IOCTL_WAIT_EVENT, SECFILE_LOAD_AGENT_ID);
290         if (ret) {
291             tloge("gtask agent wait event failed, errno = %" PUBLIC "d\n", errno);
292             break;
293         }
294         SecLoadAgentWork(secAgentControl);
295 
296         __asm__ volatile("isb");
297         __asm__ volatile("dsb sy");
298 
299         secAgentControl->magic = SECFILE_LOAD_AGENT_ID;
300 
301         __asm__ volatile("isb");
302         __asm__ volatile("dsb sy");
303         ret = ioctl(g_secLoadAgentFd, (int)TC_NS_CLIENT_IOCTL_SEND_EVENT_RESPONSE, SECFILE_LOAD_AGENT_ID);
304         if (ret) {
305             tloge("gtask agent send reponse failed\n");
306             break;
307         }
308     }
309     return NULL;
310 }
311