blob: a39ac0427beb7dce9efdb9c4b277263c8896a745 [file] [log] [blame]
Andrew Boie13ca6fe2017-09-23 12:05:49 -07001/*
2 * Copyright (c) 2017, Intel Corporation
3 *
Anas Nashifc7f5cc92018-04-12 13:45:33 -05004 * SPDX-License-Identifier: Apache-2.0
Andrew Boie13ca6fe2017-09-23 12:05:49 -07005 */
6
7
Stephanos Ioannidis2d746042019-10-25 00:08:21 +09008#ifndef ZEPHYR_INCLUDE_SYSCALL_HANDLER_H_
9#define ZEPHYR_INCLUDE_SYSCALL_HANDLER_H_
Andrew Boie13ca6fe2017-09-23 12:05:49 -070010
11#ifdef CONFIG_USERSPACE
12
13#ifndef _ASMLANGUAGE
14#include <kernel.h>
Stephanos Ioannidis2d746042019-10-25 00:08:21 +090015#include <sys/arch_interface.h>
Anas Nashif6ecadb02019-06-26 10:33:45 -040016#include <sys/math_extras.h>
Flavio Ceolinb3d92022018-09-17 15:56:06 -070017#include <stdbool.h>
Andrew Boie99b3f862019-09-30 14:25:23 -070018#include <logging/log.h>
Andrew Boie13ca6fe2017-09-23 12:05:49 -070019
20extern const _k_syscall_handler_t _k_syscall_table[K_SYSCALL_LIMIT];
21
Andrew Boiea2b40ec2017-10-15 14:22:08 -070022enum _obj_init_check {
23 _OBJ_INIT_TRUE = 0,
24 _OBJ_INIT_FALSE = -1,
25 _OBJ_INIT_ANY = 1
26};
27
Andrew Boie13ca6fe2017-09-23 12:05:49 -070028/**
Andrew Boie378024c2020-05-28 11:48:54 -070029 * Return true if we are currently handling a system call from user mode
30 *
31 * Inside z_vrfy functions, we always know that we are handling
32 * a system call invoked from user context.
33 *
34 * However, some checks that are only relevant to user mode must
35 * instead be placed deeper within the implementation. This
36 * API is useful to conditionally make these checks.
37 *
38 * For performance reasons, whenever possible, checks should be placed
39 * in the relevant z_vrfy function since these are completely skipped
40 * when a syscall is invoked.
41 *
42 * This will return true only if we are handling a syscall for a
43 * user thread. If the system call was invoked from supervisor mode,
44 * or we are not handling a system call, this will return false.
45 *
46 * @return whether the current context is handling a syscall for a user
47 * mode thread
48 */
49static inline bool z_is_in_user_syscall(void)
50{
51 /* This gets set on entry to the syscall's generasted z_mrsh
52 * function and then cleared on exit. This code path is only
53 * encountered when a syscall is made from user mode, system
54 * calls from supervisor mode bypass everything directly to
55 * the implementation function.
56 */
57 return !k_is_in_isr() && _current->syscall_frame != NULL;
58}
59
60/**
Andrew Boiecee72412017-10-09 15:20:37 -070061 * Ensure a system object is a valid object of the expected type
62 *
63 * Searches for the object and ensures that it is indeed an object
64 * of the expected type, that the caller has the right permissions on it,
65 * and that the object has been initialized.
66 *
67 * This function is intended to be called on the kernel-side system
68 * call handlers to validate kernel object pointers passed in from
69 * userspace.
70 *
Andrew Boie7e3d3d72017-10-10 09:31:32 -070071 * @param ko Kernel object metadata pointer, or NULL
72 * @param otype Expected type of the kernel object, or K_OBJ_ANY if type
73 * doesn't matter
Andrew Boiea2b40ec2017-10-15 14:22:08 -070074 * @param init Indicate whether the object needs to already be in initialized
75 * or uninitialized state, or that we don't care
Andrew Boiecee72412017-10-09 15:20:37 -070076 * @return 0 If the object is valid
77 * -EBADF if not a valid object of the specified type
78 * -EPERM If the caller does not have permissions
79 * -EINVAL Object is not initialized
80 */
Andrew Boie2dc2ecf2020-03-11 07:13:07 -070081int z_object_validate(struct z_object *ko, enum k_objects otype,
82 enum _obj_init_check init);
Andrew Boie7e3d3d72017-10-10 09:31:32 -070083
84/**
Patrik Flykt4344e272019-03-08 14:19:05 -070085 * Dump out error information on failed z_object_validate() call
Andrew Boie7e3d3d72017-10-10 09:31:32 -070086 *
Patrik Flykt4344e272019-03-08 14:19:05 -070087 * @param retval Return value from z_object_validate()
Andrew Boie7e3d3d72017-10-10 09:31:32 -070088 * @param obj Kernel object we were trying to verify
Andrew Boie2dc2ecf2020-03-11 07:13:07 -070089 * @param ko If retval=-EPERM, struct z_object * that was looked up, or NULL
Andrew Boie7e3d3d72017-10-10 09:31:32 -070090 * @param otype Expected type of the kernel object
91 */
Peter Bigot2fcf7622020-05-14 05:06:08 -050092extern void z_dump_object_error(int retval, const void *obj,
93 struct z_object *ko, enum k_objects otype);
Andrew Boie7e3d3d72017-10-10 09:31:32 -070094
95/**
96 * Kernel object validation function
97 *
98 * Retrieve metadata for a kernel object. This function is implemented in
99 * the gperf script footer, see gen_kobject_list.py
100 *
101 * @param obj Address of kernel object to get metadata
102 * @return Kernel object's metadata, or NULL if the parameter wasn't the
103 * memory address of a kernel object
104 */
Peter Bigot2fcf7622020-05-14 05:06:08 -0500105extern struct z_object *z_object_find(const void *obj);
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700106
Andrew Boie2dc2ecf2020-03-11 07:13:07 -0700107typedef void (*_wordlist_cb_func_t)(struct z_object *ko, void *context);
Andrew Boie47f8fd12017-10-05 11:11:02 -0700108
109/**
110 * Iterate over all the kernel object metadata in the system
111 *
Andrew Boie2dc2ecf2020-03-11 07:13:07 -0700112 * @param func function to run on each struct z_object
Andrew Boie47f8fd12017-10-05 11:11:02 -0700113 * @param context Context pointer to pass to each invocation
114 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700115extern void z_object_wordlist_foreach(_wordlist_cb_func_t func, void *context);
Andrew Boie47f8fd12017-10-05 11:11:02 -0700116
117/**
118 * Copy all kernel object permissions from the parent to the child
119 *
120 * @param parent Parent thread, to get permissions from
121 * @param child Child thread, to copy permissions to
122 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700123extern void z_thread_perms_inherit(struct k_thread *parent,
Andrew Boie47f8fd12017-10-05 11:11:02 -0700124 struct k_thread *child);
125
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700126/**
127 * Grant a thread permission to a kernel object
128 *
129 * @param ko Kernel object metadata to update
130 * @param thread The thread to grant permission
131 */
Andrew Boie2dc2ecf2020-03-11 07:13:07 -0700132extern void z_thread_perms_set(struct z_object *ko, struct k_thread *thread);
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700133
134/**
Andrew Boiea89bf012017-10-09 14:47:55 -0700135 * Revoke a thread's permission to a kernel object
136 *
137 * @param ko Kernel object metadata to update
138 * @param thread The thread to grant permission
139 */
Andrew Boie2dc2ecf2020-03-11 07:13:07 -0700140extern void z_thread_perms_clear(struct z_object *ko, struct k_thread *thread);
Andrew Boiea89bf012017-10-09 14:47:55 -0700141
Andrew Boie04caa672017-10-13 13:57:07 -0700142/*
143 * Revoke access to all objects for the provided thread
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700144 *
Patrik Flykt4344e272019-03-08 14:19:05 -0700145 * NOTE: Unlike z_thread_perms_clear(), this function will not clear
Andrew Boiea2b40ec2017-10-15 14:22:08 -0700146 * permissions on public objects.
147 *
Andrew Boie04caa672017-10-13 13:57:07 -0700148 * @param thread Thread object to revoke access
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700149 */
Patrik Flykt4344e272019-03-08 14:19:05 -0700150extern void z_thread_perms_all_clear(struct k_thread *thread);
Andrew Boiecee72412017-10-09 15:20:37 -0700151
152/**
Andrew Boie4a9a4242017-10-05 12:21:36 -0700153 * Clear initialization state of a kernel object
154 *
155 * Intended for thread objects upon thread exit, or for other kernel objects
156 * that were released back to an object pool.
157 *
158 * @param object Address of the kernel object
159 */
Peter Bigot2fcf7622020-05-14 05:06:08 -0500160void z_object_uninit(const void *obj);
Andrew Boie4a9a4242017-10-05 12:21:36 -0700161
Andrew Boiec8188f62018-06-22 14:31:51 -0700162/**
Andrew Boie83fda7c2018-07-31 14:39:11 -0700163 * Initialize and reset permissions to only access by the caller
164 *
165 * Intended for scenarios where objects are fetched from slab pools
166 * and may have had different permissions set during prior usage.
167 *
168 * This is only intended for pools of objects, where such objects are
169 * acquired and released to the pool. If an object has already been used,
170 * we do not want stale permission information hanging around, the object
171 * should only have permissions on the caller. Objects which are not
172 * managed by a pool-like mechanism should not use this API.
173 *
174 * The object will be marked as initialized and the calling thread
175 * granted access to it.
176 *
177 * @param object Address of the kernel object
178 */
Peter Bigot2fcf7622020-05-14 05:06:08 -0500179void z_object_recycle(const void *obj);
Andrew Boie83fda7c2018-07-31 14:39:11 -0700180
181/**
Andrew Boiec8188f62018-06-22 14:31:51 -0700182 * @brief Obtain the size of a C string passed from user mode
183 *
184 * Given a C string pointer and a maximum size, obtain the true
185 * size of the string (not including the trailing NULL byte) just as
186 * if calling strnlen() on it, with the same semantics of strnlen() with
187 * respect to the return value and the maxlen parameter.
188 *
189 * Any memory protection faults triggered by the examination of the string
190 * will be safely handled and an error code returned.
191 *
192 * NOTE: Doesn't guarantee that user mode has actual access to this
193 * string, you will need to still do a Z_SYSCALL_MEMORY_READ()
194 * with the obtained size value to guarantee this.
195 *
196 * @param src String to measure size of
197 * @param maxlen Maximum number of characters to examine
198 * @param err Pointer to int, filled in with -1 on memory error, 0 on
199 * success
200 * @return undefined on error, or strlen(src) if that is less than maxlen, or
201 * maxlen if there were no NULL terminating characters within the
202 * first maxlen bytes.
203 */
204static inline size_t z_user_string_nlen(const char *src, size_t maxlen,
205 int *err)
206{
Andrew Boie4f77c2a2019-11-07 12:43:29 -0800207 return arch_user_string_nlen(src, maxlen, err);
Andrew Boiec8188f62018-06-22 14:31:51 -0700208}
209
210/**
211 * @brief Copy data from userspace into a resource pool allocation
212 *
213 * Given a pointer and a size, allocate a similarly sized buffer in the
214 * caller's resource pool and copy all the data within it to the newly
215 * allocated buffer. This will need to be freed later with k_free().
216 *
217 * Checks are done to ensure that the current thread would have read
218 * access to the provided buffer.
219 *
220 * @param src Source memory address
221 * @param size Size of the memory buffer
222 * @return An allocated buffer with the data copied within it, or NULL
223 * if some error condition occurred
224 */
Andrew Boie526807c2019-03-28 15:17:31 -0700225extern void *z_user_alloc_from_copy(const void *src, size_t size);
Andrew Boiec8188f62018-06-22 14:31:51 -0700226
227/**
228 * @brief Copy data from user mode
229 *
230 * Given a userspace pointer and a size, copies data from it into a provided
231 * destination buffer, performing checks to ensure that the caller would have
232 * appropriate access when in user mode.
233 *
234 * @param dst Destination memory buffer
235 * @param src Source memory buffer, in userspace
236 * @param size Number of bytes to copy
237 * @retval 0 On success
238 * @retval EFAULT On memory access error
239 */
Andrew Boie526807c2019-03-28 15:17:31 -0700240extern int z_user_from_copy(void *dst, const void *src, size_t size);
Andrew Boiec8188f62018-06-22 14:31:51 -0700241
242/**
243 * @brief Copy data to user mode
244 *
245 * Given a userspace pointer and a size, copies data to it from a provided
246 * source buffer, performing checks to ensure that the caller would have
247 * appropriate access when in user mode.
248 *
249 * @param dst Destination memory buffer, in userspace
250 * @param src Source memory buffer
251 * @param size Number of bytes to copy
252 * @retval 0 On success
253 * @retval EFAULT On memory access error
254 */
Andrew Boie526807c2019-03-28 15:17:31 -0700255extern int z_user_to_copy(void *dst, const void *src, size_t size);
Andrew Boiec8188f62018-06-22 14:31:51 -0700256
257/**
258 * @brief Copy a C string from userspace into a resource pool allocation
259 *
260 * Given a C string and maximum length, duplicate the string using an
261 * allocation from the calling thread's resource pool. This will need to be
262 * freed later with k_free().
263 *
264 * Checks are performed to ensure that the string is valid memory and that
265 * the caller has access to it in user mode.
266 *
267 * @param src Source string pointer, in userspace
268 * @param maxlen Maximum size of the string including trailing NULL
269 * @return The duplicated string, or NULL if an error occurred.
270 */
Andrew Boie526807c2019-03-28 15:17:31 -0700271extern char *z_user_string_alloc_copy(const char *src, size_t maxlen);
Andrew Boiec8188f62018-06-22 14:31:51 -0700272
273/**
274 * @brief Copy a C string from userspace into a provided buffer
275 *
276 * Given a C string and maximum length, copy the string into a buffer.
277 *
278 * Checks are performed to ensure that the string is valid memory and that
279 * the caller has access to it in user mode.
280 *
281 * @param dst Destination buffer
282 * @param src Source string pointer, in userspace
283 * @param maxlen Maximum size of the string including trailing NULL
284 * @retval 0 on success
285 * @retval EINVAL if the source string is too long with respect
286 * to maxlen
287 * @retval EFAULT On memory access error
288 */
Andrew Boie526807c2019-03-28 15:17:31 -0700289extern int z_user_string_copy(char *dst, const char *src, size_t maxlen);
Andrew Boiec8188f62018-06-22 14:31:51 -0700290
Andrew Boie8345e5e2018-05-04 15:57:57 -0700291#define Z_OOPS(expr) \
292 do { \
293 if (expr) { \
Andy Ross7353c7f2020-02-06 13:39:03 -0800294 arch_syscall_oops(_current->syscall_frame); \
Andrew Boie8345e5e2018-05-04 15:57:57 -0700295 } \
Flavio Ceolinb3d92022018-09-17 15:56:06 -0700296 } while (false)
Andrew Boie8345e5e2018-05-04 15:57:57 -0700297
Andrew Boie4a9a4242017-10-05 12:21:36 -0700298/**
Andrew Boie13ca6fe2017-09-23 12:05:49 -0700299 * @brief Runtime expression check for system call arguments
300 *
301 * Used in handler functions to perform various runtime checks on arguments,
Andrew Boie231b95c2017-10-09 15:09:29 -0700302 * and generate a kernel oops if anything is not expected, printing a custom
303 * message.
Andrew Boie13ca6fe2017-09-23 12:05:49 -0700304 *
305 * @param expr Boolean expression to verify, a false result will trigger an
306 * oops
Andrew Boie231b95c2017-10-09 15:09:29 -0700307 * @param fmt Printf-style format string (followed by appropriate variadic
308 * arguments) to print on verification failure
Andrew Boief5951cd2019-02-22 15:21:59 -0800309 * @return False on success, True on failure
Andrew Boie13ca6fe2017-09-23 12:05:49 -0700310 */
Andrew Boief5951cd2019-02-22 15:21:59 -0800311#define Z_SYSCALL_VERIFY_MSG(expr, fmt, ...) ({ \
312 bool expr_copy = !(expr); \
313 if (expr_copy) { \
Krzysztof Chruscinski3ed80832020-11-26 19:32:34 +0100314 LOG_MODULE_DECLARE(os, CONFIG_KERNEL_LOG_LEVEL); \
Andrew Boie99b3f862019-09-30 14:25:23 -0700315 LOG_ERR("syscall %s failed check: " fmt, \
316 __func__, ##__VA_ARGS__); \
Andrew Boief5951cd2019-02-22 15:21:59 -0800317 } \
318 expr_copy; })
Andrew Boie13ca6fe2017-09-23 12:05:49 -0700319
320/**
Andrew Boie231b95c2017-10-09 15:09:29 -0700321 * @brief Runtime expression check for system call arguments
322 *
323 * Used in handler functions to perform various runtime checks on arguments,
324 * and generate a kernel oops if anything is not expected.
325 *
326 * @param expr Boolean expression to verify, a false result will trigger an
327 * oops. A stringified version of this expression will be printed.
Andrew Boie8345e5e2018-05-04 15:57:57 -0700328 * @return 0 on success, nonzero on failure
Andrew Boie231b95c2017-10-09 15:09:29 -0700329 */
Andrew Boie8345e5e2018-05-04 15:57:57 -0700330#define Z_SYSCALL_VERIFY(expr) Z_SYSCALL_VERIFY_MSG(expr, #expr)
Andrew Boie231b95c2017-10-09 15:09:29 -0700331
Ioannis Glaropouloscac20e92019-03-13 09:32:44 +0100332/**
333 * @brief Runtime check that a user thread has read and/or write permission to
334 * a memory area
335 *
336 * Checks that the particular memory area is readable and/or writeable by the
337 * currently running thread if the CPU was in user mode, and generates a kernel
338 * oops if it wasn't. Prevents userspace from getting the kernel to read and/or
339 * modify memory the thread does not have access to, or passing in garbage
340 * pointers that would crash/pagefault the kernel if dereferenced.
341 *
342 * @param ptr Memory area to examine
343 * @param size Size of the memory area
344 * @param write If the thread should be able to write to this memory, not just
345 * read it
346 * @return 0 on success, nonzero on failure
347 */
Andrew Boie8345e5e2018-05-04 15:57:57 -0700348#define Z_SYSCALL_MEMORY(ptr, size, write) \
Andrew Boie4f77c2a2019-11-07 12:43:29 -0800349 Z_SYSCALL_VERIFY_MSG(arch_buffer_validate((void *)ptr, size, write) \
Flavio Ceolin92ea2f92018-09-20 16:14:57 -0700350 == 0, \
Andrew Boiedfab6ef2019-11-21 18:56:02 -0800351 "Memory region %p (size %zu) %s access denied", \
352 (void *)(ptr), (size_t)(size), \
Andrew Boie8345e5e2018-05-04 15:57:57 -0700353 write ? "write" : "read")
Andrew Boie32a08a82017-10-10 12:25:55 -0700354
Andrew Boie231b95c2017-10-09 15:09:29 -0700355/**
Andrew Boie32a08a82017-10-10 12:25:55 -0700356 * @brief Runtime check that a user thread has read permission to a memory area
Andrew Boie13ca6fe2017-09-23 12:05:49 -0700357 *
Andrew Boie32a08a82017-10-10 12:25:55 -0700358 * Checks that the particular memory area is readable by the currently running
359 * thread if the CPU was in user mode, and generates a kernel oops if it
360 * wasn't. Prevents userspace from getting the kernel to read memory the thread
361 * does not have access to, or passing in garbage pointers that would
362 * crash/pagefault the kernel if dereferenced.
Andrew Boie13ca6fe2017-09-23 12:05:49 -0700363 *
364 * @param ptr Memory area to examine
365 * @param size Size of the memory area
Andrew Boie8345e5e2018-05-04 15:57:57 -0700366 * @return 0 on success, nonzero on failure
Andrew Boie13ca6fe2017-09-23 12:05:49 -0700367 */
Andrew Boie8345e5e2018-05-04 15:57:57 -0700368#define Z_SYSCALL_MEMORY_READ(ptr, size) \
369 Z_SYSCALL_MEMORY(ptr, size, 0)
Andrew Boie13ca6fe2017-09-23 12:05:49 -0700370
371/**
Andrew Boie32a08a82017-10-10 12:25:55 -0700372 * @brief Runtime check that a user thread has write permission to a memory area
Andrew Boie13ca6fe2017-09-23 12:05:49 -0700373 *
Andrew Boie32a08a82017-10-10 12:25:55 -0700374 * Checks that the particular memory area is readable and writable by the
375 * currently running thread if the CPU was in user mode, and generates a kernel
376 * oops if it wasn't. Prevents userspace from getting the kernel to read or
377 * modify memory the thread does not have access to, or passing in garbage
378 * pointers that would crash/pagefault the kernel if dereferenced.
Andrew Boie13ca6fe2017-09-23 12:05:49 -0700379 *
Andrew Boie32a08a82017-10-10 12:25:55 -0700380 * @param ptr Memory area to examine
381 * @param size Size of the memory area
Andrew Boie8345e5e2018-05-04 15:57:57 -0700382 * @param 0 on success, nonzero on failure
Andrew Boie13ca6fe2017-09-23 12:05:49 -0700383 */
Andrew Boie8345e5e2018-05-04 15:57:57 -0700384#define Z_SYSCALL_MEMORY_WRITE(ptr, size) \
385 Z_SYSCALL_MEMORY(ptr, size, 1)
Andrew Boie32a08a82017-10-10 12:25:55 -0700386
Andrew Boie8345e5e2018-05-04 15:57:57 -0700387#define Z_SYSCALL_MEMORY_ARRAY(ptr, nmemb, size, write) \
388 ({ \
Andrew Boiedfab6ef2019-11-21 18:56:02 -0800389 size_t product; \
390 Z_SYSCALL_VERIFY_MSG(!size_mul_overflow((size_t)(nmemb), \
391 (size_t)(size), \
392 &product), \
393 "%zux%zu array is too large", \
394 (size_t)(nmemb), (size_t)(size)) || \
Andrew Boie8345e5e2018-05-04 15:57:57 -0700395 Z_SYSCALL_MEMORY(ptr, product, write); \
396 })
Andrew Boie38ac2352017-10-10 17:19:32 -0700397
398/**
399 * @brief Validate user thread has read permission for sized array
400 *
401 * Used when the memory region is expressed in terms of number of elements and
402 * each element size, handles any overflow issues with computing the total
403 * array bounds. Otherwise see _SYSCALL_MEMORY_READ.
404 *
405 * @param ptr Memory area to examine
406 * @param nmemb Number of elements in the array
407 * @param size Size of each array element
Andrew Boie8345e5e2018-05-04 15:57:57 -0700408 * @return 0 on success, nonzero on failure
Andrew Boie38ac2352017-10-10 17:19:32 -0700409 */
Andrew Boie8345e5e2018-05-04 15:57:57 -0700410#define Z_SYSCALL_MEMORY_ARRAY_READ(ptr, nmemb, size) \
411 Z_SYSCALL_MEMORY_ARRAY(ptr, nmemb, size, 0)
Andrew Boie38ac2352017-10-10 17:19:32 -0700412
413/**
414 * @brief Validate user thread has read/write permission for sized array
415 *
416 * Used when the memory region is expressed in terms of number of elements and
417 * each element size, handles any overflow issues with computing the total
418 * array bounds. Otherwise see _SYSCALL_MEMORY_WRITE.
419 *
420 * @param ptr Memory area to examine
421 * @param nmemb Number of elements in the array
422 * @param size Size of each array element
Andrew Boie8345e5e2018-05-04 15:57:57 -0700423 * @return 0 on success, nonzero on failure
Andrew Boie38ac2352017-10-10 17:19:32 -0700424 */
Andrew Boie8345e5e2018-05-04 15:57:57 -0700425#define Z_SYSCALL_MEMORY_ARRAY_WRITE(ptr, nmemb, size) \
426 Z_SYSCALL_MEMORY_ARRAY(ptr, nmemb, size, 1)
Andrew Boie38ac2352017-10-10 17:19:32 -0700427
Andrew Boie2dc2ecf2020-03-11 07:13:07 -0700428static inline int z_obj_validation_check(struct z_object *ko,
Peter Bigot2fcf7622020-05-14 05:06:08 -0500429 const void *obj,
Andrew Boie2dc2ecf2020-03-11 07:13:07 -0700430 enum k_objects otype,
431 enum _obj_init_check init)
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700432{
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700433 int ret;
434
Patrik Flykt4344e272019-03-08 14:19:05 -0700435 ret = z_object_validate(ko, otype, init);
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700436
Andrew Boiecb1dd742019-10-01 10:28:32 -0700437#ifdef CONFIG_LOG
Flavio Ceolin76b35182018-12-16 12:48:29 -0800438 if (ret != 0) {
Patrik Flykt4344e272019-03-08 14:19:05 -0700439 z_dump_object_error(ret, obj, ko, otype);
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700440 }
Andrew Boiea2b40ec2017-10-15 14:22:08 -0700441#else
442 ARG_UNUSED(obj);
Andrew Boie7e3d3d72017-10-10 09:31:32 -0700443#endif
444
445 return ret;
446}
447
Andrew Boie8345e5e2018-05-04 15:57:57 -0700448#define Z_SYSCALL_IS_OBJ(ptr, type, init) \
Peter Bigot2fcf7622020-05-14 05:06:08 -0500449 Z_SYSCALL_VERIFY_MSG(z_obj_validation_check( \
450 z_object_find((const void *)ptr), \
451 (const void *)ptr, \
452 type, init) == 0, "access denied")
Andrew Boie13ca6fe2017-09-23 12:05:49 -0700453
Andrew Boie32a08a82017-10-10 12:25:55 -0700454/**
Leandro Pereirac2003672018-04-04 13:50:32 -0700455 * @brief Runtime check driver object pointer for presence of operation
456 *
457 * Validates if the driver object is capable of performing a certain operation.
458 *
459 * @param ptr Untrusted device instance object pointer
460 * @param api_struct Name of the driver API struct (e.g. gpio_driver_api)
461 * @param op Driver operation (e.g. manage_callback)
Andrew Boie8345e5e2018-05-04 15:57:57 -0700462 * @return 0 on success, nonzero on failure
Leandro Pereirac2003672018-04-04 13:50:32 -0700463 */
Andrew Boie8345e5e2018-05-04 15:57:57 -0700464#define Z_SYSCALL_DRIVER_OP(ptr, api_name, op) \
465 ({ \
Leandro Pereirac2003672018-04-04 13:50:32 -0700466 struct api_name *__device__ = (struct api_name *) \
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +0200467 ((const struct device *)ptr)->api; \
Andrew Boie8345e5e2018-05-04 15:57:57 -0700468 Z_SYSCALL_VERIFY_MSG(__device__->op != NULL, \
Leandro Pereirac2003672018-04-04 13:50:32 -0700469 "Operation %s not defined for driver " \
470 "instance %p", \
471 # op, __device__); \
Andrew Boie8345e5e2018-05-04 15:57:57 -0700472 })
Leandro Pereirac2003672018-04-04 13:50:32 -0700473
474/**
Andrew Boie74f114c2018-12-12 13:58:30 -0800475 * @brief Runtime check that device object is of a specific driver type
476 *
477 * Checks that the driver object passed in is initialized, the caller has
478 * correct permissions, and that it belongs to the specified driver
Tomasz Bursztyka48135cd2020-03-13 09:53:54 +0100479 * subsystems. Additionally, all devices store a structure pointer of the
480 * driver's API. If this doesn't match the value provided, the check will fail.
Andrew Boie74f114c2018-12-12 13:58:30 -0800481 *
482 * This provides an easy way to determine if a device object not only
483 * belongs to a particular subsystem, but is of a specific device driver
484 * implementation. Useful for defining out-of-subsystem system calls
485 * which are implemented for only one driver.
486 *
487 * @param _device Untrusted device pointer
488 * @param _dtype Expected kernel object type for the provided device pointer
Tomasz Bursztyka48135cd2020-03-13 09:53:54 +0100489 * @param _api Expected driver API structure memory address
Andrew Boie74f114c2018-12-12 13:58:30 -0800490 * @return 0 on success, nonzero on failure
491 */
Tomasz Bursztyka48135cd2020-03-13 09:53:54 +0100492#define Z_SYSCALL_SPECIFIC_DRIVER(_device, _dtype, _api) \
Andrew Boie74f114c2018-12-12 13:58:30 -0800493 ({ \
Tomasz Bursztykae18fcbb2020-04-30 20:33:38 +0200494 const struct device *_dev = (const struct device *)_device; \
Andrew Boie74f114c2018-12-12 13:58:30 -0800495 Z_SYSCALL_OBJ(_dev, _dtype) || \
Tomasz Bursztyka98d9b012020-05-28 21:23:02 +0200496 Z_SYSCALL_VERIFY_MSG(_dev->api == _api, \
Tomasz Bursztyka48135cd2020-03-13 09:53:54 +0100497 "API structure mismatch"); \
Andrew Boie74f114c2018-12-12 13:58:30 -0800498 })
499
500/**
Andrew Boie32a08a82017-10-10 12:25:55 -0700501 * @brief Runtime check kernel object pointer for non-init functions
502 *
Patrik Flykt4344e272019-03-08 14:19:05 -0700503 * Calls z_object_validate and triggers a kernel oops if the check fails.
Andrew Boiea2b40ec2017-10-15 14:22:08 -0700504 * For use in system call handlers which are not init functions; a fatal
David B. Kinder4600c37ff12017-10-17 15:55:47 -0700505 * error will occur if the object is not initialized.
Andrew Boie32a08a82017-10-10 12:25:55 -0700506 *
507 * @param ptr Untrusted kernel object pointer
508 * @param type Expected kernel object type
Andrew Boie8345e5e2018-05-04 15:57:57 -0700509 * @return 0 on success, nonzero on failure
Andrew Boie32a08a82017-10-10 12:25:55 -0700510 */
Andrew Boie8345e5e2018-05-04 15:57:57 -0700511#define Z_SYSCALL_OBJ(ptr, type) \
512 Z_SYSCALL_IS_OBJ(ptr, type, _OBJ_INIT_TRUE)
Andrew Boie32a08a82017-10-10 12:25:55 -0700513
514/**
515 * @brief Runtime check kernel object pointer for non-init functions
516 *
Andrew Boiea2b40ec2017-10-15 14:22:08 -0700517 * See description of _SYSCALL_IS_OBJ. No initialization checks are done.
518 * Intended for init functions where objects may be re-initialized at will.
Andrew Boie32a08a82017-10-10 12:25:55 -0700519 *
520 * @param ptr Untrusted kernel object pointer
521 * @param type Expected kernel object type
Andrew Boie8345e5e2018-05-04 15:57:57 -0700522 * @return 0 on success, nonzero on failure
Andrew Boie32a08a82017-10-10 12:25:55 -0700523 */
524
Andrew Boie8345e5e2018-05-04 15:57:57 -0700525#define Z_SYSCALL_OBJ_INIT(ptr, type) \
526 Z_SYSCALL_IS_OBJ(ptr, type, _OBJ_INIT_ANY)
Andrew Boiea2b40ec2017-10-15 14:22:08 -0700527
528/**
529 * @brief Runtime check kernel object pointer for non-init functions
530 *
531 * See description of _SYSCALL_IS_OBJ. Triggers a fatal error if the object is
532 * initialized. Intended for init functions where objects, once initialized,
533 * can only be re-used when their initialization state expires due to some
534 * other mechanism.
535 *
536 * @param ptr Untrusted kernel object pointer
537 * @param type Expected kernel object type
Andrew Boie8345e5e2018-05-04 15:57:57 -0700538 * @return 0 on success, nonzero on failure
Andrew Boiea2b40ec2017-10-15 14:22:08 -0700539 */
540
Andrew Boie8345e5e2018-05-04 15:57:57 -0700541#define Z_SYSCALL_OBJ_NEVER_INIT(ptr, type) \
542 Z_SYSCALL_IS_OBJ(ptr, type, _OBJ_INIT_FALSE)
Andrew Boie32a08a82017-10-10 12:25:55 -0700543
Leandro Pereirac2003672018-04-04 13:50:32 -0700544#include <driver-validation.h>
545
Andrew Boie13ca6fe2017-09-23 12:05:49 -0700546#endif /* _ASMLANGUAGE */
547
548#endif /* CONFIG_USERSPACE */
549
Stephanos Ioannidis2d746042019-10-25 00:08:21 +0900550#endif /* ZEPHYR_INCLUDE_SYSCALL_HANDLER_H_ */