xref: /optee_os/lib/libutee/tee_api_panic.c (revision 4f4374c8555bb3308d5d646b41df5e53c52ee7e5)
11bb92983SJerome Forissier // SPDX-License-Identifier: BSD-2-Clause
294e8a4fcSJens Wiklander /*
394e8a4fcSJens Wiklander  * Copyright (c) 2014, STMicroelectronics International N.V.
46915bbbbSJens Wiklander  * Copyright (c) 2020, Linaro Limited
594e8a4fcSJens Wiklander  */
66915bbbbSJens Wiklander 
76915bbbbSJens Wiklander #include <config.h>
86915bbbbSJens Wiklander #include <string.h>
994e8a4fcSJens Wiklander #include <tee_api.h>
1094e8a4fcSJens Wiklander #include <utee_syscalls.h>
116915bbbbSJens Wiklander #include <util.h>
126915bbbbSJens Wiklander 
136915bbbbSJens Wiklander #include "tee_api_private.h"
146915bbbbSJens Wiklander 
156915bbbbSJens Wiklander #define ACCESS_RW	(TEE_MEMORY_ACCESS_READ | TEE_MEMORY_ACCESS_WRITE)
166915bbbbSJens Wiklander #define ACCESS_W_ANY	(TEE_MEMORY_ACCESS_WRITE | TEE_MEMORY_ACCESS_ANY_OWNER)
176915bbbbSJens Wiklander #define ACCESS_R	TEE_MEMORY_ACCESS_READ
186915bbbbSJens 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);
25b68bcfebSJens Wiklander #ifdef __COVERITY__
26b68bcfebSJens Wiklander 	__coverity_panic__();
27b68bcfebSJens Wiklander #endif
2894e8a4fcSJens Wiklander }
296915bbbbSJens Wiklander 
306915bbbbSJens Wiklander static void check_res(const char *msg __maybe_unused, TEE_Result res)
316915bbbbSJens Wiklander {
326915bbbbSJens Wiklander 	if (res) {
336915bbbbSJens Wiklander 		DMSG("%s: error %#"PRIx32, msg, res);
346915bbbbSJens Wiklander 		TEE_Panic(0);
356915bbbbSJens Wiklander 	}
366915bbbbSJens Wiklander }
376915bbbbSJens Wiklander 
386915bbbbSJens Wiklander static TEE_Result check_access(uint32_t flags, void *buf, size_t len)
396915bbbbSJens Wiklander {
406915bbbbSJens Wiklander 	if (!len)
416915bbbbSJens Wiklander 		return TEE_SUCCESS;
426915bbbbSJens Wiklander 
436915bbbbSJens Wiklander 	if (!buf)
446915bbbbSJens Wiklander 		return TEE_ERROR_SECURITY;
456915bbbbSJens Wiklander 
466915bbbbSJens Wiklander 	if (IS_ENABLED(CFG_TA_STRICT_ANNOTATION_CHECKS))
476915bbbbSJens Wiklander 		return TEE_CheckMemoryAccessRights(flags, buf, len);
486915bbbbSJens Wiklander 
496915bbbbSJens Wiklander 	return TEE_SUCCESS;
506915bbbbSJens Wiklander }
516915bbbbSJens Wiklander 
526915bbbbSJens Wiklander void __utee_check_outbuf_annotation(void *buf, uint32_t *len)
536915bbbbSJens Wiklander {
546915bbbbSJens Wiklander 	check_res("[outbuf] len",
556915bbbbSJens Wiklander 		  check_access(ACCESS_RW, len, sizeof(*len)));
566915bbbbSJens Wiklander 	check_res("[outbuf] buf",
576915bbbbSJens Wiklander 		  check_access(ACCESS_W_ANY, buf, *len));
586915bbbbSJens Wiklander }
596915bbbbSJens Wiklander 
606915bbbbSJens Wiklander void __utee_check_instring_annotation(const char *buf)
616915bbbbSJens Wiklander {
626915bbbbSJens Wiklander 	check_res("[instring]",
636915bbbbSJens Wiklander 		  check_access(ACCESS_R, (char *)buf, strlen(buf) + 1));
646915bbbbSJens Wiklander }
656915bbbbSJens Wiklander 
666915bbbbSJens Wiklander void __utee_check_outstring_annotation(char *buf, uint32_t *len)
676915bbbbSJens Wiklander {
686915bbbbSJens Wiklander 	check_res("[outstring] len",
696915bbbbSJens Wiklander 		  check_access(ACCESS_RW, len, sizeof(*len)));
706915bbbbSJens Wiklander 	check_res("[outstring] buf",
716915bbbbSJens Wiklander 		  check_access(ACCESS_W_ANY, buf, *len));
726915bbbbSJens Wiklander }
736915bbbbSJens Wiklander 
746915bbbbSJens Wiklander void __utee_check_out_annotation(void *buf, const size_t len)
756915bbbbSJens Wiklander {
766915bbbbSJens Wiklander 	check_res("[out]",
776915bbbbSJens Wiklander 		  check_access(ACCESS_W, buf, len));
786915bbbbSJens Wiklander }
796915bbbbSJens Wiklander 
806915bbbbSJens Wiklander void __utee_check_attr_in_annotation(const TEE_Attribute *attr, size_t count)
816915bbbbSJens Wiklander {
826915bbbbSJens Wiklander 	check_res("[in] attr",
836915bbbbSJens Wiklander 		  check_access(ACCESS_R, (void *)attr, sizeof(*attr) * count));
846915bbbbSJens Wiklander }
856915bbbbSJens Wiklander 
86*4f4374c8SJens Wiklander void __utee_check_gp11_attr_in_annotation(const __GP11_TEE_Attribute *attr,
87*4f4374c8SJens Wiklander 					  size_t count)
88*4f4374c8SJens Wiklander 
89*4f4374c8SJens Wiklander {
90*4f4374c8SJens Wiklander 	check_res("[in] attr",
91*4f4374c8SJens Wiklander 		  check_access(ACCESS_R, (void *)attr, sizeof(*attr) * count));
92*4f4374c8SJens Wiklander }
93*4f4374c8SJens Wiklander 
946915bbbbSJens Wiklander void __utee_check_inout_annotation(void *buf, const size_t len)
956915bbbbSJens Wiklander {
966915bbbbSJens Wiklander 	check_res("[inout]",
976915bbbbSJens Wiklander 		  check_access(ACCESS_RW, buf, len));
986915bbbbSJens Wiklander }
99