Add user_ptr_is_valid() and user_ptr_set_addr() helpers to operate on user pointers in different situations in subsequent commits.
* user_ptr_is_valid() validates the user pointer by fetching the tag.
* user_ptr_set_addr() sets the address field of the user pointer.
Both of the above helpers use CHERI compiler builtins for PCuABI case.
Signed-off-by: Amit Daniel Kachhap amitdaniel.kachhap@arm.com --- Documentation/core-api/user_ptr.rst | 12 +++++++++++ include/linux/user_ptr.h | 33 +++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+)
diff --git a/Documentation/core-api/user_ptr.rst b/Documentation/core-api/user_ptr.rst index 9db5e9271578..1427c4701af8 100644 --- a/Documentation/core-api/user_ptr.rst +++ b/Documentation/core-api/user_ptr.rst @@ -222,6 +222,18 @@ equal without being identical. To check whether two user pointers are truly identical, ``user_ptr_is_same(p1, p2)`` (``<linux/user_ptr.h>``) should be used.
+Validity +---------- + +To check whether a user pointer is valid, +``user_ptr_is_valid(p)`` (``<linux/user_ptr.h>``) should be used. + +Setting the address +------------------- + +To set the address field of the user pointers, +``user_ptr_set_addr(p)`` (``<linux/user_ptr.h>``) should be used. + Alignment ---------
diff --git a/include/linux/user_ptr.h b/include/linux/user_ptr.h index d663c6105d54..8cf69280bfcc 100644 --- a/include/linux/user_ptr.h +++ b/include/linux/user_ptr.h @@ -226,4 +226,37 @@ static inline bool user_ptr_is_same(const void __user *p1, const void __user *p2 #endif }
+/** + * user_ptr_is_valid() - Checks if the user pointer is valid. + * @ptr: The user pointer to check. + * + * Return: true if @ptr is valid. + * + * This function returns the tag of user pointer @ptr. + */ +static inline bool user_ptr_is_valid(const void __user *ptr) +{ +#ifdef CONFIG_CHERI_PURECAP_UABI + return __builtin_cheri_tag_get(ptr); +#else + return 0; +#endif +} + +/** + * user_ptr_set_addr() - Sets the address of the user pointer. + * @ptr: The user pointer to set address. + * @addr: The address to set the pointer to. + * + * Return: A user pointer with its address set to @addr. + */ +static inline void __user *user_ptr_set_addr(void __user *ptr, ptraddr_t addr) +{ +#ifdef CONFIG_CHERI_PURECAP_UABI + return __builtin_cheri_address_set(ptr, addr); +#else + return as_user_ptr(addr); +#endif +} + #endif /* _LINUX_USER_PTR_H */