aboutsummaryrefslogtreecommitdiff
path: root/drivers/tee/tee_driver.c
blob: 3655f9e5bfcd0662c936fae35dbe7da764284c43 (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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
/*
 * Copyright (C) ST-Ericsson SA 2010
 * Author: Martin Hovang <martin.xm.hovang@stericsson.com>
 * Author: Joakim Bech <joakim.xx.bech@stericsson.com>
 * License terms: GNU General Public License (GPL) version 2
 */

#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/cdev.h>
#include <linux/mutex.h>
#include <linux/miscdevice.h>
#include <linux/uaccess.h>
#include <linux/tee.h>
#include <linux/slab.h>

#define TEED_NAME "tee"

#define TEED_STATE_OPEN_DEV 0
#define TEED_STATE_OPEN_SESSION 1

static int tee_open(struct inode *inode, struct file *file);
static int tee_release(struct inode *inode, struct file *file);
static int tee_read(struct file *filp, char __user *buffer,
		    size_t length, loff_t *offset);
static int tee_write(struct file *filp, const char __user *buffer,
		     size_t length, loff_t *offset);

static inline void set_emsg(struct tee_session *ts, u32 msg)
{
	ts->err = msg;
	ts->origin = TEED_ORIGIN_DRIVER;
}

static int copy_ta(struct tee_session *ts,
		   struct tee_write *buf)
{
	ts->ta = kmalloc(buf->ta_size, GFP_KERNEL);
	if (ts->ta == NULL) {
		printk(KERN_INFO "[%s] error, out of memory (ta)\n",
		       __func__);
		set_emsg(ts, TEED_ERROR_OUT_OF_MEMORY);
		return -ENOMEM;
	}

	ts->ta_size = buf->ta_size;

	memcpy(ts->ta, buf->ta, buf->ta_size);
	return 0;
}

static int copy_uuid(struct tee_session *ts,
		     struct tee_write *buf)
{
	ts->uuid = kmalloc(sizeof(struct tee_uuid), GFP_KERNEL);

	if (ts->uuid == NULL) {
		printk(KERN_INFO "[%s] error, out of memory (uuid)\n",
		       __func__);
		set_emsg(ts, TEED_ERROR_OUT_OF_MEMORY);
		return -ENOMEM;
	}

	memcpy(ts->uuid, buf->uuid, sizeof(struct tee_uuid));

	return 0;
}

static int open_tee_device(struct tee_session *ts,
			   struct tee_write *buf)
{
	int ret;

	if (buf->id != TEED_OPEN_SESSION) {
		set_emsg(ts, TEED_ERROR_BAD_STATE);
		return -EINVAL;
	}

	if (buf->ta) {
		ret = copy_ta(ts, buf);
	} else if (buf->uuid) {
		ret = copy_uuid(ts, buf);
	} else {
		set_emsg(ts, TEED_ERROR_COMMUNICATION);
		return -EINVAL;
	}

	ts->id = 0;
	ts->state = TEED_STATE_OPEN_SESSION;
	return ret;
}

static int invoke_command(struct tee_session *ts,
			  struct tee_write *kbuf,
			  struct tee_write __user *ubuf)
{
	int i;
	int ret = 0;
	struct tee_operation *ubuf_op =
		(struct tee_operation *)kbuf->inData;

	ts->idata = kmalloc(sizeof(struct tee_operation), GFP_KERNEL);

	if (!ts->idata) {
		if (ts->idata == NULL) {
			printk(KERN_INFO "[%s] error, out of memory "
			       "(idata)\n", __func__);
			set_emsg(ts, TEED_ERROR_OUT_OF_MEMORY);
			ret = -ENOMEM;
			goto err;
		}
	}

	/* Copy memrefs to kernel space */
	ts->idata->flags = ubuf_op->flags;
	ts->cmd = kbuf->cmd;

	for (i = 0; i < 4; ++i) {
		if (ubuf_op->flags & (1 << i)) {
			ts->idata->shm[i].buffer =
				kmalloc(ubuf_op->shm[i].size,
					GFP_KERNEL);

			if (!ts->idata->shm[i].buffer) {
				printk(KERN_ERR "[%s] out of memory\n",
				       __func__);
				set_emsg(ts, TEED_ERROR_OUT_OF_MEMORY);
				ret = -ENOMEM;
				goto err;
			}
			/*
			 * Copy all shared memory operations to a local
			 * kernel buffer.
			 */
			memcpy(ts->idata->shm[i].buffer,
			       ubuf_op->shm[i].buffer,
			       ubuf_op->shm[i].size);

			ts->idata->shm[i].size = ubuf_op->shm[i].size;
			ts->idata->shm[i].flags = ubuf_op->shm[i].flags;

			/* Secure world expects physical addresses. */
			ts->idata->shm[i].buffer =
				virt_to_phys(ts->idata->shm[i].buffer);
		} else {
			ts->idata->shm[i].buffer = NULL;
			ts->idata->shm[i].size = 0;
			ts->idata->shm[i].flags = 0;
		}
	}

	/* To call secure world */
	if (call_sec_world(ts)) {
		ret = -EINVAL;
		goto err;
	}

	/*
	 * Convert physical addresses back to virtual address so the kernel can
	 * free the buffers when closing the session.
	 */
	for (i = 0; i < 4; ++i) {
		if (ubuf_op->flags & (1 << i)) {
			ts->idata->shm[i].buffer =
				phys_to_virt(ts->idata->shm[i].buffer);
		}
	}

	for (i = 0; i < 4; ++i) {
		if (ubuf_op->flags & (1 << i)) {
			u32 bytes_left = copy_to_user(ubuf_op->shm[i].buffer,
					 ts->idata->shm[i].buffer,
					 ts->idata->shm[i].size);
			if (bytes_left != 0) {
				printk(KERN_ERR "[%s] Failed to copy result to "
				       "user space (%d bytes left).\n", __func__, bytes_left);
			}
		}
	}

err:
	if (ret) {
		for (i = 0; i < 4; ++i)
			kfree(ts->idata->shm[i].buffer);

		kfree(ts->idata);
	}

	return ret;
}

static int tee_open(struct inode *inode, struct file *filp)
{
	struct tee_session *ts;

	filp->private_data = kmalloc(sizeof(struct tee_session), GFP_KERNEL);

	if (filp->private_data == NULL)
		return -ENOMEM;

	ts = (struct tee_session *) (filp->private_data);
	ts->state = TEED_STATE_OPEN_DEV;
	ts->err = TEED_SUCCESS;
	ts->origin = TEED_ORIGIN_DRIVER;
	ts->sync = kmalloc(sizeof(struct mutex), GFP_KERNEL);
	mutex_init(ts->sync);
	ts->ta = NULL;
	ts->uuid = NULL;
	ts->id = 0;
	ts->idata = NULL;
	ts->ta_size = 0;
	ts->err = TEED_SUCCESS;

	return 0;
}

static int tee_release(struct inode *inode, struct file *filp)
{
	struct tee_session *ts;
	int i;

	ts = (struct tee_session *) (filp->private_data);

	if (ts != NULL) {
		if (ts->idata) {
			for (i = 0; i < 4; ++i) {
				kfree(ts->idata->shm[i].buffer);
				ts->idata->shm[i].buffer = NULL;
			}

		}

		kfree(ts->idata);
		ts->idata = NULL;

		kfree(ts->sync);
		ts->sync = NULL;

		kfree(ts->ta);
		ts->ta = NULL;
	}

	kfree(filp->private_data);
	filp->private_data = NULL;

	return 0;
}

/*
 * Called when a process, which already opened the dev file, attempts
 * to read from it. This function gets the current status of the session.
 */
static int tee_read(struct file *filp, char __user *buffer,
		    size_t length, loff_t *offset)
{
	struct tee_read buf;
	struct tee_session *ts;

	if (length != sizeof(struct tee_read)) {
		printk(KERN_INFO "[%s] error, incorrect input length\n",
		       __func__);
		return -EINVAL;
	}

	ts = (struct tee_session *) (filp->private_data);

	if (ts == NULL || ts->sync == NULL) {
		printk(KERN_INFO "[%s] error, private_data not initialized\n",
		       __func__);
		return -EINVAL;
	}

	mutex_lock(ts->sync);

	buf.id = ts->err;
	buf.origin = ts->origin;

	mutex_unlock(ts->sync);

	if (copy_to_user(buffer, &buf, length)) {
		printk(KERN_INFO "[%s] error, copy_to_user failed!\n",
		       __func__);
		return -EINVAL;
	}

	return length;
}

/*
 * Called when a process writes to a dev file
 */
static int tee_write(struct file *filp, const char __user *buffer,
		     size_t length, loff_t *offset)
{
	struct tee_write buf;
	struct tee_session *ts;
	int ret = length;

	if (length != sizeof(struct tee_write)) {
		printk(KERN_INFO "[%s] error, incorrect input length\n",
		       __func__);
		return -EINVAL;
	}

	if (copy_from_user(&buf, buffer, length)) {
		printk(KERN_INFO "[%s] error, tee_session copy_from_user "
		       "failed\n", __func__);
		return -EINVAL;
	}

	ts = (struct tee_session *) (filp->private_data);

	if (ts == NULL || ts->sync == NULL) {
		printk(KERN_INFO "[%s] error, private_data not initialized\n",
		       __func__);
		return -EINVAL;
	}

	mutex_lock(ts->sync);

	switch (ts->state) {
	case TEED_STATE_OPEN_DEV:
		ret = open_tee_device(ts, &buf);
		break;

	case TEED_STATE_OPEN_SESSION:
		switch (buf.id) {
		case TEED_INVOKE:
			ret = invoke_command(ts, &buf,
					     (struct tee_write *)buffer);
			break;

		case TEED_CLOSE_SESSION:
			/* no caching implemented yet... */
			kfree(ts->ta);
			ts->ta = NULL;

			ts->state = TEED_STATE_OPEN_DEV;
			break;

		default:
			set_emsg(ts, TEED_ERROR_BAD_PARAMETERS);
			ret = -EINVAL;
		}
		break;
	default:
		printk(KERN_INFO "[%s] unknown state\n", __func__);
		set_emsg(ts, TEED_ERROR_BAD_STATE);
		ret = -EINVAL;
	}

	/*
	 * We expect that ret has value zero when reaching the end here. If
	 * it has any other value some error must have occured.
	 */
	if (!ret)
		ret = length;
	else
		ret = -EINVAL;

	mutex_unlock(ts->sync);

	return ret;
}

static const struct file_operations tee_fops = {
	.owner = THIS_MODULE,
	.read = tee_read,
	.write = tee_write,
	.open = tee_open,
	.release = tee_release,
};

static struct miscdevice tee_dev = {
	MISC_DYNAMIC_MINOR,
	TEED_NAME,
	&tee_fops
};

static int __init tee_init(void)
{
	int err = 0;

	err = misc_register(&tee_dev);

	if (err) {
		printk(KERN_ERR "[%s] error %d adding character device TEE\n",
		       __func__, err);
	}

	return err;
}

static void __exit tee_exit(void)
{
	misc_deregister(&tee_dev);
}

module_init(tee_init);
module_exit(tee_exit);

MODULE_LICENSE("Dual BSD/GPL");
MODULE_DESCRIPTION("Trusted Execution Enviroment driver");