aboutsummaryrefslogtreecommitdiff
path: root/core/tee/fs_dirfile.c
blob: 3a0081448d2a31841c789fb2b2a9134cca8f26fc (plain)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
// SPDX-License-Identifier: BSD-2-Clause
/*
 * Copyright (c) 2017, Linaro Limited
 * 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 <assert.h>
#include <bitstring.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <tee/fs_dirfile.h>
#include <types_ext.h>

struct tee_fs_dirfile_dirh {
	const struct tee_fs_dirfile_operations *fops;
	struct tee_file_handle *fh;
	int nbits;
	bitstr_t *files;
	size_t ndents;
};

struct dirfile_entry {
	TEE_UUID uuid;
	uint8_t oid[TEE_OBJECT_ID_MAX_LEN];
	uint32_t oidlen;
	uint8_t hash[TEE_FS_HTREE_HASH_SIZE];
	uint32_t file_number;
};

/*
 * File layout
 *
 * dirfile_entry.0
 * ...
 * dirfile_entry.n
 *
 * where n the index is disconnected from file_number in struct dirfile_entry
 */

static TEE_Result maybe_grow_files(struct tee_fs_dirfile_dirh *dirh, int idx)
{
	void *p;

	if (idx < dirh->nbits)
		return TEE_SUCCESS;

	p = realloc(dirh->files, bitstr_size(idx + 1));
	if (!p)
		return TEE_ERROR_OUT_OF_MEMORY;
	dirh->files = p;

	bit_nclear(dirh->files, dirh->nbits, idx);
	dirh->nbits = idx + 1;

	return TEE_SUCCESS;
}

static TEE_Result set_file(struct tee_fs_dirfile_dirh *dirh, int idx)
{
	TEE_Result res = maybe_grow_files(dirh, idx);

	if (!res)
		bit_set(dirh->files, idx);

	return res;
}

static void clear_file(struct tee_fs_dirfile_dirh *dirh, int idx)
{
	if (idx < dirh->nbits)
		bit_clear(dirh->files, idx);
}

static bool test_file(struct tee_fs_dirfile_dirh *dirh, int idx)
{
	if (idx < dirh->nbits)
		return bit_test(dirh->files, idx);

	return false;
}

static TEE_Result read_dent(struct tee_fs_dirfile_dirh *dirh, int idx,
			    struct dirfile_entry *dent)
{
	TEE_Result res;
	size_t l;

	l = sizeof(*dent);
	res = dirh->fops->read(dirh->fh, sizeof(struct dirfile_entry) * idx,
			       dent, &l);
	if (!res && l != sizeof(*dent))
		res = TEE_ERROR_ITEM_NOT_FOUND;

	return res;
}

static TEE_Result write_dent(struct tee_fs_dirfile_dirh *dirh, size_t n,
			     struct dirfile_entry *dent)
{
	TEE_Result res;

	res = dirh->fops->write(dirh->fh, sizeof(*dent) * n,
				dent, sizeof(*dent));
	if (!res && n >= dirh->ndents)
		dirh->ndents = n + 1;

	return res;
}

TEE_Result tee_fs_dirfile_open(bool create, uint8_t *hash,
			       const struct tee_fs_dirfile_operations *fops,
			       struct tee_fs_dirfile_dirh **dirh_ret)
{
	TEE_Result res;
	struct tee_fs_dirfile_dirh *dirh = calloc(1, sizeof(*dirh));
	size_t n;

	if (!dirh)
		return TEE_ERROR_OUT_OF_MEMORY;

	dirh->fops = fops;
	res = fops->open(create, hash, NULL, NULL, &dirh->fh);
	if (res)
		goto out;

	for (n = 0;; n++) {
		struct dirfile_entry dent;

		res = read_dent(dirh, n, &dent);
		if (res) {
			if (res == TEE_ERROR_ITEM_NOT_FOUND)
				res = TEE_SUCCESS;
			goto out;
		}

		if (!dent.oidlen)
			continue;

		if (test_file(dirh, dent.file_number)) {
			DMSG("clearing duplicate file number %" PRIu32,
			     dent.file_number);
			memset(&dent, 0, sizeof(dent));
			res = write_dent(dirh, n, &dent);
			if (res)
				goto out;
			continue;
		}

		res = set_file(dirh, dent.file_number);
		if (res != TEE_SUCCESS)
			goto out;
	}
out:
	if (!res) {
		dirh->ndents = n;
		*dirh_ret = dirh;
	} else {
		tee_fs_dirfile_close(dirh);
	}
	return res;
}

void tee_fs_dirfile_close(struct tee_fs_dirfile_dirh *dirh)
{
	if (dirh) {
		dirh->fops->close(dirh->fh);
		free(dirh->files);
		free(dirh);
	}
}

TEE_Result tee_fs_dirfile_commit_writes(struct tee_fs_dirfile_dirh *dirh,
					uint8_t *hash)
{
	return dirh->fops->commit_writes(dirh->fh, hash);
}

TEE_Result tee_fs_dirfile_get_tmp(struct tee_fs_dirfile_dirh *dirh,
				  struct tee_fs_dirfile_fileh *dfh)
{
	TEE_Result res;
	int i = 0;

	if (dirh->files) {
		bit_ffc(dirh->files, dirh->nbits, &i);
		if (i == -1)
			i = dirh->nbits;
	}

