xref: /optee_os/lib/libutee/tee_api_panic.c (revision 6915bbbb5b56e147ee652b98f6172f4dfbb325b9)
11bb92983SJerome Forissier // SPDX-License-Identifier: BSD-2-Clause
294e8a4fcSJens Wiklander /*
394e8a4fcSJens Wiklander  * Copyright (c) 2014, STMicroelectronics International N.V.
4*6915bbbbSJens Wiklander  * Copyright (c) 2020, Linaro Limited
594e8a4fcSJens Wiklander  */
6*6915bbbbSJens Wiklander 
7*6915bbbbSJens Wiklander #include <config.h>
8*6915bbbbSJens Wiklander #include <string.h>
994e8a4fcSJens Wiklander #include <tee_api.h>
1094e8a4fcSJens Wiklander #include <utee_syscalls.h>
11*6915bbbbSJens Wiklander #include <util.h>
12*6915bbbbSJens Wiklander 
13*6915bbbbSJens Wiklander #include "tee_api_private.h"
14*6915bbbbSJens Wiklander 
15*6915bbbbSJens Wiklander #define ACCESS_RW	(TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE)
16*6915bbbbSJens Wiklander #define ACCESS_W_ANY	(TEE_MEMORY_ACCESS_WRITE | TEE_MEMORY_ACCESS_ANY_OWNER)
17*6915bbbbSJens Wiklander #define ACCESS_R	TEE_MEMORY_ACCESS_READ
18*6915bbbbSJens Wiklander #define ACCESS_W	TEE_MEMORY_ACCESS_WRITE
1994e8a4fcSJens Wiklander 
2094e8a4fcSJens Wiklander /* System API - Misc */
2194e8a4fcSJens Wiklander 
220e1c6e8eSJerome Forissier void TEE_Panic(TEE_Result panicCode)
2394e8a4fcSJens Wiklander {
242c028fdeSJerome Forissier 	_utee_panic(panicCode);
2594e8a4fcSJens Wiklander }
26*6915bbbbSJens Wiklander 
27*6915bbbbSJens Wiklander static void check_res(const char *msg __maybe_unused, TEE_Result res)
28*6915bbbbSJens Wiklander {
29*6915bbbbSJens Wiklander 	if (res) {
30*6915bbbbSJens Wiklander 		DMSG("%s: error %#"PRIx32, msg, res);
31*6915bbbbSJens Wiklander 		TEE_Panic(0);
32*6915bbbbSJens Wiklander 	}
33*6915bbbbSJens Wiklander }
34*6915bbbbSJens Wiklander 
35*6915bbbbSJens Wiklander static TEE_Result check_access(uint32_t flags, void *buf, size_t len)
36*6915bbbbSJens Wiklander {
37*6915bbbbSJens Wiklander 	if (!len)
38*6915bbbbSJens Wiklander 		return TEE_SUCCESS;
39*6915bbbbSJens Wiklander 
40*6915bbbbSJens Wiklander 	if (!buf)
41*6915bbbbSJens Wiklander 		return TEE_ERROR_SECURITY;
42*6915bbbbSJens Wiklander 
43*6915bbbbSJens Wiklander 	if (IS_ENABLED(CFG_TA_STRICT_ANNOTATION_CHECKS))
44*6915bbbbSJens Wiklander 		return TEE_CheckMemoryAccessRights(flags, buf, len);
45*6915bbbbSJens Wiklander 
46*6915bbbbSJens Wiklander 	return TEE_SUCCESS;
47*6915bbbbSJens Wiklander }
48*6915bbbbSJens Wiklander 
49*6915bbbbSJens Wiklander void __utee_check_outbuf_annotation(void *buf, uint32_t *len)
50*6915bbbbSJens Wiklander {
51*6915bbbbSJens Wiklander 	check_res("[outbuf] len",
52*6915bbbbSJens Wiklander 		  check_access(ACCESS_RW, len, sizeof(*len)));
53*6915bbbbSJens Wiklander 	check_res("[outbuf] buf",
54*6915bbbbSJens Wiklander 		  check_access(ACCESS_W_ANY, buf, *len));
55*6915bbbbSJens Wiklander }
56*6915bbbbSJens Wiklander 
57*6915bbbbSJens Wiklander void __utee_check_instring_annotation(const char *buf)
58*6915bbbbSJens Wiklander {
59*6915bbbbSJens Wiklander 	check_res("[instring]",
60*6915bbbbSJens Wiklander 		  check_access(ACCESS_R, (char *)buf, strlen(buf) + 1));
61*6915bbbbSJens Wiklander }
62*6915bbbbSJens Wiklander 
63*6915bbbbSJens Wiklander void __utee_check_outstring_annotation(char *buf, uint32_t *len)
64*6915bbbbSJens Wiklander {
65*6915bbbbSJens Wiklander 	check_res("[outstring] len",
66*6915bbbbSJens Wiklander 		  check_access(ACCESS_RW, len, sizeof(*len)));
67*6915bbbbSJens Wiklander 	check_res("[outstring] buf",
68*6915bbbbSJens Wiklander 		  check_access(ACCESS_W_ANY, buf, *len));
69*6915bbbbSJens Wiklander }
70*6915bbbbSJens Wiklander 
71*6915bbbbSJens Wiklander void __utee_check_out_annotation(void *buf, const size_t len)
72*6915bbbbSJens Wiklander {
73*6915bbbbSJens Wiklander 	check_res("[out]",
74*6915bbbbSJens Wiklander 		  check_access(ACCESS_W, buf, len));
75*6915bbbbSJens Wiklander }
76*6915bbbbSJens Wiklander 
77*6915bbbbSJens Wiklander void __utee_check_attr_in_annotation(const TEE_Attribute *attr, size_t count)
78*6915bbbbSJens Wiklander {
79*6915bbbbSJens Wiklander 	check_res("[in] attr",
80*6915bbbbSJens Wiklander 		  check_access(ACCESS_R, (void *)attr, sizeof(*attr) * count));
81*6915bbbbSJens Wiklander }
82*6915bbbbSJens Wiklander 
83*6915bbbbSJens Wiklander void __utee_check_inout_annotation(void *buf, const size_t len)
84*6915bbbbSJens Wiklander {
85*6915bbbbSJens Wiklander 	check_res("[inout]",
86*6915bbbbSJens Wiklander 		  check_access(ACCESS_RW, buf, len));
87*6915bbbbSJens Wiklander }
88