1 /*
2  * Copyright (c) 2019 Intel Corporation.
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/internal/syscall_handler.h>
8 #include <zephyr/logging/log.h>
9 
10 LOG_MODULE_REGISTER(app_syscall);
11 
12 /* Implementation of magic_syscall() which is a system call just for this
13  * application, not part of the kernel codebase.
14  *
15  * It's trivial, update the supplied cookie value by 1, only if it is less than
16  * 42. This is just for demonstration purposes to show how syscalls can be
17  * defined in application code.
18  */
z_impl_magic_syscall(unsigned int * cookie)19 int z_impl_magic_syscall(unsigned int *cookie)
20 {
21 	LOG_DBG("magic syscall: got a cookie %u", *cookie);
22 
23 	if (*cookie > 42) {
24 		LOG_ERR("bad cookie :(");
25 		return -EINVAL;
26 	}
27 
28 	*cookie = *cookie + 1;
29 
30 	return 0;
31 }
32 
z_vrfy_magic_syscall(unsigned int * cookie)33 static int z_vrfy_magic_syscall(unsigned int *cookie)
34 {
35 	unsigned int cookie_copy;
36 	int ret;
37 
38 	/* Confirm that this user-supplied pointer is valid memory that
39 	 * can be accessed. If it's OK, copy into cookie_copy.
40 	 */
41 	if (k_usermode_from_copy(&cookie_copy, cookie, sizeof(*cookie)) != 0) {
42 		return -EPERM;
43 	}
44 
45 	/* We pass to the implementation the *copy*, to prevent TOCTOU attacks
46 	 */
47 	ret = z_impl_magic_syscall(&cookie_copy);
48 
49 	if (ret == 0 &&
50 	    k_usermode_to_copy(cookie, &cookie_copy, sizeof(*cookie)) != 0) {
51 		return -EPERM;
52 	}
53 
54 	return ret;
55 }
56 
57 #include <syscalls/magic_syscall_mrsh.c>
58