forked from OP-TEE/optee_client
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathteec_ta_load.c
172 lines (153 loc) · 4.85 KB
/
teec_ta_load.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/*
* Copyright (c) 2014, STMicroelectronics International N.V.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <dirent.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <teec_trace.h>
#include <teec_ta_load.h>
#ifndef TEEC_LOAD_PATH
#define TEEC_LOAD_PATH "/lib"
#endif
/*
* Attempt to first load TAs from a writable directory. This is
* intended for testing (xtest 1008, load_corrupt_ta specifically),
* and should not be enabled in a production system, as it would
* greatly facilitate loading rogue TA code.
*/
#ifdef CFG_TA_TEST_PATH
# ifndef TEEC_TEST_LOAD_PATH
# ifdef __ANDROID__
# define TEEC_TEST_LOAD_PATH "/data/tmp"
# else
# define TEEC_TEST_LOAD_PATH "/tmp"
# endif
# endif
#endif
#ifndef PATH_MAX
#define PATH_MAX 255
#endif
struct tee_rpc_cmd {
void *buffer;
uint32_t size;
uint32_t type;
int fd;
};
/*
* Based on the uuid this function will try to find a TA-binary on the
* filesystem and return it back to the caller in the parameter ta.
*
* @param: destination The uuid of the TA we are searching for.
* @param: ta A pointer which this function will allocate and copy
* the TA from the filesystem to the pointer itself. It is
* the callers responsibility to free the pointer.
* @param: ta_size The size of the TA found on file system. It will be 0
* if no TA was not found.
*
* @return 0 if TA was found, otherwise -1.
*/
static int try_load_secure_module(const char* prefix,
const char* dev_path,
const TEEC_UUID *destination, void *ta,
size_t *ta_size)
{
char fname[PATH_MAX];
FILE *file = NULL;
int n;
size_t s;
if (!ta_size || !destination) {
printf("wrong inparameter to TEECI_LoadSecureModule\n");
return TA_BINARY_NOT_FOUND;
}
n = snprintf(fname, PATH_MAX,
"%s/%s/%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x.ta",
prefix, dev_path,
destination->timeLow,
destination->timeMid,
destination->timeHiAndVersion,
destination->clockSeqAndNode[0],
destination->clockSeqAndNode[1],
destination->clockSeqAndNode[2],
destination->clockSeqAndNode[3],
destination->clockSeqAndNode[4],
destination->clockSeqAndNode[5],
destination->clockSeqAndNode[6],
destination->clockSeqAndNode[7]);
DMSG("Attempt to load %s", fname);
if ((n < 0) || (n >= PATH_MAX)) {
EMSG("wrong TA path [%s]", fname);
return TA_BINARY_NOT_FOUND;
}
file = fopen(fname, "r");
if (file == NULL) {
DMSG("failed to open the ta %s TA-file", fname);
return TA_BINARY_NOT_FOUND;
}
if (fseek(file, 0, SEEK_END) != 0) {
fclose(file);
return TA_BINARY_NOT_FOUND;
}
s = ftell(file);
if (s > *ta_size || !ta) {
/*
* Buffer isn't large enough, return the required size to
* let the caller increase the size of the buffer and try
* again.
*/
goto out;
}
if (fseek(file, 0, SEEK_SET) != 0) {
fclose(file);
return TA_BINARY_NOT_FOUND;
}
if (s != fread(ta, 1, s, file)) {
printf("error fread TA file\n");
fclose(file);
return TA_BINARY_NOT_FOUND;
}
out:
*ta_size = s;
fclose(file);
return TA_BINARY_FOUND;
}
int TEECI_LoadSecureModule(const char* dev_path,
const TEEC_UUID *destination, void *ta,
size_t *ta_size)
{
#ifdef TEEC_TEST_LOAD_PATH
int res;
res = try_load_secure_module(TEEC_TEST_LOAD_PATH,
dev_path, destination, ta, ta_size);
if (res != TA_BINARY_NOT_FOUND)
return res;
#endif
return try_load_secure_module(TEEC_LOAD_PATH,
dev_path, destination, ta, ta_size);
}