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 * Returns 0 for success, or number of bytes not copied on error.
59 static inline int copy_from_sockptr(void *dst
, sockptr_t src
, size_t size
)
61 return copy_from_sockptr_offset(dst
, src
, 0, size
);
65 * copy_safe_from_sockptr: copy a struct from sockptr
66 * @dst: Destination address, in kernel space. This buffer must be @ksize
68 * @ksize: Size of @dst struct.
69 * @optval: Source address. (in user or kernel space)
70 * @optlen: Size of @optval data.
73 * * -EINVAL: @optlen < @ksize
74 * * -EFAULT: access to userspace failed.
75 * * 0 : @ksize bytes were copied
77 static inline int copy_safe_from_sockptr(void *dst
, size_t ksize
,
78 sockptr_t optval
, unsigned int optlen
)
82 if (copy_from_sockptr(dst
, optval
, ksize
))
87 static inline int copy_struct_from_sockptr(void *dst
, size_t ksize
,
88 sockptr_t src
, size_t usize
)
90 size_t size
= min(ksize
, usize
);
91 size_t rest
= max(ksize
, usize
) - size
;
93 if (!sockptr_is_kernel(src
))
94 return copy_struct_from_user(dst
, ksize
, src
.user
, size
);
97 memset(dst
+ size
, 0, rest
);
98 } else if (usize
> ksize
) {
106 memcpy(dst
, src
.kernel
, size
);
110 static inline int copy_to_sockptr_offset(sockptr_t dst
, size_t offset
,
111 const void *src
, size_t size
)
113 if (!sockptr_is_kernel(dst
))
114 return copy_to_user(dst
.user
+ offset
, src
, size
);
115 memcpy(dst
.kernel
+ offset
, src
, size
);
119 static inline int copy_to_sockptr(sockptr_t dst
, const void *src
, size_t size
)
121 return copy_to_sockptr_offset(dst
, 0, src
, size
);
124 static inline void *memdup_sockptr_noprof(sockptr_t src
, size_t len
)
126 void *p
= kmalloc_track_caller_noprof(len
, GFP_USER
| __GFP_NOWARN
);
129 return ERR_PTR(-ENOMEM
);
130 if (copy_from_sockptr(p
, src
, len
)) {
132 return ERR_PTR(-EFAULT
);
136 #define memdup_sockptr(...) alloc_hooks(memdup_sockptr_noprof(__VA_ARGS__))
138 static inline void *memdup_sockptr_nul_noprof(sockptr_t src
, size_t len
)
140 char *p
= kmalloc_track_caller_noprof(len
+ 1, GFP_KERNEL
);
143 return ERR_PTR(-ENOMEM
);
144 if (copy_from_sockptr(p
, src
, len
)) {
146 return ERR_PTR(-EFAULT
);
151 #define memdup_sockptr_nul(...) alloc_hooks(memdup_sockptr_nul_noprof(__VA_ARGS__))
153 static inline long strncpy_from_sockptr(char *dst
, sockptr_t src
, size_t count
)
155 if (sockptr_is_kernel(src
)) {
156 size_t len
= min(strnlen(src
.kernel
, count
- 1) + 1, count
);
158 memcpy(dst
, src
.kernel
, len
);
161 return strncpy_from_user(dst
, src
.user
, count
);
164 static inline int check_zeroed_sockptr(sockptr_t src
, size_t offset
,
167 if (!sockptr_is_kernel(src
))
168 return check_zeroed_user(src
.user
+ offset
, size
);
169 return memchr_inv(src
.kernel
+ offset
, 0, size
) == NULL
;
172 #endif /* _LINUX_SOCKPTR_H */