1 /*
2  * Copyright (c) 2017 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/syscall_handler.h>
9 #include <zephyr/kernel_structs.h>
10 #include <zephyr/toolchain.h>
11 
validate_kernel_object(const void * obj,enum k_objects otype,enum _obj_init_check init)12 static struct z_object *validate_kernel_object(const void *obj,
13 					       enum k_objects otype,
14 					       enum _obj_init_check init)
15 {
16 	struct z_object *ko;
17 	int ret;
18 
19 	ko = z_object_find(obj);
20 
21 	/* This can be any kernel object and it doesn't have to be
22 	 * initialized
23 	 */
24 	ret = z_object_validate(ko, otype, init);
25 	if (ret != 0) {
26 #ifdef CONFIG_LOG
27 		z_dump_object_error(ret, obj, ko, otype);
28 #endif
29 		return NULL;
30 	}
31 
32 	return ko;
33 }
34 
validate_any_object(const void * obj)35 static ALWAYS_INLINE struct z_object *validate_any_object(const void *obj)
36 {
37 	return validate_kernel_object(obj, K_OBJ_ANY, _OBJ_INIT_ANY);
38 }
39 
k_object_is_valid(const void * obj,enum k_objects otype)40 bool k_object_is_valid(const void *obj, enum k_objects otype)
41 {
42 	struct z_object *ko;
43 
44 	ko = validate_kernel_object(obj, otype, _OBJ_INIT_TRUE);
45 
46 	return (ko != NULL);
47 }
48 
49 /* Normally these would be included in userspace.c, but the way
50  * syscall_dispatch.c declares weak handlers results in build errors if these
51  * are located in userspace.c. Just put in a separate file.
52  *
53  * To avoid double z_object_find() lookups, we don't call the implementation
54  * function, but call a level deeper.
55  */
z_vrfy_k_object_access_grant(const void * object,struct k_thread * thread)56 static inline void z_vrfy_k_object_access_grant(const void *object,
57 						struct k_thread *thread)
58 {
59 	struct z_object *ko;
60 
61 	Z_OOPS(Z_SYSCALL_OBJ_INIT(thread, K_OBJ_THREAD));
62 	ko = validate_any_object(object);
63 	Z_OOPS(Z_SYSCALL_VERIFY_MSG(ko != NULL, "object %p access denied",
64 				    object));
65 	z_thread_perms_set(ko, thread);
66 }
67 #include <syscalls/k_object_access_grant_mrsh.c>
68 
z_vrfy_k_object_release(const void * object)69 static inline void z_vrfy_k_object_release(const void *object)
70 {
71 	struct z_object *ko;
72 
73 	ko = validate_any_object((void *)object);
74 	Z_OOPS(Z_SYSCALL_VERIFY_MSG(ko != NULL, "object %p access denied",
75 				    (void *)object));
76 	z_thread_perms_clear(ko, _current);
77 }
78 #include <syscalls/k_object_release_mrsh.c>
79 
z_vrfy_k_object_alloc(enum k_objects otype)80 static inline void *z_vrfy_k_object_alloc(enum k_objects otype)
81 {
82 	return z_impl_k_object_alloc(otype);
83 }
84 #include <syscalls/k_object_alloc_mrsh.c>
85 
z_vrfy_k_object_alloc_size(enum k_objects otype,size_t size)86 static inline void *z_vrfy_k_object_alloc_size(enum k_objects otype, size_t size)
87 {
88 	return z_impl_k_object_alloc_size(otype, size);
89 }
90 #include <syscalls/k_object_alloc_size_mrsh.c>
91