1 /* SPDX-License-Identifier: (GPL-2.0-only or LGPL-2.1-only)
5 * wrapper around linux/uaccess.h.
7 * Copyright (C) 2019 Michael Jeanson <mjeanson@efficios.com>
10 #ifndef _LTTNG_WRAPPER_UACCESS_H
11 #define _LTTNG_WRAPPER_UACCESS_H
13 #include <linux/uaccess.h>
14 #include <wrapper/bitops.h>
15 #include <lttng/kernel-version.h>
17 #if (LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,0,0) || \
18 LTTNG_RHEL_KERNEL_RANGE(4,18,0,147,0,0, 4,19,0,0,0,0))
21 #define VERIFY_WRITE 1
22 #define lttng_access_ok(type, addr, size) access_ok(addr, size)
24 #else /* LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,0,0) */
26 #define lttng_access_ok(type, addr, size) access_ok(type, addr, size)
28 #endif /* LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,0,0) */
30 #if LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,4,0)
31 static __always_inline __must_check
int
32 lttng_copy_struct_from_user(void *dst
, size_t ksize
, const void __user
*src
,
35 return copy_struct_from_user(dst
, ksize
, src
, usize
);
37 #else /* LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,4,0) */
39 * lttng_check_zeroed_user: check if a userspace buffer only contains zero bytes
40 * @from: Source address, in userspace.
41 * @size: Size of buffer.
43 * This is effectively shorthand for "memchr_inv(from, 0, size) == NULL" for
44 * userspace addresses (and is more efficient because we don't care where the
45 * first non-zero byte is).
48 * * 0: There were non-zero bytes present in the buffer.
49 * * 1: The buffer was full of zero bytes.
50 * * -EFAULT: access to userspace failed.
53 int lttng_check_zeroed_user(const void __user
*from
, size_t size
)
56 uintptr_t align
= (uintptr_t) from
% sizeof(unsigned long);
59 if (unlikely(size
== 0))
65 if (!lttng_access_ok(VERIFY_READ
, from
, size
))
68 ret
= get_user(val
, (unsigned long __user
*) from
);
72 val
&= ~lttng_aligned_byte_mask(align
);
74 while (size
> sizeof(unsigned long)) {
78 from
+= sizeof(unsigned long);
79 size
-= sizeof(unsigned long);
81 ret
= get_user(val
, (unsigned long __user
*) from
);
86 if (size
< sizeof(unsigned long))
87 val
&= lttng_aligned_byte_mask(size
);
93 static __always_inline __must_check
int
94 lttng_copy_struct_from_user(void *dst
, size_t ksize
, const void __user
*src
,
97 size_t size
= min(ksize
, usize
);
98 size_t rest
= max(ksize
, usize
) - size
;
100 /* Deal with trailing bytes. */
102 memset(dst
+ size
, 0, rest
);
103 } else if (usize
> ksize
) {
104 int ret
= lttng_check_zeroed_user(src
+ size
, rest
);
106 return ret
?: -E2BIG
;
108 /* Copy the interoperable parts of the struct. */
109 if (copy_from_user(dst
, src
, size
))
113 #endif /* LTTNG_LINUX_VERSION_CODE >= LTTNG_KERNEL_VERSION(5,4,0) */
115 #endif /* _LTTNG_WRAPPER_UACCESS_H */