1 /* SPDX-License-Identifier: GPL-2.0-only */
3 * Copyright (c) 2020 Christoph Hellwig.
5 * Support for "universal" pointers that can point to either kernel or userspace
8 #ifndef _LINUX_SOCKPTR_H
9 #define _LINUX_SOCKPTR_H
11 #include <linux/slab.h>
12 #include <linux/uaccess.h>
22 static inline bool sockptr_is_kernel(sockptr_t sockptr)
24 return sockptr.is_kernel;
27 static inline sockptr_t KERNEL_SOCKPTR(void *p)
29 return (sockptr_t) { .kernel = p, .is_kernel = true };
32 static inline sockptr_t USER_SOCKPTR(void __user *p)
34 return (sockptr_t) { .user = p };
37 static inline bool sockptr_is_null(sockptr_t sockptr)
39 if (sockptr_is_kernel(sockptr))
40 return !sockptr.kernel;
44 static inline int copy_from_sockptr_offset(void *dst, sockptr_t src,
45 size_t offset, size_t size)
47 if (!sockptr_is_kernel(src))
48 return copy_from_user(dst, src.user + offset, size);
49 memcpy(dst, src.kernel + offset, size);
54 * This is unsafe, unless caller checked user provided optlen.
55 * Prefer copy_safe_from_sockptr() instead.
57 static inline int copy_from_sockptr(void *dst, sockptr_t src, size_t size)
59 return copy_from_sockptr_offset(dst, src, 0, size);
63 * copy_safe_from_sockptr: copy a struct from sockptr
64 * @dst: Destination address, in kernel space. This buffer must be @ksize
66 * @ksize: Size of @dst struct.
67 * @optval: Source address. (in user or kernel space)
68 * @optlen: Size of @optval data.
71 * * -EINVAL: @optlen < @ksize
72 * * -EFAULT: access to userspace failed.
73 * * 0 : @ksize bytes were copied
75 static inline int copy_safe_from_sockptr(void *dst, size_t ksize,
76 sockptr_t optval, unsigned int optlen)
80 return copy_from_sockptr(dst, optval, ksize);
83 static inline int copy_struct_from_sockptr(void *dst, size_t ksize,
84 sockptr_t src, size_t usize)
86 size_t size = min(ksize, usize);
87 size_t rest = max(ksize, usize) - size;
89 if (!sockptr_is_kernel(src))
90 return copy_struct_from_user(dst, ksize, src.user, size);
93 memset(dst + size, 0, rest);
94 } else if (usize > ksize) {
102 memcpy(dst, src.kernel, size);
106 static inline int copy_to_sockptr_offset(sockptr_t dst, size_t offset,
107 const void *src, size_t size)
109 if (!sockptr_is_kernel(dst))
110 return copy_to_user(dst.user + offset, src, size);
111 memcpy(dst.kernel + offset, src, size);
115 static inline int copy_to_sockptr(sockptr_t dst, const void *src, size_t size)
117 return copy_to_sockptr_offset(dst, 0, src, size);
120 static inline void *memdup_sockptr(sockptr_t src, size_t len)
122 void *p = kmalloc_track_caller(len, GFP_USER | __GFP_NOWARN);
125 return ERR_PTR(-ENOMEM);
126 if (copy_from_sockptr(p, src, len)) {
128 return ERR_PTR(-EFAULT);
133 static inline void *memdup_sockptr_nul(sockptr_t src, size_t len)
135 char *p = kmalloc_track_caller(len + 1, GFP_KERNEL);
138 return ERR_PTR(-ENOMEM);
139 if (copy_from_sockptr(p, src, len)) {
141 return ERR_PTR(-EFAULT);
147 static inline long strncpy_from_sockptr(char *dst, sockptr_t src, size_t count)
149 if (sockptr_is_kernel(src)) {
150 size_t len = min(strnlen(src.kernel, count - 1) + 1, count);
152 memcpy(dst, src.kernel, len);
155 return strncpy_from_user(dst, src.user, count);
158 static inline int check_zeroed_sockptr(sockptr_t src, size_t offset,
161 if (!sockptr_is_kernel(src))
162 return check_zeroed_user(src.user + offset, size);
163 return memchr_inv(src.kernel + offset, 0, size) == NULL;
166 #endif /* _LINUX_SOCKPTR_H */