1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /* A pointer that can point to either kernel or userspace memory. */
3 #ifndef _LINUX_BPFPTR_H
4 #define _LINUX_BPFPTR_H
5
6 #include <linux/sockptr.h>
7
8 typedef sockptr_t bpfptr_t;
9
bpfptr_is_kernel(bpfptr_t bpfptr)10 static inline bool bpfptr_is_kernel(bpfptr_t bpfptr)
11 {
12 return bpfptr.is_kernel;
13 }
14
KERNEL_BPFPTR(void * p)15 static inline bpfptr_t KERNEL_BPFPTR(void *p)
16 {
17 return (bpfptr_t) { .kernel = p, .is_kernel = true };
18 }
19
USER_BPFPTR(void __user * p)20 static inline bpfptr_t USER_BPFPTR(void __user *p)
21 {
22 return (bpfptr_t) { .user = p };
23 }
24
make_bpfptr(u64 addr,bool is_kernel)25 static inline bpfptr_t make_bpfptr(u64 addr, bool is_kernel)
26 {
27 if (is_kernel)
28 return KERNEL_BPFPTR((void*) (uintptr_t) addr);
29 else
30 return USER_BPFPTR(u64_to_user_ptr(addr));
31 }
32
bpfptr_is_null(bpfptr_t bpfptr)33 static inline bool bpfptr_is_null(bpfptr_t bpfptr)
34 {
35 if (bpfptr_is_kernel(bpfptr))
36 return !bpfptr.kernel;
37 return !bpfptr.user;
38 }
39
bpfptr_add(bpfptr_t * bpfptr,size_t val)40 static inline void bpfptr_add(bpfptr_t *bpfptr, size_t val)
41 {
42 if (bpfptr_is_kernel(*bpfptr))
43 bpfptr->kernel += val;
44 else
45 bpfptr->user += val;
46 }
47
copy_from_bpfptr_offset(void * dst,bpfptr_t src,size_t offset,size_t size)48 static inline int copy_from_bpfptr_offset(void *dst, bpfptr_t src,
49 size_t offset, size_t size)
50 {
51 if (!bpfptr_is_kernel(src))
52 return copy_from_user(dst, src.user + offset, size);
53 return copy_from_kernel_nofault(dst, src.kernel + offset, size);
54 }
55
copy_from_bpfptr(void * dst,bpfptr_t src,size_t size)56 static inline int copy_from_bpfptr(void *dst, bpfptr_t src, size_t size)
57 {
58 return copy_from_bpfptr_offset(dst, src, 0, size);
59 }
60
copy_to_bpfptr_offset(bpfptr_t dst,size_t offset,const void * src,size_t size)61 static inline int copy_to_bpfptr_offset(bpfptr_t dst, size_t offset,
62 const void *src, size_t size)
63 {
64 return copy_to_sockptr_offset((sockptr_t) dst, offset, src, size);
65 }
66
kvmemdup_bpfptr(bpfptr_t src,size_t len)67 static inline void *kvmemdup_bpfptr(bpfptr_t src, size_t len)
68 {
69 void *p = kvmalloc(len, GFP_USER | __GFP_NOWARN);
70
71 if (!p)
72 return ERR_PTR(-ENOMEM);
73 if (copy_from_bpfptr(p, src, len)) {
74 kvfree(p);
75 return ERR_PTR(-EFAULT);
76 }
77 return p;
78 }
79
strncpy_from_bpfptr(char * dst,bpfptr_t src,size_t count)80 static inline long strncpy_from_bpfptr(char *dst, bpfptr_t src, size_t count)
81 {
82 if (bpfptr_is_kernel(src))
83 return strncpy_from_kernel_nofault(dst, src.kernel, count);
84 return strncpy_from_user(dst, src.user, count);
85 }
86
87 #endif /* _LINUX_BPFPTR_H */
88