Andrew Boie | 13ca6fe | 2017-09-23 12:05:49 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017, Intel Corporation |
| 3 | * |
Anas Nashif | c7f5cc9 | 2018-04-12 13:45:33 -0500 | [diff] [blame] | 4 | * SPDX-License-Identifier: Apache-2.0 |
Andrew Boie | 13ca6fe | 2017-09-23 12:05:49 -0700 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | |
Stephanos Ioannidis | 2d74604 | 2019-10-25 00:08:21 +0900 | [diff] [blame] | 8 | #ifndef ZEPHYR_INCLUDE_SYSCALL_HANDLER_H_ |
| 9 | #define ZEPHYR_INCLUDE_SYSCALL_HANDLER_H_ |
Andrew Boie | 13ca6fe | 2017-09-23 12:05:49 -0700 | [diff] [blame] | 10 | |
| 11 | #ifdef CONFIG_USERSPACE |
| 12 | |
| 13 | #ifndef _ASMLANGUAGE |
| 14 | #include <kernel.h> |
Stephanos Ioannidis | 2d74604 | 2019-10-25 00:08:21 +0900 | [diff] [blame] | 15 | #include <sys/arch_interface.h> |
Anas Nashif | 6ecadb0 | 2019-06-26 10:33:45 -0400 | [diff] [blame] | 16 | #include <sys/math_extras.h> |
Flavio Ceolin | b3d9202 | 2018-09-17 15:56:06 -0700 | [diff] [blame] | 17 | #include <stdbool.h> |
Andrew Boie | 99b3f86 | 2019-09-30 14:25:23 -0700 | [diff] [blame] | 18 | #include <logging/log.h> |
Andrew Boie | 13ca6fe | 2017-09-23 12:05:49 -0700 | [diff] [blame] | 19 | |
| 20 | extern const _k_syscall_handler_t _k_syscall_table[K_SYSCALL_LIMIT]; |
| 21 | |
Andrew Boie | a2b40ec | 2017-10-15 14:22:08 -0700 | [diff] [blame] | 22 | enum _obj_init_check { |
| 23 | _OBJ_INIT_TRUE = 0, |
| 24 | _OBJ_INIT_FALSE = -1, |
| 25 | _OBJ_INIT_ANY = 1 |
| 26 | }; |
| 27 | |
Andrew Boie | 13ca6fe | 2017-09-23 12:05:49 -0700 | [diff] [blame] | 28 | /** |
Andrew Boie | 378024c | 2020-05-28 11:48:54 -0700 | [diff] [blame] | 29 | * 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 | */ |
| 49 | static 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 Boie | cee7241 | 2017-10-09 15:20:37 -0700 | [diff] [blame] | 61 | * 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 Boie | 7e3d3d7 | 2017-10-10 09:31:32 -0700 | [diff] [blame] | 71 | * @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 Boie | a2b40ec | 2017-10-15 14:22:08 -0700 | [diff] [blame] | 74 | * @param init Indicate whether the object needs to already be in initialized |
| 75 | * or uninitialized state, or that we don't care |
Andrew Boie | cee7241 | 2017-10-09 15:20:37 -0700 | [diff] [blame] | 76 | * @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 Boie | 2dc2ecf | 2020-03-11 07:13:07 -0700 | [diff] [blame] | 81 | int z_object_validate(struct z_object *ko, enum k_objects otype, |
| 82 | enum _obj_init_check init); |
Andrew Boie | 7e3d3d7 | 2017-10-10 09:31:32 -0700 | [diff] [blame] | 83 | |
| 84 | /** |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 85 | * Dump out error information on failed z_object_validate() call |
Andrew Boie | 7e3d3d7 | 2017-10-10 09:31:32 -0700 | [diff] [blame] | 86 | * |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 87 | * @param retval Return value from z_object_validate() |
Andrew Boie | 7e3d3d7 | 2017-10-10 09:31:32 -0700 | [diff] [blame] | 88 | * @param obj Kernel object we were trying to verify |
Andrew Boie | 2dc2ecf | 2020-03-11 07:13:07 -0700 | [diff] [blame] | 89 | * @param ko If retval=-EPERM, struct z_object * that was looked up, or NULL |
Andrew Boie | 7e3d3d7 | 2017-10-10 09:31:32 -0700 | [diff] [blame] | 90 | * @param otype Expected type of the kernel object |
| 91 | */ |
Peter Bigot | 2fcf762 | 2020-05-14 05:06:08 -0500 | [diff] [blame] | 92 | extern void z_dump_object_error(int retval, const void *obj, |
| 93 | struct z_object *ko, enum k_objects otype); |
Andrew Boie | 7e3d3d7 | 2017-10-10 09:31:32 -0700 | [diff] [blame] | 94 | |
| 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 Bigot | 2fcf762 | 2020-05-14 05:06:08 -0500 | [diff] [blame] | 105 | extern struct z_object *z_object_find(const void *obj); |
Andrew Boie | 7e3d3d7 | 2017-10-10 09:31:32 -0700 | [diff] [blame] | 106 | |
Andrew Boie | 2dc2ecf | 2020-03-11 07:13:07 -0700 | [diff] [blame] | 107 | typedef void (*_wordlist_cb_func_t)(struct z_object *ko, void *context); |
Andrew Boie | 47f8fd1 | 2017-10-05 11:11:02 -0700 | [diff] [blame] | 108 | |
| 109 | /** |
| 110 | * Iterate over all the kernel object metadata in the system |
| 111 | * |
Andrew Boie | 2dc2ecf | 2020-03-11 07:13:07 -0700 | [diff] [blame] | 112 | * @param func function to run on each struct z_object |
Andrew Boie | 47f8fd1 | 2017-10-05 11:11:02 -0700 | [diff] [blame] | 113 | * @param context Context pointer to pass to each invocation |
| 114 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 115 | extern void z_object_wordlist_foreach(_wordlist_cb_func_t func, void *context); |
Andrew Boie | 47f8fd1 | 2017-10-05 11:11:02 -0700 | [diff] [blame] | 116 | |
| 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 Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 123 | extern void z_thread_perms_inherit(struct k_thread *parent, |
Andrew Boie | 47f8fd1 | 2017-10-05 11:11:02 -0700 | [diff] [blame] | 124 | struct k_thread *child); |
| 125 | |
Andrew Boie | 7e3d3d7 | 2017-10-10 09:31:32 -0700 | [diff] [blame] | 126 | /** |
| 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 Boie | 2dc2ecf | 2020-03-11 07:13:07 -0700 | [diff] [blame] | 132 | extern void z_thread_perms_set(struct z_object *ko, struct k_thread *thread); |
Andrew Boie | 7e3d3d7 | 2017-10-10 09:31:32 -0700 | [diff] [blame] | 133 | |
| 134 | /** |
Andrew Boie | a89bf01 | 2017-10-09 14:47:55 -0700 | [diff] [blame] | 135 | * 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 Boie | 2dc2ecf | 2020-03-11 07:13:07 -0700 | [diff] [blame] | 140 | extern void z_thread_perms_clear(struct z_object *ko, struct k_thread *thread); |
Andrew Boie | a89bf01 | 2017-10-09 14:47:55 -0700 | [diff] [blame] | 141 | |
Andrew Boie | 04caa67 | 2017-10-13 13:57:07 -0700 | [diff] [blame] | 142 | /* |
| 143 | * Revoke access to all objects for the provided thread |
Andrew Boie | 7e3d3d7 | 2017-10-10 09:31:32 -0700 | [diff] [blame] | 144 | * |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 145 | * NOTE: Unlike z_thread_perms_clear(), this function will not clear |
Andrew Boie | a2b40ec | 2017-10-15 14:22:08 -0700 | [diff] [blame] | 146 | * permissions on public objects. |
| 147 | * |
Andrew Boie | 04caa67 | 2017-10-13 13:57:07 -0700 | [diff] [blame] | 148 | * @param thread Thread object to revoke access |
Andrew Boie | 7e3d3d7 | 2017-10-10 09:31:32 -0700 | [diff] [blame] | 149 | */ |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 150 | extern void z_thread_perms_all_clear(struct k_thread *thread); |
Andrew Boie | cee7241 | 2017-10-09 15:20:37 -0700 | [diff] [blame] | 151 | |
| 152 | /** |
Andrew Boie | 4a9a424 | 2017-10-05 12:21:36 -0700 | [diff] [blame] | 153 | * 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 Bigot | 2fcf762 | 2020-05-14 05:06:08 -0500 | [diff] [blame] | 160 | void z_object_uninit(const void *obj); |
Andrew Boie | 4a9a424 | 2017-10-05 12:21:36 -0700 | [diff] [blame] | 161 | |
Andrew Boie | c8188f6 | 2018-06-22 14:31:51 -0700 | [diff] [blame] | 162 | /** |
Andrew Boie | 83fda7c | 2018-07-31 14:39:11 -0700 | [diff] [blame] | 163 | * 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 Bigot | 2fcf762 | 2020-05-14 05:06:08 -0500 | [diff] [blame] | 179 | void z_object_recycle(const void *obj); |
Andrew Boie | 83fda7c | 2018-07-31 14:39:11 -0700 | [diff] [blame] | 180 | |
| 181 | /** |
Andrew Boie | c8188f6 | 2018-06-22 14:31:51 -0700 | [diff] [blame] | 182 | * @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 | */ |
| 204 | static inline size_t z_user_string_nlen(const char *src, size_t maxlen, |
| 205 | int *err) |
| 206 | { |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 207 | return arch_user_string_nlen(src, maxlen, err); |
Andrew Boie | c8188f6 | 2018-06-22 14:31:51 -0700 | [diff] [blame] | 208 | } |
| 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 Boie | 526807c | 2019-03-28 15:17:31 -0700 | [diff] [blame] | 225 | extern void *z_user_alloc_from_copy(const void *src, size_t size); |
Andrew Boie | c8188f6 | 2018-06-22 14:31:51 -0700 | [diff] [blame] | 226 | |
| 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 Boie | 526807c | 2019-03-28 15:17:31 -0700 | [diff] [blame] | 240 | extern int z_user_from_copy(void *dst, const void *src, size_t size); |
Andrew Boie | c8188f6 | 2018-06-22 14:31:51 -0700 | [diff] [blame] | 241 | |
| 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 Boie | 526807c | 2019-03-28 15:17:31 -0700 | [diff] [blame] | 255 | extern int z_user_to_copy(void *dst, const void *src, size_t size); |
Andrew Boie | c8188f6 | 2018-06-22 14:31:51 -0700 | [diff] [blame] | 256 | |
| 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 Boie | 526807c | 2019-03-28 15:17:31 -0700 | [diff] [blame] | 271 | extern char *z_user_string_alloc_copy(const char *src, size_t maxlen); |
Andrew Boie | c8188f6 | 2018-06-22 14:31:51 -0700 | [diff] [blame] | 272 | |
| 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 Boie | 526807c | 2019-03-28 15:17:31 -0700 | [diff] [blame] | 289 | extern int z_user_string_copy(char *dst, const char *src, size_t maxlen); |
Andrew Boie | c8188f6 | 2018-06-22 14:31:51 -0700 | [diff] [blame] | 290 | |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 291 | #define Z_OOPS(expr) \ |
| 292 | do { \ |
| 293 | if (expr) { \ |
Andy Ross | 7353c7f | 2020-02-06 13:39:03 -0800 | [diff] [blame] | 294 | arch_syscall_oops(_current->syscall_frame); \ |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 295 | } \ |
Flavio Ceolin | b3d9202 | 2018-09-17 15:56:06 -0700 | [diff] [blame] | 296 | } while (false) |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 297 | |
Andrew Boie | 4a9a424 | 2017-10-05 12:21:36 -0700 | [diff] [blame] | 298 | /** |
Andrew Boie | 13ca6fe | 2017-09-23 12:05:49 -0700 | [diff] [blame] | 299 | * @brief Runtime expression check for system call arguments |
| 300 | * |
| 301 | * Used in handler functions to perform various runtime checks on arguments, |
Andrew Boie | 231b95c | 2017-10-09 15:09:29 -0700 | [diff] [blame] | 302 | * and generate a kernel oops if anything is not expected, printing a custom |
| 303 | * message. |
Andrew Boie | 13ca6fe | 2017-09-23 12:05:49 -0700 | [diff] [blame] | 304 | * |
| 305 | * @param expr Boolean expression to verify, a false result will trigger an |
| 306 | * oops |
Andrew Boie | 231b95c | 2017-10-09 15:09:29 -0700 | [diff] [blame] | 307 | * @param fmt Printf-style format string (followed by appropriate variadic |
| 308 | * arguments) to print on verification failure |
Andrew Boie | f5951cd | 2019-02-22 15:21:59 -0800 | [diff] [blame] | 309 | * @return False on success, True on failure |
Andrew Boie | 13ca6fe | 2017-09-23 12:05:49 -0700 | [diff] [blame] | 310 | */ |
Andrew Boie | f5951cd | 2019-02-22 15:21:59 -0800 | [diff] [blame] | 311 | #define Z_SYSCALL_VERIFY_MSG(expr, fmt, ...) ({ \ |
| 312 | bool expr_copy = !(expr); \ |
| 313 | if (expr_copy) { \ |
Krzysztof Chruscinski | 3ed8083 | 2020-11-26 19:32:34 +0100 | [diff] [blame] | 314 | LOG_MODULE_DECLARE(os, CONFIG_KERNEL_LOG_LEVEL); \ |
Andrew Boie | 99b3f86 | 2019-09-30 14:25:23 -0700 | [diff] [blame] | 315 | LOG_ERR("syscall %s failed check: " fmt, \ |
| 316 | __func__, ##__VA_ARGS__); \ |
Andrew Boie | f5951cd | 2019-02-22 15:21:59 -0800 | [diff] [blame] | 317 | } \ |
| 318 | expr_copy; }) |
Andrew Boie | 13ca6fe | 2017-09-23 12:05:49 -0700 | [diff] [blame] | 319 | |
| 320 | /** |
Andrew Boie | 231b95c | 2017-10-09 15:09:29 -0700 | [diff] [blame] | 321 | * @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 Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 328 | * @return 0 on success, nonzero on failure |
Andrew Boie | 231b95c | 2017-10-09 15:09:29 -0700 | [diff] [blame] | 329 | */ |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 330 | #define Z_SYSCALL_VERIFY(expr) Z_SYSCALL_VERIFY_MSG(expr, #expr) |
Andrew Boie | 231b95c | 2017-10-09 15:09:29 -0700 | [diff] [blame] | 331 | |
Ioannis Glaropoulos | cac20e9 | 2019-03-13 09:32:44 +0100 | [diff] [blame] | 332 | /** |
| 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 Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 348 | #define Z_SYSCALL_MEMORY(ptr, size, write) \ |
Andrew Boie | 4f77c2a | 2019-11-07 12:43:29 -0800 | [diff] [blame] | 349 | Z_SYSCALL_VERIFY_MSG(arch_buffer_validate((void *)ptr, size, write) \ |
Flavio Ceolin | 92ea2f9 | 2018-09-20 16:14:57 -0700 | [diff] [blame] | 350 | == 0, \ |
Andrew Boie | dfab6ef | 2019-11-21 18:56:02 -0800 | [diff] [blame] | 351 | "Memory region %p (size %zu) %s access denied", \ |
| 352 | (void *)(ptr), (size_t)(size), \ |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 353 | write ? "write" : "read") |
Andrew Boie | 32a08a8 | 2017-10-10 12:25:55 -0700 | [diff] [blame] | 354 | |
Andrew Boie | 231b95c | 2017-10-09 15:09:29 -0700 | [diff] [blame] | 355 | /** |
Andrew Boie | 32a08a8 | 2017-10-10 12:25:55 -0700 | [diff] [blame] | 356 | * @brief Runtime check that a user thread has read permission to a memory area |
Andrew Boie | 13ca6fe | 2017-09-23 12:05:49 -0700 | [diff] [blame] | 357 | * |
Andrew Boie | 32a08a8 | 2017-10-10 12:25:55 -0700 | [diff] [blame] | 358 | * 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 Boie | 13ca6fe | 2017-09-23 12:05:49 -0700 | [diff] [blame] | 363 | * |
| 364 | * @param ptr Memory area to examine |
| 365 | * @param size Size of the memory area |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 366 | * @return 0 on success, nonzero on failure |
Andrew Boie | 13ca6fe | 2017-09-23 12:05:49 -0700 | [diff] [blame] | 367 | */ |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 368 | #define Z_SYSCALL_MEMORY_READ(ptr, size) \ |
| 369 | Z_SYSCALL_MEMORY(ptr, size, 0) |
Andrew Boie | 13ca6fe | 2017-09-23 12:05:49 -0700 | [diff] [blame] | 370 | |
| 371 | /** |
Andrew Boie | 32a08a8 | 2017-10-10 12:25:55 -0700 | [diff] [blame] | 372 | * @brief Runtime check that a user thread has write permission to a memory area |
Andrew Boie | 13ca6fe | 2017-09-23 12:05:49 -0700 | [diff] [blame] | 373 | * |
Andrew Boie | 32a08a8 | 2017-10-10 12:25:55 -0700 | [diff] [blame] | 374 | * 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 Boie | 13ca6fe | 2017-09-23 12:05:49 -0700 | [diff] [blame] | 379 | * |
Andrew Boie | 32a08a8 | 2017-10-10 12:25:55 -0700 | [diff] [blame] | 380 | * @param ptr Memory area to examine |
| 381 | * @param size Size of the memory area |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 382 | * @param 0 on success, nonzero on failure |
Andrew Boie | 13ca6fe | 2017-09-23 12:05:49 -0700 | [diff] [blame] | 383 | */ |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 384 | #define Z_SYSCALL_MEMORY_WRITE(ptr, size) \ |
| 385 | Z_SYSCALL_MEMORY(ptr, size, 1) |
Andrew Boie | 32a08a8 | 2017-10-10 12:25:55 -0700 | [diff] [blame] | 386 | |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 387 | #define Z_SYSCALL_MEMORY_ARRAY(ptr, nmemb, size, write) \ |
| 388 | ({ \ |
Andrew Boie | dfab6ef | 2019-11-21 18:56:02 -0800 | [diff] [blame] | 389 | 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 Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 395 | Z_SYSCALL_MEMORY(ptr, product, write); \ |
| 396 | }) |
Andrew Boie | 38ac235 | 2017-10-10 17:19:32 -0700 | [diff] [blame] | 397 | |
| 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 Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 408 | * @return 0 on success, nonzero on failure |
Andrew Boie | 38ac235 | 2017-10-10 17:19:32 -0700 | [diff] [blame] | 409 | */ |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 410 | #define Z_SYSCALL_MEMORY_ARRAY_READ(ptr, nmemb, size) \ |
| 411 | Z_SYSCALL_MEMORY_ARRAY(ptr, nmemb, size, 0) |
Andrew Boie | 38ac235 | 2017-10-10 17:19:32 -0700 | [diff] [blame] | 412 | |
| 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 Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 423 | * @return 0 on success, nonzero on failure |
Andrew Boie | 38ac235 | 2017-10-10 17:19:32 -0700 | [diff] [blame] | 424 | */ |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 425 | #define Z_SYSCALL_MEMORY_ARRAY_WRITE(ptr, nmemb, size) \ |
| 426 | Z_SYSCALL_MEMORY_ARRAY(ptr, nmemb, size, 1) |
Andrew Boie | 38ac235 | 2017-10-10 17:19:32 -0700 | [diff] [blame] | 427 | |
Andrew Boie | 2dc2ecf | 2020-03-11 07:13:07 -0700 | [diff] [blame] | 428 | static inline int z_obj_validation_check(struct z_object *ko, |
Peter Bigot | 2fcf762 | 2020-05-14 05:06:08 -0500 | [diff] [blame] | 429 | const void *obj, |
Andrew Boie | 2dc2ecf | 2020-03-11 07:13:07 -0700 | [diff] [blame] | 430 | enum k_objects otype, |
| 431 | enum _obj_init_check init) |
Andrew Boie | 7e3d3d7 | 2017-10-10 09:31:32 -0700 | [diff] [blame] | 432 | { |
Andrew Boie | 7e3d3d7 | 2017-10-10 09:31:32 -0700 | [diff] [blame] | 433 | int ret; |
| 434 | |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 435 | ret = z_object_validate(ko, otype, init); |
Andrew Boie | 7e3d3d7 | 2017-10-10 09:31:32 -0700 | [diff] [blame] | 436 | |
Andrew Boie | cb1dd74 | 2019-10-01 10:28:32 -0700 | [diff] [blame] | 437 | #ifdef CONFIG_LOG |
Flavio Ceolin | 76b3518 | 2018-12-16 12:48:29 -0800 | [diff] [blame] | 438 | if (ret != 0) { |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 439 | z_dump_object_error(ret, obj, ko, otype); |
Andrew Boie | 7e3d3d7 | 2017-10-10 09:31:32 -0700 | [diff] [blame] | 440 | } |
Andrew Boie | a2b40ec | 2017-10-15 14:22:08 -0700 | [diff] [blame] | 441 | #else |
| 442 | ARG_UNUSED(obj); |
Andrew Boie | 7e3d3d7 | 2017-10-10 09:31:32 -0700 | [diff] [blame] | 443 | #endif |
| 444 | |
| 445 | return ret; |
| 446 | } |
| 447 | |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 448 | #define Z_SYSCALL_IS_OBJ(ptr, type, init) \ |
Peter Bigot | 2fcf762 | 2020-05-14 05:06:08 -0500 | [diff] [blame] | 449 | 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 Boie | 13ca6fe | 2017-09-23 12:05:49 -0700 | [diff] [blame] | 453 | |
Andrew Boie | 32a08a8 | 2017-10-10 12:25:55 -0700 | [diff] [blame] | 454 | /** |
Leandro Pereira | c200367 | 2018-04-04 13:50:32 -0700 | [diff] [blame] | 455 | * @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 Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 462 | * @return 0 on success, nonzero on failure |
Leandro Pereira | c200367 | 2018-04-04 13:50:32 -0700 | [diff] [blame] | 463 | */ |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 464 | #define Z_SYSCALL_DRIVER_OP(ptr, api_name, op) \ |
| 465 | ({ \ |
Leandro Pereira | c200367 | 2018-04-04 13:50:32 -0700 | [diff] [blame] | 466 | struct api_name *__device__ = (struct api_name *) \ |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 467 | ((const struct device *)ptr)->api; \ |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 468 | Z_SYSCALL_VERIFY_MSG(__device__->op != NULL, \ |
Leandro Pereira | c200367 | 2018-04-04 13:50:32 -0700 | [diff] [blame] | 469 | "Operation %s not defined for driver " \ |
| 470 | "instance %p", \ |
| 471 | # op, __device__); \ |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 472 | }) |
Leandro Pereira | c200367 | 2018-04-04 13:50:32 -0700 | [diff] [blame] | 473 | |
| 474 | /** |
Andrew Boie | 74f114c | 2018-12-12 13:58:30 -0800 | [diff] [blame] | 475 | * @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 Bursztyka | 48135cd | 2020-03-13 09:53:54 +0100 | [diff] [blame] | 479 | * 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 Boie | 74f114c | 2018-12-12 13:58:30 -0800 | [diff] [blame] | 481 | * |
| 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 Bursztyka | 48135cd | 2020-03-13 09:53:54 +0100 | [diff] [blame] | 489 | * @param _api Expected driver API structure memory address |
Andrew Boie | 74f114c | 2018-12-12 13:58:30 -0800 | [diff] [blame] | 490 | * @return 0 on success, nonzero on failure |
| 491 | */ |
Tomasz Bursztyka | 48135cd | 2020-03-13 09:53:54 +0100 | [diff] [blame] | 492 | #define Z_SYSCALL_SPECIFIC_DRIVER(_device, _dtype, _api) \ |
Andrew Boie | 74f114c | 2018-12-12 13:58:30 -0800 | [diff] [blame] | 493 | ({ \ |
Tomasz Bursztyka | e18fcbb | 2020-04-30 20:33:38 +0200 | [diff] [blame] | 494 | const struct device *_dev = (const struct device *)_device; \ |
Andrew Boie | 74f114c | 2018-12-12 13:58:30 -0800 | [diff] [blame] | 495 | Z_SYSCALL_OBJ(_dev, _dtype) || \ |
Tomasz Bursztyka | 98d9b01 | 2020-05-28 21:23:02 +0200 | [diff] [blame] | 496 | Z_SYSCALL_VERIFY_MSG(_dev->api == _api, \ |
Tomasz Bursztyka | 48135cd | 2020-03-13 09:53:54 +0100 | [diff] [blame] | 497 | "API structure mismatch"); \ |
Andrew Boie | 74f114c | 2018-12-12 13:58:30 -0800 | [diff] [blame] | 498 | }) |
| 499 | |
| 500 | /** |
Andrew Boie | 32a08a8 | 2017-10-10 12:25:55 -0700 | [diff] [blame] | 501 | * @brief Runtime check kernel object pointer for non-init functions |
| 502 | * |
Patrik Flykt | 4344e27 | 2019-03-08 14:19:05 -0700 | [diff] [blame] | 503 | * Calls z_object_validate and triggers a kernel oops if the check fails. |
Andrew Boie | a2b40ec | 2017-10-15 14:22:08 -0700 | [diff] [blame] | 504 | * For use in system call handlers which are not init functions; a fatal |
David B. Kinder | 4600c37ff1 | 2017-10-17 15:55:47 -0700 | [diff] [blame] | 505 | * error will occur if the object is not initialized. |
Andrew Boie | 32a08a8 | 2017-10-10 12:25:55 -0700 | [diff] [blame] | 506 | * |
| 507 | * @param ptr Untrusted kernel object pointer |
| 508 | * @param type Expected kernel object type |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 509 | * @return 0 on success, nonzero on failure |
Andrew Boie | 32a08a8 | 2017-10-10 12:25:55 -0700 | [diff] [blame] | 510 | */ |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 511 | #define Z_SYSCALL_OBJ(ptr, type) \ |
| 512 | Z_SYSCALL_IS_OBJ(ptr, type, _OBJ_INIT_TRUE) |
Andrew Boie | 32a08a8 | 2017-10-10 12:25:55 -0700 | [diff] [blame] | 513 | |
| 514 | /** |
| 515 | * @brief Runtime check kernel object pointer for non-init functions |
| 516 | * |
Andrew Boie | a2b40ec | 2017-10-15 14:22:08 -0700 | [diff] [blame] | 517 | * 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 Boie | 32a08a8 | 2017-10-10 12:25:55 -0700 | [diff] [blame] | 519 | * |
| 520 | * @param ptr Untrusted kernel object pointer |
| 521 | * @param type Expected kernel object type |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 522 | * @return 0 on success, nonzero on failure |
Andrew Boie | 32a08a8 | 2017-10-10 12:25:55 -0700 | [diff] [blame] | 523 | */ |
| 524 | |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 525 | #define Z_SYSCALL_OBJ_INIT(ptr, type) \ |
| 526 | Z_SYSCALL_IS_OBJ(ptr, type, _OBJ_INIT_ANY) |
Andrew Boie | a2b40ec | 2017-10-15 14:22:08 -0700 | [diff] [blame] | 527 | |
| 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 Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 538 | * @return 0 on success, nonzero on failure |
Andrew Boie | a2b40ec | 2017-10-15 14:22:08 -0700 | [diff] [blame] | 539 | */ |
| 540 | |
Andrew Boie | 8345e5e | 2018-05-04 15:57:57 -0700 | [diff] [blame] | 541 | #define Z_SYSCALL_OBJ_NEVER_INIT(ptr, type) \ |
| 542 | Z_SYSCALL_IS_OBJ(ptr, type, _OBJ_INIT_FALSE) |
Andrew Boie | 32a08a8 | 2017-10-10 12:25:55 -0700 | [diff] [blame] | 543 | |
Leandro Pereira | c200367 | 2018-04-04 13:50:32 -0700 | [diff] [blame] | 544 | #include <driver-validation.h> |
| 545 | |
Andrew Boie | 13ca6fe | 2017-09-23 12:05:49 -0700 | [diff] [blame] | 546 | #endif /* _ASMLANGUAGE */ |
| 547 | |
| 548 | #endif /* CONFIG_USERSPACE */ |
| 549 | |
Stephanos Ioannidis | 2d74604 | 2019-10-25 00:08:21 +0900 | [diff] [blame] | 550 | #endif /* ZEPHYR_INCLUDE_SYSCALL_HANDLER_H_ */ |