	res = set_file(dirh, i);
	if (!res)
		dfh->file_number = i;

	return res;
}

TEE_Result tee_fs_dirfile_find(struct tee_fs_dirfile_dirh *dirh,
			       const TEE_UUID *uuid, const void *oid,
			       size_t oidlen, struct tee_fs_dirfile_fileh *dfh)
{
	TEE_Result res;
	struct dirfile_entry dent;
	int n;
	int first_free = -1;

	for (n = 0;; n++) {
		res = read_dent(dirh, n, &dent);
		if (res == TEE_ERROR_ITEM_NOT_FOUND && !oidlen) {
			memset(&dent, 0, sizeof(dent));
			if (first_free != -1)
				n = first_free;
			break;
		}
		if (res)
			return res;

		/* TODO check this loop when oidlen == 0 */

		if (!dent.oidlen && first_free == -1)
			first_free = n;
		if (dent.oidlen != oidlen)
			continue;

		assert(!oidlen || !dent.oidlen ||
		       test_file(dirh, dent.file_number));

		if (!memcmp(&dent.uuid, uuid, sizeof(dent.uuid)) &&
		    !memcmp(&dent.oid, oid, oidlen))
			break;
	}

	if (dfh) {
		dfh->idx = n;
		dfh->file_number = dent.file_number;
		memcpy(dfh->hash, dent.hash, sizeof(dent.hash));
	}

	return TEE_SUCCESS;
}

TEE_Result tee_fs_dirfile_fileh_to_fname(const struct tee_fs_dirfile_fileh *dfh,
					 char *fname, size_t *fnlen)
{
	int r;
	size_t l = *fnlen;

	if (dfh)
		r = snprintf(fname, l, "%" PRIx32, dfh->file_number);
	else
		r = snprintf(fname, l, "dirf.db");

	if (r < 0)
		return TEE_ERROR_GENERIC;

	*fnlen = r + 1;
	if ((size_t)r >= l)
		return TEE_ERROR_SHORT_BUFFER;

	return TEE_SUCCESS;
}

TEE_Result tee_fs_dirfile_rename(struct tee_fs_dirfile_dirh *dirh,
				 const TEE_UUID *uuid,
				 struct tee_fs_dirfile_fileh *dfh,
				 const void *oid, size_t oidlen)
{
	TEE_Result res;
	struct dirfile_entry dent;

	if (!oidlen || oidlen > sizeof(dent.oid))
		return TEE_ERROR_BAD_PARAMETERS;
	memset(&dent, 0, sizeof(dent));
	dent.uuid = *uuid;
	memcpy(dent.oid, oid, oidlen);
	dent.oidlen = oidlen;
	memcpy(dent.hash, dfh->hash, sizeof(dent.hash));
	dent.file_number = dfh->file_number;

	if (dfh->idx < 0) {
		struct tee_fs_dirfile_fileh dfh2;

		res = tee_fs_dirfile_find(dirh, uuid, oid, oidlen, &dfh2);
		if (res) {
			if (res == TEE_ERROR_ITEM_NOT_FOUND)
				res = tee_fs_dirfile_find(dirh, uuid, NULL, 0,
							  &dfh2);
			if (res)
				return res;
		}
		dfh->idx = dfh2.idx;
	}

	return write_dent(dirh, dfh->idx, &dent);
}

TEE_Result tee_fs_dirfile_remove(struct tee_fs_dirfile_dirh *dirh,
				 const struct tee_fs_dirfile_fileh *dfh)
{
	TEE_Result res;
	struct dirfile_entry dent;
	uint32_t file_number;

	res = read_dent(dirh, dfh->idx, &dent);
	if (res)
		return res;

	if (!dent.oidlen)
		return TEE_SUCCESS;

	file_number = dent.file_number;
	assert(dfh->file_number == file_number);
	assert(test_file(dirh, file_number));

	memset(&dent, 0, sizeof(dent));
	res = write_dent(dirh, dfh->idx, &dent);
	if (!res)
		clear_file(dirh, file_number);

	return res;
}

TEE_Result tee_fs_dirfile_update_hash(struct tee_fs_dirfile_dirh *dirh,
				      const struct tee_fs_dirfile_fileh *dfh)
{
	TEE_Result res;
	struct dirfile_entry dent;

	res = read_dent(dirh, dfh->idx, &dent);
	if (res)
		return res;
	assert(dent.file_number == dfh->file_number);
	assert(test_file(dirh, dent.file_number));

	memcpy(&dent.hash, dfh->hash, sizeof(dent.hash));

	return write_dent(dirh, dfh->idx, &dent);
}

TEE_Result tee_fs_dirfile_get_next(struct tee_fs_dirfile_dirh *dirh,
				   const TEE_UUID *uuid, int *idx, void *oid,
				   size_t *oidlen)
{
	TEE_Result res;
	int i = *idx + 1;
	struct dirfile_entry dent;

	if (i < 0)
		i = 0;

	for (;; i++) {
		res = read_dent(dirh, i, &dent);
		if (res)
			return res;
		if (!memcmp(&dent.uuid, uuid, sizeof(dent.uuid)) &&
		    dent.oidlen)
			break;
	}

	if (*oidlen < dent.oidlen)
		return TEE_ERROR_SHORT_BUFFER;

	memcpy(oid, dent.oid, dent.oidlen);
	*oidlen = dent.oidlen;
	*idx = i;

	return TEE_SUCCESS;
}